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 static inline libvlc_track_description_t *freeAndGetNextTrack(libvlc_track_description_t *track)
66 libvlc_track_description_t *next = track->p_next;
67 libvlc_track_description_release(track);
71 /* libvlc event callback */
72 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
74 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
75 withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
76 withNotificationName:VLCMediaPlayerVolumeChanged];
79 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
81 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
82 [[VLCEventManager sharedManager] callOnMainThreadObject:self
83 withMethod:@selector(mediaPlayerTimeChanged:)
84 withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
86 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
87 withDelegateMethod:@selector(mediaPlayerTimeChanged:)
88 withNotificationName:VLCMediaPlayerTimeChanged];
92 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
94 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
96 [[VLCEventManager sharedManager] callOnMainThreadObject:self
97 withMethod:@selector(mediaPlayerPositionChanged:)
98 withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
102 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
104 VLCMediaPlayerState newState;
106 if( event->type == libvlc_MediaPlayerPlaying )
107 newState = VLCMediaPlayerStatePlaying;
108 else if( event->type == libvlc_MediaPlayerPaused )
109 newState = VLCMediaPlayerStatePaused;
110 else if( event->type == libvlc_MediaPlayerEndReached )
111 newState = VLCMediaPlayerStateStopped;
112 else if( event->type == libvlc_MediaPlayerEncounteredError )
113 newState = VLCMediaPlayerStateError;
116 NSLog(@"%s: Unknown event", __FUNCTION__);
120 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
122 [[VLCEventManager sharedManager] callOnMainThreadObject:self
123 withMethod:@selector(mediaPlayerStateChanged:)
124 withArgumentAsObject:[NSNumber numberWithInt:newState]];
126 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
127 withDelegateMethod:@selector(mediaPlayerStateChanged:)
128 withNotificationName:VLCMediaPlayerStateChanged];
134 static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
136 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
138 [[VLCEventManager sharedManager] callOnMainThreadObject:self
139 withMethod:@selector(mediaPlayerMediaChanged:)
140 withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
147 // TODO: Documentation
148 @interface VLCMediaPlayer (Private)
149 - (id)initWithDrawable:(id)aDrawable;
151 - (void)registerObservers;
152 - (void)unregisterObservers;
153 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
154 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
155 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
156 - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
159 @implementation VLCMediaPlayer
162 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
164 static NSDictionary * dict = nil;
165 NSSet * superKeyPaths;
168 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
169 [NSSet setWithObject:@"state"], @"playing",
170 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
171 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
172 [NSSet setWithObjects:@"state", @"media", nil], @"description",
175 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
177 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
178 [ret unionSet:superKeyPaths];
181 return [dict objectForKey: key];
187 return [self initWithDrawable:nil];
190 - (id)initWithVideoView:(VLCVideoView *)aVideoView
192 return [self initWithDrawable: aVideoView];
195 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
197 return [self initWithDrawable: aVideoLayer];
204 if([self retainCount] <= 1)
206 /* We must make sure we won't receive new event after an upcoming dealloc
207 * We also may receive a -retain in some event callback that may occcur
208 * Before libvlc_event_detach. So this can't happen in dealloc */
209 [self unregisterObservers];
217 NSAssert(libvlc_media_player_get_state(instance) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
219 // Always get rid of the delegate first so we can stop sending messages to it
220 // TODO: Should we tell the delegate that we're shutting down?
223 // Clear our drawable as we are going to release it, we don't
224 // want the core to use it from this point. This won't happen as
225 // the media player must be stopped.
226 libvlc_media_player_set_nsobject(instance, nil);
228 libvlc_media_player_release(instance);
230 // Get rid of everything else
232 [cachedTime release];
233 [cachedRemainingTime release];
239 - (void)setDelegate:(id)value
249 - (void)setVideoView:(VLCVideoView *)aVideoView
251 [self setDrawable: aVideoView];
254 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
256 [self setDrawable: aVideoLayer];
259 - (void)setDrawable:(id)aDrawable
261 // Make sure that this instance has been associated with the drawing canvas.
262 libvlc_media_player_set_nsobject(instance, aDrawable);
267 libvlc_exception_t ex;
268 libvlc_exception_init( &ex );
269 id ret = libvlc_media_player_get_nsobject(instance);
270 catch_exception( &ex );
276 return [[VLCLibrary sharedLibrary] audio];
280 #pragma mark Subtitles
282 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
284 libvlc_exception_t ex;
285 libvlc_exception_init( &ex );
286 libvlc_video_set_spu( instance, (int)index, &ex );
287 catch_exception( &ex );
290 - (NSUInteger)currentVideoSubTitleIndex
292 libvlc_exception_t ex;
293 libvlc_exception_init( &ex );
294 NSInteger count = libvlc_video_get_spu_count( instance, &ex );
295 if (libvlc_exception_raised( &ex ))
297 libvlc_exception_clear( &ex );
302 NSUInteger result = libvlc_video_get_spu( instance, &ex );
303 if (libvlc_exception_raised( &ex ))
305 libvlc_exception_clear( &ex );
312 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
314 libvlc_exception_t ex;
315 libvlc_exception_init( &ex );
316 BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
317 catch_exception( &ex );
321 - (NSArray *)videoSubTitles
323 libvlc_exception_t ex;
324 libvlc_exception_init( &ex );
325 libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description( instance, &ex );
326 catch_exception( &ex );
328 NSMutableArray *tempArray = [NSMutableArray array];
329 while (currentTrack) {
330 [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
331 currentTrack = freeAndGetNextTrack(currentTrack);
333 return [NSArray arrayWithArray: tempArray];
338 #pragma mark Video Crop geometry
340 - (void)setVideoCropGeometry:(char *)value
342 libvlc_exception_t ex;
343 libvlc_exception_init( &ex );
344 libvlc_video_set_crop_geometry( instance, value, &ex );
345 catch_exception( &ex );
348 - (char *)videoCropGeometry
350 libvlc_exception_t ex;
351 libvlc_exception_init( &ex );
352 char * result = libvlc_video_get_crop_geometry( instance, &ex );
353 catch_exception( &ex );
357 - (void)setVideoAspectRatio:(char *)value
359 libvlc_exception_t ex;
360 libvlc_exception_init( &ex );
361 libvlc_video_set_aspect_ratio( instance, value, &ex );
362 catch_exception( &ex );
365 - (char *)videoAspectRatio
367 libvlc_exception_t ex;
368 libvlc_exception_init( &ex );
369 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
370 catch_exception( &ex );
374 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
376 libvlc_exception_t ex;
377 libvlc_exception_init( &ex );
378 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
379 catch_exception( &ex );
382 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
384 libvlc_exception_t ex;
385 libvlc_exception_init( &ex );
386 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
387 catch_exception( &ex );
390 - (void)setRate:(float)value
392 libvlc_exception_t ex;
393 libvlc_exception_init( &ex );
394 libvlc_media_player_set_rate( instance, value, &ex );
395 catch_exception( &ex );
400 libvlc_exception_t ex;
401 libvlc_exception_init( &ex );
402 float result = libvlc_media_player_get_rate( instance, &ex );
403 if (libvlc_exception_raised(&ex))
406 libvlc_exception_clear(&ex);
413 libvlc_exception_t ex;
414 libvlc_exception_init( &ex );
415 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
416 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
417 catch_exception( &ex );
423 libvlc_exception_t ex;
424 libvlc_exception_init( &ex );
425 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
426 if (libvlc_exception_raised( &ex ))
428 libvlc_exception_clear( &ex );
435 - (float)framesPerSecond
437 libvlc_exception_t ex;
438 libvlc_exception_init( &ex );
439 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
440 catch_exception( &ex );
444 - (void)setTime:(VLCTime *)value
446 libvlc_exception_t ex;
447 libvlc_exception_init( &ex );
448 // Time is managed in seconds, while duration is managed in microseconds
449 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
450 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
451 (value ? [[value numberValue] longLongValue] : 0),
453 catch_exception( &ex );
461 - (VLCTime *)remainingTime
463 return cachedRemainingTime;
468 libvlc_exception_t ex;
469 libvlc_exception_init( &ex );
470 NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
471 catch_exception( &ex );
476 #pragma mark Chapters
477 - (void)setCurrentChapterIndex:(NSUInteger)value;
479 libvlc_exception_t ex;
480 libvlc_exception_init( &ex );
481 libvlc_media_player_set_chapter( instance, value, &ex );
482 catch_exception( &ex );
485 - (NSUInteger)currentChapterIndex
487 libvlc_exception_t ex;
488 libvlc_exception_init( &ex );
489 NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
490 catch_exception( &ex );
493 NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
494 catch_exception( &ex );
500 libvlc_exception_t ex;
501 libvlc_exception_init( &ex );
502 libvlc_media_player_next_chapter( instance, &ex );
503 catch_exception( &ex );
506 - (void)previousChapter
508 libvlc_exception_t ex;
509 libvlc_exception_init( &ex );
510 libvlc_media_player_previous_chapter( instance, &ex );
511 catch_exception( &ex );
514 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
516 libvlc_exception_t ex;
517 libvlc_exception_init( &ex );
518 NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
520 return [NSArray array];
522 libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
523 NSMutableArray *tempArray = [NSMutableArray array];
525 for (i = 0; i < count ; i++)
527 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
528 tracks = freeAndGetNextTrack(tracks);
530 return [NSArray arrayWithArray: tempArray];
536 - (void)setCurrentTitleIndex:(NSUInteger)value
538 libvlc_exception_t ex;
539 libvlc_exception_init( &ex );
540 libvlc_media_player_set_title( instance, value, &ex );
541 catch_exception( &ex );
544 - (NSUInteger)currentTitleIndex
546 libvlc_exception_t ex;
547 libvlc_exception_init( &ex );
549 NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
550 catch_exception( &ex );
554 NSUInteger result = libvlc_media_player_get_title( instance, &ex );
555 catch_exception( &ex );
559 - (NSUInteger)countOfTitles
561 libvlc_exception_t ex;
562 libvlc_exception_init( &ex );
563 NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
564 catch_exception( &ex );
570 libvlc_exception_t ex;
571 libvlc_exception_init( &ex );
572 libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
573 NSMutableArray *tempArray = [NSMutableArray array];
575 for (i = 0; i < [self countOfTitles] ; i++)
577 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
578 tracks = freeAndGetNextTrack(tracks);
580 return [NSArray arrayWithArray: tempArray];
584 #pragma mark Audio tracks
585 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
587 libvlc_exception_t ex;
588 libvlc_exception_init( &ex );
589 libvlc_audio_set_track( instance, (int)value, &ex );
590 catch_exception( &ex );
593 - (NSUInteger)currentAudioTrackIndex
595 libvlc_exception_t ex;
596 libvlc_exception_init( &ex );
597 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
598 catch_exception( &ex );
602 NSUInteger result = libvlc_audio_get_track( instance, &ex );
603 catch_exception( &ex );
607 - (NSArray *)audioTracks
609 libvlc_exception_t ex;
610 libvlc_exception_init( &ex );
611 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
612 catch_exception( &ex );
614 return [NSArray array];
616 libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
617 NSMutableArray *tempArray = [NSMutableArray array];
619 for (i = 0; i < count ; i++)
621 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
622 tracks = freeAndGetNextTrack(tracks);
625 return [NSArray arrayWithArray: tempArray];
628 - (void)setAudioChannel:(NSInteger)value
630 libvlc_exception_t ex;
631 libvlc_exception_init( &ex );
632 libvlc_audio_set_channel( instance, value, &ex );
633 catch_exception( &ex );
636 - (NSInteger)audioChannel
638 libvlc_exception_t ex;
639 libvlc_exception_init( &ex );
640 NSInteger result = libvlc_audio_get_channel( instance, &ex );
641 catch_exception( &ex );
645 - (void)setMedia:(VLCMedia *)value
649 if (media && [media compare:value] == NSOrderedSame)
653 media = [value retain];
655 libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
666 libvlc_exception_t ex;
667 libvlc_exception_init( &ex );
668 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
669 catch_exception( &ex );
675 if( [NSThread isMainThread] )
677 /* Hack because we create a dead lock here, when the vout is stopped
678 * and tries to recontact us on the main thread */
679 /* FIXME: to do this properly we need to do some locking. We may want
680 * to move that to libvlc */
681 [self performSelectorInBackground:@selector(pause) withObject:nil];
686 libvlc_exception_t ex;
687 libvlc_exception_init(&ex);
688 libvlc_media_player_pause(instance, &ex);
691 // in most cases, it's just EOF so let's stop
692 if (libvlc_exception_raised(&ex))
695 libvlc_exception_clear(&ex);
700 libvlc_media_player_stop(instance);
703 - (void)gotoNextFrame
705 libvlc_exception_t e;
706 libvlc_exception_init(&e);
707 libvlc_media_player_next_frame(instance, &e);
714 [self fastForwardAtRate: 2.0];
717 - (void)fastForwardAtRate:(float)rate
724 [self rewindAtRate: 2.0];
727 - (void)rewindAtRate:(float)rate
729 [self setRate: -rate];
732 - (void)jumpBackward:(NSInteger)interval
734 if( [self isSeekable] )
736 interval = interval * 1000;
737 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
741 - (void)jumpForward:(NSInteger)interval
743 if( [self isSeekable] )
745 interval = interval * 1000;
746 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
750 - (void)extraShortJumpBackward
752 [self jumpBackward:3];
755 - (void)extraShortJumpForward
757 [self jumpForward:3];
760 - (void)shortJumpBackward
762 [self jumpBackward:10];
765 - (void)shortJumpForward
767 [self jumpForward:10];
770 - (void)mediumJumpBackward
772 [self jumpBackward:60];
775 - (void)mediumJumpForward
777 [self jumpForward:60];
780 - (void)longJumpBackward
782 [self jumpBackward:300];
785 - (void)longJumpForward
787 [self jumpForward:300];
790 + (NSSet *)keyPathsForValuesAffectingIsPlaying
792 return [NSSet setWithObjects:@"state", nil];
797 VLCMediaPlayerState state = [self state];
798 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
799 (state == VLCMediaPlayerStatePlaying));
804 libvlc_exception_t ex;
805 libvlc_exception_init( &ex );
806 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
807 if (libvlc_exception_raised(&ex))
809 libvlc_exception_clear(&ex);
816 static const VLCMediaPlayerState libvlc_to_local_state[] =
818 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
819 [libvlc_Opening] = VLCMediaPlayerStateOpening,
820 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
821 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
822 [libvlc_Paused] = VLCMediaPlayerStatePaused,
823 [libvlc_Ended] = VLCMediaPlayerStateEnded,
824 [libvlc_Error] = VLCMediaPlayerStateError
827 - (VLCMediaPlayerState)state
837 - (void)setPosition:(float)newPosition
839 libvlc_exception_t ex;
840 libvlc_exception_init( &ex );
841 libvlc_media_player_set_position( instance, newPosition, &ex );
842 catch_exception( &ex );
847 libvlc_exception_t ex;
848 libvlc_exception_init( &ex );
849 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
850 catch_exception( &ex );
856 libvlc_exception_t ex;
857 libvlc_exception_init( &ex );
858 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
859 catch_exception( &ex );
863 - (void *)libVLCMediaPlayer
869 @implementation VLCMediaPlayer (Private)
870 - (id)initWithDrawable:(id)aDrawable
872 if (self = [super init])
876 cachedTime = [[VLCTime nullTime] retain];
877 cachedRemainingTime = [[VLCTime nullTime] retain];
879 cachedState = VLCMediaPlayerStateStopped;
881 // Create a media instance, it doesn't matter what library we start off with
882 // it will change depending on the media descriptor provided to the media
884 libvlc_exception_t ex;
885 libvlc_exception_init( &ex );
886 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
887 catch_exception( &ex );
889 [self registerObservers];
891 [self setDrawable:aDrawable];
896 - (void)registerObservers
898 // Attach event observers into the media instance
899 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
900 libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
901 libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
902 libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
903 libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
904 /* FIXME: We may want to turn that off when none is interested by that */
905 libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
906 libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
907 libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
910 - (void)unregisterObservers
912 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
913 libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
914 libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
915 libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
916 libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
917 libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
918 libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
919 libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
922 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
924 [self willChangeValueForKey:@"time"];
925 [self willChangeValueForKey:@"remainingTime"];
926 [cachedTime release];
927 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
928 [cachedRemainingTime release];
929 double currentTime = [[cachedTime numberValue] doubleValue];
930 double remaining = currentTime / position * (1 - position);
931 cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
932 [self didChangeValueForKey:@"remainingTime"];
933 [self didChangeValueForKey:@"time"];
938 UpdateSystemActivity(UsrActivity);
941 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
943 // This seems to be the most relevant place to delay sleeping and screen saver.
946 [self willChangeValueForKey:@"position"];
947 position = [newPosition floatValue];
948 [self didChangeValueForKey:@"position"];
951 - (void)mediaPlayerStateChanged:(NSNumber *)newState
953 [self willChangeValueForKey:@"state"];
954 cachedState = [newState intValue];
955 [self didChangeValueForKey:@"state"];
958 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
960 [self willChangeValueForKey:@"media"];
961 if (media != newMedia)
964 media = [newMedia retain];
966 [self didChangeValueForKey:@"media"];