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