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)
40 - (void)initInternalMediaList;
42 /* Libvlc event bridges */
43 - (void)mediaListItemAdded:(NSArray *)args;
44 - (void)mediaListItemRemoved:(NSNumber *)index;
48 /* libvlc event callback */
49 static void HandleMediaListItemAdded(const libvlc_event_t *event, void *user_data)
53 // Check to see if the last item added is this item we're trying to introduce
54 // If no, then add the item to the local list, otherwise, the item has already
56 [[VLCEventManager sharedManager] callOnMainThreadObject:self
57 withMethod:@selector(mediaListItemAdded:)
58 withArgumentAsObject:[NSArray arrayWithObjects:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item],
59 [NSNumber numberWithInt:event->u.media_list_item_added.index], nil]];
62 static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
66 // Check to see if the last item deleted is this item we're trying delete now.
67 // If no, then delete the item from the local list, otherwise, the item has already
69 [[VLCEventManager sharedManager] callOnMainThreadObject:self
70 withMethod:@selector(mediaListItemRemoved:)
71 withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
74 @implementation VLCMediaList
78 if (self = [super init])
80 // Create a new libvlc media list instance
81 libvlc_exception_t p_e;
82 libvlc_exception_init(&p_e);
83 p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance], &p_e);
84 quit_on_exception(&p_e);
86 // Initialize internals to defaults
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, NULL);
103 libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, NULL);
104 libvlc_event_detach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, NULL);
112 // Release allocated memory
113 libvlc_media_list_release(p_mlist);
118 - (void)setDelegate:(id)value
130 libvlc_media_list_lock(p_mlist);
135 libvlc_media_list_unlock(p_mlist);
138 - (int)addMedia:(VLCMedia *)media
140 int index = [self count];
141 [self insertMedia:media atIndex:index];
145 - (void)insertMedia:(VLCMedia *)media atIndex: (int)index
149 // Add it to the libvlc's medialist
150 libvlc_exception_t p_e;
151 libvlc_exception_init( &p_e );
152 libvlc_media_list_insert_media_descriptor(p_mlist, [media libVLCMediaDescriptor], index, &p_e);
153 quit_on_exception(&p_e);
156 - (void)removeMediaAtIndex:(int)index
158 [[self mediaAtIndex:index] release];
160 // Remove it from the libvlc's medialist
161 libvlc_exception_t p_e;
162 libvlc_exception_init(&p_e);
163 libvlc_media_list_remove_index(p_mlist, index, &p_e);
164 quit_on_exception(&p_e);
167 - (VLCMedia *)mediaAtIndex:(int)index
169 libvlc_exception_t p_e;
170 libvlc_exception_init(&p_e);
171 libvlc_media_descriptor_t *p_md = libvlc_media_list_item_at_index(p_mlist, index, &p_e);
172 quit_on_exception(&p_e);
174 // Returns local object for media descriptor, searchs for user data first. If not found it creates a
175 // new cocoa object representation of the media descriptor.
176 return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
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);
189 - (int)indexOfMedia:(VLCMedia *)media
191 libvlc_exception_t p_e;
192 libvlc_exception_init(&p_e);
193 int result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor], &p_e);
194 quit_on_exception(&p_e);
199 - (NSArray *)sublists
201 NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity: 0];
204 libvlc_exception_t p_e;
205 libvlc_exception_init(&p_e);
206 count = libvlc_media_list_count(p_mlist, &p_e);
207 quit_on_exception(&p_e);
209 for(i = 0; i < count; i++)
211 libvlc_media_descriptor_t *p_md;
212 libvlc_media_list_t *p_submlist;
213 p_md = libvlc_media_list_item_at_index(p_mlist, i, NULL);
214 p_submlist = libvlc_media_descriptor_subitems(p_md, NULL);
217 [ret addObject:[VLCMediaList medialistWithLibVLCMediaList:p_submlist]];
218 libvlc_media_list_release(p_submlist);
220 libvlc_media_descriptor_release(p_md);
222 return [ret autorelease];
225 //- (VLCMediaList *)flatPlaylist
227 // VLCMediaList * flatPlaylist;
228 // libvlc_exception_t p_e;
229 // libvlc_exception_init( &p_e );
230 // libvlc_media_list_t * p_flat_mlist = libvlc_media_list_flat_media_list( p_mlist, &p_e );
231 // quit_on_exception( &p_e );
232 // flatPlaylist = [VLCMediaList medialistWithLibVLCMediaList: p_flat_mlist];
233 // libvlc_media_list_release( p_flat_mlist );
234 // return flatPlaylist;
237 //- (VLCMedia *)providerMedia
240 // libvlc_exception_t p_e;
241 // libvlc_exception_init( &p_e );
242 // libvlc_media_descriptor_t * p_md = libvlc_media_list_media_descriptor( p_mlist, &p_e );
243 // ret = [VLCMedia mediaWithLibVLCMediaDescriptor: p_md];
244 // libvlc_media_descriptor_release( p_md );
245 // quit_on_exception( &p_e );
251 @implementation VLCMediaList (LibVLCBridging)
253 + (id)medialistWithLibVLCMediaList:(void *)p_new_mlist;
255 return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
258 - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
260 if( self = [super init] )
262 p_mlist = p_new_mlist;
263 libvlc_media_list_retain(p_mlist);
264 [self initInternalMediaList];
269 - (void *)libVLCMediaList
276 @implementation VLCMediaList (Private)
278 - (void)initInternalMediaList
280 // Add event callbacks
282 libvlc_exception_t p_e;
283 libvlc_exception_init(&p_e);
285 libvlc_event_manager_t *p_em = libvlc_media_list_event_manager(p_mlist, &p_e);
286 libvlc_event_attach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self, &p_e);
287 libvlc_event_attach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, &p_e);
290 quit_on_exception( &p_e );
293 - (void)mediaListItemAdded:(NSArray *)args
295 VLCMedia *media = [args objectAtIndex:0];
296 NSNumber *index = [args objectAtIndex:1];
298 // Post the notification
299 [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
301 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
306 // Let the delegate know that the item was added
307 if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
308 [delegate mediaList:self mediaAdded:media atIndex:[index intValue]];
311 - (void)mediaListItemRemoved:(NSNumber *)index
313 // Post the notification
314 [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
316 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
320 // Let the delegate know that the item is being removed
321 if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
322 [delegate mediaList:self mediaRemovedAtIndex:index];