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];
127 static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
129 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
131 [[VLCEventManager sharedManager] callOnMainThreadObject:self
132 withMethod:@selector(mediaPlayerMediaChanged:)
133 withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
140 // TODO: Documentation
141 @interface VLCMediaPlayer (Private)
142 - (id)initWithDrawable:(id)aDrawable;
144 - (void)registerObservers;
145 - (void)unregisterObservers;
146 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
147 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
148 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
149 - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
152 @implementation VLCMediaPlayer
155 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
157 static NSDictionary * dict = nil;
158 NSSet * superKeyPaths;
161 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
162 [NSSet setWithObject:@"state"], @"playing",
163 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
164 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
165 [NSSet setWithObjects:@"state", @"media", nil], @"description",
168 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
170 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
171 [ret unionSet:superKeyPaths];
174 return [dict objectForKey: key];
180 return [self initWithDrawable:nil];
183 - (id)initWithVideoView:(VLCVideoView *)aVideoView
185 return [self initWithDrawable: aVideoView];
188 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
190 return [self initWithDrawable: aVideoLayer];
197 if([self retainCount] <= 1)
199 /* We must make sure we won't receive new event after an upcoming dealloc
200 * We also may receive a -retain in some event callback that may occcur
201 * Before libvlc_event_detach. So this can't happen in dealloc */
202 [self unregisterObservers];
210 NSAssert(libvlc_media_player_get_state(instance, NULL) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
212 // Always get rid of the delegate first so we can stop sending messages to it
213 // TODO: Should we tell the delegate that we're shutting down?
216 // Clear our drawable as we are going to release it, we don't
217 // want the core to use it from this point. This won't happen as
218 // the media player must be stopped.
219 libvlc_media_player_set_nsobject(instance, nil, NULL);
221 libvlc_media_player_release(instance);
223 // Get rid of everything else
225 [cachedTime release];
226 [cachedRemainingTime release];
232 - (void)setDelegate:(id)value
242 - (void)setVideoView:(VLCVideoView *)aVideoView
244 [self setDrawable: aVideoView];
247 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
249 [self setDrawable: aVideoLayer];
252 - (void)setDrawable:(id)aDrawable
254 // Make sure that this instance has been associated with the drawing canvas.
255 libvlc_exception_t ex;
256 libvlc_exception_init( &ex );
257 libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
258 catch_exception( &ex );
263 libvlc_exception_t ex;
264 libvlc_exception_init( &ex );
265 id ret = libvlc_media_player_get_nsobject(instance);
266 catch_exception( &ex );
272 return [[VLCLibrary sharedLibrary] audio];
276 #pragma mark Subtitles
278 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
280 libvlc_exception_t ex;
281 libvlc_exception_init( &ex );
282 libvlc_video_set_spu( instance, (int)index, &ex );
283 catch_exception( &ex );
286 - (NSUInteger)currentVideoSubTitleIndex
288 libvlc_exception_t ex;
289 libvlc_exception_init( &ex );
290 NSInteger count = libvlc_video_get_spu_count( instance, &ex );
291 if (libvlc_exception_raised( &ex ))
293 libvlc_exception_clear( &ex );
298 NSUInteger result = libvlc_video_get_spu( instance, &ex );
299 if (libvlc_exception_raised( &ex ))
301 libvlc_exception_clear( &ex );
308 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
310 libvlc_exception_t ex;
311 libvlc_exception_init( &ex );
312 BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
313 catch_exception( &ex );
317 - (NSArray *)videoSubTitles
319 libvlc_exception_t ex;
320 libvlc_exception_init( &ex );
321 NSInteger count = libvlc_video_get_spu_count( instance, &ex );
322 catch_exception( &ex );
324 libvlc_track_description_t *tracks = libvlc_video_get_spu_description( instance, &ex );
325 NSMutableArray *tempArray = [NSMutableArray array];
327 for (i = 0; i < count; i++)
329 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
330 tracks = tracks->p_next;
332 return [NSArray arrayWithArray: tempArray];
337 #pragma mark Video Crop geometry
339 - (void)setVideoCropGeometry:(char *)value
341 libvlc_exception_t ex;
342 libvlc_exception_init( &ex );
343 libvlc_video_set_crop_geometry( instance, value, &ex );
344 catch_exception( &ex );
347 - (char *)videoCropGeometry
349 libvlc_exception_t ex;
350 libvlc_exception_init( &ex );
351 char * result = libvlc_video_get_crop_geometry( instance, &ex );
352 catch_exception( &ex );
356 - (void)setVideoAspectRatio:(char *)value
358 libvlc_exception_t ex;
359 libvlc_exception_init( &ex );
360 libvlc_video_set_aspect_ratio( instance, value, &ex );
361 catch_exception( &ex );
364 - (char *)videoAspectRatio
366 libvlc_exception_t ex;
367 libvlc_exception_init( &ex );
368 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
369 catch_exception( &ex );
373 - (void)setVideoTeleText:(NSUInteger)value
375 libvlc_exception_t ex;
376 libvlc_exception_init( &ex );
377 libvlc_video_set_teletext( instance, value, &ex );
378 catch_exception( &ex );
381 - (NSUInteger)videoTeleText
383 libvlc_exception_t ex;
384 libvlc_exception_init( &ex );
385 NSInteger result = libvlc_video_get_teletext( instance, &ex );
386 catch_exception( &ex );
390 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
392 libvlc_exception_t ex;
393 libvlc_exception_init( &ex );
394 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
395 catch_exception( &ex );
398 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
400 libvlc_exception_t ex;
401 libvlc_exception_init( &ex );
402 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
403 catch_exception( &ex );
406 - (void)setRate:(float)value
408 libvlc_exception_t ex;
409 libvlc_exception_init( &ex );
410 libvlc_media_player_set_rate( instance, value, &ex );
411 catch_exception( &ex );
416 libvlc_exception_t ex;
417 libvlc_exception_init( &ex );
418 float result = libvlc_media_player_get_rate( instance, &ex );
419 catch_exception( &ex );
425 libvlc_exception_t ex;
426 libvlc_exception_init( &ex );
427 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
428 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
429 catch_exception( &ex );
435 libvlc_exception_t ex;
436 libvlc_exception_init( &ex );
437 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
438 if (libvlc_exception_raised( &ex ))
440 libvlc_exception_clear( &ex );
447 - (float)framesPerSecond
449 libvlc_exception_t ex;
450 libvlc_exception_init( &ex );
451 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
452 catch_exception( &ex );
456 - (void)setTime:(VLCTime *)value
458 libvlc_exception_t ex;
459 libvlc_exception_init( &ex );
460 // Time is managed in seconds, while duration is managed in microseconds
461 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
462 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
463 (value ? [[value numberValue] longLongValue] : 0),
465 catch_exception( &ex );
473 - (VLCTime *)remainingTime
475 return cachedRemainingTime;
480 libvlc_exception_t ex;
481 libvlc_exception_init( &ex );
482 NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
483 catch_exception( &ex );
488 #pragma mark Chapters
489 - (void)setCurrentChapterIndex:(NSUInteger)value;
491 libvlc_exception_t ex;
492 libvlc_exception_init( &ex );
493 libvlc_media_player_set_chapter( instance, value, &ex );
494 catch_exception( &ex );
497 - (NSUInteger)currentChapterIndex
499 libvlc_exception_t ex;
500 libvlc_exception_init( &ex );
501 NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
502 catch_exception( &ex );
505 NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
506 catch_exception( &ex );
512 libvlc_exception_t ex;
513 libvlc_exception_init( &ex );
514 libvlc_media_player_next_chapter( instance, &ex );
515 catch_exception( &ex );
518 - (void)previousChapter
520 libvlc_exception_t ex;
521 libvlc_exception_init( &ex );
522 libvlc_media_player_previous_chapter( instance, &ex );
523 catch_exception( &ex );
526 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
528 libvlc_exception_t ex;
529 libvlc_exception_init( &ex );
530 NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
532 return [NSArray array];
534 libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
535 NSMutableArray *tempArray = [NSMutableArray array];
537 for (i = 0; i < count ; i++)
539 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
540 tracks = tracks->p_next;
542 return [NSArray arrayWithArray: tempArray];
548 - (void)setCurrentTitleIndex:(NSUInteger)value
550 libvlc_exception_t ex;
551 libvlc_exception_init( &ex );
552 libvlc_media_player_set_title( instance, value, &ex );
553 catch_exception( &ex );
556 - (NSUInteger)currentTitleIndex
558 libvlc_exception_t ex;
559 libvlc_exception_init( &ex );
561 NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
562 catch_exception( &ex );
566 NSUInteger result = libvlc_media_player_get_title( instance, &ex );
567 catch_exception( &ex );
571 - (NSUInteger)countOfTitles
573 libvlc_exception_t ex;
574 libvlc_exception_init( &ex );
575 NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
576 catch_exception( &ex );
582 libvlc_exception_t ex;
583 libvlc_exception_init( &ex );
584 libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
585 NSMutableArray *tempArray = [NSMutableArray array];
587 for (i = 0; i < [self countOfTitles] ; i++)
589 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
590 tracks = tracks->p_next;
592 return [NSArray arrayWithArray: tempArray];
596 #pragma mark Audio tracks
597 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
599 libvlc_exception_t ex;
600 libvlc_exception_init( &ex );
601 libvlc_audio_set_track( instance, (int)value, &ex );
602 catch_exception( &ex );
605 - (NSUInteger)currentAudioTrackIndex
607 libvlc_exception_t ex;
608 libvlc_exception_init( &ex );
609 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
610 catch_exception( &ex );
614 NSUInteger result = libvlc_audio_get_track( instance, &ex );
615 catch_exception( &ex );
619 - (NSArray *)audioTracks
621 libvlc_exception_t ex;
622 libvlc_exception_init( &ex );
623 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
624 catch_exception( &ex );
626 return [NSArray array];
628 libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
629 NSMutableArray *tempArray = [NSMutableArray array];
631 for (i = 0; i < count ; i++)
633 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
634 tracks = tracks->p_next;
637 return [NSArray arrayWithArray: tempArray];
640 - (void)setAudioChannel:(NSInteger)value
642 libvlc_exception_t ex;
643 libvlc_exception_init( &ex );
644 libvlc_audio_set_channel( instance, value, &ex );
645 catch_exception( &ex );
648 - (NSInteger)audioChannel
650 libvlc_exception_t ex;
651 libvlc_exception_init( &ex );
652 NSInteger result = libvlc_audio_get_channel( instance, &ex );
653 catch_exception( &ex );
657 - (void)setMedia:(VLCMedia *)value
661 if (media && [media compare:value] == NSOrderedSame)
665 media = [value retain];
667 libvlc_exception_t ex;
668 libvlc_exception_init( &ex );
669 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
670 catch_exception( &ex );
681 libvlc_exception_t ex;
682 libvlc_exception_init( &ex );
683 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
684 catch_exception( &ex );
690 if( [NSThread isMainThread] )
692 /* Hack because we create a dead lock here, when the vout is stopped
693 * and tries to recontact us on the main thread */
694 /* FIXME: to do this properly we need to do some locking. We may want
695 * to move that to libvlc */
696 [self performSelectorInBackground:@selector(pause) withObject:nil];
701 libvlc_exception_t ex;
702 libvlc_exception_init( &ex );
703 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
706 // in most cases, it's just EOF so let's stop
707 if (libvlc_exception_raised(&ex))
710 libvlc_exception_clear(&ex);
715 libvlc_exception_t ex;
716 libvlc_exception_init( &ex );
717 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
718 catch_exception( &ex );
723 [self fastForwardAtRate: 2.0];
726 - (void)fastForwardAtRate:(float)rate
733 [self rewindAtRate: 2.0];
736 - (void)rewindAtRate:(float)rate
738 [self setRate: -rate];
741 - (void)jumpBackward:(NSInteger)interval
743 if( [self isSeekable] )
745 interval = interval * 1000;
746 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
750 - (void)jumpForward:(NSInteger)interval
752 if( [self isSeekable] )
754 interval = interval * 1000;
755 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
759 - (void)extraShortJumpBackward
761 [self jumpBackward:3];
764 - (void)extraShortJumpForward
766 [self jumpForward:3];
769 - (void)shortJumpBackward
771 [self jumpBackward:10];
774 - (void)shortJumpForward
776 [self jumpForward:10];
779 - (void)mediumJumpBackward
781 [self jumpBackward:60];
784 - (void)mediumJumpForward
786 [self jumpForward:60];
789 - (void)longJumpBackward
791 [self jumpBackward:300];
794 - (void)longJumpForward
796 [self jumpForward:300];
799 + (NSSet *)keyPathsForValuesAffectingIsPlaying
801 return [NSSet setWithObjects:@"state", nil];
806 VLCMediaPlayerState state = [self state];
807 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
808 (state == VLCMediaPlayerStatePlaying));
813 libvlc_exception_t ex;
814 libvlc_exception_init( &ex );
815 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
816 if (libvlc_exception_raised(&ex))
818 libvlc_exception_clear(&ex);
825 static const VLCMediaPlayerState libvlc_to_local_state[] =
827 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
828 [libvlc_Opening] = VLCMediaPlayerStateOpening,
829 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
830 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
831 [libvlc_Paused] = VLCMediaPlayerStatePaused,
832 [libvlc_Ended] = VLCMediaPlayerStateEnded,
833 [libvlc_Error] = VLCMediaPlayerStateError
836 - (VLCMediaPlayerState)state
846 - (void)setPosition:(float)newPosition
848 libvlc_exception_t ex;
849 libvlc_exception_init( &ex );
850 libvlc_media_player_set_position( instance, newPosition, &ex );
851 catch_exception( &ex );
856 libvlc_exception_t ex;
857 libvlc_exception_init( &ex );
858 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
859 catch_exception( &ex );
865 libvlc_exception_t ex;
866 libvlc_exception_init( &ex );
867 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
868 catch_exception( &ex );
872 - (void *)libVLCMediaPlayer
878 @implementation VLCMediaPlayer (Private)
879 - (id)initWithDrawable:(id)aDrawable
881 if (self = [super init])
885 cachedTime = [[VLCTime nullTime] retain];
886 cachedRemainingTime = [[VLCTime nullTime] retain];
888 cachedState = VLCMediaPlayerStateStopped;
890 // Create a media instance, it doesn't matter what library we start off with
891 // it will change depending on the media descriptor provided to the media
893 libvlc_exception_t ex;
894 libvlc_exception_init( &ex );
895 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
896 catch_exception( &ex );
898 [self registerObservers];
900 [self setDrawable:aDrawable];
905 - (void)registerObservers
907 libvlc_exception_t ex;
908 libvlc_exception_init( &ex );
910 // Attach event observers into the media instance
911 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
912 libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
913 libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
914 libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
915 libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
916 /* FIXME: We may want to turn that off when none is interested by that */
917 libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
918 libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
919 libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, &ex );
920 catch_exception( &ex );
923 - (void)unregisterObservers
925 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
926 libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
927 libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
928 libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
929 libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
930 libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
931 libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
932 libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, NULL );
935 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
937 [self willChangeValueForKey:@"time"];
938 [self willChangeValueForKey:@"remainingTime"];
939 [cachedTime release];
940 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
941 [cachedRemainingTime release];
942 double currentTime = [[cachedTime numberValue] doubleValue];
943 double remaining = currentTime / position * (1 - position);
944 cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
945 [self didChangeValueForKey:@"remainingTime"];
946 [self didChangeValueForKey:@"time"];
951 UpdateSystemActivity(UsrActivity);
954 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
956 // This seems to be the most relevant place to delay sleeping and screen saver.
959 [self willChangeValueForKey:@"position"];
960 position = [newPosition floatValue];
961 [self didChangeValueForKey:@"position"];
964 - (void)mediaPlayerStateChanged:(NSNumber *)newState
966 [self willChangeValueForKey:@"state"];
967 cachedState = [newState intValue];
968 [self didChangeValueForKey:@"state"];
971 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
973 [self willChangeValueForKey:@"media"];
974 if (media != newMedia)
977 media = [newMedia retain];
979 [self didChangeValueForKey:@"media"];