libvlc: deprecate AGL vout setter/getter
[vlc.git] / lib / media_player.c
1 /*****************************************************************************
2  * media_player.c: Libvlc API Media Instance management functions
3  *****************************************************************************
4  * Copyright (C) 2005-2015 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 )
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 );
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_DEAD )
306     {
307         libvlc_state_t libvlc_state = libvlc_Ended;
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_GetInteger( 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_GetInteger( 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  * Create a Media Instance object.
467  *
468  * Refcount strategy:
469  * - All items created by _new start with a refcount set to 1.
470  * - Accessor _release decrease the refcount by 1, if after that
471  *   operation the refcount is 0, the object is destroyed.
472  * - Accessor _retain increase the refcount by 1 (XXX: to implement)
473  *
474  * Object locking strategy:
475  * - No lock held while in constructor.
476  * - When accessing any member variable this lock is held. (XXX who locks?)
477  * - When attempting to destroy the object the lock is also held.
478  **************************************************************************/
479 libvlc_media_player_t *
480 libvlc_media_player_new( libvlc_instance_t *instance )
481 {
482     libvlc_media_player_t * mp;
483
484     assert(instance);
485
486     mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
487     if (unlikely(mp == NULL))
488     {
489         libvlc_printerr("Not enough memory");
490         return NULL;
491     }
492
493     /* Input */
494     var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
495
496     /* Video */
497     var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
498     var_Create (mp, "window", VLC_VAR_STRING);
499     var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
500     var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
501     var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
502     var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
503     var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
504     var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
505     var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
506     var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
507     var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
508     var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
509     var_Create (mp, "avcodec-hw", VLC_VAR_STRING);
510     var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
511 #if defined (_WIN32) || defined (__OS2__)
512     var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
513 #endif
514 #ifdef __APPLE__
515     var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
516     var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
517 #endif
518 #ifdef __ANDROID__
519     var_Create (mp, "android-jvm", VLC_VAR_ADDRESS);
520     var_Create (mp, "drawable-androidwindow", VLC_VAR_ADDRESS);
521 #endif
522
523     var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
524     var_SetBool (mp, "keyboard-events", true);
525     var_Create (mp, "mouse-events", VLC_VAR_BOOL);
526     var_SetBool (mp, "mouse-events", true);
527
528     var_Create (mp, "fullscreen", VLC_VAR_BOOL);
529     var_Create (mp, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
530     var_Create (mp, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
531     var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
532     var_Create (mp, "crop", VLC_VAR_STRING);
533     var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
534     var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
535
536     var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
537     var_SetInteger (mp, "vbi-page", 100);
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);
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     release_input_thread( p_mi );
750
751     lock( p_mi );
752     set_state( p_mi, libvlc_NothingSpecial, true );
753     unlock_input( p_mi );
754
755     libvlc_media_release( p_mi->p_md );
756
757     if( !p_md )
758     {
759         p_mi->p_md = NULL;
760         unlock(p_mi);
761         return; /* It is ok to pass a NULL md */
762     }
763
764     libvlc_media_retain( p_md );
765     p_mi->p_md = p_md;
766
767     /* The policy here is to ignore that we were created using a different
768      * libvlc_instance, because we don't really care */
769     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
770
771     unlock(p_mi);
772
773     /* Send an event for the newly available media */
774     libvlc_event_t event;
775     event.type = libvlc_MediaPlayerMediaChanged;
776     event.u.media_player_media_changed.new_media = p_md;
777     libvlc_event_send( p_mi->p_event_manager, &event );
778
779 }
780
781 /**************************************************************************
782  * Get the Media descriptor associated with the instance.
783  **************************************************************************/
784 libvlc_media_t *
785 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
786 {
787     libvlc_media_t *p_m;
788
789     lock( p_mi );
790     p_m = p_mi->p_md;
791     if( p_m )
792         libvlc_media_retain( p_m );
793     unlock( p_mi );
794
795     return p_m;
796 }
797
798 /**************************************************************************
799  * Get the event Manager.
800  **************************************************************************/
801 libvlc_event_manager_t *
802 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
803 {
804     return p_mi->p_event_manager;
805 }
806
807 static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
808 {
809     var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
810     var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
811     var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
812     var_AddCallback( p_input_thread, "video-es", input_es_selected, p_mi );
813     var_AddCallback( p_input_thread, "audio-es", input_es_selected, p_mi );
814     var_AddCallback( p_input_thread, "spu-es", input_es_selected, p_mi );
815 }
816
817 static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
818 {
819     var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
820     var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
821     var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
822     var_DelCallback( p_input_thread, "video-es", input_es_selected, p_mi );
823     var_DelCallback( p_input_thread, "audio-es", input_es_selected, p_mi );
824     var_DelCallback( p_input_thread, "spu-es", input_es_selected, p_mi );
825 }
826
827 /**************************************************************************
828  * Tell media player to start playing.
829  **************************************************************************/
830 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
831 {
832     lock_input( p_mi );
833
834     input_thread_t *p_input_thread = p_mi->input.p_thread;
835     if( p_input_thread )
836     {
837         /* A thread already exists, send it a play message */
838         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
839         unlock_input( p_mi );
840         return 0;
841     }
842
843     /* Ignore previous exception */
844     lock(p_mi);
845
846     if( !p_mi->p_md )
847     {
848         unlock(p_mi);
849         unlock_input( p_mi );
850         libvlc_printerr( "No associated media descriptor" );
851         return -1;
852     }
853
854     p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
855                                    p_mi->input.p_resource );
856     unlock(p_mi);
857     if( !p_input_thread )
858     {
859         unlock_input(p_mi);
860         libvlc_printerr( "Not enough memory" );
861         return -1;
862     }
863
864     var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
865     var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
866     var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
867     var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
868     add_es_callbacks( p_input_thread, p_mi );
869
870     if( input_Start( p_input_thread ) )
871     {
872         unlock_input(p_mi);
873         del_es_callbacks( p_input_thread, p_mi );
874         var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
875         var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
876         var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
877         var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
878         input_Close( p_input_thread );
879         libvlc_printerr( "Input initialization failure" );
880         return -1;
881     }
882     p_mi->input.p_thread = p_input_thread;
883     unlock_input(p_mi);
884     return 0;
885 }
886
887 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
888 {
889     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
890     if( !p_input_thread )
891         return;
892
893     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
894     if( state == libvlc_Playing || state == libvlc_Buffering )
895     {
896         if( paused )
897         {
898             if( libvlc_media_player_can_pause( p_mi ) )
899                 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
900             else
901                 input_Stop( p_input_thread );
902         }
903     }
904     else
905     {
906         if( !paused )
907             input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
908     }
909
910     vlc_object_release( p_input_thread );
911 }
912
913 /**************************************************************************
914  * Toggle pause.
915  **************************************************************************/
916 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
917 {
918     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
919     bool playing = (state == libvlc_Playing || state == libvlc_Buffering);
920
921     libvlc_media_player_set_pause( p_mi, playing );
922 }
923
924 /**************************************************************************
925  * Tells whether the media player is currently playing.
926  *
927  * Enter with lock held.
928  **************************************************************************/
929 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
930 {
931     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
932     return (libvlc_Playing == state) || (libvlc_Buffering == state);
933 }
934
935 /**************************************************************************
936  * Stop playing.
937  **************************************************************************/
938 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
939 {
940     lock_input(p_mi);
941     release_input_thread( p_mi ); /* This will stop the input thread */
942
943     /* Force to go to stopped state, in case we were in Ended, or Error
944      * state. */
945     if( p_mi->state != libvlc_Stopped )
946     {
947         set_state( p_mi, libvlc_Stopped, false );
948
949         /* Construct and send the event */
950         libvlc_event_t event;
951         event.type = libvlc_MediaPlayerStopped;
952         libvlc_event_send( p_mi->p_event_manager, &event );
953     }
954
955     input_resource_Terminate( p_mi->input.p_resource );
956     unlock_input(p_mi);
957 }
958
959
960 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
961     void *(*lock_cb) (void *, void **),
962     void (*unlock_cb) (void *, void *, void *const *),
963     void (*display_cb) (void *, void *),
964     void *opaque )
965 {
966     var_SetAddress( mp, "vmem-lock", lock_cb );
967     var_SetAddress( mp, "vmem-unlock", unlock_cb );
968     var_SetAddress( mp, "vmem-display", display_cb );
969     var_SetAddress( mp, "vmem-data", opaque );
970     var_SetString( mp, "avcodec-hw", "none" );
971     var_SetString( mp, "vout", "vmem" );
972     var_SetString( mp, "window", "none" );
973 }
974
975 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
976                                         libvlc_video_format_cb setup,
977                                         libvlc_video_cleanup_cb cleanup )
978 {
979     var_SetAddress( mp, "vmem-setup", setup );
980     var_SetAddress( mp, "vmem-cleanup", cleanup );
981 }
982
983 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
984                               unsigned width, unsigned height, unsigned pitch )
985 {
986     var_SetString( mp, "vmem-chroma", chroma );
987     var_SetInteger( mp, "vmem-width", width );
988     var_SetInteger( mp, "vmem-height", height );
989     var_SetInteger( mp, "vmem-pitch", pitch );
990 }
991
992 /**************************************************************************
993  * set_nsobject
994  **************************************************************************/
995 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
996                                         void * drawable )
997 {
998     assert (p_mi != NULL);
999 #ifdef __APPLE__
1000     var_SetString (p_mi, "avcodec-hw", "");
1001     var_SetString (p_mi, "vout", "");
1002     var_SetString (p_mi, "window", "");
1003     var_SetAddress (p_mi, "drawable-nsobject", drawable);
1004 #else
1005     (void) p_mi; (void)drawable;
1006 #endif
1007 }
1008
1009 /**************************************************************************
1010  * get_nsobject
1011  **************************************************************************/
1012 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1013 {
1014     assert (p_mi != NULL);
1015 #ifdef __APPLE__
1016     return var_GetAddress (p_mi, "drawable-nsobject");
1017 #else
1018     return NULL;
1019 #endif
1020 }
1021
1022 /**************************************************************************
1023  * set_agl
1024  **************************************************************************/
1025 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
1026                                   uint32_t drawable )
1027 {
1028     (void) p_mi; (void)drawable;
1029 }
1030
1031 /**************************************************************************
1032  * get_agl
1033  **************************************************************************/
1034 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
1035 {
1036     return 0;
1037 }
1038
1039 /**************************************************************************
1040  * set_xwindow
1041  **************************************************************************/
1042 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1043                                       uint32_t drawable )
1044 {
1045     assert (p_mi != NULL);
1046
1047     var_SetString (p_mi, "avcodec-hw", "");
1048     var_SetString (p_mi, "vout", drawable ? "xid" : "");
1049     var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1050     var_SetInteger (p_mi, "drawable-xid", drawable);
1051 }
1052
1053 /**************************************************************************
1054  * get_xwindow
1055  **************************************************************************/
1056 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1057 {
1058     return var_GetInteger (p_mi, "drawable-xid");
1059 }
1060
1061 /**************************************************************************
1062  * set_hwnd
1063  **************************************************************************/
1064 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1065                                    void *drawable )
1066 {
1067     assert (p_mi != NULL);
1068 #if defined (_WIN32) || defined (__OS2__)
1069     var_SetString (p_mi, "avcodec-hw", "");
1070     var_SetString (p_mi, "vout", "");
1071     var_SetString (p_mi, "window",
1072                    (drawable != NULL) ? "embed-hwnd,any" : "");
1073     var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1074 #else
1075     (void) p_mi; (void) drawable;
1076 #endif
1077 }
1078
1079 /**************************************************************************
1080  * get_hwnd
1081  **************************************************************************/
1082 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1083 {
1084     assert (p_mi != NULL);
1085 #if defined (_WIN32) || defined (__OS2__)
1086     return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1087 #else
1088     return NULL;
1089 #endif
1090 }
1091
1092 /**************************************************************************
1093  * set_android_context
1094  **************************************************************************/
1095 void libvlc_media_player_set_android_context( libvlc_media_player_t *p_mi,
1096                                               void *p_jvm,
1097                                               void *p_awindow_handler )
1098 {
1099     assert (p_mi != NULL);
1100 #ifdef __ANDROID__
1101     var_SetAddress (p_mi, "android-jvm", p_jvm);
1102     var_SetAddress (p_mi, "drawable-androidwindow", p_awindow_handler);
1103 #else
1104     (void) p_mi; (void) p_jvm; (void) p_awindow_handler;
1105 #endif
1106 }
1107
1108 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1109                                  libvlc_audio_play_cb play_cb,
1110                                  libvlc_audio_pause_cb pause_cb,
1111                                  libvlc_audio_resume_cb resume_cb,
1112                                  libvlc_audio_flush_cb flush_cb,
1113                                  libvlc_audio_drain_cb drain_cb,
1114                                  void *opaque )
1115 {
1116     var_SetAddress( mp, "amem-play", play_cb );
1117     var_SetAddress( mp, "amem-pause", pause_cb );
1118     var_SetAddress( mp, "amem-resume", resume_cb );
1119     var_SetAddress( mp, "amem-flush", flush_cb );
1120     var_SetAddress( mp, "amem-drain", drain_cb );
1121     var_SetAddress( mp, "amem-data", opaque );
1122     var_SetString( mp, "aout", "amem,none" );
1123
1124     input_resource_ResetAout(mp->input.p_resource);
1125 }
1126
1127 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1128                                        libvlc_audio_set_volume_cb cb )
1129 {
1130     var_SetAddress( mp, "amem-set-volume", cb );
1131
1132     input_resource_ResetAout(mp->input.p_resource);
1133 }
1134
1135 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1136                                         libvlc_audio_setup_cb setup,
1137                                         libvlc_audio_cleanup_cb cleanup )
1138 {
1139     var_SetAddress( mp, "amem-setup", setup );
1140     var_SetAddress( mp, "amem-cleanup", cleanup );
1141
1142     input_resource_ResetAout(mp->input.p_resource);
1143 }
1144
1145 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1146                               unsigned rate, unsigned channels )
1147 {
1148     var_SetString( mp, "amem-format", format );
1149     var_SetInteger( mp, "amem-rate", rate );
1150     var_SetInteger( mp, "amem-channels", channels );
1151
1152     input_resource_ResetAout(mp->input.p_resource);
1153 }
1154
1155
1156 /**************************************************************************
1157  * Getters for stream information
1158  **************************************************************************/
1159 libvlc_time_t libvlc_media_player_get_length(
1160                              libvlc_media_player_t *p_mi )
1161 {
1162     input_thread_t *p_input_thread;
1163     libvlc_time_t i_time;
1164
1165     p_input_thread = libvlc_get_input_thread ( p_mi );
1166     if( !p_input_thread )
1167         return -1;
1168
1169     i_time = from_mtime(var_GetInteger( p_input_thread, "length" ));
1170     vlc_object_release( p_input_thread );
1171
1172     return i_time;
1173 }
1174
1175 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1176 {
1177     input_thread_t *p_input_thread;
1178     libvlc_time_t i_time;
1179
1180     p_input_thread = libvlc_get_input_thread ( p_mi );
1181     if( !p_input_thread )
1182         return -1;
1183
1184     i_time = from_mtime(var_GetInteger( p_input_thread , "time" ));
1185     vlc_object_release( p_input_thread );
1186     return i_time;
1187 }
1188
1189 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1190                                    libvlc_time_t i_time )
1191 {
1192     input_thread_t *p_input_thread;
1193
1194     p_input_thread = libvlc_get_input_thread ( p_mi );
1195     if( !p_input_thread )
1196         return;
1197
1198     var_SetInteger( p_input_thread, "time", to_mtime(i_time) );
1199     vlc_object_release( p_input_thread );
1200 }
1201
1202 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1203                                        float position )
1204 {
1205     input_thread_t *p_input_thread;
1206
1207     p_input_thread = libvlc_get_input_thread ( p_mi );
1208     if( !p_input_thread )
1209         return;
1210
1211     var_SetFloat( p_input_thread, "position", position );
1212     vlc_object_release( p_input_thread );
1213 }
1214
1215 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1216 {
1217     input_thread_t *p_input_thread;
1218     float f_position;
1219
1220     p_input_thread = libvlc_get_input_thread ( p_mi );
1221     if( !p_input_thread )
1222         return -1.0;
1223
1224     f_position = var_GetFloat( p_input_thread, "position" );
1225     vlc_object_release( p_input_thread );
1226
1227     return f_position;
1228 }
1229
1230 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1231                                       int chapter )
1232 {
1233     input_thread_t *p_input_thread;
1234
1235     p_input_thread = libvlc_get_input_thread ( p_mi );
1236     if( !p_input_thread )
1237         return;
1238
1239     var_SetInteger( p_input_thread, "chapter", chapter );
1240     vlc_object_release( p_input_thread );
1241 }
1242
1243 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1244 {
1245     input_thread_t *p_input_thread;
1246     int i_chapter;
1247
1248     p_input_thread = libvlc_get_input_thread ( p_mi );
1249     if( !p_input_thread )
1250         return -1;
1251
1252     i_chapter = var_GetInteger( p_input_thread, "chapter" );
1253     vlc_object_release( p_input_thread );
1254
1255     return i_chapter;
1256 }
1257
1258 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1259 {
1260     input_thread_t *p_input_thread;
1261     vlc_value_t val;
1262
1263     p_input_thread = libvlc_get_input_thread ( p_mi );
1264     if( !p_input_thread )
1265         return -1;
1266
1267     int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1268     vlc_object_release( p_input_thread );
1269
1270     return i_ret == VLC_SUCCESS ? val.i_int : -1;
1271 }
1272
1273 int libvlc_media_player_get_chapter_count_for_title(
1274                                  libvlc_media_player_t *p_mi,
1275                                  int i_title )
1276 {
1277     vlc_value_t val;
1278
1279     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1280     if( !p_input_thread )
1281         return -1;
1282
1283     char psz_name[sizeof ("title ") + 3 * sizeof (int)];
1284     sprintf( psz_name, "title %2u", i_title );
1285
1286     int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1287     vlc_object_release( p_input_thread );
1288
1289     return i_ret == VLC_SUCCESS ? val.i_int : -1;
1290 }
1291
1292 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1293                                     int i_title )
1294 {
1295     input_thread_t *p_input_thread;
1296
1297     p_input_thread = libvlc_get_input_thread ( p_mi );
1298     if( !p_input_thread )
1299         return;
1300
1301     var_SetInteger( p_input_thread, "title", i_title );
1302     vlc_object_release( p_input_thread );
1303
1304     //send event
1305     libvlc_event_t event;
1306     event.type = libvlc_MediaPlayerTitleChanged;
1307     event.u.media_player_title_changed.new_title = i_title;
1308     libvlc_event_send( p_mi->p_event_manager, &event );
1309 }
1310
1311 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1312 {
1313     input_thread_t *p_input_thread;
1314     int i_title;
1315
1316     p_input_thread = libvlc_get_input_thread ( p_mi );
1317     if( !p_input_thread )
1318         return -1;
1319
1320     i_title = var_GetInteger( p_input_thread, "title" );
1321     vlc_object_release( p_input_thread );
1322
1323     return i_title;
1324 }
1325
1326 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1327 {
1328     input_thread_t *p_input_thread;
1329     vlc_value_t val;
1330
1331     p_input_thread = libvlc_get_input_thread ( p_mi );
1332     if( !p_input_thread )
1333         return -1;
1334
1335     int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1336     vlc_object_release( p_input_thread );
1337
1338     return i_ret == VLC_SUCCESS ? val.i_int : -1;
1339 }
1340
1341 int libvlc_media_player_get_full_title_descriptions( libvlc_media_player_t *p_mi,
1342                                                      libvlc_title_description_t *** pp_titles )
1343 {
1344     assert( p_mi );
1345
1346     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1347
1348     if( !p_input_thread )
1349         return -1;
1350
1351     input_title_t **p_input_title;
1352     int count;
1353
1354     /* fetch data */
1355     int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO,
1356                              &p_input_title, &count );
1357     vlc_object_release( p_input_thread );
1358     if( ret != VLC_SUCCESS )
1359         return -1;
1360
1361     libvlc_title_description_t **titles = malloc( count * sizeof (*titles) );
1362     if( count > 0 && unlikely(titles == NULL) )
1363         return -1;
1364
1365     /* fill array */
1366     for( int i = 0; i < count; i++)
1367     {
1368         libvlc_title_description_t *title = malloc( sizeof (*title) );
1369         if( unlikely(title == NULL) )
1370         {
1371             libvlc_title_descriptions_release( titles, count );
1372             return -1;
1373         }
1374         titles[i] = title;
1375
1376         /* we want to return milliseconds to match the rest of the API */
1377         title->i_duration = p_input_title[i]->i_length / 1000;
1378         title->b_menu = p_input_title[i]->b_menu;
1379         if( p_input_title[i]->psz_name )
1380             title->psz_name = strdup( p_input_title[i]->psz_name );
1381         else
1382             title->psz_name = NULL;
1383         vlc_input_title_Delete( p_input_title[i] );
1384     }
1385     free( p_input_title );
1386
1387     *pp_titles = titles;
1388     return count;
1389 }
1390
1391 void libvlc_title_descriptions_release( libvlc_title_description_t **p_titles,
1392                                         unsigned i_count )
1393 {
1394     for (unsigned i = 0; i < i_count; i++ )
1395     {
1396         if ( !p_titles[i] )
1397             continue;
1398
1399         free( p_titles[i]->psz_name );
1400         free( p_titles[i] );
1401     }
1402     free( p_titles );
1403 }
1404
1405 int libvlc_media_player_get_full_chapter_descriptions( libvlc_media_player_t *p_mi,
1406                                                       int i_chapters_of_title,
1407                                                       libvlc_chapter_description_t *** pp_chapters )
1408 {
1409     assert( p_mi );
1410
1411     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1412
1413     if( !p_input_thread )
1414         return -1;
1415
1416     seekpoint_t **p_seekpoint = NULL;
1417
1418     /* fetch data */
1419     int ret = input_Control(p_input_thread, INPUT_GET_SEEKPOINTS, &p_seekpoint, &i_chapters_of_title);
1420     vlc_object_release( p_input_thread );
1421
1422     if( ret != VLC_SUCCESS)
1423     {
1424         return -1;
1425     }
1426
1427     if (i_chapters_of_title == 0 || p_seekpoint == NULL)
1428     {
1429         return 0;
1430     }
1431
1432     const int ci_chapter_count = (const int)i_chapters_of_title;
1433
1434     *pp_chapters = calloc( ci_chapter_count, sizeof(**pp_chapters) );
1435     if( !*pp_chapters )
1436     {
1437         return -1;
1438     }
1439
1440     /* fill array */
1441     for( int i = 0; i < ci_chapter_count; i++)
1442     {
1443         libvlc_chapter_description_t *p_chapter = malloc( sizeof(*p_chapter) );
1444         if( unlikely(p_chapter == NULL) )
1445         {
1446             libvlc_chapter_descriptions_release( *pp_chapters, ci_chapter_count );
1447             return -1;
1448         }
1449         (*pp_chapters)[i] = p_chapter;
1450
1451         p_chapter->i_time_offset = p_seekpoint[i]->i_time_offset / 1000;
1452
1453         if( i > 0 )
1454         {
1455             p_chapter->i_duration = p_chapter->i_time_offset - (*pp_chapters)[i-1]->i_time_offset;
1456         }
1457         else
1458         {
1459             p_chapter->i_duration = p_chapter->i_time_offset;
1460         }
1461
1462         if( p_seekpoint[i]->psz_name )
1463         {
1464             p_chapter->psz_name = strdup( p_seekpoint[i]->psz_name );
1465         }
1466         else
1467         {
1468             p_chapter->psz_name = NULL;
1469         }
1470         vlc_seekpoint_Delete( p_seekpoint[i] );
1471     }
1472
1473     return ci_chapter_count;
1474 }
1475
1476 void libvlc_chapter_descriptions_release( libvlc_chapter_description_t **p_chapters,
1477                                           unsigned i_count )
1478 {
1479     for (unsigned i = 0; i < i_count; i++ )
1480     {
1481         if ( !p_chapters[i] )
1482             continue;
1483
1484         free( p_chapters[i]->psz_name );
1485         free( p_chapters[i] );
1486     }
1487     free( p_chapters );
1488 }
1489
1490 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1491 {
1492     input_thread_t *p_input_thread;
1493
1494     p_input_thread = libvlc_get_input_thread ( p_mi );
1495     if( !p_input_thread )
1496         return;
1497
1498     int i_type = var_Type( p_input_thread, "next-chapter" );
1499     var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1500                             "next-chapter":"next-title" );
1501
1502     vlc_object_release( p_input_thread );
1503 }
1504
1505 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1506 {
1507     input_thread_t *p_input_thread;
1508
1509     p_input_thread = libvlc_get_input_thread ( p_mi );
1510     if( !p_input_thread )
1511         return;
1512
1513     int i_type = var_Type( p_input_thread, "next-chapter" );
1514     var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1515                             "prev-chapter":"prev-title" );
1516
1517     vlc_object_release( p_input_thread );
1518 }
1519
1520 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1521 {
1522     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1523     double f_fps = 0.0;
1524
1525     if( p_input_thread )
1526     {
1527         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1528             f_fps = 0.0;
1529         vlc_object_release( p_input_thread );
1530     }
1531     return f_fps;
1532 }
1533
1534 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1535 {
1536     input_thread_t *p_input_thread =
1537                             libvlc_get_input_thread ( p_mi );
1538     if ( !p_input_thread )
1539         return false;
1540
1541     int state = var_GetInteger( p_input_thread, "state" );
1542     vlc_object_release( p_input_thread );
1543
1544     return state != END_S && state != ERROR_S;
1545 }
1546
1547 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1548 {
1549     var_SetFloat (p_mi, "rate", rate);
1550
1551     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1552     if( !p_input_thread )
1553         return 0;
1554     var_SetFloat( p_input_thread, "rate", rate );
1555     vlc_object_release( p_input_thread );
1556     return 0;
1557 }
1558
1559 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1560 {
1561     return var_GetFloat (p_mi, "rate");
1562 }
1563
1564 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1565 {
1566     lock(p_mi);
1567     libvlc_state_t state = p_mi->state;
1568     unlock(p_mi);
1569     return state;
1570 }
1571
1572 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1573 {
1574     input_thread_t *p_input_thread;
1575     bool b_seekable;
1576
1577     p_input_thread = libvlc_get_input_thread ( p_mi );
1578     if ( !p_input_thread )
1579         return false;
1580     b_seekable = var_GetBool( p_input_thread, "can-seek" );
1581     vlc_object_release( p_input_thread );
1582
1583     return b_seekable;
1584 }
1585
1586 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1587                                    unsigned navigate )
1588 {
1589     static const vlc_action_t map[] =
1590     {
1591         INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1592         INPUT_NAV_LEFT, INPUT_NAV_RIGHT,
1593     };
1594
1595     if( navigate >= sizeof(map) / sizeof(map[0]) )
1596       return;
1597
1598     input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1599     if ( p_input == NULL )
1600       return;
1601
1602     input_Control( p_input, map[navigate], NULL );
1603     vlc_object_release( p_input );
1604 }
1605
1606 /* internal function, used by audio, video */
1607 libvlc_track_description_t *
1608         libvlc_get_track_description( libvlc_media_player_t *p_mi,
1609                                       const char *psz_variable )
1610 {
1611     input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1612     libvlc_track_description_t *p_track_description = NULL,
1613                                *p_actual, *p_previous;
1614
1615     if( !p_input )
1616         return NULL;
1617
1618     vlc_value_t val_list, text_list;
1619     int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES, &val_list, &text_list );
1620     if( i_ret != VLC_SUCCESS )
1621         return NULL;
1622
1623     /* no tracks */
1624     if( val_list.p_list->i_count <= 0 )
1625         goto end;
1626
1627     p_track_description = ( libvlc_track_description_t * )
1628         malloc( sizeof( libvlc_track_description_t ) );
1629     if ( !p_track_description )
1630     {
1631         libvlc_printerr( "Not enough memory" );
1632         goto end;
1633     }
1634     p_actual = p_track_description;
1635     p_previous = NULL;
1636     for( int i = 0; i < val_list.p_list->i_count; i++ )
1637     {
1638         if( !p_actual )
1639         {
1640             p_actual = ( libvlc_track_description_t * )
1641                 malloc( sizeof( libvlc_track_description_t ) );
1642             if ( !p_actual )
1643             {
1644                 libvlc_track_description_list_release( p_track_description );
1645                 libvlc_printerr( "Not enough memory" );
1646                 goto end;
1647             }
1648         }
1649         p_actual->i_id = val_list.p_list->p_values[i].i_int;
1650         p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1651         p_actual->p_next = NULL;
1652         if( p_previous )
1653             p_previous->p_next = p_actual;
1654         p_previous = p_actual;
1655         p_actual =  NULL;
1656     }
1657
1658 end:
1659     var_FreeList( &val_list, &text_list );
1660     vlc_object_release( p_input );
1661
1662     return p_track_description;
1663 }
1664
1665 // Deprecated alias for libvlc_track_description_list_release
1666 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1667 {
1668     libvlc_track_description_list_release( p_td );
1669 }
1670
1671 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1672 {
1673     libvlc_track_description_t *p_actual, *p_before;
1674     p_actual = p_td;
1675
1676     while ( p_actual )
1677     {
1678         free( p_actual->psz_name );
1679         p_before = p_actual;
1680         p_actual = p_before->p_next;
1681         free( p_before );
1682     }
1683 }
1684
1685 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1686 {
1687     input_thread_t *p_input_thread;
1688     bool b_can_pause;
1689
1690     p_input_thread = libvlc_get_input_thread ( p_mi );
1691     if ( !p_input_thread )
1692         return false;
1693     b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1694     vlc_object_release( p_input_thread );
1695
1696     return b_can_pause;
1697 }
1698
1699 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1700 {
1701     input_thread_t *p_input_thread;
1702     bool b_program_scrambled;
1703
1704     p_input_thread = libvlc_get_input_thread ( p_mi );
1705     if ( !p_input_thread )
1706         return false;
1707     b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1708     vlc_object_release( p_input_thread );
1709
1710     return b_program_scrambled;
1711 }
1712
1713 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1714 {
1715     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1716     if( p_input_thread != NULL )
1717     {
1718         var_TriggerCallback( p_input_thread, "frame-next" );
1719         vlc_object_release( p_input_thread );
1720     }
1721 }
1722
1723 /**
1724  * Private lookup table to get subpicture alignment flag values corresponding
1725  * to a libvlc_position_t enumerated value.
1726  */
1727 static const unsigned char position_subpicture_alignment[] = {
1728     [libvlc_position_center]       = 0,
1729     [libvlc_position_left]         = SUBPICTURE_ALIGN_LEFT,
1730     [libvlc_position_right]        = SUBPICTURE_ALIGN_RIGHT,
1731     [libvlc_position_top]          = SUBPICTURE_ALIGN_TOP,
1732     [libvlc_position_top_left]     = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1733     [libvlc_position_top_right]    = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1734     [libvlc_position_bottom]       = SUBPICTURE_ALIGN_BOTTOM,
1735     [libvlc_position_bottom_left]  = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1736     [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1737 };
1738
1739 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1740 {
1741     assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1742
1743     if ( position != libvlc_position_disable )
1744     {
1745         var_SetBool( p_mi, "video-title-show", true );
1746         var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1747         var_SetInteger( p_mi, "video-title-timeout", timeout );
1748     }
1749     else
1750     {
1751         var_SetBool( p_mi, "video-title-show", false );
1752     }
1753 }
1754
1755 /**
1756  * Maximum size of a formatted equalizer amplification band frequency value.
1757  *
1758  * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1759  *
1760  * The format string " %.07f" with a minimum value of "-20" gives a maximum
1761  * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1762  * terminator).
1763  */
1764 #define EQZ_BAND_VALUE_SIZE 12
1765
1766 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1767 {
1768     char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1769
1770     if( p_equalizer != NULL )
1771     {
1772         for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1773         {
1774             c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
1775                           p_equalizer->f_amp[i] );
1776             if( unlikely(c >= sizeof(bands)) )
1777                 return -1;
1778         }
1779
1780         var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
1781         var_SetString( p_mi, "equalizer-bands", bands );
1782     }
1783     var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1784
1785     audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
1786     if( p_aout != NULL )
1787     {
1788         if( p_equalizer != NULL )
1789         {
1790             var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
1791             var_SetString( p_aout, "equalizer-bands", bands );
1792         }
1793
1794         var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
1795         vlc_object_release( p_aout );
1796     }
1797
1798     return 0;
1799 }