1 /*****************************************************************************
2 * VLCMediaList.m: VLCKit.framework VLCMediaList implementation
3 *****************************************************************************
4 * Copyright (C) 2007 Pierre d'Herbemont
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
25 #import "VLCMediaList.h"
26 #import "VLCLibrary.h"
27 #import "VLCEventManager.h"
28 #import "VLCLibVLCBridging.h"
34 #include <vlc/libvlc.h>
36 /* Notification Messages */
37 NSString * VLCMediaListItemAdded = @"VLCMediaListItemAdded";
38 NSString * VLCMediaListItemDeleted = @"VLCMediaListItemDeleted";
40 // TODO: Documentation
41 @interface VLCMediaList (Private)
43 - (void)initInternalMediaList;
45 /* Libvlc event bridges */
46 - (void)mediaListItemAdded:(NSArray *)args;
47 - (void)mediaListItemRemoved:(NSNumber *)index;
50 /* libvlc event callback */
51 static void HandleMediaListItemAdded(const libvlc_event_t * event, void * user_data)
53 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
55 [[VLCEventManager sharedManager] callOnMainThreadObject:self
56 withMethod:@selector(mediaListItemAdded:)
57 withArgumentAsObject:[NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:
58 [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
59 [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
64 static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
66 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
68 [[VLCEventManager sharedManager] callOnMainThreadObject:self
69 withMethod:@selector(mediaListItemRemoved:)
70 withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
74 @implementation VLCMediaList
77 if (self = [super init])
79 // Create a new libvlc media list instance
80 libvlc_exception_t p_e;
81 libvlc_exception_init( &p_e );
82 p_mlist = libvlc_media_list_new( [VLCLibrary sharedInstance], &p_e );
83 catch_exception( &p_e );
85 // Initialize internals to defaults
86 cachedMedia = [[NSMutableArray alloc] init];
87 delegate = flatAspect = hierarchicalAspect = hierarchicalNodeAspect = nil;
88 [self initInternalMediaList];
97 if([self retainCount] <= 1)
99 /* We must make sure we won't receive new event after an upcoming dealloc
100 * We also may receive a -retain in some event callback that may occcur
101 * Before libvlc_event_detach. So this can't happen in dealloc */
102 libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
103 libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
104 libvlc_event_detach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
112 // Release allocated memory
115 libvlc_media_list_release( p_mlist );
116 [cachedMedia release];
117 [flatAspect release];
118 [hierarchicalAspect release];
119 [hierarchicalNodeAspect release];
123 - (NSString *)description
125 NSMutableString * content = [NSMutableString string];
127 for( i = 0; i < [self count]; i++)
129 [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
131 return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self className], self, content];
136 libvlc_media_list_lock( p_mlist );
141 libvlc_media_list_unlock( p_mlist );
144 - (NSInteger)addMedia:(VLCMedia *)media
146 int index = [self count];
147 [self insertMedia:media atIndex:index];
151 - (void)insertMedia:(VLCMedia *)media atIndex: (NSInteger)index
155 // Add it to the libvlc's medialist
156 libvlc_exception_t p_e;
157 libvlc_exception_init( &p_e );
158 libvlc_media_list_insert_media( p_mlist, [media libVLCMediaDescriptor], index, &p_e );
159 catch_exception( &p_e );
162 - (void)removeMediaAtIndex:(NSInteger)index
164 [[self mediaAtIndex:index] release];
166 // Remove it from the libvlc's medialist
167 libvlc_exception_t p_e;
168 libvlc_exception_init( &p_e );
169 libvlc_media_list_remove_index( p_mlist, index, &p_e );
170 catch_exception( &p_e );
173 - (VLCMedia *)mediaAtIndex:(NSInteger)index
175 return [cachedMedia objectAtIndex:index];
178 - (NSInteger)indexOfMedia:(VLCMedia *)media
180 NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
184 /* KVC Compliance: For the @"media" key */
185 - (NSInteger)countOfMedia
190 - (id)objectInMediaAtIndex:(NSUInteger)i
192 return [self mediaAtIndex:i];
197 return [cachedMedia count];
200 - (void)insertObject:(id)object inMediaAtIndex:(NSUInteger)i
202 [self insertMedia:object atIndex:i];
205 - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
207 [self removeMediaAtIndex:i];
210 @synthesize delegate;
214 return libvlc_media_list_is_readonly( p_mlist );
217 /* Media list aspect */
218 - (VLCMediaListAspect *)hierarchicalAspect
220 if( hierarchicalAspect )
221 return hierarchicalAspect;
223 libvlc_media_list_view_t * p_mlv = libvlc_media_list_hierarchical_view( p_mlist, NULL );
224 hierarchicalAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_mlv andMediaList:self] retain];
225 libvlc_media_list_view_release( p_mlv );
226 return hierarchicalAspect;
229 - (VLCMediaListAspect *)hierarchicalNodeAspect
231 if( hierarchicalNodeAspect )
232 return hierarchicalNodeAspect;
234 libvlc_media_list_view_t * p_mlv = libvlc_media_list_hierarchical_node_view( p_mlist, NULL );
235 hierarchicalNodeAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_mlv andMediaList:self] retain];
236 libvlc_media_list_view_release( p_mlv );
237 return hierarchicalNodeAspect;
240 - (VLCMediaListAspect *)flatAspect
245 libvlc_media_list_view_t * p_mlv = libvlc_media_list_flat_view( p_mlist, NULL );
246 flatAspect = [[VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_mlv andMediaList:self] retain];
247 libvlc_media_list_view_release( p_mlv );
253 @implementation VLCMediaList (LibVLCBridging)
254 + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
256 return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
259 - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
261 if( self = [super init] )
263 p_mlist = p_new_mlist;
264 libvlc_media_list_retain( p_mlist );
265 libvlc_media_list_lock( p_mlist );
266 cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
268 NSUInteger i, count = libvlc_media_list_count(p_mlist);
269 for( i = 0; i < count; i++ )
271 libvlc_media_t * p_md = libvlc_media_list_item_at_index( p_mlist, i, NULL );
272 [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
273 libvlc_media_release(p_md);
275 [self initInternalMediaList];
276 libvlc_media_list_unlock(p_mlist);
281 - (void *)libVLCMediaList
287 @implementation VLCMediaList (Private)
288 - (void)initInternalMediaList
290 // Add event callbacks
291 libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
292 libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
293 libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
296 - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
298 /* We hope to receive index in a nide range, that could change one day */
299 NSInteger start = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
300 NSInteger end = [[[arrayOfArgs objectAtIndex: [arrayOfArgs count]-1] objectForKey:@"index"] intValue];
301 NSRange range = NSMakeRange(start, end-start);
303 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
304 for( NSDictionary * args in arrayOfArgs )
306 NSInteger index = [[args objectForKey:@"index"] intValue];
307 VLCMedia * media = [args objectForKey:@"media"];
309 if( index && index > [cachedMedia count] )
310 index = [cachedMedia count];
311 [cachedMedia insertObject:media atIndex:index];
313 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
315 // Post the notification
316 // [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
320 // Let the delegate know that the item was added
321 // if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
322 // [delegate mediaList:self mediaAdded:media atIndex:index];
325 - (void)mediaListItemRemoved:(NSNumber *)index
327 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
328 [cachedMedia removeObjectAtIndex:[index intValue]];
329 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
331 // Post the notification
332 [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
334 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
338 // Let the delegate know that the item is being removed
339 if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
340 [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];