VLCMediaList.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*****************************************************************************
  2. * VLCMediaList.m: VLC.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. #include <vlc/vlc.h>
  29. #include <vlc/libvlc.h>
  30. /* Notification Messages */
  31. NSString *VLCMediaListItemAdded = @"VLCMediaListItemAdded";
  32. NSString *VLCMediaListItemDeleted = @"VLCMediaListItemDeleted";
  33. // TODO: Documentation
  34. @interface VLCMediaList (Private)
  35. /* Initializers */
  36. - (void)initInternalMediaList;
  37. /* Libvlc event bridges */
  38. - (void)mediaListItemAdded:(NSDictionary *)args;
  39. - (void)mediaListItemRemoved:(NSNumber *)index;
  40. @end
  41. /* libvlc event callback */
  42. static void HandleMediaListItemAdded(const libvlc_event_t *event, void *user_data)
  43. {
  44. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  45. id self = user_data;
  46. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  47. withMethod:@selector(mediaListItemAdded:)
  48. withArgumentAsObject:[NSDictionary dictionaryWithObjectsAndKeys:
  49. [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
  50. [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
  51. nil]];
  52. [pool release];
  53. }
  54. static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
  55. {
  56. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  57. id self = user_data;
  58. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  59. withMethod:@selector(mediaListItemRemoved:)
  60. withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
  61. [pool release];
  62. }
  63. @implementation VLCMediaList (KeyValueCodingCompliance)
  64. /* For the @"media" key */
  65. - (int) countOfMedia
  66. {
  67. return [self count];
  68. }
  69. - (id) objectInMediaAtIndex:(int)i
  70. {
  71. return [self mediaAtIndex:i];
  72. }
  73. @end
  74. @implementation VLCMediaList
  75. - (id)init
  76. {
  77. if (self = [super init])
  78. {
  79. // Create a new libvlc media list instance
  80. libvlc_exception_t p_e;
  81. libvlc_exception_init(&p_e);
  82. p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance], &p_e);
  83. quit_on_exception(&p_e);
  84. // Initialize internals to defaults
  85. cachedMedia = [[NSMutableArray alloc] init];
  86. delegate = flatAspect = hierarchicalAspect = nil;
  87. [self initInternalMediaList];
  88. }
  89. return self;
  90. }
  91. - (void)release
  92. {
  93. @synchronized(self)
  94. {
  95. if([self retainCount] <= 1)
  96. {
  97. /* We must make sure we won't receive new event after an upcoming dealloc
  98. * We also may receive a -retain in some event callback that may occcur
  99. * Before libvlc_event_detach. So this can't happen in dealloc */
  100. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist, NULL);
  101. libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, NULL);
  102. libvlc_event_detach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, NULL);
  103. }
  104. [super release];
  105. }
  106. }
  107. - (void)dealloc
  108. {
  109. // Release allocated memory
  110. libvlc_media_list_release(p_mlist);
  111. [cachedMedia release];
  112. [flatAspect release];
  113. [hierarchicalAspect release];
  114. [super dealloc];
  115. }
  116. - (NSString *)description
  117. {
  118. NSMutableString *content = [NSMutableString string];
  119. int i;
  120. [self lock];
  121. for( i = 0; i < [self count]; i++)
  122. {
  123. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  124. }
  125. [self unlock];
  126. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self className], self, content];
  127. }
  128. - (void)setDelegate:(id)value
  129. {
  130. delegate = value;
  131. }
  132. - (id)delegate
  133. {
  134. return delegate;
  135. }
  136. - (void)lock
  137. {
  138. libvlc_media_list_lock( p_mlist );
  139. }
  140. - (void)unlock
  141. {
  142. libvlc_media_list_unlock( p_mlist );
  143. }
  144. - (int)addMedia:(VLCMedia *)media
  145. {
  146. int index = [self count];
  147. [self insertMedia:media atIndex:index];
  148. return index;
  149. }
  150. - (void)insertMedia:(VLCMedia *)media atIndex: (int)index
  151. {
  152. [media retain];
  153. // Add it to the libvlc's medialist
  154. libvlc_exception_t p_e;
  155. libvlc_exception_init( &p_e );
  156. libvlc_media_list_insert_media_descriptor( p_mlist, [media libVLCMediaDescriptor], index, &p_e );
  157. quit_on_exception( &p_e );
  158. }
  159. - (void)removeMediaAtIndex:(int)index
  160. {
  161. [[self mediaAtIndex:index] release];
  162. // Remove it from the libvlc's medialist
  163. libvlc_exception_t p_e;
  164. libvlc_exception_init( &p_e );
  165. libvlc_media_list_remove_index( p_mlist, index, &p_e );
  166. quit_on_exception( &p_e );
  167. }
  168. - (VLCMedia *)mediaAtIndex:(int)index
  169. {
  170. return [cachedMedia objectAtIndex:index];
  171. }
  172. - (int)count
  173. {
  174. return [cachedMedia count];
  175. }
  176. - (int)indexOfMedia:(VLCMedia *)media
  177. {
  178. libvlc_exception_t p_e;
  179. libvlc_exception_init( &p_e );
  180. int result = libvlc_media_list_index_of_item( p_mlist, [media libVLCMediaDescriptor], &p_e );
  181. quit_on_exception( &p_e );
  182. return result;
  183. }
  184. /* Media list aspect */
  185. - (VLCMediaListAspect *)hierarchicalAspect
  186. {
  187. if( hierarchicalAspect )
  188. return hierarchicalAspect;
  189. libvlc_media_list_view_t * p_mlv = libvlc_media_list_hierarchical_view( p_mlist, NULL );
  190. hierarchicalAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_mlv] retain];
  191. libvlc_media_list_view_release( p_mlv );
  192. return hierarchicalAspect;
  193. }
  194. - (VLCMediaListAspect *)flatAspect
  195. {
  196. if( flatAspect )
  197. return flatAspect;
  198. libvlc_media_list_view_t * p_mlv = libvlc_media_list_flat_view( p_mlist, NULL );
  199. flatAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_mlv] retain];
  200. libvlc_media_list_view_release( p_mlv );
  201. return flatAspect;
  202. }
  203. @end
  204. @implementation VLCMediaList (LibVLCBridging)
  205. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  206. {
  207. return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
  208. }
  209. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  210. {
  211. if( self = [super init] )
  212. {
  213. p_mlist = p_new_mlist;
  214. libvlc_media_list_retain(p_mlist);
  215. libvlc_media_list_lock(p_mlist);
  216. cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist, NULL)];
  217. int i, count = libvlc_media_list_count(p_mlist, NULL);
  218. for( i = 0; i < count; i++ )
  219. {
  220. libvlc_media_descriptor_t * p_md = libvlc_media_list_item_at_index(p_mlist, i, NULL);
  221. [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
  222. libvlc_media_descriptor_release(p_md);
  223. }
  224. [self initInternalMediaList];
  225. libvlc_media_list_unlock(p_mlist);
  226. }
  227. return self;
  228. }
  229. - (void *)libVLCMediaList
  230. {
  231. return p_mlist;
  232. }
  233. @end
  234. @implementation VLCMediaList (Private)
  235. - (void)initInternalMediaList
  236. {
  237. // Add event callbacks
  238. libvlc_exception_t p_e;
  239. libvlc_exception_init(&p_e);
  240. libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlist, &p_e );
  241. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, &p_e );
  242. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, &p_e );
  243. quit_on_exception( &p_e );
  244. }
  245. - (void)mediaListItemAdded:(NSDictionary *)args
  246. {
  247. int index = [[args objectForKey:@"index"] intValue];
  248. VLCMedia * media = [args objectForKey:@"media"];
  249. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
  250. [cachedMedia insertObject:media atIndex:index];
  251. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
  252. // Post the notification
  253. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
  254. object:self
  255. userInfo:args];
  256. // Let the delegate know that the item was added
  257. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  258. [delegate mediaList:self mediaAdded:media atIndex:index];
  259. }
  260. - (void)mediaListItemRemoved:(NSNumber *)index
  261. {
  262. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  263. [cachedMedia removeObjectAtIndex:[index intValue]];
  264. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  265. // Post the notification
  266. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  267. object:self
  268. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  269. index, @"index",
  270. nil]];
  271. // Let the delegate know that the item is being removed
  272. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  273. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  274. }
  275. @end