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