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