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) == 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);
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_media_player_set_nsobject(instance, aDrawable);
260 libvlc_exception_t ex;
261 libvlc_exception_init( &ex );
262 id ret = libvlc_media_player_get_nsobject(instance);
263 catch_exception( &ex );
269 return [[VLCLibrary sharedLibrary] audio];
273 #pragma mark Subtitles
275 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
277 libvlc_exception_t ex;
278 libvlc_exception_init( &ex );
279 libvlc_video_set_spu( instance, (int)index, &ex );
280 catch_exception( &ex );
283 - (NSUInteger)currentVideoSubTitleIndex
285 libvlc_exception_t ex;
286 libvlc_exception_init( &ex );
287 NSInteger count = libvlc_video_get_spu_count( instance, &ex );
288 if (libvlc_exception_raised( &ex ))
290 libvlc_exception_clear( &ex );
295 NSUInteger result = libvlc_video_get_spu( instance, &ex );
296 if (libvlc_exception_raised( &ex ))
298 libvlc_exception_clear( &ex );
305 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
307 libvlc_exception_t ex;
308 libvlc_exception_init( &ex );
309 BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
310 catch_exception( &ex );
314 - (NSArray *)videoSubTitles
316 libvlc_exception_t ex;
317 libvlc_exception_init( &ex );
318 libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description( instance, &ex );
319 catch_exception( &ex );
321 NSMutableArray *tempArray = [NSMutableArray array];
322 while (currentTrack) {
323 [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
324 free(currentTrack->psz_name);
325 libvlc_track_description_t *tofree = currentTrack;
326 currentTrack = currentTrack->p_next;
329 return [NSArray arrayWithArray: tempArray];
334 #pragma mark Video Crop geometry
336 - (void)setVideoCropGeometry:(char *)value
338 libvlc_exception_t ex;
339 libvlc_exception_init( &ex );
340 libvlc_video_set_crop_geometry( instance, value, &ex );
341 catch_exception( &ex );
344 - (char *)videoCropGeometry
346 libvlc_exception_t ex;
347 libvlc_exception_init( &ex );
348 char * result = libvlc_video_get_crop_geometry( instance, &ex );
349 catch_exception( &ex );
353 - (void)setVideoAspectRatio:(char *)value
355 libvlc_exception_t ex;
356 libvlc_exception_init( &ex );
357 libvlc_video_set_aspect_ratio( instance, value, &ex );
358 catch_exception( &ex );
361 - (char *)videoAspectRatio
363 libvlc_exception_t ex;
364 libvlc_exception_init( &ex );
365 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
366 catch_exception( &ex );
370 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
372 libvlc_exception_t ex;
373 libvlc_exception_init( &ex );
374 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
375 catch_exception( &ex );
378 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
380 libvlc_exception_t ex;
381 libvlc_exception_init( &ex );
382 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
383 catch_exception( &ex );
386 - (void)setRate:(float)value
388 libvlc_exception_t ex;
389 libvlc_exception_init( &ex );
390 libvlc_media_player_set_rate( instance, value, &ex );
391 catch_exception( &ex );
396 libvlc_exception_t ex;
397 libvlc_exception_init( &ex );
398 float result = libvlc_media_player_get_rate( instance, &ex );
399 if (libvlc_exception_raised(&ex))
402 libvlc_exception_clear(&ex);
409 libvlc_exception_t ex;
410 libvlc_exception_init( &ex );
411 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
412 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
413 catch_exception( &ex );
419 libvlc_exception_t ex;
420 libvlc_exception_init( &ex );
421 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
422 if (libvlc_exception_raised( &ex ))
424 libvlc_exception_clear( &ex );
431 - (float)framesPerSecond
433 libvlc_exception_t ex;
434 libvlc_exception_init( &ex );
435 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
436 catch_exception( &ex );
440 - (void)setTime:(VLCTime *)value
442 libvlc_exception_t ex;
443 libvlc_exception_init( &ex );
444 // Time is managed in seconds, while duration is managed in microseconds
445 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
446 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
447 (value ? [[value numberValue] longLongValue] : 0),
449 catch_exception( &ex );
457 - (VLCTime *)remainingTime
459 return cachedRemainingTime;
464 libvlc_exception_t ex;
465 libvlc_exception_init( &ex );
466 NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
467 catch_exception( &ex );
472 #pragma mark Chapters
473 - (void)setCurrentChapterIndex:(NSUInteger)value;
475 libvlc_exception_t ex;
476 libvlc_exception_init( &ex );
477 libvlc_media_player_set_chapter( instance, value, &ex );
478 catch_exception( &ex );
481 - (NSUInteger)currentChapterIndex
483 libvlc_exception_t ex;
484 libvlc_exception_init( &ex );
485 NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
486 catch_exception( &ex );
489 NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
490 catch_exception( &ex );
496 libvlc_exception_t ex;
497 libvlc_exception_init( &ex );
498 libvlc_media_player_next_chapter( instance, &ex );
499 catch_exception( &ex );
502 - (void)previousChapter
504 libvlc_exception_t ex;
505 libvlc_exception_init( &ex );
506 libvlc_media_player_previous_chapter( instance, &ex );
507 catch_exception( &ex );
510 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
512 libvlc_exception_t ex;
513 libvlc_exception_init( &ex );
514 NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
516 return [NSArray array];
518 libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
519 NSMutableArray *tempArray = [NSMutableArray array];
521 for (i = 0; i < count ; i++)
523 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
524 tracks = tracks->p_next;
526 return [NSArray arrayWithArray: tempArray];
532 - (void)setCurrentTitleIndex:(NSUInteger)value
534 libvlc_exception_t ex;
535 libvlc_exception_init( &ex );
536 libvlc_media_player_set_title( instance, value, &ex );
537 catch_exception( &ex );
540 - (NSUInteger)currentTitleIndex
542 libvlc_exception_t ex;
543 libvlc_exception_init( &ex );
545 NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
546 catch_exception( &ex );
550 NSUInteger result = libvlc_media_player_get_title( instance, &ex );
551 catch_exception( &ex );
555 - (NSUInteger)countOfTitles
557 libvlc_exception_t ex;
558 libvlc_exception_init( &ex );
559 NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
560 catch_exception( &ex );
566 libvlc_exception_t ex;
567 libvlc_exception_init( &ex );
568 libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
569 NSMutableArray *tempArray = [NSMutableArray array];
571 for (i = 0; i < [self countOfTitles] ; i++)
573 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
574 tracks = tracks->p_next;
576 return [NSArray arrayWithArray: tempArray];
580 #pragma mark Audio tracks
581 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
583 libvlc_exception_t ex;
584 libvlc_exception_init( &ex );
585 libvlc_audio_set_track( instance, (int)value, &ex );
586 catch_exception( &ex );
589 - (NSUInteger)currentAudioTrackIndex
591 libvlc_exception_t ex;
592 libvlc_exception_init( &ex );
593 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
594 catch_exception( &ex );
598 NSUInteger result = libvlc_audio_get_track( instance, &ex );
599 catch_exception( &ex );
603 - (NSArray *)audioTracks
605 libvlc_exception_t ex;
606 libvlc_exception_init( &ex );
607 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
608 catch_exception( &ex );
610 return [NSArray array];
612 libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
613 NSMutableArray *tempArray = [NSMutableArray array];
615 for (i = 0; i < count ; i++)
617 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
618 tracks = tracks->p_next;
621 return [NSArray arrayWithArray: tempArray];
624 - (void)setAudioChannel:(NSInteger)value
626 libvlc_exception_t ex;
627 libvlc_exception_init( &ex );
628 libvlc_audio_set_channel( instance, value, &ex );
629 catch_exception( &ex );
632 - (NSInteger)audioChannel
634 libvlc_exception_t ex;
635 libvlc_exception_init( &ex );
636 NSInteger result = libvlc_audio_get_channel( instance, &ex );
637 catch_exception( &ex );
641 - (void)setMedia:(VLCMedia *)value
645 if (media && [media compare:value] == NSOrderedSame)
649 media = [value retain];
651 libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
662 libvlc_exception_t ex;
663 libvlc_exception_init( &ex );
664 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
665 catch_exception( &ex );
671 if( [NSThread isMainThread] )
673 /* Hack because we create a dead lock here, when the vout is stopped
674 * and tries to recontact us on the main thread */
675 /* FIXME: to do this properly we need to do some locking. We may want
676 * to move that to libvlc */
677 [self performSelectorInBackground:@selector(pause) withObject:nil];
682 libvlc_exception_t ex;
683 libvlc_exception_init(&ex);
684 libvlc_media_player_pause(instance, &ex);
687 // in most cases, it's just EOF so let's stop
688 if (libvlc_exception_raised(&ex))
691 libvlc_exception_clear(&ex);
696 libvlc_media_player_stop(instance);
699 - (void)gotoNextFrame
701 libvlc_exception_t e;
702 libvlc_exception_init(&e);
703 libvlc_media_player_next_frame(instance, &e);
710 [self fastForwardAtRate: 2.0];
713 - (void)fastForwardAtRate:(float)rate
720 [self rewindAtRate: 2.0];
723 - (void)rewindAtRate:(float)rate
725 [self setRate: -rate];
728 - (void)jumpBackward:(NSInteger)interval
730 if( [self isSeekable] )
732 interval = interval * 1000;
733 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
737 - (void)jumpForward:(NSInteger)interval
739 if( [self isSeekable] )
741 interval = interval * 1000;
742 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
746 - (void)extraShortJumpBackward
748 [self jumpBackward:3];
751 - (void)extraShortJumpForward
753 [self jumpForward:3];
756 - (void)shortJumpBackward
758 [self jumpBackward:10];
761 - (void)shortJumpForward
763 [self jumpForward:10];
766 - (void)mediumJumpBackward
768 [self jumpBackward:60];
771 - (void)mediumJumpForward
773 [self jumpForward:60];
776 - (void)longJumpBackward
778 [self jumpBackward:300];
781 - (void)longJumpForward
783 [self jumpForward:300];
786 + (NSSet *)keyPathsForValuesAffectingIsPlaying
788 return [NSSet setWithObjects:@"state", nil];
793 VLCMediaPlayerState state = [self state];
794 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
795 (state == VLCMediaPlayerStatePlaying));
800 libvlc_exception_t ex;
801 libvlc_exception_init( &ex );
802 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
803 if (libvlc_exception_raised(&ex))
805 libvlc_exception_clear(&ex);
812 static const VLCMediaPlayerState libvlc_to_local_state[] =
814 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
815 [libvlc_Opening] = VLCMediaPlayerStateOpening,
816 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
817 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
818 [libvlc_Paused] = VLCMediaPlayerStatePaused,
819 [libvlc_Ended] = VLCMediaPlayerStateEnded,
820 [libvlc_Error] = VLCMediaPlayerStateError
823 - (VLCMediaPlayerState)state
833 - (void)setPosition:(float)newPosition
835 libvlc_exception_t ex;
836 libvlc_exception_init( &ex );
837 libvlc_media_player_set_position( instance, newPosition, &ex );
838 catch_exception( &ex );
843 libvlc_exception_t ex;
844 libvlc_exception_init( &ex );
845 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
846 catch_exception( &ex );
852 libvlc_exception_t ex;
853 libvlc_exception_init( &ex );
854 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
855 catch_exception( &ex );
859 - (void *)libVLCMediaPlayer
865 @implementation VLCMediaPlayer (Private)
866 - (id)initWithDrawable:(id)aDrawable
868 if (self = [super init])
872 cachedTime = [[VLCTime nullTime] retain];
873 cachedRemainingTime = [[VLCTime nullTime] retain];
875 cachedState = VLCMediaPlayerStateStopped;
877 // Create a media instance, it doesn't matter what library we start off with
878 // it will change depending on the media descriptor provided to the media
880 libvlc_exception_t ex;
881 libvlc_exception_init( &ex );
882 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
883 catch_exception( &ex );
885 [self registerObservers];
887 [self setDrawable:aDrawable];
892 - (void)registerObservers
894 // Attach event observers into the media instance
895 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
896 libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
897 libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
898 libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
899 libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
900 /* FIXME: We may want to turn that off when none is interested by that */
901 libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
902 libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
903 libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
906 - (void)unregisterObservers
908 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
909 libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
910 libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
911 libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
912 libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
913 libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
914 libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
915 libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
918 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
920 [self willChangeValueForKey:@"time"];
921 [self willChangeValueForKey:@"remainingTime"];
922 [cachedTime release];
923 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
924 [cachedRemainingTime release];
925 double currentTime = [[cachedTime numberValue] doubleValue];
926 double remaining = currentTime / position * (1 - position);
927 cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
928 [self didChangeValueForKey:@"remainingTime"];
929 [self didChangeValueForKey:@"time"];
934 UpdateSystemActivity(UsrActivity);
937 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
939 // This seems to be the most relevant place to delay sleeping and screen saver.
942 [self willChangeValueForKey:@"position"];
943 position = [newPosition floatValue];
944 [self didChangeValueForKey:@"position"];
947 - (void)mediaPlayerStateChanged:(NSNumber *)newState
949 [self willChangeValueForKey:@"state"];
950 cachedState = [newState intValue];
951 [self didChangeValueForKey:@"state"];
954 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
956 [self willChangeValueForKey:@"media"];
957 if (media != newMedia)
960 media = [newMedia retain];
962 [self didChangeValueForKey:@"media"];