VLCMediaPlayer.m 45.3 KB
Newer Older
1
/*****************************************************************************
2
 * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
3
 *****************************************************************************
4
 * Copyright (C) 2007-2009 Pierre d'Herbemont
5 6
 * Copyright (C) 2007-2015 VLC authors and VideoLAN
 * Partial Copyright (C) 2009-2015 Felix Paul Kühne
7 8 9 10
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *          Faustion Osuna <enrique.osuna # gmail.com>
11
 *          Felix Paul Kühne <fkuehne # videolan.org>
12
 *          Soomin Lee <TheHungryBu # gmail.com>
13
 *
14 15 16
 * 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
17 18 19 20
 * (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
21 22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
23
 *
24 25 26
 * 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.
27 28 29 30 31 32
 *****************************************************************************/

#import "VLCLibrary.h"
#import "VLCMediaPlayer.h"
#import "VLCEventManager.h"
#import "VLCLibVLCBridging.h"
Pierre's avatar
Pierre committed
33 34 35
#if !TARGET_OS_IPHONE
# import "VLCVideoView.h"
#endif
36 37 38 39
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Pierre's avatar
Pierre committed
40
#if !TARGET_OS_IPHONE
41
/* prevent system sleep */
Pierre's avatar
Pierre committed
42
# import <CoreServices/CoreServices.h>
43
/* FIXME: Ugly hack! */
Pierre's avatar
Pierre committed
44 45 46
# ifdef __x86_64__
#  import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
# endif
47
#endif
48

49 50 51
#include <vlc/vlc.h>

/* Notification Messages */
52 53
NSString *const VLCMediaPlayerTimeChanged       = @"VLCMediaPlayerTimeChanged";
NSString *const VLCMediaPlayerStateChanged      = @"VLCMediaPlayerStateChanged";
54 55
NSString *const VLCMediaPlayerTitleChanged       = @"VLCMediaPlayerTitleChanged";
NSString *const VLCMediaPlayerChapterChanged      = @"VLCMediaPlayerChapterChanged";
56
NSString *const VLCMediaPlayerSnapshotTaken     = @"VLCMediaPlayerSnapshotTaken";
57 58 59 60 61 62 63 64 65 66

/* title keys */
NSString *const VLCTitleDescriptionName         = @"VLCTitleDescriptionName";
NSString *const VLCTitleDescriptionDuration     = @"VLCTitleDescriptionDuration";
NSString *const VLCTitleDescriptionIsMenu       = @"VLCTitleDescriptionIsMenu";

/* chapter keys */
NSString *const VLCChapterDescriptionName       = @"VLCChapterDescriptionName";
NSString *const VLCChapterDescriptionTimeOffset = @"VLCChapterDescriptionTimeOffset";
NSString *const VLCChapterDescriptionDuration   = @"VLCChapterDescriptionDuration";
67

