VLCMediaList.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*****************************************************************************
  2. * VLCMediaList.m: VLCKit.framework VLCMediaList implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 VLC authors and VideoLAN
  6. * Copyright (C) 2009, 2013, 2017 Felix Paul Kühne
  7. * Copyright (C) 2018 Carola Nitz
  8. * $Id$
  9. *
  10. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  11. * Felix Paul Kühne <fkuehne # videolan.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26. *****************************************************************************/
  27. #import "VLCMediaList.h"
  28. #import "VLCLibrary.h"
  29. #import "VLCEventManager.h"
  30. #import "VLCLibVLCBridging.h"
  31. #ifdef HAVE_CONFIG_H
  32. # include "config.h"
  33. #endif
  34. #include <vlc/vlc.h>
  35. #include <vlc/libvlc.h>
  36. /* Notification Messages */
  37. NSString *const VLCMediaListItemAdded = @"VLCMediaListItemAdded";
  38. NSString *const VLCMediaListItemDeleted = @"VLCMediaListItemDeleted";
  39. // TODO: Documentation
  40. @interface VLCMediaList (Private)
  41. /* Initializers */
  42. - (void)initInternalMediaList;
  43. /* Libvlc event bridges */
  44. - (void)mediaListItemAdded:(NSArray *)args;
  45. - (void)mediaListItemRemoved:(NSNumber *)index;
  46. @end
  47. /* libvlc event callback */
  48. static void HandleMediaListItemAdded(const libvlc_event_t * event, void * user_data)
  49. {
  50. @autoreleasepool {
  51. id self = (__bridge id)(user_data);
  52. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  53. withMethod:@selector(mediaListItemAdded:)
  54. withArgumentAsObject:@[@{@"media": [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item],
  55. @"index": @(event->u.media_list_item_added.index)}]];
  56. }
  57. }
  58. static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
  59. {
  60. @autoreleasepool {
  61. id self = (__bridge id)(user_data);
  62. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  63. withMethod:@selector(mediaListItemRemoved:)
  64. withArgumentAsObject:@(event->u.media_list_item_deleted.index)];
  65. }
  66. }
  67. @interface VLCMediaList()
  68. {
  69. void * p_mlist; ///< Internal instance of media list
  70. id <VLCMediaListDelegate,NSObject> __weak delegate; ///< Delegate object
  71. /* We need that private copy because of Cocoa Bindings, that need to be working on first thread */
  72. NSMutableArray *_mediaObjects; ///< Private copy of media objects.
  73. dispatch_queue_t _serialMediaObjectsQueue; ///< Queue for accessing and modifying the mediaobjects
  74. }
  75. @end
  76. @implementation VLCMediaList
  77. - (instancetype)init
  78. {
  79. if (self = [super init]) {
  80. // Create a new libvlc media list instance
  81. p_mlist = libvlc_media_list_new([VLCLibrary sharedLibrary].instance);
  82. // Initialize internals to defaults
  83. _mediaObjects = [[NSMutableArray alloc] init];
  84. _serialMediaObjectsQueue = dispatch_queue_create("org.videolan.serialMediaObjectsQueue", DISPATCH_QUEUE_SERIAL);
  85. [self initInternalMediaList];
  86. }
  87. return self;
  88. }
  89. - (instancetype)initWithArray:(NSArray *)array
  90. {
  91. if (self = [self init]) {
  92. /* do something useful with the provided array */
  93. NSUInteger count = [array count];
  94. for (NSUInteger x = 0; x < count; x++)
  95. [self addMedia:array[x]];
  96. }
  97. return self;
  98. }
  99. - (void)dealloc
  100. {
  101. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  102. if (em) {
  103. libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  104. libvlc_event_detach(em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  105. }
  106. [[VLCEventManager sharedManager] cancelCallToObject:self];
  107. // Release allocated memory
  108. delegate = nil;
  109. libvlc_media_list_release( p_mlist );
  110. }
  111. - (NSString *)description
  112. {
  113. NSMutableString * content = [NSMutableString string];
  114. for (NSInteger i = 0; i < [self count]; i++) {
  115. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  116. }
  117. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
  118. }
  119. - (void)lock
  120. {
  121. libvlc_media_list_lock( p_mlist );
  122. }
  123. - (void)unlock
  124. {
  125. libvlc_media_list_unlock( p_mlist );
  126. }
  127. - (NSUInteger)addMedia:(VLCMedia *)media
  128. {
  129. NSInteger index = [self count];
  130. [self insertMedia:media atIndex:index];
  131. return index;
  132. }
  133. - (void)insertMedia:(VLCMedia *)media atIndex: (NSUInteger)index
  134. {
  135. // Add the media object to our cache
  136. dispatch_sync(_serialMediaObjectsQueue, ^{
  137. [_mediaObjects insertObject:media atIndex:index];
  138. });
  139. // Add it to libvlc's medialist
  140. libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], (int)index);
  141. }
  142. - (void)removeMediaAtIndex:(NSUInteger)index
  143. {
  144. dispatch_sync(_serialMediaObjectsQueue, ^{
  145. if (index >= [_mediaObjects count])
  146. return;
  147. //remove from cached Media
  148. [_mediaObjects removeObjectAtIndex:index];
  149. });
  150. // Remove it from libvlc's medialist
  151. libvlc_media_list_remove_index(p_mlist, (int)index);
  152. }
  153. - (VLCMedia *)mediaAtIndex:(NSUInteger)index
  154. {
  155. __block VLCMedia *media;
  156. dispatch_sync(_serialMediaObjectsQueue, ^{
  157. media = index >= [_mediaObjects count] ? nil : [_mediaObjects objectAtIndex:index];
  158. });
  159. return media;
  160. }
  161. - (NSInteger)indexOfMedia:(VLCMedia *)media
  162. {
  163. NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
  164. return result;
  165. }
  166. /* KVC Compliance: For the @"media" key */
  167. - (NSInteger)countOfMedia
  168. {
  169. return [self count];
  170. }
  171. - (VLCMedia *)objectInMediaAtIndex:(NSUInteger)i
  172. {
  173. return [self mediaAtIndex:i];
  174. }
  175. - (NSInteger)count
  176. {
  177. __block NSInteger count;
  178. dispatch_sync(_serialMediaObjectsQueue, ^{
  179. count = [_mediaObjects count];
  180. });
  181. return count;
  182. }
  183. - (void)insertObject:(VLCMedia *)object inMediaAtIndex:(NSUInteger)i
  184. {
  185. [self insertMedia:object atIndex:i];
  186. }
  187. - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
  188. {
  189. [self removeMediaAtIndex:i];
  190. }
  191. @synthesize delegate;
  192. - (BOOL)isReadOnly
  193. {
  194. return libvlc_media_list_is_readonly( p_mlist );
  195. }
  196. @end
  197. @implementation VLCMediaList (LibVLCBridging)
  198. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  199. {
  200. return [[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist];
  201. }
  202. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  203. {
  204. if (self = [super init]) {
  205. p_mlist = p_new_mlist;
  206. libvlc_media_list_retain( p_mlist );
  207. libvlc_media_list_lock( p_mlist );
  208. _mediaObjects = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
  209. _serialMediaObjectsQueue = dispatch_queue_create("org.videolan.serialMediaObjectsQueue", NULL);
  210. NSUInteger count = libvlc_media_list_count(p_mlist);
  211. for (int i = 0; i < count; i++) {
  212. libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
  213. dispatch_sync(_serialMediaObjectsQueue, ^{
  214. [_mediaObjects addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
  215. });
  216. libvlc_media_release(p_md);
  217. }
  218. [self initInternalMediaList];
  219. libvlc_media_list_unlock(p_mlist);
  220. }
  221. return self;
  222. }
  223. - (void *)libVLCMediaList
  224. {
  225. return p_mlist;
  226. }
  227. @end
  228. @implementation VLCMediaList (Private)
  229. - (void)initInternalMediaList
  230. {
  231. // Add event callbacks
  232. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  233. if (!em)
  234. return;
  235. libvlc_event_attach( em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  236. libvlc_event_attach( em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  237. }
  238. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  239. {
  240. /* We hope to receive index in a nide range, that could change one day */
  241. NSInteger start = [arrayOfArgs[0][@"index"] intValue];
  242. NSInteger end = [arrayOfArgs[[arrayOfArgs count]-1][@"index"] intValue];
  243. NSRange range = NSMakeRange(start, end-start);
  244. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  245. for (NSDictionary *args in arrayOfArgs) {
  246. NSInteger index = [args[@"index"] intValue];
  247. VLCMedia *tempMedia = args[@"media"];
  248. __block VLCMedia *foundMedia;
  249. //we have two instances of VLCMedia. One from the event and the one we added to _mediaObjects, hence check them to avoid duplication
  250. dispatch_sync(_serialMediaObjectsQueue, ^{
  251. for (VLCMedia *media in _mediaObjects) {
  252. if ([tempMedia.url isEqual:media.url]){
  253. foundMedia = media;
  254. break;
  255. }
  256. }
  257. });
  258. if (!foundMedia) {
  259. // In case we found Media on the network we don't have a cached copy yet
  260. foundMedia = tempMedia;
  261. dispatch_sync(_serialMediaObjectsQueue, ^{
  262. index >= [_mediaObjects count] ? [_mediaObjects addObject:foundMedia] : [_mediaObjects insertObject:foundMedia atIndex:index];
  263. });
  264. }
  265. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  266. [delegate mediaList:self mediaAdded:foundMedia atIndex:index];
  267. }
  268. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  269. }
  270. - (void)mediaListItemRemoved:(NSNumber *)index
  271. {
  272. [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  273. dispatch_sync(_serialMediaObjectsQueue, ^{
  274. [_mediaObjects removeObjectAtIndex:[index intValue]];
  275. });
  276. [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  277. // Post the notification
  278. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  279. object:self
  280. userInfo:@{@"index": index}];
  281. // Let the delegate know that the item is being removed
  282. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  283. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  284. }
  285. @end