1 /*****************************************************************************
2 * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
3 *****************************************************************************
4 * Copyright (C) 2007-2009 Pierre d'Herbemont
5 * Copyright (C) 2007-2009 the VideoLAN team
6 * Partial Copyright (C) 2009 Felix Paul Kühne
9 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10 * Faustion Osuna <enrique.osuna # gmail.com>
11 * Felix Paul Kühne <fkuehne # videolan.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #import "VLCLibrary.h"
29 #import "VLCMediaPlayer.h"
30 #import "VLCEventManager.h"
31 #import "VLCLibVLCBridging.h"
32 #import "VLCVideoView.h"
37 /* prevent system sleep */
38 #import <CoreServices/CoreServices.h>
39 /* FIXME: Ugly hack! */
41 #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
46 /* Notification Messages */
47 NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
48 NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
50 NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
52 static NSString * stateToStrings[] = {
53 [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
54 [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
55 [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
56 [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
57 [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
58 [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
59 [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
61 return stateToStrings[state];
64 /* libvlc event callback */
65 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
67 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
68 withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
69 withNotificationName:VLCMediaPlayerVolumeChanged];
72 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
74 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
75 [[VLCEventManager sharedManager] callOnMainThreadObject:self
76 withMethod:@selector(mediaPlayerTimeChanged:)
77 withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
79 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
80 withDelegateMethod:@selector(mediaPlayerTimeChanged:)
81 withNotificationName:VLCMediaPlayerTimeChanged];
85 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
87 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
89 [[VLCEventManager sharedManager] callOnMainThreadObject:self
90 withMethod:@selector(mediaPlayerPositionChanged:)
91 withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
95 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
97 VLCMediaPlayerState newState;
99 if( event->type == libvlc_MediaPlayerPlaying )
100 newState = VLCMediaPlayerStatePlaying;
101 else if( event->type == libvlc_MediaPlayerPaused )
102 newState = VLCMediaPlayerStatePaused;
103 else if( event->type == libvlc_MediaPlayerEndReached )
104 newState = VLCMediaPlayerStateStopped;
105 else if( event->type == libvlc_MediaPlayerEncounteredError )
106 newState = VLCMediaPlayerStateError;
109 NSLog(@"%s: Unknown event", __FUNCTION__);
113 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
115 [[VLCEventManager sharedManager] callOnMainThreadObject:self
116 withMethod:@selector(mediaPlayerStateChanged:)
117 withArgumentAsObject:[NSNumber numberWithInt:newState]];
119 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
120 withDelegateMethod:@selector(mediaPlayerStateChanged:)
121 withNotificationName:VLCMediaPlayerStateChanged];
128 // TODO: Documentation
129 @interface VLCMediaPlayer (Private)
130 - (id)initWithDrawable:(id)aDrawable;
132 - (void)registerObservers;
133 - (void)unregisterObservers;
134 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
135 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
136 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
139 @implementation VLCMediaPlayer
142 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
144 static NSDictionary * dict = nil;
145 NSSet * superKeyPaths;
148 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
149 [NSSet setWithObject:@"state"], @"playing",
150 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
151 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
152 [NSSet setWithObjects:@"state", @"media", nil], @"description",
155 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
157 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
158 [ret unionSet:superKeyPaths];
161 return [dict objectForKey: key];
167 return [self initWithDrawable:nil];
170 - (id)initWithVideoView:(VLCVideoView *)aVideoView
172 return [self initWithDrawable: aVideoView];
175 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
177 return [self initWithDrawable: aVideoLayer];
184 if([self retainCount] <= 1)
186 /* We must make sure we won't receive new event after an upcoming dealloc
187 * We also may receive a -retain in some event callback that may occcur
188 * Before libvlc_event_detach. So this can't happen in dealloc */
189 [self unregisterObservers];
197 NSAssert(libvlc_media_player_get_state(instance, NULL) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
199 // Always get rid of the delegate first so we can stop sending messages to it
200 // TODO: Should we tell the delegate that we're shutting down?
203 // Clear our drawable as we are going to release it, we don't
204 // want the core to use it from this point. This won't happen as
205 // the media player must be stopped.
206 libvlc_media_player_set_nsobject(instance, nil, NULL);
208 libvlc_media_player_release(instance);
210 // Get rid of everything else
212 [cachedTime release];
218 - (void)setDelegate:(id)value
228 - (void)setVideoView:(VLCVideoView *)aVideoView
230 [self setDrawable: aVideoView];
233 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
235 [self setDrawable: aVideoLayer];
238 - (void)setDrawable:(id)aDrawable
240 // Make sure that this instance has been associated with the drawing canvas.
241 libvlc_exception_t ex;
242 libvlc_exception_init( &ex );
243 libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
244 catch_exception( &ex );
249 libvlc_exception_t ex;
250 libvlc_exception_init( &ex );
251 id ret = libvlc_media_player_get_nsobject(instance);
252 catch_exception( &ex );
258 return [[VLCLibrary sharedLibrary] audio];
261 - (void)setVideoAspectRatio:(char *)value
263 libvlc_exception_t ex;
264 libvlc_exception_init( &ex );
265 libvlc_video_set_aspect_ratio( instance, value, &ex );
266 catch_exception( &ex );
269 - (char *)videoAspectRatio
271 libvlc_exception_t ex;
272 libvlc_exception_init( &ex );
273 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
274 catch_exception( &ex );
278 - (void)setVideoSubTitles:(int)value
280 libvlc_exception_t ex;
281 libvlc_exception_init( &ex );
282 libvlc_video_set_spu( instance, value, &ex );
283 catch_exception( &ex );
286 - (int)videoSubTitles
288 libvlc_exception_t ex;
289 libvlc_exception_init( &ex );
290 int result = libvlc_video_get_spu( instance, &ex );
291 catch_exception( &ex );
295 - (void)setVideoCropGeometry:(char *)value
297 libvlc_exception_t ex;
298 libvlc_exception_init( &ex );
299 libvlc_video_set_crop_geometry( instance, value, &ex );
300 catch_exception( &ex );
303 - (char *)videoCropGeometry
305 libvlc_exception_t ex;
306 libvlc_exception_init( &ex );
307 char * result = libvlc_video_get_crop_geometry( instance, &ex );
308 catch_exception( &ex );
312 - (void)setVideoTeleText:(int)value
314 libvlc_exception_t ex;
315 libvlc_exception_init( &ex );
316 libvlc_video_set_teletext( instance, value, &ex );
317 catch_exception( &ex );
322 libvlc_exception_t ex;
323 libvlc_exception_init( &ex );
324 int result = libvlc_video_get_teletext( instance, &ex );
325 catch_exception( &ex );
329 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
331 libvlc_exception_t ex;
332 libvlc_exception_init( &ex );
333 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
334 catch_exception( &ex );
337 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
339 libvlc_exception_t ex;
340 libvlc_exception_init( &ex );
341 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
342 catch_exception( &ex );
345 - (void)setRate:(float)value
347 libvlc_exception_t ex;
348 libvlc_exception_init( &ex );
349 libvlc_media_player_set_rate( instance, value, &ex );
350 catch_exception( &ex );
355 libvlc_exception_t ex;
356 libvlc_exception_init( &ex );
357 float result = libvlc_media_player_get_rate( instance, &ex );
358 catch_exception( &ex );
364 libvlc_exception_t ex;
365 libvlc_exception_init( &ex );
366 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
367 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
368 catch_exception( &ex );
374 libvlc_exception_t ex;
375 libvlc_exception_init( &ex );
376 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
377 if (libvlc_exception_raised( &ex ))
379 libvlc_exception_clear( &ex );
386 - (float)framesPerSecond
388 libvlc_exception_t ex;
389 libvlc_exception_init( &ex );
390 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
391 catch_exception( &ex );
395 - (void)setTime:(VLCTime *)value
397 libvlc_exception_t ex;
398 libvlc_exception_init( &ex );
399 // Time is managed in seconds, while duration is managed in microseconds
400 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
401 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
402 (value ? [[value numberValue] longLongValue] / 1000 : 0),
404 catch_exception( &ex );
412 - (VLCTime *)remainingTime
414 double currentTime = [[cachedTime numberValue] doubleValue];
415 double remaining = currentTime / position * (1 - position);
416 return [VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]];
421 libvlc_exception_t ex;
422 libvlc_exception_init( &ex );
423 int result = libvlc_media_player_get_fps( instance, &ex );
424 catch_exception( &ex );
428 - (void)setChapter:(int)value;
430 libvlc_exception_t ex;
431 libvlc_exception_init( &ex );
432 libvlc_media_player_set_chapter( instance, value, &ex );
433 catch_exception( &ex );
438 libvlc_exception_t ex;
439 libvlc_exception_init( &ex );
440 int result = libvlc_media_player_get_chapter( instance, &ex );
441 catch_exception( &ex );
445 - (int)countOfChapters
447 libvlc_exception_t ex;
448 libvlc_exception_init( &ex );
449 int result = libvlc_media_player_get_chapter_count( instance, &ex );
450 catch_exception( &ex );
456 libvlc_exception_t ex;
457 libvlc_exception_init( &ex );
458 libvlc_media_player_next_chapter( instance, &ex );
459 catch_exception( &ex );
462 - (void)previousChapter
464 libvlc_exception_t ex;
465 libvlc_exception_init( &ex );
466 libvlc_media_player_previous_chapter( instance, &ex );
467 catch_exception( &ex );
470 - (void)setTitle:(int)value
472 libvlc_exception_t ex;
473 libvlc_exception_init( &ex );
474 libvlc_media_player_set_title( instance, value, &ex );
475 catch_exception( &ex );
480 libvlc_exception_t ex;
481 libvlc_exception_init( &ex );
482 int result = libvlc_media_player_get_title( instance, &ex );
483 catch_exception( &ex );
489 libvlc_exception_t ex;
490 libvlc_exception_init( &ex );
491 int result = libvlc_media_player_get_title_count( instance, &ex );
492 catch_exception( &ex );
496 - (void)setAudioTrack:(int)value
498 libvlc_exception_t ex;
499 libvlc_exception_init( &ex );
500 libvlc_audio_set_track( instance, value, &ex );
501 catch_exception( &ex );
506 libvlc_exception_t ex;
507 libvlc_exception_init( &ex );
508 int result = libvlc_audio_get_track( instance, &ex );
509 catch_exception( &ex );
513 - (int)countOfAudioTracks
515 libvlc_exception_t ex;
516 libvlc_exception_init( &ex );
517 int result = libvlc_audio_get_track_count( instance, &ex );
518 catch_exception( &ex );
522 - (void)setAudioChannel:(int)value
524 libvlc_exception_t ex;
525 libvlc_exception_init( &ex );
526 libvlc_audio_set_channel( instance, value, &ex );
527 catch_exception( &ex );
532 libvlc_exception_t ex;
533 libvlc_exception_init( &ex );
534 int result = libvlc_audio_get_channel( instance, &ex );
535 catch_exception( &ex );
539 - (void)setMedia:(VLCMedia *)value
543 if (media && [media compare:value] == NSOrderedSame)
547 media = [value retain];
549 libvlc_exception_t ex;
550 libvlc_exception_init( &ex );
551 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
552 catch_exception( &ex );
563 libvlc_exception_t ex;
564 libvlc_exception_init( &ex );
565 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
566 catch_exception( &ex );
572 if( [NSThread isMainThread] )
574 /* Hack because we create a dead lock here, when the vout is stopped
575 * and tries to recontact us on the main thread */
576 /* FIXME: to do this properly we need to do some locking. We may want
577 * to move that to libvlc */
578 [self performSelectorInBackground:@selector(pause) withObject:nil];
583 libvlc_exception_t ex;
584 libvlc_exception_init( &ex );
585 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
586 catch_exception( &ex );
591 libvlc_exception_t ex;
592 libvlc_exception_init( &ex );
593 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
594 catch_exception( &ex );
599 [self fastForwardAtRate: 2.0];
602 - (void)fastForwardAtRate:(float)rate
609 [self rewindAtRate: 2.0];
612 - (void)rewindAtRate:(float)rate
614 [self setRate: -rate];
617 - (void)jumpBackward:(NSInteger)interval
619 if( [self isSeekable] )
621 interval = interval * 1000000;
622 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
626 - (void)jumpForward:(NSInteger)interval
628 if( [self isSeekable] )
630 interval = interval * 1000000;
631 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
635 - (void)extraShortJumpBackward
637 [self jumpBackward:3];
640 - (void)extraShortJumpForward
642 [self jumpForward:3];
645 - (void)shortJumpBackward
647 [self jumpBackward:10];
650 - (void)shortJumpForward
652 [self jumpForward:10];
655 - (void)mediumJumpBackward
657 [self jumpBackward:60];
660 - (void)mediumJumpForward
662 [self jumpForward:60];
665 - (void)longJumpBackward
667 [self jumpBackward:300];
670 - (void)longJumpForward
672 [self jumpForward:300];
675 + (NSSet *)keyPathsForValuesAffectingIsPlaying
677 return [NSSet setWithObjects:@"state", nil];
682 VLCMediaPlayerState state = [self state];
683 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
684 (state == VLCMediaPlayerStatePlaying));
689 libvlc_exception_t ex;
690 libvlc_exception_init( &ex );
691 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
692 if (libvlc_exception_raised(&ex))
694 libvlc_exception_clear(&ex);
701 static const VLCMediaPlayerState libvlc_to_local_state[] =
703 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
704 [libvlc_Opening] = VLCMediaPlayerStateOpening,
705 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
706 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
707 [libvlc_Paused] = VLCMediaPlayerStatePaused,
708 [libvlc_Ended] = VLCMediaPlayerStateEnded,
709 [libvlc_Error] = VLCMediaPlayerStateError
712 - (VLCMediaPlayerState)state
722 - (void)setPosition:(float)newPosition
724 libvlc_exception_t ex;
725 libvlc_exception_init( &ex );
726 libvlc_media_player_set_position( instance, newPosition, &ex );
727 catch_exception( &ex );
732 libvlc_exception_t ex;
733 libvlc_exception_init( &ex );
734 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
735 catch_exception( &ex );
741 libvlc_exception_t ex;
742 libvlc_exception_init( &ex );
743 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
744 catch_exception( &ex );
748 - (void *)libVLCMediaPlayer
754 @implementation VLCMediaPlayer (Private)
755 - (id)initWithDrawable:(id)aDrawable
757 if (self = [super init])
761 cachedTime = [[VLCTime nullTime] retain];
763 cachedState = VLCMediaPlayerStateStopped;
765 // Create a media instance, it doesn't matter what library we start off with
766 // it will change depending on the media descriptor provided to the media
768 libvlc_exception_t ex;
769 libvlc_exception_init( &ex );
770 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
771 catch_exception( &ex );
773 [self registerObservers];
775 [self setDrawable:aDrawable];
780 - (void)registerObservers
782 libvlc_exception_t ex;
783 libvlc_exception_init( &ex );
785 // Attach event observers into the media instance
786 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
787 libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
788 libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
789 libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
790 libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
791 /* FIXME: We may want to turn that off when none is interested by that */
792 libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
793 libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
794 catch_exception( &ex );
797 - (void)unregisterObservers
799 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
800 libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
801 libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
802 libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
803 libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
804 libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
805 libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
808 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
810 [self willChangeValueForKey:@"time"];
811 [self willChangeValueForKey:@"remainingTime"];
812 [cachedTime release];
813 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
815 [self didChangeValueForKey:@"remainingTime"];
816 [self didChangeValueForKey:@"time"];
821 UpdateSystemActivity(UsrActivity);
824 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
826 // This seems to be the most relevant place to delay sleeping and screen saver.
829 [self willChangeValueForKey:@"position"];
830 position = [newPosition floatValue];
831 [self didChangeValueForKey:@"position"];
834 - (void)mediaPlayerStateChanged:(NSNumber *)newState
836 [self willChangeValueForKey:@"state"];
837 cachedState = [newState intValue];
838 [self didChangeValueForKey:@"state"];