]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCMediaPlayer.m
a50333b1eef0bb1addeea92eadd7b57983ce1db8
[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 drain];
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 drain];
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 drain];
124
125 }
126
127 static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
128 {
129     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
130
131     [[VLCEventManager sharedManager] callOnMainThreadObject:self
132                                                  withMethod:@selector(mediaPlayerMediaChanged:)
133                                        withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
134
135     [pool drain];
136
137 }
138
139
140 // TODO: Documentation
141 @interface VLCMediaPlayer (Private)
142 - (id)initWithDrawable:(id)aDrawable;
143
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;
150 @end
151
152 @implementation VLCMediaPlayer
153
154 /* Bindings */
155 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
156 {
157     static NSDictionary * dict = nil;
158     NSSet * superKeyPaths;
159     if( !dict )
160     {
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",
166             nil] retain];
167     }
168     if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
169     {
170         NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
171         [ret unionSet:superKeyPaths];
172         return ret;
173     }
174     return [dict objectForKey: key];
175 }
176
177 /* Contructor */
178 - (id)init
179 {
180     return [self initWithDrawable:nil];
181 }
182
183 - (id)initWithVideoView:(VLCVideoView *)aVideoView
184 {
185     return [self initWithDrawable: aVideoView];
186 }
187
188 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
189 {
190     return [self initWithDrawable: aVideoLayer];
191 }
192
193 - (void)release
194 {
195     @synchronized(self)
196     {
197         if([self retainCount] <= 1)
198         {
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];
203         }
204         [super release];
205     }
206 }
207
208 - (void)dealloc
209 {
210     NSAssert(libvlc_media_player_get_state(instance) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
211
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?
214     delegate = nil;
215
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);
220
221     libvlc_media_player_release(instance);
222
223     // Get rid of everything else
224     [media release];
225     [cachedTime release];
226     [cachedRemainingTime release];
227     [drawable release];
228     [audio release];
229
230     [super dealloc];
231 }
232
233 - (void)setDelegate:(id)value
234 {
235     delegate = value;
236 }
237
238 - (id)delegate
239 {
240     return delegate;
241 }
242
243 - (void)setVideoView:(VLCVideoView *)aVideoView
244 {
245     [self setDrawable: aVideoView];
246 }
247
248 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
249 {
250     [self setDrawable: aVideoLayer];
251 }
252
253 - (void)setDrawable:(id)aDrawable
254 {
255     // Make sure that this instance has been associated with the drawing canvas.
256     libvlc_media_player_set_nsobject(instance, aDrawable);
257 }
258
259 - (id)drawable
260 {
261     return libvlc_media_player_get_nsobject(instance);
262 }
263
264 - (VLCAudio *)audio
265 {
266     if (!audio)
267         audio = [[VLCAudio alloc] initWithMediaPlayer:self];
268     return audio;
269 }
270
271 #pragma mark -
272 #pragma mark Subtitles
273
274 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
275 {
276     libvlc_video_set_spu(instance, (int)index);
277 }
278
279 - (NSUInteger)currentVideoSubTitleIndex
280 {
281     NSInteger count = libvlc_video_get_spu_count(instance);
282
283     if (count <= 0)
284         return NSNotFound;
285
286     return libvlc_video_get_spu(instance);
287 }
288
289 - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
290 {
291     return libvlc_video_set_subtitle_file(instance, [path UTF8String]);
292 }
293
294 - (NSArray *)videoSubTitles
295 {
296     libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(instance);
297
298     NSMutableArray *tempArray = [NSMutableArray array];
299     while (currentTrack) {
300         [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
301         currentTrack = currentTrack->p_next;
302     }
303     libvlc_track_description_release(currentTrack);
304     return [NSArray arrayWithArray: tempArray];
305 }
306
307
308 #pragma mark -
309 #pragma mark Video Crop geometry
310
311 - (void)setVideoCropGeometry:(char *)value
312 {
313     libvlc_video_set_crop_geometry(instance, value);
314 }
315
316 - (char *)videoCropGeometry
317 {
318     char * result = libvlc_video_get_crop_geometry(instance);
319     return result;
320 }
321
322 - (void)setVideoAspectRatio:(char *)value
323 {
324     libvlc_video_set_aspect_ratio( instance, value );
325 }
326
327 - (char *)videoAspectRatio
328 {
329     char * result = libvlc_video_get_aspect_ratio( instance );
330     return result;
331 }
332
333 - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
334 {
335     int failure = libvlc_video_take_snapshot(instance, 0, [path UTF8String], width, height);
336     if (failure)
337         [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
338 }
339
340 - (void)setDeinterlaceFilter:(NSString *)name
341 {
342     libvlc_video_set_deinterlace(instance, [name UTF8String]);
343 }
344
345 - (void)setRate:(float)value
346 {
347     libvlc_media_player_set_rate(instance, value);
348 }
349
350 - (float)rate
351 {
352     return libvlc_media_player_get_rate(instance);
353 }
354
355 - (NSSize)videoSize
356 {
357     unsigned height = 0, width = 0;
358     int failure = libvlc_video_get_size(instance, 0, &width, &height);
359     if (failure)
360         [[NSException exceptionWithName:@"Can't get video size" reason:@"No video output" userInfo:nil] raise];
361     return NSMakeSize(width, height);
362 }
363
364 - (BOOL)hasVideoOut
365 {
366     return libvlc_media_player_has_vout(instance);
367 }
368
369 - (float)framesPerSecond
370 {
371     return libvlc_media_player_get_fps(instance);
372 }
373
374 - (void)setTime:(VLCTime *)value
375 {
376     // Time is managed in seconds, while duration is managed in microseconds
377     // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
378     libvlc_media_player_set_time(instance, value ? [[value numberValue] longLongValue] : 0);
379 }
380
381 - (VLCTime *)time
382 {
383     return cachedTime;
384 }
385
386 - (VLCTime *)remainingTime
387 {
388     return cachedRemainingTime;
389 }
390
391 - (NSUInteger)fps
392 {
393     return libvlc_media_player_get_fps(instance);
394 }
395
396 #pragma mark -
397 #pragma mark Chapters
398 - (void)setCurrentChapterIndex:(NSUInteger)value;
399 {
400     libvlc_media_player_set_chapter(instance, value);
401 }
402
403 - (NSUInteger)currentChapterIndex
404 {
405     NSInteger count = libvlc_media_player_get_chapter_count(instance);
406     if (count <= 0)
407         return NSNotFound;
408     NSUInteger result = libvlc_media_player_get_chapter(instance);
409     return result;
410 }
411
412 - (void)nextChapter
413 {
414     libvlc_media_player_next_chapter(instance);
415 }
416
417 - (void)previousChapter
418 {
419     libvlc_media_player_previous_chapter(instance);
420 }
421
422 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
423 {
424     NSInteger count = libvlc_media_player_get_chapter_count(instance);
425     if (count <= 0)
426         return [NSArray array];
427
428     libvlc_track_description_t *tracks = libvlc_video_get_chapter_description(instance, title);
429     NSMutableArray *tempArray = [NSMutableArray array];
430     NSInteger i;
431     for (i = 0; i < count ; i++)
432     {
433         [tempArray addObject:[NSString stringWithUTF8String:tracks->psz_name]];
434         tracks = tracks->p_next;
435     }
436     libvlc_track_description_release(tracks);
437     return [NSArray arrayWithArray:tempArray];
438 }
439
440 #pragma mark -
441 #pragma mark Titles
442
443 - (void)setCurrentTitleIndex:(NSUInteger)value
444 {
445     libvlc_media_player_set_title(instance, value);
446 }
447
448 - (NSUInteger)currentTitleIndex
449 {
450     NSInteger count = libvlc_media_player_get_title_count(instance);
451     if (count <= 0)
452         return NSNotFound;
453
454     return libvlc_media_player_get_title(instance);
455 }
456
457 - (NSUInteger)countOfTitles
458 {
459     NSUInteger result = libvlc_media_player_get_title_count(instance);
460     return result;
461 }
462
463 - (NSArray *)titles
464 {
465     libvlc_track_description_t *tracks = libvlc_video_get_title_description(instance);
466     NSMutableArray *tempArray = [NSMutableArray array];
467     NSInteger i;
468     for (i = 0; i < [self countOfTitles] ; i++)
469     {
470         [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
471         tracks = tracks->p_next;
472     }
473     libvlc_track_description_release(tracks);
474     return [NSArray arrayWithArray: tempArray];
475 }
476
477 #pragma mark -
478 #pragma mark Audio tracks
479 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
480 {
481     libvlc_audio_set_track( instance, (int)value);
482 }
483
484 - (NSUInteger)currentAudioTrackIndex
485 {
486     NSInteger count = libvlc_audio_get_track_count(instance);
487     if (count <= 0)
488         return NSNotFound;
489
490     NSUInteger result = libvlc_audio_get_track(instance);
491     return result;
492 }
493
494 - (NSArray *)audioTracks
495 {
496     NSInteger count = libvlc_audio_get_track_count(instance);
497     if (count <= 0)
498         return [NSArray array];
499
500     libvlc_track_description_t *tracks = libvlc_audio_get_track_description(instance);
501     NSMutableArray *tempArray = [NSMutableArray array];
502     NSUInteger i;
503     for (i = 0; i < count ; i++)
504     {
505         [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
506         tracks = tracks->p_next;
507     }
508     libvlc_track_description_release(tracks);
509
510     return [NSArray arrayWithArray: tempArray];
511 }
512
513 - (void)setAudioChannel:(NSInteger)value
514 {
515     libvlc_audio_set_channel(instance, value);
516 }
517
518 - (NSInteger)audioChannel
519 {
520     return libvlc_audio_get_channel(instance);
521 }
522
523 - (void)setMedia:(VLCMedia *)value
524 {
525     if (media != value)
526     {
527         if (media && [media compare:value] == NSOrderedSame)
528             return;
529
530         [media release];
531         media = [value retain];
532
533         libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
534     }
535 }
536
537 - (VLCMedia *)media
538 {
539     return media;
540 }
541
542 - (BOOL)play
543 {
544     libvlc_media_player_play(instance);
545     return YES;
546 }
547
548 - (void)pause
549 {
550     if( [NSThread isMainThread] )
551     {
552         /* Hack because we create a dead lock here, when the vout is stopped
553          * and tries to recontact us on the main thread */
554         /* FIXME: to do this properly we need to do some locking. We may want
555          * to move that to libvlc */
556         [self performSelectorInBackground:@selector(pause) withObject:nil];
557         return;
558     }
559
560     // Pause the stream
561     libvlc_media_player_pause(instance);
562 }
563
564 - (void)stop
565 {
566     libvlc_media_player_stop(instance);
567 }
568
569 - (void)gotoNextFrame
570 {
571     libvlc_media_player_next_frame(instance);
572
573 }
574
575 - (void)fastForward
576 {
577     [self fastForwardAtRate: 2.0];
578 }
579
580 - (void)fastForwardAtRate:(float)rate
581 {
582     [self setRate:rate];
583 }
584
585 - (void)rewind
586 {
587     [self rewindAtRate: 2.0];
588 }
589
590 - (void)rewindAtRate:(float)rate
591 {
592     [self setRate: -rate];
593 }
594
595 - (void)jumpBackward:(NSInteger)interval
596 {
597     if( [self isSeekable] )
598     {
599         interval = interval * 1000;
600         [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
601     }
602 }
603
604 - (void)jumpForward:(NSInteger)interval
605 {
606     if( [self isSeekable] )
607     {
608         interval = interval * 1000;
609         [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
610     }
611 }
612
613 - (void)extraShortJumpBackward
614 {
615     [self jumpBackward:3];
616 }
617
618 - (void)extraShortJumpForward
619 {
620     [self jumpForward:3];
621 }
622
623 - (void)shortJumpBackward
624 {
625     [self jumpBackward:10];
626 }
627
628 - (void)shortJumpForward
629 {
630     [self jumpForward:10];
631 }
632
633 - (void)mediumJumpBackward
634 {
635     [self jumpBackward:60];
636 }
637
638 - (void)mediumJumpForward
639 {
640     [self jumpForward:60];
641 }
642
643 - (void)longJumpBackward
644 {
645     [self jumpBackward:300];
646 }
647
648 - (void)longJumpForward
649 {
650     [self jumpForward:300];
651 }
652
653 + (NSSet *)keyPathsForValuesAffectingIsPlaying
654 {
655     return [NSSet setWithObjects:@"state", nil];
656 }
657
658 - (BOOL)isPlaying
659 {
660     VLCMediaPlayerState state = [self state];
661     return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
662             (state == VLCMediaPlayerStatePlaying));
663 }
664
665 - (BOOL)willPlay
666 {
667     return libvlc_media_player_will_play(instance);
668 }
669
670 static const VLCMediaPlayerState libvlc_to_local_state[] =
671 {
672     [libvlc_Stopped]    = VLCMediaPlayerStateStopped,
673     [libvlc_Opening]    = VLCMediaPlayerStateOpening,
674     [libvlc_Buffering]  = VLCMediaPlayerStateBuffering,
675     [libvlc_Playing]    = VLCMediaPlayerStatePlaying,
676     [libvlc_Paused]     = VLCMediaPlayerStatePaused,
677     [libvlc_Ended]      = VLCMediaPlayerStateEnded,
678     [libvlc_Error]      = VLCMediaPlayerStateError
679 };
680
681 - (VLCMediaPlayerState)state
682 {
683     return cachedState;
684 }
685
686 - (float)position
687 {
688     return position;
689 }
690
691 - (void)setPosition:(float)newPosition
692 {
693     libvlc_media_player_set_position(instance, newPosition);
694 }
695
696 - (BOOL)isSeekable
697 {
698     return libvlc_media_player_is_seekable(instance);
699 }
700
701 - (BOOL)canPause
702 {
703     return libvlc_media_player_can_pause(instance);
704 }
705
706 - (void *)libVLCMediaPlayer
707 {
708     return instance;
709 }
710 @end
711
712 @implementation VLCMediaPlayer (Private)
713 - (id)initWithDrawable:(id)aDrawable
714 {
715     if (self = [super init])
716     {
717         delegate = nil;
718         media = nil;
719         cachedTime = [[VLCTime nullTime] retain];
720         cachedRemainingTime = [[VLCTime nullTime] retain];
721         position = 0.0f;
722         cachedState = VLCMediaPlayerStateStopped;
723
724         // Create a media instance, it doesn't matter what library we start off with
725         // it will change depending on the media descriptor provided to the media
726         // instance
727         instance = libvlc_media_player_new([VLCLibrary sharedInstance]);
728
729         [self registerObservers];
730
731         [self setDrawable:aDrawable];
732     }
733     return self;
734 }
735
736 - (void)registerObservers
737 {
738     // Attach event observers into the media instance
739     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
740     libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self);
741     libvlc_event_attach(p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self);
742     libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
743     libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self);
744     /* FIXME: We may want to turn that off when none is interested by that */
745     libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged,      self);
746     libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged,     HandleMediaTimeChanged,          self);
747     libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged,    HandleMediaPlayerMediaChanged,  self);
748 }
749
750 - (void)unregisterObservers
751 {
752     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
753     libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self);
754     libvlc_event_detach(p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self);
755     libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
756     libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self);
757     libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged,  HandleMediaPositionChanged,      self);
758     libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged,      HandleMediaTimeChanged,          self);
759     libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged,     HandleMediaPlayerMediaChanged,   self);
760 }
761
762 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
763 {
764     [self willChangeValueForKey:@"time"];
765     [self willChangeValueForKey:@"remainingTime"];
766     [cachedTime release];
767     cachedTime = [[VLCTime timeWithNumber:newTime] retain];
768     [cachedRemainingTime release];
769     double currentTime = [[cachedTime numberValue] doubleValue];
770     double remaining = currentTime / position * (1 - position);
771     cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
772     [self didChangeValueForKey:@"remainingTime"];
773     [self didChangeValueForKey:@"time"];
774 }
775
776 - (void)delaySleep
777 {
778     UpdateSystemActivity(UsrActivity);
779 }
780
781 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
782 {
783     // This seems to be the most relevant place to delay sleeping and screen saver.
784     [self delaySleep];
785
786     [self willChangeValueForKey:@"position"];
787     position = [newPosition floatValue];
788     [self didChangeValueForKey:@"position"];
789 }
790
791 - (void)mediaPlayerStateChanged:(NSNumber *)newState
792 {
793     [self willChangeValueForKey:@"state"];
794     cachedState = [newState intValue];
795     [self didChangeValueForKey:@"state"];
796 }
797
798 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
799 {
800     [self willChangeValueForKey:@"media"];
801     if (media != newMedia)
802     {
803         [media release];
804         media = [newMedia retain];
805     }
806     [self didChangeValueForKey:@"media"];
807 }
808
809 @end