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 catch_exception( &ex );
294 NSUInteger result = libvlc_video_get_spu( instance, &ex );
295 catch_exception( &ex );
299 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
301 libvlc_exception_t ex;
302 libvlc_exception_init( &ex );
303 BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
304 catch_exception( &ex );
308 - (NSArray *)videoSubTitles
310 libvlc_exception_t ex;
311 libvlc_exception_init( &ex );
312 NSInteger count = libvlc_video_get_spu_count( instance, &ex );
313 catch_exception( &ex );
315 libvlc_track_description_t *tracks = libvlc_video_get_spu_description( instance, &ex );
316 NSMutableArray *tempArray = [NSMutableArray array];
318 for (i = 0; i < count; i++)
320 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
321 tracks = tracks->p_next;
323 return [NSArray arrayWithArray: tempArray];
328 #pragma mark Video Crop geometry
330 - (void)setVideoCropGeometry:(char *)value
332 libvlc_exception_t ex;
333 libvlc_exception_init( &ex );
334 libvlc_video_set_crop_geometry( instance, value, &ex );
335 catch_exception( &ex );
338 - (char *)videoCropGeometry
340 libvlc_exception_t ex;
341 libvlc_exception_init( &ex );
342 char * result = libvlc_video_get_crop_geometry( instance, &ex );
343 catch_exception( &ex );
347 - (void)setVideoAspectRatio:(char *)value
349 libvlc_exception_t ex;
350 libvlc_exception_init( &ex );
351 libvlc_video_set_aspect_ratio( instance, value, &ex );
352 catch_exception( &ex );
355 - (char *)videoAspectRatio
357 libvlc_exception_t ex;
358 libvlc_exception_init( &ex );
359 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
360 catch_exception( &ex );
364 - (void)setVideoTeleText:(NSUInteger)value
366 libvlc_exception_t ex;
367 libvlc_exception_init( &ex );
368 libvlc_video_set_teletext( instance, value, &ex );
369 catch_exception( &ex );
372 - (NSUInteger)videoTeleText
374 libvlc_exception_t ex;
375 libvlc_exception_init( &ex );
376 NSInteger result = libvlc_video_get_teletext( instance, &ex );
377 catch_exception( &ex );
381 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
383 libvlc_exception_t ex;
384 libvlc_exception_init( &ex );
385 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
386 catch_exception( &ex );
389 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
391 libvlc_exception_t ex;
392 libvlc_exception_init( &ex );
393 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
394 catch_exception( &ex );
397 - (void)setRate:(float)value
399 libvlc_exception_t ex;
400 libvlc_exception_init( &ex );
401 libvlc_media_player_set_rate( instance, value, &ex );
402 catch_exception( &ex );
407 libvlc_exception_t ex;
408 libvlc_exception_init( &ex );
409 float result = libvlc_media_player_get_rate( instance, &ex );
410 catch_exception( &ex );
416 libvlc_exception_t ex;
417 libvlc_exception_init( &ex );
418 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
419 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
420 catch_exception( &ex );
426 libvlc_exception_t ex;
427 libvlc_exception_init( &ex );
428 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
429 if (libvlc_exception_raised( &ex ))
431 libvlc_exception_clear( &ex );
438 - (float)framesPerSecond
440 libvlc_exception_t ex;
441 libvlc_exception_init( &ex );
442 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
443 catch_exception( &ex );
447 - (void)setTime:(VLCTime *)value
449 libvlc_exception_t ex;
450 libvlc_exception_init( &ex );
451 // Time is managed in seconds, while duration is managed in microseconds
452 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
453 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
454 (value ? [[value numberValue] longLongValue] : 0),
456 catch_exception( &ex );
464 - (VLCTime *)remainingTime
466 return cachedRemainingTime;
471 libvlc_exception_t ex;
472 libvlc_exception_init( &ex );
473 NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
474 catch_exception( &ex );
479 #pragma mark Chapters
480 - (void)setCurrentChapterIndex:(NSUInteger)value;
482 libvlc_exception_t ex;
483 libvlc_exception_init( &ex );
484 libvlc_media_player_set_chapter( instance, value, &ex );
485 catch_exception( &ex );
488 - (NSUInteger)currentChapterIndex
490 libvlc_exception_t ex;
491 libvlc_exception_init( &ex );
492 NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
493 catch_exception( &ex );
496 NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
497 catch_exception( &ex );
503 libvlc_exception_t ex;
504 libvlc_exception_init( &ex );
505 libvlc_media_player_next_chapter( instance, &ex );
506 catch_exception( &ex );
509 - (void)previousChapter
511 libvlc_exception_t ex;
512 libvlc_exception_init( &ex );
513 libvlc_media_player_previous_chapter( instance, &ex );
514 catch_exception( &ex );
517 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
519 libvlc_exception_t ex;
520 libvlc_exception_init( &ex );
521 NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
523 return [NSArray array];
525 libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
526 NSMutableArray *tempArray = [NSMutableArray array];
528 for (i = 0; i < count ; i++)
530 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
531 tracks = tracks->p_next;
533 return [NSArray arrayWithArray: tempArray];
539 - (void)setCurrentTitleIndex:(NSUInteger)value
541 libvlc_exception_t ex;
542 libvlc_exception_init( &ex );
543 libvlc_media_player_set_title( instance, value, &ex );
544 catch_exception( &ex );
547 - (NSUInteger)currentTitleIndex
549 libvlc_exception_t ex;
550 libvlc_exception_init( &ex );
552 NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
553 catch_exception( &ex );
557 NSUInteger result = libvlc_media_player_get_title( instance, &ex );
558 catch_exception( &ex );
562 - (NSUInteger)countOfTitles
564 libvlc_exception_t ex;
565 libvlc_exception_init( &ex );
566 NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
567 catch_exception( &ex );
573 libvlc_exception_t ex;
574 libvlc_exception_init( &ex );
575 libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
576 NSMutableArray *tempArray = [NSMutableArray array];
578 for (i = 0; i < [self countOfTitles] ; i++)
580 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
581 tracks = tracks->p_next;
583 return [NSArray arrayWithArray: tempArray];
587 #pragma mark Audio tracks
588 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
590 libvlc_exception_t ex;
591 libvlc_exception_init( &ex );
592 libvlc_audio_set_track( instance, (int)value, &ex );
593 catch_exception( &ex );
596 - (NSUInteger)currentAudioTrackIndex
598 libvlc_exception_t ex;
599 libvlc_exception_init( &ex );
600 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
601 catch_exception( &ex );
605 NSUInteger result = libvlc_audio_get_track( instance, &ex );
606 catch_exception( &ex );
610 - (NSArray *)audioTracks
612 libvlc_exception_t ex;
613 libvlc_exception_init( &ex );
614 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
615 catch_exception( &ex );
617 return [NSArray array];
619 libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
620 NSMutableArray *tempArray = [NSMutableArray array];
622 for (i = 0; i < count ; i++)
624 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
625 tracks = tracks->p_next;
628 return [NSArray arrayWithArray: tempArray];
631 - (void)setAudioChannel:(NSInteger)value
633 libvlc_exception_t ex;
634 libvlc_exception_init( &ex );
635 libvlc_audio_set_channel( instance, value, &ex );
636 catch_exception( &ex );
639 - (NSInteger)audioChannel
641 libvlc_exception_t ex;
642 libvlc_exception_init( &ex );
643 NSInteger result = libvlc_audio_get_channel( instance, &ex );
644 catch_exception( &ex );
648 - (void)setMedia:(VLCMedia *)value
652 if (media && [media compare:value] == NSOrderedSame)
656 media = [value retain];
658 libvlc_exception_t ex;
659 libvlc_exception_init( &ex );
660 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
661 catch_exception( &ex );
672 libvlc_exception_t ex;
673 libvlc_exception_init( &ex );
674 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
675 catch_exception( &ex );
681 if( [NSThread isMainThread] )
683 /* Hack because we create a dead lock here, when the vout is stopped
684 * and tries to recontact us on the main thread */
685 /* FIXME: to do this properly we need to do some locking. We may want
686 * to move that to libvlc */
687 [self performSelectorInBackground:@selector(pause) withObject:nil];
692 libvlc_exception_t ex;
693 libvlc_exception_init( &ex );
694 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
697 // in most cases, it's just EOF so let's stop
698 if (libvlc_exception_raised(&ex))
701 libvlc_exception_clear(&ex);
706 libvlc_exception_t ex;
707 libvlc_exception_init( &ex );
708 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
709 catch_exception( &ex );
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 libvlc_exception_t ex;
899 libvlc_exception_init( &ex );
901 // Attach event observers into the media instance
902 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
903 libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
904 libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
905 libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
906 libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
907 /* FIXME: We may want to turn that off when none is interested by that */
908 libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
909 libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
910 libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, &ex );
911 catch_exception( &ex );
914 - (void)unregisterObservers
916 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
917 libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
918 libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
919 libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
920 libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
921 libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
922 libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
923 libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, NULL );
926 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
928 [self willChangeValueForKey:@"time"];
929 [self willChangeValueForKey:@"remainingTime"];
930 [cachedTime release];
931 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
932 [cachedRemainingTime release];
933 double currentTime = [[cachedTime numberValue] doubleValue];
934 double remaining = currentTime / position * (1 - position);
935 cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
936 [self didChangeValueForKey:@"remainingTime"];
937 [self didChangeValueForKey:@"time"];
942 UpdateSystemActivity(UsrActivity);
945 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
947 // This seems to be the most relevant place to delay sleeping and screen saver.
950 [self willChangeValueForKey:@"position"];
951 position = [newPosition floatValue];
952 [self didChangeValueForKey:@"position"];
955 - (void)mediaPlayerStateChanged:(NSNumber *)newState
957 [self willChangeValueForKey:@"state"];
958 cachedState = [newState intValue];
959 [self didChangeValueForKey:@"state"];
962 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
964 [self willChangeValueForKey:@"media"];
965 if (media != newMedia)
968 media = [newMedia retain];
970 [self didChangeValueForKey:@"media"];