VLCMediaList.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*****************************************************************************
  2. * VLCMediaList.m: VLCKit.framework VLCMediaList implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCMediaList.h"
  25. #import "VLCLibrary.h"
  26. #import "VLCEventManager.h"
  27. #import "VLCLibVLCBridging.h"
  28. #ifdef HAVE_CONFIG_H
  29. # include "config.h"
  30. #endif
  31. #include <vlc/vlc.h>
  32. #include <vlc/libvlc.h>
  33. /* Notification Messages */
  34. NSString * VLCMediaListItemAdded = @"VLCMediaListItemAdded";
  35. NSString * VLCMediaListItemDeleted = @"VLCMediaListItemDeleted";
  36. // TODO: Documentation
  37. @interface VLCMediaList (Private)
  38. /* Initializers */
  39. - (void)initInternalMediaList;
  40. /* Libvlc event bridges */
  41. - (void)mediaListItemAdded:(NSArray *)args;
  42. - (void)mediaListItemRemoved:(NSNumber *)index;
  43. @end
  44. /* libvlc event callback */
  45. static void HandleMediaListItemAdded(const libvlc_event_t * event, void * user_data)
  46. {
  47. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  48. id self = user_data;
  49. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  50. withMethod:@selector(mediaListItemAdded:)
  51. withArgumentAsObject:[NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:
  52. [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
  53. [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
  54. nil]]];
  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:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
  64. [pool release];
  65. }
  66. @implementation VLCMediaList
  67. - (id)init
  68. {
  69. if (self = [super init])
  70. {
  71. // Create a new libvlc media list instance
  72. libvlc_exception_t p_e;
  73. libvlc_exception_init( &p_e );
  74. p_mlist = libvlc_media_list_new( [VLCLibrary sharedInstance], &p_e );
  75. catch_exception( &p_e );
  76. // Initialize internals to defaults
  77. cachedMedia = [[NSMutableArray alloc] init];
  78. delegate = flatAspect = hierarchicalAspect = hierarchicalNodeAspect = nil;
  79. [self initInternalMediaList];
  80. }
  81. return self;
  82. }
  83. - (void)release
  84. {
  85. @synchronized(self)
  86. {
  87. if([self retainCount] <= 1)
  88. {
  89. /* We must make sure we won't receive new event after an upcoming dealloc
  90. * We also may receive a -retain in some event callback that may occcur
  91. * Before libvlc_event_detach. So this can't happen in dealloc */
  92. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
  93. libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
  94. libvlc_event_detach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
  95. }
  96. [super release];
  97. }
  98. }
  99. - (void)dealloc
  100. {
  101. // Release allocated memory
  102. delegate = nil;
  103. libvlc_media_list_release( p_mlist );
  104. [cachedMedia release];
  105. [flatAspect release];
  106. [hierarchicalAspect release];
  107. [hierarchicalNodeAspect release];
  108. [super dealloc];
  109. }
  110. - (NSString *)description
  111. {
  112. NSMutableString * content = [NSMutableString string];
  113. int i;
  114. for( i = 0; i < [self count]; i++)
  115. {
  116. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  117. }
  118. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self className], self, content];
  119. }
  120. - (void)lock
  121. {
  122. libvlc_media_list_lock( p_mlist );
  123. }
  124. - (void)unlock
  125. {
  126. libvlc_media_list_unlock( p_mlist );
  127. }
  128. - (NSInteger)addMedia:(VLCMedia *)media
  129. {
  130. int index = [self count];
  131. [self insertMedia:media atIndex:index];
  132. return index;
  133. }
  134. - (void)insertMedia:(VLCMedia *)media atIndex: (NSInteger)index
  135. {
  136. [media retain];
  137. // Add it to the libvlc's medialist
  138. libvlc_exception_t p_e;
  139. libvlc_exception_init( &p_e );
  140. libvlc_media_list_insert_media( p_mlist, [media libVLCMediaDescriptor], index, &p_e );
  141. catch_exception( &p_e );
  142. }
  143. - (void)removeMediaAtIndex:(NSInteger)index
  144. {
  145. [[self mediaAtIndex:index] release];
  146. // Remove it from the libvlc's medialist
  147. libvlc_exception_t p_e;
  148. libvlc_exception_init( &p_e );
  149. libvlc_media_list_remove_index( p_mlist, index, &p_e );
  150. catch_exception( &p_e );
  151. }
  152. - (VLCMedia *)mediaAtIndex:(NSInteger)index
  153. {
  154. return [cachedMedia objectAtIndex:index];
  155. }
  156. - (NSInteger)indexOfMedia:(VLCMedia *)media
  157. {
  158. NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
  159. return result;
  160. }
  161. /* KVC Compliance: For the @"media" key */
  162. - (NSInteger)countOfMedia
  163. {
  164. return [self count];
  165. }
  166. - (id)objectInMediaAtIndex:(NSUInteger)i
  167. {
  168. return [self mediaAtIndex:i];
  169. }
  170. - (NSInteger)count
  171. {
  172. return [cachedMedia count];
  173. }
  174. - (void)insertObject:(id)object inMediaAtIndex:(NSUInteger)i
  175. {
  176. [self insertMedia:object atIndex:i];
  177. }
  178. - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
  179. {
  180. [self removeMediaAtIndex:i];
  181. }
  182. @synthesize delegate;
  183. - (BOOL)isReadOnly
  184. {
  185. libvlc_exception_t p_e;
  186. libvlc_exception_init( &p_e );
  187. BOOL isReadOnly = libvlc_media_list_is_readonly( p_mlist );
  188. catch_exception( &p_e );
  189. return isReadOnly;
  190. }
  191. /* Media list aspect */
  192. - (VLCMediaListAspect *)hierarchicalAspect
  193. {
  194. if( hierarchicalAspect )
  195. return hierarchicalAspect;
  196. libvlc_media_list_view_t * p_mlv = libvlc_media_list_hierarchical_view( p_mlist, NULL );
  197. hierarchicalAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_mlv andMediaList:self] retain];
  198. libvlc_media_list_view_release( p_mlv );
  199. return hierarchicalAspect;
  200. }
  201. - (VLCMediaListAspect *)hierarchicalNodeAspect
  202. {
  203. if( hierarchicalNodeAspect )
  204. return hierarchicalNodeAspect;
  205. libvlc_media_list_view_t * p_mlv = libvlc_media_list_hierarchical_node_view( p_mlist, NULL );
  206. hierarchicalNodeAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_mlv andMediaList:self] retain];
  207. libvlc_media_list_view_release( p_mlv );
  208. return hierarchicalNodeAspect;
  209. }
  210. - (VLCMediaListAspect *)flatAspect
  211. {
  212. if( flatAspect )
  213. return flatAspect;
  214. libvlc_media_list_view_t * p_mlv = libvlc_media_list_flat_view( p_mlist, NULL );
  215. flatAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_mlv andMediaList:self] retain];
  216. libvlc_media_list_view_release( p_mlv );
  217. return flatAspect;
  218. }
  219. @end
  220. @implementation VLCMediaList (LibVLCBridging)
  221. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  222. {
  223. return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
  224. }
  225. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  226. {
  227. if( self = [super init] )
  228. {
  229. p_mlist = p_new_mlist;
  230. libvlc_media_list_retain( p_mlist );
  231. libvlc_media_list_lock( p_mlist );
  232. cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
  233. NSUInteger i, count = libvlc_media_list_count(p_mlist);
  234. for( i = 0; i < count; i++ )
  235. {
  236. libvlc_media_t * p_md = libvlc_media_list_item_at_index( p_mlist, i, NULL );
  237. [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
  238. libvlc_media_release(p_md);
  239. }
  240. [self initInternalMediaList];
  241. libvlc_media_list_unlock(p_mlist);
  242. }
  243. return self;
  244. }
  245. - (void *)libVLCMediaList
  246. {
  247. return p_mlist;
  248. }
  249. @end
  250. @implementation VLCMediaList (Private)
  251. - (void)initInternalMediaList
  252. {
  253. // Add event callbacks
  254. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
  255. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
  256. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
  257. }
  258. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  259. {
  260. /* We hope to receive index in a nide range, that could change one day */
  261. NSInteger start = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
  262. NSInteger end = [[[arrayOfArgs objectAtIndex: [arrayOfArgs count]-1] objectForKey:@"index"] intValue];
  263. NSRange range = NSMakeRange(start, end-start);
  264. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  265. for( NSDictionary * args in arrayOfArgs )
  266. {
  267. NSInteger index = [[args objectForKey:@"index"] intValue];
  268. VLCMedia * media = [args objectForKey:@"media"];
  269. /* Sanity check */
  270. if( index && index > [cachedMedia count] )
  271. index = [cachedMedia count];
  272. [cachedMedia insertObject:media atIndex:index];
  273. }
  274. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  275. // Post the notification
  276. // [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
  277. // object:self
  278. // userInfo:args];
  279. // Let the delegate know that the item was added
  280. // if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  281. // [delegate mediaList:self mediaAdded:media atIndex:index];
  282. }
  283. - (void)mediaListItemRemoved:(NSNumber *)index
  284. {
  285. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  286. [cachedMedia removeObjectAtIndex:[index intValue]];
  287. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  288. // Post the notification
  289. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  290. object:self
  291. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  292. index, @"index",
  293. nil]];
  294. // Let the delegate know that the item is being removed
  295. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  296. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  297. }
  298. @end