VLCPlaylist.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*****************************************************************************
  2. * VLCPlaylist.h: VLC.framework VLCPlaylist 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 <VLC/VLCPlaylist.h>
  25. #import "VLCLibrary.h"
  26. #import "VLCEventManager.h"
  27. #import <vlc/vlc.h>
  28. #import <vlc/libvlc.h>
  29. /* Our notification */
  30. NSString * VLCPlaylistItemChanged = @"VLCPlaylistItemChanged";
  31. NSString * VLCPlaylistItemDeleted = @"VLCPlaylistItemDeleted";
  32. NSString * VLCPlaylistItemAdded = @"VLCPlaylistItemAdded";
  33. @interface VLCPlaylist (Private)
  34. - (void)initializeInternalMediaList;
  35. - (void)removeObjectFromLibVLCInItemsAtIndex:(int) i;
  36. - (void)insertObjectFromLibVLC:(id)object inItemsAtIndex:(int) i;
  37. @end
  38. @interface VLCPlaylist (Binding)
  39. /* Bindings */
  40. - (unsigned int)countOfItems;
  41. - (id)objectInItemsAtIndex:(unsigned int)index;
  42. - (void)insertObject:(id)anObject inItemsAtIndex:(unsigned int)index;
  43. - (void)replaceObjectInItemsAtIndex:(unsigned int)index withObject:(id)anObject;
  44. @end
  45. /* libvlc event callback */
  46. static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
  47. {
  48. id self = user_data;
  49. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  50. #ifdef DONT_UPDATE_VARIABLES_FROM_NON_MAIN_THREAD_CAUSE_BINDINGS_DONT_LIKE
  51. /* That would be nice to sync self's variables on non main thread, but
  52. * I did experience some Bindings related crash with an NSTreeController */
  53. [self removeObjectFromLibVLCInItemsAtIndex: event->u.media_list_item_deleted.index];
  54. #endif
  55. [[VLCEventManager sharedManager] callOnMainThreadObject:self withMethod:@selector(playlistItemRemoved:) withArgumentAsObject:
  56. [NSNumber numberWithInt: event->u.media_list_item_deleted.index] ];
  57. #ifdef DONT_SEND_NOTIFICATION_BECAUSE_WE_DONT_HAVE_TO
  58. /* This was disabled to avoid non necessary overhead, but we may want to
  59. * retablish that once the rest becomes more stable */
  60. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  61. withDelegateMethod: @selector(playlistItemAdded:)
  62. withNotificationName: VLCPlaylistItemDeleted];
  63. #endif
  64. [pool release];
  65. }
  66. /* libvlc event callback */
  67. static void HandleMediaListItemAdded( const libvlc_event_t * event, void * user_data)
  68. {
  69. id self = user_data;
  70. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  71. #ifdef DONT_UPDATE_VARIABLES_FROM_NON_MAIN_THREAD_CAUSE_BINDINGS_DONT_LIKE
  72. [self insertObjectFromLibVLC: [VLCMedia mediaWithLibVLCMediaDescriptor: event->u.media_list_item_added.item]
  73. inItemsAtIndex: event->u.media_list_item_added.index ];
  74. #endif
  75. [[VLCEventManager sharedManager] callOnMainThreadObject:self withMethod:@selector(playlistItemAdded:) withArgumentAsObject:
  76. [NSArray arrayWithObjects: [VLCMedia mediaWithLibVLCMediaDescriptor: event->u.media_list_item_added.item],
  77. [NSNumber numberWithInt: event->u.media_list_item_added.index], nil ] ];
  78. #ifdef DONT_SEND_NOTIFICATION_BECAUSE_WE_DONT_HAVE_TO
  79. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  80. withDelegateMethod: @selector(playlistItemAdded:)
  81. withNotificationName: VLCPlaylistItemAdded];
  82. #endif
  83. [pool release];
  84. }
  85. @implementation VLCPlaylist (Private)
  86. - (void)initializeInternalMediaList
  87. {
  88. libvlc_exception_t p_e;
  89. int i;
  90. [self lock];
  91. items = [[NSMutableArray alloc] init];
  92. for( i = 0; i < libvlc_media_list_count( p_mlist, NULL ); i++ )
  93. {
  94. libvlc_media_descriptor_t * p_md;
  95. p_md = libvlc_media_list_item_at_index( p_mlist, i, NULL );
  96. [items addObject:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
  97. libvlc_media_descriptor_release( p_md );
  98. }
  99. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager( p_mlist, &p_e );
  100. libvlc_exception_init( &p_e );
  101. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, &p_e );
  102. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, &p_e );
  103. [self unlock];
  104. quit_on_exception( &p_e );
  105. }
  106. /* LibVLC callback, executed on main thread */
  107. - (void)playlistItemAdded:(NSArray *)args
  108. {
  109. [self insertObjectFromLibVLC: [args objectAtIndex: 0]
  110. inItemsAtIndex: [[args objectAtIndex: 1] intValue] ];
  111. }
  112. - (void)playlistItemRemoved:(NSNumber *)index
  113. {
  114. [self removeObjectFromLibVLCInItemsAtIndex: [index intValue] ];
  115. }
  116. /* When LibVLC change its mlist's item this functions gets called */
  117. - (void)removeObjectFromLibVLCInItemsAtIndex:(int) i
  118. {
  119. [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:i] forKey:@"items"];
  120. [items removeObjectAtIndex:i];
  121. [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:i] forKey:@"items"];
  122. }
  123. - (void)insertObjectFromLibVLC:(id)object inItemsAtIndex:(int) i
  124. {
  125. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:i] forKey:@"items"];
  126. [items insertObject:object atIndex: i];
  127. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:i] forKey:@"items"];
  128. }
  129. @end
  130. @implementation VLCPlaylist (Binding)
  131. /* "items" bindings */
  132. - (unsigned int)countOfItems
  133. {
  134. return [items count];
  135. }
  136. - (id)objectInItemsAtIndex:(unsigned int)index
  137. {
  138. return [items objectAtIndex:index];
  139. }
  140. /* Setters go through LibVLC */
  141. - (void)insertObject:(id)anObject inItemsAtIndex:(unsigned int)index
  142. {
  143. [self insertMedia:anObject atIndex:index];
  144. }
  145. - (void)removeObjectFromItemsAtIndex:(unsigned int)index
  146. {
  147. [self removeMediaAtIndex:index];
  148. }
  149. - (void)replaceObjectInItemsAtIndex:(unsigned int)index withObject:(id)anObject
  150. {
  151. [self removeMediaAtIndex:index];
  152. [self insertMedia:anObject atIndex:index];
  153. }
  154. @end
  155. @implementation VLCPlaylist
  156. - (id)init
  157. {
  158. [VLCEventManager sharedManager];
  159. if (self = [super init])
  160. {
  161. libvlc_exception_t p_e;
  162. libvlc_exception_init( &p_e );
  163. p_mlist = libvlc_media_list_new( [VLCLibrary sharedInstance], &p_e );
  164. quit_on_exception( &p_e );
  165. [self initializeInternalMediaList];
  166. }
  167. return self;
  168. }
  169. - (void)release
  170. {
  171. @synchronized(self)
  172. {
  173. if([self retainCount] <= 1)
  174. {
  175. /* We must make sure we won't receive new event after an upcoming dealloc
  176. * We also may receive a -retain in some event callback that may occcur
  177. * Before libvlc_event_detach. So this can't happen in dealloc */
  178. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager( p_mlist, NULL );
  179. libvlc_event_detach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, NULL );
  180. libvlc_event_detach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, NULL );
  181. }
  182. [super release];
  183. }
  184. }
  185. - (void)dealloc
  186. {
  187. libvlc_media_list_release( p_mlist );
  188. [items release];
  189. [super dealloc];
  190. }
  191. - (VLCMedia *)mediaAtIndex: (int)index
  192. {
  193. libvlc_exception_t p_e;
  194. libvlc_exception_init( &p_e );
  195. libvlc_media_descriptor_t * p_md = libvlc_media_list_item_at_index( p_mlist, index, &p_e );
  196. quit_on_exception( &p_e );
  197. return [VLCMedia mediaWithLibVLCMediaDescriptor: p_md];
  198. }
  199. - (int)countMedia
  200. {
  201. libvlc_exception_t p_e;
  202. libvlc_exception_init( &p_e );
  203. int count = libvlc_media_list_count( p_mlist, &p_e );
  204. quit_on_exception( &p_e );
  205. return count;
  206. }
  207. - (NSArray *)sublists
  208. {
  209. NSMutableArray * ret = [[NSMutableArray alloc] initWithCapacity: 0];
  210. int i, count;
  211. libvlc_exception_t p_e;
  212. libvlc_exception_init( &p_e );
  213. count = libvlc_media_list_count( p_mlist, &p_e );
  214. quit_on_exception( &p_e );
  215. for( i = 0; i < count; i++ )
  216. {
  217. libvlc_media_descriptor_t * p_md;
  218. libvlc_media_list_t * p_submlist;
  219. p_md = libvlc_media_list_item_at_index( p_mlist, i, NULL );
  220. p_submlist = libvlc_media_descriptor_subitems( p_md, NULL );
  221. if( p_submlist )
  222. {
  223. [ret addObject: [VLCPlaylist playlistWithLibVLCMediaList: p_submlist]];
  224. libvlc_media_list_release( p_submlist );
  225. }
  226. libvlc_media_descriptor_release( p_md );
  227. }
  228. return [ret autorelease];
  229. }
  230. - (VLCPlaylist *)flatPlaylist
  231. {
  232. VLCPlaylist * flatPlaylist;
  233. libvlc_exception_t p_e;
  234. libvlc_exception_init( &p_e );
  235. libvlc_media_list_t * p_flat_mlist = libvlc_media_list_flat_media_list( p_mlist, &p_e );
  236. quit_on_exception( &p_e );
  237. flatPlaylist = [VLCPlaylist playlistWithLibVLCMediaList: p_flat_mlist];
  238. libvlc_media_list_release( p_flat_mlist );
  239. return flatPlaylist;
  240. }
  241. - (VLCMedia *)providerMedia
  242. {
  243. VLCMedia * ret;
  244. libvlc_exception_t p_e;
  245. libvlc_exception_init( &p_e );
  246. libvlc_media_descriptor_t * p_md = libvlc_media_list_media_descriptor( p_mlist, &p_e );
  247. ret = [VLCMedia mediaWithLibVLCMediaDescriptor: p_md];
  248. libvlc_media_descriptor_release( p_md );
  249. quit_on_exception( &p_e );
  250. return ret;
  251. }
  252. - (void)addMedia: (VLCMedia *)item
  253. {
  254. libvlc_exception_t p_e;
  255. libvlc_exception_init( &p_e );
  256. libvlc_media_descriptor_t * p_md = [item libVLCMediaDescriptor];
  257. libvlc_media_list_add_media_descriptor( p_mlist, p_md, &p_e );
  258. libvlc_media_descriptor_release( p_md );
  259. quit_on_exception( &p_e );
  260. }
  261. - (void)insertMedia: (VLCMedia *)item atIndex: (int)index
  262. {
  263. libvlc_exception_t p_e;
  264. libvlc_exception_init( &p_e );
  265. libvlc_media_descriptor_t * p_md = [item libVLCMediaDescriptor];
  266. libvlc_media_list_insert_media_descriptor( p_mlist, p_md, index, &p_e );
  267. libvlc_media_descriptor_release( p_md );
  268. quit_on_exception( &p_e );
  269. }
  270. - (void)removeMediaAtIndex: (int)index
  271. {
  272. libvlc_exception_t p_e;
  273. libvlc_exception_init( &p_e );
  274. libvlc_media_list_remove_index( p_mlist, index, &p_e );
  275. quit_on_exception( &p_e );
  276. }
  277. - (void)lock
  278. {
  279. libvlc_media_list_lock( p_mlist );
  280. }
  281. - (void)unlock
  282. {
  283. libvlc_media_list_unlock( p_mlist );
  284. }
  285. - (id)delegate
  286. {
  287. /* No delegate supported */
  288. return nil;
  289. }
  290. @end
  291. @implementation VLCPlaylist (LibVLCBridging)
  292. - (id) initWithLibVLCMediaList: (libvlc_media_list_t *)p_new_mlist;
  293. {
  294. if( self = [super init] )
  295. {
  296. p_mlist = p_new_mlist;
  297. libvlc_media_list_retain( p_mlist );
  298. [self initializeInternalMediaList];
  299. }
  300. return self;
  301. }
  302. + (id) playlistWithLibVLCMediaList: (libvlc_media_list_t *)p_new_mlist;
  303. {
  304. return [[[VLCPlaylist alloc] initWithLibVLCMediaList: p_new_mlist] autorelease];
  305. }
  306. - (libvlc_media_list_t *) libVLCMediaList
  307. {
  308. libvlc_media_list_retain( p_mlist );
  309. return p_mlist;
  310. }
  311. @end