VLCMediaList.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * Copyright (C) 2009, 2013 Felix Paul Kühne
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCMediaList.h"
  27. #import "VLCLibrary.h"
  28. #import "VLCEventManager.h"
  29. #import "VLCLibVLCBridging.h"
  30. #ifdef HAVE_CONFIG_H
  31. # include "config.h"
  32. #endif
  33. #include <vlc/vlc.h>
  34. #include <vlc/libvlc.h>
  35. /* Notification Messages */
  36. NSString *const VLCMediaListItemAdded = @"VLCMediaListItemAdded";
  37. NSString *const VLCMediaListItemDeleted = @"VLCMediaListItemDeleted";
  38. // TODO: Documentation
  39. @interface VLCMediaList (Private)
  40. /* Initializers */
  41. - (void)initInternalMediaList;
  42. /* Libvlc event bridges */
  43. - (void)mediaListItemAdded:(NSArray *)args;
  44. - (void)mediaListItemRemoved:(NSNumber *)index;
  45. @end
  46. /* libvlc event callback */
  47. static void HandleMediaListItemAdded(const libvlc_event_t * event, void * user_data)
  48. {
  49. @autoreleasepool {
  50. id self = (__bridge id)(user_data);
  51. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  52. withMethod:@selector(mediaListItemAdded:)
  53. withArgumentAsObject:@[@{@"media": [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item],
  54. @"index": @(event->u.media_list_item_added.index)}]];
  55. }
  56. }
  57. static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
  58. {
  59. @autoreleasepool {
  60. id self = (__bridge id)(user_data);
  61. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  62. withMethod:@selector(mediaListItemRemoved:)
  63. withArgumentAsObject:@(event->u.media_list_item_deleted.index)];
  64. }
  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 sharedLibrary].instance);
  72. // Initialize internals to defaults
  73. cachedMedia = [[NSMutableArray alloc] init];
  74. [self initInternalMediaList];
  75. }
  76. return self;
  77. }
  78. - (id)initWithArray:(NSArray *)array
  79. {
  80. if (self = [self init]) {
  81. /* do something useful with the provided array */
  82. NSUInteger count = [array count];
  83. for (NSUInteger x = 0; x < count; x++)
  84. [self addMedia:array[x]];
  85. }
  86. return self;
  87. }
  88. - (void)dealloc
  89. {
  90. libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
  91. libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  92. libvlc_event_detach(em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  93. [[VLCEventManager sharedManager] cancelCallToObject:self];
  94. // Release allocated memory
  95. delegate = nil;
  96. libvlc_media_list_release( p_mlist );
  97. }
  98. - (NSString *)description
  99. {
  100. NSMutableString * content = [NSMutableString string];
  101. for (NSInteger i = 0; i < [self count]; i++) {
  102. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  103. }
  104. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
  105. }
  106. - (void)lock
  107. {
  108. libvlc_media_list_lock( p_mlist );
  109. }
  110. - (void)unlock
  111. {
  112. libvlc_media_list_unlock( p_mlist );
  113. }
  114. - (NSInteger)addMedia:(VLCMedia *)media
  115. {
  116. NSInteger index = [self count];
  117. [self insertMedia:media atIndex:index];
  118. return index;
  119. }
  120. - (void)insertMedia:(VLCMedia *)media atIndex: (NSInteger)index
  121. {
  122. // Add it to the libvlc's medialist
  123. libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], (int)index);
  124. }
  125. - (void)removeMediaAtIndex:(NSInteger)index
  126. {
  127. if (index < [cachedMedia count]) {
  128. [cachedMedia removeObjectAtIndex:index];
  129. // Remove it from the libvlc's medialist
  130. libvlc_media_list_remove_index(p_mlist, (int)index);
  131. }
  132. }
  133. - (VLCMedia *)mediaAtIndex:(NSInteger)index
  134. {
  135. if (index < [cachedMedia count])
  136. return cachedMedia[index];
  137. return NULL;
  138. }
  139. - (NSInteger)indexOfMedia:(VLCMedia *)media
  140. {
  141. NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
  142. return result;
  143. }
  144. /* KVC Compliance: For the @"media" key */
  145. - (NSInteger)countOfMedia
  146. {
  147. return [self count];
  148. }
  149. - (id)objectInMediaAtIndex:(NSUInteger)i
  150. {
  151. return [self mediaAtIndex:i];
  152. }
  153. - (NSInteger)count
  154. {
  155. return [cachedMedia count];
  156. }
  157. - (void)insertObject:(id)object inMediaAtIndex:(NSUInteger)i
  158. {
  159. [self insertMedia:object atIndex:i];
  160. }
  161. - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
  162. {
  163. [self removeMediaAtIndex:i];
  164. }
  165. @synthesize delegate;
  166. - (BOOL)isReadOnly
  167. {
  168. return libvlc_media_list_is_readonly( p_mlist );
  169. }
  170. @end
  171. @implementation VLCMediaList (LibVLCBridging)
  172. + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
  173. {
  174. return [[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist];
  175. }
  176. - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
  177. {
  178. if (self = [super init]) {
  179. p_mlist = p_new_mlist;
  180. libvlc_media_list_retain( p_mlist );
  181. libvlc_media_list_lock( p_mlist );
  182. cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
  183. NSUInteger count = libvlc_media_list_count(p_mlist);
  184. for (int i = 0; i < count; i++) {
  185. libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
  186. [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
  187. libvlc_media_release(p_md);
  188. }
  189. [self initInternalMediaList];
  190. libvlc_media_list_unlock(p_mlist);
  191. }
  192. return self;
  193. }
  194. - (void *)libVLCMediaList
  195. {
  196. return p_mlist;
  197. }
  198. @end
  199. @implementation VLCMediaList (Private)
  200. - (void)initInternalMediaList
  201. {
  202. // Add event callbacks
  203. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
  204. libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, (__bridge void *)(self));
  205. libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
  206. }
  207. - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
  208. {
  209. /* We hope to receive index in a nide range, that could change one day */
  210. NSInteger start = [arrayOfArgs[0][@"index"] intValue];
  211. NSInteger end = [arrayOfArgs[[arrayOfArgs count]-1][@"index"] intValue];
  212. NSRange range = NSMakeRange(start, end-start);
  213. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  214. for (NSDictionary * args in arrayOfArgs) {
  215. NSInteger index = [args[@"index"] intValue];
  216. VLCMedia * media = args[@"media"];
  217. /* Sanity check */
  218. if (index && index > [cachedMedia count])
  219. index = [cachedMedia count];
  220. [cachedMedia insertObject:media atIndex:index];
  221. index = [cachedMedia count] - 1;
  222. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
  223. [delegate mediaList:self mediaAdded:media atIndex:index];
  224. }
  225. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  226. // Post the notification
  227. // [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
  228. // object:self
  229. // userInfo:args];
  230. }
  231. - (void)mediaListItemRemoved:(NSNumber *)index
  232. {
  233. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  234. [cachedMedia removeObjectAtIndex:[index intValue]];
  235. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  236. // Post the notification
  237. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
  238. object:self
  239. userInfo:@{@"index": index}];
  240. // Let the delegate know that the item is being removed
  241. if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
  242. [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
  243. }
  244. @end