VLCMediaList.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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:@[@{@"media": [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_deleted.item],
  65. @"index": @(event->u.media_list_item_deleted.index)}]];
  66. }
  67. }
  68. @interface VLCMediaList()
  69. {
  70. void * p_mlist; ///< Internal instance of media list
  71. id <VLCMediaListDelegate,NSObject> __weak delegate; ///< Delegate object
  72. /* We need that private copy because of Cocoa Bindings, that need to be working on first thread */
  73. NSMutableArray *_mediaObjects; ///< Private copy of media objects.
  74. dispatch_queue_t _serialMediaObjectsQueue; ///< Queue for accessing and modifying the mediaobjects
  75. }
  76. @end
  77. @implementation VLCMediaList
  78. - (instancetype)init
  79. {
  80. if (self = [super init]) {
  81. // Create a new libvlc media list instance
  82. p_mlist = libvlc_media_list_new([VLCLibrary sharedLibrary].instance);
  83. // Initialize internals to defaults
  84. _mediaObjects = [[NSMutableArray alloc] init];
  85. _serialMediaObjectsQueue = dispatch_queue_create("org.videolan.serialMediaObjectsQueue", DISPATCH_QUEUE_SERIAL);
  86. [self initInternalMediaList];
  87. }
  88. return self;
  89. }
  90. - (instancetype)initWithArray:(NSArray *)array
  91. {
  92. if (self = [self init]) {
  93. /* do something useful with the provided array */
  94. NSUInteger count = [array count];
  95. for (NSUInteger x = 0; x < count; x++)
  96. [self addMedia:array[x]];
  97. }
  98. return self;
  99. }
  100. - (void)dealloc
  101. {
  102. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  103. if (em) {
  104. libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  105. libvlc_event_detach(em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  106. }
  107. [[VLCEventManager sharedManager] cancelCallToObject:self];
  108. // Release allocated memory
  109. delegate = nil;
  110. libvlc_media_list_release( p_mlist );
  111. }
  112. - (NSString *)description
  113. {
  114. NSMutableString * content = [NSMutableString string];
  115. for (NSInteger i = 0; i < [self count]; i++) {
  116. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  117. }
  118. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
  119. }
  120. - (void)lock
  121. {
  122. libvlc_media_list_lock( p_mlist );
  123. }
  124. - (void)unlock
  125. {
  126. libvlc_media_list_unlock( p_mlist );
  127. }
  128. - (NSUInteger)addMedia:(VLCMedia *)media
  129. {
  130. NSInteger index = [self count];
  131. [self insertMedia:media atIndex:index];
  132. return index;
  133. }
  134. - (void)insertMedia:(VLCMedia *)media atIndex: (NSUInteger)index
  135. {
  136. // Add the media object to our cache
  137. dispatch_sync(_serialMediaObjectsQueue, ^{
  138. [_mediaObjects insertObject:media atIndex:index];
  139. });
  140. // Add it to libvlc's medialist
  141. libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], (int)index);
  142. }
  143. - (BOOL)removeMediaAtIndex:(NSUInteger)index
  144. {
  145. __block BOOL ok = YES;
  146. dispatch_sync(_serialMediaObjectsQueue, ^{
  147. // Remove from cached Media
  148. if (index >= [_mediaObjects count]) {
  149. ok = NO;
  150. return;
  151. }
  152. [_mediaObjects removeObjectAtIndex:index];
  153. });
  154. // Remove from libvlc's medialist
  155. libvlc_media_list_remove_index(p_mlist, (int)index);
  156. return ok;
  157. }
  158. - (VLCMedia *)mediaAtIndex:(NSUInteger)index
  159. {
  160. __block VLCMedia *media;
  161. dispatch_sync(_serialMediaObjectsQueue, ^{
  162. media = index >= [_mediaObjects count] ? nil : [_mediaObjects objectAtIndex:index];
  163. });
  164. return media;
  165. }
  166. - (NSUInteger)indexOfMedia:(VLCMedia *)media
  167. {
  168. return [_mediaObjects indexOfObject:media];
  169. }
  170. /* KVC Compliance: For the @"media" key */
  171. - (NSInteger)countOfMedia
  172. {
  173. return [self count];
  174. }
  175. - (VLCMedia *)objectInMediaAtIndex:(NSUInteger)i
  176. {
  177. return [self mediaAtIndex:i];
  178. }
  179. - (NSInteger)count
  180. {
  181. __block NSInteger count;
  182. dispatch_sync(_serialMediaObjectsQueue, ^{
  183. count = [_mediaObjects count];
  184. });
  185. return count;
  186. }
  187. - (void)insertObject:(VLCMedia *)object inMediaAtIndex:(NSUInteger)i
  188. {
  189. [self insertMedia:object atIndex: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. /* We need the caller to wait until this block is done.
  236. * The initialized object shall not be returned until the event attachments are done. */
  237. dispatch_sync(_serialMediaObjectsQueue,^{
  238. libvlc_event_attach( em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  239. libvlc_event_attach( em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  240. });
  241. }
  242. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  243. {
  244. /* We hope to receive index in a nide range, that could change one day */
  245. NSInteger start = [arrayOfArgs[0][@"index"] intValue];
  246. NSInteger end = [arrayOfArgs[[arrayOfArgs count]-1][@"index"] intValue];
  247. NSRange range = NSMakeRange(start, end-start);
  248. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  249. for (NSDictionary *args in arrayOfArgs) {
  250. NSInteger index = [args[@"index"] intValue];
  251. VLCMedia *tempMedia = args[@"media"];
  252. __block VLCMedia *foundMedia;
  253. //we have two instances of VLCMedia. One from the event and the one we added to _mediaObjects, hence check them to avoid duplication
  254. dispatch_sync(_serialMediaObjectsQueue, ^{
  255. for (VLCMedia *media in _mediaObjects) {
  256. if ([tempMedia.url isEqual:media.url]){
  257. foundMedia = media;
  258. break;
  259. }
  260. }
  261. });
  262. if (!foundMedia) {
  263. // In case we found Media on the network we don't have a cached copy yet
  264. foundMedia = tempMedia;
  265. dispatch_sync(_serialMediaObjectsQueue, ^{
  266. index >= [_mediaObjects count] ? [_mediaObjects addObject:foundMedia] : [_mediaObjects insertObject:foundMedia atIndex:index];
  267. });
  268. }
  269. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  270. [delegate mediaList:self mediaAdded:foundMedia atIndex:index];
  271. }
  272. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  273. }
  274. - (void)mediaListItemRemoved:(NSArray *)arguments
  275. {
  276. NSNumber *index = arguments.firstObject[@"index"];
  277. VLCMedia *deleted = arguments.firstObject[@"media"];
  278. [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  279. dispatch_sync(_serialMediaObjectsQueue, ^{
  280. if ([_mediaObjects containsObject:deleted]) {
  281. [_mediaObjects removeObject:deleted];
  282. }
  283. });
  284. [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  285. // Post the notification
  286. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  287. object:self
  288. userInfo:@{@"index": index}];
  289. // Let the delegate know that the item is being removed
  290. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  291. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  292. }
  293. @end