packetizer: mpeg4video: update debug code
[vlc.git] / lib / media_player.c
blob30374d4ee92c2aca005078aa3c94b408b27a3786
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 var_SetInteger( p_input_thread, "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, input_LegacyEvents, NULL,
988 p_mi->p_md->p_input_item, NULL,
989 p_mi->input.p_resource,
990 p_mi->input.p_renderer );
991 unlock(p_mi);
992 if( !p_input_thread )
994 unlock_input(p_mi);
995 media_detach_preparsed_event( p_mi->p_md );
996 libvlc_printerr( "Not enough memory" );
997 return -1;
1000 input_LegacyVarInit( p_input_thread );
1001 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
1002 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
1003 var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
1004 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
1005 add_es_callbacks( p_input_thread, p_mi );
1007 if( input_Start( p_input_thread ) )
1009 unlock_input(p_mi);
1010 del_es_callbacks( p_input_thread, p_mi );
1011 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
1012 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
1013 var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
1014 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
1015 input_Close( p_input_thread );
1016 media_detach_preparsed_event( p_mi->p_md );
1017 libvlc_printerr( "Input initialization failure" );
1018 return -1;
1020 p_mi->input.p_thread = p_input_thread;
1021 unlock_input(p_mi);
1022 return 0;
1025 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
1027 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
1028 if( !p_input_thread )
1029 return;
1031 if( paused )
1033 if( libvlc_media_player_can_pause( p_mi ) )
1034 var_SetInteger( p_input_thread, "state", PAUSE_S );
1035 else
1036 input_Stop( p_input_thread );
1038 else
1040 var_SetInteger( p_input_thread, "state", PLAYING_S );
1043 vlc_object_release( p_input_thread );
1046 /**************************************************************************
1047 * Toggle pause.
1048 **************************************************************************/
1049 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
1051 libvlc_media_player_set_pause( p_mi, libvlc_media_player_is_playing( p_mi ) );
1054 /**************************************************************************
1055 * Tells whether the media player is currently playing.
1056 **************************************************************************/
1057 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
1059 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
1060 return libvlc_Playing == state;
1063 /**************************************************************************
1064 * Stop playing.
1065 **************************************************************************/
1066 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
1068 lock_input(p_mi);
1069 release_input_thread( p_mi ); /* This will stop the input thread */
1071 /* Force to go to stopped state, in case we were in Ended, or Error
1072 * state. */
1073 if( p_mi->state != libvlc_Stopped )
1075 set_state( p_mi, libvlc_Stopped, false );
1077 /* Construct and send the event */
1078 libvlc_event_t event;
1079 event.type = libvlc_MediaPlayerStopped;
1080 libvlc_event_send( &p_mi->event_manager, &event );
1083 input_resource_Terminate( p_mi->input.p_resource );
1084 unlock_input(p_mi);
1087 int libvlc_media_player_set_renderer( libvlc_media_player_t *p_mi,
1088 libvlc_renderer_item_t *p_litem )
1090 vlc_renderer_item_t *p_item =
1091 p_litem ? libvlc_renderer_item_to_vlc( p_litem ) : NULL;
1093 lock_input( p_mi );
1094 input_thread_t *p_input_thread = p_mi->input.p_thread;
1095 if( p_input_thread )
1096 input_Control( p_input_thread, INPUT_SET_RENDERER, p_item );
1098 if( p_mi->input.p_renderer )
1099 vlc_renderer_item_release( p_mi->input.p_renderer );
1100 p_mi->input.p_renderer = p_item ? vlc_renderer_item_hold( p_item ) : NULL;
1101 unlock_input( p_mi );
1103 return 0;
1106 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
1107 void *(*lock_cb) (void *, void **),
1108 void (*unlock_cb) (void *, void *, void *const *),
1109 void (*display_cb) (void *, void *),
1110 void *opaque )
1112 var_SetAddress( mp, "vmem-lock", lock_cb );
1113 var_SetAddress( mp, "vmem-unlock", unlock_cb );
1114 var_SetAddress( mp, "vmem-display", display_cb );
1115 var_SetAddress( mp, "vmem-data", opaque );
1116 var_SetString( mp, "avcodec-hw", "none" );
1117 var_SetString( mp, "vout", "vmem" );
1118 var_SetString( mp, "window", "dummy" );
1121 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
1122 libvlc_video_format_cb setup,
1123 libvlc_video_cleanup_cb cleanup )
1125 var_SetAddress( mp, "vmem-setup", setup );
1126 var_SetAddress( mp, "vmem-cleanup", cleanup );
1129 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
1130 unsigned width, unsigned height, unsigned pitch )
1132 var_SetString( mp, "vmem-chroma", chroma );
1133 var_SetInteger( mp, "vmem-width", width );
1134 var_SetInteger( mp, "vmem-height", height );
1135 var_SetInteger( mp, "vmem-pitch", pitch );
1138 /**************************************************************************
1139 * set_nsobject
1140 **************************************************************************/
1141 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1142 void * drawable )
1144 assert (p_mi != NULL);
1145 #ifdef __APPLE__
1146 var_SetString (p_mi, "avcodec-hw", "");
1147 var_SetString (p_mi, "vout", "");
1148 var_SetString (p_mi, "window", "");
1149 var_SetAddress (p_mi, "drawable-nsobject", drawable);
1150 #else
1151 (void)drawable;
1152 libvlc_printerr ("can't set nsobject: APPLE build required");
1153 assert(false);
1154 var_SetString (p_mi, "vout", "none");
1155 var_SetString (p_mi, "window", "none");
1156 #endif
1159 /**************************************************************************
1160 * get_nsobject
1161 **************************************************************************/
1162 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1164 assert (p_mi != NULL);
1165 #ifdef __APPLE__
1166 return var_GetAddress (p_mi, "drawable-nsobject");
1167 #else
1168 (void) p_mi;
1169 return NULL;
1170 #endif
1173 /**************************************************************************
1174 * set_xwindow
1175 **************************************************************************/
1176 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1177 uint32_t drawable )
1179 assert (p_mi != NULL);
1181 var_SetString (p_mi, "avcodec-hw", "");
1182 var_SetString (p_mi, "vout", "");
1183 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1184 var_SetInteger (p_mi, "drawable-xid", drawable);
1187 /**************************************************************************
1188 * get_xwindow
1189 **************************************************************************/
1190 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1192 return var_GetInteger (p_mi, "drawable-xid");
1195 /**************************************************************************
1196 * set_hwnd
1197 **************************************************************************/
1198 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1199 void *drawable )
1201 assert (p_mi != NULL);
1202 #if defined (_WIN32) || defined (__OS2__)
1203 var_SetString (p_mi, "avcodec-hw", "");
1204 var_SetString (p_mi, "vout", "");
1205 var_SetString (p_mi, "window",
1206 (drawable != NULL) ? "embed-hwnd,any" : "");
1207 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1208 #else
1209 (void) drawable;
1210 libvlc_printerr ("can't set nsobject: WIN32 build required");
1211 assert(false);
1212 var_SetString (p_mi, "vout", "none");
1213 var_SetString (p_mi, "window", "none");
1214 #endif
1217 /**************************************************************************
1218 * get_hwnd
1219 **************************************************************************/
1220 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1222 assert (p_mi != NULL);
1223 #if defined (_WIN32) || defined (__OS2__)
1224 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1225 #else
1226 (void) p_mi;
1227 return NULL;
1228 #endif
1231 /**************************************************************************
1232 * set_android_context
1233 **************************************************************************/
1234 void libvlc_media_player_set_android_context( libvlc_media_player_t *p_mi,
1235 void *p_awindow_handler )
1237 assert (p_mi != NULL);
1238 #ifdef __ANDROID__
1239 var_SetAddress (p_mi, "drawable-androidwindow", p_awindow_handler);
1240 #else
1241 (void) p_awindow_handler;
1242 libvlc_printerr ("can't set android context: ANDROID build required");
1243 assert(false);
1244 var_SetString (p_mi, "vout", "none");
1245 var_SetString (p_mi, "window", "none");
1246 #endif
1249 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1250 libvlc_audio_play_cb play_cb,
1251 libvlc_audio_pause_cb pause_cb,
1252 libvlc_audio_resume_cb resume_cb,
1253 libvlc_audio_flush_cb flush_cb,
1254 libvlc_audio_drain_cb drain_cb,
1255 void *opaque )
1257 var_SetAddress( mp, "amem-play", play_cb );
1258 var_SetAddress( mp, "amem-pause", pause_cb );
1259 var_SetAddress( mp, "amem-resume", resume_cb );
1260 var_SetAddress( mp, "amem-flush", flush_cb );
1261 var_SetAddress( mp, "amem-drain", drain_cb );
1262 var_SetAddress( mp, "amem-data", opaque );
1263 var_SetString( mp, "aout", "amem,none" );
1265 input_resource_ResetAout(mp->input.p_resource);
1268 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1269 libvlc_audio_set_volume_cb cb )
1271 var_SetAddress( mp, "amem-set-volume", cb );
1273 input_resource_ResetAout(mp->input.p_resource);
1276 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1277 libvlc_audio_setup_cb setup,
1278 libvlc_audio_cleanup_cb cleanup )
1280 var_SetAddress( mp, "amem-setup", setup );
1281 var_SetAddress( mp, "amem-cleanup", cleanup );
1283 input_resource_ResetAout(mp->input.p_resource);
1286 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1287 unsigned rate, unsigned channels )
1289 var_SetString( mp, "amem-format", format );
1290 var_SetInteger( mp, "amem-rate", rate );
1291 var_SetInteger( mp, "amem-channels", channels );
1293 input_resource_ResetAout(mp->input.p_resource);
1297 /**************************************************************************
1298 * Getters for stream information
1299 **************************************************************************/
1300 libvlc_time_t libvlc_media_player_get_length(
1301 libvlc_media_player_t *p_mi )
1303 input_thread_t *p_input_thread;
1304 libvlc_time_t i_time;
1306 p_input_thread = libvlc_get_input_thread ( p_mi );
1307 if( !p_input_thread )
1308 return -1;
1310 i_time = from_mtime(var_GetInteger( p_input_thread, "length" ));
1311 vlc_object_release( p_input_thread );
1313 return i_time;
1316 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1318 input_thread_t *p_input_thread;
1319 libvlc_time_t i_time;
1321 p_input_thread = libvlc_get_input_thread ( p_mi );
1322 if( !p_input_thread )
1323 return -1;
1325 i_time = from_mtime(var_GetInteger( p_input_thread , "time" ));
1326 vlc_object_release( p_input_thread );
1327 return i_time;
1330 int libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1331 libvlc_time_t i_time, bool b_fast )
1333 input_thread_t *p_input_thread;
1335 p_input_thread = libvlc_get_input_thread ( p_mi );
1336 if( !p_input_thread )
1337 return -1;
1339 input_SetTime( p_input_thread, to_mtime(i_time), b_fast );
1340 vlc_object_release( p_input_thread );
1341 return 0;
1344 int libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1345 float position, bool b_fast )
1347 input_thread_t *p_input_thread;
1349 p_input_thread = libvlc_get_input_thread ( p_mi );
1350 if( !p_input_thread )
1351 return -1;
1353 input_SetPosition( p_input_thread, position, b_fast );
1354 vlc_object_release( p_input_thread );
1355 return 0;
1358 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1360 input_thread_t *p_input_thread;
1361 float f_position;
1363 p_input_thread = libvlc_get_input_thread ( p_mi );
1364 if( !p_input_thread )
1365 return -1.0;
1367 f_position = var_GetFloat( p_input_thread, "position" );
1368 vlc_object_release( p_input_thread );
1370 return f_position;
1373 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1374 int chapter )
1376 input_thread_t *p_input_thread;
1378 p_input_thread = libvlc_get_input_thread ( p_mi );
1379 if( !p_input_thread )
1380 return;
1382 var_SetInteger( p_input_thread, "chapter", chapter );
1383 vlc_object_release( p_input_thread );
1386 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1388 input_thread_t *p_input_thread;
1389 int i_chapter;
1391 p_input_thread = libvlc_get_input_thread ( p_mi );
1392 if( !p_input_thread )
1393 return -1;
1395 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1396 vlc_object_release( p_input_thread );
1398 return i_chapter;
1401 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1403 input_thread_t *p_input_thread;
1404 size_t val;
1406 p_input_thread = libvlc_get_input_thread ( p_mi );
1407 if( !p_input_thread )
1408 return -1;
1410 int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val );
1411 vlc_object_release( p_input_thread );
1413 return i_ret == VLC_SUCCESS ? (ssize_t)val : -1;
1416 int libvlc_media_player_get_chapter_count_for_title(
1417 libvlc_media_player_t *p_mi,
1418 int i_title )
1420 size_t val;
1422 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1423 if( !p_input_thread )
1424 return -1;
1426 char psz_name[sizeof ("title ") + 3 * sizeof (int)];
1427 sprintf( psz_name, "title %2u", i_title );
1429 int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val );
1430 vlc_object_release( p_input_thread );
1432 return i_ret == VLC_SUCCESS ? (ssize_t)val : -1;
1435 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1436 int i_title )
1438 input_thread_t *p_input_thread;
1440 p_input_thread = libvlc_get_input_thread ( p_mi );
1441 if( !p_input_thread )
1442 return;
1444 var_SetInteger( p_input_thread, "title", i_title );
1445 vlc_object_release( p_input_thread );
1447 //send event
1448 libvlc_event_t event;
1449 event.type = libvlc_MediaPlayerTitleChanged;
1450 event.u.media_player_title_changed.new_title = i_title;
1451 libvlc_event_send( &p_mi->event_manager, &event );
1454 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1456 input_thread_t *p_input_thread;
1457 int i_title;
1459 p_input_thread = libvlc_get_input_thread ( p_mi );
1460 if( !p_input_thread )
1461 return -1;
1463 i_title = var_GetInteger( p_input_thread, "title" );
1464 vlc_object_release( p_input_thread );
1466 return i_title;
1469 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1471 input_thread_t *p_input_thread;
1472 size_t val;
1474 p_input_thread = libvlc_get_input_thread ( p_mi );
1475 if( !p_input_thread )
1476 return -1;
1478 int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val );
1479 vlc_object_release( p_input_thread );
1481 return i_ret == VLC_SUCCESS ? (ssize_t)val : -1;
1484 int libvlc_media_player_get_full_title_descriptions( libvlc_media_player_t *p_mi,
1485 libvlc_title_description_t *** pp_titles )
1487 assert( p_mi );
1489 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1491 if( !p_input_thread )
1492 return -1;
1494 input_title_t **p_input_title;
1495 int count;
1497 /* fetch data */
1498 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO,
1499 &p_input_title, &count );
1500 vlc_object_release( p_input_thread );
1501 if( ret != VLC_SUCCESS )
1502 return -1;
1504 libvlc_title_description_t **titles = vlc_alloc( count, sizeof (*titles) );
1505 if( count > 0 && unlikely(titles == NULL) )
1506 return -1;
1508 /* fill array */
1509 for( int i = 0; i < count; i++)
1511 libvlc_title_description_t *title = malloc( sizeof (*title) );
1512 if( unlikely(title == NULL) )
1514 libvlc_title_descriptions_release( titles, i );
1515 return -1;
1517 titles[i] = title;
1519 /* we want to return milliseconds to match the rest of the API */
1520 title->i_duration = MS_FROM_VLC_TICK(p_input_title[i]->i_length);
1521 title->i_flags = p_input_title[i]->i_flags;
1522 if( p_input_title[i]->psz_name )
1523 title->psz_name = strdup( p_input_title[i]->psz_name );
1524 else
1525 title->psz_name = NULL;
1526 vlc_input_title_Delete( p_input_title[i] );
1528 free( p_input_title );
1530 *pp_titles = titles;
1531 return count;
1534 void libvlc_title_descriptions_release( libvlc_title_description_t **p_titles,
1535 unsigned i_count )
1537 for (unsigned i = 0; i < i_count; i++ )
1539 if ( !p_titles[i] )
1540 continue;
1542 free( p_titles[i]->psz_name );
1543 free( p_titles[i] );
1545 free( p_titles );
1548 int libvlc_media_player_get_full_chapter_descriptions( libvlc_media_player_t *p_mi,
1549 int i_chapters_of_title,
1550 libvlc_chapter_description_t *** pp_chapters )
1552 assert( p_mi );
1554 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1556 if( !p_input_thread )
1557 return -1;
1559 seekpoint_t **p_seekpoint = NULL;
1560 input_title_t **pp_title, *p_title = NULL;
1561 int i_title_count = 0, ci_chapter_count = 0;
1562 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO, &pp_title,
1563 &i_title_count );
1564 vlc_object_release( p_input_thread );
1566 if( ret != VLC_SUCCESS || i_chapters_of_title >= i_title_count )
1567 goto error;
1569 p_title = pp_title[i_chapters_of_title];
1570 int64_t i_title_duration = MS_FROM_VLC_TICK(p_title->i_length);
1571 p_seekpoint = p_title->seekpoint;
1572 ci_chapter_count = p_title->i_seekpoint;
1574 if( ci_chapter_count == 0 || p_seekpoint == NULL)
1575 goto error;
1577 *pp_chapters = calloc( ci_chapter_count, sizeof(**pp_chapters) );
1578 if( !*pp_chapters )
1580 goto error;
1583 /* fill array */
1584 for( int i = 0; i < ci_chapter_count; ++i )
1586 libvlc_chapter_description_t *p_chapter = malloc( sizeof(*p_chapter) );
1587 if( unlikely(p_chapter == NULL) )
1589 goto error;
1591 (*pp_chapters)[i] = p_chapter;
1593 p_chapter->i_time_offset = p_seekpoint[i]->i_time_offset / 1000;
1595 if( i < ci_chapter_count - 1 )
1597 p_chapter->i_duration = p_seekpoint[i + 1]->i_time_offset / 1000 -
1598 p_chapter->i_time_offset;
1600 else
1602 if ( i_title_duration )
1603 p_chapter->i_duration = i_title_duration - p_chapter->i_time_offset;
1604 else
1605 p_chapter->i_duration = 0;
1608 if( p_seekpoint[i]->psz_name )
1610 p_chapter->psz_name = strdup( p_seekpoint[i]->psz_name );
1612 else
1614 p_chapter->psz_name = NULL;
1616 vlc_seekpoint_Delete( p_seekpoint[i] );
1617 p_seekpoint[i] = NULL;
1620 free( p_seekpoint );
1621 return ci_chapter_count;
1623 error:
1624 if( *pp_chapters )
1625 libvlc_chapter_descriptions_release( *pp_chapters, ci_chapter_count );
1626 for( int i = 0; i < i_title_count; i++ )
1627 vlc_input_title_Delete( pp_title[i] );
1628 free( pp_title ) ;
1629 return -1;
1632 void libvlc_chapter_descriptions_release( libvlc_chapter_description_t **p_chapters,
1633 unsigned i_count )
1635 for (unsigned i = 0; i < i_count; i++ )
1637 if ( !p_chapters[i] )
1638 continue;
1640 free( p_chapters[i]->psz_name );
1641 free( p_chapters[i] );
1643 free( p_chapters );
1646 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1648 input_thread_t *p_input_thread;
1650 p_input_thread = libvlc_get_input_thread ( p_mi );
1651 if( !p_input_thread )
1652 return;
1654 int i_type = var_Type( p_input_thread, "next-chapter" );
1655 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1656 "next-chapter":"next-title" );
1658 vlc_object_release( p_input_thread );
1661 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1663 input_thread_t *p_input_thread;
1665 p_input_thread = libvlc_get_input_thread ( p_mi );
1666 if( !p_input_thread )
1667 return;
1669 int i_type = var_Type( p_input_thread, "next-chapter" );
1670 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1671 "prev-chapter":"prev-title" );
1673 vlc_object_release( p_input_thread );
1676 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1678 input_thread_t *p_input_thread =
1679 libvlc_get_input_thread ( p_mi );
1680 if ( !p_input_thread )
1681 return false;
1683 int state = var_GetInteger( p_input_thread, "state" );
1684 vlc_object_release( p_input_thread );
1686 return state != END_S && state != ERROR_S;
1689 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1691 var_SetFloat (p_mi, "rate", rate);
1693 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1694 if( !p_input_thread )
1695 return 0;
1696 var_SetFloat( p_input_thread, "rate", rate );
1697 vlc_object_release( p_input_thread );
1698 return 0;
1701 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1703 return var_GetFloat (p_mi, "rate");
1706 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1708 lock(p_mi);
1709 libvlc_state_t state = p_mi->state;
1710 unlock(p_mi);
1711 return state;
1714 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1716 input_thread_t *p_input_thread;
1717 bool b_seekable;
1719 p_input_thread = libvlc_get_input_thread ( p_mi );
1720 if ( !p_input_thread )
1721 return false;
1722 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1723 vlc_object_release( p_input_thread );
1725 return b_seekable;
1728 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1729 unsigned navigate )
1731 static const int map[] =
1733 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1734 INPUT_NAV_LEFT, INPUT_NAV_RIGHT, INPUT_NAV_POPUP,
1737 if( navigate >= sizeof(map) / sizeof(map[0]) )
1738 return;
1740 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1741 if ( p_input == NULL )
1742 return;
1744 input_Control( p_input, map[navigate], NULL );
1745 vlc_object_release( p_input );
1748 /* internal function, used by audio, video */
1749 libvlc_track_description_t *
1750 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1751 const char *psz_variable )
1753 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1754 libvlc_track_description_t *ret, **pp = &ret;
1756 if( !p_input )
1757 return NULL;
1759 vlc_value_t *val_list;
1760 char **text_list;
1761 size_t count;
1763 int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES,
1764 &count, &val_list, &text_list );
1765 if( i_ret != VLC_SUCCESS )
1766 return NULL;
1768 for (size_t i = 0; i < count; i++)
1770 libvlc_track_description_t *tr = malloc(sizeof (*tr));
1771 if (unlikely(tr == NULL))
1773 libvlc_printerr("Not enough memory");
1774 continue;
1777 *pp = tr;
1778 tr->i_id = val_list[i].i_int;
1779 tr->psz_name = text_list[i];
1780 pp = &tr->p_next;
1783 *pp = NULL;
1784 free(val_list);
1785 free(text_list);
1786 vlc_object_release( p_input );
1788 return ret;
1791 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1793 libvlc_track_description_t *p_actual, *p_before;
1794 p_actual = p_td;
1796 while ( p_actual )
1798 free( p_actual->psz_name );
1799 p_before = p_actual;
1800 p_actual = p_before->p_next;
1801 free( p_before );
1805 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1807 input_thread_t *p_input_thread;
1808 bool b_can_pause;
1810 p_input_thread = libvlc_get_input_thread ( p_mi );
1811 if ( !p_input_thread )
1812 return false;
1813 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1814 vlc_object_release( p_input_thread );
1816 return b_can_pause;
1819 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1821 input_thread_t *p_input_thread;
1822 bool b_program_scrambled;
1824 p_input_thread = libvlc_get_input_thread ( p_mi );
1825 if ( !p_input_thread )
1826 return false;
1827 b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1828 vlc_object_release( p_input_thread );
1830 return b_program_scrambled;
1833 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1835 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1836 if( p_input_thread != NULL )
1838 var_TriggerCallback( p_input_thread, "frame-next" );
1839 vlc_object_release( p_input_thread );
1844 * Private lookup table to get subpicture alignment flag values corresponding
1845 * to a libvlc_position_t enumerated value.
1847 static const unsigned char position_subpicture_alignment[] = {
1848 [libvlc_position_center] = 0,
1849 [libvlc_position_left] = SUBPICTURE_ALIGN_LEFT,
1850 [libvlc_position_right] = SUBPICTURE_ALIGN_RIGHT,
1851 [libvlc_position_top] = SUBPICTURE_ALIGN_TOP,
1852 [libvlc_position_top_left] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1853 [libvlc_position_top_right] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1854 [libvlc_position_bottom] = SUBPICTURE_ALIGN_BOTTOM,
1855 [libvlc_position_bottom_left] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1856 [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1859 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1861 assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1863 if ( position != libvlc_position_disable )
1865 var_SetBool( p_mi, "video-title-show", true );
1866 var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1867 var_SetInteger( p_mi, "video-title-timeout", timeout );
1869 else
1871 var_SetBool( p_mi, "video-title-show", false );
1875 int libvlc_media_player_add_slave( libvlc_media_player_t *p_mi,
1876 libvlc_media_slave_type_t i_type,
1877 const char *psz_uri, bool b_select )
1879 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1881 if( p_input_thread == NULL )
1883 libvlc_media_t *p_media = libvlc_media_player_get_media( p_mi );
1884 if( p_media == NULL )
1885 return -1;
1887 int i_ret = libvlc_media_slaves_add( p_media, i_type, 4, psz_uri );
1888 libvlc_media_release( p_media );
1889 return i_ret;
1891 else
1893 int i_ret = input_AddSlave( p_input_thread, (enum slave_type) i_type,
1894 psz_uri, b_select, false, false );
1895 vlc_object_release( p_input_thread );
1897 return i_ret == VLC_SUCCESS ? 0 : -1;
1902 * Maximum size of a formatted equalizer amplification band frequency value.
1904 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1906 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1907 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1908 * terminator).
1910 #define EQZ_BAND_VALUE_SIZE 12
1912 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1914 char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1916 if( p_equalizer != NULL )
1918 for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1920 c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
1921 p_equalizer->f_amp[i] );
1922 if( unlikely(c >= sizeof(bands)) )
1923 return -1;
1926 var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
1927 var_SetString( p_mi, "equalizer-bands", bands );
1929 var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1931 audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
1932 if( p_aout != NULL )
1934 if( p_equalizer != NULL )
1936 var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
1937 var_SetString( p_aout, "equalizer-bands", bands );
1940 var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
1941 vlc_object_release( p_aout );
1944 return 0;
1947 static const char roles[][16] =
1949 [libvlc_role_Music] = "music",
1950 [libvlc_role_Video] = "video",
1951 [libvlc_role_Communication] = "communication",
1952 [libvlc_role_Game] = "game",
1953 [libvlc_role_Notification] = "notification",
1954 [libvlc_role_Animation] = "animation",
1955 [libvlc_role_Production] = "production",
1956 [libvlc_role_Accessibility] = "accessibility",
1957 [libvlc_role_Test] = "test",
1960 int libvlc_media_player_set_role(libvlc_media_player_t *mp, unsigned role)
1962 if (role >= ARRAY_SIZE(roles)
1963 || var_SetString(mp, "role", roles[role]) != VLC_SUCCESS)
1964 return -1;
1965 return 0;
1968 int libvlc_media_player_get_role(libvlc_media_player_t *mp)
1970 int ret = -1;
1971 char *str = var_GetString(mp, "role");
1972 if (str == NULL)
1973 return 0;
1975 for (size_t i = 0; i < ARRAY_SIZE(roles); i++)
1976 if (!strcmp(roles[i], str))
1978 ret = i;
1979 break;
1982 free(str);
1983 return ret;