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