VLCMediaListPlayer.m 8.46 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCMediaListPlayer.m: VLCKit.framework VLCMediaListPlayer implementation
 *****************************************************************************
 * Copyright (C) 2009 Pierre d'Herbemont
5
 * Partial Copyright (C) 2009-2017 Felix Paul Kühne
6
 * Copyright (C) 2009-2019 VLC authors and VideoLAN
7 8 9
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10 11
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *          Soomin Lee <bubu # mikan.io>
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 32

#import "VLCMediaListPlayer.h"
#import "VLCMedia.h"
#import "VLCMediaPlayer.h"
#import "VLCMediaList.h"
#import "VLCLibVLCBridging.h"
33
#import "VLCLibrary.h"
34
#import "VLCEventManager.h"
35

36 37 38 39 40 41
@interface VLCMediaListPlayer () {
    void *instance;
    VLCMedia *_rootMedia;
    VLCMediaPlayer *_mediaPlayer;
    VLCMediaList *_mediaList;
    VLCRepeatMode _repeatMode;
42
    dispatch_queue_t _libVLCBackgroundQueue;
43 44 45
}
@end

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
static void HandleMediaListPlayerPlayed(const libvlc_event_t * event, void * self)
{
    @autoreleasepool {
        [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                     withMethod:@selector(mediaListPlayerPlayed)
                                           withArgumentAsObject:nil];
    }
}

static void HandleMediaListPlayerNextItemSet(const libvlc_event_t * event, void * self)
{
    @autoreleasepool {
        VLCMedia *media = [[VLCMedia alloc]
                           initWithLibVLCMediaDescriptor:event->u.media_list_player_next_item_set.item];

        [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                     withMethod:@selector(mediaListPlayerNextItemSet:)
                                           withArgumentAsObject:media];
    }
}

static void HandleMediaListPlayerStopped(const libvlc_event_t * event, void * self)
{
    @autoreleasepool {
        [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                     withMethod:@selector(mediaListPlayerStopped)
                                           withArgumentAsObject:nil];
    }
}

76
@implementation VLCMediaListPlayer
77

78
- (instancetype)initWithOptions:(NSArray *)options andDrawable:(id)drawable
79
{
80
    if (self = [super init]) {
81
        _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
82

83 84 85 86 87
        VLCLibrary *library;
        if (options != nil) {
            library = [[VLCLibrary alloc] initWithOptions:options];
        } else
            library = [VLCLibrary sharedLibrary];
88

89
        instance = libvlc_media_list_player_new([library instance]);
90

91
        _mediaPlayer = [[VLCMediaPlayer alloc] initWithLibVLCInstance:libvlc_media_list_player_get_media_player(instance) andLibrary:library];
92 93
        if (drawable != nil)
            [_mediaPlayer setDrawable:drawable];
94 95

        [self registerObservers];
96 97
    }
    return self;
98
}
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
- (void)registerObservers
{
    __block libvlc_event_manager_t * p_em = libvlc_media_list_player_event_manager(instance);

    if (!p_em) {
        return;
    }

    dispatch_sync(_libVLCBackgroundQueue,^{
        libvlc_event_attach(p_em, libvlc_MediaListPlayerPlayed,
                            HandleMediaListPlayerPlayed, (__bridge void *)(self));
        libvlc_event_attach(p_em, libvlc_MediaListPlayerNextItemSet,
                            HandleMediaListPlayerNextItemSet, (__bridge void *)(self));
        libvlc_event_attach(p_em, libvlc_MediaListPlayerStopped,
                            HandleMediaListPlayerStopped, (__bridge void *)(self));
    });
}

- (void)unregisterObservers
{
    libvlc_event_manager_t * p_em = libvlc_media_list_player_event_manager(instance);

    if (!p_em) {
        return;
    }

    libvlc_event_detach(p_em, libvlc_MediaListPlayerPlayed,
                        HandleMediaListPlayerPlayed, (__bridge void *)(self));
    libvlc_event_detach(p_em, libvlc_MediaListPlayerNextItemSet,
                        HandleMediaListPlayerNextItemSet, (__bridge void *)(self));
    libvlc_event_detach(p_em, libvlc_MediaListPlayerStopped,
                        HandleMediaListPlayerStopped, (__bridge void *)(self));
}

134 135 136 137 138
- (instancetype)initWithOptions:(NSArray *)options
{
    return [self initWithOptions:options andDrawable:nil];
}

139
- (instancetype)init
140
{
141 142 143 144 145 146
    return [self initWithOptions:nil andDrawable:nil];
}

