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 p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance]);
82 // Initialize internals to defaults
83 cachedMedia = [[NSMutableArray alloc] init];
84 [self initInternalMediaList];
89 - (id)initWithArray:(NSArray *)array
95 for (VLCMedia *media in array)
96 [self addMedia:media];
104 if([self retainCount] <= 1)
106 /* We must make sure we won't receive new event after an upcoming dealloc
107 * We also may receive a -retain in some event callback that may occcur
108 * Before libvlc_event_detach. So this can't happen in dealloc */
109 libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
110 libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
111 libvlc_event_detach(p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
119 // Release allocated memory
122 libvlc_media_list_release( p_mlist );
123 [cachedMedia release];
127 - (NSString *)description
129 NSMutableString * content = [NSMutableString string];
131 for( i = 0; i < [self count]; i++)
133 [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
135 return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self className], self, content];
140 libvlc_media_list_lock( p_mlist );
145 libvlc_media_list_unlock( p_mlist );
148 - (NSInteger)addMedia:(VLCMedia *)media
150 int index = [self count];
151 [self insertMedia:media atIndex:index];
155 - (void)insertMedia:(VLCMedia *)media atIndex: (NSInteger)index
159 // Add it to the libvlc's medialist
160 libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], index);
163 - (void)removeMediaAtIndex:(NSInteger)index
165 [[self mediaAtIndex:index] release];
167 // Remove it from the libvlc's medialist
168 libvlc_media_list_remove_index(p_mlist, index);
171 - (VLCMedia *)mediaAtIndex:(NSInteger)index
173 return [cachedMedia objectAtIndex:index];
176 - (NSInteger)indexOfMedia:(VLCMedia *)media
178 NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
182 /* KVC Compliance: For the @"media" key */
183 - (NSInteger)countOfMedia
188 - (id)objectInMediaAtIndex:(NSUInteger)i
190 return [self mediaAtIndex:i];
195 return [cachedMedia count];
198 - (void)insertObject:(id)object inMediaAtIndex:(NSUInteger)i
200 [self insertMedia:object atIndex:i];
203 - (void)removeObjectFromMediaAtIndex:(NSUInteger)i
205 [self removeMediaAtIndex:i];
208 @synthesize delegate;
212 return libvlc_media_list_is_readonly( p_mlist );
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)];
232 NSUInteger i, count = libvlc_media_list_count(p_mlist);
233 for( i = 0; i < count; i++ )
235 libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
236 [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
237 libvlc_media_release(p_md);
239 [self initInternalMediaList];
240 libvlc_media_list_unlock(p_mlist);
245 - (void *)libVLCMediaList
251 @implementation VLCMediaList (Private)
252 - (void)initInternalMediaList
254 // Add event callbacks
255 libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist);
256 libvlc_event_attach( p_em, libvlc_MediaListItemAdded, HandleMediaListItemAdded, self);
257 libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self);
260 - (void)mediaListItemAdded:(NSArray *)arrayOfArgs
262 /* We hope to receive index in a nide range, that could change one day */
263 NSInteger start = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
264 NSInteger end = [[[arrayOfArgs objectAtIndex: [arrayOfArgs count]-1] objectForKey:@"index"] intValue];
265 NSRange range = NSMakeRange(start, end-start);
267 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
268 for( NSDictionary * args in arrayOfArgs )
270 NSInteger index = [[args objectForKey:@"index"] intValue];
271 VLCMedia * media = [args objectForKey:@"media"];
273 if( index && index > [cachedMedia count] )
274 index = [cachedMedia count];
275 [cachedMedia insertObject:media atIndex:index];
277 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
279 // Post the notification
280 // [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
284 // Let the delegate know that the item was added
285 // if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
286 // [delegate mediaList:self mediaAdded:media atIndex:index];
289 - (void)mediaListItemRemoved:(NSNumber *)index
291 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
292 [cachedMedia removeObjectAtIndex:[index intValue]];
293 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
295 // Post the notification
296 [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
298 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
302 // Let the delegate know that the item is being removed
303 if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
304 [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];