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