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