]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCMediaPlayer.m
a386b629b71bb169257e5fc26f8f3e85fc35adef
[vlc] / projects / macosx / framework / Sources / VLCMediaPlayer.m
1 /*****************************************************************************
2  * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *          Faustion Osuna <enrique.osuna # gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #import "VLCLibrary.h"
27 #import "VLCMediaPlayer.h"
28 #import "VLCEventManager.h"
29 #import "VLCLibVLCBridging.h"
30 #import "VLCVideoView.h"
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 /* prevent system sleep */
36 #import <CoreServices/CoreServices.h>
37 /* FIXME: Ugly hack! */
38 #ifdef __x86_64__
39 #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
40 #endif
41
42 #include <vlc/vlc.h>
43
44 /* Notification Messages */
45 NSString * VLCMediaPlayerTimeChanged    = @"VLCMediaPlayerTimeChanged";
46 NSString * VLCMediaPlayerStateChanged   = @"VLCMediaPlayerStateChanged";
47
48 NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
49 {
50     static NSString * stateToStrings[] = {
51         [VLCMediaPlayerStateStopped]      = @"VLCMediaPlayerStateStopped",
52         [VLCMediaPlayerStateOpening]      = @"VLCMediaPlayerStateOpening",
53         [VLCMediaPlayerStateBuffering]    = @"VLCMediaPlayerStateBuffering",
54         [VLCMediaPlayerStateEnded]        = @"VLCMediaPlayerStateEnded",
55         [VLCMediaPlayerStateError]        = @"VLCMediaPlayerStateError",
56         [VLCMediaPlayerStatePlaying]      = @"VLCMediaPlayerStatePlaying",
57         [VLCMediaPlayerStatePaused]       = @"VLCMediaPlayerStatePaused"
58     };
59     return stateToStrings[state];
60 }
61
62 /* libvlc event callback */
63 static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
64 {
65     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
66                                                    withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
67                                                  withNotificationName:VLCMediaPlayerVolumeChanged];
68 }
69
70 static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
71 {
72     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
73     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
74                                                  withMethod:@selector(mediaPlayerTimeChanged:) 
75                                        withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
76
77     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
78                                                    withDelegateMethod:@selector(mediaPlayerTimeChanged:)
79                                                  withNotificationName:VLCMediaPlayerTimeChanged];
80     [pool release];
81 }
82
83 static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
84 {
85     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
86
87     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
88                                                  withMethod:@selector(mediaPlayerPositionChanged:) 
89                                        withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
90     [pool release];
91 }
92
93 static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
94 {
95     VLCMediaPlayerState newState;
96
97     if( event->type == libvlc_MediaPlayerPlaying )
98         newState = VLCMediaPlayerStatePlaying;
99     else if( event->type == libvlc_MediaPlayerPaused )
100         newState = VLCMediaPlayerStatePaused;
101     else if( event->type == libvlc_MediaPlayerEndReached )
102         newState = VLCMediaPlayerStateStopped;
103     else if( event->type == libvlc_MediaPlayerEncounteredError )
104         newState = VLCMediaPlayerStateError;
105     else
106     {
107         NSLog(@"%s: Unknown event", __FUNCTION__);
108         return;
109     }
110
111     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
112
113     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
114                                                  withMethod:@selector(mediaPlayerStateChanged:) 
115                                        withArgumentAsObject:[NSNumber numberWithInt:newState]];
116
117     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
118                                                    withDelegateMethod:@selector(mediaPlayerStateChanged:)
119                                                  withNotificationName:VLCMediaPlayerStateChanged];
120
121     [pool release];
122
123 }
124
125
126 // TODO: Documentation
127 @interface VLCMediaPlayer (Private)
128 - (id)initWithDrawable:(id)aDrawable;
129
130 - (void)registerObservers;
131 - (void)unregisterObservers;
132 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
133 - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
134 - (void)mediaPlayerStateChanged:(NSNumber *)newState;
135 @end
136
137 @implementation VLCMediaPlayer
138
139 /* Bindings */
140 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
141 {
142     static NSDictionary * dict = nil;
143     NSSet * superKeyPaths;
144     if( !dict )
145     {
146         dict = [[NSDictionary dictionaryWithObjectsAndKeys:
147             [NSSet setWithObject:@"state"], @"playing",
148             [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
149             [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
150             [NSSet setWithObjects:@"state", @"media", nil], @"description",
151             nil] retain];
152     }
153     if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
154     {
155         NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
156         [ret unionSet:superKeyPaths];
157         return ret;
158     }
159     return [dict objectForKey: key];
160 }
161
162 /* Contructor */
163 - (id)init
164 {
165     return [self initWithDrawable:nil];
166 }
167
168 - (id)initWithVideoView:(VLCVideoView *)aVideoView
169 {
170     return [self initWithDrawable: aVideoView];
171 }
172
173 - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
174 {
175     return [self initWithDrawable: aVideoLayer];
176 }
177
178 - (void)release
179 {
180     @synchronized(self)
181     {
182         if([self retainCount] <= 1)
183         {
184             /* We must make sure we won't receive new event after an upcoming dealloc
185              * We also may receive a -retain in some event callback that may occcur
186              * Before libvlc_event_detach. So this can't happen in dealloc */
187             [self unregisterObservers];
188         }
189         [super release];
190     }
191 }
192
193 - (void)dealloc
194 {
195     // Always get rid of the delegate first so we can stop sending messages to it
196     // TODO: Should we tell the delegate that we're shutting down?
197     delegate = nil;
198
199     libvlc_media_player_release((libvlc_media_player_t *)instance);
200     
201     // Get rid of everything else
202     [media release];
203     [cachedTime release];
204
205     [super dealloc];
206 }
207
208 - (void)setDelegate:(id)value
209 {
210     delegate = value;
211 }
212
213 - (id)delegate
214 {
215     return delegate;
216 }
217
218 - (void)setVideoView:(VLCVideoView *)aVideoView
219 {    
220     [self setDrawable: aVideoView];
221 }
222
223 - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
224 {
225     [self setDrawable: aVideoLayer];
226 }
227
228 - (void)setDrawable:(id)aDrawable
229 {
230     // Make sure that this instance has been associated with the drawing canvas.
231     libvlc_exception_t ex;
232     libvlc_exception_init( &ex );
233     libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
234     catch_exception( &ex );
235 }
236
237 - (id)drawable
238 {
239     libvlc_exception_t ex;
240     libvlc_exception_init( &ex );
241     id ret = libvlc_media_player_get_nsobject(instance);
242     catch_exception( &ex );
243     return ret;
244 }
245
246 - (VLCAudio *)audio
247 {
248     return [[VLCLibrary sharedLibrary] audio];
249 }
250
251 - (void)setVideoAspectRatio:(char *)value
252 {
253     libvlc_video_set_aspect_ratio( instance, value, NULL );
254 }
255
256 - (char *)videoAspectRatio
257 {
258     libvlc_exception_t ex;
259     libvlc_exception_init( &ex );
260     char * result = libvlc_video_get_aspect_ratio( instance, &ex );
261     catch_exception( &ex );
262     return result;
263 }
264
265 - (void)setVideoSubTitles:(int)value
266 {
267     libvlc_video_set_spu( instance, value, NULL );
268 }
269
270 - (int)videoSubTitles
271 {
272     libvlc_exception_t ex;
273     libvlc_exception_init( &ex );
274     int result = libvlc_video_get_spu( instance, &ex );
275     catch_exception( &ex );
276     return result;
277 }
278
279 - (void)setVideoCropGeometry:(char *)value
280 {
281     libvlc_video_set_crop_geometry( instance, value, NULL );
282 }
283
284 - (char *)videoCropGeometry
285 {
286     libvlc_exception_t ex;
287     libvlc_exception_init( &ex );
288     char * result = libvlc_video_get_crop_geometry( instance, &ex );
289     catch_exception( &ex );
290     return result;
291 }
292
293 - (void)setVideoTeleText:(int)value
294 {
295     libvlc_video_set_teletext( instance, value, NULL );
296 }
297
298 - (int)videoTeleText
299 {
300     libvlc_exception_t ex;
301     libvlc_exception_init( &ex );
302     int result = libvlc_video_get_teletext( instance, &ex );
303     catch_exception( &ex );
304     return result;
305 }
306
307 - (void)setRate:(float)value
308 {
309     libvlc_media_player_set_rate( instance, value, NULL );
310 }
311
312 - (float)rate
313 {
314     libvlc_exception_t ex;
315     libvlc_exception_init( &ex );
316     float result = libvlc_media_player_get_rate( instance, &ex );
317     catch_exception( &ex );
318     return result;
319 }
320
321 - (NSSize)videoSize
322 {
323     libvlc_exception_t ex;
324     libvlc_exception_init( &ex );
325     NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
326                                libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
327     catch_exception( &ex );
328     return result;    
329 }
330
331 - (BOOL)hasVideoOut
332 {
333     libvlc_exception_t ex;
334     libvlc_exception_init( &ex );
335     BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
336     if (libvlc_exception_raised( &ex ))
337     {
338         libvlc_exception_clear( &ex );
339         return NO;
340     }
341     else
342         return result;
343 }
344
345 - (float)framesPerSecond
346 {
347     libvlc_exception_t ex;
348     libvlc_exception_init( &ex );
349     float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
350     catch_exception( &ex );
351     return result;
352 }
353
354 - (void)setTime:(VLCTime *)value
355 {
356     libvlc_exception_t ex;
357     libvlc_exception_init( &ex );
358     // Time is managed in seconds, while duration is managed in microseconds
359     // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
360     libvlc_media_player_set_time( (libvlc_media_player_t *)instance, 
361                                     (value ? [[value numberValue] longLongValue] / 1000 : 0),
362                                     &ex );
363     catch_exception( &ex );
364 }
365
366 - (VLCTime *)time
367 {
368     return cachedTime;
369 }
370
371 - (void)setChapter:(int)value;
372 {
373     libvlc_media_player_set_chapter( instance, value, NULL );
374 }
375
376 - (int)chapter
377 {
378     libvlc_exception_t ex;
379     libvlc_exception_init( &ex );
380     int result = libvlc_media_player_get_chapter( instance, &ex );
381     catch_exception( &ex );
382     return result;
383 }
384
385 - (int)countOfChapters
386 {
387     libvlc_exception_t ex;
388     libvlc_exception_init( &ex );
389     int result = libvlc_media_player_get_chapter_count( instance, &ex );
390     catch_exception( &ex );
391     return result;
392 }
393
394 - (void)setAudioTrack:(int)value
395 {
396     libvlc_audio_set_track( instance, value, NULL );
397 }
398
399 - (int)audioTrack
400 {
401     libvlc_exception_t ex;
402     libvlc_exception_init( &ex );
403     int result = libvlc_audio_get_track( instance, &ex );
404     catch_exception( &ex );
405     return result;
406 }
407
408 - (int)countOfAudioTracks
409 {
410     libvlc_exception_t ex;
411     libvlc_exception_init( &ex );
412     int result = libvlc_audio_get_track_count( instance, &ex );
413     catch_exception( &ex );
414     return result;
415 }
416
417 - (void)setAudioChannel:(int)value
418 {
419     libvlc_audio_set_channel( instance, value, NULL );
420 }
421
422 - (int)audioChannel
423 {
424     libvlc_exception_t ex;
425     libvlc_exception_init( &ex );
426     int result = libvlc_audio_get_channel( instance, &ex );
427     catch_exception( &ex );
428     return result;
429 }
430
431 - (void)setMedia:(VLCMedia *)value
432 {
433     if (media != value)
434     {
435         if (media && [media compare:value] == NSOrderedSame)
436             return;
437         
438         [media release];
439         media = [value retain];
440
441         libvlc_exception_t ex;
442         libvlc_exception_init( &ex );
443         libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
444         catch_exception( &ex );
445     }
446 }
447
448 - (VLCMedia *)media
449 {
450     return media;
451 }
452
453 - (BOOL)play
454 {    
455     libvlc_exception_t ex;
456     libvlc_exception_init( &ex );
457     libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
458     catch_exception( &ex );
459     return YES;
460 }
461
462 - (void)pause
463 {
464     if( [NSThread isMainThread] )
465     {
466         /* Hack because we create a dead lock here, when the vout is stopped
467          * and tries to recontact us on the main thread */
468         /* FIXME: to do this properly we need to do some locking. We may want 
469          * to move that to libvlc */
470         [self performSelectorInBackground:@selector(pause) withObject:nil];
471         return;
472     }
473
474     // Pause the stream
475     libvlc_exception_t ex;
476     libvlc_exception_init( &ex );
477     libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
478     catch_exception( &ex );
479 }
480
481 - (void)stop
482 {
483     if( 0 && [NSThread isMainThread] )
484     {
485         /* Hack because we create a dead lock here, when the vout is stopped
486          * and tries to recontact us on the main thread */
487         /* FIXME: to do this properly we need to do some locking. We may want 
488          * to move that to libvlc */
489         [self performSelectorInBackground:@selector(stop) withObject:nil];
490         return;
491     }
492     
493     libvlc_exception_t ex;
494     libvlc_exception_init( &ex );
495     libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
496     catch_exception( &ex );
497 }
498
499 - (void)fastForward
500 {
501     [self fastForwardAtRate: 2.0];
502 }
503
504 - (void)fastForwardAtRate:(float)rate
505 {
506     [self setRate:rate];
507 }
508
509 - (void)rewind
510 {
511     [self rewindAtRate: 2.0];
512 }
513
514 - (void)rewindAtRate:(float)rate
515 {
516     [self setRate: -rate];
517 }
518
519 + (NSSet *)keyPathsForValuesAffectingIsPlaying
520 {
521     return [NSSet setWithObjects:@"state", nil];
522 }
523
524 - (BOOL)isPlaying
525 {
526     VLCMediaPlayerState state = [self state];
527     return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
528             (state == VLCMediaPlayerStatePlaying));
529 }
530
531 - (BOOL)willPlay
532 {
533     libvlc_exception_t ex;
534     libvlc_exception_init( &ex );
535     BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
536     if (libvlc_exception_raised(&ex))
537     {
538         libvlc_exception_clear(&ex);
539         return NO;
540     }
541     else
542         return ret;
543 }
544
545 static const VLCMediaPlayerState libvlc_to_local_state[] =
546 {
547     [libvlc_Stopped]    = VLCMediaPlayerStateStopped,
548     [libvlc_Opening]    = VLCMediaPlayerStateOpening,
549     [libvlc_Buffering]  = VLCMediaPlayerStateBuffering,
550     [libvlc_Playing]    = VLCMediaPlayerStatePlaying,
551     [libvlc_Paused]     = VLCMediaPlayerStatePaused,
552     [libvlc_Ended]      = VLCMediaPlayerStateEnded,
553     [libvlc_Error]      = VLCMediaPlayerStateError
554 };
555
556 - (VLCMediaPlayerState)state
557 {
558     return cachedState;
559 }
560
561 - (float)position
562 {
563     return position;
564 }
565
566 - (void)setPosition:(float)newPosition
567 {
568     libvlc_exception_t ex;
569     libvlc_exception_init( &ex );
570     libvlc_media_player_set_position( instance, newPosition, &ex );
571     catch_exception( &ex );
572 }
573
574 - (BOOL)isSeekable
575 {
576     libvlc_exception_t ex;
577     libvlc_exception_init( &ex );
578     BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
579     catch_exception( &ex );
580     return ret;
581 }
582
583 - (BOOL)canPause
584 {
585     libvlc_exception_t ex;
586     libvlc_exception_init( &ex );
587     BOOL ret = libvlc_media_player_can_pause( instance, &ex );
588     catch_exception( &ex );
589     return ret;
590 }
591
592 - (void *)libVLCMediaPlayer
593 {
594     return instance;
595 }
596 @end
597
598 @implementation VLCMediaPlayer (Private)
599 - (id)initWithDrawable:(id)aDrawable
600 {
601     if (self = [super init])
602     {
603         delegate = nil;
604         media = nil;
605         cachedTime = [[VLCTime nullTime] retain];
606         position = 0.0f;
607         cachedState = VLCMediaPlayerStateStopped;
608
609         // Create a media instance, it doesn't matter what library we start off with
610         // it will change depending on the media descriptor provided to the media
611         // instance
612         libvlc_exception_t ex;
613         libvlc_exception_init( &ex );
614         instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
615         catch_exception( &ex );
616         
617         [self registerObservers];
618         
619         [self setDrawable:aDrawable];
620     }
621     return self;
622 }
623
624 - (void)registerObservers
625 {
626     libvlc_exception_t ex;
627     libvlc_exception_init( &ex );
628
629     // Attach event observers into the media instance
630     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
631     libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self, &ex );
632     libvlc_event_attach( p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self, &ex );
633     libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
634     libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self, &ex );
635     /* FIXME: We may want to turn that off when none is interested by that */
636     libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged,      self, &ex );
637     libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged,     HandleMediaTimeChanged,          self, &ex );
638     catch_exception( &ex );
639 }
640
641 - (void)unregisterObservers
642 {
643     libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
644     libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying,          HandleMediaInstanceStateChanged, self, NULL );
645     libvlc_event_detach( p_em, libvlc_MediaPlayerPaused,           HandleMediaInstanceStateChanged, self, NULL );
646     libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
647     libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached,       HandleMediaInstanceStateChanged, self, NULL );
648     libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged,  HandleMediaPositionChanged,      self, NULL );
649     libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged,      HandleMediaTimeChanged,          self, NULL );
650 }
651
652 - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
653 {
654     [self willChangeValueForKey:@"time"];
655     [cachedTime release];
656     cachedTime = [[VLCTime timeWithNumber:newTime] retain];
657
658     [self didChangeValueForKey:@"time"];
659 }
660
661 - (void)delaySleep
662 {
663     UpdateSystemActivity(UsrActivity);
664 }
665
666 - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
667 {
668     // This seems to be the most relevant place to delay sleeping and screen saver.
669     [self delaySleep];
670
671     [self willChangeValueForKey:@"position"];
672     position = [newPosition floatValue];
673     [self didChangeValueForKey:@"position"];
674 }
675
676 - (void)mediaPlayerStateChanged:(NSNumber *)newState
677 {
678     [self willChangeValueForKey:@"state"];
679     cachedState = [newState intValue];
680     [self didChangeValueForKey:@"state"];
681 }
682
683 @end