MediaList.cs 12.7 KB
Newer Older
Martin Finkel's avatar
Martin Finkel committed
1 2 3 4
using System;
using System.Runtime.InteropServices;
using System.Security;

5
namespace LibVLCSharp.Shared
Martin Finkel's avatar
Martin Finkel committed
6 7 8 9
{
    public class MediaList : Internal
    {
        MediaListEventManager _eventManager;
10 11
        readonly object _syncLock = new object();
        bool _nativeLock;
Martin Finkel's avatar
Martin Finkel committed
12

13
        readonly struct Native
Martin Finkel's avatar
Martin Finkel committed
14
        {
15

16
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
17 18 19
                EntryPoint = "libvlc_media_subitems")]
            internal static extern IntPtr LibVLCMediaSubitems(IntPtr media);

20

21
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
22 23 24
                EntryPoint = "libvlc_media_list_new")]
            internal static extern IntPtr LibVLCMediaListNew(IntPtr instance);

25

26
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
27 28 29
                EntryPoint = "libvlc_media_list_release")]
            internal static extern void LibVLCMediaListRelease(IntPtr mediaList);

30

31
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
32 33 34
                EntryPoint = "libvlc_media_discoverer_media_list")]
            internal static extern IntPtr LibVLCMediaDiscovererMediaList(IntPtr discovererMediaList);

35

36
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
37 38 39
                EntryPoint = "libvlc_media_library_media_list")]
            internal static extern IntPtr LibVLCMediaLibraryMediaList(IntPtr libraryMediaList);

40

41
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
42 43 44
                EntryPoint = "libvlc_media_list_set_media")]
            internal static extern void LibVLCMediaListSetMedia(IntPtr mediaList, IntPtr media);

45

46
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
47 48 49
                EntryPoint = "libvlc_media_list_add_media")]
            internal static extern int LibVLCMediaListAddMedia(IntPtr mediaList, IntPtr media);

50

51
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
52 53 54
                EntryPoint = "libvlc_media_list_insert_media")]
            internal static extern int LibVLCMediaListInsertMedia(IntPtr mediaList, IntPtr media, int positionIndex);

55

56
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
57 58 59
                EntryPoint = "libvlc_media_list_remove_index")]
            internal static extern int LibVLCMediaListRemoveIndex(IntPtr mediaList, int positionIndex);

60

61
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
62 63 64
                EntryPoint = "libvlc_media_list_count")]
            internal static extern int LibVLCMediaListCount(IntPtr mediaList);

65

66
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
67 68 69
                EntryPoint = "libvlc_media_list_item_at_index")]
            internal static extern IntPtr LibVLCMediaListItemAtIndex(IntPtr mediaList, int positionIndex);

70

71
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
72 73 74
                EntryPoint = "libvlc_media_list_index_of_item")]
            internal static extern int LibVLCMediaListIndexOfItem(IntPtr mediaList, IntPtr media);

75

76
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
77 78 79
                EntryPoint = "libvlc_media_list_is_readonly")]
            internal static extern int LibVLCMediaListIsReadonly(IntPtr mediaList);

80

81
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
82 83 84
                EntryPoint = "libvlc_media_list_lock")]
            internal static extern void LibVLCMediaListLock(IntPtr mediaList);
            
85

86
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
87 88 89
                EntryPoint = "libvlc_media_list_unlock")]
            internal static extern void LibVLCMediaListUnlock(IntPtr mediaList);

90

91
            [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
Martin Finkel's avatar
Martin Finkel committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                EntryPoint = "libvlc_media_list_event_manager")]
            internal static extern IntPtr LibVLCMediaListEventManager(IntPtr mediaList);
        }

        /// <summary>
        /// Get subitems of media descriptor object.
        /// </summary>
        /// <param name="media"></param>
        public MediaList(Media media)
            : base(() => Native.LibVLCMediaSubitems(media.NativeReference), Native.LibVLCMediaListRelease)
        {
        }

        /// <summary>
        /// Get media service discover media list.
        /// </summary>
        /// <param name="mediaDiscoverer"></param>
        public MediaList(MediaDiscoverer mediaDiscoverer)
