contrib: update libogg to 1.3.3
[vlc.git] / lib / media_player.c
blobdaef2faedd78a85be723f5968208143944fd5584
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_resource = input_resource_New(VLC_OBJECT(mp));
732 if (unlikely(mp->input.p_resource == NULL))
734 vlc_object_release(mp);
735 return NULL;
737 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
738 if( aout != NULL )
739 input_resource_PutAout(mp->input.p_resource, aout);
741 vlc_viewpoint_init(&mp->viewpoint);
743 var_Create (mp, "viewpoint", VLC_VAR_ADDRESS);
744 var_SetAddress( mp, "viewpoint", &mp->viewpoint );
745 vlc_mutex_init (&mp->input.lock);
746 mp->i_refcount = 1;
747 libvlc_event_manager_init(&mp->event_manager, mp);
748 vlc_mutex_init(&mp->object_lock);
750 var_AddCallback(mp, "corks", corks_changed, NULL);
751 var_AddCallback(mp, "audio-device", audio_device_changed, NULL);
752 var_AddCallback(mp, "mute", mute_changed, NULL);
753 var_AddCallback(mp, "volume", volume_changed, NULL);
755 /* Snapshot initialization */
756 /* Attach a var callback to the global object to provide the glue between
757 * vout_thread that generates the event and media_player that re-emits it
758 * with its own event manager
760 * FIXME: It's unclear why we want to put this in public API, and why we
761 * want to expose it in such a limiting and ugly way.
763 var_AddCallback(mp->obj.libvlc, "snapshot-file", snapshot_was_taken, mp);
765 libvlc_retain(instance);
766 return mp;
769 /**************************************************************************
770 * Create a Media Instance object with a media descriptor.
771 **************************************************************************/
772 libvlc_media_player_t *
773 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
775 libvlc_media_player_t * p_mi;
777 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
778 if( !p_mi )
779 return NULL;
781 libvlc_media_retain( p_md );
782 p_mi->p_md = p_md;
784 return p_mi;
787 /**************************************************************************
788 * Destroy a Media Instance object (libvlc internal)
790 * Warning: No lock held here, but hey, this is internal. Caller must lock.
791 **************************************************************************/
792 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
794 assert( p_mi );
796 /* Detach Callback from the main libvlc object */
797 var_DelCallback( p_mi->obj.libvlc,
798 "snapshot-file", snapshot_was_taken, p_mi );
800 /* Detach callback from the media player / input manager object */
801 var_DelCallback( p_mi, "volume", volume_changed, NULL );
802 var_DelCallback( p_mi, "mute", mute_changed, NULL );
803 var_DelCallback( p_mi, "audio-device", audio_device_changed, NULL );
804 var_DelCallback( p_mi, "corks", corks_changed, NULL );
806 /* No need for lock_input() because no other threads knows us anymore */
807 if( p_mi->input.p_thread )
808 release_input_thread(p_mi);
809 input_resource_Terminate( p_mi->input.p_resource );
810 input_resource_Release( p_mi->input.p_resource );
811 vlc_mutex_destroy( &p_mi->input.lock );
813 libvlc_event_manager_destroy(&p_mi->event_manager);
814 libvlc_media_release( p_mi->p_md );
815 vlc_mutex_destroy( &p_mi->object_lock );
817 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
818 vlc_object_release( p_mi );
819 libvlc_release(instance);
822 /**************************************************************************
823 * Release a Media Instance object.
825 * Function does the locking.
826 **************************************************************************/
827 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
829 bool destroy;
831 assert( p_mi );
832 lock(p_mi);
833 destroy = !--p_mi->i_refcount;
834 unlock(p_mi);
836 if( destroy )
837 libvlc_media_player_destroy( p_mi );
840 /**************************************************************************
841 * Retain a Media Instance object.
843 * Caller must hold the lock.
844 **************************************************************************/
845 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
847 assert( p_mi );
849 lock(p_mi);
850 p_mi->i_refcount++;
851 unlock(p_mi);
854 /**************************************************************************
855 * Set the Media descriptor associated with the instance.
857 * Enter without lock -- function will lock the object.
858 **************************************************************************/
859 void libvlc_media_player_set_media(
860 libvlc_media_player_t *p_mi,
861 libvlc_media_t *p_md )
863 lock_input(p_mi);
865 release_input_thread( p_mi );
867 lock( p_mi );
868 set_state( p_mi, libvlc_NothingSpecial, true );
869 unlock_input( p_mi );
871 libvlc_media_release( p_mi->p_md );
873 if( !p_md )
875 p_mi->p_md = NULL;
876 unlock(p_mi);
877 return; /* It is ok to pass a NULL md */
880 libvlc_media_retain( p_md );
881 p_mi->p_md = p_md;
883 /* The policy here is to ignore that we were created using a different
884 * libvlc_instance, because we don't really care */
885 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
887 unlock(p_mi);
889 /* Send an event for the newly available media */
890 libvlc_event_t event;
891 event.type = libvlc_MediaPlayerMediaChanged;
892 event.u.media_player_media_changed.new_media = p_md;
893 libvlc_event_send( &p_mi->event_manager, &event );
897 /**************************************************************************
898 * Get the Media descriptor associated with the instance.
899 **************************************************************************/
900 libvlc_media_t *
901 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
903 libvlc_media_t *p_m;
905 lock( p_mi );
906 p_m = p_mi->p_md;
907 if( p_m )
908 libvlc_media_retain( p_m );
909 unlock( p_mi );
911 return p_m;
914 /**************************************************************************
915 * Get the event Manager.
916 **************************************************************************/
917 libvlc_event_manager_t *
918 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
920 return &p_mi->event_manager;
923 static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
925 var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
926 var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
927 var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
930 static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
932 var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
933 var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
934 var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
937 /**************************************************************************
938 * Tell media player to start playing.
939 **************************************************************************/
940 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
942 lock_input( p_mi );
944 input_thread_t *p_input_thread = p_mi->input.p_thread;
945 if( p_input_thread )
947 /* A thread already exists, send it a play message */
948 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
949 unlock_input( p_mi );
950 return 0;
953 /* Ignore previous exception */
954 lock(p_mi);
956 if( !p_mi->p_md )
958 unlock(p_mi);
959 unlock_input( p_mi );
960 libvlc_printerr( "No associated media descriptor" );
961 return -1;
964 for( size_t i = 0; i < ARRAY_SIZE( p_mi->selected_es ); ++i )
965 p_mi->selected_es[i] = ES_INIT;
967 media_attach_preparsed_event( p_mi->p_md );
969 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
970 p_mi->input.p_resource, NULL );
971 unlock(p_mi);
972 if( !p_input_thread )
974 unlock_input(p_mi);
975 media_detach_preparsed_event( p_mi->p_md );
976 libvlc_printerr( "Not enough memory" );
977 return -1;
980 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
981 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
982 var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
983 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
984 add_es_callbacks( p_input_thread, p_mi );
986 if( input_Start( p_input_thread ) )
988 unlock_input(p_mi);
989 del_es_callbacks( p_input_thread, p_mi );
990 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
991 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
992 var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
993 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
994 input_Close( p_input_thread );
995 media_detach_preparsed_event( p_mi->p_md );
996 libvlc_printerr( "Input initialization failure" );
997 return -1;
999 p_mi->input.p_thread = p_input_thread;
1000 unlock_input(p_mi);
1001 return 0;
1004 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
1006 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
1007 if( !p_input_thread )
1008 return;
1010 if( paused )
1012 if( libvlc_media_player_can_pause( p_mi ) )
1013 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
1014 else
1015 input_Stop( p_input_thread );
1017 else
1019 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
1022 vlc_object_release( p_input_thread );
1025 /**************************************************************************
1026 * Toggle pause.
1027 **************************************************************************/
1028 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
1030 libvlc_media_player_set_pause( p_mi, libvlc_media_player_is_playing( p_mi ) );
1033 /**************************************************************************
1034 * Tells whether the media player is currently playing.
1035 **************************************************************************/
1036 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
1038 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
1039 return libvlc_Playing == state;
1042 /**************************************************************************
1043 * Stop playing.
1044 **************************************************************************/
1045 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
1047 lock_input(p_mi);
1048 release_input_thread( p_mi ); /* This will stop the input thread */
1050 /* Force to go to stopped state, in case we were in Ended, or Error
1051 * state. */
1052 if( p_mi->state != libvlc_Stopped )
1054 set_state( p_mi, libvlc_Stopped, false );
1056 /* Construct and send the event */
1057 libvlc_event_t event;
1058 event.type = libvlc_MediaPlayerStopped;
1059 libvlc_event_send( &p_mi->event_manager, &event );
1062 input_resource_Terminate( p_mi->input.p_resource );
1063 unlock_input(p_mi);
1066 int libvlc_media_player_set_renderer( libvlc_media_player_t *p_mi,
1067 const libvlc_renderer_item_t *p_litem )
1069 const vlc_renderer_item_t *p_item = libvlc_renderer_item_to_vlc( p_litem );
1070 char *psz_sout;
1071 if( asprintf( &psz_sout, "#%s", vlc_renderer_item_sout( p_item ) ) == -1 )
1072 return -1;
1074 var_SetString( p_mi, "sout", psz_sout );
1075 var_SetString( p_mi, "demux-filter", vlc_renderer_item_demux_filter( p_item ) );
1076 free( psz_sout );
1077 return 0;
1080 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
1081 void *(*lock_cb) (void *, void **),
1082 void (*unlock_cb) (void *, void *, void *const *),
1083 void (*display_cb) (void *, void *),
1084 void *opaque )
1086 var_SetAddress( mp, "vmem-lock", lock_cb );
1087 var_SetAddress( mp, "vmem-unlock", unlock_cb );
1088 var_SetAddress( mp, "vmem-display", display_cb );
1089 var_SetAddress( mp, "vmem-data", opaque );
1090 var_SetString( mp, "avcodec-hw", "none" );
1091 var_SetString( mp, "vout", "vmem" );
1092 var_SetString( mp, "window", "none" );
1095 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
1096 libvlc_video_format_cb setup,
1097 libvlc_video_cleanup_cb cleanup )
1099 var_SetAddress( mp, "vmem-setup", setup );
1100 var_SetAddress( mp, "vmem-cleanup", cleanup );
1103 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
1104 unsigned width, unsigned height, unsigned pitch )
1106 var_SetString( mp, "vmem-chroma", chroma );
1107 var_SetInteger( mp, "vmem-width", width );
1108 var_SetInteger( mp, "vmem-height", height );
1109 var_SetInteger( mp, "vmem-pitch", pitch );
1112 /**************************************************************************
1113 * set_nsobject
1114 **************************************************************************/
1115 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1116 void * drawable )
1118 assert (p_mi != NULL);
1119 #ifdef __APPLE__
1120 var_SetString (p_mi, "avcodec-hw", "");
1121 var_SetString (p_mi, "vout", "");
1122 var_SetString (p_mi, "window", "");
1123 var_SetAddress (p_mi, "drawable-nsobject", drawable);
1124 #else
1125 (void)drawable;
1126 libvlc_printerr ("can't set nsobject: APPLE build required");
1127 assert(false);
1128 var_SetString (p_mi, "vout", "none");
1129 var_SetString (p_mi, "window", "none");
1130 #endif
1133 /**************************************************************************
1134 * get_nsobject
1135 **************************************************************************/
1136 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1138 assert (p_mi != NULL);
1139 #ifdef __APPLE__
1140 return var_GetAddress (p_mi, "drawable-nsobject");
1141 #else
1142 (void) p_mi;
1143 return NULL;
1144 #endif
1147 /**************************************************************************
1148 * set_agl
1149 **************************************************************************/
1150 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
1151 uint32_t drawable )
1153 (void)drawable;
1154 libvlc_printerr ("can't set agl: use libvlc_media_player_set_nsobject instead");
1155 assert(false);
1156 var_SetString (p_mi, "vout", "none");
1157 var_SetString (p_mi, "window", "none");
1160 /**************************************************************************
1161 * get_agl
1162 **************************************************************************/
1163 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
1165 (void) p_mi;
1166 return 0;
1169 /**************************************************************************
1170 * set_xwindow
1171 **************************************************************************/
1172 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1173 uint32_t drawable )
1175 assert (p_mi != NULL);
1177 var_SetString (p_mi, "avcodec-hw", "");
1178 var_SetString (p_mi, "vout", "");
1179 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1180 var_SetInteger (p_mi, "drawable-xid", drawable);
1183 /**************************************************************************
1184 * get_xwindow
1185 **************************************************************************/
1186 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1188 return var_GetInteger (p_mi, "drawable-xid");
1191 /**************************************************************************
1192 * set_hwnd
1193 **************************************************************************/
1194 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1195 void *drawable )
1197 assert (p_mi != NULL);
1198 #if defined (_WIN32) || defined (__OS2__)
1199 var_SetString (p_mi, "avcodec-hw", "");
1200 var_SetString (p_mi, "vout", "");
1201 var_SetString (p_mi, "window",
1202 (drawable != NULL) ? "embed-hwnd,any" : "");
1203 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1204 #else
1205 (void) drawable;
1206 libvlc_printerr ("can't set nsobject: WIN32 build required");
1207 assert(false);
1208 var_SetString (p_mi, "vout", "none");
1209 var_SetString (p_mi, "window", "none");
1210 #endif
1213 /**************************************************************************
1214 * get_hwnd
1215 **************************************************************************/
1216 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1218 assert (p_mi != NULL);
1219 #if defined (_WIN32) || defined (__OS2__)
1220 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1221 #else
1222 (void) p_mi;
1223 return NULL;
1224 #endif
1227 /**************************************************************************
1228 * set_android_context
1229 **************************************************************************/
1230 void libvlc_media_player_set_android_context( libvlc_media_player_t *p_mi,
1231 void *p_awindow_handler )
1233 assert (p_mi != NULL);
1234 #ifdef __ANDROID__
1235 var_SetAddress (p_mi, "drawable-androidwindow", p_awindow_handler);
1236 #else
1237 (void) p_awindow_handler;
1238 libvlc_printerr ("can't set android context: ANDROID build required");
1239 assert(false);
1240 var_SetString (p_mi, "vout", "none");
1241 var_SetString (p_mi, "window", "none");
1242 #endif
1245 /**************************************************************************
1246 * set_evas_object
1247 **************************************************************************/
1248 int libvlc_media_player_set_evas_object( libvlc_media_player_t *p_mi,
1249 void *p_evas_object )
1251 assert (p_mi != NULL);
1252 #ifdef HAVE_EVAS
1253 var_SetString (p_mi, "vout", "evas");
1254 var_SetString (p_mi, "window", "none");
1255 var_SetAddress (p_mi, "drawable-evasobject", p_evas_object);
1256 return 0;
1257 #else
1258 (void) p_mi; (void) p_evas_object;
1259 return -1;
1260 #endif
1263 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1264 libvlc_audio_play_cb play_cb,
1265 libvlc_audio_pause_cb pause_cb,
1266 libvlc_audio_resume_cb resume_cb,
1267 libvlc_audio_flush_cb flush_cb,
1268 libvlc_audio_drain_cb drain_cb,
1269 void *opaque )
1271 var_SetAddress( mp, "amem-play", play_cb );
1272 var_SetAddress( mp, "amem-pause", pause_cb );
1273 var_SetAddress( mp, "amem-resume", resume_cb );
1274 var_SetAddress( mp, "amem-flush", flush_cb );
1275 var_SetAddress( mp, "amem-drain", drain_cb );
1276 var_SetAddress( mp, "amem-data", opaque );
1277 var_SetString( mp, "aout", "amem,none" );
1279 input_resource_ResetAout(mp->input.p_resource);
1282 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1283 libvlc_audio_set_volume_cb cb )
1285 var_SetAddress( mp, "amem-set-volume", cb );
1287 input_resource_ResetAout(mp->input.p_resource);
1290 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1291 libvlc_audio_setup_cb setup,
1292 libvlc_audio_cleanup_cb cleanup )
1294 var_SetAddress( mp, "amem-setup", setup );
1295 var_SetAddress( mp, "amem-cleanup", cleanup );
1297 input_resource_ResetAout(mp->input.p_resource);
1300 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1301 unsigned rate, unsigned channels )
1303 var_SetString( mp, "amem-format", format );
1304 var_SetInteger( mp, "amem-rate", rate );
1305 var_SetInteger( mp, "amem-channels", channels );
1307 input_resource_ResetAout(mp->input.p_resource);
1311 /**************************************************************************
1312 * Getters for stream information
1313 **************************************************************************/
1314 libvlc_time_t libvlc_media_player_get_length(
1315 libvlc_media_player_t *p_mi )
1317 input_thread_t *p_input_thread;
1318 libvlc_time_t i_time;
1320 p_input_thread = libvlc_get_input_thread ( p_mi );
1321 if( !p_input_thread )
1322 return -1;
1324 i_time = from_mtime(var_GetInteger( p_input_thread, "length" ));
1325 vlc_object_release( p_input_thread );
1327 return i_time;
1330 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1332 input_thread_t *p_input_thread;
1333 libvlc_time_t i_time;
1335 p_input_thread = libvlc_get_input_thread ( p_mi );
1336 if( !p_input_thread )
1337 return -1;
1339 i_time = from_mtime(var_GetInteger( p_input_thread , "time" ));
1340 vlc_object_release( p_input_thread );
1341 return i_time;
1344 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1345 libvlc_time_t i_time )
1347 input_thread_t *p_input_thread;
1349 p_input_thread = libvlc_get_input_thread ( p_mi );
1350 if( !p_input_thread )
1351 return;
1353 var_SetInteger( p_input_thread, "time", to_mtime(i_time) );
1354 vlc_object_release( p_input_thread );
1357 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1358 float position )
1360 input_thread_t *p_input_thread;
1362 p_input_thread = libvlc_get_input_thread ( p_mi );
1363 if( !p_input_thread )
1364 return;
1366 var_SetFloat( p_input_thread, "position", position );
1367 vlc_object_release( p_input_thread );
1370 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1372 input_thread_t *p_input_thread;
1373 float f_position;
1375 p_input_thread = libvlc_get_input_thread ( p_mi );
1376 if( !p_input_thread )
1377 return -1.0;
1379 f_position = var_GetFloat( p_input_thread, "position" );
1380 vlc_object_release( p_input_thread );
1382 return f_position;
1385 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1386 int chapter )
1388 input_thread_t *p_input_thread;
1390 p_input_thread = libvlc_get_input_thread ( p_mi );
1391 if( !p_input_thread )
1392 return;
1394 var_SetInteger( p_input_thread, "chapter", chapter );
1395 vlc_object_release( p_input_thread );
1398 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1400 input_thread_t *p_input_thread;
1401 int i_chapter;
1403 p_input_thread = libvlc_get_input_thread ( p_mi );
1404 if( !p_input_thread )
1405 return -1;
1407 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1408 vlc_object_release( p_input_thread );
1410 return i_chapter;
1413 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1415 input_thread_t *p_input_thread;
1416 vlc_value_t val;
1418 p_input_thread = libvlc_get_input_thread ( p_mi );
1419 if( !p_input_thread )
1420 return -1;
1422 int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1423 vlc_object_release( p_input_thread );
1425 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1428 int libvlc_media_player_get_chapter_count_for_title(
1429 libvlc_media_player_t *p_mi,
1430 int i_title )
1432 vlc_value_t val;
1434 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1435 if( !p_input_thread )
1436 return -1;
1438 char psz_name[sizeof ("title ") + 3 * sizeof (int)];
1439 sprintf( psz_name, "title %2u", i_title );
1441 int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1442 vlc_object_release( p_input_thread );
1444 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1447 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1448 int i_title )
1450 input_thread_t *p_input_thread;
1452 p_input_thread = libvlc_get_input_thread ( p_mi );
1453 if( !p_input_thread )
1454 return;
1456 var_SetInteger( p_input_thread, "title", i_title );
1457 vlc_object_release( p_input_thread );
1459 //send event
1460 libvlc_event_t event;
1461 event.type = libvlc_MediaPlayerTitleChanged;
1462 event.u.media_player_title_changed.new_title = i_title;
1463 libvlc_event_send( &p_mi->event_manager, &event );
1466 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1468 input_thread_t *p_input_thread;
1469 int i_title;
1471 p_input_thread = libvlc_get_input_thread ( p_mi );
1472 if( !p_input_thread )
1473 return -1;
1475 i_title = var_GetInteger( p_input_thread, "title" );
1476 vlc_object_release( p_input_thread );
1478 return i_title;
1481 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1483 input_thread_t *p_input_thread;
1484 vlc_value_t val;
1486 p_input_thread = libvlc_get_input_thread ( p_mi );
1487 if( !p_input_thread )
1488 return -1;
1490 int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1491 vlc_object_release( p_input_thread );
1493 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1496 int libvlc_media_player_get_full_title_descriptions( libvlc_media_player_t *p_mi,
1497 libvlc_title_description_t *** pp_titles )
1499 assert( p_mi );
1501 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1503 if( !p_input_thread )
1504 return -1;
1506 input_title_t **p_input_title;
1507 int count;
1509 /* fetch data */
1510 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO,
1511 &p_input_title, &count );
1512 vlc_object_release( p_input_thread );
1513 if( ret != VLC_SUCCESS )
1514 return -1;
1516 libvlc_title_description_t **titles = vlc_alloc( count, sizeof (*titles) );
1517 if( count > 0 && unlikely(titles == NULL) )
1518 return -1;
1520 /* fill array */
1521 for( int i = 0; i < count; i++)
1523 libvlc_title_description_t *title = malloc( sizeof (*title) );
1524 if( unlikely(title == NULL) )
1526 libvlc_title_descriptions_release( titles, i );
1527 return -1;
1529 titles[i] = title;
1531 /* we want to return milliseconds to match the rest of the API */
1532 title->i_duration = p_input_title[i]->i_length / 1000;
1533 title->i_flags = p_input_title[i]->i_flags;
1534 if( p_input_title[i]->psz_name )
1535 title->psz_name = strdup( p_input_title[i]->psz_name );
1536 else
1537 title->psz_name = NULL;
1538 vlc_input_title_Delete( p_input_title[i] );
1540 free( p_input_title );
1542 *pp_titles = titles;
1543 return count;
1546 void libvlc_title_descriptions_release( libvlc_title_description_t **p_titles,
1547 unsigned i_count )
1549 for (unsigned i = 0; i < i_count; i++ )
1551 if ( !p_titles[i] )
1552 continue;
1554 free( p_titles[i]->psz_name );
1555 free( p_titles[i] );
1557 free( p_titles );
1560 int libvlc_media_player_get_full_chapter_descriptions( libvlc_media_player_t *p_mi,
1561 int i_chapters_of_title,
1562 libvlc_chapter_description_t *** pp_chapters )
1564 assert( p_mi );
1566 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1568 if( !p_input_thread )
1569 return -1;
1571 seekpoint_t **p_seekpoint = NULL;
1573 /* fetch data */
1574 int ret = input_Control(p_input_thread, INPUT_GET_SEEKPOINTS, &p_seekpoint, &i_chapters_of_title);
1575 vlc_object_release( p_input_thread );
1577 if( ret != VLC_SUCCESS)
1579 return -1;
1582 if (i_chapters_of_title == 0 || p_seekpoint == NULL)
1584 return 0;
1587 const int ci_chapter_count = (const int)i_chapters_of_title;
1589 *pp_chapters = calloc( ci_chapter_count, sizeof(**pp_chapters) );
1590 if( !*pp_chapters )
1592 return -1;
1595 /* fill array */
1596 for( int i = 0; i < ci_chapter_count; i++)
1598 libvlc_chapter_description_t *p_chapter = malloc( sizeof(*p_chapter) );
1599 if( unlikely(p_chapter == NULL) )
1601 libvlc_chapter_descriptions_release( *pp_chapters, ci_chapter_count );
1602 return -1;
1604 (*pp_chapters)[i] = p_chapter;
1606 p_chapter->i_time_offset = p_seekpoint[i]->i_time_offset / 1000;
1608 if( i > 0 )
1610 p_chapter->i_duration = p_chapter->i_time_offset - (*pp_chapters)[i-1]->i_time_offset;
1612 else
1614 p_chapter->i_duration = p_chapter->i_time_offset;
1617 if( p_seekpoint[i]->psz_name )
1619 p_chapter->psz_name = strdup( p_seekpoint[i]->psz_name );
1621 else
1623 p_chapter->psz_name = NULL;
1625 vlc_seekpoint_Delete( p_seekpoint[i] );
1628 return ci_chapter_count;
1631 void libvlc_chapter_descriptions_release( libvlc_chapter_description_t **p_chapters,
1632 unsigned i_count )
1634 for (unsigned i = 0; i < i_count; i++ )
1636 if ( !p_chapters[i] )
1637 continue;
1639 free( p_chapters[i]->psz_name );
1640 free( p_chapters[i] );
1642 free( p_chapters );
1645 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1647 input_thread_t *p_input_thread;
1649 p_input_thread = libvlc_get_input_thread ( p_mi );
1650 if( !p_input_thread )
1651 return;
1653 int i_type = var_Type( p_input_thread, "next-chapter" );
1654 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1655 "next-chapter":"next-title" );
1657 vlc_object_release( p_input_thread );
1660 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1662 input_thread_t *p_input_thread;
1664 p_input_thread = libvlc_get_input_thread ( p_mi );
1665 if( !p_input_thread )
1666 return;
1668 int i_type = var_Type( p_input_thread, "next-chapter" );
1669 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1670 "prev-chapter":"prev-title" );
1672 vlc_object_release( p_input_thread );
1675 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1677 libvlc_media_t *media = libvlc_media_player_get_media( p_mi );
1678 if( media == NULL )
1679 return 0.f;
1681 input_item_t *item = media->p_input_item;
1682 float fps = 0.f;
1684 vlc_mutex_lock( &item->lock );
1685 for( int i = 0; i < item->i_es; i++ )
1687 const es_format_t *fmt = item->es[i];
1689 if( fmt->i_cat == VIDEO_ES && fmt->video.i_frame_rate_base > 0 )
1690 fps = (float)fmt->video.i_frame_rate
1691 / (float)fmt->video.i_frame_rate_base;
1693 vlc_mutex_unlock( &item->lock );
1694 libvlc_media_release( media );
1696 return fps;
1699 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1701 input_thread_t *p_input_thread =
1702 libvlc_get_input_thread ( p_mi );
1703 if ( !p_input_thread )
1704 return false;
1706 int state = var_GetInteger( p_input_thread, "state" );
1707 vlc_object_release( p_input_thread );
1709 return state != END_S && state != ERROR_S;
1712 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1714 var_SetFloat (p_mi, "rate", rate);
1716 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1717 if( !p_input_thread )
1718 return 0;
1719 var_SetFloat( p_input_thread, "rate", rate );
1720 vlc_object_release( p_input_thread );
1721 return 0;
1724 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1726 return var_GetFloat (p_mi, "rate");
1729 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1731 lock(p_mi);
1732 libvlc_state_t state = p_mi->state;
1733 unlock(p_mi);
1734 return state;
1737 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1739 input_thread_t *p_input_thread;
1740 bool b_seekable;
1742 p_input_thread = libvlc_get_input_thread ( p_mi );
1743 if ( !p_input_thread )
1744 return false;
1745 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1746 vlc_object_release( p_input_thread );
1748 return b_seekable;
1751 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1752 unsigned navigate )
1754 static const int map[] =
1756 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1757 INPUT_NAV_LEFT, INPUT_NAV_RIGHT, INPUT_NAV_POPUP,
1760 if( navigate >= sizeof(map) / sizeof(map[0]) )
1761 return;
1763 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1764 if ( p_input == NULL )
1765 return;
1767 input_Control( p_input, map[navigate], NULL );
1768 vlc_object_release( p_input );
1771 /* internal function, used by audio, video */
1772 libvlc_track_description_t *
1773 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1774 const char *psz_variable )
1776 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1777 libvlc_track_description_t *p_track_description = NULL,
1778 *p_actual, *p_previous;
1780 if( !p_input )
1781 return NULL;
1783 vlc_value_t val_list, text_list;
1784 int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES, &val_list, &text_list );
1785 if( i_ret != VLC_SUCCESS )
1786 return NULL;
1788 /* no tracks */
1789 if( val_list.p_list->i_count <= 0 )
1790 goto end;
1792 p_track_description = malloc( sizeof *p_track_description );
1793 if ( !p_track_description )
1795 libvlc_printerr( "Not enough memory" );
1796 goto end;
1798 p_actual = p_track_description;
1799 p_previous = NULL;
1800 for( int i = 0; i < val_list.p_list->i_count; i++ )
1802 if( !p_actual )
1804 p_actual = malloc( sizeof *p_actual );
1805 if ( !p_actual )
1807 libvlc_track_description_list_release( p_track_description );
1808 libvlc_printerr( "Not enough memory" );
1810 p_track_description = NULL;
1811 goto end;
1814 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1815 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1816 p_actual->p_next = NULL;
1817 if( p_previous )
1818 p_previous->p_next = p_actual;
1819 p_previous = p_actual;
1820 p_actual = NULL;
1823 end:
1824 var_FreeList( &val_list, &text_list );
1825 vlc_object_release( p_input );
1827 return p_track_description;
1830 // Deprecated alias for libvlc_track_description_list_release
1831 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1833 libvlc_track_description_list_release( p_td );
1836 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1838 libvlc_track_description_t *p_actual, *p_before;
1839 p_actual = p_td;
1841 while ( p_actual )
1843 free( p_actual->psz_name );
1844 p_before = p_actual;
1845 p_actual = p_before->p_next;
1846 free( p_before );
1850 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1852 input_thread_t *p_input_thread;
1853 bool b_can_pause;
1855 p_input_thread = libvlc_get_input_thread ( p_mi );
1856 if ( !p_input_thread )
1857 return false;
1858 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1859 vlc_object_release( p_input_thread );
1861 return b_can_pause;
1864 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1866 input_thread_t *p_input_thread;
1867 bool b_program_scrambled;
1869 p_input_thread = libvlc_get_input_thread ( p_mi );
1870 if ( !p_input_thread )
1871 return false;
1872 b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1873 vlc_object_release( p_input_thread );
1875 return b_program_scrambled;
1878 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1880 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1881 if( p_input_thread != NULL )
1883 var_TriggerCallback( p_input_thread, "frame-next" );
1884 vlc_object_release( p_input_thread );
1889 * Private lookup table to get subpicture alignment flag values corresponding
1890 * to a libvlc_position_t enumerated value.
1892 static const unsigned char position_subpicture_alignment[] = {
1893 [libvlc_position_center] = 0,
1894 [libvlc_position_left] = SUBPICTURE_ALIGN_LEFT,
1895 [libvlc_position_right] = SUBPICTURE_ALIGN_RIGHT,
1896 [libvlc_position_top] = SUBPICTURE_ALIGN_TOP,
1897 [libvlc_position_top_left] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1898 [libvlc_position_top_right] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1899 [libvlc_position_bottom] = SUBPICTURE_ALIGN_BOTTOM,
1900 [libvlc_position_bottom_left] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1901 [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1904 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1906 assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1908 if ( position != libvlc_position_disable )
1910 var_SetBool( p_mi, "video-title-show", true );
1911 var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1912 var_SetInteger( p_mi, "video-title-timeout", timeout );
1914 else
1916 var_SetBool( p_mi, "video-title-show", false );
1920 int libvlc_media_player_add_slave( libvlc_media_player_t *p_mi,
1921 libvlc_media_slave_type_t i_type,
1922 const char *psz_uri, bool b_select )
1924 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1926 if( p_input_thread == NULL )
1928 libvlc_media_t *p_media = libvlc_media_player_get_media( p_mi );
1929 if( p_media == NULL )
1930 return -1;
1932 int i_ret = libvlc_media_slaves_add( p_media, i_type, 4, psz_uri );
1933 libvlc_media_release( p_media );
1934 return i_ret;
1936 else
1938 int i_ret = input_AddSlave( p_input_thread, (enum slave_type) i_type,
1939 psz_uri, b_select, false, false );
1940 vlc_object_release( p_input_thread );
1942 return i_ret == VLC_SUCCESS ? 0 : -1;
1947 * Maximum size of a formatted equalizer amplification band frequency value.
1949 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1951 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1952 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1953 * terminator).
1955 #define EQZ_BAND_VALUE_SIZE 12
1957 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1959 char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1961 if( p_equalizer != NULL )
1963 for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1965 c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
1966 p_equalizer->f_amp[i] );
1967 if( unlikely(c >= sizeof(bands)) )
1968 return -1;
1971 var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
1972 var_SetString( p_mi, "equalizer-bands", bands );
1974 var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1976 audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
1977 if( p_aout != NULL )
1979 if( p_equalizer != NULL )
1981 var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
1982 var_SetString( p_aout, "equalizer-bands", bands );
1985 var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
1986 vlc_object_release( p_aout );
1989 return 0;
1992 static const char roles[][16] =
1994 [libvlc_role_Music] = "music",
1995 [libvlc_role_Video] = "video",
1996 [libvlc_role_Communication] = "communication",
1997 [libvlc_role_Game] = "game",
1998 [libvlc_role_Notification] = "notification",
1999 [libvlc_role_Animation] = "animation",
2000 [libvlc_role_Production] = "production",
2001 [libvlc_role_Accessibility] = "accessibility",
2002 [libvlc_role_Test] = "test",
2005 int libvlc_media_player_set_role(libvlc_media_player_t *mp, unsigned role)
2007 if (role >= ARRAY_SIZE(roles)
2008 || var_SetString(mp, "role", roles[role]) != VLC_SUCCESS)
2009 return -1;
2010 return 0;
2013 int libvlc_media_player_get_role(libvlc_media_player_t *mp)
2015 int ret = -1;
2016 char *str = var_GetString(mp, "role");
2017 if (str == NULL)
2018 return 0;
2020 for (size_t i = 0; i < ARRAY_SIZE(roles); i++)
2021 if (!strcmp(roles[i], str))
2023 ret = i;
2024 break;
2027 free(str);
2028 return ret;