VLCMediaList.m 11.3 KB
Newer Older
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, 2017 Felix Paul Kühne
7
 * Copyright (C) 2018 Carola Nitz
8 9 10
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
11
 *          Felix Paul Kühne <fkuehne # videolan.org>
12
 *
13 14 15
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
16 17 18 19
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
22
 *
23 24 25
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 27 28 29 30 31
 *****************************************************************************/

#import "VLCMediaList.h"
#import "VLCLibrary.h"
#import "VLCEventManager.h"
#import "VLCLibVLCBridging.h"
32 33 34 35
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

36 37 38 39
#include <vlc/vlc.h>
#include <vlc/libvlc.h>

/* Notification Messages */
40 41
NSString *const VLCMediaListItemAdded        = @"VLCMediaListItemAdded";
NSString *const VLCMediaListItemDeleted      = @"VLCMediaListItemDeleted";
42 43

// TODO: Documentation
44
@interface VLCMediaList (Private)
45

46 47 48 49
/* Initializers */
- (void)initInternalMediaList;

/* Libvlc event bridges */
50
- (void)mediaListItemAdded:(NSArray *)args;
51 52 53 54
- (void)mediaListItemRemoved:(NSNumber *)index;
@end

/* libvlc event callback */
55
static void HandleMediaListItemAdded(const libvlc_event_t * event, void * user_data)
56
{
57 58 59 60
    @autoreleasepool {
        id self = (__bridge id)(user_data);
        [[VLCEventManager sharedManager] callOnMainThreadObject:self
                                                     withMethod:@selector(mediaListItemAdded:)
61
                                           withArgumentAsObject:@[@{@"media": [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item],
62 63
                                                              @"index": @(event->u.media_list_item_added.index)}]];
    }
64 65 66 67
}

static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
{
68 69 70 71
    @autoreleasepool {
        id self = (__bridge id)(user_data);
        [[VLCEventManager sharedManager] callOnMainThreadObject:self
                                                     withMethod:@selector(mediaListItemRemoved:)
72
                                           withArgumentAsObject:@(event->u.media_list_item_deleted.index)];
73
    }
74 75
}

76 77
@interface VLCMediaList()
{
78 79
    void * p_mlist;                                 ///< Internal instance of media list
    id <VLCMediaListDelegate,NSObject> __weak delegate;    ///< Delegate object
80
    /* We need that private copy because of Cocoa Bindings, that need to be working on first thread */
81
    NSMutableArray *_mediaObjects;                   ///< Private copy of media objects.
82
    dispatch_queue_t _serialMediaObjectsQueue;      ///< Queue for accessing and modifying the mediaobjects
83 84 85
}
@end

86
@implementation VLCMediaList
87
- (instancetype)init
88
{
89
    if (self = [super init]) {
90
        // Create a new libvlc media list instance
91
        p_mlist = libvlc_media_list_new([VLCLibrary sharedLibrary].instance);
92

93 94
        // Initialize internals to defaults
        _mediaObjects = [[NSMutableArray alloc] init];
95
        _serialMediaObjectsQueue = dispatch_queue_create("org.videolan.serialMediaObjectsQueue",  DISPATCH_QUEUE_SERIAL);
96 97
        [self initInternalMediaList];
    }
98 99 100 101

    return self;
}

102
- (instancetype)initWithArray:(NSArray *)array
103 104 105 106 107 108 109 110
{
    if (self = [self init]) {
        /* do something useful with the provided array */
        NSUInteger count = [array count];
        for (NSUInteger x = 0; x < count; x++)
            [self addMedia:array[x]];
    }

111 112 113 114 115
    return self;
}

- (void)dealloc
{
Pierre's avatar
Pierre committed
116
    libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
117 118 119 120
    if (em) {
        libvlc_event_detach(em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
        libvlc_event_detach(em, libvlc_MediaListItemAdded,   HandleMediaListItemAdded,   (__bridge void *)(self));
    }
Pierre's avatar
Pierre committed
121 122
    [[VLCEventManager sharedManager] cancelCallToObject:self];

123
    // Release allocated memory
124
    delegate = nil;
125

126
    libvlc_media_list_release( p_mlist );
127 128
}

129 130
- (NSString *)description
{
131
    NSMutableString * content = [NSMutableString string];
132
    for (NSInteger i = 0; i < [self count]; i++) {
133 134
        [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
    }
Pierre's avatar
Pierre committed
135
    return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self class], self, content];
136 137
}

138 139
- (void)lock
{
140
    libvlc_media_list_lock( p_mlist );
141 142 143 144
}

- (void)unlock
{
145
    libvlc_media_list_unlock( p_mlist );
146 147
}

148
- (NSUInteger)addMedia:(VLCMedia *)media
149
{
150
    NSInteger index = [self count];
151 152
    [self insertMedia:media atIndex:index];
    return index;
153 154
}

155
- (void)insertMedia:(VLCMedia *)media atIndex: (NSUInteger)index
156
{
157
    // Add the media object to our cache
158 159 160
    dispatch_sync(_serialMediaObjectsQueue, ^{
        [_mediaObjects insertObject:media atIndex:index];
    });
161

162
    // Add it to libvlc's medialist
163
    libvlc_media_list_insert_media(p_mlist, [media libVLCMediaDescriptor], (int)index);
164 165
}

166
- (void)removeMediaAtIndex:(NSUInteger)index
167
{
168 169 170 171 172 173
    dispatch_sync(_serialMediaObjectsQueue, ^{
        if (index >= [_mediaObjects count])
            return;
        //remove from cached Media
        [_mediaObjects removeObjectAtIndex:index];
    });
174

175 176
    // Remove it from libvlc's medialist
    libvlc_media_list_remove_index(p_mlist, (int)index);
177 178
}

