VLCMediaList.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. dispatch_queue_attr_t qosAttribute = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
  86. QOS_CLASS_USER_INITIATED,
  87. 0);
  88. _serialMediaObjectsQueue = dispatch_queue_create("org.videolan.serialMediaObjectsQueue", qosAttribute);
  89. [self initInternalMediaList];
  90. }
  91. return self;
  92. }
  93. - (instancetype)initWithArray:(NSArray *)array
  94. {
  95. if (self = [self init]) {
  96. /* do something useful with the provided array */
  97. NSUInteger count = [array count];
  98. for (NSUInteger x = 0; x < count; x++)
  99. [self addMedia:array[x]];
  100. }
  101. return self;
  102. }
  103. - (void)dealloc
  104. {
  105. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  106. if (em) {
  107. libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  108. libvlc_event_detach(em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  109. }
  110. [[VLCEventManager sharedManager] cancelCallToObject:self];
  111. // Release allocated memory
  112. delegate = nil;
  113. libvlc_media_list_release( p_mlist );
  114. }
  115. - (NSString *)description
  116. {
  117. NSMutableString * content = [NSMutableString string];
  118. for (NSInteger i = 0; i < [self count]; i++) {
  119. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  120. }
  121. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
  122. }
  123. - (void)lock
  124. {
  125. libvlc_media_list_lock( p_mlist );
  126. }
  127. - (void)unlock
  128. {
  129. libvlc_media_list_unlock( p_mlist );
  130. }
  131. - (NSUInteger)addMedia:(VLCMedia *)media
  132. {
  133. NSInteger index = [self count];
  134. [self insertMedia:media atIndex:index];
  135. return index;
  136. }
  137. - (void)insertMedia:(VLCMedia *)media atIndex: (NSUInteger)index
  138. {
  139. // Add the media object to our cache
  140. dispatch_sync(_serialMediaObjectsQueue, ^{
  141. [_mediaObjects insertObject:media atIndex:index];
  142. });
  143. // Add it to libvlc's medialist
  144. libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], (int)index);
  145. }
  146. - (BOOL)removeMediaAtIndex:(NSUInteger)index
  147. {
  148. __block BOOL ok = YES;
  149. dispatch_sync(_serialMediaObjectsQueue, ^{
  150. // Remove from cached Media
  151. if (index >= [_mediaObjects count]) {
  152. ok = NO;
  153. return;
  154. }
  155. [_mediaObjects removeObjectAtIndex:index];
  156. });
  157. // Remove from libvlc's medialist
  158. libvlc_media_list_remove_index(p_mlist, (int)index);
  159. return ok;
  160. }
  161. - (VLCMedia *)mediaAtIndex:(NSUInteger)index
  162. {
  163. __block VLCMedia *media;
  164. dispatch_sync(_serialMediaObjectsQueue, ^{
  165. media = index >= [_mediaObjects count] ? nil : [_mediaObjects objectAtIndex:index];
  166. });
  167. return media;
  168. }
  169. - (NSUInteger)indexOfMedia:(VLCMedia *)media
  170. {
  171. return [_mediaObjects indexOfObject:media];
  172. }
  173. /* KVC Compliance: For the @"media" key */
  174. - (NSInteger)countOfMedia
  175. {
  176. return [self count];
  177. }
  178. - (VLCMedia *)objectInMediaAtIndex:(NSUInteger)i
  179. {
  180. return [self mediaAtIndex:i];
  181. }
  182. - (NSInteger)count
  183. {
  184. __block NSInteger count;
  185. dispatch_sync(_serialMediaObjectsQueue, ^{
  186. count = [_mediaObjects count];
  187. });
  188. return count;
  189. }
  190. - (void)insertObject:(VLCMedia *)object inMediaAtIndex:(NSUInteger)i
  191. {
  192. [self insertMedia:object atIndex:i];
  193. }
  194. @synthesize delegate;
  195. - (BOOL)isReadOnly
  196. {
  197. return libvlc_media_list_is_readonly( p_mlist );
  198. }
  199. @end
  200. @implementation VLCMediaList (LibVLCBridging)
  201. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  202. {
  203. return [[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist];
  204. }
  205. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  206. {
  207. if (self = [super init]) {
  208. p_mlist = p_new_mlist;
  209. libvlc_media_list_retain( p_mlist );
  210. libvlc_media_list_lock( p_mlist );
  211. _mediaObjects = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
  212. _serialMediaObjectsQueue = dispatch_queue_create("org.videolan.serialMediaObjectsQueue", NULL);
  213. NSUInteger count = libvlc_media_list_count(p_mlist);
  214. for (int i = 0; i < count; i++) {
  215. libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
  216. dispatch_sync(_serialMediaObjectsQueue, ^{
  217. [_mediaObjects addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
  218. });
  219. libvlc_media_release(p_md);
  220. }
  221. [self initInternalMediaList];
  222. libvlc_media_list_unlock(p_mlist);
  223. }
  224. return self;
  225. }
  226. - (void *)libVLCMediaList
  227. {
  228. return p_mlist;
  229. }
  230. @end
  231. @implementation VLCMediaList (Private)
  232. - (void)initInternalMediaList
  233. {
  234. // Add event callbacks
  235. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  236. if (!em)
  237. return;
  238. /* We need the caller to wait until this block is done.
  239. * The initialized object shall not be returned until the event attachments are done. */
  240. dispatch_sync(_serialMediaObjectsQueue,^{
  241. libvlc_event_attach( em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  242. libvlc_event_attach( em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  243. });
  244. }
  245. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  246. {
  247. /* We hope to receive index in a nide range, that could change one day */
  248. NSInteger start = [arrayOfArgs[0][@"index"] intValue];
  249. NSInteger end = [arrayOfArgs[[arrayOfArgs count]-1][@"index"] intValue];
  250. NSRange range = NSMakeRange(start, end-start);
  251. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  252. for (NSDictionary *args in arrayOfArgs) {
  253. NSInteger index = [args[@"index"] intValue];
  254. VLCMedia *tempMedia = args[@"media"];
  255. __block VLCMedia *foundMedia;
  256. //we have two instances of VLCMedia. One from the event and the one we added to _mediaObjects, hence check them to avoid duplication
  257. dispatch_sync(_serialMediaObjectsQueue, ^{
  258. for (VLCMedia *media in _mediaObjects) {
  259. if ([tempMedia.url isEqual:media.url]){
  260. foundMedia = media;
  261. break;
  262. }
  263. }
  264. });
  265. if (!foundMedia) {
  266. // In case we found Media on the network we don't have a cached copy yet
  267. foundMedia = tempMedia;
  268. dispatch_sync(_serialMediaObjectsQueue, ^{
  269. index >= [_mediaObjects count] ? [_mediaObjects addObject:foundMedia] : [_mediaObjects insertObject:foundMedia atIndex:index];
  270. });
  271. }
  272. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  273. [delegate mediaList:self mediaAdded:foundMedia atIndex:index];
  274. }
  275. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  276. }
  277. - (void)mediaListItemRemoved:(NSArray *)arguments
  278. {
  279. NSNumber *index = arguments.firstObject[@"index"];
  280. VLCMedia *deleted = arguments.firstObject[@"media"];
  281. [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  282. dispatch_sync(_serialMediaObjectsQueue, ^{
  283. if ([_mediaObjects containsObject:deleted]) {
  284. [_mediaObjects removeObject:deleted];
  285. }
  286. });
  287. [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  288. // Post the notification
  289. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  290. object:self
  291. userInfo:@{@"index": index}];
  292. // Let the delegate know that the item is being removed
  293. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  294. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  295. }
  296. @end