1 /*****************************************************************************
2 * VLCMediaList.m: VLC.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"
30 #include <vlc/libvlc.h>
32 /* Notification Messages */
33 NSString *VLCMediaListItemAdded = @"VLCMediaListItemAdded";
34 NSString *VLCMediaListItemDeleted = @"VLCMediaListItemDeleted";
36 // TODO: Documentation
37 @interface VLCMediaList (Private)
39 - (void)initInternalMediaList;
41 /* Libvlc event bridges */
42 - (void)mediaListItemAdded:(NSDictionary *)args;
43 - (void)mediaListItemRemoved:(NSNumber *)index;
46 /* libvlc event callback */
47 static void HandleMediaListItemAdded(const libvlc_event_t *event, void *user_data)
49 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
51 [[VLCEventManager sharedManager] callOnMainThreadObject:self
52 withMethod:@selector(mediaListItemAdded:)
53 withArgumentAsObject:[NSDictionary dictionaryWithObjectsAndKeys:
54 [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
55 [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
60 static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
62 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
64 [[VLCEventManager sharedManager] callOnMainThreadObject:self
65 withMethod:@selector(mediaListItemRemoved:)
66 withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
70 @implementation VLCMediaList (KeyValueCodingCompliance)
71 /* For the @"Media" key */
77 - (id) objectInMediaAtIndex:(int)i
79 return [self mediaAtIndex:i];
83 @implementation VLCMediaList
86 if (self = [super init])
88 // Create a new libvlc media list instance
89 libvlc_exception_t p_e;
90 libvlc_exception_init(&p_e);
91 p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance], &p_e);
92 quit_on_exception(&p_e);
94 // Initialize internals to defaults
95 cachedMedia = [[NSMutableArray alloc] init];
97 [self initInternalMediaList];
106 if([self retainCount] <= 1)
108 /* We must make sure we won't receive new event after an upcoming dealloc
109 * We also may receive a -retain in some event callback that may occcur
110 * Before libvlc_event_detach. So this can't happen in dealloc */
111 libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist, NULL);
112 libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, NULL);
113 libvlc_event_detach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, NULL);
121 // Release allocated memory
122 libvlc_media_list_release(p_mlist);
123 [cachedMedia release];
127 - (void)setDelegate:(id)value
139 libvlc_media_list_lock( p_mlist );
144 libvlc_media_list_unlock( p_mlist );
147 - (int)addMedia:(VLCMedia *)media
149 int index = [self count];
150 [self insertMedia:media atIndex:index];
154 - (void)insertMedia:(VLCMedia *)media atIndex: (int)index
158 // Add it to the libvlc's medialist
159 libvlc_exception_t p_e;
160 libvlc_exception_init( &p_e );
161 libvlc_media_list_insert_media_descriptor( p_mlist, [media libVLCMediaDescriptor], index, &p_e );
162 quit_on_exception( &p_e );
165 - (void)removeMediaAtIndex:(int)index
167 [[self mediaAtIndex:index] release];
169 // Remove it from the libvlc's medialist
170 libvlc_exception_t p_e;
171 libvlc_exception_init( &p_e );
172 libvlc_media_list_remove_index( p_mlist, index, &p_e );
173 quit_on_exception( &p_e );
176 - (VLCMedia *)mediaAtIndex:(int)index
178 return [cachedMedia objectAtIndex:index];
183 return [cachedMedia count];
186 - (int)indexOfMedia:(VLCMedia *)media
188 libvlc_exception_t p_e;
189 libvlc_exception_init( &p_e );
190 int result = libvlc_media_list_index_of_item( p_mlist, [media libVLCMediaDescriptor], &p_e );
191 quit_on_exception( &p_e );
196 /* Media list aspect */
197 - (VLCMediaListAspect *)hierarchicalAspect
199 VLCMediaListAspect * hierarchicalAspect;
200 libvlc_media_list_view_t * p_mlv = libvlc_media_list_hierarchical_view( p_mlist, NULL );
201 hierarchicalAspect = [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_mlv];
202 libvlc_media_list_view_release( p_mlv );
203 return hierarchicalAspect;
206 - (VLCMediaListAspect *)flatAspect
208 VLCMediaListAspect * flatAspect;
209 libvlc_media_list_view_t * p_mlv = libvlc_media_list_flat_view( p_mlist, NULL );
210 flatAspect = [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_mlv];
211 libvlc_media_list_view_release( p_mlv );
217 @implementation VLCMediaList (LibVLCBridging)
218 + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
220 return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
223 - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
225 if( self = [super init] )
227 p_mlist = p_new_mlist;
228 libvlc_media_list_retain(p_mlist);
229 libvlc_media_list_lock(p_mlist);
230 cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist, NULL)];
231 int i, count = libvlc_media_list_count(p_mlist, NULL);
232 for( i = 0; i < count; i++ )
234 libvlc_media_descriptor_t * p_md = libvlc_media_list_item_at_index(p_mlist, i, NULL);
235 [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
236 libvlc_media_descriptor_release(p_md);
238 [self initInternalMediaList];
239 libvlc_media_list_unlock(p_mlist);
244 - (void *)libVLCMediaList
250 @implementation VLCMediaList (Private)
251 - (void)initInternalMediaList
253 // Add event callbacks
255 libvlc_exception_t p_e;
256 libvlc_exception_init(&p_e);
258 libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlist, &p_e );
259 libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, &p_e );
260 libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, &p_e );
263 quit_on_exception( &p_e );
266 - (void)mediaListItemAdded:(NSDictionary *)args
268 int index = [[args objectForKey:@"index"] intValue];
269 VLCMedia * media = [args objectForKey:@"media"];
271 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
272 [cachedMedia insertObject:media atIndex:index];
273 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
275 // Post the notification
276 [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
280 // Let the delegate know that the item was added
281 if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
282 [delegate mediaList:self mediaAdded:media atIndex:index];
285 - (void)mediaListItemRemoved:(NSNumber *)index
287 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"Media"];
288 [cachedMedia removeObjectAtIndex:[index intValue]];
289 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"Media"];
291 // Post the notification
292 [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
294 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
298 // Let the delegate know that the item is being removed
299 if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
300 [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];