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"
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];
66 [[VLCEventManager sharedManager] callOnMainThreadObject:self
67 withMethod:@selector(mediaPlayerTimeChanged:)
68 withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
70 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
71 withDelegateMethod:@selector(mediaPlayerTimeChanged:)
72 withNotificationName:VLCMediaPlayerTimeChanged];
76 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
78 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
80 [[VLCEventManager sharedManager] callOnMainThreadObject:self
81 withMethod:@selector(mediaPlayerPositionChanged:)
82 withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
86 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
88 VLCMediaPlayerState newState;
90 if( event->type == libvlc_MediaPlayerPlaying )
91 newState = VLCMediaPlayerStatePlaying;
92 else if( event->type == libvlc_MediaPlayerPaused )
93 newState = VLCMediaPlayerStatePaused;
94 else if( event->type == libvlc_MediaPlayerEndReached )
95 newState = VLCMediaPlayerStateStopped;
96 else if( event->type == libvlc_MediaPlayerEncounteredError )
97 newState = VLCMediaPlayerStateError;
100 NSLog(@"%s: Unknown event", __FUNCTION__);
104 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
106 [[VLCEventManager sharedManager] callOnMainThreadObject:self
107 withMethod:@selector(mediaPlayerStateChanged:)
108 withArgumentAsObject:[NSNumber numberWithInt:newState]];
110 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
111 withDelegateMethod:@selector(mediaPlayerStateChanged:)
112 withNotificationName:VLCMediaPlayerStateChanged];
119 // TODO: Documentation
120 @interface VLCMediaPlayer (Private)
121 - (id)initWithDrawable:(id)aDrawable;
123 - (void)registerObservers;
124 - (void)unregisterObservers;
125 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
126 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
127 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
130 @implementation VLCMediaPlayer
133 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
135 static NSDictionary * dict = nil;
136 NSSet * superKeyPaths;
139 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
140 [NSSet setWithObject:@"state"], @"playing",
141 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
142 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
143 [NSSet setWithObjects:@"state", @"media", nil], @"description",
146 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
148 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
149 [ret unionSet:superKeyPaths];
152 return [dict objectForKey: key];
158 return [self initWithDrawable:nil];
161 - (id)initWithVideoView:(VLCVideoView *)aVideoView
163 return [self initWithDrawable: aVideoView];
166 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
168 return [self initWithDrawable: aVideoLayer];
175 if([self retainCount] <= 1)
177 /* We must make sure we won't receive new event after an upcoming dealloc
178 * We also may receive a -retain in some event callback that may occcur
179 * Before libvlc_event_detach. So this can't happen in dealloc */
180 [self unregisterObservers];
188 // Always get rid of the delegate first so we can stop sending messages to it
189 // TODO: Should we tell the delegate that we're shutting down?
192 libvlc_media_player_release((libvlc_media_player_t *)instance);
194 // Get rid of everything else
196 [cachedTime release];
201 - (void)setDelegate:(id)value
211 - (void)setVideoView:(VLCVideoView *)aVideoView
213 [self setDrawable: aVideoView];
216 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
218 [self setDrawable: aVideoLayer];
221 - (void)setDrawable:(id)aDrawable
223 // Make sure that this instance has been associated with the drawing canvas.
224 libvlc_exception_t ex;
225 libvlc_exception_init( &ex );
226 libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
227 catch_exception( &ex );
232 libvlc_exception_t ex;
233 libvlc_exception_init( &ex );
234 id ret = libvlc_media_player_get_nsobject(instance);
235 catch_exception( &ex );
241 return [[VLCLibrary sharedLibrary] audio];
244 - (void)setVideoAspectRatio:(char *)value
246 libvlc_video_set_aspect_ratio( instance, value, NULL );
249 - (char *)videoAspectRatio
251 libvlc_exception_t ex;
252 libvlc_exception_init( &ex );
253 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
254 catch_exception( &ex );
258 - (void)setVideoSubTitles:(int)value
260 libvlc_video_set_spu( instance, value, NULL );
263 - (int)videoSubTitles
265 libvlc_exception_t ex;
266 libvlc_exception_init( &ex );
267 int result = libvlc_video_get_spu( instance, &ex );
268 catch_exception( &ex );
272 - (void)setVideoCropGeometry:(char *)value
274 libvlc_video_set_crop_geometry( instance, value, NULL );
277 - (char *)videoCropGeometry
279 libvlc_exception_t ex;
280 libvlc_exception_init( &ex );
281 char * result = libvlc_video_get_crop_geometry( instance, &ex );
282 catch_exception( &ex );
286 - (void)setVideoTeleText:(int)value
288 libvlc_video_set_teletext( instance, value, NULL );
293 libvlc_exception_t ex;
294 libvlc_exception_init( &ex );
295 int result = libvlc_video_get_teletext( instance, &ex );
296 catch_exception( &ex );
300 - (void)setRate:(float)value
302 libvlc_media_player_set_rate( instance, value, NULL );
307 libvlc_exception_t ex;
308 libvlc_exception_init( &ex );
309 float result = libvlc_media_player_get_rate( instance, &ex );
310 catch_exception( &ex );
316 libvlc_exception_t ex;
317 libvlc_exception_init( &ex );
318 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
319 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
320 catch_exception( &ex );
326 libvlc_exception_t ex;
327 libvlc_exception_init( &ex );
328 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
329 if (libvlc_exception_raised( &ex ))
331 libvlc_exception_clear( &ex );
338 - (float)framesPerSecond
340 libvlc_exception_t ex;
341 libvlc_exception_init( &ex );
342 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
343 catch_exception( &ex );
347 - (void)setTime:(VLCTime *)value
349 libvlc_exception_t ex;
350 libvlc_exception_init( &ex );
351 // Time is managed in seconds, while duration is managed in microseconds
352 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
353 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
354 (value ? [[value numberValue] longLongValue] / 1000 : 0),
356 catch_exception( &ex );
364 - (void)setChapter:(int)value;
366 libvlc_media_player_set_chapter( instance, value, NULL );
371 libvlc_exception_t ex;
372 libvlc_exception_init( &ex );
373 int result = libvlc_media_player_get_chapter( instance, &ex );
374 catch_exception( &ex );
378 - (int)countOfChapters
380 libvlc_exception_t ex;
381 libvlc_exception_init( &ex );
382 int result = libvlc_media_player_get_chapter_count( instance, &ex );
383 catch_exception( &ex );
387 - (void)setAudioTrack:(int)value
389 libvlc_audio_set_track( instance, value, NULL );
394 libvlc_exception_t ex;
395 libvlc_exception_init( &ex );
396 int result = libvlc_audio_get_track( instance, &ex );
397 catch_exception( &ex );
401 - (int)countOfAudioTracks
403 libvlc_exception_t ex;
404 libvlc_exception_init( &ex );
405 int result = libvlc_audio_get_track_count( instance, &ex );
406 catch_exception( &ex );
410 - (void)setAudioChannel:(int)value
412 libvlc_audio_set_channel( instance, value, NULL );
417 libvlc_exception_t ex;
418 libvlc_exception_init( &ex );
419 int result = libvlc_audio_get_channel( instance, &ex );
420 catch_exception( &ex );
424 - (void)setMedia:(VLCMedia *)value
428 if (media && [media compare:value] == NSOrderedSame)
432 media = [value retain];
434 libvlc_exception_t ex;
435 libvlc_exception_init( &ex );
436 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
437 catch_exception( &ex );
448 libvlc_exception_t ex;
449 libvlc_exception_init( &ex );
450 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
451 catch_exception( &ex );
457 if( [NSThread isMainThread] )
459 /* Hack because we create a dead lock here, when the vout is stopped
460 * and tries to recontact us on the main thread */
461 /* FIXME: to do this properly we need to do some locking. We may want
462 * to move that to libvlc */
463 [self performSelectorInBackground:@selector(pause) withObject:nil];
467 // Return if there is no media available or if the stream is not paused or
468 // playing something else
469 if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
472 // Should never get here.
478 libvlc_exception_t ex;
479 libvlc_exception_init( &ex );
480 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
481 catch_exception( &ex );
483 // TODO: Should we record the time in case the media instance is destroyed
489 if( 0 && [NSThread isMainThread] )
491 /* Hack because we create a dead lock here, when the vout is stopped
492 * and tries to recontact us on the main thread */
493 /* FIXME: to do this properly we need to do some locking. We may want
494 * to move that to libvlc */
495 [self performSelectorInBackground:@selector(stop) withObject:nil];
499 // Return if there is no media available or if the system is not in play status
504 libvlc_exception_t ex;
505 libvlc_exception_init( &ex );
506 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
507 catch_exception( &ex );
512 [self fastForwardAtRate: 2.0];
515 - (void)fastForwardAtRate:(float)rate
522 [self rewindAtRate: 2.0];
525 - (void)rewindAtRate:(float)rate
527 [self setRate: -rate];
530 + (NSSet *)keyPathsForValuesAffectingIsPlaying
532 return [NSSet setWithObjects:@"state", nil];
537 VLCMediaPlayerState state = [self state];
538 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
539 (state == VLCMediaPlayerStatePlaying));
544 libvlc_exception_t ex;
545 libvlc_exception_init( &ex );
546 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
547 if (libvlc_exception_raised(&ex))
549 libvlc_exception_clear(&ex);
556 static const VLCMediaPlayerState libvlc_to_local_state[] =
558 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
559 [libvlc_Opening] = VLCMediaPlayerStateOpening,
560 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
561 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
562 [libvlc_Paused] = VLCMediaPlayerStatePaused,
563 [libvlc_Ended] = VLCMediaPlayerStateEnded,
564 [libvlc_Error] = VLCMediaPlayerStateError
567 - (VLCMediaPlayerState)state
577 - (void)setPosition:(float)newPosition
579 libvlc_exception_t ex;
580 libvlc_exception_init( &ex );
581 libvlc_media_player_set_position( instance, newPosition, &ex );
582 catch_exception( &ex );
587 libvlc_exception_t ex;
588 libvlc_exception_init( &ex );
589 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
590 catch_exception( &ex );
596 libvlc_exception_t ex;
597 libvlc_exception_init( &ex );
598 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
599 catch_exception( &ex );
603 - (void *)libVLCMediaPlayer
609 @implementation VLCMediaPlayer (Private)
610 - (id)initWithDrawable:(id)aDrawable
612 if (self = [super init])
616 cachedTime = [[VLCTime nullTime] retain];
618 cachedState = VLCMediaPlayerStateStopped;
620 // Create a media instance, it doesn't matter what library we start off with
621 // it will change depending on the media descriptor provided to the media
623 libvlc_exception_t ex;
624 libvlc_exception_init( &ex );
625 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
626 catch_exception( &ex );
628 [self registerObservers];
630 [self setDrawable:aDrawable];
635 - (void)registerObservers
637 libvlc_exception_t ex;
638 libvlc_exception_init( &ex );
640 // Attach event observers into the media instance
641 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
642 libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
643 libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
644 libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
645 libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, 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_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
648 libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, 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_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
656 libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
657 libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
658 libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
659 libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
660 libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
663 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
665 [self willChangeValueForKey:@"time"];
666 [cachedTime release];
667 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
669 [self didChangeValueForKey:@"time"];
672 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
674 [self willChangeValueForKey:@"position"];
675 position = [newPosition floatValue];
676 [self didChangeValueForKey:@"position"];
679 - (void)mediaPlayerStateChanged:(NSNumber *)newState
681 [self willChangeValueForKey:@"state"];
682 cachedState = [newState intValue];
683 [self didChangeValueForKey:@"state"];