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