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_video_set_aspect_ratio( instance, value, NULL );
266 - (char *)videoAspectRatio
268 libvlc_exception_t ex;
269 libvlc_exception_init( &ex );
270 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
271 catch_exception( &ex );
275 - (void)setVideoSubTitles:(int)value
277 libvlc_video_set_spu( instance, value, NULL );
280 - (int)videoSubTitles
282 libvlc_exception_t ex;
283 libvlc_exception_init( &ex );
284 int result = libvlc_video_get_spu( instance, &ex );
285 catch_exception( &ex );
289 - (void)setVideoCropGeometry:(char *)value
291 libvlc_video_set_crop_geometry( instance, value, NULL );
294 - (char *)videoCropGeometry
296 libvlc_exception_t ex;
297 libvlc_exception_init( &ex );
298 char * result = libvlc_video_get_crop_geometry( instance, &ex );
299 catch_exception( &ex );
303 - (void)setVideoTeleText:(int)value
305 libvlc_video_set_teletext( instance, value, NULL );
310 libvlc_exception_t ex;
311 libvlc_exception_init( &ex );
312 int result = libvlc_video_get_teletext( instance, &ex );
313 catch_exception( &ex );
317 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
319 libvlc_exception_t ex;
320 libvlc_exception_init( &ex );
321 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
322 catch_exception( &ex );
325 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
327 libvlc_exception_t ex;
328 libvlc_exception_init( &ex );
329 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
330 catch_exception( &ex );
333 - (void)setRate:(float)value
335 libvlc_media_player_set_rate( instance, value, NULL );
340 libvlc_exception_t ex;
341 libvlc_exception_init( &ex );
342 float result = libvlc_media_player_get_rate( instance, &ex );
343 catch_exception( &ex );
349 libvlc_exception_t ex;
350 libvlc_exception_init( &ex );
351 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
352 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
353 catch_exception( &ex );
359 libvlc_exception_t ex;
360 libvlc_exception_init( &ex );
361 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
362 if (libvlc_exception_raised( &ex ))
364 libvlc_exception_clear( &ex );
371 - (float)framesPerSecond
373 libvlc_exception_t ex;
374 libvlc_exception_init( &ex );
375 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
376 catch_exception( &ex );
380 - (void)setTime:(VLCTime *)value
382 libvlc_exception_t ex;
383 libvlc_exception_init( &ex );
384 // Time is managed in seconds, while duration is managed in microseconds
385 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
386 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
387 (value ? [[value numberValue] longLongValue] / 1000 : 0),
389 catch_exception( &ex );
397 - (VLCTime *)remainingTime
399 double currentTime = [[cachedTime numberValue] doubleValue];
400 double remaining = currentTime / position * (1 - position);
401 return [VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]];
406 libvlc_exception_t ex;
407 libvlc_exception_init( &ex );
408 int result = libvlc_media_player_get_fps( instance, &ex );
409 catch_exception( &ex );
413 - (void)setChapter:(int)value;
415 libvlc_exception_t ex;
416 libvlc_exception_init( &ex );
417 libvlc_media_player_set_chapter( instance, value, &ex );
418 catch_exception( &ex );
423 libvlc_exception_t ex;
424 libvlc_exception_init( &ex );
425 int result = libvlc_media_player_get_chapter( instance, &ex );
426 catch_exception( &ex );
430 - (int)countOfChapters
432 libvlc_exception_t ex;
433 libvlc_exception_init( &ex );
434 int result = libvlc_media_player_get_chapter_count( instance, &ex );
435 catch_exception( &ex );
441 libvlc_exception_t ex;
442 libvlc_exception_init( &ex );
443 libvlc_media_player_next_chapter( instance, &ex );
444 catch_exception( &ex );
447 - (void)previousChapter
449 libvlc_exception_t ex;
450 libvlc_exception_init( &ex );
451 libvlc_media_player_previous_chapter( instance, &ex );
452 catch_exception( &ex );
455 - (void)setTitle:(int)value
457 libvlc_exception_t ex;
458 libvlc_exception_init( &ex );
459 libvlc_media_player_set_title( instance, value, &ex );
460 catch_exception( &ex );
465 libvlc_exception_t ex;
466 libvlc_exception_init( &ex );
467 int result = libvlc_media_player_get_title( instance, &ex );
468 catch_exception( &ex );
474 libvlc_exception_t ex;
475 libvlc_exception_init( &ex );
476 int result = libvlc_media_player_get_title_count( instance, &ex );
477 catch_exception( &ex );
481 - (void)setAudioTrack:(int)value
483 libvlc_audio_set_track( instance, value, NULL );
488 libvlc_exception_t ex;
489 libvlc_exception_init( &ex );
490 int result = libvlc_audio_get_track( instance, &ex );
491 catch_exception( &ex );
495 - (int)countOfAudioTracks
497 libvlc_exception_t ex;
498 libvlc_exception_init( &ex );
499 int result = libvlc_audio_get_track_count( instance, &ex );
500 catch_exception( &ex );
504 - (void)setAudioChannel:(int)value
506 libvlc_audio_set_channel( instance, value, NULL );
511 libvlc_exception_t ex;
512 libvlc_exception_init( &ex );
513 int result = libvlc_audio_get_channel( instance, &ex );
514 catch_exception( &ex );
518 - (void)setMedia:(VLCMedia *)value
522 if (media && [media compare:value] == NSOrderedSame)
526 media = [value retain];
528 libvlc_exception_t ex;
529 libvlc_exception_init( &ex );
530 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
531 catch_exception( &ex );
542 libvlc_exception_t ex;
543 libvlc_exception_init( &ex );
544 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
545 catch_exception( &ex );
551 if( [NSThread isMainThread] )
553 /* Hack because we create a dead lock here, when the vout is stopped
554 * and tries to recontact us on the main thread */
555 /* FIXME: to do this properly we need to do some locking. We may want
556 * to move that to libvlc */
557 [self performSelectorInBackground:@selector(pause) withObject:nil];
562 libvlc_exception_t ex;
563 libvlc_exception_init( &ex );
564 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
565 catch_exception( &ex );
570 libvlc_exception_t ex;
571 libvlc_exception_init( &ex );
572 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
573 catch_exception( &ex );
578 [self fastForwardAtRate: 2.0];
581 - (void)fastForwardAtRate:(float)rate
588 [self rewindAtRate: 2.0];
591 - (void)rewindAtRate:(float)rate
593 [self setRate: -rate];
596 - (void)jumpBackward:(NSInteger)interval
598 if( [self isSeekable] )
600 interval = interval * 1000000;
601 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
605 - (void)jumpForward:(NSInteger)interval
607 if( [self isSeekable] )
609 interval = interval * 1000000;
610 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
614 - (void)extraShortJumpBackward
616 [self jumpBackward:3];
619 - (void)extraShortJumpForward
621 [self jumpForward:3];
624 - (void)shortJumpBackward
626 [self jumpBackward:10];
629 - (void)shortJumpForward
631 [self jumpForward:10];
634 - (void)mediumJumpBackward
636 [self jumpBackward:60];
639 - (void)mediumJumpForward
641 [self jumpForward:60];
644 - (void)longJumpBackward
646 [self jumpBackward:300];
649 - (void)longJumpForward
651 [self jumpForward:300];
654 + (NSSet *)keyPathsForValuesAffectingIsPlaying
656 return [NSSet setWithObjects:@"state", nil];
661 VLCMediaPlayerState state = [self state];
662 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
663 (state == VLCMediaPlayerStatePlaying));
668 libvlc_exception_t ex;
669 libvlc_exception_init( &ex );
670 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
671 if (libvlc_exception_raised(&ex))
673 libvlc_exception_clear(&ex);
680 static const VLCMediaPlayerState libvlc_to_local_state[] =
682 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
683 [libvlc_Opening] = VLCMediaPlayerStateOpening,
684 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
685 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
686 [libvlc_Paused] = VLCMediaPlayerStatePaused,
687 [libvlc_Ended] = VLCMediaPlayerStateEnded,
688 [libvlc_Error] = VLCMediaPlayerStateError
691 - (VLCMediaPlayerState)state
701 - (void)setPosition:(float)newPosition
703 libvlc_exception_t ex;
704 libvlc_exception_init( &ex );
705 libvlc_media_player_set_position( instance, newPosition, &ex );
706 catch_exception( &ex );
711 libvlc_exception_t ex;
712 libvlc_exception_init( &ex );
713 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
714 catch_exception( &ex );
720 libvlc_exception_t ex;
721 libvlc_exception_init( &ex );
722 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
723 catch_exception( &ex );
727 - (void *)libVLCMediaPlayer
733 @implementation VLCMediaPlayer (Private)
734 - (id)initWithDrawable:(id)aDrawable
736 if (self = [super init])
740 cachedTime = [[VLCTime nullTime] retain];
742 cachedState = VLCMediaPlayerStateStopped;
744 // Create a media instance, it doesn't matter what library we start off with
745 // it will change depending on the media descriptor provided to the media
747 libvlc_exception_t ex;
748 libvlc_exception_init( &ex );
749 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
750 catch_exception( &ex );
752 [self registerObservers];
754 [self setDrawable:aDrawable];
759 - (void)registerObservers
761 libvlc_exception_t ex;
762 libvlc_exception_init( &ex );
764 // Attach event observers into the media instance
765 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
766 libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
767 libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
768 libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
769 libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
770 /* FIXME: We may want to turn that off when none is interested by that */
771 libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
772 libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
773 catch_exception( &ex );
776 - (void)unregisterObservers
778 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
779 libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
780 libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
781 libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
782 libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
783 libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
784 libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
787 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
789 [self willChangeValueForKey:@"time"];
790 [self willChangeValueForKey:@"remainingTime"];
791 [cachedTime release];
792 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
794 [self didChangeValueForKey:@"remainingTime"];
795 [self didChangeValueForKey:@"time"];
800 UpdateSystemActivity(UsrActivity);
803 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
805 // This seems to be the most relevant place to delay sleeping and screen saver.
808 [self willChangeValueForKey:@"position"];
809 position = [newPosition floatValue];
810 [self didChangeValueForKey:@"position"];
813 - (void)mediaPlayerStateChanged:(NSNumber *)newState
815 [self willChangeValueForKey:@"state"];
816 cachedState = [newState intValue];
817 [self didChangeValueForKey:@"state"];