]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCMediaPlayer.m
d85f08b919a5f008f26d8001e209ba8688504190
[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 static inline libvlc_track_description_t *freeAndGetNextTrack(libvlc_track_description_t *track)
65 {
66     libvlc_track_description_t *next = track->p_next;
67     libvlc_track_description_release(track);
68     return next;
69 }
70
71 /* libvlc event callback */
72 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
73 {
74     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
75                                                    withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
76                                                  withNotificationName:VLCMediaPlayerVolumeChanged];
77 }
78
79 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
80 {
81     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
82     [[VLCEventManager sharedManager] callOnMainThreadObject:self
83                                                  withMethod:@selector(mediaPlayerTimeChanged:)
84                                        withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
85
86     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
87                                                    withDelegateMethod:@selector(mediaPlayerTimeChanged:)
88                                                  withNotificationName:VLCMediaPlayerTimeChanged];
89     [pool release];
90 }
91
92 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
93 {
94     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
95
96     [[VLCEventManager sharedManager] callOnMainThreadObject:self
97                                                  withMethod:@selector(mediaPlayerPositionChanged:)
98                                        withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
99     [pool release];
100 }
101
102 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
103 {
104     VLCMediaPlayerState newState;
105
106     if( event->type == libvlc_MediaPlayerPlaying )
107         newState = VLCMediaPlayerStatePlaying;
108     else if( event->type == libvlc_MediaPlayerPaused )
109         newState = VLCMediaPlayerStatePaused;
110     else if( event->type == libvlc_MediaPlayerEndReached )
111         newState = VLCMediaPlayerStateStopped;
112     else if( event->type == libvlc_MediaPlayerEncounteredError )
113         newState = VLCMediaPlayerStateError;
114     else
115     {
116         NSLog(@"%s: Unknown event", __FUNCTION__);
117         return;
118     }
119
120     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
121
122     [[VLCEventManager sharedManager] callOnMainThreadObject:self
123                                                  withMethod:@selector(mediaPlayerStateChanged:)
124                                        withArgumentAsObject:[NSNumber numberWithInt:newState]];
125
126     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
127                                                    withDelegateMethod:@selector(mediaPlayerStateChanged:)
128                                                  withNotificationName:VLCMediaPlayerStateChanged];
129
130     [pool release];
131
132 }
133
134 static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
135 {
136     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
137
138     [[VLCEventManager sharedManager] callOnMainThreadObject:self
139                                                  withMethod:@selector(mediaPlayerMediaChanged:)
140                                        withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
141
142     [pool release];
143
144 }
145
146
147 // TODO: Documentation
148 @interface VLCMediaPlayer (Private)
149 - (id)initWithDrawable:(id)aDrawable;
150
151 - (void)registerObservers;
152 - (void)unregisterObservers;
153 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
154 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
155 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
156 - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
157 @end
158
159 @implementation VLCMediaPlayer
160
161 /* Bindings */
162 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
163 {
164     static NSDictionary * dict = nil;
165     NSSet * superKeyPaths;
166     if( !dict )
167     {
168         dict = [[NSDictionary dictionaryWithObjectsAndKeys:
169             [NSSet setWithObject:@"state"], @"playing",
170             [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
171             [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
172             [NSSet setWithObjects:@"state", @"media", nil], @"description",
173             nil] retain];
174     }
175     if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
176     {
177         NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
178         [ret unionSet:superKeyPaths];
179         return ret;
180     }
181     return [dict objectForKey: key];
182 }
183
184 /* Contructor */
185 - (id)init
186 {
187     return [self initWithDrawable:nil];
188 }
189
190 - (id)initWithVideoView:(VLCVideoView *)aVideoView
191 {
192     return [self initWithDrawable: aVideoView];
193 }
194
195 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
196 {
197     return [self initWithDrawable: aVideoLayer];
198 }
199
200 - (void)release
201 {
202     @synchronized(self)
203     {
204         if([self retainCount] <= 1)
205         {
206             /* We must make sure we won't receive new event after an upcoming dealloc
207              * We also may receive a -retain in some event callback that may occcur
208              * Before libvlc_event_detach. So this can't happen in dealloc */
209             [self unregisterObservers];
210         }
211         [super release];
212     }
213 }
214
215 - (void)dealloc
216 {
217     NSAssert(libvlc_media_player_get_state(instance) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
218
219     // Always get rid of the delegate first so we can stop sending messages to it
220     // TODO: Should we tell the delegate that we're shutting down?
221     delegate = nil;
222
223     // Clear our drawable as we are going to release it, we don't
224     // want the core to use it from this point. This won't happen as
225     // the media player must be stopped.
226     libvlc_media_player_set_nsobject(instance, nil);
227
228     libvlc_media_player_release(instance);
229
230     // Get rid of everything else
231     [media release];
232     [cachedTime release];
233     [cachedRemainingTime release];
234     [drawable release];
235
236     [super dealloc];
237 }
238
239 - (void)setDelegate:(id)value
240 {
241     delegate = value;
242 }
243
244 - (id)delegate
245 {
246     return delegate;
247 }
248
249 - (void)setVideoView:(VLCVideoView *)aVideoView
250 {
251     [self setDrawable: aVideoView];
252 }
253
254 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
255 {
256     [self setDrawable: aVideoLayer];
257 }
258
259 - (void)setDrawable:(id)aDrawable
260 {
261     // Make sure that this instance has been associated with the drawing canvas.
262     libvlc_media_player_set_nsobject(instance, aDrawable);
263 }
264
265 - (id)drawable
266 {
267     libvlc_exception_t ex;
268     libvlc_exception_init( &ex );
269     id ret = libvlc_media_player_get_nsobject(instance);
270     catch_exception( &ex );
271     return ret;
272 }
273
274 - (VLCAudio *)audio
275 {
276     return [[VLCLibrary sharedLibrary] audio];
277 }
278
279 #pragma mark -
280 #pragma mark Subtitles
281
282 - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
283 {
284     libvlc_exception_t ex;
285     libvlc_exception_init( &ex );
286     libvlc_video_set_spu( instance, (int)index, &ex );
287     catch_exception( &ex );
288 }
289
290 - (NSUInteger)currentVideoSubTitleIndex
291 {
292     libvlc_exception_t ex;
293     libvlc_exception_init( &ex );
294     NSInteger count = libvlc_video_get_spu_count( instance, &ex );
295     if (libvlc_exception_raised( &ex ))
296     {
297         libvlc_exception_clear( &ex );
298         return NSNotFound;
299     }
300     if (count <= 0)
301         return NSNotFound;
302     NSUInteger result = libvlc_video_get_spu( instance, &ex );
303     if (libvlc_exception_raised( &ex ))
304     {
305         libvlc_exception_clear( &ex );
306         return NSNotFound;
307     }
308     else
309         return result;
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 - (NSArray *)videoSubTitles
322 {
323     libvlc_exception_t ex;
324     libvlc_exception_init( &ex );
325     libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description( instance, &ex );
326     catch_exception( &ex );
327
328     NSMutableArray *tempArray = [NSMutableArray array];
329     while (currentTrack) {
330         [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
331         currentTrack = freeAndGetNextTrack(currentTrack);
332     }
333     return [NSArray arrayWithArray: tempArray];
334 }
335
336
337 #pragma mark -
338 #pragma mark Video Crop geometry
339
340 - (void)setVideoCropGeometry:(char *)value
341 {
342     libvlc_exception_t ex;
343     libvlc_exception_init( &ex );
344     libvlc_video_set_crop_geometry( instance, value, &ex );
345     catch_exception( &ex );
346 }
347
348 - (char *)videoCropGeometry
349 {
350     libvlc_exception_t ex;
351     libvlc_exception_init( &ex );
352     char * result = libvlc_video_get_crop_geometry( instance, &ex );
353     catch_exception( &ex );
354     return result;
355 }
356
357 - (void)setVideoAspectRatio:(char *)value
358 {
359     libvlc_exception_t ex;
360     libvlc_exception_init( &ex );
361     libvlc_video_set_aspect_ratio( instance, value, &ex );
362     catch_exception( &ex );
363 }
364
365 - (char *)videoAspectRatio
366 {
367     libvlc_exception_t ex;
368     libvlc_exception_init( &ex );
369     char * result = libvlc_video_get_aspect_ratio( instance, &ex );
370     catch_exception( &ex );
371     return result;
372 }
373
374 - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
375 {
376     libvlc_exception_t ex;
377     libvlc_exception_init( &ex );
378     libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
379     catch_exception( &ex );
380 }
381
382 - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
383 {
384     libvlc_exception_t ex;
385     libvlc_exception_init( &ex );
386     libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
387     catch_exception( &ex );
388 }
389
390 - (void)setRate:(float)value
391 {
392     libvlc_exception_t ex;
393     libvlc_exception_init( &ex );
394     libvlc_media_player_set_rate( instance, value, &ex );
395     catch_exception( &ex );
396 }
397
398 - (float)rate
399 {
400     libvlc_exception_t ex;
401     libvlc_exception_init( &ex );
402     float result = libvlc_media_player_get_rate( instance, &ex );
403     if (libvlc_exception_raised(&ex))
404     {
405         result = 1;
406         libvlc_exception_clear(&ex);
407     }
408     return result;
409 }
410
411 - (NSSize)videoSize
412 {
413     libvlc_exception_t ex;
414     libvlc_exception_init( &ex );
415     NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
416                                libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
417     catch_exception( &ex );
418     return result;
419 }
420
421 - (BOOL)hasVideoOut
422 {
423     libvlc_exception_t ex;
424     libvlc_exception_init( &ex );
425     BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
426     if (libvlc_exception_raised( &ex ))
427     {
428         libvlc_exception_clear( &ex );
429         return NO;
430     }
431     else
432         return result;
433 }
434
435 - (float)framesPerSecond
436 {
437     libvlc_exception_t ex;
438     libvlc_exception_init( &ex );
439     float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
440     catch_exception( &ex );
441     return result;
442 }
443
444 - (void)setTime:(VLCTime *)value
445 {
446     libvlc_exception_t ex;
447     libvlc_exception_init( &ex );
448     // Time is managed in seconds, while duration is managed in microseconds
449     // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
450     libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
451                                     (value ? [[value numberValue] longLongValue] : 0),
452                                     &ex );
453     catch_exception( &ex );
454 }
455
456 - (VLCTime *)time
457 {
458     return cachedTime;
459 }
460
461 - (VLCTime *)remainingTime
462 {
463     return cachedRemainingTime;
464 }
465
466 - (NSUInteger)fps
467 {
468     libvlc_exception_t ex;
469     libvlc_exception_init( &ex );
470     NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
471     catch_exception( &ex );
472     return result;
473 }
474
475 #pragma mark -
476 #pragma mark Chapters
477 - (void)setCurrentChapterIndex:(NSUInteger)value;
478 {
479     libvlc_exception_t ex;
480     libvlc_exception_init( &ex );
481     libvlc_media_player_set_chapter( instance, value, &ex );
482     catch_exception( &ex );
483 }
484
485 - (NSUInteger)currentChapterIndex
486 {
487     libvlc_exception_t ex;
488     libvlc_exception_init( &ex );
489     NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
490     catch_exception( &ex );
491     if (count <= 0)
492         return NSNotFound;
493     NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
494     catch_exception( &ex );
495     return result;
496 }
497
498 - (void)nextChapter
499 {
500     libvlc_exception_t ex;
501     libvlc_exception_init( &ex );
502     libvlc_media_player_next_chapter( instance, &ex );
503     catch_exception( &ex );
504 }
505
506 - (void)previousChapter
507 {
508     libvlc_exception_t ex;
509     libvlc_exception_init( &ex );
510     libvlc_media_player_previous_chapter( instance, &ex );
511     catch_exception( &ex );
512 }
513
514 - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
515 {
516     libvlc_exception_t ex;
517     libvlc_exception_init( &ex );
518     NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
519     if (count <= 0)
520         return [NSArray array];
521
522     libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
523     NSMutableArray *tempArray = [NSMutableArray array];
524     NSInteger i;
525     for (i = 0; i < count ; i++)
526     {
527         [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
528         tracks = freeAndGetNextTrack(tracks);
529     }
530     return [NSArray arrayWithArray: tempArray];
531 }
532
533 #pragma mark -
534 #pragma mark Titles
535
536 - (void)setCurrentTitleIndex:(NSUInteger)value
537 {
538     libvlc_exception_t ex;
539     libvlc_exception_init( &ex );
540     libvlc_media_player_set_title( instance, value, &ex );
541     catch_exception( &ex );
542 }
543
544 - (NSUInteger)currentTitleIndex
545 {
546     libvlc_exception_t ex;
547     libvlc_exception_init( &ex );
548
549     NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
550     catch_exception( &ex );
551     if (count <= 0)
552         return NSNotFound;
553
554     NSUInteger result = libvlc_media_player_get_title( instance, &ex );
555     catch_exception( &ex );
556     return result;
557 }
558
559 - (NSUInteger)countOfTitles
560 {
561     libvlc_exception_t ex;
562     libvlc_exception_init( &ex );
563     NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
564     catch_exception( &ex );
565     return result;
566 }
567
568 - (NSArray *)titles
569 {
570     libvlc_exception_t ex;
571     libvlc_exception_init( &ex );
572     libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
573     NSMutableArray *tempArray = [NSMutableArray array];
574     NSInteger i;
575     for (i = 0; i < [self countOfTitles] ; i++)
576     {
577         [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
578         tracks = freeAndGetNextTrack(tracks);
579     }
580     return [NSArray arrayWithArray: tempArray];
581 }
582
583 #pragma mark -
584 #pragma mark Audio tracks
585 - (void)setCurrentAudioTrackIndex:(NSUInteger)value
586 {
587     libvlc_exception_t ex;
588     libvlc_exception_init( &ex );
589     libvlc_audio_set_track( instance, (int)value, &ex );
590     catch_exception( &ex );
591 }
592
593 - (NSUInteger)currentAudioTrackIndex
594 {
595     libvlc_exception_t ex;
596     libvlc_exception_init( &ex );
597     NSInteger count = libvlc_audio_get_track_count( instance, &ex );
598     catch_exception( &ex );
599     if (count <= 0)
600         return NSNotFound;
601
602     NSUInteger result = libvlc_audio_get_track( instance, &ex );
603     catch_exception( &ex );
604     return result;
605 }
606
607 - (NSArray *)audioTracks
608 {
609     libvlc_exception_t ex;
610     libvlc_exception_init( &ex );
611     NSInteger count = libvlc_audio_get_track_count( instance, &ex );
612     catch_exception( &ex );
613     if (count <= 0)
614         return [NSArray array];
615
616     libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
617     NSMutableArray *tempArray = [NSMutableArray array];
618     NSUInteger i;
619     for (i = 0; i < count ; i++)
620     {
621         [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
622         tracks = freeAndGetNextTrack(tracks);
623     }
624
625     return [NSArray arrayWithArray: tempArray];
626 }
627
628 - (void)setAudioChannel:(NSInteger)value
629 {
630     libvlc_exception_t ex;
631     libvlc_exception_init( &ex );
632     libvlc_audio_set_channel( instance, value, &ex );
633     catch_exception( &ex );
634 }
635
636 - (NSInteger)audioChannel
637 {
638     libvlc_exception_t ex;
639     libvlc_exception_init( &ex );
640     NSInteger result = libvlc_audio_get_channel( instance, &ex );
641     catch_exception( &ex );
642     return result;
643 }
644
645 - (void)setMedia:(VLCMedia *)value
646 {
647     if (media != value)
648     {
649         if (media && [media compare:value] == NSOrderedSame)
650             return;
651
652         [media release];
653         media = [value retain];
654
655         libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
656     }
657 }
658
659 - (VLCMedia *)media
660 {
661     return media;
662 }
663
664 - (BOOL)play
665 {
666     libvlc_exception_t ex;
667     libvlc_exception_init( &ex );
668     libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
669     catch_exception( &ex );
670     return YES;
671 }
672
673 - (void)pause
674 {
675     if( [NSThread isMainThread] )
676     {
677         /* Hack because we create a dead lock here, when the vout is stopped
678          * and tries to recontact us on the main thread */
679         /* FIXME: to do this properly we need to do some locking. We may want
680          * to move that to libvlc */
681         [self performSelectorInBackground:@selector(pause) withObject:nil];
682         return;
683     }
684
685     // Pause the stream
686     libvlc_exception_t ex;
687     libvlc_exception_init(&ex);
688     libvlc_media_player_pause(instance, &ex);
689
690     // fail gracefully
691     // in most cases, it's just EOF so let's stop
692     if (libvlc_exception_raised(&ex))
693         [self stop];
694
695     libvlc_exception_clear(&ex);
696 }
697
698 - (void)stop
699 {
700     libvlc_media_player_stop(instance);
701 }
702
703 - (void)gotoNextFrame
704 {
705     libvlc_exception_t e;
706     libvlc_exception_init(&e);
707     libvlc_media_player_next_frame(instance, &e);
708     catch_exception(&e);
709
710 }
711
712 - (void)fastForward
713 {
714     [self fastForwardAtRate: 2.0];
715 }
716
717 - (void)fastForwardAtRate:(float)rate
718 {
719     [self setRate:rate];
720 }
721
722 - (void)rewind
723 {
724     [self rewindAtRate: 2.0];
725 }
726
727 - (void)rewindAtRate:(float)rate
728 {
729     [self setRate: -rate];
730 }
731
732 - (void)jumpBackward:(NSInteger)interval
733 {
734     if( [self isSeekable] )
735     {
736         interval = interval * 1000;
737         [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
738     }
739 }
740
741 - (void)jumpForward:(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)extraShortJumpBackward
751 {
752     [self jumpBackward:3];
753 }
754
755 - (void)extraShortJumpForward
756 {
757     [self jumpForward:3];
758 }
759
760 - (void)shortJumpBackward
761 {
762     [self jumpBackward:10];
763 }
764
765 - (void)shortJumpForward
766 {
767     [self jumpForward:10];
768 }
769
770 - (void)mediumJumpBackward
771 {
772     [self jumpBackward:60];
773 }
774
775 - (void)mediumJumpForward
776 {
777     [self jumpForward:60];
778 }
779
780 - (void)longJumpBackward
781 {
782     [self jumpBackward:300];
783 }
784
785 - (void)longJumpForward
786 {
787     [self jumpForward:300];
788 }
789
790 + (NSSet *)keyPathsForValuesAffectingIsPlaying
791 {
792     return [NSSet setWithObjects:@"state", nil];
793 }
794
795 - (BOOL)isPlaying
796 {
797     VLCMediaPlayerState state = [self state];
798     return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
799             (state == VLCMediaPlayerStatePlaying));
800 }
801
802 - (BOOL)willPlay
803 {
804     libvlc_exception_t ex;
805     libvlc_exception_init( &ex );
806     BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
807     if (libvlc_exception_raised(&ex))
808     {
809         libvlc_exception_clear(&ex);
810         return NO;
811     }
812     else
813         return ret;
814 }
815
816 static const VLCMediaPlayerState libvlc_to_local_state[] =
817 {
818     [libvlc_Stopped]    = VLCMediaPlayerStateStopped,
819     [libvlc_Opening]    = VLCMediaPlayerStateOpening,
820     [libvlc_Buffering]  = VLCMediaPlayerStateBuffering,
821     [libvlc_Playing]    = VLCMediaPlayerStatePlaying,
822     [libvlc_Paused]     = VLCMediaPlayerStatePaused,
823     [libvlc_Ended]      = VLCMediaPlayerStateEnded,
824     [libvlc_Error]      = VLCMediaPlayerStateError
825 };
826
827 - (VLCMediaPlayerState)state
828 {
829     return cachedState;
830 }
831
832 - (float)position
833 {
834     return position;
835 }
836
837 - (void)setPosition:(float)newPosition
838 {
839     libvlc_exception_t ex;
840     libvlc_exception_init( &ex );
841     libvlc_media_player_set_position( instance, newPosition, &ex );
842     catch_exception( &ex );
843 }
844
845 - (BOOL)isSeekable
846 {
847     libvlc_exception_t ex;
848     libvlc_exception_init( &ex );
849     BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
850     catch_exception( &ex );
851     return ret;
852 }
853
854 - (BOOL)canPause
855 {
856     libvlc_exception_t ex;
857     libvlc_exception_init( &ex );
858     BOOL ret = libvlc_media_player_can_pause( instance, &ex );
859     catch_exception( &ex );
860     return ret;
861 }
862
863 - (void *)libVLCMediaPlayer
864 {
865     return instance;
866 }
867 @end
868
869 @implementation VLCMediaPlayer (Private)
870 - (id)initWithDrawable:(id)aDrawable
871 {
872     if (self = [super init])
873     {
874         delegate = nil;
875         media = nil;
876         cachedTime = [[VLCTime nullTime] retain];
877         cachedRemainingTime = [[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     // Attach event observers into the media instance
899     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
900     libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self);
901     libvlc_event_attach(p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self);
902     libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
903     libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self);
904     /* FIXME: We may want to turn that off when none is interested by that */
905     libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged,      self);
906     libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged,     HandleMediaTimeChanged,          self);
907     libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged,    HandleMediaPlayerMediaChanged,  self);
908 }
909
910 - (void)unregisterObservers
911 {
912     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
913     libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self);
914     libvlc_event_detach(p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self);
915     libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
916     libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self);
917     libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged,  HandleMediaPositionChanged,      self);
918     libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged,      HandleMediaTimeChanged,          self);
919     libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged,     HandleMediaPlayerMediaChanged,   self);
920 }
921
922 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
923 {
924     [self willChangeValueForKey:@"time"];
925     [self willChangeValueForKey:@"remainingTime"];
926     [cachedTime release];
927     cachedTime = [[VLCTime timeWithNumber:newTime] retain];
928     [cachedRemainingTime release];
929     double currentTime = [[cachedTime numberValue] doubleValue];
930     double remaining = currentTime / position * (1 - position);
931     cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
932     [self didChangeValueForKey:@"remainingTime"];
933     [self didChangeValueForKey:@"time"];
934 }
935
936 - (void)delaySleep
937 {
938     UpdateSystemActivity(UsrActivity);
939 }
940
941 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
942 {
943     // This seems to be the most relevant place to delay sleeping and screen saver.
944     [self delaySleep];
945
946     [self willChangeValueForKey:@"position"];
947     position = [newPosition floatValue];
948     [self didChangeValueForKey:@"position"];
949 }
950
951 - (void)mediaPlayerStateChanged:(NSNumber *)newState
952 {
953     [self willChangeValueForKey:@"state"];
954     cachedState = [newState intValue];
955     [self didChangeValueForKey:@"state"];
956 }
957
958 - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
959 {
960     [self willChangeValueForKey:@"media"];
961     if (media != newMedia)
962     {
963         [media release];
964         media = [newMedia retain];
965     }
966     [self didChangeValueForKey:@"media"];
967 }
968
969 @end