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