mp4: Fix make distcheck
[vlc.git] / lib / media_player.c
blob60ef18b4d4f0cf2678dfaf62572882b7dd568c57
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>
39 #include <vlc_http.h>
41 #include "libvlc_internal.h"
42 #include "media_internal.h" // libvlc_media_set_state()
43 #include "media_player_internal.h"
44 #include "renderer_discoverer_internal.h"
46 #define ES_INIT (-2) /* -1 is reserved for ES deselect */
48 static int
49 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
50 vlc_value_t oldval, vlc_value_t newval,
51 void * p_userdata );
52 static int
53 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
54 vlc_value_t oldval, vlc_value_t newval,
55 void * p_userdata );
56 static int
57 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
58 vlc_value_t oldval, vlc_value_t newval,
59 void * p_userdata );
60 static int
61 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
62 vlc_value_t oldval, vlc_value_t newval,
63 void * p_userdata );
65 static int
66 input_es_changed( vlc_object_t * p_this, char const * psz_cmd,
67 int action, vlc_value_t *p_val,
68 void *p_userdata);
70 static int
71 corks_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
72 vlc_value_t cur, void *opaque);
74 static int
75 mute_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
76 vlc_value_t cur, void *opaque);
78 static int
79 volume_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
80 vlc_value_t cur, void *opaque);
82 static void
83 add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
85 static void
86 del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
88 static int
89 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
90 vlc_value_t oldval, vlc_value_t newval, void *p_data );
92 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
95 * Shortcuts
99 * The input lock protects the input and input resource pointer.
100 * It MUST NOT be used from callbacks.
102 * The object lock protects the reset, namely the media and the player state.
103 * It can, and usually needs to be taken from callbacks.
104 * The object lock can be acquired under the input lock... and consequently
105 * the opposite order is STRICTLY PROHIBITED.
107 static inline void lock(libvlc_media_player_t *mp)
109 vlc_mutex_lock(&mp->object_lock);
112 static inline void unlock(libvlc_media_player_t *mp)
114 vlc_mutex_unlock(&mp->object_lock);
117 static inline void lock_input(libvlc_media_player_t *mp)
119 vlc_mutex_lock(&mp->input.lock);
122 static inline void unlock_input(libvlc_media_player_t *mp)
124 vlc_mutex_unlock(&mp->input.lock);
127 static void input_item_preparsed_changed( const vlc_event_t *p_event,
128 void * user_data )
130 libvlc_media_t *p_md = user_data;
131 if( p_event->u.input_item_preparsed_changed.new_status & ITEM_PREPARSED )
133 /* Send the event */
134 libvlc_event_t event;
135 event.type = libvlc_MediaParsedChanged;
136 event.u.media_parsed_changed.new_status = libvlc_media_parsed_status_done;
137 libvlc_event_send( &p_md->event_manager, &event );
141 static void media_attach_preparsed_event( libvlc_media_t *p_md )
143 vlc_event_attach( &p_md->p_input_item->event_manager,
144 vlc_InputItemPreparsedChanged,
145 input_item_preparsed_changed, p_md );
148 static void media_detach_preparsed_event( libvlc_media_t *p_md )
150 vlc_event_detach( &p_md->p_input_item->event_manager,
151 vlc_InputItemPreparsedChanged,
152 input_item_preparsed_changed,
153 p_md );
157 * Release the associated input thread.
159 * Object lock is NOT held.
160 * Input lock is held or instance is being destroyed.
162 static void release_input_thread( libvlc_media_player_t *p_mi )
164 assert( p_mi );
166 input_thread_t *p_input_thread = p_mi->input.p_thread;
167 if( !p_input_thread )
168 return;
169 p_mi->input.p_thread = NULL;
171 media_detach_preparsed_event( p_mi->p_md );
173 var_DelCallback( p_input_thread, "can-seek",
174 input_seekable_changed, p_mi );
175 var_DelCallback( p_input_thread, "can-pause",
176 input_pausable_changed, p_mi );
177 var_DelCallback( p_input_thread, "program-scrambled",
178 input_scrambled_changed, p_mi );
179 var_DelCallback( p_input_thread, "intf-event",
180 input_event_changed, p_mi );
181 del_es_callbacks( p_input_thread, p_mi );
183 /* We owned this one */
184 input_Stop( p_input_thread );
185 input_Close( p_input_thread );
189 * Retrieve the input thread. Be sure to release the object
190 * once you are done with it. (libvlc Internal)
192 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
194 input_thread_t *p_input_thread;
196 assert( p_mi );
198 lock_input(p_mi);
199 p_input_thread = p_mi->input.p_thread;
200 if( p_input_thread )
201 vlc_object_hold( p_input_thread );
202 else
203 libvlc_printerr( "No active input" );
204 unlock_input(p_mi);
206 return p_input_thread;
210 * Set the internal state of the media_player. (media player Internal)
212 * Function will lock the media_player.
214 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
215 bool b_locked )
217 if(!b_locked)
218 lock(p_mi);
219 p_mi->state = state;
221 libvlc_media_t *media = p_mi->p_md;
222 if (media)
223 libvlc_media_retain(media);
225 if(!b_locked)
226 unlock(p_mi);
228 if (media)
230 // Also set the state of the corresponding media
231 // This is strictly for convenience.
232 libvlc_media_set_state(media, state);
234 libvlc_media_release(media);
238 static int
239 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
240 vlc_value_t oldval, vlc_value_t newval,
241 void * p_userdata )
243 VLC_UNUSED(oldval);
244 VLC_UNUSED(p_this);
245 VLC_UNUSED(psz_cmd);
246 libvlc_media_player_t * p_mi = p_userdata;
247 libvlc_event_t event;
249 event.type = libvlc_MediaPlayerSeekableChanged;
250 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
252 libvlc_event_send( &p_mi->event_manager, &event );
253 return VLC_SUCCESS;
256 static int
257 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
258 vlc_value_t oldval, vlc_value_t newval,
259 void * p_userdata )
261 VLC_UNUSED(oldval);
262 VLC_UNUSED(p_this);
263 VLC_UNUSED(psz_cmd);
264 libvlc_media_player_t * p_mi = p_userdata;
265 libvlc_event_t event;
267 event.type = libvlc_MediaPlayerPausableChanged;
268 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
270 libvlc_event_send( &p_mi->event_manager, &event );
271 return VLC_SUCCESS;
274 static int
275 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
276 vlc_value_t oldval, vlc_value_t newval,
277 void * p_userdata )
279 VLC_UNUSED(oldval);
280 VLC_UNUSED(p_this);
281 VLC_UNUSED(psz_cmd);
282 libvlc_media_player_t * p_mi = p_userdata;
283 libvlc_event_t event;
285 event.type = libvlc_MediaPlayerScrambledChanged;
286 event.u.media_player_scrambled_changed.new_scrambled = newval.b_bool;
288 libvlc_event_send( &p_mi->event_manager, &event );
289 return VLC_SUCCESS;
292 static int
293 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
294 vlc_value_t oldval, vlc_value_t newval,
295 void * p_userdata )
297 VLC_UNUSED(oldval); VLC_UNUSED(psz_cmd);
298 input_thread_t * p_input = (input_thread_t *)p_this;
299 libvlc_media_player_t * p_mi = p_userdata;
300 libvlc_event_t event;
302 assert( !strcmp( psz_cmd, "intf-event" ) );
304 if( newval.i_int == INPUT_EVENT_STATE )
306 libvlc_state_t libvlc_state;
308 switch ( var_GetInteger( p_input, "state" ) )
310 case INIT_S:
311 libvlc_state = libvlc_NothingSpecial;
312 event.type = libvlc_MediaPlayerNothingSpecial;
313 break;
314 case OPENING_S:
315 libvlc_state = libvlc_Opening;
316 event.type = libvlc_MediaPlayerOpening;
317 break;
318 case PLAYING_S:
319 libvlc_state = libvlc_Playing;
320 event.type = libvlc_MediaPlayerPlaying;
321 break;
322 case PAUSE_S:
323 libvlc_state = libvlc_Paused;
324 event.type = libvlc_MediaPlayerPaused;
325 break;
326 case END_S:
327 libvlc_state = libvlc_Ended;
328 event.type = libvlc_MediaPlayerEndReached;
329 break;
330 case ERROR_S:
331 libvlc_state = libvlc_Error;
332 event.type = libvlc_MediaPlayerEncounteredError;
333 break;
335 default:
336 return VLC_SUCCESS;
339 set_state( p_mi, libvlc_state, false );
340 libvlc_event_send( &p_mi->event_manager, &event );
342 else if( newval.i_int == INPUT_EVENT_DEAD )
344 libvlc_state_t libvlc_state = libvlc_Ended;
345 event.type = libvlc_MediaPlayerStopped;
347 set_state( p_mi, libvlc_state, false );
348 libvlc_event_send( &p_mi->event_manager, &event );
350 else if( newval.i_int == INPUT_EVENT_POSITION )
352 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
353 return VLC_SUCCESS; /* Don't send the position while stopped */
355 /* */
356 event.type = libvlc_MediaPlayerPositionChanged;
357 event.u.media_player_position_changed.new_position =
358 var_GetFloat( p_input, "position" );
359 libvlc_event_send( &p_mi->event_manager, &event );
361 /* */
362 event.type = libvlc_MediaPlayerTimeChanged;
363 event.u.media_player_time_changed.new_time =
364 from_mtime(var_GetInteger( p_input, "time" ));
365 libvlc_event_send( &p_mi->event_manager, &event );
367 else if( newval.i_int == INPUT_EVENT_LENGTH )
369 event.type = libvlc_MediaPlayerLengthChanged;
370 event.u.media_player_length_changed.new_length =
371 from_mtime(var_GetInteger( p_input, "length" ));
372 libvlc_event_send( &p_mi->event_manager, &event );
374 else if( newval.i_int == INPUT_EVENT_CACHE )
376 event.type = libvlc_MediaPlayerBuffering;
377 event.u.media_player_buffering.new_cache = (100 *
378 var_GetFloat( p_input, "cache" ));
379 libvlc_event_send( &p_mi->event_manager, &event );
381 else if( newval.i_int == INPUT_EVENT_VOUT )
383 vout_thread_t **pp_vout;
384 size_t i_vout;
385 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
387 i_vout = 0;
389 else
391 for( size_t i = 0; i < i_vout; i++ )
392 vlc_object_release( pp_vout[i] );
393 free( pp_vout );
396 event.type = libvlc_MediaPlayerVout;
397 event.u.media_player_vout.new_count = i_vout;
398 libvlc_event_send( &p_mi->event_manager, &event );
400 else if ( newval.i_int == INPUT_EVENT_TITLE )
402 event.type = libvlc_MediaPlayerTitleChanged;
403 event.u.media_player_title_changed.new_title = var_GetInteger( p_input, "title" );
404 libvlc_event_send( &p_mi->event_manager, &event );
406 else if ( newval.i_int == INPUT_EVENT_CHAPTER )
408 event.type = libvlc_MediaPlayerChapterChanged;
409 event.u.media_player_chapter_changed.new_chapter = var_GetInteger( p_input, "chapter" );
410 libvlc_event_send( &p_mi->event_manager, &event );
412 else if ( newval.i_int == INPUT_EVENT_ES )
414 /* Send ESSelected events from this callback ("intf-event") and not
415 * from "audio-es", "video-es", "spu-es" callbacks. Indeed, these
416 * callbacks are not triggered when the input_thread changes an ES
417 * while this callback is. */
418 struct {
419 const char *psz_name;
420 const libvlc_track_type_t type;
421 int new_es;
422 } es_list[] = {
423 { "audio-es", libvlc_track_audio, ES_INIT },
424 { "video-es", libvlc_track_video, ES_INIT },
425 { "spu-es", libvlc_track_text, ES_INIT },
427 /* Check if an ES selection changed */
428 lock( p_mi );
429 for( size_t i = 0; i < ARRAY_SIZE( es_list ); ++i )
431 int current_es = var_GetInteger( p_input, es_list[i].psz_name );
432 if( current_es != p_mi->selected_es[i] )
433 es_list[i].new_es = p_mi->selected_es[i] = current_es;
435 unlock( p_mi );
437 /* Send the ESSelected event for each ES that were newly selected */
438 for( size_t i = 0; i < ARRAY_SIZE( es_list ); ++i )
440 if( es_list[i].new_es != ES_INIT )
442 event.type = libvlc_MediaPlayerESSelected;
443 event.u.media_player_es_changed.i_type = es_list[i].type;
444 event.u.media_player_es_changed.i_id = es_list[i].new_es;
445 libvlc_event_send( &p_mi->event_manager, &event );
450 return VLC_SUCCESS;
453 static int track_type_from_name(const char *psz_name)
455 if( !strcmp( psz_name, "video-es" ) )
456 return libvlc_track_video;
457 else if( !strcmp( psz_name, "audio-es" ) )
458 return libvlc_track_audio;
459 else if( !strcmp( psz_name, "spu-es" ) )
460 return libvlc_track_text;
461 else
462 return libvlc_track_unknown;
465 static int input_es_changed( vlc_object_t *p_this,
466 char const *psz_cmd,
467 int action,
468 vlc_value_t *p_val,
469 void *p_userdata )
471 VLC_UNUSED(p_this);
472 libvlc_media_player_t *mp = p_userdata;
473 libvlc_event_t event;
475 /* Ignore the "Disable" element */
476 if (p_val && p_val->i_int < 0)
477 return VLC_EGENERIC;
479 switch (action)
481 case VLC_VAR_ADDCHOICE:
482 event.type = libvlc_MediaPlayerESAdded;
483 break;
484 case VLC_VAR_DELCHOICE:
485 case VLC_VAR_CLEARCHOICES:
486 event.type = libvlc_MediaPlayerESDeleted;
487 break;
488 default:
489 return VLC_EGENERIC;
492 event.u.media_player_es_changed.i_type = track_type_from_name(psz_cmd);
494 int i_id;
495 if (action != VLC_VAR_CLEARCHOICES)
497 if (!p_val)
498 return VLC_EGENERIC;
499 i_id = p_val->i_int;
501 else
503 /* -1 means all ES tracks of this type were deleted. */
504 i_id = -1;
506 event.u.media_player_es_changed.i_id = i_id;
508 libvlc_event_send(&mp->event_manager, &event);
510 return VLC_SUCCESS;
513 /**************************************************************************
514 * Snapshot Taken Event.
516 * FIXME: This snapshot API interface makes no sense in media_player.
517 *************************************************************************/
518 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
519 vlc_value_t oldval, vlc_value_t newval, void *p_data )
521 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
523 libvlc_media_player_t *mp = p_data;
524 libvlc_event_t event;
525 event.type = libvlc_MediaPlayerSnapshotTaken;
526 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
527 libvlc_event_send(&mp->event_manager, &event);
529 return VLC_SUCCESS;
532 static int corks_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
533 vlc_value_t cur, void *opaque)
535 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
537 if (!old.i_int != !cur.i_int)
539 libvlc_event_t event;
541 event.type = cur.i_int ? libvlc_MediaPlayerCorked
542 : libvlc_MediaPlayerUncorked;
543 libvlc_event_send(&mp->event_manager, &event);
545 VLC_UNUSED(name); VLC_UNUSED(opaque);
546 return VLC_SUCCESS;
549 static int audio_device_changed(vlc_object_t *obj, const char *name,
550 vlc_value_t old, vlc_value_t cur, void *opaque)
552 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
553 libvlc_event_t event;
555 event.type = libvlc_MediaPlayerAudioDevice;
556 event.u.media_player_audio_device.device = cur.psz_string;
557 libvlc_event_send(&mp->event_manager, &event);
558 VLC_UNUSED(name); VLC_UNUSED(old); VLC_UNUSED(opaque);
559 return VLC_SUCCESS;
562 static int mute_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
563 vlc_value_t cur, void *opaque)
565 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
567 if (old.b_bool != cur.b_bool)
569 libvlc_event_t event;
571 event.type = cur.b_bool ? libvlc_MediaPlayerMuted
572 : libvlc_MediaPlayerUnmuted;
573 libvlc_event_send(&mp->event_manager, &event);
575 VLC_UNUSED(name); VLC_UNUSED(opaque);
576 return VLC_SUCCESS;
579 static int volume_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
580 vlc_value_t cur, void *opaque)
582 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
583 libvlc_event_t event;
585 event.type = libvlc_MediaPlayerAudioVolume;
586 event.u.media_player_audio_volume.volume = cur.f_float;
587 libvlc_event_send(&mp->event_manager, &event);
588 VLC_UNUSED(name); VLC_UNUSED(old); VLC_UNUSED(opaque);
589 return VLC_SUCCESS;
592 /**************************************************************************
593 * Create a Media Instance object.
595 * Refcount strategy:
596 * - All items created by _new start with a refcount set to 1.
597 * - Accessor _release decrease the refcount by 1, if after that
598 * operation the refcount is 0, the object is destroyed.
599 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
601 * Object locking strategy:
602 * - No lock held while in constructor.
603 * - When accessing any member variable this lock is held. (XXX who locks?)
604 * - When attempting to destroy the object the lock is also held.
605 **************************************************************************/
606 libvlc_media_player_t *
607 libvlc_media_player_new( libvlc_instance_t *instance )
609 libvlc_media_player_t * mp;
611 assert(instance);
613 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
614 if (unlikely(mp == NULL))
616 libvlc_printerr("Not enough memory");
617 return NULL;
620 /* Input */
621 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
622 var_Create (mp, "sout", VLC_VAR_STRING);
623 var_Create (mp, "demux-filter", VLC_VAR_STRING);
625 /* Video */
626 var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
627 var_Create (mp, "window", VLC_VAR_STRING);
628 var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
629 var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
630 var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
631 var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
632 var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
633 var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
634 var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
635 var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
636 var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
637 var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
638 var_Create (mp, "avcodec-hw", VLC_VAR_STRING);
639 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
640 #if defined (_WIN32) || defined (__OS2__)
641 var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
642 #endif
643 #ifdef __APPLE__
644 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
645 #endif
646 #ifdef __ANDROID__
647 var_Create (mp, "drawable-androidwindow", VLC_VAR_ADDRESS);
648 #endif
650 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
651 var_SetBool (mp, "keyboard-events", true);
652 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
653 var_SetBool (mp, "mouse-events", true);
655 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
656 var_Create (mp, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
657 var_Create (mp, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
658 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
659 var_Create (mp, "crop", VLC_VAR_STRING);
660 var_Create (mp, "deinterlace", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
661 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
663 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
664 var_SetInteger (mp, "vbi-page", 100);
666 var_Create (mp, "video-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
667 var_Create (mp, "sub-source", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
668 var_Create (mp, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
670 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
671 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
672 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
673 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
674 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
675 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
676 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
677 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
678 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
680 var_Create (mp, "logo-file", VLC_VAR_STRING);
681 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
682 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
683 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
684 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
685 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
686 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
688 var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
689 var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
690 var_Create (mp, "hue", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
691 var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
692 var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
694 /* Audio */
695 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
696 var_Create (mp, "audio-device", VLC_VAR_STRING);
697 var_Create (mp, "mute", VLC_VAR_BOOL);
698 var_Create (mp, "volume", VLC_VAR_FLOAT);
699 var_Create (mp, "corks", VLC_VAR_INTEGER);
700 var_Create (mp, "audio-filter", VLC_VAR_STRING);
701 var_Create (mp, "role", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
702 var_Create (mp, "amem-data", VLC_VAR_ADDRESS);
703 var_Create (mp, "amem-setup", VLC_VAR_ADDRESS);
704 var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
705 var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
706 var_Create (mp, "amem-pause", VLC_VAR_ADDRESS);
707 var_Create (mp, "amem-resume", VLC_VAR_ADDRESS);
708 var_Create (mp, "amem-flush", VLC_VAR_ADDRESS);
709 var_Create (mp, "amem-drain", VLC_VAR_ADDRESS);
710 var_Create (mp, "amem-set-volume", VLC_VAR_ADDRESS);
711 var_Create (mp, "amem-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
712 var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
713 var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
715 /* Video Title */
716 var_Create (mp, "video-title-show", VLC_VAR_BOOL);
717 var_Create (mp, "video-title-position", VLC_VAR_INTEGER);
718 var_Create (mp, "video-title-timeout", VLC_VAR_INTEGER);
720 /* Equalizer */
721 var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
722 var_Create (mp, "equalizer-vlcfreqs", VLC_VAR_BOOL);
723 var_Create (mp, "equalizer-bands", VLC_VAR_STRING);
725 /* Initialize the shared HTTP cookie jar */
726 vlc_value_t cookies;
727 cookies.p_address = vlc_http_cookies_new();
728 if ( likely(cookies.p_address) )
730 var_Create(mp, "http-cookies", VLC_VAR_ADDRESS);
731 var_SetChecked(mp, "http-cookies", VLC_VAR_ADDRESS, cookies);
734 mp->p_md = NULL;
735 mp->state = libvlc_NothingSpecial;
736 mp->p_libvlc_instance = instance;
737 mp->input.p_thread = NULL;
738 mp->input.p_renderer = NULL;
739 mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
740 if (unlikely(mp->input.p_resource == NULL))
742 vlc_object_release(mp);
743 return NULL;
745 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
746 if( aout != NULL )
747 input_resource_PutAout(mp->input.p_resource, aout);
749 vlc_viewpoint_init(&mp->viewpoint);
751 var_Create (mp, "viewpoint", VLC_VAR_ADDRESS);
752 var_SetAddress( mp, "viewpoint", &mp->viewpoint );
753 vlc_mutex_init (&mp->input.lock);
754 mp->i_refcount = 1;
755 libvlc_event_manager_init(&mp->event_manager, mp);
756 vlc_mutex_init(&mp->object_lock);
758 var_AddCallback(mp, "corks", corks_changed, NULL);
759 var_AddCallback(mp, "audio-device", audio_device_changed, NULL);
760 var_AddCallback(mp, "mute", mute_changed, NULL);
761 var_AddCallback(mp, "volume", volume_changed, NULL);
763 /* Snapshot initialization */
764 /* Attach a var callback to the global object to provide the glue between
765 * vout_thread that generates the event and media_player that re-emits it
766 * with its own event manager
768 * FIXME: It's unclear why we want to put this in public API, and why we
769 * want to expose it in such a limiting and ugly way.
771 var_AddCallback(mp->obj.libvlc, "snapshot-file", snapshot_was_taken, mp);
773 libvlc_retain(instance);
774 return mp;
777 /**************************************************************************
778 * Create a Media Instance object with a media descriptor.
779 **************************************************************************/
780 libvlc_media_player_t *
781 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
783 libvlc_media_player_t * p_mi;
785 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
786 if( !p_mi )
787 return NULL;
789 libvlc_media_retain( p_md );
790 p_mi->p_md = p_md;
792 return p_mi;
795 /**************************************************************************
796 * Destroy a Media Instance object (libvlc internal)
798 * Warning: No lock held here, but hey, this is internal. Caller must lock.
799 **************************************************************************/
800 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
802 assert( p_mi );
804 /* Detach Callback from the main libvlc object */
805 var_DelCallback( p_mi->obj.libvlc,
806 "snapshot-file", snapshot_was_taken, p_mi );
808 /* Detach callback from the media player / input manager object */
809 var_DelCallback( p_mi, "volume", volume_changed, NULL );
810 var_DelCallback( p_mi, "mute", mute_changed, NULL );
811 var_DelCallback( p_mi, "audio-device", audio_device_changed, NULL );
812 var_DelCallback( p_mi, "corks", corks_changed, NULL );
814 /* No need for lock_input() because no other threads knows us anymore */
815 if( p_mi->input.p_thread )
816 release_input_thread(p_mi);
817 input_resource_Terminate( p_mi->input.p_resource );
818 input_resource_Release( p_mi->input.p_resource );
819 if( p_mi->input.p_renderer )
820 vlc_renderer_item_release( p_mi->input.p_renderer );
822 vlc_mutex_destroy( &p_mi->input.lock );
824 libvlc_event_manager_destroy(&p_mi->event_manager);
825 libvlc_media_release( p_mi->p_md );
826 vlc_mutex_destroy( &p_mi->object_lock );
828 vlc_http_cookie_jar_t *cookies = var_GetAddress( p_mi, "http-cookies" );
829 if ( cookies )
831 var_Destroy( p_mi, "http-cookies" );
832 vlc_http_cookies_destroy( cookies );
835 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
836 vlc_object_release( p_mi );
837 libvlc_release(instance);
840 /**************************************************************************
841 * Release a Media Instance object.
843 * Function does the locking.
844 **************************************************************************/
845 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
847 bool destroy;
849 assert( p_mi );
850 lock(p_mi);
851 destroy = !--p_mi->i_refcount;
852 unlock(p_mi);
854 if( destroy )
855 libvlc_media_player_destroy( p_mi );
858 /**************************************************************************
859 * Retain a Media Instance object.
861 * Caller must hold the lock.
862 **************************************************************************/
863 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
865 assert( p_mi );
867 lock(p_mi);
868 p_mi->i_refcount++;
869 unlock(p_mi);
872 /**************************************************************************
873 * Set the Media descriptor associated with the instance.
875 * Enter without lock -- function will lock the object.
876 **************************************************************************/
877 void libvlc_media_player_set_media(
878 libvlc_media_player_t *p_mi,
879 libvlc_media_t *p_md )
881 lock_input(p_mi);
883 release_input_thread( p_mi );
885 lock( p_mi );
886 set_state( p_mi, libvlc_NothingSpecial, true );
887 unlock_input( p_mi );
889 libvlc_media_release( p_mi->p_md );
891 if( !p_md )
893 p_mi->p_md = NULL;
894 unlock(p_mi);
895 return; /* It is ok to pass a NULL md */
898 libvlc_media_retain( p_md );
899 p_mi->p_md = p_md;
901 /* The policy here is to ignore that we were created using a different
902 * libvlc_instance, because we don't really care */
903 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
905 unlock(p_mi);
907 /* Send an event for the newly available media */
908 libvlc_event_t event;
909 event.type = libvlc_MediaPlayerMediaChanged;
910 event.u.media_player_media_changed.new_media = p_md;
911 libvlc_event_send( &p_mi->event_manager, &event );
915 /**************************************************************************
916 * Get the Media descriptor associated with the instance.
917 **************************************************************************/
918 libvlc_media_t *
919 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
921 libvlc_media_t *p_m;
923 lock( p_mi );
924 p_m = p_mi->p_md;
925 if( p_m )
926 libvlc_media_retain( p_m );
927 unlock( p_mi );
929 return p_m;
932 /**************************************************************************
933 * Get the event Manager.
934 **************************************************************************/
935 libvlc_event_manager_t *
936 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
938 return &p_mi->event_manager;
941 static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
943 var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
944 var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
945 var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
948 static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
950 var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
951 var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
952 var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
955 /**************************************************************************
956 * Tell media player to start playing.
957 **************************************************************************/
958 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
960 lock_input( p_mi );
962 input_thread_t *p_input_thread = p_mi->input.p_thread;
963 if( p_input_thread )
965 /* A thread already exists, send it a play message */
966 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
967 unlock_input( p_mi );
968 return 0;
971 /* Ignore previous exception */
972 lock(p_mi);
974 if( !p_mi->p_md )
976 unlock(p_mi);
977 unlock_input( p_mi );
978 libvlc_printerr( "No associated media descriptor" );
979 return -1;
982 for( size_t i = 0; i < ARRAY_SIZE( p_mi->selected_es ); ++i )
983 p_mi->selected_es[i] = ES_INIT;
985 media_attach_preparsed_event( p_mi->p_md );
987 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
988 p_mi->input.p_resource,
989 p_mi->input.p_renderer );
990 unlock(p_mi);
991 if( !p_input_thread )
993 unlock_input(p_mi);
994 media_detach_preparsed_event( p_mi->p_md );
995 libvlc_printerr( "Not enough memory" );
996 return -1;
999 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
1000 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
1001 var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
1002 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
1003 add_es_callbacks( p_input_thread, p_mi );
1005 if( input_Start( p_input_thread ) )
1007 unlock_input(p_mi);
1008 del_es_callbacks( p_input_thread, p_mi );
1009 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
1010 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
1011 var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
1012 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
1013 input_Close( p_input_thread );
1014 media_detach_preparsed_event( p_mi->p_md );
1015 libvlc_printerr( "Input initialization failure" );
1016 return -1;
1018 p_mi->input.p_thread = p_input_thread;
1019 unlock_input(p_mi);
1020 return 0;
1023 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
1025 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
1026 if( !p_input_thread )
1027 return;
1029 if( paused )
1031 if( libvlc_media_player_can_pause( p_mi ) )
1032 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
1033 else
1034 input_Stop( p_input_thread );
1036 else
1038 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
1041 vlc_object_release( p_input_thread );
1044 /**************************************************************************
1045 * Toggle pause.
1046 **************************************************************************/
1047 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
1049 libvlc_media_player_set_pause( p_mi, libvlc_media_player_is_playing( p_mi ) );
1052 /**************************************************************************
1053 * Tells whether the media player is currently playing.
1054 **************************************************************************/
1055 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
1057 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
1058 return libvlc_Playing == state;
1061 /**************************************************************************
1062 * Stop playing.
1063 **************************************************************************/
1064 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
1066 lock_input(p_mi);
1067 release_input_thread( p_mi ); /* This will stop the input thread */
1069 /* Force to go to stopped state, in case we were in Ended, or Error
1070 * state. */
1071 if( p_mi->state != libvlc_Stopped )
1073 set_state( p_mi, libvlc_Stopped, false );
1075 /* Construct and send the event */
1076 libvlc_event_t event;
1077 event.type = libvlc_MediaPlayerStopped;
1078 libvlc_event_send( &p_mi->event_manager, &event );
1081 input_resource_Terminate( p_mi->input.p_resource );
1082 unlock_input(p_mi);
1085 int libvlc_media_player_set_renderer( libvlc_media_player_t *p_mi,
1086 libvlc_renderer_item_t *p_litem )
1088 vlc_renderer_item_t *p_item =
1089 p_litem ? libvlc_renderer_item_to_vlc( p_litem ) : NULL;
1091 lock_input( p_mi );
1092 input_thread_t *p_input_thread = p_mi->input.p_thread;
1093 if( p_input_thread )
1094 input_Control( p_input_thread, INPUT_SET_RENDERER, p_item );
1096 if( p_mi->input.p_renderer )
1097 vlc_renderer_item_release( p_mi->input.p_renderer );
1098 p_mi->input.p_renderer = p_item ? vlc_renderer_item_hold( p_item ) : NULL;
1099 unlock_input( p_mi );
1101 return 0;
1104 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
1105 void *(*lock_cb) (void *, void **),
1106 void (*unlock_cb) (void *, void *, void *const *),
1107 void (*display_cb) (void *, void *),
1108 void *opaque )
1110 var_SetAddress( mp, "vmem-lock", lock_cb );
1111 var_SetAddress( mp, "vmem-unlock", unlock_cb );
1112 var_SetAddress( mp, "vmem-display", display_cb );
1113 var_SetAddress( mp, "vmem-data", opaque );
1114 var_SetString( mp, "avcodec-hw", "none" );
1115 var_SetString( mp, "vout", "vmem" );
1116 var_SetString( mp, "window", "none" );
1119 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
1120 libvlc_video_format_cb setup,
1121 libvlc_video_cleanup_cb cleanup )
1123 var_SetAddress( mp, "vmem-setup", setup );
1124 var_SetAddress( mp, "vmem-cleanup", cleanup );
1127 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
1128 unsigned width, unsigned height, unsigned pitch )
1130 var_SetString( mp, "vmem-chroma", chroma );
1131 var_SetInteger( mp, "vmem-width", width );
1132 var_SetInteger( mp, "vmem-height", height );
1133 var_SetInteger( mp, "vmem-pitch", pitch );
1136 /**************************************************************************
1137 * set_nsobject
1138 **************************************************************************/
1139 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1140 void * drawable )
1142 assert (p_mi != NULL);
1143 #ifdef __APPLE__
1144 var_SetString (p_mi, "avcodec-hw", "");
1145 var_SetString (p_mi, "vout", "");
1146 var_SetString (p_mi, "window", "");
1147 var_SetAddress (p_mi, "drawable-nsobject", drawable);
1148 #else
1149 (void)drawable;
1150 libvlc_printerr ("can't set nsobject: APPLE build required");
1151 assert(false);
1152 var_SetString (p_mi, "vout", "none");
1153 var_SetString (p_mi, "window", "none");
1154 #endif
1157 /**************************************************************************
1158 * get_nsobject
1159 **************************************************************************/
1160 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1162 assert (p_mi != NULL);
1163 #ifdef __APPLE__
1164 return var_GetAddress (p_mi, "drawable-nsobject");
1165 #else
1166 (void) p_mi;
1167 return NULL;
1168 #endif
1171 /**************************************************************************
1172 * set_agl
1173 **************************************************************************/
1174 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
1175 uint32_t drawable )
1177 (void)drawable;
1178 libvlc_printerr ("can't set agl: use libvlc_media_player_set_nsobject instead");
1179 assert(false);
1180 var_SetString (p_mi, "vout", "none");
1181 var_SetString (p_mi, "window", "none");
1184 /**************************************************************************
1185 * get_agl
1186 **************************************************************************/
1187 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
1189 (void) p_mi;
1190 return 0;
1193 /**************************************************************************
1194 * set_xwindow
1195 **************************************************************************/
1196 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1197 uint32_t drawable )
1199 assert (p_mi != NULL);
1201 var_SetString (p_mi, "avcodec-hw", "");
1202 var_SetString (p_mi, "vout", "");
1203 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1204 var_SetInteger (p_mi, "drawable-xid", drawable);
1207 /**************************************************************************
1208 * get_xwindow
1209 **************************************************************************/
1210 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1212 return var_GetInteger (p_mi, "drawable-xid");
1215 /**************************************************************************
1216 * set_hwnd
1217 **************************************************************************/
1218 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1219 void *drawable )
1221 assert (p_mi != NULL);
1222 #if defined (_WIN32) || defined (__OS2__)
1223 var_SetString (p_mi, "avcodec-hw", "");
1224 var_SetString (p_mi, "vout", "");
1225 var_SetString (p_mi, "window",
1226 (drawable != NULL) ? "embed-hwnd,any" : "");
1227 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1228 #else
1229 (void) drawable;
1230 libvlc_printerr ("can't set nsobject: WIN32 build required");
1231 assert(false);
1232 var_SetString (p_mi, "vout", "none");
1233 var_SetString (p_mi, "window", "none");
1234 #endif
1237 /**************************************************************************
1238 * get_hwnd
1239 **************************************************************************/
1240 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1242 assert (p_mi != NULL);
1243 #if defined (_WIN32) || defined (__OS2__)
1244 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1245 #else
1246 (void) p_mi;
1247 return NULL;
1248 #endif
1251 /**************************************************************************
1252 * set_android_context
1253 **************************************************************************/
1254 void libvlc_media_player_set_android_context( libvlc_media_player_t *p_mi,
1255 void *p_awindow_handler )
1257 assert (p_mi != NULL);
1258 #ifdef __ANDROID__
1259 var_SetAddress (p_mi, "drawable-androidwindow", p_awindow_handler);
1260 #else
1261 (void) p_awindow_handler;
1262 libvlc_printerr ("can't set android context: ANDROID build required");
1263 assert(false);
1264 var_SetString (p_mi, "vout", "none");
1265 var_SetString (p_mi, "window", "none");
1266 #endif
1269 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1270 libvlc_audio_play_cb play_cb,
1271 libvlc_audio_pause_cb pause_cb,
1272 libvlc_audio_resume_cb resume_cb,
1273 libvlc_audio_flush_cb flush_cb,
1274 libvlc_audio_drain_cb drain_cb,
1275 void *opaque )
1277 var_SetAddress( mp, "amem-play", play_cb );
1278 var_SetAddress( mp, "amem-pause", pause_cb );
1279 var_SetAddress( mp, "amem-resume", resume_cb );
1280 var_SetAddress( mp, "amem-flush", flush_cb );
1281 var_SetAddress( mp, "amem-drain", drain_cb );
1282 var_SetAddress( mp, "amem-data", opaque );
1283 var_SetString( mp, "aout", "amem,none" );
1285 input_resource_ResetAout(mp->input.p_resource);
1288 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1289 libvlc_audio_set_volume_cb cb )
1291 var_SetAddress( mp, "amem-set-volume", cb );
1293 input_resource_ResetAout(mp->input.p_resource);
1296 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1297 libvlc_audio_setup_cb setup,
1298 libvlc_audio_cleanup_cb cleanup )
1300 var_SetAddress( mp, "amem-setup", setup );
1301 var_SetAddress( mp, "amem-cleanup", cleanup );
1303 input_resource_ResetAout(mp->input.p_resource);
1306 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1307 unsigned rate, unsigned channels )
1309 var_SetString( mp, "amem-format", format );
1310 var_SetInteger( mp, "amem-rate", rate );
1311 var_SetInteger( mp, "amem-channels", channels );
1313 input_resource_ResetAout(mp->input.p_resource);
1317 /**************************************************************************
1318 * Getters for stream information
1319 **************************************************************************/
1320 libvlc_time_t libvlc_media_player_get_length(
1321 libvlc_media_player_t *p_mi )
1323 input_thread_t *p_input_thread;
1324 libvlc_time_t i_time;
1326 p_input_thread = libvlc_get_input_thread ( p_mi );
1327 if( !p_input_thread )
1328 return -1;
1330 i_time = from_mtime(var_GetInteger( p_input_thread, "length" ));
1331 vlc_object_release( p_input_thread );
1333 return i_time;
1336 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1338 input_thread_t *p_input_thread;
1339 libvlc_time_t i_time;
1341 p_input_thread = libvlc_get_input_thread ( p_mi );
1342 if( !p_input_thread )
1343 return -1;
1345 i_time = from_mtime(var_GetInteger( p_input_thread , "time" ));
1346 vlc_object_release( p_input_thread );
1347 return i_time;
1350 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1351 libvlc_time_t i_time )
1353 input_thread_t *p_input_thread;
1355 p_input_thread = libvlc_get_input_thread ( p_mi );
1356 if( !p_input_thread )
1357 return;
1359 var_SetInteger( p_input_thread, "time", to_mtime(i_time) );
1360 vlc_object_release( p_input_thread );
1363 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1364 float position )
1366 input_thread_t *p_input_thread;
1368 p_input_thread = libvlc_get_input_thread ( p_mi );
1369 if( !p_input_thread )
1370 return;
1372 var_SetFloat( p_input_thread, "position", position );
1373 vlc_object_release( p_input_thread );
1376 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1378 input_thread_t *p_input_thread;
1379 float f_position;
1381 p_input_thread = libvlc_get_input_thread ( p_mi );
1382 if( !p_input_thread )
1383 return -1.0;
1385 f_position = var_GetFloat( p_input_thread, "position" );
1386 vlc_object_release( p_input_thread );
1388 return f_position;
1391 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1392 int chapter )
1394 input_thread_t *p_input_thread;
1396 p_input_thread = libvlc_get_input_thread ( p_mi );
1397 if( !p_input_thread )
1398 return;
1400 var_SetInteger( p_input_thread, "chapter", chapter );
1401 vlc_object_release( p_input_thread );
1404 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1406 input_thread_t *p_input_thread;
1407 int i_chapter;
1409 p_input_thread = libvlc_get_input_thread ( p_mi );
1410 if( !p_input_thread )
1411 return -1;
1413 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1414 vlc_object_release( p_input_thread );
1416 return i_chapter;
1419 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1421 input_thread_t *p_input_thread;
1422 vlc_value_t val;
1424 p_input_thread = libvlc_get_input_thread ( p_mi );
1425 if( !p_input_thread )
1426 return -1;
1428 int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1429 vlc_object_release( p_input_thread );
1431 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1434 int libvlc_media_player_get_chapter_count_for_title(
1435 libvlc_media_player_t *p_mi,
1436 int i_title )
1438 vlc_value_t val;
1440 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1441 if( !p_input_thread )
1442 return -1;
1444 char psz_name[sizeof ("title ") + 3 * sizeof (int)];
1445 sprintf( psz_name, "title %2u", i_title );
1447 int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1448 vlc_object_release( p_input_thread );
1450 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1453 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1454 int i_title )
1456 input_thread_t *p_input_thread;
1458 p_input_thread = libvlc_get_input_thread ( p_mi );
1459 if( !p_input_thread )
1460 return;
1462 var_SetInteger( p_input_thread, "title", i_title );
1463 vlc_object_release( p_input_thread );
1465 //send event
1466 libvlc_event_t event;
1467 event.type = libvlc_MediaPlayerTitleChanged;
1468 event.u.media_player_title_changed.new_title = i_title;
1469 libvlc_event_send( &p_mi->event_manager, &event );
1472 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1474 input_thread_t *p_input_thread;
1475 int i_title;
1477 p_input_thread = libvlc_get_input_thread ( p_mi );
1478 if( !p_input_thread )
1479 return -1;
1481 i_title = var_GetInteger( p_input_thread, "title" );
1482 vlc_object_release( p_input_thread );
1484 return i_title;
1487 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1489 input_thread_t *p_input_thread;
1490 vlc_value_t val;
1492 p_input_thread = libvlc_get_input_thread ( p_mi );
1493 if( !p_input_thread )
1494 return -1;
1496 int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1497 vlc_object_release( p_input_thread );
1499 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1502 int libvlc_media_player_get_full_title_descriptions( libvlc_media_player_t *p_mi,
1503 libvlc_title_description_t *** pp_titles )
1505 assert( p_mi );
1507 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1509 if( !p_input_thread )
1510 return -1;
1512 input_title_t **p_input_title;
1513 int count;
1515 /* fetch data */
1516 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO,
1517 &p_input_title, &count );
1518 vlc_object_release( p_input_thread );
1519 if( ret != VLC_SUCCESS )
1520 return -1;
1522 libvlc_title_description_t **titles = vlc_alloc( count, sizeof (*titles) );
1523 if( count > 0 && unlikely(titles == NULL) )
1524 return -1;
1526 /* fill array */
1527 for( int i = 0; i < count; i++)
1529 libvlc_title_description_t *title = malloc( sizeof (*title) );
1530 if( unlikely(title == NULL) )
1532 libvlc_title_descriptions_release( titles, i );
1533 return -1;
1535 titles[i] = title;
1537 /* we want to return milliseconds to match the rest of the API */
1538 title->i_duration = p_input_title[i]->i_length / 1000;
1539 title->i_flags = p_input_title[i]->i_flags;
1540 if( p_input_title[i]->psz_name )
1541 title->psz_name = strdup( p_input_title[i]->psz_name );
1542 else
1543 title->psz_name = NULL;
1544 vlc_input_title_Delete( p_input_title[i] );
1546 free( p_input_title );
1548 *pp_titles = titles;
1549 return count;
1552 void libvlc_title_descriptions_release( libvlc_title_description_t **p_titles,
1553 unsigned i_count )
1555 for (unsigned i = 0; i < i_count; i++ )
1557 if ( !p_titles[i] )
1558 continue;
1560 free( p_titles[i]->psz_name );
1561 free( p_titles[i] );
1563 free( p_titles );
1566 int libvlc_media_player_get_full_chapter_descriptions( libvlc_media_player_t *p_mi,
1567 int i_chapters_of_title,
1568 libvlc_chapter_description_t *** pp_chapters )
1570 assert( p_mi );
1572 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1574 if( !p_input_thread )
1575 return -1;
1577 seekpoint_t **p_seekpoint = NULL;
1579 /* fetch data */
1580 int ci_chapter_count = i_chapters_of_title;
1582 int ret = input_Control(p_input_thread, INPUT_GET_SEEKPOINTS, &p_seekpoint, &ci_chapter_count);
1583 if( ret != VLC_SUCCESS)
1585 vlc_object_release( p_input_thread );
1586 return -1;
1589 if (ci_chapter_count == 0 || p_seekpoint == NULL)
1591 vlc_object_release( p_input_thread );
1592 return 0;
1595 input_title_t *p_title;
1596 ret = input_Control( p_input_thread, INPUT_GET_TITLE_INFO, &p_title,
1597 &i_chapters_of_title );
1598 vlc_object_release( p_input_thread );
1599 if( ret != VLC_SUCCESS )
1601 goto error;
1603 int64_t i_title_duration = p_title->i_length / 1000;
1604 vlc_input_title_Delete( p_title );
1606 *pp_chapters = calloc( ci_chapter_count, sizeof(**pp_chapters) );
1607 if( !*pp_chapters )
1609 goto error;
1612 /* fill array */
1613 for( int i = 0; i < ci_chapter_count; ++i )
1615 libvlc_chapter_description_t *p_chapter = malloc( sizeof(*p_chapter) );
1616 if( unlikely(p_chapter == NULL) )
1618 goto error;
1620 (*pp_chapters)[i] = p_chapter;
1622 p_chapter->i_time_offset = p_seekpoint[i]->i_time_offset / 1000;
1624 if( i < ci_chapter_count - 1 )
1626 p_chapter->i_duration = p_seekpoint[i + 1]->i_time_offset / 1000 -
1627 p_chapter->i_time_offset;
1629 else
1631 if ( i_title_duration )
1632 p_chapter->i_duration = i_title_duration - p_chapter->i_time_offset;
1633 else
1634 p_chapter->i_duration = 0;
1637 if( p_seekpoint[i]->psz_name )
1639 p_chapter->psz_name = strdup( p_seekpoint[i]->psz_name );
1641 else
1643 p_chapter->psz_name = NULL;
1645 vlc_seekpoint_Delete( p_seekpoint[i] );
1646 p_seekpoint[i] = NULL;
1649 free( p_seekpoint );
1650 return ci_chapter_count;
1652 error:
1653 if( *pp_chapters )
1654 libvlc_chapter_descriptions_release( *pp_chapters, ci_chapter_count );
1655 for ( int i = 0; i < ci_chapter_count; ++i )
1656 vlc_seekpoint_Delete( p_seekpoint[i] );
1657 free( p_seekpoint );
1658 return -1;
1661 void libvlc_chapter_descriptions_release( libvlc_chapter_description_t **p_chapters,
1662 unsigned i_count )
1664 for (unsigned i = 0; i < i_count; i++ )
1666 if ( !p_chapters[i] )
1667 continue;
1669 free( p_chapters[i]->psz_name );
1670 free( p_chapters[i] );
1672 free( p_chapters );
1675 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1677 input_thread_t *p_input_thread;
1679 p_input_thread = libvlc_get_input_thread ( p_mi );
1680 if( !p_input_thread )
1681 return;
1683 int i_type = var_Type( p_input_thread, "next-chapter" );
1684 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1685 "next-chapter":"next-title" );
1687 vlc_object_release( p_input_thread );
1690 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1692 input_thread_t *p_input_thread;
1694 p_input_thread = libvlc_get_input_thread ( p_mi );
1695 if( !p_input_thread )
1696 return;
1698 int i_type = var_Type( p_input_thread, "next-chapter" );
1699 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1700 "prev-chapter":"prev-title" );
1702 vlc_object_release( p_input_thread );
1705 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1707 libvlc_media_t *media = libvlc_media_player_get_media( p_mi );
1708 if( media == NULL )
1709 return 0.f;
1711 input_item_t *item = media->p_input_item;
1712 float fps = 0.f;
1714 vlc_mutex_lock( &item->lock );
1715 for( int i = 0; i < item->i_es; i++ )
1717 const es_format_t *fmt = item->es[i];
1719 if( fmt->i_cat == VIDEO_ES && fmt->video.i_frame_rate_base > 0 )
1720 fps = (float)fmt->video.i_frame_rate
1721 / (float)fmt->video.i_frame_rate_base;
1723 vlc_mutex_unlock( &item->lock );
1724 libvlc_media_release( media );
1726 return fps;
1729 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1731 input_thread_t *p_input_thread =
1732 libvlc_get_input_thread ( p_mi );
1733 if ( !p_input_thread )
1734 return false;
1736 int state = var_GetInteger( p_input_thread, "state" );
1737 vlc_object_release( p_input_thread );
1739 return state != END_S && state != ERROR_S;
1742 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1744 var_SetFloat (p_mi, "rate", rate);
1746 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1747 if( !p_input_thread )
1748 return 0;
1749 var_SetFloat( p_input_thread, "rate", rate );
1750 vlc_object_release( p_input_thread );
1751 return 0;
1754 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1756 return var_GetFloat (p_mi, "rate");
1759 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1761 lock(p_mi);
1762 libvlc_state_t state = p_mi->state;
1763 unlock(p_mi);
1764 return state;
1767 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1769 input_thread_t *p_input_thread;
1770 bool b_seekable;
1772 p_input_thread = libvlc_get_input_thread ( p_mi );
1773 if ( !p_input_thread )
1774 return false;
1775 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1776 vlc_object_release( p_input_thread );
1778 return b_seekable;
1781 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1782 unsigned navigate )
1784 static const int map[] =
1786 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1787 INPUT_NAV_LEFT, INPUT_NAV_RIGHT, INPUT_NAV_POPUP,
1790 if( navigate >= sizeof(map) / sizeof(map[0]) )
1791 return;
1793 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1794 if ( p_input == NULL )
1795 return;
1797 input_Control( p_input, map[navigate], NULL );
1798 vlc_object_release( p_input );
1801 /* internal function, used by audio, video */
1802 libvlc_track_description_t *
1803 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1804 const char *psz_variable )
1806 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1807 libvlc_track_description_t *p_track_description = NULL,
1808 *p_actual, *p_previous;
1810 if( !p_input )
1811 return NULL;
1813 vlc_value_t val_list, text_list;
1814 int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES, &val_list, &text_list );
1815 if( i_ret != VLC_SUCCESS )
1816 return NULL;
1818 /* no tracks */
1819 if( val_list.p_list->i_count <= 0 )
1820 goto end;
1822 p_track_description = malloc( sizeof *p_track_description );
1823 if ( !p_track_description )
1825 libvlc_printerr( "Not enough memory" );
1826 goto end;
1828 p_actual = p_track_description;
1829 p_previous = NULL;
1830 for( int i = 0; i < val_list.p_list->i_count; i++ )
1832 if( !p_actual )
1834 p_actual = malloc( sizeof *p_actual );
1835 if ( !p_actual )
1837 libvlc_track_description_list_release( p_track_description );
1838 libvlc_printerr( "Not enough memory" );
1840 p_track_description = NULL;
1841 goto end;
1844 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1845 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1846 p_actual->p_next = NULL;
1847 if( p_previous )
1848 p_previous->p_next = p_actual;
1849 p_previous = p_actual;
1850 p_actual = NULL;
1853 end:
1854 var_FreeList( &val_list, &text_list );
1855 vlc_object_release( p_input );
1857 return p_track_description;
1860 // Deprecated alias for libvlc_track_description_list_release
1861 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1863 libvlc_track_description_list_release( p_td );
1866 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1868 libvlc_track_description_t *p_actual, *p_before;
1869 p_actual = p_td;
1871 while ( p_actual )
1873 free( p_actual->psz_name );
1874 p_before = p_actual;
1875 p_actual = p_before->p_next;
1876 free( p_before );
1880 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1882 input_thread_t *p_input_thread;
1883 bool b_can_pause;
1885 p_input_thread = libvlc_get_input_thread ( p_mi );
1886 if ( !p_input_thread )
1887 return false;
1888 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1889 vlc_object_release( p_input_thread );
1891 return b_can_pause;
1894 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1896 input_thread_t *p_input_thread;
1897 bool b_program_scrambled;
1899 p_input_thread = libvlc_get_input_thread ( p_mi );
1900 if ( !p_input_thread )
1901 return false;
1902 b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1903 vlc_object_release( p_input_thread );
1905 return b_program_scrambled;
1908 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1910 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1911 if( p_input_thread != NULL )
1913 var_TriggerCallback( p_input_thread, "frame-next" );
1914 vlc_object_release( p_input_thread );
1919 * Private lookup table to get subpicture alignment flag values corresponding
1920 * to a libvlc_position_t enumerated value.
1922 static const unsigned char position_subpicture_alignment[] = {
1923 [libvlc_position_center] = 0,
1924 [libvlc_position_left] = SUBPICTURE_ALIGN_LEFT,
1925 [libvlc_position_right] = SUBPICTURE_ALIGN_RIGHT,
1926 [libvlc_position_top] = SUBPICTURE_ALIGN_TOP,
1927 [libvlc_position_top_left] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1928 [libvlc_position_top_right] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1929 [libvlc_position_bottom] = SUBPICTURE_ALIGN_BOTTOM,
1930 [libvlc_position_bottom_left] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1931 [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1934 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1936 assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1938 if ( position != libvlc_position_disable )
1940 var_SetBool( p_mi, "video-title-show", true );
1941 var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1942 var_SetInteger( p_mi, "video-title-timeout", timeout );
1944 else
1946 var_SetBool( p_mi, "video-title-show", false );
1950 int libvlc_media_player_add_slave( libvlc_media_player_t *p_mi,
1951 libvlc_media_slave_type_t i_type,
1952 const char *psz_uri, bool b_select )
1954 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1956 if( p_input_thread == NULL )
1958 libvlc_media_t *p_media = libvlc_media_player_get_media( p_mi );
1959 if( p_media == NULL )
1960 return -1;
1962 int i_ret = libvlc_media_slaves_add( p_media, i_type, 4, psz_uri );
1963 libvlc_media_release( p_media );
1964 return i_ret;
1966 else
1968 int i_ret = input_AddSlave( p_input_thread, (enum slave_type) i_type,
1969 psz_uri, b_select, false, false );
1970 vlc_object_release( p_input_thread );
1972 return i_ret == VLC_SUCCESS ? 0 : -1;
1977 * Maximum size of a formatted equalizer amplification band frequency value.
1979 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1981 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1982 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1983 * terminator).
1985 #define EQZ_BAND_VALUE_SIZE 12
1987 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1989 char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1991 if( p_equalizer != NULL )
1993 for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1995 c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
1996 p_equalizer->f_amp[i] );
1997 if( unlikely(c >= sizeof(bands)) )
1998 return -1;
2001 var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
2002 var_SetString( p_mi, "equalizer-bands", bands );
2004 var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
2006 audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
2007 if( p_aout != NULL )
2009 if( p_equalizer != NULL )
2011 var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
2012 var_SetString( p_aout, "equalizer-bands", bands );
2015 var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
2016 vlc_object_release( p_aout );
2019 return 0;
2022 static const char roles[][16] =
2024 [libvlc_role_Music] = "music",
2025 [libvlc_role_Video] = "video",
2026 [libvlc_role_Communication] = "communication",
2027 [libvlc_role_Game] = "game",
2028 [libvlc_role_Notification] = "notification",
2029 [libvlc_role_Animation] = "animation",
2030 [libvlc_role_Production] = "production",
2031 [libvlc_role_Accessibility] = "accessibility",
2032 [libvlc_role_Test] = "test",
2035 int libvlc_media_player_set_role(libvlc_media_player_t *mp, unsigned role)
2037 if (role >= ARRAY_SIZE(roles)
2038 || var_SetString(mp, "role", roles[role]) != VLC_SUCCESS)
2039 return -1;
2040 return 0;
2043 int libvlc_media_player_get_role(libvlc_media_player_t *mp)
2045 int ret = -1;
2046 char *str = var_GetString(mp, "role");
2047 if (str == NULL)
2048 return 0;
2050 for (size_t i = 0; i < ARRAY_SIZE(roles); i++)
2051 if (!strcmp(roles[i], str))
2053 ret = i;
2054 break;
2057 free(str);
2058 return ret;