179
- (VLCMedia *)mediaAtIndex:(NSUInteger)index
180
{
181 182 183 184 185
    __block VLCMedia *media;
    dispatch_sync(_serialMediaObjectsQueue, ^{
        media = index >= [_mediaObjects count] ? nil : [_mediaObjects objectAtIndex:index];
    });
    return media;
186 187
}

188
- (NSInteger)indexOfMedia:(VLCMedia *)media
189
{
190
    NSInteger result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor]);
191
    return result;
192
}
193

194
/* KVC Compliance: For the @"media" key */
195
- (NSInteger)countOfMedia
196 197 198 199
{
    return [self count];
}

200
- (VLCMedia *)objectInMediaAtIndex:(NSUInteger)i
201 202 203 204
{
    return [self mediaAtIndex:i];
}

205
- (NSInteger)count
206
{
207 208 209 210 211
    __block NSInteger count;
    dispatch_sync(_serialMediaObjectsQueue, ^{
        count = [_mediaObjects count];
    });
    return count;
212 213
}

214
- (void)insertObject:(VLCMedia *)object inMediaAtIndex:(NSUInteger)i
215 216 217 218 219 220 221 222 223
{
    [self insertMedia:object atIndex:i];
}

- (void)removeObjectFromMediaAtIndex:(NSUInteger)i
{
    [self removeMediaAtIndex:i];
}

224 225
@synthesize delegate;

226 227
- (BOOL)isReadOnly
{
228
    return libvlc_media_list_is_readonly( p_mlist );
229 230
}

231 232 233
@end

@implementation VLCMediaList (LibVLCBridging)
234
+ (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
235
{
236
    return [[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist];
237 238 239 240
}

- (id)initWithLibVLCMediaList:(void *)p_new_mlist;
{
241
    if (self = [super init]) {
242
        p_mlist = p_new_mlist;
243 244
        libvlc_media_list_retain( p_mlist );
        libvlc_media_list_lock( p_mlist );
245
        _mediaObjects = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist)];
246
        _serialMediaObjectsQueue = dispatch_queue_create("org.videolan.serialMediaObjectsQueue", NULL);
247
        NSUInteger count = libvlc_media_list_count(p_mlist);
248
        for (int i = 0; i < count; i++) {
249
            libvlc_media_t * p_md = libvlc_media_list_item_at_index(p_mlist, i);
250 251 252
            dispatch_sync(_serialMediaObjectsQueue, ^{
                [_mediaObjects addObject:[VLCMedia mediaWithLibVLCMediaDescriptor:p_md]];
            });
253
            libvlc_media_release(p_md);
254
        }
255
        [self initInternalMediaList];
256
        libvlc_media_list_unlock(p_mlist);
257 258 259 260 261 262 263 264 265 266
    }
    return self;
}

- (void *)libVLCMediaList
{
    return p_mlist;
}
@end

267
@implementation VLCMediaList (Private)
268 269
- (void)initInternalMediaList
{
270
    // Add event callbacks
271 272 273 274
    libvlc_event_manager_t *em = libvlc_media_list_event_manager(p_mlist);
    if (!em)
        return;

275 276 277 278 279 280
    /* We need the caller to wait until this block is done.
     * The initialized object shall not be returned until the event attachments are done. */
    dispatch_sync(_serialMediaObjectsQueue,^{
        libvlc_event_attach( em, libvlc_MediaListItemAdded,   HandleMediaListItemAdded,   (__bridge void *)(self));
        libvlc_event_attach( em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, (__bridge void *)(self));
    });
281 282
}

283
- (void)mediaListItemAdded:(NSArray *)arrayOfArgs
284
{
285
    /* We hope to receive index in a nide range, that could change one day */
286 287
    NSInteger start = [arrayOfArgs[0][@"index"] intValue];
    NSInteger end = [arrayOfArgs[[arrayOfArgs count]-1][@"index"] intValue];
288
    NSRange range = NSMakeRange(start, end-start);
289

290
    [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
291
    for (NSDictionary *args in arrayOfArgs) {
292
        NSInteger index = [args[@"index"] intValue];
293
        VLCMedia *tempMedia = args[@"media"];
294 295 296 297 298 299 300 301
        __block VLCMedia *foundMedia;
        //we have two instances of VLCMedia. One from the event and the one we added to _mediaObjects, hence check them to avoid duplication
        dispatch_sync(_serialMediaObjectsQueue, ^{
            for (VLCMedia *media in _mediaObjects) {
                if ([tempMedia.url isEqual:media.url]){
                    foundMedia = media;
                    break;
                }
302
            }
303
        });
304 305 306 307

        if (!foundMedia) {
            // In case we found Media on the network we don't have a cached copy yet
            foundMedia = tempMedia;
308 309 310
            dispatch_sync(_serialMediaObjectsQueue, ^{
                index >= [_mediaObjects count] ? [_mediaObjects addObject:foundMedia] : [_mediaObjects insertObject:foundMedia atIndex:index];
            });
311
        }
312
        if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
313
            [delegate mediaList:self mediaAdded:foundMedia atIndex:index];
314 315
    }
    [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
316 317
}

318
- (void)mediaListItemRemoved:(NSNumber *)index
319
{
320
    [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
321 322 323
    dispatch_sync(_serialMediaObjectsQueue, ^{
        [_mediaObjects removeObjectAtIndex:[index intValue]];
    });
324
    [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
325

326 327 328 329
    // Post the notification
    [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted
                                                        object:self
                                                      userInfo:@{@"index": index}];
330

331 332 333
    // Let the delegate know that the item is being removed
    if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
        [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
334

335 336
}
@end