chromecast: Hide some private variable from settings
[vlc.git] / lib / media_player.c
blob6e00a4547ff8ada6732c4d80b21e45b7a4b24ed3
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_actions.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", 100);
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_renderer = NULL;
732 mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
733 if (unlikely(mp->input.p_resource == NULL))
735 vlc_object_release(mp);
736 return NULL;
738 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
739 if( aout != NULL )
740 input_resource_PutAout(mp->input.p_resource, aout);
742 vlc_viewpoint_init(&mp->viewpoint);
744 var_Create (mp, "viewpoint", VLC_VAR_ADDRESS);
745 var_SetAddress( mp, "viewpoint", &mp->viewpoint );
746 vlc_mutex_init (&mp->input.lock);
747 mp->i_refcount = 1;
748 libvlc_event_manager_init(&mp->event_manager, mp);
749 vlc_mutex_init(&mp->object_lock);
751 var_AddCallback(mp, "corks", corks_changed, NULL);
752 var_AddCallback(mp, "audio-device", audio_device_changed, NULL);
753 var_AddCallback(mp, "mute", mute_changed, NULL);
754 var_AddCallback(mp, "volume", volume_changed, NULL);
756 /* Snapshot initialization */
757 /* Attach a var callback to the global object to provide the glue between
758 * vout_thread that generates the event and media_player that re-emits it
759 * with its own event manager
761 * FIXME: It's unclear why we want to put this in public API, and why we
762 * want to expose it in such a limiting and ugly way.
764 var_AddCallback(mp->obj.libvlc, "snapshot-file", snapshot_was_taken, mp);
766 libvlc_retain(instance);
767 return mp;
770 /**************************************************************************
771 * Create a Media Instance object with a media descriptor.
772 **************************************************************************/
773 libvlc_media_player_t *
774 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
776 libvlc_media_player_t * p_mi;
778 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
779 if( !p_mi )
780 return NULL;
782 libvlc_media_retain( p_md );
783 p_mi->p_md = p_md;
785 return p_mi;
788 /**************************************************************************
789 * Destroy a Media Instance object (libvlc internal)
791 * Warning: No lock held here, but hey, this is internal. Caller must lock.
792 **************************************************************************/
793 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
795 assert( p_mi );
797 /* Detach Callback from the main libvlc object */
798 var_DelCallback( p_mi->obj.libvlc,
799 "snapshot-file", snapshot_was_taken, p_mi );
801 /* Detach callback from the media player / input manager object */
802 var_DelCallback( p_mi, "volume", volume_changed, NULL );
803 var_DelCallback( p_mi, "mute", mute_changed, NULL );
804 var_DelCallback( p_mi, "audio-device", audio_device_changed, NULL );
805 var_DelCallback( p_mi, "corks", corks_changed, NULL );
807 /* No need for lock_input() because no other threads knows us anymore */
808 if( p_mi->input.p_thread )
809 release_input_thread(p_mi);
810 input_resource_Terminate( p_mi->input.p_resource );
811 input_resource_Release( p_mi->input.p_resource );
812 if( p_mi->input.p_renderer )
813 vlc_renderer_item_release( p_mi->input.p_renderer );
815 vlc_mutex_destroy( &p_mi->input.lock );
817 libvlc_event_manager_destroy(&p_mi->event_manager);
818 libvlc_media_release( p_mi->p_md );
819 vlc_mutex_destroy( &p_mi->object_lock );
821 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
822 vlc_object_release( p_mi );
823 libvlc_release(instance);
826 /**************************************************************************
827 * Release a Media Instance object.
829 * Function does the locking.
830 **************************************************************************/
831 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
833 bool destroy;
835 assert( p_mi );
836 lock(p_mi);
837 destroy = !--p_mi->i_refcount;
838 unlock(p_mi);
840 if( destroy )
841 libvlc_media_player_destroy( p_mi );
844 /**************************************************************************
845 * Retain a Media Instance object.
847 * Caller must hold the lock.
848 **************************************************************************/
849 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
851 assert( p_mi );
853 lock(p_mi);
854 p_mi->i_refcount++;
855 unlock(p_mi);
858 /**************************************************************************
859 * Set the Media descriptor associated with the instance.
861 * Enter without lock -- function will lock the object.
862 **************************************************************************/
863 void libvlc_media_player_set_media(
864 libvlc_media_player_t *p_mi,
865 libvlc_media_t *p_md )
867 lock_input(p_mi);
869 release_input_thread( p_mi );
871 lock( p_mi );
872 set_state( p_mi, libvlc_NothingSpecial, true );
873 unlock_input( p_mi );
875 libvlc_media_release( p_mi->p_md );
877 if( !p_md )
879 p_mi->p_md = NULL;
880 unlock(p_mi);
881 return; /* It is ok to pass a NULL md */
884 libvlc_media_retain( p_md );
885 p_mi->p_md = p_md;
887 /* The policy here is to ignore that we were created using a different
888 * libvlc_instance, because we don't really care */
889 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
891 unlock(p_mi);
893 /* Send an event for the newly available media */
894 libvlc_event_t event;
895 event.type = libvlc_MediaPlayerMediaChanged;
896 event.u.media_player_media_changed.new_media = p_md;
897 libvlc_event_send( &p_mi->event_manager, &event );
901 /**************************************************************************
902 * Get the Media descriptor associated with the instance.
903 **************************************************************************/
904 libvlc_media_t *
905 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
907 libvlc_media_t *p_m;
909 lock( p_mi );
910 p_m = p_mi->p_md;
911 if( p_m )
912 libvlc_media_retain( p_m );
913 unlock( p_mi );
915 return p_m;
918 /**************************************************************************
919 * Get the event Manager.
920 **************************************************************************/
921 libvlc_event_manager_t *
922 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
924 return &p_mi->event_manager;
927 static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
929 var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
930 var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
931 var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
934 static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
936 var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
937 var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
938 var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
941 /**************************************************************************
942 * Tell media player to start playing.
943 **************************************************************************/
944 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
946 lock_input( p_mi );
948 input_thread_t *p_input_thread = p_mi->input.p_thread;
949 if( p_input_thread )
951 /* A thread already exists, send it a play message */
952 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
953 unlock_input( p_mi );
954 return 0;
957 /* Ignore previous exception */
958 lock(p_mi);
960 if( !p_mi->p_md )
962 unlock(p_mi);
963 unlock_input( p_mi );
964 libvlc_printerr( "No associated media descriptor" );
965 return -1;
968 for( size_t i = 0; i < ARRAY_SIZE( p_mi->selected_es ); ++i )
969 p_mi->selected_es[i] = ES_INIT;
971 media_attach_preparsed_event( p_mi->p_md );
973 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
974 p_mi->input.p_resource,
975 p_mi->input.p_renderer );
976 unlock(p_mi);
977 if( !p_input_thread )
979 unlock_input(p_mi);
980 media_detach_preparsed_event( p_mi->p_md );
981 libvlc_printerr( "Not enough memory" );
982 return -1;
985 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
986 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
987 var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
988 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
989 add_es_callbacks( p_input_thread, p_mi );
991 if( input_Start( p_input_thread ) )
993 unlock_input(p_mi);
994 del_es_callbacks( p_input_thread, p_mi );
995 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
996 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
997 var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
998 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
999 input_Close( p_input_thread );
1000 media_detach_preparsed_event( p_mi->p_md );
1001 libvlc_printerr( "Input initialization failure" );
1002 return -1;
1004 p_mi->input.p_thread = p_input_thread;
1005 unlock_input(p_mi);
1006 return 0;
1009 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
1011 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
1012 if( !p_input_thread )
1013 return;
1015 if( paused )
1017 if( libvlc_media_player_can_pause( p_mi ) )
1018 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
1019 else
1020 input_Stop( p_input_thread );
1022 else
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 libvlc_renderer_item_t *p_litem )
1074 vlc_renderer_item_t *p_item =
1075 p_litem ? libvlc_renderer_item_to_vlc( p_litem ) : NULL;
1077 lock_input( p_mi );
1078 input_thread_t *p_input_thread = p_mi->input.p_thread;
1079 if( p_input_thread )
1080 input_Control( p_input_thread, INPUT_SET_RENDERER, p_item );
1082 if( p_mi->input.p_renderer )
1083 vlc_renderer_item_release( p_mi->input.p_renderer );
1084 p_mi->input.p_renderer = p_item ? vlc_renderer_item_hold( p_item ) : NULL;
1085 unlock_input( p_mi );
1087 return 0;
1090 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
1091 void *(*lock_cb) (void *, void **),
1092 void (*unlock_cb) (void *, void *, void *const *),
1093 void (*display_cb) (void *, void *),
1094 void *opaque )
1096 var_SetAddress( mp, "vmem-lock", lock_cb );
1097 var_SetAddress( mp, "vmem-unlock", unlock_cb );
1098 var_SetAddress( mp, "vmem-display", display_cb );
1099 var_SetAddress( mp, "vmem-data", opaque );
1100 var_SetString( mp, "avcodec-hw", "none" );
1101 var_SetString( mp, "vout", "vmem" );
1102 var_SetString( mp, "window", "none" );
1105 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
1106 libvlc_video_format_cb setup,
1107 libvlc_video_cleanup_cb cleanup )
1109 var_SetAddress( mp, "vmem-setup", setup );
1110 var_SetAddress( mp, "vmem-cleanup", cleanup );
1113 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
1114 unsigned width, unsigned height, unsigned pitch )
1116 var_SetString( mp, "vmem-chroma", chroma );
1117 var_SetInteger( mp, "vmem-width", width );
1118 var_SetInteger( mp, "vmem-height", height );
1119 var_SetInteger( mp, "vmem-pitch", pitch );
1122 /**************************************************************************
1123 * set_nsobject
1124 **************************************************************************/
1125 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1126 void * drawable )
1128 assert (p_mi != NULL);
1129 #ifdef __APPLE__
1130 var_SetString (p_mi, "avcodec-hw", "");
1131 var_SetString (p_mi, "vout", "");
1132 var_SetString (p_mi, "window", "");
1133 var_SetAddress (p_mi, "drawable-nsobject", drawable);
1134 #else
1135 (void)drawable;
1136 libvlc_printerr ("can't set nsobject: APPLE build required");
1137 assert(false);
1138 var_SetString (p_mi, "vout", "none");
1139 var_SetString (p_mi, "window", "none");
1140 #endif
1143 /**************************************************************************
1144 * get_nsobject
1145 **************************************************************************/
1146 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1148 assert (p_mi != NULL);
1149 #ifdef __APPLE__
1150 return var_GetAddress (p_mi, "drawable-nsobject");
1151 #else
1152 (void) p_mi;
1153 return NULL;
1154 #endif
1157 /**************************************************************************
1158 * set_agl
1159 **************************************************************************/
1160 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
1161 uint32_t drawable )
1163 (void)drawable;
1164 libvlc_printerr ("can't set agl: use libvlc_media_player_set_nsobject instead");
1165 assert(false);
1166 var_SetString (p_mi, "vout", "none");
1167 var_SetString (p_mi, "window", "none");
1170 /**************************************************************************
1171 * get_agl
1172 **************************************************************************/
1173 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
1175 (void) p_mi;
1176 return 0;
1179 /**************************************************************************
1180 * set_xwindow
1181 **************************************************************************/
1182 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1183 uint32_t drawable )
1185 assert (p_mi != NULL);
1187 var_SetString (p_mi, "avcodec-hw", "");
1188 var_SetString (p_mi, "vout", "");
1189 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1190 var_SetInteger (p_mi, "drawable-xid", drawable);
1193 /**************************************************************************
1194 * get_xwindow
1195 **************************************************************************/
1196 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1198 return var_GetInteger (p_mi, "drawable-xid");
1201 /**************************************************************************
1202 * set_hwnd
1203 **************************************************************************/
1204 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1205 void *drawable )
1207 assert (p_mi != NULL);
1208 #if defined (_WIN32) || defined (__OS2__)
1209 var_SetString (p_mi, "avcodec-hw", "");
1210 var_SetString (p_mi, "vout", "");
1211 var_SetString (p_mi, "window",
1212 (drawable != NULL) ? "embed-hwnd,any" : "");
1213 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1214 #else
1215 (void) drawable;
1216 libvlc_printerr ("can't set nsobject: WIN32 build required");
1217 assert(false);
1218 var_SetString (p_mi, "vout", "none");
1219 var_SetString (p_mi, "window", "none");
1220 #endif
1223 /**************************************************************************
1224 * get_hwnd
1225 **************************************************************************/
1226 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1228 assert (p_mi != NULL);
1229 #if defined (_WIN32) || defined (__OS2__)
1230 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1231 #else
1232 (void) p_mi;
1233 return NULL;
1234 #endif
1237 /**************************************************************************
1238 * set_android_context
1239 **************************************************************************/
1240 void libvlc_media_player_set_android_context( libvlc_media_player_t *p_mi,
1241 void *p_awindow_handler )
1243 assert (p_mi != NULL);
1244 #ifdef __ANDROID__
1245 var_SetAddress (p_mi, "drawable-androidwindow", p_awindow_handler);
1246 #else
1247 (void) p_awindow_handler;
1248 libvlc_printerr ("can't set android context: ANDROID build required");
1249 assert(false);
1250 var_SetString (p_mi, "vout", "none");
1251 var_SetString (p_mi, "window", "none");
1252 #endif
1255 /**************************************************************************
1256 * set_evas_object
1257 **************************************************************************/
1258 int libvlc_media_player_set_evas_object( libvlc_media_player_t *p_mi,
1259 void *p_evas_object )
1261 assert (p_mi != NULL);
1262 #ifdef HAVE_EVAS
1263 var_SetString (p_mi, "vout", "evas");
1264 var_SetString (p_mi, "window", "none");
1265 var_SetAddress (p_mi, "drawable-evasobject", p_evas_object);
1266 return 0;
1267 #else
1268 (void) p_mi; (void) p_evas_object;
1269 return -1;
1270 #endif
1273 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1274 libvlc_audio_play_cb play_cb,
1275 libvlc_audio_pause_cb pause_cb,
1276 libvlc_audio_resume_cb resume_cb,
1277 libvlc_audio_flush_cb flush_cb,
1278 libvlc_audio_drain_cb drain_cb,
1279 void *opaque )
1281 var_SetAddress( mp, "amem-play", play_cb );
1282 var_SetAddress( mp, "amem-pause", pause_cb );
1283 var_SetAddress( mp, "amem-resume", resume_cb );
1284 var_SetAddress( mp, "amem-flush", flush_cb );
1285 var_SetAddress( mp, "amem-drain", drain_cb );
1286 var_SetAddress( mp, "amem-data", opaque );
1287 var_SetString( mp, "aout", "amem,none" );
1289 input_resource_ResetAout(mp->input.p_resource);
1292 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1293 libvlc_audio_set_volume_cb cb )
1295 var_SetAddress( mp, "amem-set-volume", cb );
1297 input_resource_ResetAout(mp->input.p_resource);
1300 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1301 libvlc_audio_setup_cb setup,
1302 libvlc_audio_cleanup_cb cleanup )
1304 var_SetAddress( mp, "amem-setup", setup );
1305 var_SetAddress( mp, "amem-cleanup", cleanup );
1307 input_resource_ResetAout(mp->input.p_resource);
1310 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1311 unsigned rate, unsigned channels )
1313 var_SetString( mp, "amem-format", format );
1314 var_SetInteger( mp, "amem-rate", rate );
1315 var_SetInteger( mp, "amem-channels", channels );
1317 input_resource_ResetAout(mp->input.p_resource);
1321 /**************************************************************************
1322 * Getters for stream information
1323 **************************************************************************/
1324 libvlc_time_t libvlc_media_player_get_length(
1325 libvlc_media_player_t *p_mi )
1327 input_thread_t *p_input_thread;
1328 libvlc_time_t i_time;
1330 p_input_thread = libvlc_get_input_thread ( p_mi );
1331 if( !p_input_thread )
1332 return -1;
1334 i_time = from_mtime(var_GetInteger( p_input_thread, "length" ));
1335 vlc_object_release( p_input_thread );
1337 return i_time;
1340 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1342 input_thread_t *p_input_thread;
1343 libvlc_time_t i_time;
1345 p_input_thread = libvlc_get_input_thread ( p_mi );
1346 if( !p_input_thread )
1347 return -1;
1349 i_time = from_mtime(var_GetInteger( p_input_thread , "time" ));
1350 vlc_object_release( p_input_thread );
1351 return i_time;
1354 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1355 libvlc_time_t i_time )
1357 input_thread_t *p_input_thread;
1359 p_input_thread = libvlc_get_input_thread ( p_mi );
1360 if( !p_input_thread )
1361 return;
1363 var_SetInteger( p_input_thread, "time", to_mtime(i_time) );
1364 vlc_object_release( p_input_thread );
1367 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1368 float position )
1370 input_thread_t *p_input_thread;
1372 p_input_thread = libvlc_get_input_thread ( p_mi );
1373 if( !p_input_thread )
1374 return;
1376 var_SetFloat( p_input_thread, "position", position );
1377 vlc_object_release( p_input_thread );
1380 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1382 input_thread_t *p_input_thread;
1383 float f_position;
1385 p_input_thread = libvlc_get_input_thread ( p_mi );
1386 if( !p_input_thread )
1387 return -1.0;
1389 f_position = var_GetFloat( p_input_thread, "position" );
1390 vlc_object_release( p_input_thread );
1392 return f_position;
1395 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1396 int chapter )
1398 input_thread_t *p_input_thread;
1400 p_input_thread = libvlc_get_input_thread ( p_mi );
1401 if( !p_input_thread )
1402 return;
1404 var_SetInteger( p_input_thread, "chapter", chapter );
1405 vlc_object_release( p_input_thread );
1408 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1410 input_thread_t *p_input_thread;
1411 int i_chapter;
1413 p_input_thread = libvlc_get_input_thread ( p_mi );
1414 if( !p_input_thread )
1415 return -1;
1417 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1418 vlc_object_release( p_input_thread );
1420 return i_chapter;
1423 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1425 input_thread_t *p_input_thread;
1426 vlc_value_t val;
1428 p_input_thread = libvlc_get_input_thread ( p_mi );
1429 if( !p_input_thread )
1430 return -1;
1432 int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1433 vlc_object_release( p_input_thread );
1435 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1438 int libvlc_media_player_get_chapter_count_for_title(
1439 libvlc_media_player_t *p_mi,
1440 int i_title )
1442 vlc_value_t val;
1444 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1445 if( !p_input_thread )
1446 return -1;
1448 char psz_name[sizeof ("title ") + 3 * sizeof (int)];
1449 sprintf( psz_name, "title %2u", i_title );
1451 int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1452 vlc_object_release( p_input_thread );
1454 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1457 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1458 int i_title )
1460 input_thread_t *p_input_thread;
1462 p_input_thread = libvlc_get_input_thread ( p_mi );
1463 if( !p_input_thread )
1464 return;
1466 var_SetInteger( p_input_thread, "title", i_title );
1467 vlc_object_release( p_input_thread );
1469 //send event
1470 libvlc_event_t event;
1471 event.type = libvlc_MediaPlayerTitleChanged;
1472 event.u.media_player_title_changed.new_title = i_title;
1473 libvlc_event_send( &p_mi->event_manager, &event );
1476 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1478 input_thread_t *p_input_thread;
1479 int i_title;
1481 p_input_thread = libvlc_get_input_thread ( p_mi );
1482 if( !p_input_thread )
1483 return -1;
1485 i_title = var_GetInteger( p_input_thread, "title" );
1486 vlc_object_release( p_input_thread );
1488 return i_title;
1491 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1493 input_thread_t *p_input_thread;
1494 vlc_value_t val;
1496 p_input_thread = libvlc_get_input_thread ( p_mi );
1497 if( !p_input_thread )
1498 return -1;
1500 int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1501 vlc_object_release( p_input_thread );
1503 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1506 int libvlc_media_player_get_full_title_descriptions( libvlc_media_player_t *p_mi,
1507 libvlc_title_description_t *** pp_titles )
1509 assert( p_mi );
1511 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1513 if( !p_input_thread )
1514 return -1;
1516 input_title_t **p_input_title;
1517 int count;
1519 /* fetch data */
1520 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO,
1521 &p_input_title, &count );
1522 vlc_object_release( p_input_thread );
1523 if( ret != VLC_SUCCESS )
1524 return -1;
1526 libvlc_title_description_t **titles = vlc_alloc( count, sizeof (*titles) );
1527 if( count > 0 && unlikely(titles == NULL) )
1528 return -1;
1530 /* fill array */
1531 for( int i = 0; i < count; i++)
1533 libvlc_title_description_t *title = malloc( sizeof (*title) );
1534 if( unlikely(title == NULL) )
1536 libvlc_title_descriptions_release( titles, i );
1537 return -1;
1539 titles[i] = title;
1541 /* we want to return milliseconds to match the rest of the API */
1542 title->i_duration = p_input_title[i]->i_length / 1000;
1543 title->i_flags = p_input_title[i]->i_flags;
1544 if( p_input_title[i]->psz_name )
1545 title->psz_name = strdup( p_input_title[i]->psz_name );
1546 else
1547 title->psz_name = NULL;
1548 vlc_input_title_Delete( p_input_title[i] );
1550 free( p_input_title );
1552 *pp_titles = titles;
1553 return count;
1556 void libvlc_title_descriptions_release( libvlc_title_description_t **p_titles,
1557 unsigned i_count )
1559 for (unsigned i = 0; i < i_count; i++ )
1561 if ( !p_titles[i] )
1562 continue;
1564 free( p_titles[i]->psz_name );
1565 free( p_titles[i] );
1567 free( p_titles );
1570 int libvlc_media_player_get_full_chapter_descriptions( libvlc_media_player_t *p_mi,
1571 int i_chapters_of_title,
1572 libvlc_chapter_description_t *** pp_chapters )
1574 assert( p_mi );
1576 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1578 if( !p_input_thread )
1579 return -1;
1581 seekpoint_t **p_seekpoint = NULL;
1583 /* fetch data */
1584 int ci_chapter_count = i_chapters_of_title;
1586 int ret = input_Control(p_input_thread, INPUT_GET_SEEKPOINTS, &p_seekpoint, &ci_chapter_count);
1587 if( ret != VLC_SUCCESS)
1589 vlc_object_release( p_input_thread );
1590 return -1;
1593 if (ci_chapter_count == 0 || p_seekpoint == NULL)
1595 vlc_object_release( p_input_thread );
1596 return 0;
1599 input_title_t *p_title;
1600 ret = input_Control( p_input_thread, INPUT_GET_TITLE_INFO, &p_title,
1601 &i_chapters_of_title );
1602 vlc_object_release( p_input_thread );
1603 if( ret != VLC_SUCCESS )
1605 goto error;
1607 int64_t i_title_duration = p_title->i_length / 1000;
1608 vlc_input_title_Delete( p_title );
1610 *pp_chapters = calloc( ci_chapter_count, sizeof(**pp_chapters) );
1611 if( !*pp_chapters )
1613 goto error;
1616 /* fill array */
1617 for( int i = 0; i < ci_chapter_count; ++i )
1619 libvlc_chapter_description_t *p_chapter = malloc( sizeof(*p_chapter) );
1620 if( unlikely(p_chapter == NULL) )
1622 goto error;
1624 (*pp_chapters)[i] = p_chapter;
1626 p_chapter->i_time_offset = p_seekpoint[i]->i_time_offset / 1000;
1628 if( i < ci_chapter_count - 1 )
1630 p_chapter->i_duration = p_seekpoint[i + 1]->i_time_offset / 1000 -
1631 p_chapter->i_time_offset;
1633 else
1635 if ( i_title_duration )
1636 p_chapter->i_duration = i_title_duration - p_chapter->i_time_offset;
1637 else
1638 p_chapter->i_duration = 0;
1641 if( p_seekpoint[i]->psz_name )
1643 p_chapter->psz_name = strdup( p_seekpoint[i]->psz_name );
1645 else
1647 p_chapter->psz_name = NULL;
1649 vlc_seekpoint_Delete( p_seekpoint[i] );
1650 p_seekpoint[i] = NULL;
1653 free( p_seekpoint );
1654 return ci_chapter_count;
1656 error:
1657 if( *pp_chapters )
1658 libvlc_chapter_descriptions_release( *pp_chapters, ci_chapter_count );
1659 for ( int i = 0; i < ci_chapter_count; ++i )
1660 vlc_seekpoint_Delete( p_seekpoint[i] );
1661 free( p_seekpoint );
1662 return -1;
1665 void libvlc_chapter_descriptions_release( libvlc_chapter_description_t **p_chapters,
1666 unsigned i_count )
1668 for (unsigned i = 0; i < i_count; i++ )
1670 if ( !p_chapters[i] )
1671 continue;
1673 free( p_chapters[i]->psz_name );
1674 free( p_chapters[i] );
1676 free( p_chapters );
1679 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1681 input_thread_t *p_input_thread;
1683 p_input_thread = libvlc_get_input_thread ( p_mi );
1684 if( !p_input_thread )
1685 return;
1687 int i_type = var_Type( p_input_thread, "next-chapter" );
1688 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1689 "next-chapter":"next-title" );
1691 vlc_object_release( p_input_thread );
1694 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1696 input_thread_t *p_input_thread;
1698 p_input_thread = libvlc_get_input_thread ( p_mi );
1699 if( !p_input_thread )
1700 return;
1702 int i_type = var_Type( p_input_thread, "next-chapter" );
1703 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1704 "prev-chapter":"prev-title" );
1706 vlc_object_release( p_input_thread );
1709 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1711 libvlc_media_t *media = libvlc_media_player_get_media( p_mi );
1712 if( media == NULL )
1713 return 0.f;
1715 input_item_t *item = media->p_input_item;
1716 float fps = 0.f;
1718 vlc_mutex_lock( &item->lock );
1719 for( int i = 0; i < item->i_es; i++ )
1721 const es_format_t *fmt = item->es[i];
1723 if( fmt->i_cat == VIDEO_ES && fmt->video.i_frame_rate_base > 0 )
1724 fps = (float)fmt->video.i_frame_rate
1725 / (float)fmt->video.i_frame_rate_base;
1727 vlc_mutex_unlock( &item->lock );
1728 libvlc_media_release( media );
1730 return fps;
1733 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1735 input_thread_t *p_input_thread =
1736 libvlc_get_input_thread ( p_mi );
1737 if ( !p_input_thread )
1738 return false;
1740 int state = var_GetInteger( p_input_thread, "state" );
1741 vlc_object_release( p_input_thread );
1743 return state != END_S && state != ERROR_S;
1746 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1748 var_SetFloat (p_mi, "rate", rate);
1750 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1751 if( !p_input_thread )
1752 return 0;
1753 var_SetFloat( p_input_thread, "rate", rate );
1754 vlc_object_release( p_input_thread );
1755 return 0;
1758 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1760 return var_GetFloat (p_mi, "rate");
1763 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1765 lock(p_mi);
1766 libvlc_state_t state = p_mi->state;
1767 unlock(p_mi);
1768 return state;
1771 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1773 input_thread_t *p_input_thread;
1774 bool b_seekable;
1776 p_input_thread = libvlc_get_input_thread ( p_mi );
1777 if ( !p_input_thread )
1778 return false;
1779 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1780 vlc_object_release( p_input_thread );
1782 return b_seekable;
1785 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1786 unsigned navigate )
1788 static const int map[] =
1790 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1791 INPUT_NAV_LEFT, INPUT_NAV_RIGHT, INPUT_NAV_POPUP,
1794 if( navigate >= sizeof(map) / sizeof(map[0]) )
1795 return;
1797 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1798 if ( p_input == NULL )
1799 return;
1801 input_Control( p_input, map[navigate], NULL );
1802 vlc_object_release( p_input );
1805 /* internal function, used by audio, video */
1806 libvlc_track_description_t *
1807 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1808 const char *psz_variable )
1810 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1811 libvlc_track_description_t *p_track_description = NULL,
1812 *p_actual, *p_previous;
1814 if( !p_input )
1815 return NULL;
1817 vlc_value_t val_list, text_list;
1818 int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES, &val_list, &text_list );
1819 if( i_ret != VLC_SUCCESS )
1820 return NULL;
1822 /* no tracks */
1823 if( val_list.p_list->i_count <= 0 )
1824 goto end;
1826 p_track_description = malloc( sizeof *p_track_description );
1827 if ( !p_track_description )
1829 libvlc_printerr( "Not enough memory" );
1830 goto end;
1832 p_actual = p_track_description;
1833 p_previous = NULL;
1834 for( int i = 0; i < val_list.p_list->i_count; i++ )
1836 if( !p_actual )
1838 p_actual = malloc( sizeof *p_actual );
1839 if ( !p_actual )
1841 libvlc_track_description_list_release( p_track_description );
1842 libvlc_printerr( "Not enough memory" );
1844 p_track_description = NULL;
1845 goto end;
1848 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1849 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1850 p_actual->p_next = NULL;
1851 if( p_previous )
1852 p_previous->p_next = p_actual;
1853 p_previous = p_actual;
1854 p_actual = NULL;
1857 end:
1858 var_FreeList( &val_list, &text_list );
1859 vlc_object_release( p_input );
1861 return p_track_description;
1864 // Deprecated alias for libvlc_track_description_list_release
1865 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1867 libvlc_track_description_list_release( p_td );
1870 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1872 libvlc_track_description_t *p_actual, *p_before;
1873 p_actual = p_td;
1875 while ( p_actual )
1877 free( p_actual->psz_name );
1878 p_before = p_actual;
1879 p_actual = p_before->p_next;
1880 free( p_before );
1884 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1886 input_thread_t *p_input_thread;
1887 bool b_can_pause;
1889 p_input_thread = libvlc_get_input_thread ( p_mi );
1890 if ( !p_input_thread )
1891 return false;
1892 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1893 vlc_object_release( p_input_thread );
1895 return b_can_pause;
1898 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1900 input_thread_t *p_input_thread;
1901 bool b_program_scrambled;
1903 p_input_thread = libvlc_get_input_thread ( p_mi );
1904 if ( !p_input_thread )
1905 return false;
1906 b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1907 vlc_object_release( p_input_thread );
1909 return b_program_scrambled;
1912 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1914 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1915 if( p_input_thread != NULL )
1917 var_TriggerCallback( p_input_thread, "frame-next" );
1918 vlc_object_release( p_input_thread );
1923 * Private lookup table to get subpicture alignment flag values corresponding
1924 * to a libvlc_position_t enumerated value.
1926 static const unsigned char position_subpicture_alignment[] = {
1927 [libvlc_position_center] = 0,
1928 [libvlc_position_left] = SUBPICTURE_ALIGN_LEFT,
1929 [libvlc_position_right] = SUBPICTURE_ALIGN_RIGHT,
1930 [libvlc_position_top] = SUBPICTURE_ALIGN_TOP,
1931 [libvlc_position_top_left] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1932 [libvlc_position_top_right] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1933 [libvlc_position_bottom] = SUBPICTURE_ALIGN_BOTTOM,
1934 [libvlc_position_bottom_left] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1935 [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1938 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1940 assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1942 if ( position != libvlc_position_disable )
1944 var_SetBool( p_mi, "video-title-show", true );
1945 var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1946 var_SetInteger( p_mi, "video-title-timeout", timeout );
1948 else
1950 var_SetBool( p_mi, "video-title-show", false );
1954 int libvlc_media_player_add_slave( libvlc_media_player_t *p_mi,
1955 libvlc_media_slave_type_t i_type,
1956 const char *psz_uri, bool b_select )
1958 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1960 if( p_input_thread == NULL )
1962 libvlc_media_t *p_media = libvlc_media_player_get_media( p_mi );
1963 if( p_media == NULL )
1964 return -1;
1966 int i_ret = libvlc_media_slaves_add( p_media, i_type, 4, psz_uri );
1967 libvlc_media_release( p_media );
1968 return i_ret;
1970 else
1972 int i_ret = input_AddSlave( p_input_thread, (enum slave_type) i_type,
1973 psz_uri, b_select, false, false );
1974 vlc_object_release( p_input_thread );
1976 return i_ret == VLC_SUCCESS ? 0 : -1;
1981 * Maximum size of a formatted equalizer amplification band frequency value.
1983 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1985 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1986 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1987 * terminator).
1989 #define EQZ_BAND_VALUE_SIZE 12
1991 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1993 char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1995 if( p_equalizer != NULL )
1997 for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1999 c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
2000 p_equalizer->f_amp[i] );
2001 if( unlikely(c >= sizeof(bands)) )
2002 return -1;
2005 var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
2006 var_SetString( p_mi, "equalizer-bands", bands );
2008 var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
2010 audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
2011 if( p_aout != NULL )
2013 if( p_equalizer != NULL )
2015 var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
2016 var_SetString( p_aout, "equalizer-bands", bands );
2019 var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
2020 vlc_object_release( p_aout );
2023 return 0;
2026 static const char roles[][16] =
2028 [libvlc_role_Music] = "music",
2029 [libvlc_role_Video] = "video",
2030 [libvlc_role_Communication] = "communication",
2031 [libvlc_role_Game] = "game",
2032 [libvlc_role_Notification] = "notification",
2033 [libvlc_role_Animation] = "animation",
2034 [libvlc_role_Production] = "production",
2035 [libvlc_role_Accessibility] = "accessibility",
2036 [libvlc_role_Test] = "test",
2039 int libvlc_media_player_set_role(libvlc_media_player_t *mp, unsigned role)
2041 if (role >= ARRAY_SIZE(roles)
2042 || var_SetString(mp, "role", roles[role]) != VLC_SUCCESS)
2043 return -1;
2044 return 0;
2047 int libvlc_media_player_get_role(libvlc_media_player_t *mp)
2049 int ret = -1;
2050 char *str = var_GetString(mp, "role");
2051 if (str == NULL)
2052 return 0;
2054 for (size_t i = 0; i < ARRAY_SIZE(roles); i++)
2055 if (!strcmp(roles[i], str))
2057 ret = i;
2058 break;
2061 free(str);
2062 return ret;