1 /*****************************************************************************
2 * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
3 *****************************************************************************
4 * Copyright (C) 2007-2009 Pierre d'Herbemont
5 * Copyright (C) 2007-2009 the VideoLAN team
6 * Partial Copyright (C) 2009 Felix Paul Kühne
9 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10 * Faustion Osuna <enrique.osuna # gmail.com>
11 * Felix Paul Kühne <fkuehne # videolan.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #import "VLCLibrary.h"
29 #import "VLCMediaPlayer.h"
30 #import "VLCEventManager.h"
31 #import "VLCLibVLCBridging.h"
32 #import "VLCVideoView.h"
37 /* prevent system sleep */
38 #import <CoreServices/CoreServices.h>
39 /* FIXME: Ugly hack! */
41 #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
46 /* Notification Messages */
47 NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
48 NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
50 NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
52 static NSString * stateToStrings[] = {
53 [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
54 [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
55 [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
56 [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
57 [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
58 [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
59 [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
61 return stateToStrings[state];
64 /* libvlc event callback */
65 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
67 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
68 withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
69 withNotificationName:VLCMediaPlayerVolumeChanged];
72 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
74 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
75 [[VLCEventManager sharedManager] callOnMainThreadObject:self
76 withMethod:@selector(mediaPlayerTimeChanged:)
77 withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
79 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
80 withDelegateMethod:@selector(mediaPlayerTimeChanged:)
81 withNotificationName:VLCMediaPlayerTimeChanged];
85 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
87 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
89 [[VLCEventManager sharedManager] callOnMainThreadObject:self
90 withMethod:@selector(mediaPlayerPositionChanged:)
91 withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
95 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
97 VLCMediaPlayerState newState;
99 if( event->type == libvlc_MediaPlayerPlaying )
100 newState = VLCMediaPlayerStatePlaying;
101 else if( event->type == libvlc_MediaPlayerPaused )
102 newState = VLCMediaPlayerStatePaused;
103 else if( event->type == libvlc_MediaPlayerEndReached )
104 newState = VLCMediaPlayerStateStopped;
105 else if( event->type == libvlc_MediaPlayerEncounteredError )
106 newState = VLCMediaPlayerStateError;
109 NSLog(@"%s: Unknown event", __FUNCTION__);
113 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
115 [[VLCEventManager sharedManager] callOnMainThreadObject:self
116 withMethod:@selector(mediaPlayerStateChanged:)
117 withArgumentAsObject:[NSNumber numberWithInt:newState]];
119 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
120 withDelegateMethod:@selector(mediaPlayerStateChanged:)
121 withNotificationName:VLCMediaPlayerStateChanged];
127 static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
129 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
131 [[VLCEventManager sharedManager] callOnMainThreadObject:self
132 withMethod:@selector(mediaPlayerMediaChanged:)
133 withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
140 // TODO: Documentation
141 @interface VLCMediaPlayer (Private)
142 - (id)initWithDrawable:(id)aDrawable;
144 - (void)registerObservers;
145 - (void)unregisterObservers;
146 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
147 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
148 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
149 - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
152 @implementation VLCMediaPlayer
155 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
157 static NSDictionary * dict = nil;
158 NSSet * superKeyPaths;
161 dict = [[NSDictionary dictionaryWithObjectsAndKeys:
162 [NSSet setWithObject:@"state"], @"playing",
163 [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
164 [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
165 [NSSet setWithObjects:@"state", @"media", nil], @"description",
168 if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
170 NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
171 [ret unionSet:superKeyPaths];
174 return [dict objectForKey: key];
180 return [self initWithDrawable:nil];
183 - (id)initWithVideoView:(VLCVideoView *)aVideoView
185 return [self initWithDrawable: aVideoView];
188 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
190 return [self initWithDrawable: aVideoLayer];
197 if([self retainCount] <= 1)
199 /* We must make sure we won't receive new event after an upcoming dealloc
200 * We also may receive a -retain in some event callback that may occcur
201 * Before libvlc_event_detach. So this can't happen in dealloc */
202 [self unregisterObservers];
210 NSAssert(libvlc_media_player_get_state(instance, NULL) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
212 // Always get rid of the delegate first so we can stop sending messages to it
213 // TODO: Should we tell the delegate that we're shutting down?
216 // Clear our drawable as we are going to release it, we don't
217 // want the core to use it from this point. This won't happen as
218 // the media player must be stopped.
219 libvlc_media_player_set_nsobject(instance, nil, NULL);
221 libvlc_media_player_release(instance);
223 // Get rid of everything else
225 [cachedTime release];
226 [cachedRemainingTime release];
232 - (void)setDelegate:(id)value
242 - (void)setVideoView:(VLCVideoView *)aVideoView
244 [self setDrawable: aVideoView];
247 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
249 [self setDrawable: aVideoLayer];
252 - (void)setDrawable:(id)aDrawable
254 // Make sure that this instance has been associated with the drawing canvas.
255 libvlc_exception_t ex;
256 libvlc_exception_init( &ex );
257 libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
258 catch_exception( &ex );
263 libvlc_exception_t ex;
264 libvlc_exception_init( &ex );
265 id ret = libvlc_media_player_get_nsobject(instance);
266 catch_exception( &ex );
272 return [[VLCLibrary sharedLibrary] audio];
276 #pragma mark Subtitles
278 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
280 libvlc_exception_t ex;
281 libvlc_exception_init( &ex );
282 libvlc_video_set_spu( instance, (int)index, &ex );
283 catch_exception( &ex );
286 - (NSUInteger)currentVideoSubTitleIndex
288 libvlc_exception_t ex;
289 libvlc_exception_init( &ex );
290 NSInteger count = libvlc_video_get_spu_count( instance, &ex );
291 if (libvlc_exception_raised( &ex ))
293 libvlc_exception_clear( &ex );
298 NSUInteger result = libvlc_video_get_spu( instance, &ex );
299 if (libvlc_exception_raised( &ex ))
301 libvlc_exception_clear( &ex );
308 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
310 libvlc_exception_t ex;
311 libvlc_exception_init( &ex );
312 BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
313 catch_exception( &ex );
317 - (NSArray *)videoSubTitles
319 libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description( instance, &ex );
320 NSMutableArray *tempArray = [NSMutableArray array];
321 while (currentTrack) {
322 [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
323 free(currentTrack->psz_name);
324 libvlc_track_description_t *tofree = currentTrack;
325 currentTrack = currentTrack->p_next;
328 return [NSArray arrayWithArray: tempArray];
333 #pragma mark Video Crop geometry
335 - (void)setVideoCropGeometry:(char *)value
337 libvlc_exception_t ex;
338 libvlc_exception_init( &ex );
339 libvlc_video_set_crop_geometry( instance, value, &ex );
340 catch_exception( &ex );
343 - (char *)videoCropGeometry
345 libvlc_exception_t ex;
346 libvlc_exception_init( &ex );
347 char * result = libvlc_video_get_crop_geometry( instance, &ex );
348 catch_exception( &ex );
352 - (void)setVideoAspectRatio:(char *)value
354 libvlc_exception_t ex;
355 libvlc_exception_init( &ex );
356 libvlc_video_set_aspect_ratio( instance, value, &ex );
357 catch_exception( &ex );
360 - (char *)videoAspectRatio
362 libvlc_exception_t ex;
363 libvlc_exception_init( &ex );
364 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
365 catch_exception( &ex );
369 - (void)setVideoTeleText:(NSUInteger)value
371 libvlc_exception_t ex;
372 libvlc_exception_init( &ex );
373 libvlc_video_set_teletext( instance, value, &ex );
374 catch_exception( &ex );
377 - (NSUInteger)videoTeleText
379 libvlc_exception_t ex;
380 libvlc_exception_init( &ex );
381 NSInteger result = libvlc_video_get_teletext( instance, &ex );
382 catch_exception( &ex );
386 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
388 libvlc_exception_t ex;
389 libvlc_exception_init( &ex );
390 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
391 catch_exception( &ex );
394 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
396 libvlc_exception_t ex;
397 libvlc_exception_init( &ex );
398 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
399 catch_exception( &ex );
402 - (void)setRate:(float)value
404 libvlc_exception_t ex;
405 libvlc_exception_init( &ex );
406 libvlc_media_player_set_rate( instance, value, &ex );
407 catch_exception( &ex );
412 libvlc_exception_t ex;
413 libvlc_exception_init( &ex );
414 float result = libvlc_media_player_get_rate( instance, &ex );
415 catch_exception( &ex );
421 libvlc_exception_t ex;
422 libvlc_exception_init( &ex );
423 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
424 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
425 catch_exception( &ex );
431 libvlc_exception_t ex;
432 libvlc_exception_init( &ex );
433 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
434 if (libvlc_exception_raised( &ex ))
436 libvlc_exception_clear( &ex );
443 - (float)framesPerSecond
445 libvlc_exception_t ex;
446 libvlc_exception_init( &ex );
447 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
448 catch_exception( &ex );
452 - (void)setTime:(VLCTime *)value
454 libvlc_exception_t ex;
455 libvlc_exception_init( &ex );
456 // Time is managed in seconds, while duration is managed in microseconds
457 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
458 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
459 (value ? [[value numberValue] longLongValue] : 0),
461 catch_exception( &ex );
469 - (VLCTime *)remainingTime
471 return cachedRemainingTime;
476 libvlc_exception_t ex;
477 libvlc_exception_init( &ex );
478 NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
479 catch_exception( &ex );
484 #pragma mark Chapters
485 - (void)setCurrentChapterIndex:(NSUInteger)value;
487 libvlc_exception_t ex;
488 libvlc_exception_init( &ex );
489 libvlc_media_player_set_chapter( instance, value, &ex );
490 catch_exception( &ex );
493 - (NSUInteger)currentChapterIndex
495 libvlc_exception_t ex;
496 libvlc_exception_init( &ex );
497 NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
498 catch_exception( &ex );
501 NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
502 catch_exception( &ex );
508 libvlc_exception_t ex;
509 libvlc_exception_init( &ex );
510 libvlc_media_player_next_chapter( instance, &ex );
511 catch_exception( &ex );
514 - (void)previousChapter
516 libvlc_exception_t ex;
517 libvlc_exception_init( &ex );
518 libvlc_media_player_previous_chapter( instance, &ex );
519 catch_exception( &ex );
522 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
524 libvlc_exception_t ex;
525 libvlc_exception_init( &ex );
526 NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
528 return [NSArray array];
530 libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
531 NSMutableArray *tempArray = [NSMutableArray array];
533 for (i = 0; i < count ; i++)
535 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
536 tracks = tracks->p_next;
538 return [NSArray arrayWithArray: tempArray];
544 - (void)setCurrentTitleIndex:(NSUInteger)value
546 libvlc_exception_t ex;
547 libvlc_exception_init( &ex );
548 libvlc_media_player_set_title( instance, value, &ex );
549 catch_exception( &ex );
552 - (NSUInteger)currentTitleIndex
554 libvlc_exception_t ex;
555 libvlc_exception_init( &ex );
557 NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
558 catch_exception( &ex );
562 NSUInteger result = libvlc_media_player_get_title( instance, &ex );
563 catch_exception( &ex );
567 - (NSUInteger)countOfTitles
569 libvlc_exception_t ex;
570 libvlc_exception_init( &ex );
571 NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
572 catch_exception( &ex );
578 libvlc_exception_t ex;
579 libvlc_exception_init( &ex );
580 libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
581 NSMutableArray *tempArray = [NSMutableArray array];
583 for (i = 0; i < [self countOfTitles] ; i++)
585 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
586 tracks = tracks->p_next;
588 return [NSArray arrayWithArray: tempArray];
592 #pragma mark Audio tracks
593 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
595 libvlc_exception_t ex;
596 libvlc_exception_init( &ex );
597 libvlc_audio_set_track( instance, (int)value, &ex );
598 catch_exception( &ex );
601 - (NSUInteger)currentAudioTrackIndex
603 libvlc_exception_t ex;
604 libvlc_exception_init( &ex );
605 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
606 catch_exception( &ex );
610 NSUInteger result = libvlc_audio_get_track( instance, &ex );
611 catch_exception( &ex );
615 - (NSArray *)audioTracks
617 libvlc_exception_t ex;
618 libvlc_exception_init( &ex );
619 NSInteger count = libvlc_audio_get_track_count( instance, &ex );
620 catch_exception( &ex );
622 return [NSArray array];
624 libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
625 NSMutableArray *tempArray = [NSMutableArray array];
627 for (i = 0; i < count ; i++)
629 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
630 tracks = tracks->p_next;
633 return [NSArray arrayWithArray: tempArray];
636 - (void)setAudioChannel:(NSInteger)value
638 libvlc_exception_t ex;
639 libvlc_exception_init( &ex );
640 libvlc_audio_set_channel( instance, value, &ex );
641 catch_exception( &ex );
644 - (NSInteger)audioChannel
646 libvlc_exception_t ex;
647 libvlc_exception_init( &ex );
648 NSInteger result = libvlc_audio_get_channel( instance, &ex );
649 catch_exception( &ex );
653 - (void)setMedia:(VLCMedia *)value
657 if (media && [media compare:value] == NSOrderedSame)
661 media = [value retain];
663 libvlc_exception_t ex;
664 libvlc_exception_init( &ex );
665 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
666 catch_exception( &ex );
677 libvlc_exception_t ex;
678 libvlc_exception_init( &ex );
679 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
680 catch_exception( &ex );
686 if( [NSThread isMainThread] )
688 /* Hack because we create a dead lock here, when the vout is stopped
689 * and tries to recontact us on the main thread */
690 /* FIXME: to do this properly we need to do some locking. We may want
691 * to move that to libvlc */
692 [self performSelectorInBackground:@selector(pause) withObject:nil];
697 libvlc_exception_t ex;
698 libvlc_exception_init( &ex );
699 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
702 // in most cases, it's just EOF so let's stop
703 if (libvlc_exception_raised(&ex))
706 libvlc_exception_clear(&ex);
711 libvlc_exception_t ex;
712 libvlc_exception_init( &ex );
713 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
714 catch_exception( &ex );
719 [self fastForwardAtRate: 2.0];
722 - (void)fastForwardAtRate:(float)rate
729 [self rewindAtRate: 2.0];
732 - (void)rewindAtRate:(float)rate
734 [self setRate: -rate];
737 - (void)jumpBackward:(NSInteger)interval
739 if( [self isSeekable] )
741 interval = interval * 1000;
742 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
746 - (void)jumpForward:(NSInteger)interval
748 if( [self isSeekable] )
750 interval = interval * 1000;
751 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
755 - (void)extraShortJumpBackward
757 [self jumpBackward:3];
760 - (void)extraShortJumpForward
762 [self jumpForward:3];
765 - (void)shortJumpBackward
767 [self jumpBackward:10];
770 - (void)shortJumpForward
772 [self jumpForward:10];
775 - (void)mediumJumpBackward
777 [self jumpBackward:60];
780 - (void)mediumJumpForward
782 [self jumpForward:60];
785 - (void)longJumpBackward
787 [self jumpBackward:300];
790 - (void)longJumpForward
792 [self jumpForward:300];
795 + (NSSet *)keyPathsForValuesAffectingIsPlaying
797 return [NSSet setWithObjects:@"state", nil];
802 VLCMediaPlayerState state = [self state];
803 return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
804 (state == VLCMediaPlayerStatePlaying));
809 libvlc_exception_t ex;
810 libvlc_exception_init( &ex );
811 BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
812 if (libvlc_exception_raised(&ex))
814 libvlc_exception_clear(&ex);
821 static const VLCMediaPlayerState libvlc_to_local_state[] =
823 [libvlc_Stopped] = VLCMediaPlayerStateStopped,
824 [libvlc_Opening] = VLCMediaPlayerStateOpening,
825 [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
826 [libvlc_Playing] = VLCMediaPlayerStatePlaying,
827 [libvlc_Paused] = VLCMediaPlayerStatePaused,
828 [libvlc_Ended] = VLCMediaPlayerStateEnded,
829 [libvlc_Error] = VLCMediaPlayerStateError
832 - (VLCMediaPlayerState)state
842 - (void)setPosition:(float)newPosition
844 libvlc_exception_t ex;
845 libvlc_exception_init( &ex );
846 libvlc_media_player_set_position( instance, newPosition, &ex );
847 catch_exception( &ex );
852 libvlc_exception_t ex;
853 libvlc_exception_init( &ex );
854 BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
855 catch_exception( &ex );
861 libvlc_exception_t ex;
862 libvlc_exception_init( &ex );
863 BOOL ret = libvlc_media_player_can_pause( instance, &ex );
864 catch_exception( &ex );
868 - (void *)libVLCMediaPlayer
874 @implementation VLCMediaPlayer (Private)
875 - (id)initWithDrawable:(id)aDrawable
877 if (self = [super init])
881 cachedTime = [[VLCTime nullTime] retain];
882 cachedRemainingTime = [[VLCTime nullTime] retain];
884 cachedState = VLCMediaPlayerStateStopped;
886 // Create a media instance, it doesn't matter what library we start off with
887 // it will change depending on the media descriptor provided to the media
889 libvlc_exception_t ex;
890 libvlc_exception_init( &ex );
891 instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
892 catch_exception( &ex );
894 [self registerObservers];
896 [self setDrawable:aDrawable];
901 - (void)registerObservers
903 libvlc_exception_t ex;
904 libvlc_exception_init( &ex );
906 // Attach event observers into the media instance
907 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
908 libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
909 libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
910 libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
911 libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
912 /* FIXME: We may want to turn that off when none is interested by that */
913 libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
914 libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
915 libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, &ex );
916 catch_exception( &ex );
919 - (void)unregisterObservers
921 libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
922 libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
923 libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
924 libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
925 libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
926 libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
927 libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
928 libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, NULL );
931 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
933 [self willChangeValueForKey:@"time"];
934 [self willChangeValueForKey:@"remainingTime"];
935 [cachedTime release];
936 cachedTime = [[VLCTime timeWithNumber:newTime] retain];
937 [cachedRemainingTime release];
938 double currentTime = [[cachedTime numberValue] doubleValue];
939 double remaining = currentTime / position * (1 - position);
940 cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
941 [self didChangeValueForKey:@"remainingTime"];
942 [self didChangeValueForKey:@"time"];
947 UpdateSystemActivity(UsrActivity);
950 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
952 // This seems to be the most relevant place to delay sleeping and screen saver.
955 [self willChangeValueForKey:@"position"];
956 position = [newPosition floatValue];
957 [self didChangeValueForKey:@"position"];
960 - (void)mediaPlayerStateChanged:(NSNumber *)newState
962 [self willChangeValueForKey:@"state"];
963 cachedState = [newState intValue];
964 [self didChangeValueForKey:@"state"];
967 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
969 [self willChangeValueForKey:@"media"];
970 if (media != newMedia)
973 media = [newMedia retain];
975 [self didChangeValueForKey:@"media"];