1 /*****************************************************************************
2 * VLCMediaPlayer.m: VLC.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"
37 /* Notification Messages */
38 NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
39 NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
41 NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
43 static NSString * stateToStrings[] = {
44 [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
45 [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
46 [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
47 [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
48 [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
49 [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
50 [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
52 return stateToStrings[state];
55 /* libvlc event callback */
56 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
58 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
59 withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
60 withNotificationName:VLCMediaPlayerVolumeChanged];
63 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
65 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
67 [[VLCEventManager sharedManager] callOnMainThreadObject:self
68 withMethod:@selector(mediaPlayerTimeChanged:)
69 withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
71 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
72 withDelegateMethod:@selector(mediaPlayerTimeChanged:)
73 withNotificationName:VLCMediaPlayerTimeChanged];
77 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
79 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
81 [[VLCEventManager sharedManager] callOnMainThreadObject:self
82 withMethod:@selector(mediaPlayerPositionChanged:)
83 withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
87 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
89 VLCMediaPlayerState newState;
91 if( event->type == libvlc_MediaInstancePlayed )
92 newState = VLCMediaPlayerStatePlaying;
93 else if( event->type == libvlc_MediaInstancePaused )
94 newState = VLCMediaPlayerStatePaused;
95 else if( event->type == libvlc_MediaInstanceReachedEnd )
96 newState = VLCMediaPlayerStateStopped;
97 else if( event->type == libvlc_MediaInstanceEncounteredError )
98 newState = VLCMediaPlayerStateError;
101 NSLog(@"%s: Unknown event", __FUNCTION__);
105 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
107 [[VLCEventManager sharedManager] callOnMainThreadObject:self
108 withMethod:@selector(mediaPlayerStateChanged:)
109 withArgumentAsObject:[NSNumber numberWithInt:newState]];
111 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
112 withDelegateMethod:@selector(mediaPlayerStateChanged:)
113 withNotificationName:VLCMediaPlayerStateChanged];
120 // TODO: Documentation
121 @interface VLCMediaPlayer (Private)
122 - (id)initWithDrawable:(id)aDrawable;
124 - (void)registerObservers;
125 - (void)unregisterObservers;
126 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
127 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
128 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
131 @implementation VLCMediaPlayer
134 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
136 static NSDictionary * dict = nil;
137 NSSet * superKeyPaths;
140 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
141 [NSSet setWithObject:@"state"], @"playing",
142 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
143 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
144 [NSSet setWithObjects:@"state", @"media", nil], @"description",
147 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
149 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
150 [ret unionSet:superKeyPaths];
153 return [dict objectForKey: key];
159 return [self initWithDrawable:nil];
162 - (id)initWithVideoView:(VLCVideoView *)aVideoView
164 return [self initWithDrawable: aVideoView];
167 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
169 return [self initWithDrawable: aVideoLayer];
176 if([self retainCount] <= 1)
178 /* We must make sure we won't receive new event after an upcoming dealloc
179 * We also may receive a -retain in some event callback that may occcur
180 * Before libvlc_event_detach. So this can't happen in dealloc */
181 [self unregisterObservers];
189 // Always get rid of the delegate first so we can stop sending messages to it
190 // TODO: Should we tell the delegate that we're shutting down?
193 libvlc_media_player_release((libvlc_media_player_t *)instance);
195 // Get rid of everything else
197 [cachedTime release];
202 - (void)setDelegate:(id)value
212 - (void)setVideoView:(VLCVideoView *)aVideoView
214 [self setDrawable: aVideoView];
217 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
219 [self setDrawable: aVideoLayer];
222 - (void)setDrawable:(id)aDrawable
224 // Make sure that this instance has been associated with the drawing canvas.
225 libvlc_exception_t ex;
226 libvlc_exception_init( &ex );
227 libvlc_media_player_set_drawable ((libvlc_media_player_t *)instance,
228 (libvlc_drawable_t)aDrawable,
230 catch_exception( &ex );
235 libvlc_exception_t ex;
236 libvlc_exception_init( &ex );
237 libvlc_drawable_t ret = libvlc_media_player_get_drawable ((libvlc_media_player_t *)instance,
239 catch_exception( &ex );
245 return [[VLCLibrary sharedLibrary] audio];
248 - (void)setVideoAspectRatio:(char *)value
250 libvlc_video_set_aspect_ratio( instance, value, NULL );
253 - (char *)videoAspectRatio
255 libvlc_exception_t ex;
256 libvlc_exception_init( &ex );
257 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
258 catch_exception( &ex );
262 - (void)setVideoSubTitles:(int)value
264 libvlc_video_set_spu( instance, value, NULL );
267 - (int)videoSubTitles
269 libvlc_exception_t ex;
270 libvlc_exception_init( &ex );
271 int result = libvlc_video_get_spu( instance, &ex );
272 catch_exception( &ex );
276 - (void)setVideoCropGeometry:(char *)value
278 libvlc_video_set_crop_geometry( instance, value, NULL );
281 - (char *)videoCropGeometry
283 libvlc_exception_t ex;
284 libvlc_exception_init( &ex );
285 char * result = libvlc_video_get_crop_geometry( instance, &ex );
286 catch_exception( &ex );
290 - (void)setVideoTeleText:(int)value
292 libvlc_video_set_teletext( instance, value, NULL );
297 libvlc_exception_t ex;
298 libvlc_exception_init( &ex );
299 int result = libvlc_video_get_teletext( instance, &ex );
300 catch_exception( &ex );
304 - (void)setRate:(float)value
306 libvlc_media_player_set_rate( instance, value, NULL );
311 libvlc_exception_t ex;
312 libvlc_exception_init( &ex );
313 float result = libvlc_media_player_get_rate( instance, &ex );
314 catch_exception( &ex );
320 libvlc_exception_t ex;
321 libvlc_exception_init( &ex );
322 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
323 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
324 catch_exception( &ex );
330 libvlc_exception_t ex;
331 libvlc_exception_init( &ex );
332 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
333 if (libvlc_exception_raised( &ex ))
335 libvlc_exception_clear( &ex );
342 - (float)framesPerSecond
344 libvlc_exception_t ex;
345 libvlc_exception_init( &ex );
346 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
347 catch_exception( &ex );
351 - (void)setTime:(VLCTime *)value
353 libvlc_exception_t ex;
354 libvlc_exception_init( &ex );
355 // Time is managed in seconds, while duration is managed in microseconds
356 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
357 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
358 (value ? [[value numberValue] longLongValue] / 1000 : 0),
360 catch_exception( &ex );
368 - (void)setChapter:(int)value;
370 libvlc_media_player_set_chapter( instance, value, NULL );
375 libvlc_exception_t ex;
376 libvlc_exception_init( &ex );
377 int result = libvlc_media_player_get_chapter( instance, &ex );
378 catch_exception( &ex );
382 - (int)countOfChapters
384 libvlc_exception_t ex;
385 libvlc_exception_init( &ex );
386 int result = libvlc_media_player_get_chapter_count( instance, &ex );
387 catch_exception( &ex );
391 - (void)setAudioTrack:(int)value
393 libvlc_audio_set_track( instance, value, NULL );
398 libvlc_exception_t ex;
399 libvlc_exception_init( &ex );
400 int result = libvlc_audio_get_track( instance, &ex );
401 catch_exception( &ex );
405 - (int)countOfAudioTracks
407 libvlc_exception_t ex;
408 libvlc_exception_init( &ex );
409 int result = libvlc_audio_get_track_count( instance, &ex );
410 catch_exception( &ex );
414 - (void)setAudioChannel:(int)value
416 libvlc_audio_set_channel( instance, value, NULL );
421 libvlc_exception_t ex;
422 libvlc_exception_init( &ex );
423 int result = libvlc_audio_get_channel( instance, &ex );
424 catch_exception( &ex );
428 - (void)setMedia:(VLCMedia *)value
432 if (media && [media compare:value] == NSOrderedSame)
436 media = [value retain];
438 libvlc_exception_t ex;
439 libvlc_exception_init( &ex );
440 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
441 catch_exception( &ex );
452 libvlc_exception_t ex;
453 libvlc_exception_init( &ex );
454 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
455 catch_exception( &ex );
461 if( [NSThread isMainThread] )
463 /* Hack because we create a dead lock here, when the vout is stopped
464 * and tries to recontact us on the main thread */
465 /* FIXME: to do this properly we need to do some locking. We may want
466 * to move that to libvlc */
467 [self performSelectorInBackground:@selector(pause) withObject:nil];
471 // Return if there is no media available or if the stream is not paused or
472 // playing something else
473 if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
476 // Should never get here.
482 libvlc_exception_t ex;
483 libvlc_exception_init( &ex );
484 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
485 catch_exception( &ex );
487 // TODO: Should we record the time in case the media instance is destroyed
493 if( 0 && [NSThread isMainThread] )
495 /* Hack because we create a dead lock here, when the vout is stopped
496 * and tries to recontact us on the main thread */
497 /* FIXME: to do this properly we need to do some locking. We may want
498 * to move that to libvlc */
499 [self performSelectorInBackground:@selector(stop) withObject:nil];
503 // Return if there is no media available or if the system is not in play status
508 libvlc_exception_t ex;
509 libvlc_exception_init( &ex );
510 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
511 catch_exception( &ex );
516 [self fastForwardAtRate: 2.0];
519 - (void)fastForwardAtRate:(float)rate
526 [self rewindAtRate: 2.0];
529 - (void)rewindAtRate:(float)rate
531 [self setRate: -rate];
534 + (NSSet *)keyPathsForValuesAffectingIsPlaying
536 return [NSSet setWithObjects:@"state", nil];
541 VLCMediaPlayerState state = [self state];
542 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
543 (state == VLCMediaPlayerStatePlaying));
548 libvlc_exception_t ex;
549 libvlc_exception_init( &ex );
550 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
551 if (libvlc_exception_raised(&ex))
553 libvlc_exception_clear(&ex);
560 static const VLCMediaPlayerState libvlc_to_local_state[] =
562 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
563 [libvlc_Opening] = VLCMediaPlayerStateOpening,
564 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
565 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
566 [libvlc_Paused] = VLCMediaPlayerStatePaused,
567 [libvlc_Ended] = VLCMediaPlayerStateEnded,
568 [libvlc_Error] = VLCMediaPlayerStateError
571 - (VLCMediaPlayerState)state
581 - (void)setPosition:(float)newPosition
583 libvlc_exception_t ex;
584 libvlc_exception_init( &ex );
585 libvlc_media_player_set_position( instance, newPosition, &ex );
586 catch_exception( &ex );
591 libvlc_exception_t ex;
592 libvlc_exception_init( &ex );
593 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
594 catch_exception( &ex );
600 libvlc_exception_t ex;
601 libvlc_exception_init( &ex );
602 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
603 catch_exception( &ex );
610 @implementation VLCMediaPlayer (Private)
611 - (id)initWithDrawable:(id)aDrawable
613 if (self = [super init])
617 cachedTime = [[VLCTime nullTime] retain];
619 cachedState = VLCMediaPlayerStateStopped;
621 // Create a media instance, it doesn't matter what library we start off with
622 // it will change depending on the media descriptor provided to the media
624 libvlc_exception_t ex;
625 libvlc_exception_init( &ex );
626 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
627 catch_exception( &ex );
629 [self registerObservers];
631 [self setDrawable:aDrawable];
636 - (void)registerObservers
638 libvlc_exception_t ex;
639 libvlc_exception_init( &ex );
641 // Attach event observers into the media instance
642 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
643 libvlc_event_attach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, &ex );
644 libvlc_event_attach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, &ex );
645 libvlc_event_attach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, &ex );
646 /* FIXME: We may want to turn that off when none is interested by that */
647 libvlc_event_attach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaPositionChanged, self, &ex );
648 libvlc_event_attach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, &ex );
649 catch_exception( &ex );
652 - (void)unregisterObservers
654 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
655 libvlc_event_detach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, NULL );
656 libvlc_event_detach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, NULL );
657 libvlc_event_detach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, NULL );
658 libvlc_event_detach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaPositionChanged, self, NULL );
659 libvlc_event_detach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, NULL );
662 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
664 [self willChangeValueForKey:@"time"];
665 [cachedTime release];
666 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
667 [self didChangeValueForKey:@"time"];
670 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
672 if( [newPosition floatValue] - position < 0.005 && position - [newPosition floatValue] < 0.005 )
673 return; /* Forget that, this is too much precision for our uses */
674 [self willChangeValueForKey:@"position"];
675 position = ((float)((int)([newPosition floatValue]*1000)))/1000.;
676 [self didChangeValueForKey:@"position"];
679 - (void)mediaPlayerStateChanged:(NSNumber *)newState
681 [self willChangeValueForKey:@"state"];
682 cachedState = [newState intValue];
683 [self didChangeValueForKey:@"state"];