Martin Finkel's avatar
Martin Finkel committed
110
            : base(() => Native.LibVLCMediaDiscovererMediaList(mediaDiscoverer.NativeReference),
Martin Finkel's avatar
Martin Finkel committed
111 112 113 114 115 116 117
                Native.LibVLCMediaListRelease)
        {
        }

        /// <summary>
        /// Create an empty media list.
        /// </summary>
Martin Finkel's avatar
Martin Finkel committed
118 119 120
        /// <param name="libVLC"></param>
        public MediaList(LibVLC libVLC)
            : base(() => Native.LibVLCMediaListNew(libVLC.NativeReference), Native.LibVLCMediaListRelease)
Martin Finkel's avatar
Martin Finkel committed
121 122 123 124 125 126 127 128 129 130 131 132
        {
        }
        public MediaList(IntPtr mediaListPtr) : base(() => mediaListPtr, Native.LibVLCMediaListRelease)
        {
        }

        /// <summary>
        /// Associate media instance with this media list instance. If another
        /// media instance was present it will be released. The
        /// MediaList lock should NOT be held upon entering this function.
        /// </summary>
        /// <param name="media">media instance to add</param>
133 134 135 136
        public void SetMedia(Media media)
        {
            Native.LibVLCMediaListSetMedia(NativeReference, media.NativeReference);
        } 
Martin Finkel's avatar
Martin Finkel committed
137 138 139 140 141 142

        /// <summary>
        /// Add media instance to media list The MediaList lock should be held upon entering this function.
        /// </summary>
        /// <param name="media">a media instance</param>
        /// <returns></returns>
143 144 145 146 147 148 149 150 151
        public bool AddMedia(Media media)
        {
            lock (_syncLock)
            {
                if(!_nativeLock)
                    throw new InvalidOperationException("not locked");
                return Native.LibVLCMediaListAddMedia(NativeReference, media.NativeReference) == 0;
            }
        }
Martin Finkel's avatar
Martin Finkel committed
152 153 154 155 156 157 158 159

        /// <summary>
        /// Insert media instance in media list on a position The
        /// MediaList lock should be held upon entering this function.
        /// </summary>
        /// <param name="media">a media instance</param>
        /// <param name="position">position in array where to insert</param>
        /// <returns></returns>
160 161 162 163 164 165 166 167 168
        public bool InsertMedia(Media media, int position)
        {
            lock (_syncLock)
            {
                if(!_nativeLock)
                    throw new InvalidOperationException("not locked");
                return Native.LibVLCMediaListInsertMedia(NativeReference, media.NativeReference, position) == 0;
            }
        }
Martin Finkel's avatar
Martin Finkel committed
169 170 171 172 173 174 175 176

        /// <summary>
        /// Remove media instance from media list on a position The
        /// MediaList lock should be held upon entering this function.
        /// </summary>
        /// <param name="positionIndex">position in array where to insert</param>
        /// <returns></returns>
        public bool RemoveIndex(int positionIndex)
177 178 179 180 181 182 183 184
        {
            lock (_syncLock)
            {
                if (!_nativeLock)
                    throw new InvalidOperationException("not locked");
                return Native.LibVLCMediaListRemoveIndex(NativeReference, positionIndex) == 0;
            }
        }
Martin Finkel's avatar
Martin Finkel committed
185 186 187 188 189

        /// <summary>
        /// Get count on media list items The MediaList lock should be
        /// held upon entering this function.
        /// </summary>
190 191 192 193 194 195 196 197 198 199 200 201
        public int Count
        {
            get
            {
                lock (_syncLock)
                {
                    if(!_nativeLock)
                        throw new InvalidOperationException("not locked");
                    return Native.LibVLCMediaListCount(NativeReference);
                }
            }
        } 
Martin Finkel's avatar
Martin Finkel committed
202 203 204 205 206 207 208 209 210 211 212

        /// <summary>
        /// List media instance in media list at a position The
        /// MediaList lock should be held upon entering this function.
        /// </summary>
        /// <param name="position">position in array where to insert</param>
        /// <returns>media instance at position, or null if not found.</returns>
        public Media this[int position]
        {
            get
            {
213 214 215 216 217 218 219
                lock (_syncLock)
                {
                    if(!_nativeLock)
                        throw new InvalidOperationException("not locked");
                    var ptr = Native.LibVLCMediaListItemAtIndex(NativeReference, position);
                    return ptr == IntPtr.Zero ? null : new Media(ptr);
                }
Martin Finkel's avatar
Martin Finkel committed
220 221 222 223 224 225 226 227 228 229
            }
        }

        /// <summary>
        /// Find index position of List media instance in media list. Warning: the
        /// function will return the first matched position. The
        /// MediaList lock should be held upon entering this function.
        /// </summary>
        /// <param name="media">media instance</param>
        /// <returns>position of media instance or -1 if media not found</returns>
