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