lib: map the position enum to the correct subpicture alignment bitmasks
[vlc.git] / lib / media_player.c
1 /*****************************************************************************
2  * media_player.c: Libvlc API Media Instance management functions
3  *****************************************************************************
4  * Copyright (C) 2005-2011 VLC authors and VideoLAN
5  *
6  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <assert.h>
28
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_media.h>
31 #include <vlc/libvlc_events.h>
32
33 #include <vlc_demux.h>
34 #include <vlc_input.h>
35 #include <vlc_vout.h>
36 #include <vlc_aout.h>
37 #include <vlc_keys.h>
38
39 #include "libvlc_internal.h"
40 #include "media_internal.h" // libvlc_media_set_state()
41 #include "media_player_internal.h"
42
43 static int
44 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
45                         vlc_value_t oldval, vlc_value_t newval,
46                         void * p_userdata );
47 static int
48 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
49                         vlc_value_t oldval, vlc_value_t newval,
50                         void * p_userdata );
51 static int
52 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
53                         vlc_value_t oldval, vlc_value_t newval,
54                         void * p_userdata );
55 static int
56 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
57                      vlc_value_t oldval, vlc_value_t newval,
58                      void * p_userdata );
59
60 static int
61 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
62                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
63
64 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
65
66 /*
67  * Shortcuts
68  */
69
70 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
71 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
72 {
73     libvlc_event_manager_register_event_type(mp->p_event_manager, type);
74 }
75
76 /*
77  * The input lock protects the input and input resource pointer.
78  * It MUST NOT be used from callbacks.
79  *
80  * The object lock protects the reset, namely the media and the player state.
81  * It can, and usually needs to be taken from callbacks.
82  * The object lock can be acquired under the input lock... and consequently
83  * the opposite order is STRICTLY PROHIBITED.
84  */
85 static inline void lock(libvlc_media_player_t *mp)
86 {
87     vlc_mutex_lock(&mp->object_lock);
88 }
89
90 static inline void unlock(libvlc_media_player_t *mp)
91 {
92     vlc_mutex_unlock(&mp->object_lock);
93 }
94
95 static inline void lock_input(libvlc_media_player_t *mp)
96 {
97     vlc_mutex_lock(&mp->input.lock);
98 }
99
100 static inline void unlock_input(libvlc_media_player_t *mp)
101 {
102     vlc_mutex_unlock(&mp->input.lock);
103 }
104
105 /*
106  * Release the associated input thread.
107  *
108  * Object lock is NOT held.
109  * Input lock is held or instance is being destroyed.
110  */
111 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
112 {
113     assert( p_mi );
114
115     input_thread_t *p_input_thread = p_mi->input.p_thread;
116     if( !p_input_thread )
117         return;
118     p_mi->input.p_thread = NULL;
119
120     var_DelCallback( p_input_thread, "can-seek",
121                      input_seekable_changed, p_mi );
122     var_DelCallback( p_input_thread, "can-pause",
123                     input_pausable_changed, p_mi );
124     var_DelCallback( p_input_thread, "program-scrambled",
125                     input_scrambled_changed, p_mi );
126     var_DelCallback( p_input_thread, "intf-event",
127                      input_event_changed, p_mi );
128
129     /* We owned this one */
130     input_Stop( p_input_thread, b_input_abort );
131     input_Close( 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_scrambled_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     VLC_UNUSED(p_this);
227     VLC_UNUSED(psz_cmd);
228     libvlc_media_player_t * p_mi = p_userdata;
229     libvlc_event_t event;
230
231     event.type = libvlc_MediaPlayerScrambledChanged;
232     event.u.media_player_scrambled_changed.new_scrambled = newval.b_bool;
233
234     libvlc_event_send( p_mi->p_event_manager, &event );
235     return VLC_SUCCESS;
236 }
237
238 static int
239 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
240                      vlc_value_t oldval, vlc_value_t newval,
241                      void * p_userdata )
242 {
243     VLC_UNUSED(oldval);
244     input_thread_t * p_input = (input_thread_t *)p_this;
245     libvlc_media_player_t * p_mi = p_userdata;
246     libvlc_event_t event;
247
248     assert( !strcmp( psz_cmd, "intf-event" ) );
249
250     if( newval.i_int == INPUT_EVENT_STATE )
251     {
252         libvlc_state_t libvlc_state;
253
254         switch ( var_GetInteger( p_input, "state" ) )
255         {
256             case INIT_S:
257                 libvlc_state = libvlc_NothingSpecial;
258                 event.type = libvlc_MediaPlayerNothingSpecial;
259                 break;
260             case OPENING_S:
261                 libvlc_state = libvlc_Opening;
262                 event.type = libvlc_MediaPlayerOpening;
263                 break;
264             case PLAYING_S:
265                 libvlc_state = libvlc_Playing;
266                 event.type = libvlc_MediaPlayerPlaying;
267                 break;
268             case PAUSE_S:
269                 libvlc_state = libvlc_Paused;
270                 event.type = libvlc_MediaPlayerPaused;
271                 break;
272             case END_S:
273                 libvlc_state = libvlc_Ended;
274                 event.type = libvlc_MediaPlayerEndReached;
275                 break;
276             case ERROR_S:
277                 libvlc_state = libvlc_Error;
278                 event.type = libvlc_MediaPlayerEncounteredError;
279                 break;
280
281             default:
282                 return VLC_SUCCESS;
283         }
284
285         set_state( p_mi, libvlc_state, false );
286         libvlc_event_send( p_mi->p_event_manager, &event );
287     }
288     else if( newval.i_int == INPUT_EVENT_ABORT )
289     {
290         libvlc_state_t libvlc_state = libvlc_Stopped;
291         event.type = libvlc_MediaPlayerStopped;
292
293         set_state( p_mi, libvlc_state, false );
294         libvlc_event_send( p_mi->p_event_manager, &event );
295     }
296     else if( newval.i_int == INPUT_EVENT_POSITION )
297     {
298         if( var_GetInteger( p_input, "state" ) != PLAYING_S )
299             return VLC_SUCCESS; /* Don't send the position while stopped */
300
301         /* */
302         event.type = libvlc_MediaPlayerPositionChanged;
303         event.u.media_player_position_changed.new_position =
304                                           var_GetFloat( p_input, "position" );
305         libvlc_event_send( p_mi->p_event_manager, &event );
306
307         /* */
308         event.type = libvlc_MediaPlayerTimeChanged;
309         event.u.media_player_time_changed.new_time =
310            from_mtime(var_GetTime( p_input, "time" ));
311         libvlc_event_send( p_mi->p_event_manager, &event );
312     }
313     else if( newval.i_int == INPUT_EVENT_LENGTH )
314     {
315         event.type = libvlc_MediaPlayerLengthChanged;
316         event.u.media_player_length_changed.new_length =
317            from_mtime(var_GetTime( p_input, "length" ));
318         libvlc_event_send( p_mi->p_event_manager, &event );
319     }
320     else if( newval.i_int == INPUT_EVENT_CACHE )
321     {
322         event.type = libvlc_MediaPlayerBuffering;
323         event.u.media_player_buffering.new_cache = (int)(100 *
324             var_GetFloat( p_input, "cache" ));
325         libvlc_event_send( p_mi->p_event_manager, &event );
326     }
327     else if( newval.i_int == INPUT_EVENT_VOUT )
328     {
329         vout_thread_t **pp_vout;
330         size_t i_vout;
331         if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
332         {
333             i_vout  = 0;
334         }
335         else
336         {
337             for( size_t i = 0; i < i_vout; i++ )
338                 vlc_object_release( pp_vout[i] );
339             free( pp_vout );
340         }
341
342         event.type = libvlc_MediaPlayerVout;
343         event.u.media_player_vout.new_count = i_vout;
344         libvlc_event_send( p_mi->p_event_manager, &event );
345     }
346
347     return VLC_SUCCESS;
348 }
349
350 /**************************************************************************
351  * Snapshot Taken Event.
352  *
353  * FIXME: This snapshot API interface makes no sense in media_player.
354  *************************************************************************/
355 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
356                               vlc_value_t oldval, vlc_value_t newval, void *p_data )
357 {
358     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
359
360     libvlc_media_player_t *mp = p_data;
361     libvlc_event_t event;
362     event.type = libvlc_MediaPlayerSnapshotTaken;
363     event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
364     libvlc_event_send(mp->p_event_manager, &event);
365
366     return VLC_SUCCESS;
367 }
368
369 /* */
370 static void libvlc_media_player_destroy( libvlc_media_player_t * );
371
372
373 /**************************************************************************
374  * Create a Media Instance object.
375  *
376  * Refcount strategy:
377  * - All items created by _new start with a refcount set to 1.
378  * - Accessor _release decrease the refcount by 1, if after that
379  *   operation the refcount is 0, the object is destroyed.
380  * - Accessor _retain increase the refcount by 1 (XXX: to implement)
381  *
382  * Object locking strategy:
383  * - No lock held while in constructor.
384  * - When accessing any member variable this lock is held. (XXX who locks?)
385  * - When attempting to destroy the object the lock is also held.
386  **************************************************************************/
387 libvlc_media_player_t *
388 libvlc_media_player_new( libvlc_instance_t *instance )
389 {
390     libvlc_media_player_t * mp;
391
392     assert(instance);
393
394     mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
395     if (unlikely(mp == NULL))
396     {
397         libvlc_printerr("Not enough memory");
398         return NULL;
399     }
400
401     /* Input */
402     var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
403
404     /* Video */
405     var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
406     var_Create (mp, "window", VLC_VAR_STRING);
407     var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
408     var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
409     var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
410     var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
411     var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
412     var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
413     var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
414     var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
415     var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
416     var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
417     var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
418 #if defined (_WIN32) || defined (__OS2__)
419     var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
420 #endif
421 #ifdef __APPLE__
422     var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
423     var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
424 #endif
425
426     var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
427     var_SetBool (mp, "keyboard-events", true);
428     var_Create (mp, "mouse-events", VLC_VAR_BOOL);
429     var_SetBool (mp, "mouse-events", true);
430
431     var_Create (mp, "fullscreen", VLC_VAR_BOOL);
432     var_Create (mp, "autoscale", VLC_VAR_BOOL);
433     var_SetBool (mp, "autoscale", true);
434     var_Create (mp, "scale", VLC_VAR_FLOAT);
435     var_SetFloat (mp, "scale", 1.);
436     var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
437     var_Create (mp, "crop", VLC_VAR_STRING);
438     var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
439     var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
440
441     var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
442
443     var_Create (mp, "marq-marquee", VLC_VAR_STRING);
444     var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
445     var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
446     var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
447     var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
448     var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
449     var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
450     var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
451     var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
452
453     var_Create (mp, "logo-file", VLC_VAR_STRING);
454     var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
455     var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
456     var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
457     var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
458     var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
459     var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
460
461     var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
462     var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
463     var_Create (mp, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
464     var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
465     var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
466
467      /* Audio */
468     var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
469     var_Create (mp, "mute", VLC_VAR_BOOL);
470     var_Create (mp, "volume", VLC_VAR_FLOAT);
471     var_Create (mp, "corks", VLC_VAR_INTEGER);
472     var_Create (mp, "audio-filter", VLC_VAR_STRING);
473     var_Create (mp, "amem-data", VLC_VAR_ADDRESS);
474     var_Create (mp, "amem-setup", VLC_VAR_ADDRESS);
475     var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
476     var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
477     var_Create (mp, "amem-pause", VLC_VAR_ADDRESS);
478     var_Create (mp, "amem-resume", VLC_VAR_ADDRESS);
479     var_Create (mp, "amem-flush", VLC_VAR_ADDRESS);
480     var_Create (mp, "amem-drain", VLC_VAR_ADDRESS);
481     var_Create (mp, "amem-set-volume", VLC_VAR_ADDRESS);
482     var_Create (mp, "amem-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
483     var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
484     var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
485
486     /* Video Title */
487     var_Create (mp, "video-title-show", VLC_VAR_BOOL);
488     var_Create (mp, "video-title-position", VLC_VAR_INTEGER);
489     var_Create (mp, "video-title-timeout", VLC_VAR_INTEGER);
490
491     /* Equalizer */
492     var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
493     var_Create (mp, "equalizer-vlcfreqs", VLC_VAR_BOOL);
494     var_Create (mp, "equalizer-bands", VLC_VAR_STRING);
495
496     mp->p_md = NULL;
497     mp->state = libvlc_NothingSpecial;
498     mp->p_libvlc_instance = instance;
499     mp->input.p_thread = NULL;
500     mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
501     if (unlikely(mp->input.p_resource == NULL))
502     {
503         vlc_object_release(mp);
504         return NULL;
505     }
506     audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
507     if( aout != NULL )
508         input_resource_PutAout(mp->input.p_resource, aout);
509
510     vlc_mutex_init (&mp->input.lock);
511     mp->i_refcount = 1;
512     mp->p_event_manager = libvlc_event_manager_new(mp, instance);
513     if (unlikely(mp->p_event_manager == NULL))
514     {
515         input_resource_Release(mp->input.p_resource);
516         vlc_object_release(mp);
517         return NULL;
518     }
519     vlc_mutex_init(&mp->object_lock);
520
521     register_event(mp, NothingSpecial);
522     register_event(mp, Opening);
523     register_event(mp, Buffering);
524     register_event(mp, Playing);
525     register_event(mp, Paused);
526     register_event(mp, Stopped);
527     register_event(mp, Forward);
528     register_event(mp, Backward);
529     register_event(mp, EndReached);
530     register_event(mp, EncounteredError);
531     register_event(mp, SeekableChanged);
532
533     register_event(mp, PositionChanged);
534     register_event(mp, TimeChanged);
535     register_event(mp, LengthChanged);
536     register_event(mp, TitleChanged);
537     register_event(mp, PausableChanged);
538
539     register_event(mp, Vout);
540     register_event(mp, ScrambledChanged);
541
542     /* Snapshot initialization */
543     register_event(mp, SnapshotTaken);
544
545     register_event(mp, MediaChanged);
546
547     /* Attach a var callback to the global object to provide the glue between
548      * vout_thread that generates the event and media_player that re-emits it
549      * with its own event manager
550      *
551      * FIXME: It's unclear why we want to put this in public API, and why we
552      * want to expose it in such a limiting and ugly way.
553      */
554     var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
555
556     libvlc_retain(instance);
557     return mp;
558 }
559
560 /**************************************************************************
561  * Create a Media Instance object with a media descriptor.
562  **************************************************************************/
563 libvlc_media_player_t *
564 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
565 {
566     libvlc_media_player_t * p_mi;
567
568     p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
569     if( !p_mi )
570         return NULL;
571
572     libvlc_media_retain( p_md );
573     p_mi->p_md = p_md;
574
575     return p_mi;
576 }
577
578 /**************************************************************************
579  * Destroy a Media Instance object (libvlc internal)
580  *
581  * Warning: No lock held here, but hey, this is internal. Caller must lock.
582  **************************************************************************/
583 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
584 {
585     assert( p_mi );
586
587     /* Detach Callback from the main libvlc object */
588     var_DelCallback( p_mi->p_libvlc,
589                      "snapshot-file", snapshot_was_taken, p_mi );
590
591     /* No need for lock_input() because no other threads knows us anymore */
592     if( p_mi->input.p_thread )
593         release_input_thread(p_mi, true);
594     input_resource_Terminate( p_mi->input.p_resource );
595     input_resource_Release( p_mi->input.p_resource );
596     vlc_mutex_destroy( &p_mi->input.lock );
597
598     libvlc_event_manager_release( p_mi->p_event_manager );
599     libvlc_media_release( p_mi->p_md );
600     vlc_mutex_destroy( &p_mi->object_lock );
601
602     libvlc_instance_t *instance = p_mi->p_libvlc_instance;
603     vlc_object_release( p_mi );
604     libvlc_release(instance);
605 }
606
607 /**************************************************************************
608  * Release a Media Instance object.
609  *
610  * Function does the locking.
611  **************************************************************************/
612 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
613 {
614     bool destroy;
615
616     assert( p_mi );
617     lock(p_mi);
618     destroy = !--p_mi->i_refcount;
619     unlock(p_mi);
620
621     if( destroy )
622         libvlc_media_player_destroy( p_mi );
623 }
624
625 /**************************************************************************
626  * Retain a Media Instance object.
627  *
628  * Caller must hold the lock.
629  **************************************************************************/
630 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
631 {
632     assert( p_mi );
633
634     lock(p_mi);
635     p_mi->i_refcount++;
636     unlock(p_mi);
637 }
638
639 /**************************************************************************
640  * Set the Media descriptor associated with the instance.
641  *
642  * Enter without lock -- function will lock the object.
643  **************************************************************************/
644 void libvlc_media_player_set_media(
645                             libvlc_media_player_t *p_mi,
646                             libvlc_media_t *p_md )
647 {
648     lock_input(p_mi);
649
650     /* FIXME I am not sure if it is a user request or on die(eof/error)
651      * request here */
652     release_input_thread( p_mi,
653                           p_mi->input.p_thread &&
654                           !p_mi->input.p_thread->b_eof &&
655                           !p_mi->input.p_thread->b_error );
656
657     lock( p_mi );
658     set_state( p_mi, libvlc_NothingSpecial, true );
659     unlock_input( p_mi );
660
661     libvlc_media_release( p_mi->p_md );
662
663     if( !p_md )
664     {
665         p_mi->p_md = NULL;
666         unlock(p_mi);
667         return; /* It is ok to pass a NULL md */
668     }
669
670     libvlc_media_retain( p_md );
671     p_mi->p_md = p_md;
672
673     /* The policy here is to ignore that we were created using a different
674      * libvlc_instance, because we don't really care */
675     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
676
677     unlock(p_mi);
678
679     /* Send an event for the newly available media */
680     libvlc_event_t event;
681     event.type = libvlc_MediaPlayerMediaChanged;
682     event.u.media_player_media_changed.new_media = p_md;
683     libvlc_event_send( p_mi->p_event_manager, &event );
684
685 }
686
687 /**************************************************************************
688  * Get the Media descriptor associated with the instance.
689  **************************************************************************/
690 libvlc_media_t *
691 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
692 {
693     libvlc_media_t *p_m;
694
695     lock( p_mi );
696     p_m = p_mi->p_md;
697     if( p_m )
698         libvlc_media_retain( p_m );
699     unlock( p_mi );
700
701     return p_m;
702 }
703
704 /**************************************************************************
705  * Get the event Manager.
706  **************************************************************************/
707 libvlc_event_manager_t *
708 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
709 {
710     return p_mi->p_event_manager;
711 }
712
713 /**************************************************************************
714  * Tell media player to start playing.
715  **************************************************************************/
716 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
717 {
718     lock_input( p_mi );
719
720     input_thread_t *p_input_thread = p_mi->input.p_thread;
721     if( p_input_thread )
722     {
723         /* A thread already exists, send it a play message */
724         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
725         unlock_input( p_mi );
726         return 0;
727     }
728
729     /* Ignore previous exception */
730     lock(p_mi);
731
732     if( !p_mi->p_md )
733     {
734         unlock(p_mi);
735         unlock_input( p_mi );
736         libvlc_printerr( "No associated media descriptor" );
737         return -1;
738     }
739
740     p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
741                                    p_mi->input.p_resource );
742     unlock(p_mi);
743     if( !p_input_thread )
744     {
745         unlock_input(p_mi);
746         libvlc_printerr( "Not enough memory" );
747         return -1;
748     }
749
750     var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
751     var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
752     var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
753     var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
754
755     if( input_Start( p_input_thread ) )
756     {
757         unlock_input(p_mi);
758         var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
759         var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
760         var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
761         var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
762         vlc_object_release( p_input_thread );
763         libvlc_printerr( "Input initialization failure" );
764         return -1;
765     }
766     p_mi->input.p_thread = p_input_thread;
767     unlock_input(p_mi);
768     return 0;
769 }
770
771 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
772 {
773     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
774     if( !p_input_thread )
775         return;
776
777     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
778     if( state == libvlc_Playing || state == libvlc_Buffering )
779     {
780         if( paused )
781         {
782             if( libvlc_media_player_can_pause( p_mi ) )
783                 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
784             else
785                 libvlc_media_player_stop( p_mi );
786         }
787     }
788     else
789     {
790         if( !paused )
791             input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
792     }
793
794     vlc_object_release( p_input_thread );
795 }
796
797 /**************************************************************************
798  * Toggle pause.
799  **************************************************************************/
800 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
801 {
802     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
803     bool playing = (state == libvlc_Playing || state == libvlc_Buffering);
804
805     libvlc_media_player_set_pause( p_mi, playing );
806 }
807
808 /**************************************************************************
809  * Tells whether the media player is currently playing.
810  *
811  * Enter with lock held.
812  **************************************************************************/
813 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
814 {
815     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
816     return (libvlc_Playing == state) || (libvlc_Buffering == state);
817 }
818
819 /**************************************************************************
820  * Stop playing.
821  **************************************************************************/
822 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
823 {
824     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
825
826     lock_input(p_mi);
827     release_input_thread( p_mi, true ); /* This will stop the input thread */
828
829     /* Force to go to stopped state, in case we were in Ended, or Error
830      * state. */
831     if( state != libvlc_Stopped )
832     {
833         set_state( p_mi, libvlc_Stopped, false );
834
835         /* Construct and send the event */
836         libvlc_event_t event;
837         event.type = libvlc_MediaPlayerStopped;
838         libvlc_event_send( p_mi->p_event_manager, &event );
839     }
840
841     input_resource_Terminate( p_mi->input.p_resource );
842     unlock_input(p_mi);
843 }
844
845
846 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
847     void *(*lock_cb) (void *, void **),
848     void (*unlock_cb) (void *, void *, void *const *),
849     void (*display_cb) (void *, void *),
850     void *opaque )
851 {
852     var_SetAddress( mp, "vmem-lock", lock_cb );
853     var_SetAddress( mp, "vmem-unlock", unlock_cb );
854     var_SetAddress( mp, "vmem-display", display_cb );
855     var_SetAddress( mp, "vmem-data", opaque );
856     var_SetString( mp, "vout", "vmem" );
857 }
858
859 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
860                                         libvlc_video_format_cb setup,
861                                         libvlc_video_cleanup_cb cleanup )
862 {
863     var_SetAddress( mp, "vmem-setup", setup );
864     var_SetAddress( mp, "vmem-cleanup", cleanup );
865 }
866
867 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
868                               unsigned width, unsigned height, unsigned pitch )
869 {
870     var_SetString( mp, "vmem-chroma", chroma );
871     var_SetInteger( mp, "vmem-width", width );
872     var_SetInteger( mp, "vmem-height", height );
873     var_SetInteger( mp, "vmem-pitch", pitch );
874 }
875
876 /**************************************************************************
877  * set_nsobject
878  **************************************************************************/
879 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
880                                         void * drawable )
881 {
882     assert (p_mi != NULL);
883 #ifdef __APPLE__
884     var_SetAddress (p_mi, "drawable-nsobject", drawable);
885 #else
886     (void) p_mi; (void)drawable;
887 #endif
888 }
889
890 /**************************************************************************
891  * get_nsobject
892  **************************************************************************/
893 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
894 {
895     assert (p_mi != NULL);
896 #ifdef __APPLE__
897     return var_GetAddress (p_mi, "drawable-nsobject");
898 #else
899     return NULL;
900 #endif
901 }
902
903 /**************************************************************************
904  * set_agl
905  **************************************************************************/
906 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
907                                   uint32_t drawable )
908 {
909 #ifdef __APPLE__
910     var_SetInteger (p_mi, "drawable-agl", drawable);
911 #else
912     (void) p_mi; (void)drawable;
913 #endif
914 }
915
916 /**************************************************************************
917  * get_agl
918  **************************************************************************/
919 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
920 {
921     assert (p_mi != NULL);
922 #ifdef __APPLE__
923     return var_GetInteger (p_mi, "drawable-agl");
924 #else
925     return 0;
926 #endif
927 }
928
929 /**************************************************************************
930  * set_xwindow
931  **************************************************************************/
932 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
933                                       uint32_t drawable )
934 {
935     assert (p_mi != NULL);
936
937     var_SetString (p_mi, "vout", drawable ? "xid" : "any");
938     var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
939     var_SetInteger (p_mi, "drawable-xid", drawable);
940 }
941
942 /**************************************************************************
943  * get_xwindow
944  **************************************************************************/
945 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
946 {
947     return var_GetInteger (p_mi, "drawable-xid");
948 }
949
950 /**************************************************************************
951  * set_hwnd
952  **************************************************************************/
953 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
954                                    void *drawable )
955 {
956     assert (p_mi != NULL);
957 #if defined (_WIN32) || defined (__OS2__)
958     var_SetString (p_mi, "window",
959                    (drawable != NULL) ? "embed-hwnd,any" : "");
960     var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
961 #else
962     (void) p_mi; (void) drawable;
963 #endif
964 }
965
966 /**************************************************************************
967  * get_hwnd
968  **************************************************************************/
969 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
970 {
971     assert (p_mi != NULL);
972 #if defined (_WIN32) || defined (__OS2__)
973     return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
974 #else
975     return NULL;
976 #endif
977 }
978
979 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
980                                  libvlc_audio_play_cb play_cb,
981                                  libvlc_audio_pause_cb pause_cb,
982                                  libvlc_audio_resume_cb resume_cb,
983                                  libvlc_audio_flush_cb flush_cb,
984                                  libvlc_audio_drain_cb drain_cb,
985                                  void *opaque )
986 {
987     var_SetAddress( mp, "amem-play", play_cb );
988     var_SetAddress( mp, "amem-pause", pause_cb );
989     var_SetAddress( mp, "amem-resume", resume_cb );
990     var_SetAddress( mp, "amem-flush", flush_cb );
991     var_SetAddress( mp, "amem-drain", drain_cb );
992     var_SetAddress( mp, "amem-data", opaque );
993     var_SetString( mp, "aout", "amem,none" );
994
995     input_resource_ResetAout(mp->input.p_resource);
996 }
997
998 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
999                                        libvlc_audio_set_volume_cb cb )
1000 {
1001     var_SetAddress( mp, "amem-set-volume", cb );
1002
1003     input_resource_ResetAout(mp->input.p_resource);
1004 }
1005
1006 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1007                                         libvlc_audio_setup_cb setup,
1008                                         libvlc_audio_cleanup_cb cleanup )
1009 {
1010     var_SetAddress( mp, "amem-setup", setup );
1011     var_SetAddress( mp, "amem-cleanup", cleanup );
1012
1013     input_resource_ResetAout(mp->input.p_resource);
1014 }
1015
1016 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1017                               unsigned rate, unsigned channels )
1018 {
1019     var_SetString( mp, "amem-format", format );
1020     var_SetInteger( mp, "amem-rate", rate );
1021     var_SetInteger( mp, "amem-channels", channels );
1022
1023     input_resource_ResetAout(mp->input.p_resource);
1024 }
1025
1026
1027 /**************************************************************************
1028  * Getters for stream information
1029  **************************************************************************/
1030 libvlc_time_t libvlc_media_player_get_length(
1031                              libvlc_media_player_t *p_mi )
1032 {
1033     input_thread_t *p_input_thread;
1034     libvlc_time_t i_time;
1035
1036     p_input_thread = libvlc_get_input_thread ( p_mi );
1037     if( !p_input_thread )
1038         return -1;
1039
1040     i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
1041     vlc_object_release( p_input_thread );
1042
1043     return i_time;
1044 }
1045
1046 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1047 {
1048     input_thread_t *p_input_thread;
1049     libvlc_time_t i_time;
1050
1051     p_input_thread = libvlc_get_input_thread ( p_mi );
1052     if( !p_input_thread )
1053         return -1;
1054
1055     i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
1056     vlc_object_release( p_input_thread );
1057     return i_time;
1058 }
1059
1060 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1061                                    libvlc_time_t i_time )
1062 {
1063     input_thread_t *p_input_thread;
1064
1065     p_input_thread = libvlc_get_input_thread ( p_mi );
1066     if( !p_input_thread )
1067         return;
1068
1069     var_SetTime( p_input_thread, "time", to_mtime(i_time) );
1070     vlc_object_release( p_input_thread );
1071 }
1072
1073 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1074                                        float position )
1075 {
1076     input_thread_t *p_input_thread;
1077
1078     p_input_thread = libvlc_get_input_thread ( p_mi );
1079     if( !p_input_thread )
1080         return;
1081
1082     var_SetFloat( p_input_thread, "position", position );
1083     vlc_object_release( p_input_thread );
1084 }
1085
1086 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1087 {
1088     input_thread_t *p_input_thread;
1089     float f_position;
1090
1091     p_input_thread = libvlc_get_input_thread ( p_mi );
1092     if( !p_input_thread )
1093         return -1.0;
1094
1095     f_position = var_GetFloat( p_input_thread, "position" );
1096     vlc_object_release( p_input_thread );
1097
1098     return f_position;
1099 }
1100
1101 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1102                                       int chapter )
1103 {
1104     input_thread_t *p_input_thread;
1105
1106     p_input_thread = libvlc_get_input_thread ( p_mi );
1107     if( !p_input_thread )
1108         return;
1109
1110     var_SetInteger( p_input_thread, "chapter", chapter );
1111     vlc_object_release( p_input_thread );
1112 }
1113
1114 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1115 {
1116     input_thread_t *p_input_thread;
1117     int i_chapter;
1118
1119     p_input_thread = libvlc_get_input_thread ( p_mi );
1120     if( !p_input_thread )
1121         return -1;
1122
1123     i_chapter = var_GetInteger( p_input_thread, "chapter" );
1124     vlc_object_release( p_input_thread );
1125
1126     return i_chapter;
1127 }
1128
1129 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1130 {
1131     input_thread_t *p_input_thread;
1132     vlc_value_t val;
1133
1134     p_input_thread = libvlc_get_input_thread ( p_mi );
1135     if( !p_input_thread )
1136         return -1;
1137
1138     var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1139     vlc_object_release( p_input_thread );
1140
1141     return val.i_int;
1142 }
1143
1144 int libvlc_media_player_get_chapter_count_for_title(
1145                                  libvlc_media_player_t *p_mi,
1146                                  int i_title )
1147 {
1148     input_thread_t *p_input_thread;
1149     vlc_value_t val;
1150
1151     p_input_thread = libvlc_get_input_thread ( p_mi );
1152     if( !p_input_thread )
1153         return -1;
1154
1155     char *psz_name;
1156     if( asprintf( &psz_name,  "title %2i", i_title ) == -1 )
1157     {
1158         vlc_object_release( p_input_thread );
1159         return -1;
1160     }
1161     var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1162     vlc_object_release( p_input_thread );
1163     free( psz_name );
1164
1165     return val.i_int;
1166 }
1167
1168 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1169                                     int i_title )
1170 {
1171     input_thread_t *p_input_thread;
1172
1173     p_input_thread = libvlc_get_input_thread ( p_mi );
1174     if( !p_input_thread )
1175         return;
1176
1177     var_SetInteger( p_input_thread, "title", i_title );
1178     vlc_object_release( p_input_thread );
1179
1180     //send event
1181     libvlc_event_t event;
1182     event.type = libvlc_MediaPlayerTitleChanged;
1183     event.u.media_player_title_changed.new_title = i_title;
1184     libvlc_event_send( p_mi->p_event_manager, &event );
1185 }
1186
1187 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1188 {
1189     input_thread_t *p_input_thread;
1190     int i_title;
1191
1192     p_input_thread = libvlc_get_input_thread ( p_mi );
1193     if( !p_input_thread )
1194         return -1;
1195
1196     i_title = var_GetInteger( p_input_thread, "title" );
1197     vlc_object_release( p_input_thread );
1198
1199     return i_title;
1200 }
1201
1202 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1203 {
1204     input_thread_t *p_input_thread;
1205     vlc_value_t val;
1206
1207     p_input_thread = libvlc_get_input_thread ( p_mi );
1208     if( !p_input_thread )
1209         return -1;
1210
1211     var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1212     vlc_object_release( p_input_thread );
1213
1214     return val.i_int;
1215 }
1216
1217 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1218 {
1219     input_thread_t *p_input_thread;
1220
1221     p_input_thread = libvlc_get_input_thread ( p_mi );
1222     if( !p_input_thread )
1223         return;
1224
1225     int i_type = var_Type( p_input_thread, "next-chapter" );
1226     var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1227                             "next-chapter":"next-title" );
1228
1229     vlc_object_release( p_input_thread );
1230 }
1231
1232 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1233 {
1234     input_thread_t *p_input_thread;
1235
1236     p_input_thread = libvlc_get_input_thread ( p_mi );
1237     if( !p_input_thread )
1238         return;
1239
1240     int i_type = var_Type( p_input_thread, "next-chapter" );
1241     var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1242                             "prev-chapter":"prev-title" );
1243
1244     vlc_object_release( p_input_thread );
1245 }
1246
1247 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1248 {
1249     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1250     double f_fps = 0.0;
1251
1252     if( p_input_thread )
1253     {
1254         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1255             f_fps = 0.0;
1256         vlc_object_release( p_input_thread );
1257     }
1258     return f_fps;
1259 }
1260
1261 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1262 {
1263     bool b_will_play;
1264     input_thread_t *p_input_thread =
1265                             libvlc_get_input_thread ( p_mi );
1266     if ( !p_input_thread )
1267         return false;
1268
1269     b_will_play = !p_input_thread->b_dead;
1270     vlc_object_release( p_input_thread );
1271
1272     return b_will_play;
1273 }
1274
1275 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1276 {
1277     var_SetFloat (p_mi, "rate", rate);
1278
1279     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1280     if( !p_input_thread )
1281         return 0;
1282     var_SetFloat( p_input_thread, "rate", rate );
1283     vlc_object_release( p_input_thread );
1284     return 0;
1285 }
1286
1287 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1288 {
1289     return var_GetFloat (p_mi, "rate");
1290 }
1291
1292 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1293 {
1294     lock(p_mi);
1295     libvlc_state_t state = p_mi->state;
1296     unlock(p_mi);
1297     return state;
1298 }
1299
1300 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1301 {
1302     input_thread_t *p_input_thread;
1303     bool b_seekable;
1304
1305     p_input_thread = libvlc_get_input_thread ( p_mi );
1306     if ( !p_input_thread )
1307         return false;
1308     b_seekable = var_GetBool( p_input_thread, "can-seek" );
1309     vlc_object_release( p_input_thread );
1310
1311     return b_seekable;
1312 }
1313
1314 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1315                                    unsigned navigate )
1316 {
1317     static const vlc_action_t map[] =
1318     {
1319         INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1320         INPUT_NAV_LEFT, INPUT_NAV_RIGHT,
1321     };
1322
1323     if( navigate >= sizeof(map) / sizeof(map[0]) )
1324       return;
1325
1326     input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1327     if ( p_input == NULL )
1328       return;
1329
1330     input_Control( p_input, map[navigate], NULL );
1331     vlc_object_release( p_input );
1332 }
1333
1334 /* internal function, used by audio, video */
1335 libvlc_track_description_t *
1336         libvlc_get_track_description( libvlc_media_player_t *p_mi,
1337                                       const char *psz_variable )
1338 {
1339     input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1340     libvlc_track_description_t *p_track_description = NULL,
1341                                *p_actual, *p_previous;
1342
1343     if( !p_input )
1344         return NULL;
1345
1346     vlc_value_t val_list, text_list;
1347     var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1348
1349     /* no tracks */
1350     if( val_list.p_list->i_count <= 0 )
1351         goto end;
1352
1353     p_track_description = ( libvlc_track_description_t * )
1354         malloc( sizeof( libvlc_track_description_t ) );
1355     if ( !p_track_description )
1356     {
1357         libvlc_printerr( "Not enough memory" );
1358         goto end;
1359     }
1360     p_actual = p_track_description;
1361     p_previous = NULL;
1362     for( int i = 0; i < val_list.p_list->i_count; i++ )
1363     {
1364         if( !p_actual )
1365         {
1366             p_actual = ( libvlc_track_description_t * )
1367                 malloc( sizeof( libvlc_track_description_t ) );
1368             if ( !p_actual )
1369             {
1370                 libvlc_track_description_list_release( p_track_description );
1371                 libvlc_printerr( "Not enough memory" );
1372                 goto end;
1373             }
1374         }
1375         p_actual->i_id = val_list.p_list->p_values[i].i_int;
1376         p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1377         p_actual->p_next = NULL;
1378         if( p_previous )
1379             p_previous->p_next = p_actual;
1380         p_previous = p_actual;
1381         p_actual =  NULL;
1382     }
1383
1384 end:
1385     var_FreeList( &val_list, &text_list );
1386     vlc_object_release( p_input );
1387
1388     return p_track_description;
1389 }
1390
1391 // Deprecated alias for libvlc_track_description_list_release
1392 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1393 {
1394     libvlc_track_description_list_release( p_td );
1395 }
1396
1397 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1398 {
1399     libvlc_track_description_t *p_actual, *p_before;
1400     p_actual = p_td;
1401
1402     while ( p_actual )
1403     {
1404         free( p_actual->psz_name );
1405         p_before = p_actual;
1406         p_actual = p_before->p_next;
1407         free( p_before );
1408     }
1409 }
1410
1411 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1412 {
1413     input_thread_t *p_input_thread;
1414     bool b_can_pause;
1415
1416     p_input_thread = libvlc_get_input_thread ( p_mi );
1417     if ( !p_input_thread )
1418         return false;
1419     b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1420     vlc_object_release( p_input_thread );
1421
1422     return b_can_pause;
1423 }
1424
1425 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1426 {
1427     input_thread_t *p_input_thread;
1428     bool b_program_scrambled;
1429
1430     p_input_thread = libvlc_get_input_thread ( p_mi );
1431     if ( !p_input_thread )
1432         return false;
1433     b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1434     vlc_object_release( p_input_thread );
1435
1436     return b_program_scrambled;
1437 }
1438
1439 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1440 {
1441     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1442     if( p_input_thread != NULL )
1443     {
1444         var_TriggerCallback( p_input_thread, "frame-next" );
1445         vlc_object_release( p_input_thread );
1446     }
1447 }
1448
1449 /**
1450  * Private lookup table to get subpicture alignment flag values corresponding
1451  * to a libvlc_position_t enumerated value.
1452  */
1453 static const int position_subpicture_alignment[] = {
1454     0,
1455     SUBPICTURE_ALIGN_LEFT,
1456     SUBPICTURE_ALIGN_RIGHT,
1457     SUBPICTURE_ALIGN_TOP,
1458     SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1459     SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1460     SUBPICTURE_ALIGN_BOTTOM,
1461     SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1462     SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1463 };
1464
1465 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1466 {
1467     assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1468
1469     if ( position != libvlc_position_disable )
1470     {
1471         var_SetBool( p_mi, "video-title-show", true );
1472         var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1473         var_SetInteger( p_mi, "video-title-timeout", timeout );
1474     }
1475     else
1476     {
1477         var_SetBool( p_mi, "video-title-show", false );
1478     }
1479 }
1480
1481 /**
1482  * Maximum size of a formatted equalizer amplification band frequency value.
1483  *
1484  * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1485  *
1486  * The format string " %.07f" with a minimum value of "-20" gives a maximum
1487  * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1488  * terminator).
1489  */
1490 #define EQZ_BAND_VALUE_SIZE 12
1491
1492 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1493 {
1494     char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1495
1496     if( p_equalizer != NULL )
1497     {
1498         for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1499         {
1500             c = snprintf( bands + c, sizeof(bands) - c, " %.07f",
1501                           p_equalizer->f_amp[i] );
1502             if( unlikely(c >= sizeof(bands)) )
1503                 return -1;
1504         }
1505
1506         var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
1507         var_SetString( p_mi, "equalizer-bands", bands );
1508     }
1509     var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1510
1511     audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
1512     if( p_aout != NULL )
1513     {
1514         if( p_equalizer != NULL )
1515         {
1516             var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
1517             var_SetString( p_aout, "equalizer-bands", bands );
1518         }
1519
1520         var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1521         vlc_object_release( p_aout );
1522     }
1523
1524     return 0;
1525 }