VLCMediaList.m 9.6 KB

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