68
NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
69
{
70
    static NSString * stateToStrings[] = {
71 72 73 74 75 76 77 78 79 80 81
        [VLCMediaPlayerStateStopped]      = @"VLCMediaPlayerStateStopped",
        [VLCMediaPlayerStateOpening]      = @"VLCMediaPlayerStateOpening",
        [VLCMediaPlayerStateBuffering]    = @"VLCMediaPlayerStateBuffering",
        [VLCMediaPlayerStateEnded]        = @"VLCMediaPlayerStateEnded",
        [VLCMediaPlayerStateError]        = @"VLCMediaPlayerStateError",
        [VLCMediaPlayerStatePlaying]      = @"VLCMediaPlayerStatePlaying",
        [VLCMediaPlayerStatePaused]       = @"VLCMediaPlayerStatePaused"
    };
    return stateToStrings[state];
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
// TODO: Documentation
@interface VLCMediaPlayer (Private)

- (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options;

- (void)registerObservers;
- (void)unregisterObservers;
- (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
- (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
- (void)mediaPlayerStateChanged:(NSNumber *)newState;
- (void)mediaPlayerMediaChanged:(VLCMedia *)media;
- (void)mediaPlayerTitleChanged:(NSNumber *)newTitle;
- (void)mediaPlayerChapterChanged:(NSNumber *)newChapter;

#if TARGET_OS_IPHONE
- (void)mediaPlayerSnapshot:(NSString *)fileName;
#endif
@end

101 102
static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
{
103 104 105 106 107 108 109 110 111
    @autoreleasepool {
        [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                     withMethod:@selector(mediaPlayerTimeChanged:)
                                           withArgumentAsObject:@(event->u.media_player_time_changed.new_time)];

        [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
                                                       withDelegateMethod:@selector(mediaPlayerTimeChanged:)
                                                     withNotificationName:VLCMediaPlayerTimeChanged];
    }
112 113
}

114 115
static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
{
116
    @autoreleasepool {
117

118 119 120 121
        [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                     withMethod:@selector(mediaPlayerPositionChanged:)
                                           withArgumentAsObject:@(event->u.media_player_position_changed.new_position)];
    }
122 123
}

124
static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
125
{
126
    VLCMediaPlayerState newState;
127

128
    if (event->type == libvlc_MediaPlayerPlaying)
129
        newState = VLCMediaPlayerStatePlaying;
130
    else if (event->type == libvlc_MediaPlayerPaused)
131
        newState = VLCMediaPlayerStatePaused;
132
    else if (event->type == libvlc_MediaPlayerStopped)
133
        newState = VLCMediaPlayerStateStopped;
134
    else if (event->type == libvlc_MediaPlayerEncounteredError)
135
        newState = VLCMediaPlayerStateError;
136
    else if (event->type == libvlc_MediaPlayerBuffering)
137
        newState = VLCMediaPlayerStateBuffering;
138
    else if (event->type == libvlc_MediaPlayerOpening)
139
        newState = VLCMediaPlayerStateOpening;
140 141
    else if (event->type == libvlc_MediaPlayerEndReached)
        newState = VLCMediaPlayerStateEnded;
142
    else {
143
        VKLog(@"%s: Unknown event", __FUNCTION__);
144 145
        return;
    }
146

147
    @autoreleasepool {
148

149 150 151
        [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                     withMethod:@selector(mediaPlayerStateChanged:)
                                           withArgumentAsObject:@(newState)];
152

153 154 155
        [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
                                                       withDelegateMethod:@selector(mediaPlayerStateChanged:)
                                                     withNotificationName:VLCMediaPlayerStateChanged];
156

157
    }
158
}
159

160 161
static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
{
162
    @autoreleasepool {
163

164 165 166
        [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                     withMethod:@selector(mediaPlayerMediaChanged:)
                                           withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
167

168
    }
169 170
}

171 172 173 174
static void HandleMediaTitleChanged(const libvlc_event_t * event, void * self)
{
    @autoreleasepool {
        [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
175
                                                       withDelegateMethod:@selector(mediaPlayerTitleChanged:)
176 177 178 179 180 181 182 183 184 185 186 187 188
                                                     withNotificationName:VLCMediaPlayerTitleChanged];
    }
}

static void HandleMediaChapterChanged(const libvlc_event_t * event, void * self)
{
    @autoreleasepool {
        [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
                                                       withDelegateMethod:@selector(mediaPlayerChapterChanged:)
                                                     withNotificationName:VLCMediaPlayerChapterChanged];
    }
}

189 190 191 192 193 194 195 196
#if TARGET_OS_IPHONE
static void HandleMediaPlayerSnapshot(const libvlc_event_t * event, void * self)
{
    @autoreleasepool {
        if (event->u.media_player_snapshot_taken.psz_filename != NULL) {
            [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
                                                         withMethod:@selector(mediaPlayerSnapshot:)
                                               withArgumentAsObject:[NSString stringWithUTF8String:event->u.media_player_snapshot_taken.psz_filename]];
197 198 199 200

            [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
                                                           withDelegateMethod:@selector(mediaPlayerSnapshot:)
                                                         withNotificationName:VLCMediaPlayerSnapshotTaken];
201 202 203 204
        }
    }
}
#endif
205

206 207
@interface VLCMediaPlayer ()
{
208 209 210 211 212 213 214 215
    VLCLibrary *_privateLibrary;                ///< Internal
    void * _playerInstance;                     ///< Internal
    VLCMedia * _media;                          ///< Current media being played
    VLCTime * _cachedTime;                      ///< Cached time of the media being played
    VLCTime * _cachedRemainingTime;             ///< Cached remaining time of the media being played
    VLCMediaPlayerState _cachedState;           ///< Cached state of the media being played
    float _position;                            ///< The position of the media being played
    id _drawable;                               ///< The drawable associated to this media player
216
#if TARGET_OS_IPHONE
217
    NSMutableArray *_snapshots;                 ///< Array with snapshot file names
218
#endif
219 220 221
    VLCAudio *_audio;                           ///< The audio controller
    libvlc_equalizer_t *_equalizerInstance;     ///< The equalizer controller
    BOOL _equalizerEnabled;                     ///< Equalizer state
222
    libvlc_video_viewpoint_t *_viewpoint;       ///< Current viewpoint of the media
223 224 225
}
@end

226
@implementation VLCMediaPlayer
227
@synthesize libraryInstance = _privateLibrary;
228

229
/* Bindings */
230 231 232
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
    static NSDictionary * dict = nil;
233
    NSSet * superKeyPaths;
234
    if (!dict) {
235
        dict = @{@"playing": [NSSet setWithObject:@"state"],
236 237
                @"seekable": [NSSet setWithObjects:@"state", @"media", nil],
                @"canPause": [NSSet setWithObjects:@"state", @"media", nil],
238
                @"description": [NSSet setWithObjects:@"state", @"media", nil]};
239
    }
240
    if ((superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key])) {
241
        NSMutableSet * ret = [NSMutableSet setWithSet:dict[key]];
242 243 244
        [ret unionSet:superKeyPaths];
        return ret;
    }
245
    return dict[key];
246
}
247

248
/* Constructor */
249
- (instancetype)init
250
{
251
    return [self initWithDrawable:nil options:nil];
252 253
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
- (instancetype)initWithLibVLCInstance:(void *)playerInstance andLibrary:(VLCLibrary *)library
{
    if (self = [super init]) {
        _cachedTime = [VLCTime nullTime];
        _cachedRemainingTime = [VLCTime nullTime];
        _position = 0.0f;
        _cachedState = VLCMediaPlayerStateStopped;

        _privateLibrary = library;
        libvlc_retain([_privateLibrary instance]);

        _playerInstance = playerInstance;

        [self registerObservers];
    }
    return self;
}

Pierre's avatar
Pierre committed
272
#if !TARGET_OS_IPHONE
273
- (instancetype)initWithVideoView:(VLCVideoView *)aVideoView
274
{
275
    return [self initWithDrawable: aVideoView options:nil];
276
}
277

278
- (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
279
{
280 281 282
    return [self initWithDrawable: aVideoLayer options:nil];
}

283
- (instancetype)initWithVideoView:(VLCVideoView *)aVideoView options:(NSArray *)options
284 285 286 287
{
    return [self initWithDrawable: aVideoView options:options];
}

288
- (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer options:(NSArray *)options
289 290
{
    return [self initWithDrawable: aVideoLayer options:options];
291
}
Pierre's avatar
Pierre committed
292
#endif
293

294
- (instancetype)initWithOptions:(NSArray *)options
295 296 297 298
{
    return [self initWithDrawable:nil options:options];
}

299 300
- (void)dealloc
{
301
    NSAssert(libvlc_media_player_get_state(_playerInstance) == libvlc_Stopped || libvlc_media_player_get_state(_playerInstance) == libvlc_NothingSpecial, @"You released the media player before ensuring that it is stopped");
302

Pierre's avatar
Pierre committed
303 304 305
    [self unregisterObservers];
    [[VLCEventManager sharedManager] cancelCallToObject:self];

306 307
    // Always get rid of the delegate first so we can stop sending messages to it
    // TODO: Should we tell the delegate that we're shutting down?
308
    _delegate = nil;
309

310 311 312
    // Clear our drawable as we are going to release it, we don't
    // want the core to use it from this point. This won't happen as
    // the media player must be stopped.
313
    libvlc_media_player_set_nsobject(_playerInstance, nil);
314

315 316 317 318 319
    if (_equalizerInstance) {
        libvlc_media_player_set_equalizer(_playerInstance, NULL);
        libvlc_audio_equalizer_release(_equalizerInstance);
    }

320 321 322
    if (_viewpoint)
        libvlc_free(_viewpoint);

323
    libvlc_media_player_release(_playerInstance);
324 325
    if (_privateLibrary != [VLCLibrary sharedLibrary])
        libvlc_release(_privateLibrary.instance);
326 327
}

Pierre's avatar
Pierre committed
328
#if !TARGET_OS_IPHONE
329
- (void)setVideoView:(VLCVideoView *)aVideoView
330
{
331
    [self setDrawable: aVideoView];
332 333
}

334
- (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
335
{
336
    [self setDrawable: aVideoLayer];
337
}
Pierre's avatar
Pierre committed
338
#endif
339

340 341 342
- (void)setDrawable:(id)aDrawable
{
    // Make sure that this instance has been associated with the drawing canvas.
343
    libvlc_media_player_set_nsobject(_playerInstance, (__bridge void *)(aDrawable));
344 345 346 347
}

- (id)drawable
{
348
    return (__bridge id)(libvlc_media_player_get_nsobject(_playerInstance));
349 350
}

351 352
- (VLCAudio *)audio
{
353 354 355
    if (!_audio)
        _audio = [[VLCAudio alloc] initWithMediaPlayer:self];
    return _audio;
356 357
}

358 359
#pragma mark -
#pragma mark Video Tracks
360
- (void)setCurrentVideoTrackIndex:(int)value
361
{
362
    libvlc_video_set_track(_playerInstance, value);
363 364
}

365
- (int)currentVideoTrackIndex
366
{
367
    int count = libvlc_video_get_track_count(_playerInstance);
368
    if (count <= 0)
369
        return -1;
370

371
    return libvlc_video_get_track(_playerInstance);
372 373
}

374 375
- (NSArray *)videoTrackNames
{
376
    NSInteger count = libvlc_video_get_track_count(_playerInstance);
377
    if (count <= 0)
378
        return @[];
379

380 381
    libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
    libvlc_track_description_t *currentTrack = firstTrack;
382 383 384

    NSMutableArray *tempArray = [NSMutableArray array];
    while (currentTrack) {
385
        [tempArray addObject:@(currentTrack->psz_name)];
386 387
        currentTrack = currentTrack->p_next;
    }
388
    libvlc_track_description_list_release(firstTrack);
389 390 391 392 393
    return [NSArray arrayWithArray: tempArray];
}

- (NSArray *)videoTrackIndexes
{
394
    NSInteger count = libvlc_video_get_track_count(_playerInstance);
395
    if (count <= 0)
396
        return @[];
397

398 399
    libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
    libvlc_track_description_t *currentTrack = firstTrack;
400 401 402

    NSMutableArray *tempArray = [NSMutableArray array];
    while (currentTrack) {
403
        [tempArray addObject:@(currentTrack->i_id)];
404 405
        currentTrack = currentTrack->p_next;
    }
406
    libvlc_track_description_list_release(firstTrack);
407 408 409
    return [NSArray arrayWithArray: tempArray];
}

410 411 412 413 414
- (int)numberOfVideoTracks
{
    return libvlc_video_get_track_count(_playerInstance);
}

415 416
#pragma mark -
#pragma mark Subtitles
417

418
- (void)setCurrentVideoSubTitleIndex:(int)index
419
{
420
    libvlc_video_set_spu(_playerInstance, index);
421 422
}

423
- (int)currentVideoSubTitleIndex
424
{
425
    NSInteger count = libvlc_video_get_spu_count(_playerInstance);
426

427
    if (count <= 0)
428
        return -1;
429

430
    return libvlc_video_get_spu(_playerInstance);
431 432
}

433 434
- (NSArray *)videoSubTitlesNames
{
435
    NSInteger count = libvlc_video_get_spu_count(_playerInstance);
436
    if (count <= 0)
437
        return @[];
438

439 440
    libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
    libvlc_track_description_t *currentTrack = firstTrack;
441 442 443

    NSMutableArray *tempArray = [NSMutableArray array];
    while (currentTrack) {
444 445
        NSString *track = @(currentTrack->psz_name);
        [tempArray addObject:track != nil ? track : @""];
446 447
        currentTrack = currentTrack->p_next;
    }
448
    libvlc_track_description_list_release(firstTrack);
449 450 451 452 453
    return [NSArray arrayWithArray: tempArray];
}

- (NSArray *)videoSubTitlesIndexes
{
454
    NSInteger count = libvlc_video_get_spu_count(_playerInstance);
455
    if (count <= 0)
456
        return @[];
457

458 459
    libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
    libvlc_track_description_t *currentTrack = firstTrack;
460 461 462

    NSMutableArray *tempArray = [NSMutableArray array];
    while (currentTrack) {
463
        [tempArray addObject:@(currentTrack->i_id)];
464 465
        currentTrack = currentTrack->p_next;
    }
466
    libvlc_track_description_list_release(firstTrack);
467 468 469
    return [NSArray arrayWithArray: tempArray];
}

470 471 472 473 474
- (int)numberOfSubtitlesTracks
{
    return libvlc_video_get_spu_count(_playerInstance);
}

475 476
- (BOOL)openVideoSubTitlesFromFile:(NSString *)path
{
477 478 479 480
    return libvlc_media_player_add_slave(_playerInstance,
                                         libvlc_media_slave_type_subtitle,
                                         [path UTF8String],
                                         TRUE);
481 482
}

483 484 485 486 487 488 489 490 491 492 493
- (int)addPlaybackSlave:(NSURL *)slaveURL type:(VLCMediaPlaybackSlaveType)slaveType enforce:(BOOL)enforceSelection
{
    if (!slaveURL)
        return -1;

    return libvlc_media_player_add_slave(_playerInstance,
                                         slaveType,
                                         [[slaveURL absoluteString] UTF8String],
                                         enforceSelection);
}

494 495
- (void)setCurrentVideoSubTitleDelay:(NSInteger)index
{
496
    libvlc_video_set_spu_delay(_playerInstance, index);
497 498 499 500
}

- (NSInteger)currentVideoSubTitleDelay
{
501
    return libvlc_video_get_spu_delay(_playerInstance);
502 503
}

504
#if TARGET_OS_IPHONE
505 506
- (void)setTextRendererFontSize:(NSNumber *)fontSize
{
507
    #warning setTextRendererFontSize: NO-OP
508 509 510 511
}

- (void)setTextRendererFont:(NSString *)fontname
{
512
    #warning setTextRendererFont: NO-OP
513 514 515 516
}

- (void)setTextRendererFontColor:(NSNumber *)fontColor
{
517
    #warning setTextRendererFontColor: NO-OP
518
}
519 520 521

- (void)setTextRendererFontForceBold:(NSNumber *)fontForceBold
{
522
    #warning setTextRendererFontForceBold: NO-OP
523
}
524
#endif
525

526 527 528
#pragma mark -
#pragma mark Video Crop geometry

529 530
- (void)setVideoCropGeometry:(char *)value
{
531
    libvlc_video_set_crop_geometry(_playerInstance, value);
532 533 534 535
}

- (char *)videoCropGeometry
{
536
    char * result = libvlc_video_get_crop_geometry(_playerInstance);
537
    return result;
538 539
}

540 541
- (void)setVideoAspectRatio:(char *)value
{
542
    libvlc_video_set_aspect_ratio(_playerInstance, value);
543 544 545 546
}

- (char *)videoAspectRatio
{
547
    char * result = libvlc_video_get_aspect_ratio(_playerInstance);
548 549 550
    return result;
}

551 552
- (void)setScaleFactor:(float)value
{
553
    libvlc_video_set_scale(_playerInstance, value);
554 555 556 557
}

- (float)scaleFactor
{
558
    return libvlc_video_get_scale(_playerInstance);
559 560
}

561
- (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
562
{
563
    int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
564 565
    if (failure)
        [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
566 567
}

568
- (void)setDeinterlaceFilter:(NSString *)name
569
{
570 571 572 573
    if (!name || name.length < 1)
        libvlc_video_set_deinterlace(_playerInstance, NULL);
    else
        libvlc_video_set_deinterlace(_playerInstance, [name UTF8String]);
574 575
}

576 577
- (BOOL)adjustFilterEnabled
{
578
    return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
579 580 581
}
- (void)setAdjustFilterEnabled:(BOOL)b_value
{
582
    libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
583 584 585
}
- (float)contrast
{
586 587
    libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
    return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
588 589 590
}
- (void)setContrast:(float)f_value
{
591
    if (f_value <= 2. && f_value >= 0.) {
592 593
        libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
        libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
594
    }
595 596 597
}
- (float)brightness
{
598 599
    libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
    return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
600 601 602
}
- (void)setBrightness:(float)f_value
{
603
    if (f_value <= 2. && f_value >= 0.) {
604 605
        libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
        libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
606
    }
607
}
608 609

- (float)hue
610
{
611
    libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
612
    return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Hue);
613
}
614
- (void)setHue:(float)f_value
615
{
616
    if (f_value <= 180. && f_value >= -180.) {
617
        libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
618
        libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Hue, f_value);
619
    }
620
}
621

622 623
- (float)saturation
{
624 625
    libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
    return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
626 627 628
}
- (void)setSaturation:(float)f_value
{
629
    if (f_value <= 3. && f_value >= 0.) {
630 631
        libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
        libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
632
    }
633 634 635
}
- (float)gamma
{
636 637
    libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
    return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
638 639 640
}
- (void)setGamma:(float)f_value
{
641
    if (f_value <= 10. && f_value >= 0.) {
642 643
        libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
        libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
644
    }
645 646
}

647
- (void)setRate:(float)value
648
{
649
    libvlc_media_player_set_rate(_playerInstance, value);
650 651
}

652
- (float)rate
653
{
654
    return libvlc_media_player_get_rate(_playerInstance);
655 656
}

Pierre's avatar
Pierre committed
657
- (CGSize)videoSize
658
{
659
    unsigned height = 0, width = 0;
660
    int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
661
    if (failure)
662
        return CGSizeZero;
Pierre's avatar
Pierre committed
663
    return CGSizeMake(width, height);
664 665 666 667
}

- (BOOL)hasVideoOut
{
668
    return libvlc_media_player_has_vout(_playerInstance);
669 670 671 672
}

- (float)framesPerSecond
{
673
    return .0;
674 675 676 677
}

- (void)setTime:(VLCTime *)value
{
678 679
    // Time is managed in seconds, while duration is managed in microseconds
    // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
680
    libvlc_media_player_set_time(_playerInstance, value ? [[value value] longLongValue] : 0);
681 682 683 684
}

- (VLCTime *)time
{
685
    return _cachedTime;
686 687
}

688 689
- (VLCTime *)remainingTime
{
690
    return _cachedRemainingTime;
691 692
}

693 694
#pragma mark -
#pragma mark Chapters
695
- (void)setCurrentChapterIndex:(int)value;
696
{
697
    libvlc_media_player_set_chapter(_playerInstance, value);
698 699
}

700
- (int)currentChapterIndex
701
{
702
    int count = libvlc_media_player_get_chapter_count(_playerInstance);
703
    if (count <= 0)
704
        return -1;
705
    int result = libvlc_media_player_get_chapter(_playerInstance);
706 707 708
    return result;
}

709 710
- (void)nextChapter
{
711
    libvlc_media_player_next_chapter(_playerInstance);
712 713 714 715
}

- (void)previousChapter
{
716
    libvlc_media_player_previous_chapter(_playerInstance);
717 718
}

719
- (NSArray *)chaptersForTitleIndex:(int)title
720
{
721
    NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
722
    if (count <= 0)
723
        return @[];
724

725 726
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
727 728
    libvlc_track_description_t *firstTrack = libvlc_video_get_chapter_description(_playerInstance, title);
    libvlc_track_description_t *currentTrack = firstTrack;
729 730
#pragma clang diagnostic push

731
    NSMutableArray *tempArray = [NSMutableArray array];
732
    for (NSInteger i = 0; i < count ; i++) {
733 734
        [tempArray addObject:@(currentTrack->psz_name)];
        currentTrack = currentTrack->p_next;
735
    }
736
    libvlc_track_description_list_release(firstTrack);
737
    return [NSArray arrayWithArray:tempArray];
738 739
}

740 741 742
#pragma mark -
#pragma mark Titles

743
- (void)setCurrentTitleIndex:(int)value
744
{
745
    libvlc_media_player_set_title(_playerInstance, value);
746 747
}

748
- (int)currentTitleIndex
749
{
750
    NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
751
    if (count <= 0)
752
        return -1;
753

754
    return libvlc_media_player_get_title(_playerInstance);
755 756
}

757 758 759 760 761
- (int)numberOfTitles
{
    return libvlc_media_player_get_title_count(_playerInstance);
}

762
- (NSUInteger)countOfTitles
763
{
764
    NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
765 766 767
    return result;
}

768 769
- (NSArray *)titles
{
770 771 772 773
    NSUInteger count = [self countOfTitles];
    if (count == 0)
        return [NSArray array];

774 775
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
776 777
    libvlc_track_description_t *firstTrack = libvlc_video_get_title_description(_playerInstance);
    libvlc_track_description_t *currentTrack = firstTrack;
778 779
#pragma clang diagnostic pop

780
    if (!currentTrack)
781 782 783
        return [NSArray array];

    NSMutableArray *tempArray = [NSMutableArray array];
784 785

    while (1) {
786 787 788 789
        if (currentTrack->psz_name != nil)
            [tempArray addObject:@(currentTrack->psz_name)];
        if (currentTrack->p_next)
            currentTrack = currentTrack->p_next;
790 791
        else
            break;
792
    }
793

794
    libvlc_track_description_list_release(firstTrack);
795 796 797
    return [NSArray arrayWithArray: tempArray];
}

798 799 800 801 802 803 804 805 806 807 808 809 810
- (NSArray *)titleDescriptions
{
    libvlc_title_description_t **titleInfo;
    int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);

    if (numberOfTitleDescriptions < 0)
        return [NSArray array];

    if (numberOfTitleDescriptions == 0) {
        libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
        return [NSArray array];
    }

811
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
812 813 814 815 816

    for (int i = 0; i < numberOfTitleDescriptions; i++) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
                                           VLCTitleDescriptionDuration,
817
                                           @(titleInfo[i]->i_flags & libvlc_title_menu),
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
                                           VLCTitleDescriptionIsMenu,
                                           nil];
        if (titleInfo[i]->psz_name != NULL)
            dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
        [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
    }
    libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);

    return [NSArray arrayWithArray:array];
}

- (int)indexOfLongestTitle
{
    NSArray *titles = [self titleDescriptions];
    NSUInteger titleCount = titles.count;

    int currentlyFoundTitle = 0;
    int64_t currentlySelectedDuration = 0;
    int64_t randomTitleDuration = 0;

    for (int x = 0; x < titleCount; x++) {
        randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
        if (randomTitleDuration > currentlySelectedDuration) {
            currentlySelectedDuration = randomTitleDuration;
            currentlyFoundTitle = x;
        }
    }

    return currentlyFoundTitle;
}

849 850 851 852 853
- (int)numberOfChaptersForTitle:(int)titleIndex
{
    return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
}

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
- (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
{
    libvlc_chapter_description_t **chapterDescriptions;
    int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
                                                                                        titleIndex,
                                                                                        &chapterDescriptions);

    if (numberOfChapterDescriptions < 0)
        return [NSArray array];

    if (numberOfChapterDescriptions == 0) {
        libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
        return [NSArray array];
    }

    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];

    for (int i = 0; i < numberOfChapterDescriptions; i++) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
                                           VLCChapterDescriptionDuration,
                                           [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
                                           VLCChapterDescriptionTimeOffset,
                                           nil];
        if (chapterDescriptions[i]->psz_name != NULL)
            dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
        [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
    }

    libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);

    return [NSArray arrayWithArray:array];
}

888 889
#pragma mark -
#pragma mark Audio tracks
890
- (void)setCurrentAudioTrackIndex:(int)value
891
{
892
    libvlc_audio_set_track(_playerInstance, value);
893 894
}

895
- (int)currentAudioTrackIndex
896
{
897
    NSInteger count = libvlc_audio_get_track_count(_playerInstance);
898
    if (count <= 0)
899
        return -1;
900

901
    return libvlc_audio_get_track(_playerInstance);
902 903
}

904 905
- (NSArray *)audioTrackNames
{
906
    NSInteger count = libvlc_audio_get_track_count(_playerInstance);
907
    if (count <= 0)
908
        return @[];
909

910 911
    libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
    libvlc_track_description_t *currentTrack = firstTrack;
912 913 914

    NSMutableArray *tempArray = [NSMutableArray array];
    while (currentTrack) {
915 916
        NSString *track = @(currentTrack->psz_name);
        [tempArray addObject:track != nil ? track : @""];
917 918
        currentTrack = currentTrack->p_next;
    }
919
    libvlc_track_description_list_release(firstTrack);
920 921 922 923 924
    return [NSArray arrayWithArray: tempArray];
}

- (NSArray *)audioTrackIndexes
{
925
    NSInteger count = libvlc_audio_get_track_count(_playerInstance);
926
    if (count <= 0)
927
        return @[];
928

929 930
    libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
    libvlc_track_description_t *currentTrack = firstTrack;
931 932 933

    NSMutableArray *tempArray = [NSMutableArray array];
    while (currentTrack) {
934
        [tempArray addObject:@(currentTrack->i_id)];
935 936
        currentTrack = currentTrack->p_next;
    }
937
    libvlc_track_description_list_release(firstTrack);
938 939 940
    return [NSArray arrayWithArray: tempArray];
}

941 942 943 944 945
- (int)numberOfAudioTracks
{
    return libvlc_audio_get_track_count(_playerInstance);
}

946
- (void)setAudioChannel:(int)value
947
{
948
    libvlc_audio_set_channel(_playerInstance, value);
949 950
}

951
- (int)audioChannel
952
{
953
    return libvlc_audio_get_channel(_playerInstance);
954 955
}

956 957
- (void)setCurrentAudioPlaybackDelay:(NSInteger)index
{
958
    libvlc_audio_set_delay(_playerInstance, index);
959 960 961 962
}

- (NSInteger)currentAudioPlaybackDelay
{
963
    return libvlc_audio_get_delay(_playerInstance);
964 965
}

966 967 968 969 970
#pragma mark -
#pragma mark equalizer

- (void)setEqualizerEnabled:(BOOL)equalizerEnabled
{
971 972
    _equalizerEnabled = equalizerEnabled;
    if (!_equalizerEnabled) {
973 974 975 976 977 978 979 980 981 982 983 984
        libvlc_media_player_set_equalizer(_playerInstance, NULL);

        if (_equalizerInstance)
            libvlc_audio_equalizer_release(_equalizerInstance);
        return;
    }

    if (!_equalizerInstance)
        _equalizerInstance = libvlc_audio_equalizer_new();
    libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
}

985 986 987 988 989
- (BOOL)equalizerEnabled
{
    return _equalizerEnabled;
}

990 991 992 993 994
- (NSArray *)equalizerProfiles
{
    unsigned count = libvlc_audio_equalizer_get_preset_count();
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
    for (unsigned x = 0; x < count; x++)
995
        [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

    return [NSArray arrayWithArray:array];
}

- (void)resetEqualizerFromProfile:(unsigned)profile
{
    BOOL wasactive = NO;
    if (_equalizerInstance) {
        libvlc_media_player_set_equalizer(_playerInstance, NULL);
        libvlc_audio_equalizer_release(_equalizerInstance);
        wasactive = YES;
    }

    _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
    if (wasactive)
        libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
}

- (CGFloat)preAmplification
{
    if (!_equalizerInstance)
        return 0.;

    return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
}

- (void)setPreAmplification:(CGFloat)preAmplification
{
    if (!_equalizerInstance)
        _equalizerInstance = libvlc_audio_equalizer_new();

    libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
1028
    libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
}

- (unsigned)numberOfBands
{
    return libvlc_audio_equalizer_get_band_count();
}

- (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
{
    return libvlc_audio_equalizer_get_band_frequency(index);
}

- (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
{
    if (!_equalizerInstance)
        _equalizerInstance = libvlc_audio_equalizer_new();

    libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
}

- (CGFloat)amplificationOfBand:(unsigned int)index
{
    if (!_equalizerInstance)
        return 0.;

    return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
}

1057 1058 1059
#pragma mark -
#pragma mark set/get media

1060 1061
- (void)setMedia:(VLCMedia *)value
{
1062 1063
    if (_media != value) {
        if (_media && [_media compare:value] == NSOrderedSame)
1064
            return;
1065

1066
        _media = value;
1067

1068
        libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
1069
    }
1070 1071 1072 1073
}

- (VLCMedia *)media
{
1074
    return _media;
1075 1076
}

1077 1078 1079
#pragma mark -
#pragma mark playback

1080
- (void)play
1081
{
1082 1083 1084 1085 1086 1087 1088 1089 1090
    if ([NSThread isMainThread]) {
        /* Hack because we create a dead lock here, when the vout is created
         * and tries to recontact us on the main thread */
        /* FIXME: to do this properly we need to do some locking. We may want
         * to move that to libvlc */
        [self performSelectorInBackground:@selector(play) withObject:nil];
        return;
    }

1091
    libvlc_media_player_play(_playerInstance);
1092 1093 1094 1095
}

- (void)pause
{
1096
    if ([NSThread isMainThread]) {
1097 1098
        /* Hack because we create a dead lock here, when the vout is stopped
         * and tries to recontact us on the main thread */
1099
        /* FIXME: to do this properly we need to do some locking. We may want
1100 1101 1102 1103 1104
         * to move that to libvlc */
        [self performSelectorInBackground:@selector(pause) withObject:nil];
        return;
    }

1105
    // Pause the stream
1106
    libvlc_media_player_set_pause(_playerInstance, 1);
1107 1108 1109 1110
}

- (void)stop
{
1111 1112 1113 1114 1115 1116 1117 1118 1119
    if ([NSThread isMainThread]) {
        /* Hack because we create a dead lock here, when the vout is stopped
         * and tries to recontact us on the main thread */
        /* FIXME: to do this properly we need to do some locking. We may want
         * to move that to libvlc */
        [self performSelectorInBackground:@selector(stop) withObject:nil];
        return;
    }

1120
    libvlc_media_player_stop(_playerInstance);
1121 1122
}

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
- (BOOL)updateViewpoint:(CGFloat)yaw pitch:(CGFloat)pitch roll:(CGFloat)roll fov:(CGFloat)fov absolute:(BOOL)absolute
{
    if (_viewpoint == NULL) {
        _viewpoint = libvlc_video_new_viewpoint();
        if (_viewpoint == NULL)
            return NO;
    }

    _viewpoint->f_yaw = yaw;
    _viewpoint->f_pitch = pitch;
    _viewpoint->f_roll = roll;
    _viewpoint->f_field_of_view = fov;

    return libvlc_video_update_viewpoint(_playerInstance, _viewpoint, absolute) == 0 ? YES : NO;
}

1139 1140
- (void)gotoNextFrame
{
1141
    libvlc_media_player_next_frame(_playerInstance);
1142 1143
}

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
- (void)fastForward
{
    [self fastForwardAtRate: 2.0];
}

- (void)fastForwardAtRate:(float)rate
{
    [self setRate:rate];
}

- (void)rewind
{
    [self rewindAtRate: 2.0];
}

- (void)rewindAtRate:(float)rate
{
    [self setRate: -rate];
}
1163

1164
- (void)jumpBackward:(int)interval
1165
{
1166
    if ([self isSeekable]) {
1167
        interval = interval * 1000;
1168 1169 1170 1171
        [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
    }
}

1172
- (void)jumpForward:(int)interval
1173
{
1174
    if ([self isSeekable]) {
1175
        interval = interval * 1000;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
        [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
    }
}

- (void)extraShortJumpBackward
{
    [self jumpBackward:3];
}

- (void)extraShortJumpForward
{
    [self jumpForward:3];
}

- (void)shortJumpBackward
{
    [self jumpBackward:10];
}

- (void)shortJumpForward
{
    [self jumpForward:10];
}

- (void)mediumJumpBackward
{
    [self jumpBackward:60];
}

- (void)mediumJumpForward
{
    [self jumpForward:60];
}

- (void)longJumpBackward
{
    [self jumpBackward:300];
}

- (void)longJumpForward
{
    [self jumpForward:300];
}

1220 1221 1222 1223 1224
- (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
{
    libvlc_media_player_navigate(_playerInstance, action);
}