1 /*****************************************************************************
2 * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
3 *****************************************************************************
4 * Copyright (C) 2007 Pierre d'Herbemont
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9 * Faustion Osuna <enrique.osuna # gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #import "VLCLibrary.h"
27 #import "VLCMediaPlayer.h"
28 #import "VLCEventManager.h"
29 #import "VLCLibVLCBridging.h"
30 #import "VLCVideoView.h"
35 /* prevent system sleep */
36 #import <CoreServices/CoreServices.h>
37 /* FIXME: Ugly hack! */
39 #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
44 /* Notification Messages */
45 NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
46 NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
48 NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
50 static NSString * stateToStrings[] = {
51 [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
52 [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
53 [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
54 [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
55 [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
56 [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
57 [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
59 return stateToStrings[state];
62 /* libvlc event callback */
63 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
65 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
66 withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
67 withNotificationName:VLCMediaPlayerVolumeChanged];
70 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
72 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
73 [[VLCEventManager sharedManager] callOnMainThreadObject:self
74 withMethod:@selector(mediaPlayerTimeChanged:)
75 withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
77 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
78 withDelegateMethod:@selector(mediaPlayerTimeChanged:)
79 withNotificationName:VLCMediaPlayerTimeChanged];
83 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
85 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
87 [[VLCEventManager sharedManager] callOnMainThreadObject:self
88 withMethod:@selector(mediaPlayerPositionChanged:)
89 withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
93 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
95 VLCMediaPlayerState newState;
97 if( event->type == libvlc_MediaPlayerPlaying )
98 newState = VLCMediaPlayerStatePlaying;
99 else if( event->type == libvlc_MediaPlayerPaused )
100 newState = VLCMediaPlayerStatePaused;
101 else if( event->type == libvlc_MediaPlayerEndReached )
102 newState = VLCMediaPlayerStateStopped;
103 else if( event->type == libvlc_MediaPlayerEncounteredError )
104 newState = VLCMediaPlayerStateError;
107 NSLog(@"%s: Unknown event", __FUNCTION__);
111 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
113 [[VLCEventManager sharedManager] callOnMainThreadObject:self
114 withMethod:@selector(mediaPlayerStateChanged:)
115 withArgumentAsObject:[NSNumber numberWithInt:newState]];
117 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
118 withDelegateMethod:@selector(mediaPlayerStateChanged:)
119 withNotificationName:VLCMediaPlayerStateChanged];
126 // TODO: Documentation
127 @interface VLCMediaPlayer (Private)
128 - (id)initWithDrawable:(id)aDrawable;
130 - (void)registerObservers;
131 - (void)unregisterObservers;
132 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
133 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
134 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
137 @implementation VLCMediaPlayer
140 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
142 static NSDictionary * dict = nil;
143 NSSet * superKeyPaths;
146 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
147 [NSSet setWithObject:@"state"], @"playing",
148 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
149 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
150 [NSSet setWithObjects:@"state", @"media", nil], @"description",
153 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
155 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
156 [ret unionSet:superKeyPaths];
159 return [dict objectForKey: key];
165 return [self initWithDrawable:nil];
168 - (id)initWithVideoView:(VLCVideoView *)aVideoView
170 return [self initWithDrawable: aVideoView];
173 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
175 return [self initWithDrawable: aVideoLayer];
182 if([self retainCount] <= 1)
184 /* We must make sure we won't receive new event after an upcoming dealloc
185 * We also may receive a -retain in some event callback that may occcur
186 * Before libvlc_event_detach. So this can't happen in dealloc */
187 [self unregisterObservers];
195 NSAssert(libvlc_media_player_get_state(instance, NULL) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
197 // Always get rid of the delegate first so we can stop sending messages to it
198 // TODO: Should we tell the delegate that we're shutting down?
201 // Clear our drawable as we are going to release it, we don't
202 // want the core to use it from this point. This won't happen as
203 // the media player must be stopped.
204 libvlc_media_player_set_nsobject(instance, nil, NULL);
206 libvlc_media_player_release(instance);
208 // Get rid of everything else
210 [cachedTime release];
216 - (void)setDelegate:(id)value
226 - (void)setVideoView:(VLCVideoView *)aVideoView
228 [self setDrawable: aVideoView];
231 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
233 [self setDrawable: aVideoLayer];
236 - (void)setDrawable:(id)aDrawable
238 // Make sure that this instance has been associated with the drawing canvas.
239 libvlc_exception_t ex;
240 libvlc_exception_init( &ex );
241 libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
242 catch_exception( &ex );
247 libvlc_exception_t ex;
248 libvlc_exception_init( &ex );
249 id ret = libvlc_media_player_get_nsobject(instance);
250 catch_exception( &ex );
256 return [[VLCLibrary sharedLibrary] audio];
259 - (void)setVideoAspectRatio:(char *)value
261 libvlc_video_set_aspect_ratio( instance, value, NULL );
264 - (char *)videoAspectRatio
266 libvlc_exception_t ex;
267 libvlc_exception_init( &ex );
268 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
269 catch_exception( &ex );
273 - (void)setVideoSubTitles:(int)value
275 libvlc_video_set_spu( instance, value, NULL );
278 - (int)videoSubTitles
280 libvlc_exception_t ex;
281 libvlc_exception_init( &ex );
282 int result = libvlc_video_get_spu( instance, &ex );
283 catch_exception( &ex );
287 - (void)setVideoCropGeometry:(char *)value
289 libvlc_video_set_crop_geometry( instance, value, NULL );
292 - (char *)videoCropGeometry
294 libvlc_exception_t ex;
295 libvlc_exception_init( &ex );
296 char * result = libvlc_video_get_crop_geometry( instance, &ex );
297 catch_exception( &ex );
301 - (void)setVideoTeleText:(int)value
303 libvlc_video_set_teletext( instance, value, NULL );
308 libvlc_exception_t ex;
309 libvlc_exception_init( &ex );
310 int result = libvlc_video_get_teletext( instance, &ex );
311 catch_exception( &ex );
315 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
317 libvlc_exception_t ex;
318 libvlc_exception_init( &ex );
319 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
320 catch_exception( &ex );
323 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
325 libvlc_exception_t ex;
326 libvlc_exception_init( &ex );
327 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
328 catch_exception( &ex );
331 - (void)setRate:(float)value
333 libvlc_media_player_set_rate( instance, value, NULL );
338 libvlc_exception_t ex;
339 libvlc_exception_init( &ex );
340 float result = libvlc_media_player_get_rate( instance, &ex );
341 catch_exception( &ex );
347 libvlc_exception_t ex;
348 libvlc_exception_init( &ex );
349 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
350 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
351 catch_exception( &ex );
357 libvlc_exception_t ex;
358 libvlc_exception_init( &ex );
359 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
360 if (libvlc_exception_raised( &ex ))
362 libvlc_exception_clear( &ex );
369 - (float)framesPerSecond
371 libvlc_exception_t ex;
372 libvlc_exception_init( &ex );
373 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
374 catch_exception( &ex );
378 - (void)setTime:(VLCTime *)value
380 libvlc_exception_t ex;
381 libvlc_exception_init( &ex );
382 // Time is managed in seconds, while duration is managed in microseconds
383 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
384 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
385 (value ? [[value numberValue] longLongValue] / 1000 : 0),
387 catch_exception( &ex );
395 - (VLCTime *)remainingTime
397 double currentTime = [[cachedTime numberValue] doubleValue];
398 double remaining = currentTime / position * (1 - position);
399 return [VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]];
402 - (void)setChapter:(int)value;
404 libvlc_media_player_set_chapter( instance, value, NULL );
409 libvlc_exception_t ex;
410 libvlc_exception_init( &ex );
411 int result = libvlc_media_player_get_chapter( instance, &ex );
412 catch_exception( &ex );
416 - (int)countOfChapters
418 libvlc_exception_t ex;
419 libvlc_exception_init( &ex );
420 int result = libvlc_media_player_get_chapter_count( instance, &ex );
421 catch_exception( &ex );
425 - (void)setAudioTrack:(int)value
427 libvlc_audio_set_track( instance, value, NULL );
432 libvlc_exception_t ex;
433 libvlc_exception_init( &ex );
434 int result = libvlc_audio_get_track( instance, &ex );
435 catch_exception( &ex );
439 - (int)countOfAudioTracks
441 libvlc_exception_t ex;
442 libvlc_exception_init( &ex );
443 int result = libvlc_audio_get_track_count( instance, &ex );
444 catch_exception( &ex );
448 - (void)setAudioChannel:(int)value
450 libvlc_audio_set_channel( instance, value, NULL );
455 libvlc_exception_t ex;
456 libvlc_exception_init( &ex );
457 int result = libvlc_audio_get_channel( instance, &ex );
458 catch_exception( &ex );
462 - (void)setMedia:(VLCMedia *)value
466 if (media && [media compare:value] == NSOrderedSame)
470 media = [value retain];
472 libvlc_exception_t ex;
473 libvlc_exception_init( &ex );
474 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
475 catch_exception( &ex );
486 libvlc_exception_t ex;
487 libvlc_exception_init( &ex );
488 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
489 catch_exception( &ex );
495 if( [NSThread isMainThread] )
497 /* Hack because we create a dead lock here, when the vout is stopped
498 * and tries to recontact us on the main thread */
499 /* FIXME: to do this properly we need to do some locking. We may want
500 * to move that to libvlc */
501 [self performSelectorInBackground:@selector(pause) withObject:nil];
506 libvlc_exception_t ex;
507 libvlc_exception_init( &ex );
508 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
509 catch_exception( &ex );
514 libvlc_exception_t ex;
515 libvlc_exception_init( &ex );
516 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
517 catch_exception( &ex );
522 [self fastForwardAtRate: 2.0];
525 - (void)fastForwardAtRate:(float)rate
532 [self rewindAtRate: 2.0];
535 - (void)rewindAtRate:(float)rate
537 [self setRate: -rate];
540 - (void)jumpBackward:(NSInteger)interval
542 if( [self isSeekable] )
544 interval = interval * 1000000;
545 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
549 - (void)jumpForward:(NSInteger)interval
551 if( [self isSeekable] )
553 interval = interval * 1000000;
554 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
558 - (void)extraShortJumpBackward
560 [self jumpBackward:3];
563 - (void)extraShortJumpForward
565 [self jumpForward:3];
568 - (void)shortJumpBackward
570 [self jumpBackward:10];
573 - (void)shortJumpForward
575 [self jumpForward:10];
578 - (void)mediumJumpBackward
580 [self jumpBackward:60];
583 - (void)mediumJumpForward
585 [self jumpForward:60];
588 - (void)longJumpBackward
590 [self jumpBackward:300];
593 - (void)longJumpForward
595 [self jumpForward:300];
598 + (NSSet *)keyPathsForValuesAffectingIsPlaying
600 return [NSSet setWithObjects:@"state", nil];
605 VLCMediaPlayerState state = [self state];
606 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
607 (state == VLCMediaPlayerStatePlaying));
612 libvlc_exception_t ex;
613 libvlc_exception_init( &ex );
614 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
615 if (libvlc_exception_raised(&ex))
617 libvlc_exception_clear(&ex);
624 static const VLCMediaPlayerState libvlc_to_local_state[] =
626 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
627 [libvlc_Opening] = VLCMediaPlayerStateOpening,
628 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
629 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
630 [libvlc_Paused] = VLCMediaPlayerStatePaused,
631 [libvlc_Ended] = VLCMediaPlayerStateEnded,
632 [libvlc_Error] = VLCMediaPlayerStateError
635 - (VLCMediaPlayerState)state
645 - (void)setPosition:(float)newPosition
647 libvlc_exception_t ex;
648 libvlc_exception_init( &ex );
649 libvlc_media_player_set_position( instance, newPosition, &ex );
650 catch_exception( &ex );
655 libvlc_exception_t ex;
656 libvlc_exception_init( &ex );
657 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
658 catch_exception( &ex );
664 libvlc_exception_t ex;
665 libvlc_exception_init( &ex );
666 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
667 catch_exception( &ex );
671 - (void *)libVLCMediaPlayer
677 @implementation VLCMediaPlayer (Private)
678 - (id)initWithDrawable:(id)aDrawable
680 if (self = [super init])
684 cachedTime = [[VLCTime nullTime] retain];
686 cachedState = VLCMediaPlayerStateStopped;
688 // Create a media instance, it doesn't matter what library we start off with
689 // it will change depending on the media descriptor provided to the media
691 libvlc_exception_t ex;
692 libvlc_exception_init( &ex );
693 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
694 catch_exception( &ex );
696 [self registerObservers];
698 [self setDrawable:aDrawable];
703 - (void)registerObservers
705 libvlc_exception_t ex;
706 libvlc_exception_init( &ex );
708 // Attach event observers into the media instance
709 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
710 libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
711 libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
712 libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
713 libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
714 /* FIXME: We may want to turn that off when none is interested by that */
715 libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
716 libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
717 catch_exception( &ex );
720 - (void)unregisterObservers
722 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
723 libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
724 libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
725 libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
726 libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
727 libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
728 libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
731 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
733 [self willChangeValueForKey:@"time"];
734 [self willChangeValueForKey:@"remainingTime"];
735 [cachedTime release];
736 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
738 [self didChangeValueForKey:@"remainingTime"];
739 [self didChangeValueForKey:@"time"];
744 UpdateSystemActivity(UsrActivity);
747 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
749 // This seems to be the most relevant place to delay sleeping and screen saver.
752 [self willChangeValueForKey:@"position"];
753 position = [newPosition floatValue];
754 [self didChangeValueForKey:@"position"];
757 - (void)mediaPlayerStateChanged:(NSNumber *)newState
759 [self willChangeValueForKey:@"state"];
760 cachedState = [newState intValue];
761 [self didChangeValueForKey:@"state"];