VLCMediaList.m 9.7 KB

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