VLCMediaList.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  100. libvlc_event_detach(em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  101. [[VLCEventManager sharedManager] cancelCallToObject:self];
  102. // Release allocated memory
  103. delegate = nil;
  104. libvlc_media_list_release( p_mlist );
  105. }
  106. - (NSString *)description
  107. {
  108. NSMutableString * content = [NSMutableString string];
  109. for (NSInteger i = 0; i < [self count]; i++) {
  110. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  111. }
  112. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
  113. }
  114. - (void)lock
  115. {
  116. libvlc_media_list_lock( p_mlist );
  117. }
  118. - (void)unlock
  119. {
  120. libvlc_media_list_unlock( p_mlist );
  121. }
  122. - (NSInteger)addMedia:(VLCMedia *)media
  123. {
  124. NSInteger index = [self count];
  125. [self insertMedia:media atIndex:index];
  126. return index;
  127. }
  128. - (void)insertMedia:(VLCMedia *)media atIndex: (NSInteger)index
  129. {
  130. // Add it to libvlc's medialist
  131. libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], (int)index);
  132. // we will add the media object to cachedMedia in the callback when libvlc acknowledges its addition
  133. }
  134. - (void)removeMediaAtIndex:(NSInteger)index
  135. {
  136. if (index >= [cachedMedia count])
  137. return;
  138. // Remove it from libvlc's medialist
  139. libvlc_media_list_remove_index(p_mlist, (int)index);
  140. // we will remove the media object cachedMedia when libvlc acknowledges that it's gone
  141. }
  142. - (VLCMedia *)mediaAtIndex:(NSInteger)index
  143. {
  144. if (index < [cachedMedia count])
  145. return cachedMedia[index];
  146. return nil;
  147. }
  148. - (NSInteger)indexOfMedia:(VLCMedia *)media
  149. {
  150. NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
  151. return result;
  152. }
  153. /* KVC Compliance: For the @"media" key */
  154. - (NSInteger)countOfMedia
  155. {
  156. return [self count];
  157. }
  158. - (id)objectInMediaAtIndex:(NSUInteger)i
  159. {
  160. return [self mediaAtIndex:i];
  161. }
  162. - (NSInteger)count
  163. {
  164. return [cachedMedia count];
  165. }
  166. - (void)insertObject:(id)object inMediaAtIndex:(NSUInteger)i
  167. {
  168. [self insertMedia:object atIndex:i];
  169. }
  170. - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
  171. {
  172. [self removeMediaAtIndex:i];
  173. }
  174. @synthesize delegate;
  175. - (BOOL)isReadOnly
  176. {
  177. return libvlc_media_list_is_readonly( p_mlist );
  178. }
  179. @end
  180. @implementation VLCMediaList (LibVLCBridging)
  181. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  182. {
  183. return [[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist];
  184. }
  185. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  186. {
  187. if (self = [super init]) {
  188. p_mlist = p_new_mlist;
  189. libvlc_media_list_retain( p_mlist );
  190. libvlc_media_list_lock( p_mlist );
  191. cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
  192. NSUInteger count = libvlc_media_list_count(p_mlist);
  193. for (int i = 0; i < count; i++) {
  194. libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
  195. [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
  196. libvlc_media_release(p_md);
  197. }
  198. [self initInternalMediaList];
  199. libvlc_media_list_unlock(p_mlist);
  200. }
  201. return self;
  202. }
  203. - (void *)libVLCMediaList
  204. {
  205. return p_mlist;
  206. }
  207. @end
  208. @implementation VLCMediaList (Private)
  209. - (void)initInternalMediaList
  210. {
  211. // Add event callbacks
  212. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
  213. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  214. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  215. }
  216. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  217. {
  218. /* We hope to receive index in a nide range, that could change one day */
  219. NSInteger start = [arrayOfArgs[0][@"index"] intValue];
  220. NSInteger end = [arrayOfArgs[[arrayOfArgs count]-1][@"index"] intValue];
  221. NSRange range = NSMakeRange(start, end-start);
  222. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  223. for (NSDictionary * args in arrayOfArgs) {
  224. NSInteger index = [args[@"index"] intValue];
  225. VLCMedia * media = args[@"media"];
  226. /* Sanity check */
  227. if (index && index > [cachedMedia count])
  228. index = [cachedMedia count];
  229. [cachedMedia insertObject:media atIndex:index];
  230. index = [cachedMedia count] - 1;
  231. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  232. [delegate mediaList:self mediaAdded:media atIndex:index];
  233. }
  234. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  235. // Post the notification
  236. // [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
  237. // object:self
  238. // userInfo:args];
  239. }
  240. - (void)mediaListItemRemoved:(NSNumber *)index
  241. {
  242. [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  243. [cachedMedia removeObjectAtIndex:[index intValue]];
  244. [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  245. // Post the notification
  246. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  247. object:self
  248. userInfo:@{@"index": index}];
  249. // Let the delegate know that the item is being removed
  250. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  251. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  252. }
  253. @end