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