VLCMediaList.m 11 KB

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