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];
231 - (void)setDelegate:(id)value
241 - (void)setVideoView:(VLCVideoView *)aVideoView
243 [self setDrawable: aVideoView];
246 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
248 [self setDrawable: aVideoLayer];
251 - (void)setDrawable:(id)aDrawable
253 // Make sure that this instance has been associated with the drawing canvas.
254 libvlc_exception_t ex;
255 libvlc_exception_init( &ex );
256 libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
257 catch_exception( &ex );
262 libvlc_exception_t ex;
263 libvlc_exception_init( &ex );
264 id ret = libvlc_media_player_get_nsobject(instance);
265 catch_exception( &ex );
271 return [[VLCLibrary sharedLibrary] audio];
275 #pragma mark Subtitles
277 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
279 libvlc_exception_t ex;
280 libvlc_exception_init( &ex );
281 libvlc_video_set_spu( instance, (int)index, &ex );
282 catch_exception( &ex );
285 - (NSUInteger)currentVideoSubTitleIndex
287 libvlc_exception_t ex;
288 libvlc_exception_init( &ex );
289 int count = libvlc_video_get_spu_count( instance, &ex );
290 catch_exception( &ex );
293 NSUInteger result = libvlc_video_get_spu( instance, &ex );
294 catch_exception( &ex );
298 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
300 libvlc_exception_t ex;
301 libvlc_exception_init( &ex );
302 BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
303 catch_exception( &ex );
307 - (NSArray *)videoSubTitles
309 libvlc_exception_t ex;
310 libvlc_exception_init( &ex );
311 int count = libvlc_video_get_spu_count( instance, &ex );
312 catch_exception( &ex );
314 libvlc_track_description_t *tracks = libvlc_video_get_spu_description( instance, &ex );
315 NSMutableArray *tempArray = [NSMutableArray array];
317 for (i = 0; i < count; i++)
319 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
320 tracks = tracks->p_next;
322 return [NSArray arrayWithArray: tempArray];
327 #pragma mark Video Crop geometry
329 - (void)setVideoCropGeometry:(char *)value
331 libvlc_exception_t ex;
332 libvlc_exception_init( &ex );
333 libvlc_video_set_crop_geometry( instance, value, &ex );
334 catch_exception( &ex );
337 - (char *)videoCropGeometry
339 libvlc_exception_t ex;
340 libvlc_exception_init( &ex );
341 char * result = libvlc_video_get_crop_geometry( instance, &ex );
342 catch_exception( &ex );
346 - (void)setVideoAspectRatio:(char *)value
348 libvlc_exception_t ex;
349 libvlc_exception_init( &ex );
350 libvlc_video_set_aspect_ratio( instance, value, &ex );
351 catch_exception( &ex );
354 - (char *)videoAspectRatio
356 libvlc_exception_t ex;
357 libvlc_exception_init( &ex );
358 char * result = libvlc_video_get_aspect_ratio( instance, &ex );
359 catch_exception( &ex );
363 - (void)setVideoTeleText:(int)value
365 libvlc_exception_t ex;
366 libvlc_exception_init( &ex );
367 libvlc_video_set_teletext( instance, value, &ex );
368 catch_exception( &ex );
373 libvlc_exception_t ex;
374 libvlc_exception_init( &ex );
375 int result = libvlc_video_get_teletext( instance, &ex );
376 catch_exception( &ex );
380 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
382 libvlc_exception_t ex;
383 libvlc_exception_init( &ex );
384 libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
385 catch_exception( &ex );
388 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
390 libvlc_exception_t ex;
391 libvlc_exception_init( &ex );
392 libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
393 catch_exception( &ex );
396 - (void)setRate:(float)value
398 libvlc_exception_t ex;
399 libvlc_exception_init( &ex );
400 libvlc_media_player_set_rate( instance, value, &ex );
401 catch_exception( &ex );
406 libvlc_exception_t ex;
407 libvlc_exception_init( &ex );
408 float result = libvlc_media_player_get_rate( instance, &ex );
409 catch_exception( &ex );
415 libvlc_exception_t ex;
416 libvlc_exception_init( &ex );
417 NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
418 libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
419 catch_exception( &ex );
425 libvlc_exception_t ex;
426 libvlc_exception_init( &ex );
427 BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
428 if (libvlc_exception_raised( &ex ))
430 libvlc_exception_clear( &ex );
437 - (float)framesPerSecond
439 libvlc_exception_t ex;
440 libvlc_exception_init( &ex );
441 float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
442 catch_exception( &ex );
446 - (void)setTime:(VLCTime *)value
448 libvlc_exception_t ex;
449 libvlc_exception_init( &ex );
450 // Time is managed in seconds, while duration is managed in microseconds
451 // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
452 libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
453 (value ? [[value numberValue] longLongValue] / 1000 : 0),
455 catch_exception( &ex );
463 - (VLCTime *)remainingTime
465 double currentTime = [[cachedTime numberValue] doubleValue];
466 double remaining = currentTime / position * (1 - position);
467 return [VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]];
472 libvlc_exception_t ex;
473 libvlc_exception_init( &ex );
474 int result = libvlc_media_player_get_fps( instance, &ex );
475 catch_exception( &ex );
480 #pragma mark Chapters
481 - (void)setCurrentChapterIndex:(NSUInteger)value;
483 libvlc_exception_t ex;
484 libvlc_exception_init( &ex );
485 libvlc_media_player_set_chapter( instance, value, &ex );
486 catch_exception( &ex );
489 - (NSUInteger)currentChapterIndex
491 libvlc_exception_t ex;
492 libvlc_exception_init( &ex );
493 int count = libvlc_media_player_get_chapter_count( instance, &ex );
494 catch_exception( &ex );
497 NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
498 catch_exception( &ex );
504 libvlc_exception_t ex;
505 libvlc_exception_init( &ex );
506 libvlc_media_player_next_chapter( instance, &ex );
507 catch_exception( &ex );
510 - (void)previousChapter
512 libvlc_exception_t ex;
513 libvlc_exception_init( &ex );
514 libvlc_media_player_previous_chapter( instance, &ex );
515 catch_exception( &ex );
518 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
520 libvlc_exception_t ex;
521 libvlc_exception_init( &ex );
522 int count = libvlc_media_player_get_chapter_count(instance, &ex);
524 return [NSArray array];
526 libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
527 NSMutableArray *tempArray = [NSMutableArray array];
529 for (i = 0; i < count ; i++)
531 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
532 tracks = tracks->p_next;
534 return [NSArray arrayWithArray: tempArray];
540 - (void)setCurrentTitleIndex:(NSUInteger)value
542 libvlc_exception_t ex;
543 libvlc_exception_init( &ex );
544 libvlc_media_player_set_title( instance, value, &ex );
545 catch_exception( &ex );
548 - (NSUInteger)currentTitleIndex
550 libvlc_exception_t ex;
551 libvlc_exception_init( &ex );
553 int count = libvlc_media_player_get_title_count( instance, &ex );
554 catch_exception( &ex );
558 NSUInteger result = libvlc_media_player_get_title( instance, &ex );
559 catch_exception( &ex );
565 libvlc_exception_t ex;
566 libvlc_exception_init( &ex );
567 int result = libvlc_media_player_get_title_count( instance, &ex );
568 catch_exception( &ex );
574 libvlc_exception_t ex;
575 libvlc_exception_init( &ex );
576 libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
577 NSMutableArray *tempArray = [NSMutableArray array];
579 for (i = 0; i < [self countOfTitles] ; i++)
581 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
582 tracks = tracks->p_next;
584 return [NSArray arrayWithArray: tempArray];
588 #pragma mark Audio tracks
589 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
591 libvlc_exception_t ex;
592 libvlc_exception_init( &ex );
593 libvlc_audio_set_track( instance, (int)value, &ex );
594 catch_exception( &ex );
597 - (NSUInteger)currentAudioTrackIndex
599 libvlc_exception_t ex;
600 libvlc_exception_init( &ex );
601 int count = libvlc_audio_get_track_count( instance, &ex );
602 catch_exception( &ex );
606 NSUInteger result = libvlc_audio_get_track( instance, &ex );
607 catch_exception( &ex );
611 - (NSArray *)audioTracks
613 libvlc_exception_t ex;
614 libvlc_exception_init( &ex );
615 int count = libvlc_audio_get_track_count( instance, &ex );
616 catch_exception( &ex );
618 return [NSArray array];
620 libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
621 NSMutableArray *tempArray = [NSMutableArray array];
623 for (i = 0; i < count ; i++)
625 [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
626 tracks = tracks->p_next;
629 return [NSArray arrayWithArray: tempArray];
632 - (void)setAudioChannel:(int)value
634 libvlc_exception_t ex;
635 libvlc_exception_init( &ex );
636 libvlc_audio_set_channel( instance, value, &ex );
637 catch_exception( &ex );
642 libvlc_exception_t ex;
643 libvlc_exception_init( &ex );
644 int result = libvlc_audio_get_channel( instance, &ex );
645 catch_exception( &ex );
649 - (void)setMedia:(VLCMedia *)value
653 if (media && [media compare:value] == NSOrderedSame)
657 media = [value retain];
659 libvlc_exception_t ex;
660 libvlc_exception_init( &ex );
661 libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
662 catch_exception( &ex );
673 libvlc_exception_t ex;
674 libvlc_exception_init( &ex );
675 libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
676 catch_exception( &ex );
682 if( [NSThread isMainThread] )
684 /* Hack because we create a dead lock here, when the vout is stopped
685 * and tries to recontact us on the main thread */
686 /* FIXME: to do this properly we need to do some locking. We may want
687 * to move that to libvlc */
688 [self performSelectorInBackground:@selector(pause) withObject:nil];
693 libvlc_exception_t ex;
694 libvlc_exception_init( &ex );
695 libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
698 // in most cases, it's just EOF so let's stop
699 if (libvlc_exception_raised(&ex))
702 libvlc_exception_clear(&ex);
707 libvlc_exception_t ex;
708 libvlc_exception_init( &ex );
709 libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
710 catch_exception( &ex );
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 * 1000000;
738 [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
742 - (void)jumpForward:(NSInteger)interval
744 if( [self isSeekable] )
746 interval = interval * 1000000;
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];
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];
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"];