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