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 free(track->psz_name);
72 /* libvlc event callback */
73 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
75 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
76 withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
77 withNotificationName:VLCMediaPlayerVolumeChanged];
80 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
82 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
83 [[VLCEventManager sharedManager] callOnMainThreadObject:self
84 withMethod:@selector(mediaPlayerTimeChanged:)
85 withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
87 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
88 withDelegateMethod:@selector(mediaPlayerTimeChanged:)
89 withNotificationName:VLCMediaPlayerTimeChanged];
93 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
95 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
97 [[VLCEventManager sharedManager] callOnMainThreadObject:self
98 withMethod:@selector(mediaPlayerPositionChanged:)
99 withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
103 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
105 VLCMediaPlayerState newState;
107 if( event->type == libvlc_MediaPlayerPlaying )
108 newState = VLCMediaPlayerStatePlaying;
109 else if( event->type == libvlc_MediaPlayerPaused )
110 newState = VLCMediaPlayerStatePaused;
111 else if( event->type == libvlc_MediaPlayerEndReached )
112 newState = VLCMediaPlayerStateStopped;
113 else if( event->type == libvlc_MediaPlayerEncounteredError )
114 newState = VLCMediaPlayerStateError;
117 NSLog(@"%s: Unknown event", __FUNCTION__);
121 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
123 [[VLCEventManager sharedManager] callOnMainThreadObject:self
124 withMethod:@selector(mediaPlayerStateChanged:)
125 withArgumentAsObject:[NSNumber numberWithInt:newState]];
127 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
128 withDelegateMethod:@selector(mediaPlayerStateChanged:)
129 withNotificationName:VLCMediaPlayerStateChanged];
135 static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
137 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
139 [[VLCEventManager sharedManager] callOnMainThreadObject:self
140 withMethod:@selector(mediaPlayerMediaChanged:)
141 withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
148 // TODO: Documentation
149 @interface VLCMediaPlayer (Private)
150 - (id)initWithDrawable:(id)aDrawable;
152 - (void)registerObservers;
153 - (void)unregisterObservers;
154 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
155 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
156 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
157 - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
160 @implementation VLCMediaPlayer
163 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
165 static NSDictionary * dict = nil;
166 NSSet * superKeyPaths;
169 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
170 [NSSet setWithObject:@"state"], @"playing",
171 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
172 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
173 [NSSet setWithObjects:@"state", @"media", nil], @"description",
176 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
178 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
179 [ret unionSet:superKeyPaths];
182 return [dict objectForKey: key];
188 return [self initWithDrawable:nil];
191 - (id)initWithVideoView:(VLCVideoView *)aVideoView
193 return [self initWithDrawable: aVideoView];
196 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
198 return [self initWithDrawable: aVideoLayer];
205 if([self retainCount] <= 1)
207 /* We must make sure we won't receive new event after an upcoming dealloc
208 * We also may receive a -retain in some event callback that may occcur
209 * Before libvlc_event_detach. So this can't happen in dealloc */
210 [self unregisterObservers];
218 NSAssert(libvlc_media_player_get_state(instance) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
220 // Always get rid of the delegate first so we can stop sending messages to it
221 // TODO: Should we tell the delegate that we're shutting down?
224 // Clear our drawable as we are going to release it, we don't
225 // want the core to use it from this point. This won't happen as
226 // the media player must be stopped.
227 libvlc_media_player_set_nsobject(instance, nil);
229 libvlc_media_player_release(instance);
231 // Get rid of everything else
233 [cachedTime release];
234 [cachedRemainingTime release];
240 - (void)setDelegate:(id)value
250 - (void)setVideoView:(VLCVideoView *)aVideoView
252 [self setDrawable: aVideoView];
255 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
257 [self setDrawable: aVideoLayer];
260 - (void)setDrawable:(id)aDrawable
262 // Make sure that this instance has been associated with the drawing canvas.
263 libvlc_media_player_set_nsobject(instance, aDrawable);
268 libvlc_exception_t ex;
269 libvlc_exception_init( &ex );
270 id ret = libvlc_media_player_get_nsobject(instance);
271 catch_exception( &ex );
277 return [[VLCLibrary sharedLibrary] audio];
281 #pragma mark Subtitles
283 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
285 libvlc_exception_t ex;
286 libvlc_exception_init( &ex );
287 libvlc_video_set_spu( instance, (int)index, &ex );
288 catch_exception( &ex );
291 - (NSUInteger)currentVideoSubTitleIndex
293 libvlc_exception_t ex;
294 libvlc_exception_init( &ex );
295 NSInteger count = libvlc_video_get_spu_count( instance, &ex );
296 if (libvlc_exception_raised( &ex ))
298 libvlc_exception_clear( &ex );
303 NSUInteger result = libvlc_video_get_spu( instance, &ex );
304 if (libvlc_exception_raised( &ex ))
306 libvlc_exception_clear( &ex );
313 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
315 libvlc_exception_t ex;
316 libvlc_exception_init( &ex );
317 BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
318 catch_exception( &ex );
322 - (NSArray *)videoSubTitles
324 libvlc_exception_t ex;
325 libvlc_exception_init( &ex );
326 libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description( instance, &ex );
327 catch_exception( &ex );
329 NSMutableArray *tempArray = [NSMutableArray array];
330 while (currentTrack) {
331 [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
332 currentTrack = freeAndGetNextTrack(currentTrack);
334 return [NSArray arrayWithArray: tempArray];
339 #pragma mark Video Crop geometry
341 - (void)setVideoCropGeometry:(char *)value
343 libvlc_exception_t ex;
344 libvlc_exception_init( &ex );
345 libvlc_video_set_crop_geometry( instance, value, &ex );
346 catch_exception( &ex );
349 - (char *)videoCropGeometry
351 libvlc_exception_t ex;
352 libvlc_exception_init( &ex );
353 char * result = libvlc_video_get_crop_geometry( instance, &ex );
354 catch_exception( &ex );
358 - (void)setVideoAspectRatio:(char *)value
360 libvlc_exception_t ex;
361 libvlc_exception_init( &ex );
362 libvlc_video_set_aspect_ratio( instance, value, &ex );
363 catch_exception( &ex );
366 - (char *)videoAspectRatio
368 libvlc_exception_t ex;
369 libvlc_exception_init( &ex );
370 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
371 catch_exception( &ex );
375 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
377 libvlc_exception_t ex;
378 libvlc_exception_init( &ex );
379 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
380 catch_exception( &ex );
383 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
385 libvlc_exception_t ex;
386 libvlc_exception_init( &ex );
387 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
388 catch_exception( &ex );
391 - (void)setRate:(float)value
393 libvlc_exception_t ex;
394 libvlc_exception_init( &ex );
395 libvlc_media_player_set_rate( instance, value, &ex );
396 catch_exception( &ex );
401 libvlc_exception_t ex;
402 libvlc_exception_init( &ex );
403 float result = libvlc_media_player_get_rate( instance, &ex );
404 if (libvlc_exception_raised(&ex))
407 libvlc_exception_clear(&ex);
414 libvlc_exception_t ex;
415 libvlc_exception_init( &ex );
416 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
417 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
418 catch_exception( &ex );
424 libvlc_exception_t ex;
425 libvlc_exception_init( &ex );
426 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
427 if (libvlc_exception_raised( &ex ))
429 libvlc_exception_clear( &ex );
436 - (float)framesPerSecond
438 libvlc_exception_t ex;
439 libvlc_exception_init( &ex );
440 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
441 catch_exception( &ex );
445 - (void)setTime:(VLCTime *)value
447 libvlc_exception_t ex;
448 libvlc_exception_init( &ex );
449 // Time is managed in seconds, while duration is managed in microseconds
450 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
451 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
452 (value ? [[value numberValue] longLongValue] : 0),
454 catch_exception( &ex );
462 - (VLCTime *)remainingTime
464 return cachedRemainingTime;
469 libvlc_exception_t ex;
470 libvlc_exception_init( &ex );
471 NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
472 catch_exception( &ex );
477 #pragma mark Chapters
478 - (void)setCurrentChapterIndex:(NSUInteger)value;
480 libvlc_exception_t ex;
481 libvlc_exception_init( &ex );
482 libvlc_media_player_set_chapter( instance, value, &ex );
483 catch_exception( &ex );
486 - (NSUInteger)currentChapterIndex
488 libvlc_exception_t ex;
489 libvlc_exception_init( &ex );
490 NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
491 catch_exception( &ex );
494 NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
495 catch_exception( &ex );
501 libvlc_exception_t ex;
502 libvlc_exception_init( &ex );
503 libvlc_media_player_next_chapter( instance, &ex );
504 catch_exception( &ex );
507 - (void)previousChapter
509 libvlc_exception_t ex;
510 libvlc_exception_init( &ex );
511 libvlc_media_player_previous_chapter( instance, &ex );
512 catch_exception( &ex );
515 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
517 libvlc_exception_t ex;
518 libvlc_exception_init( &ex );
519 NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
521 return [NSArray array];
523 libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
524 NSMutableArray *tempArray = [NSMutableArray array];
526 for (i = 0; i < count ; i++)
528 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
529 tracks = freeAndGetNextTrack(tracks);
531 return [NSArray arrayWithArray: tempArray];
537 - (void)setCurrentTitleIndex:(NSUInteger)value
539 libvlc_exception_t ex;
540 libvlc_exception_init( &ex );
541 libvlc_media_player_set_title( instance, value, &ex );
542 catch_exception( &ex );
545 - (NSUInteger)currentTitleIndex
547 libvlc_exception_t ex;
548 libvlc_exception_init( &ex );
550 NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
551 catch_exception( &ex );
555 NSUInteger result = libvlc_media_player_get_title( instance, &ex );
556 catch_exception( &ex );
560 - (NSUInteger)countOfTitles
562 libvlc_exception_t ex;
563 libvlc_exception_init( &ex );
564 NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
565 catch_exception( &ex );
571 libvlc_exception_t ex;
572 libvlc_exception_init( &ex );
573 libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
574 NSMutableArray *tempArray = [NSMutableArray array];
576 for (i = 0; i < [self countOfTitles] ; i++)
578 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
579 tracks = freeAndGetNextTrack(tracks);
581 return [NSArray arrayWithArray: tempArray];
585 #pragma mark Audio tracks
586 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
588 libvlc_exception_t ex;
589 libvlc_exception_init( &ex );
590 libvlc_audio_set_track( instance, (int)value, &ex );
591 catch_exception( &ex );
594 - (NSUInteger)currentAudioTrackIndex
596 libvlc_exception_t ex;
597 libvlc_exception_init( &ex );
598 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
599 catch_exception( &ex );
603 NSUInteger result = libvlc_audio_get_track( instance, &ex );
604 catch_exception( &ex );
608 - (NSArray *)audioTracks
610 libvlc_exception_t ex;
611 libvlc_exception_init( &ex );
612 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
613 catch_exception( &ex );
615 return [NSArray array];
617 libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
618 NSMutableArray *tempArray = [NSMutableArray array];
620 for (i = 0; i < count ; i++)
622 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
623 tracks = freeAndGetNextTrack(tracks);
626 return [NSArray arrayWithArray: tempArray];
629 - (void)setAudioChannel:(NSInteger)value
631 libvlc_exception_t ex;
632 libvlc_exception_init( &ex );
633 libvlc_audio_set_channel( instance, value, &ex );
634 catch_exception( &ex );
637 - (NSInteger)audioChannel
639 libvlc_exception_t ex;
640 libvlc_exception_init( &ex );
641 NSInteger result = libvlc_audio_get_channel( instance, &ex );
642 catch_exception( &ex );
646 - (void)setMedia:(VLCMedia *)value
650 if (media && [media compare:value] == NSOrderedSame)
654 media = [value retain];
656 libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
667 libvlc_exception_t ex;
668 libvlc_exception_init( &ex );
669 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
670 catch_exception( &ex );
676 if( [NSThread isMainThread] )
678 /* Hack because we create a dead lock here, when the vout is stopped
679 * and tries to recontact us on the main thread */
680 /* FIXME: to do this properly we need to do some locking. We may want
681 * to move that to libvlc */
682 [self performSelectorInBackground:@selector(pause) withObject:nil];
687 libvlc_exception_t ex;
688 libvlc_exception_init(&ex);
689 libvlc_media_player_pause(instance, &ex);
692 // in most cases, it's just EOF so let's stop
693 if (libvlc_exception_raised(&ex))
696 libvlc_exception_clear(&ex);
701 libvlc_media_player_stop(instance);
704 - (void)gotoNextFrame
706 libvlc_exception_t e;
707 libvlc_exception_init(&e);
708 libvlc_media_player_next_frame(instance, &e);
715 [self fastForwardAtRate: 2.0];
718 - (void)fastForwardAtRate:(float)rate
725 [self rewindAtRate: 2.0];
728 - (void)rewindAtRate:(float)rate
730 [self setRate: -rate];
733 - (void)jumpBackward:(NSInteger)interval
735 if( [self isSeekable] )
737 interval = interval * 1000;
738 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
742 - (void)jumpForward:(NSInteger)interval
744 if( [self isSeekable] )
746 interval = interval * 1000;
747 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
751 - (void)extraShortJumpBackward
753 [self jumpBackward:3];
756 - (void)extraShortJumpForward
758 [self jumpForward:3];
761 - (void)shortJumpBackward
763 [self jumpBackward:10];
766 - (void)shortJumpForward
768 [self jumpForward:10];
771 - (void)mediumJumpBackward
773 [self jumpBackward:60];
776 - (void)mediumJumpForward
778 [self jumpForward:60];
781 - (void)longJumpBackward
783 [self jumpBackward:300];
786 - (void)longJumpForward
788 [self jumpForward:300];
791 + (NSSet *)keyPathsForValuesAffectingIsPlaying
793 return [NSSet setWithObjects:@"state", nil];
798 VLCMediaPlayerState state = [self state];
799 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
800 (state == VLCMediaPlayerStatePlaying));
805 libvlc_exception_t ex;
806 libvlc_exception_init( &ex );
807 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
808 if (libvlc_exception_raised(&ex))
810 libvlc_exception_clear(&ex);
817 static const VLCMediaPlayerState libvlc_to_local_state[] =
819 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
820 [libvlc_Opening] = VLCMediaPlayerStateOpening,
821 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
822 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
823 [libvlc_Paused] = VLCMediaPlayerStatePaused,
824 [libvlc_Ended] = VLCMediaPlayerStateEnded,
825 [libvlc_Error] = VLCMediaPlayerStateError
828 - (VLCMediaPlayerState)state
838 - (void)setPosition:(float)newPosition
840 libvlc_exception_t ex;
841 libvlc_exception_init( &ex );
842 libvlc_media_player_set_position( instance, newPosition, &ex );
843 catch_exception( &ex );
848 libvlc_exception_t ex;
849 libvlc_exception_init( &ex );
850 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
851 catch_exception( &ex );
857 libvlc_exception_t ex;
858 libvlc_exception_init( &ex );
859 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
860 catch_exception( &ex );
864 - (void *)libVLCMediaPlayer
870 @implementation VLCMediaPlayer (Private)
871 - (id)initWithDrawable:(id)aDrawable
873 if (self = [super init])
877 cachedTime = [[VLCTime nullTime] retain];
878 cachedRemainingTime = [[VLCTime nullTime] retain];
880 cachedState = VLCMediaPlayerStateStopped;
882 // Create a media instance, it doesn't matter what library we start off with
883 // it will change depending on the media descriptor provided to the media
885 libvlc_exception_t ex;
886 libvlc_exception_init( &ex );
887 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
888 catch_exception( &ex );
890 [self registerObservers];
892 [self setDrawable:aDrawable];
897 - (void)registerObservers
899 // Attach event observers into the media instance
900 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
901 libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
902 libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
903 libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
904 libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
905 /* FIXME: We may want to turn that off when none is interested by that */
906 libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
907 libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
908 libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
911 - (void)unregisterObservers
913 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
914 libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
915 libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
916 libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
917 libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
918 libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
919 libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
920 libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
923 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
925 [self willChangeValueForKey:@"time"];
926 [self willChangeValueForKey:@"remainingTime"];
927 [cachedTime release];
928 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
929 [cachedRemainingTime release];
930 double currentTime = [[cachedTime numberValue] doubleValue];
931 double remaining = currentTime / position * (1 - position);
932 cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
933 [self didChangeValueForKey:@"remainingTime"];
934 [self didChangeValueForKey:@"time"];
939 UpdateSystemActivity(UsrActivity);
942 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
944 // This seems to be the most relevant place to delay sleeping and screen saver.
947 [self willChangeValueForKey:@"position"];
948 position = [newPosition floatValue];
949 [self didChangeValueForKey:@"position"];
952 - (void)mediaPlayerStateChanged:(NSNumber *)newState
954 [self willChangeValueForKey:@"state"];
955 cachedState = [newState intValue];
956 [self didChangeValueForKey:@"state"];
959 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
961 [self willChangeValueForKey:@"media"];
962 if (media != newMedia)
965 media = [newMedia retain];
967 [self didChangeValueForKey:@"media"];