]> git.sesse.net Git - vlc/blob - src/control/media_player.c
3bd4fa6411cb1e76f9ad28002f27f63d3be02072
[vlc] / src / control / media_player.c
1 /*****************************************************************************
2  * media_player.c: Libvlc API Media Instance management functions
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_events.h>
33
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
36 #include <vlc_vout.h>
37
38 #include "libvlc.h"
39
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
43
44 static int
45 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
46                         vlc_value_t oldval, vlc_value_t newval,
47                         void * p_userdata );
48 static int
49 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
50                         vlc_value_t oldval, vlc_value_t newval,
51                         void * p_userdata );
52 static int
53 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
54                      vlc_value_t oldval, vlc_value_t newval,
55                      void * p_userdata );
56
57 static int
58 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
59                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
60
61 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
62
63 /*
64  * Shortcuts
65  */
66
67 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
68 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
69 {
70     libvlc_event_manager_register_event_type(mp->p_event_manager, type, NULL);
71 }
72
73 static inline void lock(libvlc_media_player_t *mp)
74 {
75     vlc_mutex_lock(&mp->object_lock);
76 }
77
78 static inline void unlock(libvlc_media_player_t *mp)
79 {
80     vlc_mutex_unlock(&mp->object_lock);
81 }
82
83 /*
84  * Release the associated input thread.
85  *
86  * Object lock is NOT held.
87  */
88 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
89 {
90     input_thread_t * p_input_thread;
91
92     if( !p_mi || !p_mi->p_input_thread )
93         return;
94
95     p_input_thread = p_mi->p_input_thread;
96
97     var_DelCallback( p_input_thread, "can-seek",
98                      input_seekable_changed, p_mi );
99     var_DelCallback( p_input_thread, "can-pause",
100                     input_pausable_changed, p_mi );
101     var_DelCallback( p_input_thread, "intf-event",
102                      input_event_changed, p_mi );
103
104     /* We owned this one */
105     input_Stop( p_input_thread, b_input_abort );
106
107     vlc_thread_join( p_input_thread );
108
109     assert( p_mi->p_input_resource == NULL );
110     assert( p_input_thread->b_dead );
111     /* Store the input resource for future use. */
112     p_mi->p_input_resource = input_DetachResource( p_input_thread );
113
114     var_Destroy( p_input_thread, "drawable-hwnd" );
115     var_Destroy( p_input_thread, "drawable-xid" );
116     var_Destroy( p_input_thread, "drawable-agl" );
117
118     vlc_object_release( p_input_thread );
119
120     p_mi->p_input_thread = NULL;
121 }
122
123 /*
124  * Retrieve the input thread. Be sure to release the object
125  * once you are done with it. (libvlc Internal)
126  *
127  * Function will lock the object.
128  */
129 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
130                                          libvlc_exception_t *p_e )
131 {
132     input_thread_t *p_input_thread;
133
134     if( !p_mi ) RAISENULL( "Media Instance is NULL" );
135
136     lock(p_mi);
137
138     if( !p_mi->p_input_thread )
139     {
140         unlock(p_mi);
141         RAISENULL( "Input is NULL" );
142     }
143
144     p_input_thread = p_mi->p_input_thread;
145     vlc_object_hold( p_input_thread );
146
147     unlock(p_mi);
148
149     return p_input_thread;
150 }
151
152 /*
153  * Set the internal state of the media_player. (media player Internal)
154  *
155  * Function will lock the media_player.
156  */
157 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state )
158 {    
159     lock(p_mi);
160     p_mi->state = state;
161     libvlc_media_t *media = p_mi->p_md;
162     if (media)
163         libvlc_media_retain(media);
164     unlock(p_mi);
165
166
167     if (media) {
168         // Also set the state of the corresponding media
169         // This is strictly for convenience.
170         libvlc_media_set_state(media, state);
171         
172         libvlc_media_release(media);        
173     }
174 }
175
176 static int
177 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
178                         vlc_value_t oldval, vlc_value_t newval,
179                         void * p_userdata )
180 {
181     VLC_UNUSED(oldval);
182     VLC_UNUSED(p_this);
183     VLC_UNUSED(psz_cmd);
184     libvlc_media_player_t * p_mi = p_userdata;
185     libvlc_event_t event;
186
187     event.type = libvlc_MediaPlayerSeekableChanged;
188     event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
189
190     libvlc_event_send( p_mi->p_event_manager, &event );
191     return VLC_SUCCESS;
192 }
193
194 static int
195 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
196                         vlc_value_t oldval, vlc_value_t newval,
197                         void * p_userdata )
198 {
199     VLC_UNUSED(oldval);
200     VLC_UNUSED(p_this);
201     VLC_UNUSED(psz_cmd);
202     libvlc_media_player_t * p_mi = p_userdata;
203     libvlc_event_t event;
204
205     event.type = libvlc_MediaPlayerPausableChanged;
206     event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
207
208     libvlc_event_send( p_mi->p_event_manager, &event );
209     return VLC_SUCCESS;
210 }
211
212 static int
213 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
214                      vlc_value_t oldval, vlc_value_t newval,
215                      void * p_userdata )
216 {
217     VLC_UNUSED(oldval);
218     input_thread_t * p_input = (input_thread_t *)p_this;
219     libvlc_media_player_t * p_mi = p_userdata;
220     libvlc_event_t event;
221
222     assert( !strcmp( psz_cmd, "intf-event" ) );
223
224     if( newval.i_int == INPUT_EVENT_STATE )
225     {
226         libvlc_state_t libvlc_state;
227
228         switch ( var_GetInteger( p_input, "state" ) )
229         {
230             case INIT_S:
231                 libvlc_state = libvlc_NothingSpecial;
232                 event.type = libvlc_MediaPlayerNothingSpecial;
233                 break;
234             case OPENING_S:
235                 libvlc_state = libvlc_Opening;
236                 event.type = libvlc_MediaPlayerOpening;
237                 break;
238             case PLAYING_S:
239                 libvlc_state = libvlc_Playing;
240                 event.type = libvlc_MediaPlayerPlaying;
241                 break;
242             case PAUSE_S:
243                 libvlc_state = libvlc_Paused;
244                 event.type = libvlc_MediaPlayerPaused;
245                 break;
246             case END_S:
247                 libvlc_state = libvlc_Ended;
248                 event.type = libvlc_MediaPlayerEndReached;
249                 break;
250             case ERROR_S:
251                 libvlc_state = libvlc_Error;
252                 event.type = libvlc_MediaPlayerEncounteredError;
253                 break;
254
255             default:
256                 return VLC_SUCCESS;
257         }
258         
259         set_state( p_mi, libvlc_state );
260         libvlc_event_send( p_mi->p_event_manager, &event );
261     }
262     else if( newval.i_int == INPUT_EVENT_ABORT )
263     {
264         libvlc_state_t libvlc_state = libvlc_Stopped;
265         event.type = libvlc_MediaPlayerStopped;
266
267         set_state( p_mi, libvlc_state );
268         libvlc_event_send( p_mi->p_event_manager, &event );
269     }
270     else if( newval.i_int == INPUT_EVENT_POSITION )
271     {
272         if( var_GetInteger( p_input, "state" ) != PLAYING_S )
273             return VLC_SUCCESS; /* Don't send the position while stopped */
274
275         /* */
276         event.type = libvlc_MediaPlayerPositionChanged;
277         event.u.media_player_position_changed.new_position =
278                                           var_GetFloat( p_input, "position" );
279         libvlc_event_send( p_mi->p_event_manager, &event );
280
281         /* */
282         event.type = libvlc_MediaPlayerTimeChanged;
283         event.u.media_player_time_changed.new_time =
284            from_mtime(var_GetTime( p_input, "time" ));
285         libvlc_event_send( p_mi->p_event_manager, &event );
286     }
287     else if( newval.i_int == INPUT_EVENT_LENGTH )
288     {
289         event.type = libvlc_MediaPlayerLengthChanged;
290         event.u.media_player_length_changed.new_length =
291            from_mtime(var_GetTime( p_input, "length" ));
292         libvlc_event_send( p_mi->p_event_manager, &event );
293     }
294
295     return VLC_SUCCESS;
296
297 }
298
299 /**************************************************************************
300  * Snapshot Taken Event.
301  *
302  * FIXME: This snapshot API interface makes no sense in media_player.
303  *************************************************************************/
304 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
305                               vlc_value_t oldval, vlc_value_t newval, void *p_data )
306 {
307     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
308
309     libvlc_media_player_t *mp = p_data;
310     libvlc_event_t event;
311     event.type = libvlc_MediaPlayerSnapshotTaken;
312     event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
313     libvlc_event_send(mp->p_event_manager, &event);
314
315     return VLC_SUCCESS;
316 }
317
318
319 static void libvlc_media_player_destroy( libvlc_media_player_t * );
320
321
322 /**************************************************************************
323  * Create a Media Instance object.
324  *
325  * Refcount strategy:
326  * - All items created by _new start with a refcount set to 1.
327  * - Accessor _release decrease the refcount by 1, if after that
328  *   operation the refcount is 0, the object is destroyed.
329  * - Accessor _retain increase the refcount by 1 (XXX: to implement)
330  *
331  * Object locking strategy:
332  * - No lock held while in constructor.
333  * - When accessing any member variable this lock is held. (XXX who locks?)
334  * - When attempting to destroy the object the lock is also held.
335  **************************************************************************/
336 libvlc_media_player_t *
337 libvlc_media_player_new( libvlc_instance_t *instance, libvlc_exception_t *e )
338 {
339     libvlc_media_player_t * mp;
340
341     assert(instance);
342
343     mp = malloc(sizeof(libvlc_media_player_t));
344     if (!mp)
345     {
346         libvlc_exception_raise(e);
347         libvlc_printerr("Not enough memory");
348         return NULL;
349     }
350     mp->p_md = NULL;
351     mp->drawable.agl = 0;
352     mp->drawable.xid = 0;
353     mp->drawable.hwnd = NULL;
354     mp->drawable.nsobject = NULL;
355     mp->keyboard_events = mp->mouse_events = 1;
356     mp->state = libvlc_NothingSpecial;
357     mp->p_libvlc_instance = instance;
358     mp->p_input_thread = NULL;
359     mp->p_input_resource = NULL;
360     mp->i_refcount = 1;
361     vlc_mutex_init(&mp->object_lock);
362     mp->p_event_manager = libvlc_event_manager_new(mp, instance, e);
363     if (libvlc_exception_raised(e))
364     {
365         vlc_mutex_destroy(&mp->object_lock);
366         free(mp);
367         return NULL;
368     }
369
370     register_event(mp, NothingSpecial);
371     register_event(mp, Opening);
372     register_event(mp, Buffering);
373     register_event(mp, Playing);
374     register_event(mp, Paused);
375     register_event(mp, Stopped);
376     register_event(mp, Forward);
377     register_event(mp, Backward);
378     register_event(mp, EndReached);
379     register_event(mp, EncounteredError);
380     register_event(mp, SeekableChanged);
381
382     register_event(mp, PositionChanged);
383     register_event(mp, TimeChanged);
384     register_event(mp, LengthChanged);
385     register_event(mp, TitleChanged);
386     register_event(mp, PausableChanged);
387
388     /* Snapshot initialization */
389     register_event(mp, SnapshotTaken);
390
391     register_event(mp, MediaChanged);
392
393     /* Attach a var callback to the global object to provide the glue between
394      * vout_thread that generates the event and media_player that re-emits it
395      * with its own event manager
396      *
397      * FIXME: It's unclear why we want to put this in public API, and why we
398      * want to expose it in such a limiting and ugly way.
399      */
400     var_AddCallback(instance->p_libvlc_int, "snapshot-file", snapshot_was_taken, mp);
401
402     return mp;
403 }
404
405 /**************************************************************************
406  * Create a Media Instance object with a media descriptor.
407  **************************************************************************/
408 libvlc_media_player_t *
409 libvlc_media_player_new_from_media(
410                                     libvlc_media_t * p_md,
411                                     libvlc_exception_t *p_e )
412 {
413     libvlc_media_player_t * p_mi;
414
415     p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
416     if( !p_mi )
417         return NULL;
418
419     libvlc_media_retain( p_md );
420     p_mi->p_md = p_md;
421
422     return p_mi;
423 }
424
425 /**************************************************************************
426  * Destroy a Media Instance object (libvlc internal)
427  *
428  * Warning: No lock held here, but hey, this is internal. Caller must lock.
429  **************************************************************************/
430 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
431 {
432     assert( p_mi );
433
434     /* Detach Callback from the main libvlc object */
435     var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
436                      "snapshot-file", snapshot_was_taken, p_mi );
437
438     /* If the input thread hasn't been already deleted it means
439      * that the owners didn't stop the thread before releasing it. */
440     assert(!p_mi->p_input_thread);
441
442     /* Fallback for those who don't use NDEBUG */
443     if (p_mi->p_input_thread)
444         release_input_thread(p_mi, true);
445
446     if( p_mi->p_input_resource )
447     {
448         input_resource_Delete( p_mi->p_input_resource );
449         p_mi->p_input_resource = NULL;    
450     }
451
452     libvlc_event_manager_release( p_mi->p_event_manager );
453     libvlc_media_release( p_mi->p_md );
454     vlc_mutex_destroy( &p_mi->object_lock );
455     free( p_mi );
456 }
457
458 /**************************************************************************
459  * Release a Media Instance object.
460  *
461  * Function does the locking.
462  **************************************************************************/
463 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
464 {
465     bool destroy;
466
467     assert( p_mi );
468     lock(p_mi);
469     destroy = !--p_mi->i_refcount;
470     unlock(p_mi);
471
472     if( destroy )
473         libvlc_media_player_destroy( p_mi );
474 }
475
476 /**************************************************************************
477  * Retain a Media Instance object.
478  *
479  * Caller must hold the lock.
480  **************************************************************************/
481 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
482 {
483     assert( p_mi );
484
485     lock(p_mi);
486     p_mi->i_refcount++;
487     unlock(p_mi);
488 }
489
490 /**************************************************************************
491  * Set the Media descriptor associated with the instance.
492  *
493  * Enter without lock -- function will lock the object.
494  **************************************************************************/
495 void libvlc_media_player_set_media(
496                             libvlc_media_player_t *p_mi,
497                             libvlc_media_t *p_md,
498                             libvlc_exception_t *p_e )
499 {
500     VLC_UNUSED(p_e);
501
502     lock(p_mi);
503
504     /* FIXME I am not sure if it is a user request or on die(eof/error)
505      * request here */
506     release_input_thread( p_mi,
507                           p_mi->p_input_thread &&
508                           !p_mi->p_input_thread->b_eof &&
509                           !p_mi->p_input_thread->b_error );
510     unlock(p_mi);
511
512     set_state( p_mi, libvlc_NothingSpecial );
513
514     lock(p_mi);
515
516     libvlc_media_release( p_mi->p_md );
517
518     if( !p_md )
519     {
520         p_mi->p_md = NULL;
521         unlock(p_mi);
522         return; /* It is ok to pass a NULL md */
523     }
524
525     libvlc_media_retain( p_md );
526     p_mi->p_md = p_md;
527
528     /* The policy here is to ignore that we were created using a different
529      * libvlc_instance, because we don't really care */
530     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
531
532     unlock(p_mi);
533
534     /* Send an event for the newly available media */
535     libvlc_event_t event;
536     event.type = libvlc_MediaPlayerMediaChanged;
537     event.u.media_player_media_changed.new_media = p_md;
538     libvlc_event_send( p_mi->p_event_manager, &event );
539     
540 }
541
542 /**************************************************************************
543  * Get the Media descriptor associated with the instance.
544  **************************************************************************/
545 libvlc_media_t *
546 libvlc_media_player_get_media(
547                             libvlc_media_player_t *p_mi,
548                             libvlc_exception_t *p_e )
549 {
550     libvlc_media_t *p_m;
551     VLC_UNUSED(p_e);
552
553     lock(p_mi);
554     p_m = p_mi->p_md;
555     if( p_m )
556         libvlc_media_retain( p_mi->p_md );
557     unlock(p_mi);
558     return p_mi->p_md;
559 }
560
561 /**************************************************************************
562  * Get the event Manager.
563  **************************************************************************/
564 libvlc_event_manager_t *
565 libvlc_media_player_event_manager(
566                             libvlc_media_player_t *p_mi,
567                             libvlc_exception_t *p_e )
568 {
569     VLC_UNUSED(p_e);
570
571     return p_mi->p_event_manager;
572 }
573
574 /**************************************************************************
575  * Tell media player to start playing.
576  **************************************************************************/
577 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
578                                  libvlc_exception_t *p_e )
579 {
580     input_thread_t * p_input_thread;
581
582     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
583     {
584         /* A thread already exists, send it a play message */
585         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
586         vlc_object_release( p_input_thread );
587         return;
588     }
589
590     /* Ignore previous exception */
591     libvlc_exception_clear( p_e );
592
593     lock(p_mi);
594
595     if( !p_mi->p_md )
596     {
597         unlock(p_mi);
598         libvlc_exception_raise( p_e );
599         libvlc_printerr( "No associated media descriptor" );
600         return;
601     }
602
603     p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
604                                          p_mi->p_md->p_input_item, NULL, p_mi->p_input_resource );
605
606     if( !p_mi->p_input_thread )
607     {
608         unlock(p_mi);
609         return;
610     }
611
612     p_mi->p_input_resource = NULL;
613     p_input_thread = p_mi->p_input_thread;
614
615     var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
616     if( p_mi->drawable.agl )
617         var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
618
619     var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
620     if( p_mi->drawable.xid )
621         var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
622
623     var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
624     if( p_mi->drawable.hwnd != NULL )
625         var_SetAddress( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
626
627     var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
628     if( p_mi->drawable.nsobject != NULL )
629         var_SetAddress( p_input_thread, "drawable-nsobject", p_mi->drawable.nsobject );
630
631     var_Create( p_input_thread, "keyboard-events", VLC_VAR_BOOL );
632     var_SetBool( p_input_thread, "keyboard-events", p_mi->keyboard_events );
633     var_Create( p_input_thread, "mouse-events", VLC_VAR_BOOL );
634     var_SetBool( p_input_thread, "mouse-events", p_mi->mouse_events );
635
636     var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
637     var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
638     var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
639
640     if( input_Start( p_input_thread ) )
641     {
642         vlc_object_release( p_input_thread );
643         p_mi->p_input_thread = NULL;
644     }
645
646     unlock(p_mi);
647 }
648
649 /**************************************************************************
650  * Pause.
651  **************************************************************************/
652 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
653                                   libvlc_exception_t *p_e )
654 {
655     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
656     if( !p_input_thread )
657         return;
658
659     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
660     if( state == libvlc_Playing || state == libvlc_Buffering )
661     {
662         if( libvlc_media_player_can_pause( p_mi, p_e ) )
663             input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
664         else
665             libvlc_media_player_stop( p_mi, p_e );
666     }
667     else
668         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
669
670     vlc_object_release( p_input_thread );
671 }
672
673 /**************************************************************************
674  * Tells whether the media player is currently playing.
675  *
676  * Enter with lock held.
677  **************************************************************************/
678 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
679                                      libvlc_exception_t *p_e )
680 {
681     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
682     return (libvlc_Playing == state) || (libvlc_Buffering == state);
683 }
684
685 /**************************************************************************
686  * Stop playing.
687  **************************************************************************/
688 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
689                                  libvlc_exception_t *p_e )
690 {
691     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
692
693     lock(p_mi);
694     release_input_thread( p_mi, true ); /* This will stop the input thread */
695     unlock(p_mi);
696
697     /* Force to go to stopped state, in case we were in Ended, or Error
698      * state. */
699     if( state != libvlc_Stopped )
700     {
701         set_state( p_mi, libvlc_Stopped );
702
703         /* Construct and send the event */
704         libvlc_event_t event;
705         event.type = libvlc_MediaPlayerStopped;
706         libvlc_event_send( p_mi->p_event_manager, &event );
707     }
708 }
709
710 /**************************************************************************
711  * set_nsobject
712  **************************************************************************/
713 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
714                                         void * drawable,
715                                         libvlc_exception_t *p_e )
716 {
717     (void) p_e;
718     p_mi->drawable.nsobject = drawable;
719 }
720
721 /**************************************************************************
722  * get_nsobject
723  **************************************************************************/
724 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
725 {
726     return p_mi->drawable.nsobject;
727 }
728
729 /**************************************************************************
730  * set_agl
731  **************************************************************************/
732 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
733                                       uint32_t drawable,
734                                       libvlc_exception_t *p_e )
735 {
736     (void) p_e;
737     p_mi->drawable.agl = drawable;
738 }
739
740 /**************************************************************************
741  * get_agl
742  **************************************************************************/
743 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
744 {
745     return p_mi->drawable.agl;
746 }
747
748 /**************************************************************************
749  * set_xwindow
750  **************************************************************************/
751 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
752                                       uint32_t drawable,
753                                       libvlc_exception_t *p_e )
754 {
755     (void) p_e;
756     p_mi->drawable.xid = drawable;
757 }
758
759 /**************************************************************************
760  * get_xwindow
761  **************************************************************************/
762 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
763 {
764     return p_mi->drawable.xid;
765 }
766
767 /**************************************************************************
768  * set_hwnd
769  **************************************************************************/
770 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
771                                    void *drawable,
772                                    libvlc_exception_t *p_e )
773 {
774     (void) p_e;
775     p_mi->drawable.hwnd = drawable;
776 }
777
778 /**************************************************************************
779  * get_hwnd
780  **************************************************************************/
781 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
782 {
783     return p_mi->drawable.hwnd;
784 }
785
786 /**************************************************************************
787  * Getters for stream information
788  **************************************************************************/
789 libvlc_time_t libvlc_media_player_get_length(
790                              libvlc_media_player_t *p_mi,
791                              libvlc_exception_t *p_e )
792 {
793     input_thread_t *p_input_thread;
794     libvlc_time_t i_time;
795
796     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
797     if( !p_input_thread )
798         return -1;
799
800     i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
801     vlc_object_release( p_input_thread );
802
803     return i_time;
804 }
805
806 libvlc_time_t libvlc_media_player_get_time(
807                                    libvlc_media_player_t *p_mi,
808                                    libvlc_exception_t *p_e )
809 {
810     input_thread_t *p_input_thread;
811     libvlc_time_t i_time;
812
813     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
814     if( !p_input_thread )
815         return -1;
816
817     i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
818     vlc_object_release( p_input_thread );
819     return i_time;
820 }
821
822 void libvlc_media_player_set_time(
823                                  libvlc_media_player_t *p_mi,
824                                  libvlc_time_t i_time,
825                                  libvlc_exception_t *p_e )
826 {
827     input_thread_t *p_input_thread;
828
829     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
830     if( !p_input_thread )
831         return;
832
833     var_SetTime( p_input_thread, "time", to_mtime(i_time) );
834     vlc_object_release( p_input_thread );
835 }
836
837 void libvlc_media_player_set_position(
838                                 libvlc_media_player_t *p_mi,
839                                 float position,
840                                 libvlc_exception_t *p_e )
841 {
842     input_thread_t *p_input_thread;
843
844     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
845     if( !p_input_thread )
846         return;
847
848     var_SetFloat( p_input_thread, "position", position );
849     vlc_object_release( p_input_thread );
850 }
851
852 float libvlc_media_player_get_position(
853                                  libvlc_media_player_t *p_mi,
854                                  libvlc_exception_t *p_e )
855 {
856     input_thread_t *p_input_thread;
857     float f_position;
858
859     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
860     if( !p_input_thread )
861         return -1.0;
862
863     f_position = var_GetFloat( p_input_thread, "position" );
864     vlc_object_release( p_input_thread );
865
866     return f_position;
867 }
868
869 void libvlc_media_player_set_chapter(
870                                  libvlc_media_player_t *p_mi,
871                                  int chapter,
872                                  libvlc_exception_t *p_e )
873 {
874     input_thread_t *p_input_thread;
875
876     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
877     if( !p_input_thread )
878         return;
879
880     var_SetInteger( p_input_thread, "chapter", chapter );
881     vlc_object_release( p_input_thread );
882 }
883
884 int libvlc_media_player_get_chapter(
885                                  libvlc_media_player_t *p_mi,
886                                  libvlc_exception_t *p_e )
887 {
888     input_thread_t *p_input_thread;
889     int i_chapter;
890
891     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
892     if( !p_input_thread )
893         return -1;
894
895     i_chapter = var_GetInteger( p_input_thread, "chapter" );
896     vlc_object_release( p_input_thread );
897
898     return i_chapter;
899 }
900
901 int libvlc_media_player_get_chapter_count(
902                                  libvlc_media_player_t *p_mi,
903                                  libvlc_exception_t *p_e )
904 {
905     input_thread_t *p_input_thread;
906     vlc_value_t val;
907
908     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
909     if( !p_input_thread )
910         return -1;
911
912     var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
913     vlc_object_release( p_input_thread );
914
915     return val.i_int;
916 }
917
918 int libvlc_media_player_get_chapter_count_for_title(
919                                  libvlc_media_player_t *p_mi,
920                                  int i_title,
921                                  libvlc_exception_t *p_e )
922 {
923     input_thread_t *p_input_thread;
924     vlc_value_t val;
925
926     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
927     if( !p_input_thread )
928         return -1;
929
930     char *psz_name;
931     if( asprintf( &psz_name,  "title %2i", i_title ) == -1 )
932     {
933         vlc_object_release( p_input_thread );
934         return -1;
935     }
936     var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
937     vlc_object_release( p_input_thread );
938     free( psz_name );
939
940     return val.i_int;
941 }
942
943 void libvlc_media_player_set_title(
944                                  libvlc_media_player_t *p_mi,
945                                  int i_title,
946                                  libvlc_exception_t *p_e )
947 {
948     input_thread_t *p_input_thread;
949
950     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
951     if( !p_input_thread )
952         return;
953
954     var_SetInteger( p_input_thread, "title", i_title );
955     vlc_object_release( p_input_thread );
956
957     //send event
958     libvlc_event_t event;
959     event.type = libvlc_MediaPlayerTitleChanged;
960     event.u.media_player_title_changed.new_title = i_title;
961     libvlc_event_send( p_mi->p_event_manager, &event );
962 }
963
964 int libvlc_media_player_get_title(
965                                  libvlc_media_player_t *p_mi,
966                                  libvlc_exception_t *p_e )
967 {
968     input_thread_t *p_input_thread;
969     int i_title;
970
971     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
972     if( !p_input_thread )
973         return -1;
974
975     i_title = var_GetInteger( p_input_thread, "title" );
976     vlc_object_release( p_input_thread );
977
978     return i_title;
979 }
980
981 int libvlc_media_player_get_title_count(
982                                  libvlc_media_player_t *p_mi,
983                                  libvlc_exception_t *p_e )
984 {
985     input_thread_t *p_input_thread;
986     vlc_value_t val;
987
988     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
989     if( !p_input_thread )
990         return -1;
991
992     var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
993     vlc_object_release( p_input_thread );
994
995     return val.i_int;
996 }
997
998 void libvlc_media_player_next_chapter(
999                                  libvlc_media_player_t *p_mi,
1000                                  libvlc_exception_t *p_e )
1001 {
1002     input_thread_t *p_input_thread;
1003
1004     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1005     if( !p_input_thread )
1006         return;
1007
1008     int i_type = var_Type( p_input_thread, "next-chapter" );
1009     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1010                             "next-chapter":"next-title", true );
1011
1012     vlc_object_release( p_input_thread );
1013 }
1014
1015 void libvlc_media_player_previous_chapter(
1016                                  libvlc_media_player_t *p_mi,
1017                                  libvlc_exception_t *p_e )
1018 {
1019     input_thread_t *p_input_thread;
1020
1021     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1022     if( !p_input_thread )
1023         return;
1024
1025     int i_type = var_Type( p_input_thread, "next-chapter" );
1026     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1027                             "prev-chapter":"prev-title", true );
1028
1029     vlc_object_release( p_input_thread );
1030 }
1031
1032 float libvlc_media_player_get_fps(
1033                                  libvlc_media_player_t *p_mi,
1034                                  libvlc_exception_t *p_e)
1035 {
1036     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1037     double f_fps = 0.0;
1038
1039     if( p_input_thread )
1040     {
1041         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1042             f_fps = 0.0;
1043         vlc_object_release( p_input_thread );
1044     }
1045     return f_fps;
1046 }
1047
1048 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1049                                      libvlc_exception_t *p_e)
1050 {
1051     bool b_will_play;
1052     input_thread_t *p_input_thread =
1053                             libvlc_get_input_thread ( p_mi, p_e);
1054     if ( !p_input_thread )
1055         return false;
1056
1057     b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1058     vlc_object_release( p_input_thread );
1059
1060     return b_will_play;
1061 }
1062
1063 void libvlc_media_player_set_rate(
1064                                  libvlc_media_player_t *p_mi,
1065                                  float rate,
1066                                  libvlc_exception_t *p_e )
1067 {
1068     input_thread_t *p_input_thread;
1069     bool b_can_rewind;
1070
1071     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1072     if( !p_input_thread )
1073         return;
1074
1075     b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1076     if( (rate < 0.0) && !b_can_rewind )
1077     {
1078         vlc_object_release( p_input_thread );
1079         libvlc_exception_raise( p_e );
1080         libvlc_printerr( "Invalid playback rate" );
1081         return;
1082     }
1083
1084     var_SetFloat( p_input_thread, "rate", rate );
1085     vlc_object_release( p_input_thread );
1086 }
1087
1088 float libvlc_media_player_get_rate(
1089                                  libvlc_media_player_t *p_mi,
1090                                  libvlc_exception_t *p_e )
1091 {
1092     input_thread_t *p_input_thread;
1093     float f_rate;
1094     bool b_can_rewind;
1095
1096     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1097     if( !p_input_thread )
1098         return 0.0;  /* rate < 0 indicates rewind */
1099
1100     f_rate = var_GetFloat( p_input_thread, "rate" );
1101     b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1102     /* FIXME: why are negative values forbidden ?? (rewinding) */
1103     if( f_rate < 0 && !b_can_rewind )
1104     {
1105         vlc_object_release( p_input_thread );
1106         return 0.0;
1107     }
1108     vlc_object_release( p_input_thread );
1109
1110     return f_rate;
1111 }
1112
1113 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1114 {
1115     VLC_UNUSED(p_e);
1116     lock(p_mi);
1117     libvlc_state_t state = p_mi->state;
1118     unlock(p_mi);
1119     return state;
1120 }
1121
1122 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1123                                        libvlc_exception_t *p_e )
1124 {
1125     input_thread_t *p_input_thread;
1126     bool b_seekable;
1127
1128     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1129     if ( !p_input_thread )
1130     {
1131         /* We do return the right value, no need to throw an exception */
1132         clear_if_needed(p_e);
1133         return false;
1134     }
1135     b_seekable = var_GetBool( p_input_thread, "can-seek" );
1136     vlc_object_release( p_input_thread );
1137
1138     return b_seekable;
1139 }
1140
1141 /* internal function, used by audio, video */
1142 libvlc_track_description_t *
1143         libvlc_get_track_description( libvlc_media_player_t *p_mi,
1144                                       const char *psz_variable,
1145                                       libvlc_exception_t *p_e )
1146 {
1147     input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1148     libvlc_track_description_t *p_track_description = NULL,
1149                                *p_actual, *p_previous;
1150
1151     if( !p_input )
1152         return NULL;
1153
1154     vlc_value_t val_list, text_list;
1155     var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1156
1157     /* no tracks */
1158     if( val_list.p_list->i_count <= 0 )
1159         goto end;
1160
1161     p_track_description = ( libvlc_track_description_t * )
1162         malloc( sizeof( libvlc_track_description_t ) );
1163     if ( !p_track_description )
1164     {
1165         libvlc_exception_raise( p_e );
1166         libvlc_printerr( "Not enough memory" );
1167         goto end;
1168     }
1169     p_actual = p_track_description;
1170     p_previous = NULL;
1171     for( int i = 0; i < val_list.p_list->i_count; i++ )
1172     {
1173         if( !p_actual )
1174         {
1175             p_actual = ( libvlc_track_description_t * )
1176                 malloc( sizeof( libvlc_track_description_t ) );
1177             if ( !p_actual )
1178             {
1179                 libvlc_track_description_release( p_track_description );
1180                 libvlc_exception_raise( p_e );
1181                 libvlc_printerr( "Not enough memory" );
1182                 goto end;
1183             }
1184         }
1185         p_actual->i_id = val_list.p_list->p_values[i].i_int;
1186         p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1187         p_actual->p_next = NULL;
1188         if( p_previous )
1189             p_previous->p_next = p_actual;
1190         p_previous = p_actual;
1191         p_actual =  NULL;
1192     }
1193
1194 end:
1195     var_FreeList( &val_list, &text_list );
1196     vlc_object_release( p_input );
1197
1198     return p_track_description;
1199 }
1200
1201 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1202 {
1203     libvlc_track_description_t *p_actual, *p_before;
1204     p_actual = p_td;
1205
1206     while ( p_actual )
1207     {
1208         free( p_actual->psz_name );
1209         p_before = p_actual;
1210         p_actual = p_before->p_next;
1211         free( p_before );
1212     }
1213 }
1214
1215 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1216                                      libvlc_exception_t *p_e )
1217 {
1218     input_thread_t *p_input_thread;
1219     bool b_can_pause;
1220
1221     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1222     if ( !p_input_thread )
1223     {
1224         /* We do return the right value, no need to throw an exception */
1225         clear_if_needed(p_e);
1226         return false;
1227     }
1228     b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1229     vlc_object_release( p_input_thread );
1230
1231     return b_can_pause;
1232 }
1233
1234 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1235 {
1236     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1237     if( p_input_thread != NULL )
1238     {
1239         var_TriggerCallback( p_input_thread, "frame-next" );
1240         vlc_object_release( p_input_thread );
1241     }
1242     else
1243     {
1244         libvlc_exception_raise( p_e );
1245         libvlc_printerr( "No active input" );
1246     }
1247 }