- (instancetype)initWithDrawable:(id)drawable
{
    return [self initWithOptions:nil andDrawable:drawable];
147 148 149 150
}

- (void)dealloc
{
151
    [_mediaPlayer stop];
152
    [self unregisterObservers];
153
    libvlc_media_list_player_release(instance);
154
}
155

156 157 158 159 160 161 162 163 164
- (VLCMediaPlayer *)mediaPlayer
{
    return _mediaPlayer;
}

- (void)setMediaList:(VLCMediaList *)mediaList
{
    if (_mediaList == mediaList)
        return;
165
    _mediaList = mediaList;
166 167

    libvlc_media_list_player_set_media_list(instance, [mediaList libVLCMediaList]);
168 169 170 171 172 173 174 175 176 177 178 179 180 181
    [self willChangeValueForKey:@"rootMedia"];
    _rootMedia = nil;
    [self didChangeValueForKey:@"rootMedia"];
}

- (VLCMediaList *)mediaList
{
    return _mediaList;
}

- (void)setRootMedia:(VLCMedia *)media
{
    if (_rootMedia == media)
        return;
182 183
    _rootMedia = nil;

184 185 186
    VLCMediaList *mediaList = [[VLCMediaList alloc] init];
    if (media)
        [mediaList addMedia:media];
187 188

    // This will clean rootMedia
189
    [self setMediaList:mediaList];
190 191

    // Thus set rootMedia here.
192
    _rootMedia = media;
193

194 195 196 197 198 199 200
}

- (VLCMedia *)rootMedia
{
    return _rootMedia;
}

201 202
- (void)playMedia:(VLCMedia *)media
{
203 204 205
    dispatch_async(_libVLCBackgroundQueue, ^{
        libvlc_media_list_player_play_item(instance, [media libVLCMediaDescriptor]);
    });
206 207
}

208 209
- (void)play
{
210 211 212
    dispatch_async(_libVLCBackgroundQueue, ^{
        libvlc_media_list_player_play(instance);
    });
213 214
}

215 216
- (void)pause
{
217 218 219
    dispatch_async(_libVLCBackgroundQueue, ^{
        libvlc_media_list_player_set_pause(instance, 1);
    });
220 221
}

222 223
- (void)stop
{
224 225 226
    dispatch_async(_libVLCBackgroundQueue, ^{
        libvlc_media_list_player_stop(instance);
    });
227 228
}

229
- (BOOL)next
230
{
231
    return libvlc_media_list_player_next(instance) == 0 ? YES : NO;
232 233
}

234
- (BOOL)previous
235
{
236
    return libvlc_media_list_player_previous(instance) == 0 ? YES : NO;
237 238
}

239
- (BOOL)playItemAtIndex:(int)index
240
{
241
    return libvlc_media_list_player_play_item_at_index(instance, index) == 0 ? YES : NO;
242 243
}

244 245
- (void)playItemAtNumber:(NSNumber *)index
{
246 247 248
    dispatch_async(_libVLCBackgroundQueue, ^{
        libvlc_media_list_player_play_item_at_index(instance, [index intValue]);
    });
249 250
}

251
- (void)setRepeatMode:(VLCRepeatMode)repeatMode
252
{
253
    libvlc_playback_mode_t mode;
254 255
    switch (repeatMode) {
        case VLCRepeatAllItems:
256
            mode = libvlc_playback_mode_loop;
257 258
            break;
        case VLCDoNotRepeat:
259
            mode = libvlc_playback_mode_default;
260 261
            break;
        case VLCRepeatCurrentItem:
262
            mode = libvlc_playback_mode_repeat;
263 264
            break;
        default:
265
            NSAssert(0, @"Should not be reached");
266 267
            break;
    }
268 269
    libvlc_media_list_player_set_playback_mode(instance, mode);

270
    _repeatMode = repeatMode;
271 272
}

273
- (VLCRepeatMode)repeatMode
274
{
275
    return _repeatMode;
276
}
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

#pragma mark - Delegate methods

- (void)mediaListPlayerPlayed
{
    if ([_delegate respondsToSelector:@selector(mediaListPlayerPlayed)]) {
        [_delegate mediaListPlayerFinishedPlayback:self];
    }
}

- (void)mediaListPlayerNextItemSet:(VLCMedia *)media
{
    if ([_delegate respondsToSelector:@selector(mediaListPlayer:nextMedia:)]) {
        [_delegate mediaListPlayer:self nextMedia:media];
    }
}

- (void)mediaListPlayerStopped
{
    if ([_delegate respondsToSelector:@selector(mediaListPlayerStopped)]) {
        [_delegate mediaListPlayerStopped:self];
    }
}

301
@end