VLCMediaList.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 * VLCMediaListItemAdded = @"VLCMediaListItemAdded";
  37. NSString * 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. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  50. id self = 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. [pool release];
  56. }
  57. static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
  58. {
  59. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  60. id self = user_data;
  61. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  62. withMethod:@selector(mediaListItemRemoved:)
  63. withArgumentAsObject:@(event->u.media_list_item_deleted.index)];
  64. [pool release];
  65. }
  66. @implementation VLCMediaList
  67. - (id)init
  68. {
  69. if (self = [super init]) {
  70. // Create a new libvlc media list instance
  71. p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance]);
  72. // Initialize internals to defaults
  73. cachedMedia = [[NSMutableArray alloc] init];
  74. [self initInternalMediaList];
  75. }
  76. return self;
  77. }
  78. - (id)initWithArray:(NSArray *)array
  79. {
  80. if (self = [self init]) {
  81. /* do something useful with the provided array */
  82. NSUInteger count = [array count];
  83. for (NSUInteger x = 0; x < count; x++)
  84. [self addMedia:array[x]];
  85. }
  86. return self;
  87. }
  88. - (void)dealloc
  89. {
  90. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  91. libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
  92. libvlc_event_detach(em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
  93. [[VLCEventManager sharedManager] cancelCallToObject:self];
  94. // Release allocated memory
  95. delegate = nil;
  96. libvlc_media_list_release( p_mlist );
  97. [cachedMedia release];
  98. [super dealloc];
  99. }
  100. - (NSString *)description
  101. {
  102. NSMutableString * content = [NSMutableString string];
  103. for (NSInteger i = 0; i < [self count]; i++) {
  104. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  105. }
  106. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
  107. }
  108. - (void)lock
  109. {
  110. libvlc_media_list_lock( p_mlist );
  111. }
  112. - (void)unlock
  113. {
  114. libvlc_media_list_unlock( p_mlist );
  115. }
  116. - (NSInteger)addMedia:(VLCMedia *)media
  117. {
  118. int index = [self count];
  119. [self insertMedia:media atIndex:index];
  120. return index;
  121. }
  122. - (void)insertMedia:(VLCMedia *)media atIndex: (NSInteger)index
  123. {
  124. [media retain];
  125. // Add it to the libvlc's medialist
  126. libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], index);
  127. }
  128. - (void)removeMediaAtIndex:(NSInteger)index
  129. {
  130. if (index < [cachedMedia count]) {
  131. [cachedMedia removeObjectAtIndex:index];
  132. // Remove it from the libvlc's medialist
  133. libvlc_media_list_remove_index(p_mlist, index);
  134. }
  135. }
  136. - (VLCMedia *)mediaAtIndex:(NSInteger)index
  137. {
  138. if (index < [cachedMedia count])
  139. return cachedMedia[index];
  140. return NULL;
  141. }
  142. - (NSInteger)indexOfMedia:(VLCMedia *)media
  143. {
  144. NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
  145. return result;
  146. }
  147. /* KVC Compliance: For the @"media" key */
  148. - (NSInteger)countOfMedia
  149. {
  150. return [self count];
  151. }
  152. - (id)objectInMediaAtIndex:(NSUInteger)i
  153. {
  154. return [self mediaAtIndex:i];
  155. }
  156. - (NSInteger)count
  157. {
  158. return [cachedMedia count];
  159. }
  160. - (void)insertObject:(id)object inMediaAtIndex:(NSUInteger)i
  161. {
  162. [self insertMedia:object atIndex:i];
  163. }
  164. - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
  165. {
  166. [self removeMediaAtIndex:i];
  167. }
  168. @synthesize delegate;
  169. - (BOOL)isReadOnly
  170. {
  171. return libvlc_media_list_is_readonly( p_mlist );
  172. }
  173. @end
  174. @implementation VLCMediaList (LibVLCBridging)
  175. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  176. {
  177. return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
  178. }
  179. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  180. {
  181. if (self = [super init]) {
  182. p_mlist = p_new_mlist;
  183. libvlc_media_list_retain( p_mlist );
  184. libvlc_media_list_lock( p_mlist );
  185. cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
  186. NSUInteger count = libvlc_media_list_count(p_mlist);
  187. for (NSUInteger i = 0; i < count; i++) {
  188. libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
  189. [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
  190. libvlc_media_release(p_md);
  191. }
  192. [self initInternalMediaList];
  193. libvlc_media_list_unlock(p_mlist);
  194. }
  195. return self;
  196. }
  197. - (void *)libVLCMediaList
  198. {
  199. return p_mlist;
  200. }
  201. @end
  202. @implementation VLCMediaList (Private)
  203. - (void)initInternalMediaList
  204. {
  205. // Add event callbacks
  206. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
  207. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
  208. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
  209. }
  210. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  211. {
  212. /* We hope to receive index in a nide range, that could change one day */
  213. NSInteger start = [arrayOfArgs[0][@"index"] intValue];
  214. NSInteger end = [arrayOfArgs[[arrayOfArgs count]-1][@"index"] intValue];
  215. NSRange range = NSMakeRange(start, end-start);
  216. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  217. for (NSDictionary * args in arrayOfArgs) {
  218. NSInteger index = [args[@"index"] intValue];
  219. VLCMedia * media = args[@"media"];
  220. /* Sanity check */
  221. if (index && index > [cachedMedia count])
  222. index = [cachedMedia count];
  223. [cachedMedia insertObject:media atIndex:index];
  224. }
  225. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  226. // Post the notification
  227. // [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
  228. // object:self
  229. // userInfo:args];
  230. // Let the delegate know that the item was added
  231. // if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  232. // [delegate mediaList:self mediaAdded:media atIndex:index];
  233. }
  234. - (void)mediaListItemRemoved:(NSNumber *)index
  235. {
  236. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  237. [cachedMedia removeObjectAtIndex:[index intValue]];
  238. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  239. // Post the notification
  240. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  241. object:self
  242. userInfo:@{@"index": index}];
  243. // Let the delegate know that the item is being removed
  244. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  245. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  246. }
  247. @end