]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCMediaPlayer.m
osx/framework: extended API to cover titles, fps, playback modes and some convienienc...
[vlc] / projects / macosx / framework / Sources / VLCMediaPlayer.m
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
7  * $Id$
8  *
9  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10  *          Faustion Osuna <enrique.osuna # gmail.com>
11  *          Felix Paul Kühne <fkuehne # videolan.org>
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 #import "VLCLibrary.h"
29 #import "VLCMediaPlayer.h"
30 #import "VLCEventManager.h"
31 #import "VLCLibVLCBridging.h"
32 #import "VLCVideoView.h"
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 /* prevent system sleep */
38 #import <CoreServices/CoreServices.h>
39 /* FIXME: Ugly hack! */
40 #ifdef __x86_64__
41 #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
42 #endif
43
44 #include <vlc/vlc.h>
45
46 /* Notification Messages */
47 NSString * VLCMediaPlayerTimeChanged    = @"VLCMediaPlayerTimeChanged";
48 NSString * VLCMediaPlayerStateChanged   = @"VLCMediaPlayerStateChanged";
49
50 NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
51 {
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"
60     };
61     return stateToStrings[state];
62 }
63
64 /* libvlc event callback */
65 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
66 {
67     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
68                                                    withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
69                                                  withNotificationName:VLCMediaPlayerVolumeChanged];
70 }
71
72 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
73 {
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]];
78
79     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
80                                                    withDelegateMethod:@selector(mediaPlayerTimeChanged:)
81                                                  withNotificationName:VLCMediaPlayerTimeChanged];
82     [pool release];
83 }
84
85 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
86 {
87     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
88
89     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
90                                                  withMethod:@selector(mediaPlayerPositionChanged:) 
91                                        withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
92     [pool release];
93 }
94
95 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
96 {
97     VLCMediaPlayerState newState;
98
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;
107     else
108     {
109         NSLog(@"%s: Unknown event", __FUNCTION__);
110         return;
111     }
112
113     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
114
115     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
116                                                  withMethod:@selector(mediaPlayerStateChanged:) 
117                                        withArgumentAsObject:[NSNumber numberWithInt:newState]];
118
119     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
120                                                    withDelegateMethod:@selector(mediaPlayerStateChanged:)
121                                                  withNotificationName:VLCMediaPlayerStateChanged];
122
123     [pool release];
124
125 }
126
127
128 // TODO: Documentation
129 @interface VLCMediaPlayer (Private)
130 - (id)initWithDrawable:(id)aDrawable;
131
132 - (void)registerObservers;
133 - (void)unregisterObservers;
134 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
135 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
136 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
137 @end
138
139 @implementation VLCMediaPlayer
140
141 /* Bindings */
142 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
143 {
144     static NSDictionary * dict = nil;
145     NSSet * superKeyPaths;
146     if( !dict )
147     {
148         dict = [[NSDictionary dictionaryWithObjectsAndKeys:
149             [NSSet setWithObject:@"state"], @"playing",
150             [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
151             [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
152             [NSSet setWithObjects:@"state", @"media", nil], @"description",
153             nil] retain];
154     }
155     if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
156     {
157         NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
158         [ret unionSet:superKeyPaths];
159         return ret;
160     }
161     return [dict objectForKey: key];
162 }
163
164 /* Contructor */
165 - (id)init
166 {
167     return [self initWithDrawable:nil];
168 }
169
170 - (id)initWithVideoView:(VLCVideoView *)aVideoView
171 {
172     return [self initWithDrawable: aVideoView];
173 }
174
175 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
176 {
177     return [self initWithDrawable: aVideoLayer];
178 }
179
180 - (void)release
181 {
182     @synchronized(self)
183     {
184         if([self retainCount] <= 1)
185         {
186             /* We must make sure we won't receive new event after an upcoming dealloc
187              * We also may receive a -retain in some event callback that may occcur
188              * Before libvlc_event_detach. So this can't happen in dealloc */
189             [self unregisterObservers];
190         }
191         [super release];
192     }
193 }
194
195 - (void)dealloc
196 {
197     NSAssert(libvlc_media_player_get_state(instance, NULL) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
198
199     // Always get rid of the delegate first so we can stop sending messages to it
200     // TODO: Should we tell the delegate that we're shutting down?
201     delegate = nil;
202
203     // Clear our drawable as we are going to release it, we don't
204     // want the core to use it from this point. This won't happen as
205     // the media player must be stopped.
206     libvlc_media_player_set_nsobject(instance, nil, NULL);
207
208     libvlc_media_player_release(instance);
209     
210     // Get rid of everything else
211     [media release];
212     [cachedTime release];
213     [drawable release];
214
215     [super dealloc];
216 }
217
218 - (void)setDelegate:(id)value
219 {
220     delegate = value;
221 }
222
223 - (id)delegate
224 {
225     return delegate;
226 }
227
228 - (void)setVideoView:(VLCVideoView *)aVideoView
229 {    
230     [self setDrawable: aVideoView];
231 }
232
233 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
234 {
235     [self setDrawable: aVideoLayer];
236 }
237
238 - (void)setDrawable:(id)aDrawable
239 {
240     // Make sure that this instance has been associated with the drawing canvas.
241     libvlc_exception_t ex;
242     libvlc_exception_init( &ex );
243     libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
244     catch_exception( &ex );
245 }
246
247 - (id)drawable
248 {
249     libvlc_exception_t ex;
250     libvlc_exception_init( &ex );
251     id ret = libvlc_media_player_get_nsobject(instance);
252     catch_exception( &ex );
253     return ret;
254 }
255
256 - (VLCAudio *)audio
257 {
258     return [[VLCLibrary sharedLibrary] audio];
259 }
260
261 - (void)setVideoAspectRatio:(char *)value
262 {
263     libvlc_video_set_aspect_ratio( instance, value, NULL );
264 }
265
266 - (char *)videoAspectRatio
267 {
268     libvlc_exception_t ex;
269     libvlc_exception_init( &ex );
270     char * result = libvlc_video_get_aspect_ratio( instance, &ex );
271     catch_exception( &ex );
272     return result;
273 }
274
275 - (void)setVideoSubTitles:(int)value
276 {
277     libvlc_video_set_spu( instance, value, NULL );
278 }
279
280 - (int)videoSubTitles
281 {
282     libvlc_exception_t ex;
283     libvlc_exception_init( &ex );
284     int result = libvlc_video_get_spu( instance, &ex );
285     catch_exception( &ex );
286     return result;
287 }
288
289 - (void)setVideoCropGeometry:(char *)value
290 {
291     libvlc_video_set_crop_geometry( instance, value, NULL );
292 }
293
294 - (char *)videoCropGeometry
295 {
296     libvlc_exception_t ex;
297     libvlc_exception_init( &ex );
298     char * result = libvlc_video_get_crop_geometry( instance, &ex );
299     catch_exception( &ex );
300     return result;
301 }
302
303 - (void)setVideoTeleText:(int)value
304 {
305     libvlc_video_set_teletext( instance, value, NULL );
306 }
307
308 - (int)videoTeleText
309 {
310     libvlc_exception_t ex;
311     libvlc_exception_init( &ex );
312     int result = libvlc_video_get_teletext( instance, &ex );
313     catch_exception( &ex );
314     return result;
315 }
316
317 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
318 {
319     libvlc_exception_t ex;
320     libvlc_exception_init( &ex );
321     libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
322     catch_exception( &ex );
323 }
324
325 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
326 {
327     libvlc_exception_t ex;
328     libvlc_exception_init( &ex );
329     libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
330     catch_exception( &ex );
331 }
332
333 - (void)setRate:(float)value
334 {
335     libvlc_media_player_set_rate( instance, value, NULL );
336 }
337
338 - (float)rate
339 {
340     libvlc_exception_t ex;
341     libvlc_exception_init( &ex );
342     float result = libvlc_media_player_get_rate( instance, &ex );
343     catch_exception( &ex );
344     return result;
345 }
346
347 - (NSSize)videoSize
348 {
349     libvlc_exception_t ex;
350     libvlc_exception_init( &ex );
351     NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
352                                libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
353     catch_exception( &ex );
354     return result;
355 }
356
357 - (BOOL)hasVideoOut
358 {
359     libvlc_exception_t ex;
360     libvlc_exception_init( &ex );
361     BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
362     if (libvlc_exception_raised( &ex ))
363     {
364         libvlc_exception_clear( &ex );
365         return NO;
366     }
367     else
368         return result;
369 }
370
371 - (float)framesPerSecond
372 {
373     libvlc_exception_t ex;
374     libvlc_exception_init( &ex );
375     float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
376     catch_exception( &ex );
377     return result;
378 }
379
380 - (void)setTime:(VLCTime *)value
381 {
382     libvlc_exception_t ex;
383     libvlc_exception_init( &ex );
384     // Time is managed in seconds, while duration is managed in microseconds
385     // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
386     libvlc_media_player_set_time( (libvlc_media_player_t *)instance, 
387                                     (value ? [[value numberValue] longLongValue] / 1000 : 0),
388                                     &ex );
389     catch_exception( &ex );
390 }
391
392 - (VLCTime *)time
393 {
394     return cachedTime;
395 }
396
397 - (VLCTime *)remainingTime
398 {
399     double currentTime = [[cachedTime numberValue] doubleValue];
400     double remaining = currentTime / position * (1 - position);
401     return [VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]];
402 }
403
404 - (int)fps
405 {
406     libvlc_exception_t ex;
407     libvlc_exception_init( &ex );
408     int result = libvlc_media_player_get_fps( instance, &ex );
409     catch_exception( &ex );
410     return result;
411 }
412
413 - (void)setChapter:(int)value;
414 {
415     libvlc_exception_t ex;
416     libvlc_exception_init( &ex );
417     libvlc_media_player_set_chapter( instance, value, &ex );
418     catch_exception( &ex );
419 }
420
421 - (int)chapter
422 {
423     libvlc_exception_t ex;
424     libvlc_exception_init( &ex );
425     int result = libvlc_media_player_get_chapter( instance, &ex );
426     catch_exception( &ex );
427     return result;
428 }
429
430 - (int)countOfChapters
431 {
432     libvlc_exception_t ex;
433     libvlc_exception_init( &ex );
434     int result = libvlc_media_player_get_chapter_count( instance, &ex );
435     catch_exception( &ex );
436     return result;
437 }
438
439 - (void)nextChapter
440 {
441     libvlc_exception_t ex;
442     libvlc_exception_init( &ex );
443     libvlc_media_player_next_chapter( instance, &ex );
444     catch_exception( &ex );
445 }
446
447 - (void)previousChapter
448 {
449     libvlc_exception_t ex;
450     libvlc_exception_init( &ex );
451     libvlc_media_player_previous_chapter( instance, &ex );
452     catch_exception( &ex );
453 }
454
455 - (void)setTitle:(int)value
456 {
457     libvlc_exception_t ex;
458     libvlc_exception_init( &ex );
459     libvlc_media_player_set_title( instance, value, &ex );
460     catch_exception( &ex );
461 }
462
463 - (int)title
464 {
465     libvlc_exception_t ex;
466     libvlc_exception_init( &ex );
467     int result = libvlc_media_player_get_title( instance, &ex );
468     catch_exception( &ex );
469     return result;
470 }
471
472 - (int)countOfTitles
473 {
474     libvlc_exception_t ex;
475     libvlc_exception_init( &ex );
476     int result = libvlc_media_player_get_title_count( instance, &ex );
477     catch_exception( &ex );
478     return result;
479 }
480
481 - (void)setAudioTrack:(int)value
482 {
483     libvlc_audio_set_track( instance, value, NULL );
484 }
485
486 - (int)audioTrack
487 {
488     libvlc_exception_t ex;
489     libvlc_exception_init( &ex );
490     int result = libvlc_audio_get_track( instance, &ex );
491     catch_exception( &ex );
492     return result;
493 }
494
495 - (int)countOfAudioTracks
496 {
497     libvlc_exception_t ex;
498     libvlc_exception_init( &ex );
499     int result = libvlc_audio_get_track_count( instance, &ex );
500     catch_exception( &ex );
501     return result;
502 }
503
504 - (void)setAudioChannel:(int)value
505 {
506     libvlc_audio_set_channel( instance, value, NULL );
507 }
508
509 - (int)audioChannel
510 {
511     libvlc_exception_t ex;
512     libvlc_exception_init( &ex );
513     int result = libvlc_audio_get_channel( instance, &ex );
514     catch_exception( &ex );
515     return result;
516 }
517
518 - (void)setMedia:(VLCMedia *)value
519 {
520     if (media != value)
521     {
522         if (media && [media compare:value] == NSOrderedSame)
523             return;
524         
525         [media release];
526         media = [value retain];
527
528         libvlc_exception_t ex;
529         libvlc_exception_init( &ex );
530         libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
531         catch_exception( &ex );
532     }
533 }
534
535 - (VLCMedia *)media
536 {
537     return media;
538 }
539
540 - (BOOL)play
541 {    
542     libvlc_exception_t ex;
543     libvlc_exception_init( &ex );
544     libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
545     catch_exception( &ex );
546     return YES;
547 }
548
549 - (void)pause
550 {
551     if( [NSThread isMainThread] )
552     {
553         /* Hack because we create a dead lock here, when the vout is stopped
554          * and tries to recontact us on the main thread */
555         /* FIXME: to do this properly we need to do some locking. We may want 
556          * to move that to libvlc */
557         [self performSelectorInBackground:@selector(pause) withObject:nil];
558         return;
559     }
560
561     // Pause the stream
562     libvlc_exception_t ex;
563     libvlc_exception_init( &ex );
564     libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
565     catch_exception( &ex );
566 }
567
568 - (void)stop
569 {
570     libvlc_exception_t ex;
571     libvlc_exception_init( &ex );
572     libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
573     catch_exception( &ex );
574 }
575
576 - (void)fastForward
577 {
578     [self fastForwardAtRate: 2.0];
579 }
580
581 - (void)fastForwardAtRate:(float)rate
582 {
583     [self setRate:rate];
584 }
585
586 - (void)rewind
587 {
588     [self rewindAtRate: 2.0];
589 }
590
591 - (void)rewindAtRate:(float)rate
592 {
593     [self setRate: -rate];
594 }
595
596 - (void)jumpBackward:(NSInteger)interval
597 {
598     if( [self isSeekable] )
599     {
600         interval = interval * 1000000;
601         [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
602     }
603 }
604
605 - (void)jumpForward:(NSInteger)interval
606 {
607     if( [self isSeekable] )
608     {
609         interval = interval * 1000000;
610         [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
611     }
612 }
613
614 - (void)extraShortJumpBackward
615 {
616     [self jumpBackward:3];
617 }
618
619 - (void)extraShortJumpForward
620 {
621     [self jumpForward:3];
622 }
623
624 - (void)shortJumpBackward
625 {
626     [self jumpBackward:10];
627 }
628
629 - (void)shortJumpForward
630 {
631     [self jumpForward:10];
632 }
633
634 - (void)mediumJumpBackward
635 {
636     [self jumpBackward:60];
637 }
638
639 - (void)mediumJumpForward
640 {
641     [self jumpForward:60];
642 }
643
644 - (void)longJumpBackward
645 {
646     [self jumpBackward:300];
647 }
648
649 - (void)longJumpForward
650 {
651     [self jumpForward:300];
652 }
653
654 + (NSSet *)keyPathsForValuesAffectingIsPlaying
655 {
656     return [NSSet setWithObjects:@"state", nil];
657 }
658
659 - (BOOL)isPlaying
660 {
661     VLCMediaPlayerState state = [self state];
662     return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
663             (state == VLCMediaPlayerStatePlaying));
664 }
665
666 - (BOOL)willPlay
667 {
668     libvlc_exception_t ex;
669     libvlc_exception_init( &ex );
670     BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
671     if (libvlc_exception_raised(&ex))
672     {
673         libvlc_exception_clear(&ex);
674         return NO;
675     }
676     else
677         return ret;
678 }
679
680 static const VLCMediaPlayerState libvlc_to_local_state[] =
681 {
682     [libvlc_Stopped]    = VLCMediaPlayerStateStopped,
683     [libvlc_Opening]    = VLCMediaPlayerStateOpening,
684     [libvlc_Buffering]  = VLCMediaPlayerStateBuffering,
685     [libvlc_Playing]    = VLCMediaPlayerStatePlaying,
686     [libvlc_Paused]     = VLCMediaPlayerStatePaused,
687     [libvlc_Ended]      = VLCMediaPlayerStateEnded,
688     [libvlc_Error]      = VLCMediaPlayerStateError
689 };
690
691 - (VLCMediaPlayerState)state
692 {
693     return cachedState;
694 }
695
696 - (float)position
697 {
698     return position;
699 }
700
701 - (void)setPosition:(float)newPosition
702 {
703     libvlc_exception_t ex;
704     libvlc_exception_init( &ex );
705     libvlc_media_player_set_position( instance, newPosition, &ex );
706     catch_exception( &ex );
707 }
708
709 - (BOOL)isSeekable
710 {
711     libvlc_exception_t ex;
712     libvlc_exception_init( &ex );
713     BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
714     catch_exception( &ex );
715     return ret;
716 }
717
718 - (BOOL)canPause
719 {
720     libvlc_exception_t ex;
721     libvlc_exception_init( &ex );
722     BOOL ret = libvlc_media_player_can_pause( instance, &ex );
723     catch_exception( &ex );
724     return ret;
725 }
726
727 - (void *)libVLCMediaPlayer
728 {
729     return instance;
730 }
731 @end
732
733 @implementation VLCMediaPlayer (Private)
734 - (id)initWithDrawable:(id)aDrawable
735 {
736     if (self = [super init])
737     {
738         delegate = nil;
739         media = nil;
740         cachedTime = [[VLCTime nullTime] retain];
741         position = 0.0f;
742         cachedState = VLCMediaPlayerStateStopped;
743
744         // Create a media instance, it doesn't matter what library we start off with
745         // it will change depending on the media descriptor provided to the media
746         // instance
747         libvlc_exception_t ex;
748         libvlc_exception_init( &ex );
749         instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
750         catch_exception( &ex );
751         
752         [self registerObservers];
753         
754         [self setDrawable:aDrawable];
755     }
756     return self;
757 }
758
759 - (void)registerObservers
760 {
761     libvlc_exception_t ex;
762     libvlc_exception_init( &ex );
763
764     // Attach event observers into the media instance
765     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
766     libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self, &ex );
767     libvlc_event_attach( p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self, &ex );
768     libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
769     libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self, &ex );
770     /* FIXME: We may want to turn that off when none is interested by that */
771     libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged,      self, &ex );
772     libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged,     HandleMediaTimeChanged,          self, &ex );
773     catch_exception( &ex );
774 }
775
776 - (void)unregisterObservers
777 {
778     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
779     libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self, NULL );
780     libvlc_event_detach( p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self, NULL );
781     libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
782     libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self, NULL );
783     libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged,  HandleMediaPositionChanged,      self, NULL );
784     libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged,      HandleMediaTimeChanged,          self, NULL );
785 }
786
787 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
788 {
789     [self willChangeValueForKey:@"time"];
790     [self willChangeValueForKey:@"remainingTime"];
791     [cachedTime release];
792     cachedTime = [[VLCTime timeWithNumber:newTime] retain];
793
794     [self didChangeValueForKey:@"remainingTime"];
795     [self didChangeValueForKey:@"time"];
796 }
797
798 - (void)delaySleep
799 {
800     UpdateSystemActivity(UsrActivity);
801 }
802
803 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
804 {
805     // This seems to be the most relevant place to delay sleeping and screen saver.
806     [self delaySleep];
807
808     [self willChangeValueForKey:@"position"];
809     position = [newPosition floatValue];
810     [self didChangeValueForKey:@"position"];
811 }
812
813 - (void)mediaPlayerStateChanged:(NSNumber *)newState
814 {
815     [self willChangeValueForKey:@"state"];
816     cachedState = [newState intValue];
817     [self didChangeValueForKey:@"state"];
818 }
819
820 @end