VLCMediaList.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*****************************************************************************
  2. * VLCMediaList.m: VLCKit.framework VLCMediaList implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 VLC authors and VideoLAN
  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 it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * 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 drain];
  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 drain];
  65. }
  66. @implementation VLCMediaList
  67. - (id)init
  68. {
  69. if (self = [super init]) {
  70. // Create a new libvlc media list instance
  71. p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance]);
  72. // Initialize internals to defaults
  73. cachedMedia = [[NSMutableArray alloc] init];
  74. [self initInternalMediaList];
  75. }
  76. return self;
  77. }
  78. - (void)dealloc
  79. {
  80. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  81. libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
  82. libvlc_event_detach(em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
  83. [[VLCEventManager sharedManager] cancelCallToObject:self];
  84. // Release allocated memory
  85. delegate = nil;
  86. libvlc_media_list_release( p_mlist );
  87. [cachedMedia release];
  88. [super dealloc];
  89. }
  90. - (NSString *)description
  91. {
  92. NSMutableString * content = [NSMutableString string];
  93. for (NSInteger i = 0; i < [self count]; i++) {
  94. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  95. }
  96. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
  97. }
  98. - (void)lock
  99. {
  100. libvlc_media_list_lock( p_mlist );
  101. }
  102. - (void)unlock
  103. {
  104. libvlc_media_list_unlock( p_mlist );
  105. }
  106. - (NSInteger)addMedia:(VLCMedia *)media
  107. {
  108. int index = [self count];
  109. [self insertMedia:media atIndex:index];
  110. return index;
  111. }
  112. - (void)insertMedia:(VLCMedia *)media atIndex: (NSInteger)index
  113. {
  114. [media retain];
  115. // Add it to the libvlc's medialist
  116. libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], index);
  117. }
  118. - (void)removeMediaAtIndex:(NSInteger)index
  119. {
  120. [cachedMedia removeObjectAtIndex:index];
  121. // Remove it from the libvlc's medialist
  122. libvlc_media_list_remove_index(p_mlist, index);
  123. }
  124. - (VLCMedia *)mediaAtIndex:(NSInteger)index
  125. {
  126. return [cachedMedia objectAtIndex:index];
  127. }
  128. - (NSInteger)indexOfMedia:(VLCMedia *)media
  129. {
  130. NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
  131. return result;
  132. }
  133. /* KVC Compliance: For the @"media" key */
  134. - (NSInteger)countOfMedia
  135. {
  136. return [self count];
  137. }
  138. - (id)objectInMediaAtIndex:(NSUInteger)i
  139. {
  140. return [self mediaAtIndex:i];
  141. }
  142. - (NSInteger)count
  143. {
  144. return [cachedMedia count];
  145. }
  146. - (void)insertObject:(id)object inMediaAtIndex:(NSUInteger)i
  147. {
  148. [self insertMedia:object atIndex:i];
  149. }
  150. - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
  151. {
  152. [self removeMediaAtIndex:i];
  153. }
  154. @synthesize delegate;
  155. - (BOOL)isReadOnly
  156. {
  157. return libvlc_media_list_is_readonly( p_mlist );
  158. }
  159. @end
  160. @implementation VLCMediaList (LibVLCBridging)
  161. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  162. {
  163. return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
  164. }
  165. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  166. {
  167. if (self = [super init]) {
  168. p_mlist = p_new_mlist;
  169. libvlc_media_list_retain( p_mlist );
  170. libvlc_media_list_lock( p_mlist );
  171. cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
  172. NSUInteger count = libvlc_media_list_count(p_mlist);
  173. for (NSUInteger i = 0; i < count; i++) {
  174. libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
  175. [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
  176. libvlc_media_release(p_md);
  177. }
  178. [self initInternalMediaList];
  179. libvlc_media_list_unlock(p_mlist);
  180. }
  181. return self;
  182. }
  183. - (void *)libVLCMediaList
  184. {
  185. return p_mlist;
  186. }
  187. @end
  188. @implementation VLCMediaList (Private)
  189. - (void)initInternalMediaList
  190. {
  191. // Add event callbacks
  192. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
  193. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
  194. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
  195. }
  196. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  197. {
  198. /* We hope to receive index in a nide range, that could change one day */
  199. NSInteger start = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
  200. NSInteger end = [[[arrayOfArgs objectAtIndex: [arrayOfArgs count]-1] objectForKey:@"index"] intValue];
  201. NSRange range = NSMakeRange(start, end-start);
  202. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  203. for (NSDictionary * args in arrayOfArgs) {
  204. NSInteger index = [[args objectForKey:@"index"] intValue];
  205. VLCMedia * media = [args objectForKey:@"media"];
  206. /* Sanity check */
  207. if (index && index > [cachedMedia count])
  208. index = [cachedMedia count];
  209. [cachedMedia insertObject:media atIndex:index];
  210. }
  211. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  212. // Post the notification
  213. // [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
  214. // object:self
  215. // userInfo:args];
  216. // Let the delegate know that the item was added
  217. // if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  218. // [delegate mediaList:self mediaAdded:media atIndex:index];
  219. }
  220. - (void)mediaListItemRemoved:(NSNumber *)index
  221. {
  222. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  223. [cachedMedia removeObjectAtIndex:[index intValue]];
  224. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  225. // Post the notification
  226. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  227. object:self
  228. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  229. index, @"index",
  230. nil]];
  231. // Let the delegate know that the item is being removed
  232. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  233. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  234. }
  235. @end