230 231 232 233 234 235 236 237 238
        public int IndexOf(Media media)
        {
            lock (_syncLock)
            {
                if (!_nativeLock)
                    throw new InvalidOperationException("not locked");
                return Native.LibVLCMediaListIndexOfItem(NativeReference, media.NativeReference);
            }
        } 
Martin Finkel's avatar
Martin Finkel committed
239 240 241 242 243 244 245 246 247 248

        /// <summary>
        /// This indicates if this media list is read-only from a user point of view
        /// true if readonly, false otherwise
        /// </summary>
        public bool IsReadonly => Native.LibVLCMediaListIsReadonly(NativeReference) == 1;

        /// <summary>
        /// Get lock on media list items
        /// </summary>
249 250 251 252 253 254 255 256 257 258 259
        public void Lock()
        {
            lock (_syncLock)
            {
                if (_nativeLock)
                    throw new InvalidOperationException("already locked");

                _nativeLock = true;
                Native.LibVLCMediaListLock(NativeReference);
            }
        }
Martin Finkel's avatar
Martin Finkel committed
260 261 262 263

        /// <summary>
        /// Release lock on media list items The MediaList lock should be held upon entering this function.
        /// </summary>
264 265 266 267 268 269 270 271 272 273 274
        public void Unlock()
        {
            lock (_syncLock)
            {
                if (!_nativeLock)
                    throw new InvalidOperationException("not locked");

                _nativeLock = false;
                Native.LibVLCMediaListUnlock(NativeReference);
            }
        }
Martin Finkel's avatar
Martin Finkel committed
275 276 277 278 279

        /// <summary>
        /// Get libvlc_event_manager from this media list instance. The
        /// p_event_manager is immutable, so you don't have to hold the lock
        /// </summary>
Martin Finkel's avatar
Martin Finkel committed
280
        MediaListEventManager EventManager
Martin Finkel's avatar
Martin Finkel committed
281 282 283 284 285 286 287 288 289
        {
            get
            {
                if (_eventManager != null) return _eventManager;
                var ptr = Native.LibVLCMediaListEventManager(NativeReference);
                _eventManager = new MediaListEventManager(ptr);
                return _eventManager;
            }
        }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

        #region Events

        public event EventHandler<MediaListItemAddedEventArgs> ItemAdded
        {
            add => EventManager.AttachEvent(EventType.MediaListItemAdded, value);
            remove => EventManager.DetachEvent(EventType.MediaListItemAdded, value);
        }

        public event EventHandler<MediaListWillAddItemEventArgs> WillAddItem
        {
            add => EventManager.AttachEvent(EventType.MediaListWillAddItem, value);
            remove => EventManager.DetachEvent(EventType.MediaListWillAddItem, value);
        }

        public event EventHandler<MediaListItemDeletedEventArgs> ItemDeleted
        {
            add => EventManager.AttachEvent(EventType.MediaListItemDeleted, value);
            remove => EventManager.DetachEvent(EventType.MediaListItemDeleted, value);
        }

        public event EventHandler<MediaListWillDeleteItemEventArgs> WillDeleteItem
        {
            add => EventManager.AttachEvent(EventType.MediaListWillDeleteItem, value);
            remove => EventManager.DetachEvent(EventType.MediaListWillDeleteItem, value);
        }

        public event EventHandler<EventArgs> EndReached
        {
            add => EventManager.AttachEvent(EventType.MediaListEndReached, value);
            remove => EventManager.DetachEvent(EventType.MediaListEndReached, value);
        }

        #endregion 
324 325 326 327 328 329 330 331

        protected override void Dispose(bool disposing)
        {
            if (IsDisposed || NativeReference == IntPtr.Zero)
                return;

            base.Dispose(disposing);
        }
Martin Finkel's avatar
Martin Finkel committed
332 333
    }
}