demux: ts: proper init/clean for fmt in hotfixes
[vlc.git] / lib / media_player.c
blobdc17f7765d719a1d44a837464616839ef105d17d
1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2015 VLC authors and VideoLAN
6 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <assert.h>
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_renderer_discoverer.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_events.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
36 #include <vlc_vout.h>
37 #include <vlc_aout.h>
38 #include <vlc_keys.h>
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
43 #include "renderer_discoverer_internal.h"
45 #define ES_INIT (-2) /* -1 is reserved for ES deselect */
47 static int
48 input_seekable_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_pausable_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_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
57 vlc_value_t oldval, vlc_value_t newval,
58 void * p_userdata );
59 static int
60 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
61 vlc_value_t oldval, vlc_value_t newval,
62 void * p_userdata );
64 static int
65 input_es_changed( vlc_object_t * p_this, char const * psz_cmd,
66 int action, vlc_value_t *p_val,
67 void *p_userdata);
69 static int
70 corks_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
71 vlc_value_t cur, void *opaque);
73 static int
74 mute_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
75 vlc_value_t cur, void *opaque);
77 static int
78 volume_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
79 vlc_value_t cur, void *opaque);
81 static void
82 add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
84 static void
85 del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
87 static int
88 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
89 vlc_value_t oldval, vlc_value_t newval, void *p_data );
91 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
94 * Shortcuts
98 * The input lock protects the input and input resource pointer.
99 * It MUST NOT be used from callbacks.
101 * The object lock protects the reset, namely the media and the player state.
102 * It can, and usually needs to be taken from callbacks.
103 * The object lock can be acquired under the input lock... and consequently
104 * the opposite order is STRICTLY PROHIBITED.
106 static inline void lock(libvlc_media_player_t *mp)
108 vlc_mutex_lock(&mp->object_lock);
111 static inline void unlock(libvlc_media_player_t *mp)
113 vlc_mutex_unlock(&mp->object_lock);
116 static inline void lock_input(libvlc_media_player_t *mp)
118 vlc_mutex_lock(&mp->input.lock);
121 static inline void unlock_input(libvlc_media_player_t *mp)
123 vlc_mutex_unlock(&mp->input.lock);
126 static void input_item_preparsed_changed( const vlc_event_t *p_event,
127 void * user_data )
129 libvlc_media_t *p_md = user_data;
130 if( p_event->u.input_item_preparsed_changed.new_status & ITEM_PREPARSED )
132 /* Send the event */
133 libvlc_event_t event;
134 event.type = libvlc_MediaParsedChanged;
135 event.u.media_parsed_changed.new_status = libvlc_media_parsed_status_done;
136 libvlc_event_send( &p_md->event_manager, &event );
140 static void media_attach_preparsed_event( libvlc_media_t *p_md )
142 vlc_event_attach( &p_md->p_input_item->event_manager,
143 vlc_InputItemPreparsedChanged,
144 input_item_preparsed_changed, p_md );
147 static void media_detach_preparsed_event( libvlc_media_t *p_md )
149 vlc_event_detach( &p_md->p_input_item->event_manager,
150 vlc_InputItemPreparsedChanged,
151 input_item_preparsed_changed,
152 p_md );
156 * Release the associated input thread.
158 * Object lock is NOT held.
159 * Input lock is held or instance is being destroyed.
161 static void release_input_thread( libvlc_media_player_t *p_mi )
163 assert( p_mi );
165 input_thread_t *p_input_thread = p_mi->input.p_thread;
166 if( !p_input_thread )
167 return;
168 p_mi->input.p_thread = NULL;
170 media_detach_preparsed_event( p_mi->p_md );
172 var_DelCallback( p_input_thread, "can-seek",
173 input_seekable_changed, p_mi );
174 var_DelCallback( p_input_thread, "can-pause",
175 input_pausable_changed, p_mi );
176 var_DelCallback( p_input_thread, "program-scrambled",
177 input_scrambled_changed, p_mi );
178 var_DelCallback( p_input_thread, "intf-event",
179 input_event_changed, p_mi );
180 del_es_callbacks( p_input_thread, p_mi );
182 /* We owned this one */
183 input_Stop( p_input_thread );
184 input_Close( p_input_thread );
188 * Retrieve the input thread. Be sure to release the object
189 * once you are done with it. (libvlc Internal)
191 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
193 input_thread_t *p_input_thread;
195 assert( p_mi );
197 lock_input(p_mi);
198 p_input_thread = p_mi->input.p_thread;
199 if( p_input_thread )
200 vlc_object_hold( p_input_thread );
201 else
202 libvlc_printerr( "No active input" );
203 unlock_input(p_mi);
205 return p_input_thread;
209 * Set the internal state of the media_player. (media player Internal)
211 * Function will lock the media_player.
213 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
214 bool b_locked )
216 if(!b_locked)
217 lock(p_mi);
218 p_mi->state = state;
220 libvlc_media_t *media = p_mi->p_md;
221 if (media)
222 libvlc_media_retain(media);
224 if(!b_locked)
225 unlock(p_mi);
227 if (media)
229 // Also set the state of the corresponding media
230 // This is strictly for convenience.
231 libvlc_media_set_state(media, state);
233 libvlc_media_release(media);
237 static int
238 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
239 vlc_value_t oldval, vlc_value_t newval,
240 void * p_userdata )
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;
248 event.type = libvlc_MediaPlayerSeekableChanged;
249 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
251 libvlc_event_send( &p_mi->event_manager, &event );
252 return VLC_SUCCESS;
255 static int
256 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
257 vlc_value_t oldval, vlc_value_t newval,
258 void * p_userdata )
260 VLC_UNUSED(oldval);
261 VLC_UNUSED(p_this);
262 VLC_UNUSED(psz_cmd);
263 libvlc_media_player_t * p_mi = p_userdata;
264 libvlc_event_t event;
266 event.type = libvlc_MediaPlayerPausableChanged;
267 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
269 libvlc_event_send( &p_mi->event_manager, &event );
270 return VLC_SUCCESS;
273 static int
274 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
275 vlc_value_t oldval, vlc_value_t newval,
276 void * p_userdata )
278 VLC_UNUSED(oldval);
279 VLC_UNUSED(p_this);
280 VLC_UNUSED(psz_cmd);
281 libvlc_media_player_t * p_mi = p_userdata;
282 libvlc_event_t event;
284 event.type = libvlc_MediaPlayerScrambledChanged;
285 event.u.media_player_scrambled_changed.new_scrambled = newval.b_bool;
287 libvlc_event_send( &p_mi->event_manager, &event );
288 return VLC_SUCCESS;
291 static int
292 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
293 vlc_value_t oldval, vlc_value_t newval,
294 void * p_userdata )
296 VLC_UNUSED(oldval); VLC_UNUSED(psz_cmd);
297 input_thread_t * p_input = (input_thread_t *)p_this;
298 libvlc_media_player_t * p_mi = p_userdata;
299 libvlc_event_t event;
301 assert( !strcmp( psz_cmd, "intf-event" ) );
303 if( newval.i_int == INPUT_EVENT_STATE )
305 libvlc_state_t libvlc_state;
307 switch ( var_GetInteger( p_input, "state" ) )
309 case INIT_S:
310 libvlc_state = libvlc_NothingSpecial;
311 event.type = libvlc_MediaPlayerNothingSpecial;
312 break;
313 case OPENING_S:
314 libvlc_state = libvlc_Opening;
315 event.type = libvlc_MediaPlayerOpening;
316 break;
317 case PLAYING_S:
318 libvlc_state = libvlc_Playing;
319 event.type = libvlc_MediaPlayerPlaying;
320 break;
321 case PAUSE_S:
322 libvlc_state = libvlc_Paused;
323 event.type = libvlc_MediaPlayerPaused;
324 break;
325 case END_S:
326 libvlc_state = libvlc_Ended;
327 event.type = libvlc_MediaPlayerEndReached;
328 break;
329 case ERROR_S:
330 libvlc_state = libvlc_Error;
331 event.type = libvlc_MediaPlayerEncounteredError;
332 break;
334 default:
335 return VLC_SUCCESS;
338 set_state( p_mi, libvlc_state, false );
339 libvlc_event_send( &p_mi->event_manager, &event );
341 else if( newval.i_int == INPUT_EVENT_DEAD )
343 libvlc_state_t libvlc_state = libvlc_Ended;
344 event.type = libvlc_MediaPlayerStopped;
346 set_state( p_mi, libvlc_state, false );
347 libvlc_event_send( &p_mi->event_manager, &event );
349 else if( newval.i_int == INPUT_EVENT_POSITION )
351 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
352 return VLC_SUCCESS; /* Don't send the position while stopped */
354 /* */
355 event.type = libvlc_MediaPlayerPositionChanged;
356 event.u.media_player_position_changed.new_position =
357 var_GetFloat( p_input, "position" );
358 libvlc_event_send( &p_mi->event_manager, &event );
360 /* */
361 event.type = libvlc_MediaPlayerTimeChanged;
362 event.u.media_player_time_changed.new_time =
363 from_mtime(var_GetInteger( p_input, "time" ));
364 libvlc_event_send( &p_mi->event_manager, &event );
366 else if( newval.i_int == INPUT_EVENT_LENGTH )
368 event.type = libvlc_MediaPlayerLengthChanged;
369 event.u.media_player_length_changed.new_length =
370 from_mtime(var_GetInteger( p_input, "length" ));
371 libvlc_event_send( &p_mi->event_manager, &event );
373 else if( newval.i_int == INPUT_EVENT_CACHE )
375 event.type = libvlc_MediaPlayerBuffering;
376 event.u.media_player_buffering.new_cache = (100 *
377 var_GetFloat( p_input, "cache" ));
378 libvlc_event_send( &p_mi->event_manager, &event );
380 else if( newval.i_int == INPUT_EVENT_VOUT )
382 vout_thread_t **pp_vout;
383 size_t i_vout;
384 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
386 i_vout = 0;
388 else
390 for( size_t i = 0; i < i_vout; i++ )
391 vlc_object_release( pp_vout[i] );
392 free( pp_vout );
395 event.type = libvlc_MediaPlayerVout;
396 event.u.media_player_vout.new_count = i_vout;
397 libvlc_event_send( &p_mi->event_manager, &event );
399 else if ( newval.i_int == INPUT_EVENT_TITLE )
401 event.type = libvlc_MediaPlayerTitleChanged;
402 event.u.media_player_title_changed.new_title = var_GetInteger( p_input, "title" );
403 libvlc_event_send( &p_mi->event_manager, &event );
405 else if ( newval.i_int == INPUT_EVENT_CHAPTER )
407 event.type = libvlc_MediaPlayerChapterChanged;
408 event.u.media_player_chapter_changed.new_chapter = var_GetInteger( p_input, "chapter" );
409 libvlc_event_send( &p_mi->event_manager, &event );
411 else if ( newval.i_int == INPUT_EVENT_ES )
413 /* Send ESSelected events from this callback ("intf-event") and not
414 * from "audio-es", "video-es", "spu-es" callbacks. Indeed, these
415 * callbacks are not triggered when the input_thread changes an ES
416 * while this callback is. */
417 struct {
418 const char *psz_name;
419 const libvlc_track_type_t type;
420 int new_es;
421 } es_list[] = {
422 { "audio-es", libvlc_track_audio, ES_INIT },
423 { "video-es", libvlc_track_video, ES_INIT },
424 { "spu-es", libvlc_track_text, ES_INIT },
426 /* Check if an ES selection changed */
427 lock( p_mi );
428 for( size_t i = 0; i < ARRAY_SIZE( es_list ); ++i )
430 int current_es = var_GetInteger( p_input, es_list[i].psz_name );
431 if( current_es != p_mi->selected_es[i] )
432 es_list[i].new_es = p_mi->selected_es[i] = current_es;
434 unlock( p_mi );
436 /* Send the ESSelected event for each ES that were newly selected */
437 for( size_t i = 0; i < ARRAY_SIZE( es_list ); ++i )
439 if( es_list[i].new_es != ES_INIT )
441 event.type = libvlc_MediaPlayerESSelected;
442 event.u.media_player_es_changed.i_type = es_list[i].type;
443 event.u.media_player_es_changed.i_id = es_list[i].new_es;
444 libvlc_event_send( &p_mi->event_manager, &event );
449 return VLC_SUCCESS;
452 static int track_type_from_name(const char *psz_name)
454 if( !strcmp( psz_name, "video-es" ) )
455 return libvlc_track_video;
456 else if( !strcmp( psz_name, "audio-es" ) )
457 return libvlc_track_audio;
458 else if( !strcmp( psz_name, "spu-es" ) )
459 return libvlc_track_text;
460 else
461 return libvlc_track_unknown;
464 static int input_es_changed( vlc_object_t *p_this,
465 char const *psz_cmd,
466 int action,
467 vlc_value_t *p_val,
468 void *p_userdata )
470 VLC_UNUSED(p_this);
471 libvlc_media_player_t *mp = p_userdata;
472 libvlc_event_t event;
474 /* Ignore the "Disable" element */
475 if (p_val && p_val->i_int < 0)
476 return VLC_EGENERIC;
478 switch (action)
480 case VLC_VAR_ADDCHOICE:
481 event.type = libvlc_MediaPlayerESAdded;
482 break;
483 case VLC_VAR_DELCHOICE:
484 case VLC_VAR_CLEARCHOICES:
485 event.type = libvlc_MediaPlayerESDeleted;
486 break;
487 default:
488 return VLC_EGENERIC;
491 event.u.media_player_es_changed.i_type = track_type_from_name(psz_cmd);
493 int i_id;
494 if (action != VLC_VAR_CLEARCHOICES)
496 if (!p_val)
497 return VLC_EGENERIC;
498 i_id = p_val->i_int;
500 else
502 /* -1 means all ES tracks of this type were deleted. */
503 i_id = -1;
505 event.u.media_player_es_changed.i_id = i_id;
507 libvlc_event_send(&mp->event_manager, &event);
509 return VLC_SUCCESS;
512 /**************************************************************************
513 * Snapshot Taken Event.
515 * FIXME: This snapshot API interface makes no sense in media_player.
516 *************************************************************************/
517 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
518 vlc_value_t oldval, vlc_value_t newval, void *p_data )
520 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
522 libvlc_media_player_t *mp = p_data;
523 libvlc_event_t event;
524 event.type = libvlc_MediaPlayerSnapshotTaken;
525 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
526 libvlc_event_send(&mp->event_manager, &event);
528 return VLC_SUCCESS;
531 static int corks_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
532 vlc_value_t cur, void *opaque)
534 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
536 if (!old.i_int != !cur.i_int)
538 libvlc_event_t event;
540 event.type = cur.i_int ? libvlc_MediaPlayerCorked
541 : libvlc_MediaPlayerUncorked;
542 libvlc_event_send(&mp->event_manager, &event);
544 VLC_UNUSED(name); VLC_UNUSED(opaque);
545 return VLC_SUCCESS;
548 static int audio_device_changed(vlc_object_t *obj, const char *name,
549 vlc_value_t old, vlc_value_t cur, void *opaque)
551 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
552 libvlc_event_t event;
554 event.type = libvlc_MediaPlayerAudioDevice;
555 event.u.media_player_audio_device.device = cur.psz_string;
556 libvlc_event_send(&mp->event_manager, &event);
557 VLC_UNUSED(name); VLC_UNUSED(old); VLC_UNUSED(opaque);
558 return VLC_SUCCESS;
561 static int mute_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
562 vlc_value_t cur, void *opaque)
564 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
566 if (old.b_bool != cur.b_bool)
568 libvlc_event_t event;
570 event.type = cur.b_bool ? libvlc_MediaPlayerMuted
571 : libvlc_MediaPlayerUnmuted;
572 libvlc_event_send(&mp->event_manager, &event);
574 VLC_UNUSED(name); VLC_UNUSED(opaque);
575 return VLC_SUCCESS;
578 static int volume_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
579 vlc_value_t cur, void *opaque)
581 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
582 libvlc_event_t event;
584 event.type = libvlc_MediaPlayerAudioVolume;
585 event.u.media_player_audio_volume.volume = cur.f_float;
586 libvlc_event_send(&mp->event_manager, &event);
587 VLC_UNUSED(name); VLC_UNUSED(old); VLC_UNUSED(opaque);
588 return VLC_SUCCESS;
591 /**************************************************************************
592 * Create a Media Instance object.
594 * Refcount strategy:
595 * - All items created by _new start with a refcount set to 1.
596 * - Accessor _release decrease the refcount by 1, if after that
597 * operation the refcount is 0, the object is destroyed.
598 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
600 * Object locking strategy:
601 * - No lock held while in constructor.
602 * - When accessing any member variable this lock is held. (XXX who locks?)
603 * - When attempting to destroy the object the lock is also held.
604 **************************************************************************/
605 libvlc_media_player_t *
606 libvlc_media_player_new( libvlc_instance_t *instance )
608 libvlc_media_player_t * mp;
610 assert(instance);
612 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
613 if (unlikely(mp == NULL))
615 libvlc_printerr("Not enough memory");
616 return NULL;
619 /* Input */
620 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
621 var_Create (mp, "sout", VLC_VAR_STRING);
622 var_Create (mp, "demux-filter", VLC_VAR_STRING);
624 /* Video */
625 var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
626 var_Create (mp, "window", VLC_VAR_STRING);
627 var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
628 var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
629 var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
630 var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
631 var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
632 var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
633 var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
634 var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
635 var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
636 var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
637 var_Create (mp, "avcodec-hw", VLC_VAR_STRING);
638 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
639 #if defined (_WIN32) || defined (__OS2__)
640 var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
641 #endif
642 #ifdef __APPLE__
643 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
644 #endif
645 #ifdef __ANDROID__
646 var_Create (mp, "drawable-androidwindow", VLC_VAR_ADDRESS);
647 #endif
648 #ifdef HAVE_EVAS
649 var_Create (mp, "drawable-evasobject", VLC_VAR_ADDRESS);
650 #endif
652 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
653 var_SetBool (mp, "keyboard-events", true);
654 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
655 var_SetBool (mp, "mouse-events", true);
657 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
658 var_Create (mp, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
659 var_Create (mp, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
660 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
661 var_Create (mp, "crop", VLC_VAR_STRING);
662 var_Create (mp, "deinterlace", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
663 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
665 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
666 var_SetInteger (mp, "vbi-page", 0);
668 var_Create (mp, "video-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
669 var_Create (mp, "sub-source", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
670 var_Create (mp, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
672 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
673 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
674 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
675 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
676 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
677 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
678 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
679 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
680 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
682 var_Create (mp, "logo-file", VLC_VAR_STRING);
683 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
684 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
685 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
686 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
687 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
688 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
690 var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
691 var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
692 var_Create (mp, "hue", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
693 var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
694 var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
696 /* Audio */
697 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
698 var_Create (mp, "audio-device", VLC_VAR_STRING);
699 var_Create (mp, "mute", VLC_VAR_BOOL);
700 var_Create (mp, "volume", VLC_VAR_FLOAT);
701 var_Create (mp, "corks", VLC_VAR_INTEGER);
702 var_Create (mp, "audio-filter", VLC_VAR_STRING);
703 var_Create (mp, "role", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
704 var_Create (mp, "amem-data", VLC_VAR_ADDRESS);
705 var_Create (mp, "amem-setup", VLC_VAR_ADDRESS);
706 var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
707 var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
708 var_Create (mp, "amem-pause", VLC_VAR_ADDRESS);
709 var_Create (mp, "amem-resume", VLC_VAR_ADDRESS);
710 var_Create (mp, "amem-flush", VLC_VAR_ADDRESS);
711 var_Create (mp, "amem-drain", VLC_VAR_ADDRESS);
712 var_Create (mp, "amem-set-volume", VLC_VAR_ADDRESS);
713 var_Create (mp, "amem-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
714 var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
715 var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
717 /* Video Title */
718 var_Create (mp, "video-title-show", VLC_VAR_BOOL);
719 var_Create (mp, "video-title-position", VLC_VAR_INTEGER);
720 var_Create (mp, "video-title-timeout", VLC_VAR_INTEGER);
722 /* Equalizer */
723 var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
724 var_Create (mp, "equalizer-vlcfreqs", VLC_VAR_BOOL);
725 var_Create (mp, "equalizer-bands", VLC_VAR_STRING);
727 mp->p_md = NULL;
728 mp->state = libvlc_NothingSpecial;
729 mp->p_libvlc_instance = instance;
730 mp->input.p_thread = NULL;
731 mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
732 if (unlikely(mp->input.p_resource == NULL))
734 vlc_object_release(mp);
735 return NULL;
737 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
738 if( aout != NULL )
739 input_resource_PutAout(mp->input.p_resource, aout);
741 vlc_viewpoint_init(&mp->viewpoint);
743 var_Create (mp, "viewpoint", VLC_VAR_ADDRESS);
744 var_SetAddress( mp, "viewpoint", &mp->viewpoint );
745 vlc_mutex_init (&mp->input.lock);
746 mp->i_refcount = 1;
747 libvlc_event_manager_init(&mp->event_manager, mp);
748 vlc_mutex_init(&mp->object_lock);
750 var_AddCallback(mp, "corks", corks_changed, NULL);
751 var_AddCallback(mp, "audio-device", audio_device_changed, NULL);
752 var_AddCallback(mp, "mute", mute_changed, NULL);
753 var_AddCallback(mp, "volume", volume_changed, NULL);
755 /* Snapshot initialization */
756 /* Attach a var callback to the global object to provide the glue between
757 * vout_thread that generates the event and media_player that re-emits it
758 * with its own event manager
760 * FIXME: It's unclear why we want to put this in public API, and why we
761 * want to expose it in such a limiting and ugly way.
763 var_AddCallback(mp->obj.libvlc, "snapshot-file", snapshot_was_taken, mp);
765 libvlc_retain(instance);
766 return mp;
769 /**************************************************************************
770 * Create a Media Instance object with a media descriptor.
771 **************************************************************************/
772 libvlc_media_player_t *
773 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
775 libvlc_media_player_t * p_mi;
777 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
778 if( !p_mi )
779 return NULL;
781 libvlc_media_retain( p_md );
782 p_mi->p_md = p_md;
784 return p_mi;
787 /**************************************************************************
788 * Destroy a Media Instance object (libvlc internal)
790 * Warning: No lock held here, but hey, this is internal. Caller must lock.
791 **************************************************************************/
792 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
794 assert( p_mi );
796 /* Detach Callback from the main libvlc object */
797 var_DelCallback( p_mi->obj.libvlc,
798 "snapshot-file", snapshot_was_taken, p_mi );
800 /* Detach callback from the media player / input manager object */
801 var_DelCallback( p_mi, "volume", volume_changed, NULL );
802 var_DelCallback( p_mi, "mute", mute_changed, NULL );
803 var_DelCallback( p_mi, "audio-device", audio_device_changed, NULL );
804 var_DelCallback( p_mi, "corks", corks_changed, NULL );
806 /* No need for lock_input() because no other threads knows us anymore */
807 if( p_mi->input.p_thread )
808 release_input_thread(p_mi);
809 input_resource_Terminate( p_mi->input.p_resource );
810 input_resource_Release( p_mi->input.p_resource );
811 vlc_mutex_destroy( &p_mi->input.lock );
813 libvlc_event_manager_destroy(&p_mi->event_manager);
814 libvlc_media_release( p_mi->p_md );
815 vlc_mutex_destroy( &p_mi->object_lock );
817 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
818 vlc_object_release( p_mi );
819 libvlc_release(instance);
822 /**************************************************************************
823 * Release a Media Instance object.
825 * Function does the locking.
826 **************************************************************************/
827 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
829 bool destroy;
831 assert( p_mi );
832 lock(p_mi);
833 destroy = !--p_mi->i_refcount;
834 unlock(p_mi);
836 if( destroy )
837 libvlc_media_player_destroy( p_mi );
840 /**************************************************************************
841 * Retain a Media Instance object.
843 * Caller must hold the lock.
844 **************************************************************************/
845 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
847 assert( p_mi );
849 lock(p_mi);
850 p_mi->i_refcount++;
851 unlock(p_mi);
854 /**************************************************************************
855 * Set the Media descriptor associated with the instance.
857 * Enter without lock -- function will lock the object.
858 **************************************************************************/
859 void libvlc_media_player_set_media(
860 libvlc_media_player_t *p_mi,
861 libvlc_media_t *p_md )
863 lock_input(p_mi);
865 release_input_thread( p_mi );
867 lock( p_mi );
868 set_state( p_mi, libvlc_NothingSpecial, true );
869 unlock_input( p_mi );
871 libvlc_media_release( p_mi->p_md );
873 if( !p_md )
875 p_mi->p_md = NULL;
876 unlock(p_mi);
877 return; /* It is ok to pass a NULL md */
880 libvlc_media_retain( p_md );
881 p_mi->p_md = p_md;
883 /* The policy here is to ignore that we were created using a different
884 * libvlc_instance, because we don't really care */
885 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
887 unlock(p_mi);
889 /* Send an event for the newly available media */
890 libvlc_event_t event;
891 event.type = libvlc_MediaPlayerMediaChanged;
892 event.u.media_player_media_changed.new_media = p_md;
893 libvlc_event_send( &p_mi->event_manager, &event );
897 /**************************************************************************
898 * Get the Media descriptor associated with the instance.
899 **************************************************************************/
900 libvlc_media_t *
901 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
903 libvlc_media_t *p_m;
905 lock( p_mi );
906 p_m = p_mi->p_md;
907 if( p_m )
908 libvlc_media_retain( p_m );
909 unlock( p_mi );
911 return p_m;
914 /**************************************************************************
915 * Get the event Manager.
916 **************************************************************************/
917 libvlc_event_manager_t *
918 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
920 return &p_mi->event_manager;
923 static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
925 var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
926 var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
927 var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
930 static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
932 var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
933 var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
934 var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
937 /**************************************************************************
938 * Tell media player to start playing.
939 **************************************************************************/
940 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
942 lock_input( p_mi );
944 input_thread_t *p_input_thread = p_mi->input.p_thread;
945 if( p_input_thread )
947 /* A thread already exists, send it a play message */
948 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
949 unlock_input( p_mi );
950 return 0;
953 /* Ignore previous exception */
954 lock(p_mi);
956 if( !p_mi->p_md )
958 unlock(p_mi);
959 unlock_input( p_mi );
960 libvlc_printerr( "No associated media descriptor" );
961 return -1;
964 for( size_t i = 0; i < ARRAY_SIZE( p_mi->selected_es ); ++i )
965 p_mi->selected_es[i] = ES_INIT;
967 media_attach_preparsed_event( p_mi->p_md );
969 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
970 p_mi->input.p_resource );
971 unlock(p_mi);
972 if( !p_input_thread )
974 unlock_input(p_mi);
975 media_detach_preparsed_event( p_mi->p_md );
976 libvlc_printerr( "Not enough memory" );
977 return -1;
980 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
981 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
982 var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
983 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
984 add_es_callbacks( p_input_thread, p_mi );
986 if( input_Start( p_input_thread ) )
988 unlock_input(p_mi);
989 del_es_callbacks( p_input_thread, p_mi );
990 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
991 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
992 var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
993 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
994 input_Close( p_input_thread );
995 media_detach_preparsed_event( p_mi->p_md );
996 libvlc_printerr( "Input initialization failure" );
997 return -1;
999 p_mi->input.p_thread = p_input_thread;
1000 unlock_input(p_mi);
1001 return 0;
1004 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
1006 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
1007 if( !p_input_thread )
1008 return;
1010 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
1011 if( state == libvlc_Playing )
1013 if( paused )
1015 if( libvlc_media_player_can_pause( p_mi ) )
1016 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
1017 else
1018 input_Stop( p_input_thread );
1021 else
1023 if( !paused )
1024 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
1027 vlc_object_release( p_input_thread );
1030 /**************************************************************************
1031 * Toggle pause.
1032 **************************************************************************/
1033 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
1035 libvlc_media_player_set_pause( p_mi, libvlc_media_player_is_playing( p_mi ) );
1038 /**************************************************************************
1039 * Tells whether the media player is currently playing.
1040 **************************************************************************/
1041 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
1043 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
1044 return libvlc_Playing == state;
1047 /**************************************************************************
1048 * Stop playing.
1049 **************************************************************************/
1050 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
1052 lock_input(p_mi);
1053 release_input_thread( p_mi ); /* This will stop the input thread */
1055 /* Force to go to stopped state, in case we were in Ended, or Error
1056 * state. */
1057 if( p_mi->state != libvlc_Stopped )
1059 set_state( p_mi, libvlc_Stopped, false );
1061 /* Construct and send the event */
1062 libvlc_event_t event;
1063 event.type = libvlc_MediaPlayerStopped;
1064 libvlc_event_send( &p_mi->event_manager, &event );
1067 input_resource_Terminate( p_mi->input.p_resource );
1068 unlock_input(p_mi);
1071 int libvlc_media_player_set_renderer( libvlc_media_player_t *p_mi,
1072 const libvlc_renderer_item_t *p_litem )
1074 const vlc_renderer_item_t *p_item = libvlc_renderer_item_to_vlc( p_litem );
1075 char *psz_sout;
1076 if( asprintf( &psz_sout, "#%s", vlc_renderer_item_sout( p_item ) ) == -1 )
1077 return -1;
1079 var_SetString( p_mi, "sout", psz_sout );
1080 var_SetString( p_mi, "demux-filter", vlc_renderer_item_demux_filter( p_item ) );
1081 free( psz_sout );
1082 return 0;
1085 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
1086 void *(*lock_cb) (void *, void **),
1087 void (*unlock_cb) (void *, void *, void *const *),
1088 void (*display_cb) (void *, void *),
1089 void *opaque )
1091 var_SetAddress( mp, "vmem-lock", lock_cb );
1092 var_SetAddress( mp, "vmem-unlock", unlock_cb );
1093 var_SetAddress( mp, "vmem-display", display_cb );
1094 var_SetAddress( mp, "vmem-data", opaque );
1095 var_SetString( mp, "avcodec-hw", "none" );
1096 var_SetString( mp, "vout", "vmem" );
1097 var_SetString( mp, "window", "none" );
1100 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
1101 libvlc_video_format_cb setup,
1102 libvlc_video_cleanup_cb cleanup )
1104 var_SetAddress( mp, "vmem-setup", setup );
1105 var_SetAddress( mp, "vmem-cleanup", cleanup );
1108 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
1109 unsigned width, unsigned height, unsigned pitch )
1111 var_SetString( mp, "vmem-chroma", chroma );
1112 var_SetInteger( mp, "vmem-width", width );
1113 var_SetInteger( mp, "vmem-height", height );
1114 var_SetInteger( mp, "vmem-pitch", pitch );
1117 /**************************************************************************
1118 * set_nsobject
1119 **************************************************************************/
1120 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1121 void * drawable )
1123 assert (p_mi != NULL);
1124 #ifdef __APPLE__
1125 var_SetString (p_mi, "avcodec-hw", "");
1126 var_SetString (p_mi, "vout", "");
1127 var_SetString (p_mi, "window", "");
1128 var_SetAddress (p_mi, "drawable-nsobject", drawable);
1129 #else
1130 (void)drawable;
1131 libvlc_printerr ("can't set nsobject: APPLE build required");
1132 assert(false);
1133 var_SetString (p_mi, "vout", "none");
1134 var_SetString (p_mi, "window", "none");
1135 #endif
1138 /**************************************************************************
1139 * get_nsobject
1140 **************************************************************************/
1141 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1143 assert (p_mi != NULL);
1144 #ifdef __APPLE__
1145 return var_GetAddress (p_mi, "drawable-nsobject");
1146 #else
1147 (void) p_mi;
1148 return NULL;
1149 #endif
1152 /**************************************************************************
1153 * set_agl
1154 **************************************************************************/
1155 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
1156 uint32_t drawable )
1158 (void)drawable;
1159 libvlc_printerr ("can't set agl: use libvlc_media_player_set_nsobject instead");
1160 assert(false);
1161 var_SetString (p_mi, "vout", "none");
1162 var_SetString (p_mi, "window", "none");
1165 /**************************************************************************
1166 * get_agl
1167 **************************************************************************/
1168 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
1170 (void) p_mi;
1171 return 0;
1174 /**************************************************************************
1175 * set_xwindow
1176 **************************************************************************/
1177 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1178 uint32_t drawable )
1180 assert (p_mi != NULL);
1182 var_SetString (p_mi, "avcodec-hw", "");
1183 var_SetString (p_mi, "vout", drawable ? "xid" : "");
1184 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1185 var_SetInteger (p_mi, "drawable-xid", drawable);
1188 /**************************************************************************
1189 * get_xwindow
1190 **************************************************************************/
1191 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1193 return var_GetInteger (p_mi, "drawable-xid");
1196 /**************************************************************************
1197 * set_hwnd
1198 **************************************************************************/
1199 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1200 void *drawable )
1202 assert (p_mi != NULL);
1203 #if defined (_WIN32) || defined (__OS2__)
1204 var_SetString (p_mi, "avcodec-hw", "");
1205 var_SetString (p_mi, "vout", "");
1206 var_SetString (p_mi, "window",
1207 (drawable != NULL) ? "embed-hwnd,any" : "");
1208 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1209 #else
1210 (void) drawable;
1211 libvlc_printerr ("can't set nsobject: WIN32 build required");
1212 assert(false);
1213 var_SetString (p_mi, "vout", "none");
1214 var_SetString (p_mi, "window", "none");
1215 #endif
1218 /**************************************************************************
1219 * get_hwnd
1220 **************************************************************************/
1221 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1223 assert (p_mi != NULL);
1224 #if defined (_WIN32) || defined (__OS2__)
1225 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1226 #else
1227 (void) p_mi;
1228 return NULL;
1229 #endif
1232 /**************************************************************************
1233 * set_android_context
1234 **************************************************************************/
1235 void libvlc_media_player_set_android_context( libvlc_media_player_t *p_mi,
1236 void *p_awindow_handler )
1238 assert (p_mi != NULL);
1239 #ifdef __ANDROID__
1240 var_SetAddress (p_mi, "drawable-androidwindow", p_awindow_handler);
1241 #else
1242 (void) p_awindow_handler;
1243 libvlc_printerr ("can't set android context: ANDROID build required");
1244 assert(false);
1245 var_SetString (p_mi, "vout", "none");
1246 var_SetString (p_mi, "window", "none");
1247 #endif
1250 /**************************************************************************
1251 * set_evas_object
1252 **************************************************************************/
1253 int libvlc_media_player_set_evas_object( libvlc_media_player_t *p_mi,
1254 void *p_evas_object )
1256 assert (p_mi != NULL);
1257 #ifdef HAVE_EVAS
1258 var_SetString (p_mi, "vout", "evas");
1259 var_SetString (p_mi, "window", "none");
1260 var_SetAddress (p_mi, "drawable-evasobject", p_evas_object);
1261 return 0;
1262 #else
1263 (void) p_mi; (void) p_evas_object;
1264 return -1;
1265 #endif
1268 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1269 libvlc_audio_play_cb play_cb,
1270 libvlc_audio_pause_cb pause_cb,
1271 libvlc_audio_resume_cb resume_cb,
1272 libvlc_audio_flush_cb flush_cb,
1273 libvlc_audio_drain_cb drain_cb,
1274 void *opaque )
1276 var_SetAddress( mp, "amem-play", play_cb );
1277 var_SetAddress( mp, "amem-pause", pause_cb );
1278 var_SetAddress( mp, "amem-resume", resume_cb );
1279 var_SetAddress( mp, "amem-flush", flush_cb );
1280 var_SetAddress( mp, "amem-drain", drain_cb );
1281 var_SetAddress( mp, "amem-data", opaque );
1282 var_SetString( mp, "aout", "amem,none" );
1284 input_resource_ResetAout(mp->input.p_resource);
1287 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1288 libvlc_audio_set_volume_cb cb )
1290 var_SetAddress( mp, "amem-set-volume", cb );
1292 input_resource_ResetAout(mp->input.p_resource);
1295 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1296 libvlc_audio_setup_cb setup,
1297 libvlc_audio_cleanup_cb cleanup )
1299 var_SetAddress( mp, "amem-setup", setup );
1300 var_SetAddress( mp, "amem-cleanup", cleanup );
1302 input_resource_ResetAout(mp->input.p_resource);
1305 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1306 unsigned rate, unsigned channels )
1308 var_SetString( mp, "amem-format", format );
1309 var_SetInteger( mp, "amem-rate", rate );
1310 var_SetInteger( mp, "amem-channels", channels );
1312 input_resource_ResetAout(mp->input.p_resource);
1316 /**************************************************************************
1317 * Getters for stream information
1318 **************************************************************************/
1319 libvlc_time_t libvlc_media_player_get_length(
1320 libvlc_media_player_t *p_mi )
1322 input_thread_t *p_input_thread;
1323 libvlc_time_t i_time;
1325 p_input_thread = libvlc_get_input_thread ( p_mi );
1326 if( !p_input_thread )
1327 return -1;
1329 i_time = from_mtime(var_GetInteger( p_input_thread, "length" ));
1330 vlc_object_release( p_input_thread );
1332 return i_time;
1335 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1337 input_thread_t *p_input_thread;
1338 libvlc_time_t i_time;
1340 p_input_thread = libvlc_get_input_thread ( p_mi );
1341 if( !p_input_thread )
1342 return -1;
1344 i_time = from_mtime(var_GetInteger( p_input_thread , "time" ));
1345 vlc_object_release( p_input_thread );
1346 return i_time;
1349 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1350 libvlc_time_t i_time )
1352 input_thread_t *p_input_thread;
1354 p_input_thread = libvlc_get_input_thread ( p_mi );
1355 if( !p_input_thread )
1356 return;
1358 var_SetInteger( p_input_thread, "time", to_mtime(i_time) );
1359 vlc_object_release( p_input_thread );
1362 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1363 float position )
1365 input_thread_t *p_input_thread;
1367 p_input_thread = libvlc_get_input_thread ( p_mi );
1368 if( !p_input_thread )
1369 return;
1371 var_SetFloat( p_input_thread, "position", position );
1372 vlc_object_release( p_input_thread );
1375 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1377 input_thread_t *p_input_thread;
1378 float f_position;
1380 p_input_thread = libvlc_get_input_thread ( p_mi );
1381 if( !p_input_thread )
1382 return -1.0;
1384 f_position = var_GetFloat( p_input_thread, "position" );
1385 vlc_object_release( p_input_thread );
1387 return f_position;
1390 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1391 int chapter )
1393 input_thread_t *p_input_thread;
1395 p_input_thread = libvlc_get_input_thread ( p_mi );
1396 if( !p_input_thread )
1397 return;
1399 var_SetInteger( p_input_thread, "chapter", chapter );
1400 vlc_object_release( p_input_thread );
1403 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1405 input_thread_t *p_input_thread;
1406 int i_chapter;
1408 p_input_thread = libvlc_get_input_thread ( p_mi );
1409 if( !p_input_thread )
1410 return -1;
1412 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1413 vlc_object_release( p_input_thread );
1415 return i_chapter;
1418 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1420 input_thread_t *p_input_thread;
1421 vlc_value_t val;
1423 p_input_thread = libvlc_get_input_thread ( p_mi );
1424 if( !p_input_thread )
1425 return -1;
1427 int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1428 vlc_object_release( p_input_thread );
1430 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1433 int libvlc_media_player_get_chapter_count_for_title(
1434 libvlc_media_player_t *p_mi,
1435 int i_title )
1437 vlc_value_t val;
1439 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1440 if( !p_input_thread )
1441 return -1;
1443 char psz_name[sizeof ("title ") + 3 * sizeof (int)];
1444 sprintf( psz_name, "title %2u", i_title );
1446 int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1447 vlc_object_release( p_input_thread );
1449 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1452 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1453 int i_title )
1455 input_thread_t *p_input_thread;
1457 p_input_thread = libvlc_get_input_thread ( p_mi );
1458 if( !p_input_thread )
1459 return;
1461 var_SetInteger( p_input_thread, "title", i_title );
1462 vlc_object_release( p_input_thread );
1464 //send event
1465 libvlc_event_t event;
1466 event.type = libvlc_MediaPlayerTitleChanged;
1467 event.u.media_player_title_changed.new_title = i_title;
1468 libvlc_event_send( &p_mi->event_manager, &event );
1471 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1473 input_thread_t *p_input_thread;
1474 int i_title;
1476 p_input_thread = libvlc_get_input_thread ( p_mi );
1477 if( !p_input_thread )
1478 return -1;
1480 i_title = var_GetInteger( p_input_thread, "title" );
1481 vlc_object_release( p_input_thread );
1483 return i_title;
1486 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1488 input_thread_t *p_input_thread;
1489 vlc_value_t val;
1491 p_input_thread = libvlc_get_input_thread ( p_mi );
1492 if( !p_input_thread )
1493 return -1;
1495 int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1496 vlc_object_release( p_input_thread );
1498 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1501 int libvlc_media_player_get_full_title_descriptions( libvlc_media_player_t *p_mi,
1502 libvlc_title_description_t *** pp_titles )
1504 assert( p_mi );
1506 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1508 if( !p_input_thread )
1509 return -1;
1511 input_title_t **p_input_title;
1512 int count;
1514 /* fetch data */
1515 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO,
1516 &p_input_title, &count );
1517 vlc_object_release( p_input_thread );
1518 if( ret != VLC_SUCCESS )
1519 return -1;
1521 libvlc_title_description_t **titles = malloc( count * sizeof (*titles) );
1522 if( count > 0 && unlikely(titles == NULL) )
1523 return -1;
1525 /* fill array */
1526 for( int i = 0; i < count; i++)
1528 libvlc_title_description_t *title = malloc( sizeof (*title) );
1529 if( unlikely(title == NULL) )
1531 libvlc_title_descriptions_release( titles, i );
1532 return -1;
1534 titles[i] = title;
1536 /* we want to return milliseconds to match the rest of the API */
1537 title->i_duration = p_input_title[i]->i_length / 1000;
1538 title->i_flags = p_input_title[i]->i_flags;
1539 if( p_input_title[i]->psz_name )
1540 title->psz_name = strdup( p_input_title[i]->psz_name );
1541 else
1542 title->psz_name = NULL;
1543 vlc_input_title_Delete( p_input_title[i] );
1545 free( p_input_title );
1547 *pp_titles = titles;
1548 return count;
1551 void libvlc_title_descriptions_release( libvlc_title_description_t **p_titles,
1552 unsigned i_count )
1554 for (unsigned i = 0; i < i_count; i++ )
1556 if ( !p_titles[i] )
1557 continue;
1559 free( p_titles[i]->psz_name );
1560 free( p_titles[i] );
1562 free( p_titles );
1565 int libvlc_media_player_get_full_chapter_descriptions( libvlc_media_player_t *p_mi,
1566 int i_chapters_of_title,
1567 libvlc_chapter_description_t *** pp_chapters )
1569 assert( p_mi );
1571 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1573 if( !p_input_thread )
1574 return -1;
1576 seekpoint_t **p_seekpoint = NULL;
1578 /* fetch data */
1579 int ret = input_Control(p_input_thread, INPUT_GET_SEEKPOINTS, &p_seekpoint, &i_chapters_of_title);
1580 vlc_object_release( p_input_thread );
1582 if( ret != VLC_SUCCESS)
1584 return -1;
1587 if (i_chapters_of_title == 0 || p_seekpoint == NULL)
1589 return 0;
1592 const int ci_chapter_count = (const int)i_chapters_of_title;
1594 *pp_chapters = calloc( ci_chapter_count, sizeof(**pp_chapters) );
1595 if( !*pp_chapters )
1597 return -1;
1600 /* fill array */
1601 for( int i = 0; i < ci_chapter_count; i++)
1603 libvlc_chapter_description_t *p_chapter = malloc( sizeof(*p_chapter) );
1604 if( unlikely(p_chapter == NULL) )
1606 libvlc_chapter_descriptions_release( *pp_chapters, ci_chapter_count );
1607 return -1;
1609 (*pp_chapters)[i] = p_chapter;
1611 p_chapter->i_time_offset = p_seekpoint[i]->i_time_offset / 1000;
1613 if( i > 0 )
1615 p_chapter->i_duration = p_chapter->i_time_offset - (*pp_chapters)[i-1]->i_time_offset;
1617 else
1619 p_chapter->i_duration = p_chapter->i_time_offset;
1622 if( p_seekpoint[i]->psz_name )
1624 p_chapter->psz_name = strdup( p_seekpoint[i]->psz_name );
1626 else
1628 p_chapter->psz_name = NULL;
1630 vlc_seekpoint_Delete( p_seekpoint[i] );
1633 return ci_chapter_count;
1636 void libvlc_chapter_descriptions_release( libvlc_chapter_description_t **p_chapters,
1637 unsigned i_count )
1639 for (unsigned i = 0; i < i_count; i++ )
1641 if ( !p_chapters[i] )
1642 continue;
1644 free( p_chapters[i]->psz_name );
1645 free( p_chapters[i] );
1647 free( p_chapters );
1650 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1652 input_thread_t *p_input_thread;
1654 p_input_thread = libvlc_get_input_thread ( p_mi );
1655 if( !p_input_thread )
1656 return;
1658 int i_type = var_Type( p_input_thread, "next-chapter" );
1659 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1660 "next-chapter":"next-title" );
1662 vlc_object_release( p_input_thread );
1665 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1667 input_thread_t *p_input_thread;
1669 p_input_thread = libvlc_get_input_thread ( p_mi );
1670 if( !p_input_thread )
1671 return;
1673 int i_type = var_Type( p_input_thread, "next-chapter" );
1674 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1675 "prev-chapter":"prev-title" );
1677 vlc_object_release( p_input_thread );
1680 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1682 libvlc_media_t *media = libvlc_media_player_get_media( p_mi );
1683 if( media == NULL )
1684 return 0.f;
1686 input_item_t *item = media->p_input_item;
1687 float fps = 0.f;
1689 vlc_mutex_lock( &item->lock );
1690 for( int i = 0; i < item->i_es; i++ )
1692 const es_format_t *fmt = item->es[i];
1694 if( fmt->i_cat == VIDEO_ES && fmt->video.i_frame_rate_base > 0 )
1695 fps = (float)fmt->video.i_frame_rate
1696 / (float)fmt->video.i_frame_rate_base;
1698 vlc_mutex_unlock( &item->lock );
1699 libvlc_media_release( media );
1701 return fps;
1704 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1706 input_thread_t *p_input_thread =
1707 libvlc_get_input_thread ( p_mi );
1708 if ( !p_input_thread )
1709 return false;
1711 int state = var_GetInteger( p_input_thread, "state" );
1712 vlc_object_release( p_input_thread );
1714 return state != END_S && state != ERROR_S;
1717 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1719 var_SetFloat (p_mi, "rate", rate);
1721 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1722 if( !p_input_thread )
1723 return 0;
1724 var_SetFloat( p_input_thread, "rate", rate );
1725 vlc_object_release( p_input_thread );
1726 return 0;
1729 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1731 return var_GetFloat (p_mi, "rate");
1734 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1736 lock(p_mi);
1737 libvlc_state_t state = p_mi->state;
1738 unlock(p_mi);
1739 return state;
1742 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1744 input_thread_t *p_input_thread;
1745 bool b_seekable;
1747 p_input_thread = libvlc_get_input_thread ( p_mi );
1748 if ( !p_input_thread )
1749 return false;
1750 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1751 vlc_object_release( p_input_thread );
1753 return b_seekable;
1756 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1757 unsigned navigate )
1759 static const enum input_query_e map[] =
1761 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1762 INPUT_NAV_LEFT, INPUT_NAV_RIGHT, INPUT_NAV_POPUP,
1765 if( navigate >= sizeof(map) / sizeof(map[0]) )
1766 return;
1768 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1769 if ( p_input == NULL )
1770 return;
1772 input_Control( p_input, map[navigate], NULL );
1773 vlc_object_release( p_input );
1776 /* internal function, used by audio, video */
1777 libvlc_track_description_t *
1778 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1779 const char *psz_variable )
1781 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1782 libvlc_track_description_t *p_track_description = NULL,
1783 *p_actual, *p_previous;
1785 if( !p_input )
1786 return NULL;
1788 vlc_value_t val_list, text_list;
1789 int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES, &val_list, &text_list );
1790 if( i_ret != VLC_SUCCESS )
1791 return NULL;
1793 /* no tracks */
1794 if( val_list.p_list->i_count <= 0 )
1795 goto end;
1797 p_track_description = malloc( sizeof *p_track_description );
1798 if ( !p_track_description )
1800 libvlc_printerr( "Not enough memory" );
1801 goto end;
1803 p_actual = p_track_description;
1804 p_previous = NULL;
1805 for( int i = 0; i < val_list.p_list->i_count; i++ )
1807 if( !p_actual )
1809 p_actual = malloc( sizeof *p_actual );
1810 if ( !p_actual )
1812 libvlc_track_description_list_release( p_track_description );
1813 libvlc_printerr( "Not enough memory" );
1815 p_track_description = NULL;
1816 goto end;
1819 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1820 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1821 p_actual->p_next = NULL;
1822 if( p_previous )
1823 p_previous->p_next = p_actual;
1824 p_previous = p_actual;
1825 p_actual = NULL;
1828 end:
1829 var_FreeList( &val_list, &text_list );
1830 vlc_object_release( p_input );
1832 return p_track_description;
1835 // Deprecated alias for libvlc_track_description_list_release
1836 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1838 libvlc_track_description_list_release( p_td );
1841 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1843 libvlc_track_description_t *p_actual, *p_before;
1844 p_actual = p_td;
1846 while ( p_actual )
1848 free( p_actual->psz_name );
1849 p_before = p_actual;
1850 p_actual = p_before->p_next;
1851 free( p_before );
1855 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1857 input_thread_t *p_input_thread;
1858 bool b_can_pause;
1860 p_input_thread = libvlc_get_input_thread ( p_mi );
1861 if ( !p_input_thread )
1862 return false;
1863 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1864 vlc_object_release( p_input_thread );
1866 return b_can_pause;
1869 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1871 input_thread_t *p_input_thread;
1872 bool b_program_scrambled;
1874 p_input_thread = libvlc_get_input_thread ( p_mi );
1875 if ( !p_input_thread )
1876 return false;
1877 b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1878 vlc_object_release( p_input_thread );
1880 return b_program_scrambled;
1883 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1885 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1886 if( p_input_thread != NULL )
1888 var_TriggerCallback( p_input_thread, "frame-next" );
1889 vlc_object_release( p_input_thread );
1894 * Private lookup table to get subpicture alignment flag values corresponding
1895 * to a libvlc_position_t enumerated value.
1897 static const unsigned char position_subpicture_alignment[] = {
1898 [libvlc_position_center] = 0,
1899 [libvlc_position_left] = SUBPICTURE_ALIGN_LEFT,
1900 [libvlc_position_right] = SUBPICTURE_ALIGN_RIGHT,
1901 [libvlc_position_top] = SUBPICTURE_ALIGN_TOP,
1902 [libvlc_position_top_left] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1903 [libvlc_position_top_right] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1904 [libvlc_position_bottom] = SUBPICTURE_ALIGN_BOTTOM,
1905 [libvlc_position_bottom_left] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1906 [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1909 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1911 assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1913 if ( position != libvlc_position_disable )
1915 var_SetBool( p_mi, "video-title-show", true );
1916 var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1917 var_SetInteger( p_mi, "video-title-timeout", timeout );
1919 else
1921 var_SetBool( p_mi, "video-title-show", false );
1925 int libvlc_media_player_add_slave( libvlc_media_player_t *p_mi,
1926 libvlc_media_slave_type_t i_type,
1927 const char *psz_uri, bool b_select )
1929 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1931 if( p_input_thread == NULL )
1933 libvlc_media_t *p_media = libvlc_media_player_get_media( p_mi );
1934 if( p_media == NULL )
1935 return -1;
1937 int i_ret = libvlc_media_slaves_add( p_media, i_type, 4, psz_uri );
1938 libvlc_media_release( p_media );
1939 return i_ret;
1941 else
1943 int i_ret = input_AddSlave( p_input_thread, (enum slave_type) i_type,
1944 psz_uri, b_select );
1945 vlc_object_release( p_input_thread );
1947 return i_ret == VLC_SUCCESS ? 0 : -1;
1952 * Maximum size of a formatted equalizer amplification band frequency value.
1954 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1956 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1957 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1958 * terminator).
1960 #define EQZ_BAND_VALUE_SIZE 12
1962 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1964 char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1966 if( p_equalizer != NULL )
1968 for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1970 c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
1971 p_equalizer->f_amp[i] );
1972 if( unlikely(c >= sizeof(bands)) )
1973 return -1;
1976 var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
1977 var_SetString( p_mi, "equalizer-bands", bands );
1979 var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1981 audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
1982 if( p_aout != NULL )
1984 if( p_equalizer != NULL )
1986 var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
1987 var_SetString( p_aout, "equalizer-bands", bands );
1990 var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
1991 vlc_object_release( p_aout );
1994 return 0;
1997 static const char roles[][16] =
1999 [libvlc_role_Music] = "music",
2000 [libvlc_role_Video] = "video",
2001 [libvlc_role_Communication] = "communication",
2002 [libvlc_role_Game] = "game",
2003 [liblvc_role_Notification] = "notification",
2004 [libvlc_role_Animation] = "animation",
2005 [libvlc_role_Production] = "production",
2006 [libvlc_role_Accessibility] = "accessibility",
2007 [libvlc_role_Test] = "test",
2010 int libvlc_media_player_set_role(libvlc_media_player_t *mp, unsigned role)
2012 if (role >= ARRAY_SIZE(roles)
2013 || var_SetString(mp, "role", roles[role]) != VLC_SUCCESS)
2014 return -1;
2015 return 0;
2018 int libvlc_media_player_get_role(libvlc_media_player_t *mp)
2020 int ret = -1;
2021 char *str = var_GetString(mp, "role");
2022 if (str == NULL)
2023 return 0;
2025 for (size_t i = 0; i < ARRAY_SIZE(roles); i++)
2026 if (!strcmp(roles[i], str))
2028 ret = i;
2029 break;
2032 free(str);
2033 return ret;