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