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:(NSArray *)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. id self = user_data;
  45. // Check to see if the last item added is this item we're trying to introduce
  46. // If no, then add the item to the local list, otherwise, the item has already
  47. // been added
  48. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  49. withMethod:@selector(mediaListItemAdded:)
  50. withArgumentAsObject:[NSArray arrayWithObjects:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item],
  51. [NSNumber numberWithInt:event->u.media_list_item_added.index], nil]];
  52. }
  53. static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
  54. {
  55. id self = user_data;
  56. // Check to see if the last item deleted is this item we're trying delete now.
  57. // If no, then delete the item from the local list, otherwise, the item has already
  58. // been deleted
  59. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  60. withMethod:@selector(mediaListItemRemoved:)
  61. withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
  62. }
  63. @implementation VLCMediaList
  64. - (id)init
  65. {
  66. if (self = [super init])
  67. {
  68. // Create a new libvlc media list instance
  69. libvlc_exception_t p_e;
  70. libvlc_exception_init(&p_e);
  71. p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance], &p_e);
  72. quit_on_exception(&p_e);
  73. // Initialize internals to defaults
  74. delegate = nil;
  75. [self initInternalMediaList];
  76. }
  77. return self;
  78. }
  79. - (void)release
  80. {
  81. @synchronized(self)
  82. {
  83. if([self retainCount] <= 1)
  84. {
  85. /* We must make sure we won't receive new event after an upcoming dealloc
  86. * We also may receive a -retain in some event callback that may occcur
  87. * Before libvlc_event_detach. So this can't happen in dealloc */
  88. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist, NULL);
  89. libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, NULL);
  90. libvlc_event_detach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, NULL);
  91. }
  92. [super release];
  93. }
  94. }
  95. - (void)dealloc
  96. {
  97. // Release allocated memory
  98. libvlc_media_list_release(p_mlist);
  99. [super dealloc];
  100. }
  101. - (void)setDelegate:(id)value
  102. {
  103. delegate = value;
  104. }
  105. - (id)delegate
  106. {
  107. return delegate;
  108. }
  109. - (void)lock
  110. {
  111. libvlc_media_list_lock( p_mlist );
  112. }
  113. - (void)unlock
  114. {
  115. libvlc_media_list_unlock( p_mlist );
  116. }
  117. - (int)addMedia:(VLCMedia *)media
  118. {
  119. int index = [self count];
  120. [self insertMedia:media atIndex:index];
  121. return index;
  122. }
  123. - (void)insertMedia:(VLCMedia *)media atIndex: (int)index
  124. {
  125. [media retain];
  126. // Add it to the libvlc's medialist
  127. libvlc_exception_t p_e;
  128. libvlc_exception_init( &p_e );
  129. libvlc_media_list_insert_media_descriptor( p_mlist, [media libVLCMediaDescriptor], index, &p_e );
  130. quit_on_exception( &p_e );
  131. }
  132. - (void)removeMediaAtIndex:(int)index
  133. {
  134. [[self mediaAtIndex:index] release];
  135. // Remove it from the libvlc's medialist
  136. libvlc_exception_t p_e;
  137. libvlc_exception_init( &p_e );
  138. libvlc_media_list_remove_index( p_mlist, index, &p_e );
  139. quit_on_exception( &p_e );
  140. }
  141. - (VLCMedia *)mediaAtIndex:(int)index
  142. {
  143. libvlc_exception_t p_e;
  144. libvlc_exception_init( &p_e );
  145. libvlc_media_descriptor_t *p_md = libvlc_media_list_item_at_index( p_mlist, index, &p_e );
  146. quit_on_exception( &p_e );
  147. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  148. // new cocoa object representation of the media descriptor.
  149. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  150. }
  151. - (int)count
  152. {
  153. libvlc_exception_t p_e;
  154. libvlc_exception_init( &p_e );
  155. int result = libvlc_media_list_count( p_mlist, &p_e );
  156. quit_on_exception( &p_e );
  157. return result;
  158. }
  159. - (int)indexOfMedia:(VLCMedia *)media
  160. {
  161. libvlc_exception_t p_e;
  162. libvlc_exception_init( &p_e );
  163. int result = libvlc_media_list_index_of_item( p_mlist, [media libVLCMediaDescriptor], &p_e );
  164. quit_on_exception( &p_e );
  165. return result;
  166. }
  167. - (NSArray *)sublists
  168. {
  169. NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity: 0];
  170. int i, count;
  171. libvlc_exception_t p_e;
  172. libvlc_exception_init( &p_e );
  173. count = libvlc_media_list_count( p_mlist, &p_e );
  174. quit_on_exception( &p_e );
  175. for(i = 0; i < count; i++)
  176. {
  177. libvlc_media_descriptor_t *p_md;
  178. libvlc_media_list_t *p_submlist;
  179. p_md = libvlc_media_list_item_at_index( p_mlist, i, NULL );
  180. p_submlist = libvlc_media_descriptor_subitems( p_md, NULL );
  181. if (p_submlist)
  182. {
  183. [ret addObject:[VLCMediaList medialistWithLibVLCMediaList:p_submlist]];
  184. libvlc_media_list_release( p_submlist );
  185. }
  186. libvlc_media_descriptor_release( p_md );
  187. }
  188. return [ret autorelease];
  189. }
  190. //- (VLCMediaList *)flatPlaylist
  191. //{
  192. // VLCMediaList * flatPlaylist;
  193. // libvlc_exception_t p_e;
  194. // libvlc_exception_init( &p_e );
  195. // libvlc_media_list_t * p_flat_mlist = libvlc_media_list_flat_media_list( p_mlist, &p_e );
  196. // quit_on_exception( &p_e );
  197. // flatPlaylist = [VLCMediaList medialistWithLibVLCMediaList: p_flat_mlist];
  198. // libvlc_media_list_release( p_flat_mlist );
  199. // return flatPlaylist;
  200. //}
  201. //
  202. //- (VLCMedia *)providerMedia
  203. //{
  204. // VLCMedia * ret;
  205. // libvlc_exception_t p_e;
  206. // libvlc_exception_init( &p_e );
  207. // libvlc_media_descriptor_t * p_md = libvlc_media_list_media_descriptor( p_mlist, &p_e );
  208. // ret = [VLCMedia mediaWithLibVLCMediaDescriptor: p_md];
  209. // libvlc_media_descriptor_release( p_md );
  210. // quit_on_exception( &p_e );
  211. // return ret;
  212. //}
  213. @end
  214. @implementation VLCMediaList (LibVLCBridging)
  215. + (id)medialistWithLibVLCMediaList:(void *)p_new_mlist;
  216. {
  217. return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
  218. }
  219. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  220. {
  221. if( self = [super init] )
  222. {
  223. p_mlist = p_new_mlist;
  224. libvlc_media_list_retain(p_mlist);
  225. [self initInternalMediaList];
  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. [self lock];
  239. libvlc_exception_t p_e;
  240. libvlc_exception_init(&p_e);
  241. libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlist, &p_e );
  242. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, &p_e );
  243. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, &p_e );
  244. [self unlock];
  245. quit_on_exception( &p_e );
  246. }
  247. - (void)mediaListItemAdded:(NSArray *)args
  248. {
  249. VLCMedia *media = [args objectAtIndex:0];
  250. NSNumber *index = [args objectAtIndex:1];
  251. // Post the notification
  252. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
  253. object:self
  254. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  255. media, @"media",
  256. index, @"index",
  257. nil]];
  258. // Let the delegate know that the item was added
  259. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  260. [delegate mediaList:self mediaAdded:media atIndex:[index intValue]];
  261. }
  262. - (void)mediaListItemRemoved:(NSNumber *)index
  263. {
  264. // Post the notification
  265. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  266. object:self
  267. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  268. index, @"index",
  269. nil]];
  270. // Let the delegate know that the item is being removed
  271. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  272. [delegate mediaList:self mediaRemovedAtIndex:index];
  273. }
  274. @end