VLCMediaList.m 9.9 KB

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