demux: heif: refactor extents reading
[vlc.git] / lib / media_player.c
blob9cb826934a1636f7aeedfd4a2dfa4b60fbe883f2
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_picture.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_events.h>
35 #include <vlc_demux.h>
36 #include <vlc_input.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_actions.h>
40 #include <vlc_http.h>
42 #include "libvlc_internal.h"
43 #include "media_internal.h" // libvlc_media_set_state()
44 #include "media_player_internal.h"
45 #include "renderer_discoverer_internal.h"
47 #define ES_INIT (-2) /* -1 is reserved for ES deselect */
49 static int
50 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
51 vlc_value_t oldval, vlc_value_t newval,
52 void * p_userdata );
53 static int
54 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
55 vlc_value_t oldval, vlc_value_t newval,
56 void * p_userdata );
57 static int
58 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
59 vlc_value_t oldval, vlc_value_t newval,
60 void * p_userdata );
61 static int
62 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
63 vlc_value_t oldval, vlc_value_t newval,
64 void * p_userdata );
66 static int
67 input_es_changed( vlc_object_t * p_this, char const * psz_cmd,
68 int action, vlc_value_t *p_val,
69 void *p_userdata);
71 static int
72 corks_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
73 vlc_value_t cur, void *opaque);
75 static int
76 mute_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
77 vlc_value_t cur, void *opaque);
79 static int
80 volume_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
81 vlc_value_t cur, void *opaque);
83 static void
84 add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
86 static void
87 del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
89 static int
90 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
91 vlc_value_t oldval, vlc_value_t newval, void *p_data );
93 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
96 * Shortcuts
100 * The input lock protects the input and input resource pointer.
101 * It MUST NOT be used from callbacks.
103 * The object lock protects the reset, namely the media and the player state.
104 * It can, and usually needs to be taken from callbacks.
105 * The object lock can be acquired under the input lock... and consequently
106 * the opposite order is STRICTLY PROHIBITED.
108 static inline void lock(libvlc_media_player_t *mp)
110 vlc_mutex_lock(&mp->object_lock);
113 static inline void unlock(libvlc_media_player_t *mp)
115 vlc_mutex_unlock(&mp->object_lock);
118 static inline void lock_input(libvlc_media_player_t *mp)
120 vlc_mutex_lock(&mp->input.lock);
123 static inline void unlock_input(libvlc_media_player_t *mp)
125 vlc_mutex_unlock(&mp->input.lock);
128 static void input_item_preparsed_changed( const vlc_event_t *p_event,
129 void * user_data )
131 libvlc_media_t *p_md = user_data;
132 if( p_event->u.input_item_preparsed_changed.new_status & ITEM_PREPARSED )
134 /* Send the event */
135 libvlc_event_t event;
136 event.type = libvlc_MediaParsedChanged;
137 event.u.media_parsed_changed.new_status = libvlc_media_parsed_status_done;
138 libvlc_event_send( &p_md->event_manager, &event );
142 static void media_attach_preparsed_event( libvlc_media_t *p_md )
144 vlc_event_attach( &p_md->p_input_item->event_manager,
145 vlc_InputItemPreparsedChanged,
146 input_item_preparsed_changed, p_md );
149 static void media_detach_preparsed_event( libvlc_media_t *p_md )
151 vlc_event_detach( &p_md->p_input_item->event_manager,
152 vlc_InputItemPreparsedChanged,
153 input_item_preparsed_changed,
154 p_md );
158 * Release the associated input thread.
160 * Object lock is NOT held.
161 * Input lock is held or instance is being destroyed.
163 static void release_input_thread( libvlc_media_player_t *p_mi )
165 assert( p_mi );
167 input_thread_t *p_input_thread = p_mi->input.p_thread;
168 if( !p_input_thread )
169 return;
170 p_mi->input.p_thread = NULL;
172 media_detach_preparsed_event( p_mi->p_md );
174 var_DelCallback( p_input_thread, "can-seek",
175 input_seekable_changed, p_mi );
176 var_DelCallback( p_input_thread, "can-pause",
177 input_pausable_changed, p_mi );
178 var_DelCallback( p_input_thread, "program-scrambled",
179 input_scrambled_changed, p_mi );
180 var_DelCallback( p_input_thread, "intf-event",
181 input_event_changed, p_mi );
182 del_es_callbacks( p_input_thread, p_mi );
184 /* We owned this one */
185 input_Stop( p_input_thread );
186 input_Close( p_input_thread );
190 * Retrieve the input thread. Be sure to release the object
191 * once you are done with it. (libvlc Internal)
193 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
195 input_thread_t *p_input_thread;
197 assert( p_mi );
199 lock_input(p_mi);
200 p_input_thread = p_mi->input.p_thread;
201 if( p_input_thread )
202 vlc_object_hold( p_input_thread );
203 else
204 libvlc_printerr( "No active input" );
205 unlock_input(p_mi);
207 return p_input_thread;
211 * Set the internal state of the media_player. (media player Internal)
213 * Function will lock the media_player.
215 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
216 bool b_locked )
218 if(!b_locked)
219 lock(p_mi);
220 p_mi->state = state;
222 libvlc_media_t *media = p_mi->p_md;
223 if (media)
224 libvlc_media_retain(media);
226 if(!b_locked)
227 unlock(p_mi);
229 if (media)
231 // Also set the state of the corresponding media
232 // This is strictly for convenience.
233 libvlc_media_set_state(media, state);
235 libvlc_media_release(media);
239 static int
240 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
241 vlc_value_t oldval, vlc_value_t newval,
242 void * p_userdata )
244 VLC_UNUSED(oldval);
245 VLC_UNUSED(p_this);
246 VLC_UNUSED(psz_cmd);
247 libvlc_media_player_t * p_mi = p_userdata;
248 libvlc_event_t event;
250 event.type = libvlc_MediaPlayerSeekableChanged;
251 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
253 libvlc_event_send( &p_mi->event_manager, &event );
254 return VLC_SUCCESS;
257 static int
258 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
259 vlc_value_t oldval, vlc_value_t newval,
260 void * p_userdata )
262 VLC_UNUSED(oldval);
263 VLC_UNUSED(p_this);
264 VLC_UNUSED(psz_cmd);
265 libvlc_media_player_t * p_mi = p_userdata;
266 libvlc_event_t event;
268 event.type = libvlc_MediaPlayerPausableChanged;
269 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
271 libvlc_event_send( &p_mi->event_manager, &event );
272 return VLC_SUCCESS;
275 static int
276 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
277 vlc_value_t oldval, vlc_value_t newval,
278 void * p_userdata )
280 VLC_UNUSED(oldval);
281 VLC_UNUSED(p_this);
282 VLC_UNUSED(psz_cmd);
283 libvlc_media_player_t * p_mi = p_userdata;
284 libvlc_event_t event;
286 event.type = libvlc_MediaPlayerScrambledChanged;
287 event.u.media_player_scrambled_changed.new_scrambled = newval.b_bool;
289 libvlc_event_send( &p_mi->event_manager, &event );
290 return VLC_SUCCESS;
293 static int
294 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
295 vlc_value_t oldval, vlc_value_t newval,
296 void * p_userdata )
298 VLC_UNUSED(oldval); VLC_UNUSED(psz_cmd);
299 input_thread_t * p_input = (input_thread_t *)p_this;
300 libvlc_media_player_t * p_mi = p_userdata;
301 libvlc_event_t event;
303 assert( !strcmp( psz_cmd, "intf-event" ) );
305 if( newval.i_int == INPUT_EVENT_STATE )
307 libvlc_state_t libvlc_state;
309 switch ( var_GetInteger( p_input, "state" ) )
311 case INIT_S:
312 libvlc_state = libvlc_NothingSpecial;
313 event.type = libvlc_MediaPlayerNothingSpecial;
314 break;
315 case OPENING_S:
316 libvlc_state = libvlc_Opening;
317 event.type = libvlc_MediaPlayerOpening;
318 break;
319 case PLAYING_S:
320 libvlc_state = libvlc_Playing;
321 event.type = libvlc_MediaPlayerPlaying;
322 break;
323 case PAUSE_S:
324 libvlc_state = libvlc_Paused;
325 event.type = libvlc_MediaPlayerPaused;
326 break;
327 case END_S:
328 libvlc_state = libvlc_Ended;
329 event.type = libvlc_MediaPlayerEndReached;
330 break;
331 case ERROR_S:
332 libvlc_state = libvlc_Error;
333 event.type = libvlc_MediaPlayerEncounteredError;
334 break;
336 default:
337 return VLC_SUCCESS;
340 set_state( p_mi, libvlc_state, false );
341 libvlc_event_send( &p_mi->event_manager, &event );
343 else if( newval.i_int == INPUT_EVENT_DEAD )
345 libvlc_state_t libvlc_state = libvlc_Ended;
346 event.type = libvlc_MediaPlayerStopped;
348 set_state( p_mi, libvlc_state, false );
349 libvlc_event_send( &p_mi->event_manager, &event );
351 else if( newval.i_int == INPUT_EVENT_POSITION )
353 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
354 return VLC_SUCCESS; /* Don't send the position while stopped */
356 /* */
357 event.type = libvlc_MediaPlayerPositionChanged;
358 event.u.media_player_position_changed.new_position =
359 var_GetFloat( p_input, "position" );
360 libvlc_event_send( &p_mi->event_manager, &event );
362 /* */
363 event.type = libvlc_MediaPlayerTimeChanged;
364 event.u.media_player_time_changed.new_time =
365 from_mtime(var_GetInteger( p_input, "time" ));
366 libvlc_event_send( &p_mi->event_manager, &event );
368 else if( newval.i_int == INPUT_EVENT_LENGTH )
370 event.type = libvlc_MediaPlayerLengthChanged;
371 event.u.media_player_length_changed.new_length =
372 from_mtime(var_GetInteger( p_input, "length" ));
373 libvlc_event_send( &p_mi->event_manager, &event );
375 else if( newval.i_int == INPUT_EVENT_CACHE )
377 event.type = libvlc_MediaPlayerBuffering;
378 event.u.media_player_buffering.new_cache = (100 *
379 var_GetFloat( p_input, "cache" ));
380 libvlc_event_send( &p_mi->event_manager, &event );
382 else if( newval.i_int == INPUT_EVENT_VOUT )
384 vout_thread_t **pp_vout;
385 size_t i_vout;
386 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
388 i_vout = 0;
390 else
392 for( size_t i = 0; i < i_vout; i++ )
393 vlc_object_release( pp_vout[i] );
394 free( pp_vout );
397 event.type = libvlc_MediaPlayerVout;
398 event.u.media_player_vout.new_count = i_vout;
399 libvlc_event_send( &p_mi->event_manager, &event );
401 else if ( newval.i_int == INPUT_EVENT_TITLE )
403 event.type = libvlc_MediaPlayerTitleChanged;
404 event.u.media_player_title_changed.new_title = var_GetInteger( p_input, "title" );
405 libvlc_event_send( &p_mi->event_manager, &event );
407 else if ( newval.i_int == INPUT_EVENT_CHAPTER )
409 event.type = libvlc_MediaPlayerChapterChanged;
410 event.u.media_player_chapter_changed.new_chapter = var_GetInteger( p_input, "chapter" );
411 libvlc_event_send( &p_mi->event_manager, &event );
413 else if ( newval.i_int == INPUT_EVENT_ES )
415 /* Send ESSelected events from this callback ("intf-event") and not
416 * from "audio-es", "video-es", "spu-es" callbacks. Indeed, these
417 * callbacks are not triggered when the input_thread changes an ES
418 * while this callback is. */
419 struct {
420 const char *psz_name;
421 const libvlc_track_type_t type;
422 int new_es;
423 } es_list[] = {
424 { "audio-es", libvlc_track_audio, ES_INIT },
425 { "video-es", libvlc_track_video, ES_INIT },
426 { "spu-es", libvlc_track_text, ES_INIT },
428 /* Check if an ES selection changed */
429 lock( p_mi );
430 for( size_t i = 0; i < ARRAY_SIZE( es_list ); ++i )
432 int current_es = var_GetInteger( p_input, es_list[i].psz_name );
433 if( current_es != p_mi->selected_es[i] )
434 es_list[i].new_es = p_mi->selected_es[i] = current_es;
436 unlock( p_mi );
438 /* Send the ESSelected event for each ES that were newly selected */
439 for( size_t i = 0; i < ARRAY_SIZE( es_list ); ++i )
441 if( es_list[i].new_es != ES_INIT )
443 event.type = libvlc_MediaPlayerESSelected;
444 event.u.media_player_es_changed.i_type = es_list[i].type;
445 event.u.media_player_es_changed.i_id = es_list[i].new_es;
446 libvlc_event_send( &p_mi->event_manager, &event );
451 return VLC_SUCCESS;
454 static int track_type_from_name(const char *psz_name)
456 if( !strcmp( psz_name, "video-es" ) )
457 return libvlc_track_video;
458 else if( !strcmp( psz_name, "audio-es" ) )
459 return libvlc_track_audio;
460 else if( !strcmp( psz_name, "spu-es" ) )
461 return libvlc_track_text;
462 else
463 return libvlc_track_unknown;
466 static int input_es_changed( vlc_object_t *p_this,
467 char const *psz_cmd,
468 int action,
469 vlc_value_t *p_val,
470 void *p_userdata )
472 VLC_UNUSED(p_this);
473 libvlc_media_player_t *mp = p_userdata;
474 libvlc_event_t event;
476 /* Ignore the "Disable" element */
477 if (p_val && p_val->i_int < 0)
478 return VLC_EGENERIC;
480 switch (action)
482 case VLC_VAR_ADDCHOICE:
483 event.type = libvlc_MediaPlayerESAdded;
484 break;
485 case VLC_VAR_DELCHOICE:
486 case VLC_VAR_CLEARCHOICES:
487 event.type = libvlc_MediaPlayerESDeleted;
488 break;
489 default:
490 return VLC_EGENERIC;
493 event.u.media_player_es_changed.i_type = track_type_from_name(psz_cmd);
495 int i_id;
496 if (action != VLC_VAR_CLEARCHOICES)
498 if (!p_val)
499 return VLC_EGENERIC;
500 i_id = p_val->i_int;
502 else
504 /* -1 means all ES tracks of this type were deleted. */
505 i_id = -1;
507 event.u.media_player_es_changed.i_id = i_id;
509 libvlc_event_send(&mp->event_manager, &event);
511 return VLC_SUCCESS;
514 /**************************************************************************
515 * Snapshot Taken Event.
517 * FIXME: This snapshot API interface makes no sense in media_player.
518 *************************************************************************/
519 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
520 vlc_value_t oldval, vlc_value_t newval, void *p_data )
522 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
524 libvlc_media_player_t *mp = p_data;
525 libvlc_event_t event;
526 event.type = libvlc_MediaPlayerSnapshotTaken;
527 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
528 libvlc_event_send(&mp->event_manager, &event);
530 return VLC_SUCCESS;
533 static int corks_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
534 vlc_value_t cur, void *opaque)
536 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
538 if (!old.i_int != !cur.i_int)
540 libvlc_event_t event;
542 event.type = cur.i_int ? libvlc_MediaPlayerCorked
543 : libvlc_MediaPlayerUncorked;
544 libvlc_event_send(&mp->event_manager, &event);
546 VLC_UNUSED(name); VLC_UNUSED(opaque);
547 return VLC_SUCCESS;
550 static int audio_device_changed(vlc_object_t *obj, const char *name,
551 vlc_value_t old, vlc_value_t cur, void *opaque)
553 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
554 libvlc_event_t event;
556 event.type = libvlc_MediaPlayerAudioDevice;
557 event.u.media_player_audio_device.device = cur.psz_string;
558 libvlc_event_send(&mp->event_manager, &event);
559 VLC_UNUSED(name); VLC_UNUSED(old); VLC_UNUSED(opaque);
560 return VLC_SUCCESS;
563 static int mute_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
564 vlc_value_t cur, void *opaque)
566 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
568 if (old.b_bool != cur.b_bool)
570 libvlc_event_t event;
572 event.type = cur.b_bool ? libvlc_MediaPlayerMuted
573 : libvlc_MediaPlayerUnmuted;
574 libvlc_event_send(&mp->event_manager, &event);
576 VLC_UNUSED(name); VLC_UNUSED(opaque);
577 return VLC_SUCCESS;
580 static int volume_changed(vlc_object_t *obj, const char *name, vlc_value_t old,
581 vlc_value_t cur, void *opaque)
583 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
584 libvlc_event_t event;
586 event.type = libvlc_MediaPlayerAudioVolume;
587 event.u.media_player_audio_volume.volume = cur.f_float;
588 libvlc_event_send(&mp->event_manager, &event);
589 VLC_UNUSED(name); VLC_UNUSED(old); VLC_UNUSED(opaque);
590 return VLC_SUCCESS;
593 /**************************************************************************
594 * Create a Media Instance object.
596 * Refcount strategy:
597 * - All items created by _new start with a refcount set to 1.
598 * - Accessor _release decrease the refcount by 1, if after that
599 * operation the refcount is 0, the object is destroyed.
600 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
602 * Object locking strategy:
603 * - No lock held while in constructor.
604 * - When accessing any member variable this lock is held. (XXX who locks?)
605 * - When attempting to destroy the object the lock is also held.
606 **************************************************************************/
607 libvlc_media_player_t *
608 libvlc_media_player_new( libvlc_instance_t *instance )
610 libvlc_media_player_t * mp;
612 assert(instance);
614 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
615 if (unlikely(mp == NULL))
617 libvlc_printerr("Not enough memory");
618 return NULL;
621 /* Input */
622 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
623 var_Create (mp, "sout", VLC_VAR_STRING);
624 var_Create (mp, "demux-filter", VLC_VAR_STRING);
626 /* Video */
627 var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
628 var_Create (mp, "window", VLC_VAR_STRING);
629 var_Create (mp, "gl", VLC_VAR_STRING);
630 var_Create (mp, "gles2", VLC_VAR_STRING);
631 var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
632 var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
633 var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
634 var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
635 var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
636 var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
637 var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
638 var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
639 var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
640 var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
642 var_Create( mp, "vgl-opaque", VLC_VAR_ADDRESS );
643 var_Create( mp, "vgl-setup-cb", VLC_VAR_ADDRESS );
644 var_Create( mp, "vgl-cleanup-cb", VLC_VAR_ADDRESS );
645 var_Create( mp, "vgl-resize-cb", VLC_VAR_ADDRESS );
646 var_Create( mp, "vgl-swap-cb", VLC_VAR_ADDRESS );
647 var_Create( mp, "vgl-get-proc-address-cb", VLC_VAR_ADDRESS );
648 var_Create( mp, "vgl-make-current-cb", VLC_VAR_ADDRESS );
650 var_Create (mp, "avcodec-hw", VLC_VAR_STRING);
651 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
652 #if defined (_WIN32) || defined (__OS2__)
653 var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
654 #endif
655 #ifdef __APPLE__
656 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
657 #endif
658 #ifdef __ANDROID__
659 var_Create (mp, "drawable-androidwindow", VLC_VAR_ADDRESS);
660 #endif
662 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
663 var_SetBool (mp, "keyboard-events", true);
664 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
665 var_SetBool (mp, "mouse-events", true);
667 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
668 var_Create (mp, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
669 var_Create (mp, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
670 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
671 var_Create (mp, "crop", VLC_VAR_STRING);
672 var_Create (mp, "deinterlace", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
673 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
675 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
676 var_SetInteger (mp, "vbi-page", 100);
678 var_Create (mp, "video-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
679 var_Create (mp, "sub-source", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
680 var_Create (mp, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
682 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
683 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
684 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
685 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
686 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
687 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
688 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
689 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
690 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
692 var_Create (mp, "logo-file", VLC_VAR_STRING);
693 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
694 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
695 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
696 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
697 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
698 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
700 var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
701 var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
702 var_Create (mp, "hue", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
703 var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
704 var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
706 /* Audio */
707 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
708 var_Create (mp, "audio-device", VLC_VAR_STRING);
709 var_Create (mp, "mute", VLC_VAR_BOOL);
710 var_Create (mp, "volume", VLC_VAR_FLOAT);
711 var_Create (mp, "corks", VLC_VAR_INTEGER);
712 var_Create (mp, "audio-filter", VLC_VAR_STRING);
713 var_Create (mp, "role", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
714 var_Create (mp, "amem-data", VLC_VAR_ADDRESS);
715 var_Create (mp, "amem-setup", VLC_VAR_ADDRESS);
716 var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
717 var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
718 var_Create (mp, "amem-pause", VLC_VAR_ADDRESS);
719 var_Create (mp, "amem-resume", VLC_VAR_ADDRESS);
720 var_Create (mp, "amem-flush", VLC_VAR_ADDRESS);
721 var_Create (mp, "amem-drain", VLC_VAR_ADDRESS);
722 var_Create (mp, "amem-set-volume", VLC_VAR_ADDRESS);
723 var_Create (mp, "amem-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
724 var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
725 var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
727 /* Video Title */
728 var_Create (mp, "video-title-show", VLC_VAR_BOOL);
729 var_Create (mp, "video-title-position", VLC_VAR_INTEGER);
730 var_Create (mp, "video-title-timeout", VLC_VAR_INTEGER);
732 /* Equalizer */
733 var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
734 var_Create (mp, "equalizer-vlcfreqs", VLC_VAR_BOOL);
735 var_Create (mp, "equalizer-bands", VLC_VAR_STRING);
737 /* Initialize the shared HTTP cookie jar */
738 vlc_value_t cookies;
739 cookies.p_address = vlc_http_cookies_new();
740 if ( likely(cookies.p_address) )
742 var_Create(mp, "http-cookies", VLC_VAR_ADDRESS);
743 var_SetChecked(mp, "http-cookies", VLC_VAR_ADDRESS, cookies);
746 mp->p_md = NULL;
747 mp->state = libvlc_NothingSpecial;
748 mp->p_libvlc_instance = instance;
749 mp->input.p_thread = NULL;
750 mp->input.p_renderer = NULL;
751 mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
752 if (unlikely(mp->input.p_resource == NULL))
754 vlc_object_release(mp);
755 return NULL;
757 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
758 if( aout != NULL )
759 input_resource_PutAout(mp->input.p_resource, aout);
761 vlc_viewpoint_init(&mp->viewpoint);
763 var_Create (mp, "viewpoint", VLC_VAR_ADDRESS);
764 var_SetAddress( mp, "viewpoint", &mp->viewpoint );
765 vlc_mutex_init (&mp->input.lock);
766 mp->i_refcount = 1;
767 libvlc_event_manager_init(&mp->event_manager, mp);
768 vlc_mutex_init(&mp->object_lock);
770 var_AddCallback(mp, "corks", corks_changed, NULL);
771 var_AddCallback(mp, "audio-device", audio_device_changed, NULL);
772 var_AddCallback(mp, "mute", mute_changed, NULL);
773 var_AddCallback(mp, "volume", volume_changed, NULL);
775 /* Snapshot initialization */
776 /* Attach a var callback to the global object to provide the glue between
777 * vout_thread that generates the event and media_player that re-emits it
778 * with its own event manager
780 * FIXME: It's unclear why we want to put this in public API, and why we
781 * want to expose it in such a limiting and ugly way.
783 var_AddCallback(mp->obj.libvlc, "snapshot-file", snapshot_was_taken, mp);
785 libvlc_retain(instance);
786 return mp;
789 /**************************************************************************
790 * Create a Media Instance object with a media descriptor.
791 **************************************************************************/
792 libvlc_media_player_t *
793 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
795 libvlc_media_player_t * p_mi;
797 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
798 if( !p_mi )
799 return NULL;
801 libvlc_media_retain( p_md );
802 p_mi->p_md = p_md;
804 return p_mi;
807 /**************************************************************************
808 * Destroy a Media Instance object (libvlc internal)
810 * Warning: No lock held here, but hey, this is internal. Caller must lock.
811 **************************************************************************/
812 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
814 assert( p_mi );
816 /* Detach Callback from the main libvlc object */
817 var_DelCallback( p_mi->obj.libvlc,
818 "snapshot-file", snapshot_was_taken, p_mi );
820 /* Detach callback from the media player / input manager object */
821 var_DelCallback( p_mi, "volume", volume_changed, NULL );
822 var_DelCallback( p_mi, "mute", mute_changed, NULL );
823 var_DelCallback( p_mi, "audio-device", audio_device_changed, NULL );
824 var_DelCallback( p_mi, "corks", corks_changed, NULL );
826 /* No need for lock_input() because no other threads knows us anymore */
827 if( p_mi->input.p_thread )
828 release_input_thread(p_mi);
829 input_resource_Terminate( p_mi->input.p_resource );
830 input_resource_Release( p_mi->input.p_resource );
831 if( p_mi->input.p_renderer )
832 vlc_renderer_item_release( p_mi->input.p_renderer );
834 vlc_mutex_destroy( &p_mi->input.lock );
836 libvlc_event_manager_destroy(&p_mi->event_manager);
837 libvlc_media_release( p_mi->p_md );
838 vlc_mutex_destroy( &p_mi->object_lock );
840 vlc_http_cookie_jar_t *cookies = var_GetAddress( p_mi, "http-cookies" );
841 if ( cookies )
843 var_Destroy( p_mi, "http-cookies" );
844 vlc_http_cookies_destroy( cookies );
847 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
848 vlc_object_release( p_mi );
849 libvlc_release(instance);
852 /**************************************************************************
853 * Release a Media Instance object.
855 * Function does the locking.
856 **************************************************************************/
857 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
859 bool destroy;
861 assert( p_mi );
862 lock(p_mi);
863 destroy = !--p_mi->i_refcount;
864 unlock(p_mi);
866 if( destroy )
867 libvlc_media_player_destroy( p_mi );
870 /**************************************************************************
871 * Retain a Media Instance object.
873 * Caller must hold the lock.
874 **************************************************************************/
875 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
877 assert( p_mi );
879 lock(p_mi);
880 p_mi->i_refcount++;
881 unlock(p_mi);
884 /**************************************************************************
885 * Set the Media descriptor associated with the instance.
887 * Enter without lock -- function will lock the object.
888 **************************************************************************/
889 void libvlc_media_player_set_media(
890 libvlc_media_player_t *p_mi,
891 libvlc_media_t *p_md )
893 lock_input(p_mi);
895 release_input_thread( p_mi );
897 lock( p_mi );
898 set_state( p_mi, libvlc_NothingSpecial, true );
899 unlock_input( p_mi );
901 libvlc_media_release( p_mi->p_md );
903 if( !p_md )
905 p_mi->p_md = NULL;
906 unlock(p_mi);
907 return; /* It is ok to pass a NULL md */
910 libvlc_media_retain( p_md );
911 p_mi->p_md = p_md;
913 /* The policy here is to ignore that we were created using a different
914 * libvlc_instance, because we don't really care */
915 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
917 unlock(p_mi);
919 /* Send an event for the newly available media */
920 libvlc_event_t event;
921 event.type = libvlc_MediaPlayerMediaChanged;
922 event.u.media_player_media_changed.new_media = p_md;
923 libvlc_event_send( &p_mi->event_manager, &event );
927 /**************************************************************************
928 * Get the Media descriptor associated with the instance.
929 **************************************************************************/
930 libvlc_media_t *
931 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
933 libvlc_media_t *p_m;
935 lock( p_mi );
936 p_m = p_mi->p_md;
937 if( p_m )
938 libvlc_media_retain( p_m );
939 unlock( p_mi );
941 return p_m;
944 /**************************************************************************
945 * Get the event Manager.
946 **************************************************************************/
947 libvlc_event_manager_t *
948 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
950 return &p_mi->event_manager;
953 static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
955 var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
956 var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
957 var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
960 static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
962 var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
963 var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
964 var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
967 static void on_input_event(input_thread_t *input,
968 const struct vlc_input_event *event, void *userdata)
970 if (event->type == INPUT_EVENT_SUBITEMS)
972 libvlc_media_player_t *media_player = userdata;
973 libvlc_media_t *media = media_player->p_md;
974 libvlc_media_add_subtree(media, event->subitems);
977 input_LegacyEvents(input, event, userdata);
980 /**************************************************************************
981 * Tell media player to start playing.
982 **************************************************************************/
983 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
985 lock_input( p_mi );
987 input_thread_t *p_input_thread = p_mi->input.p_thread;
988 if( p_input_thread )
990 /* A thread already exists, send it a play message */
991 var_SetInteger( p_input_thread, "state", PLAYING_S );
992 unlock_input( p_mi );
993 return 0;
996 /* Ignore previous exception */
997 lock(p_mi);
999 if( !p_mi->p_md )
1001 unlock(p_mi);
1002 unlock_input( p_mi );
1003 libvlc_printerr( "No associated media descriptor" );
1004 return -1;
1007 for( size_t i = 0; i < ARRAY_SIZE( p_mi->selected_es ); ++i )
1008 p_mi->selected_es[i] = ES_INIT;
1010 media_attach_preparsed_event( p_mi->p_md );
1012 p_input_thread = input_Create( p_mi, on_input_event, p_mi,
1013 p_mi->p_md->p_input_item, NULL,
1014 p_mi->input.p_resource,
1015 p_mi->input.p_renderer );
1016 unlock(p_mi);
1017 if( !p_input_thread )
1019 unlock_input(p_mi);
1020 media_detach_preparsed_event( p_mi->p_md );
1021 libvlc_printerr( "Not enough memory" );
1022 return -1;
1025 input_LegacyVarInit( p_input_thread );
1026 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
1027 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
1028 var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
1029 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
1030 add_es_callbacks( p_input_thread, p_mi );
1032 if( input_Start( p_input_thread ) )
1034 unlock_input(p_mi);
1035 del_es_callbacks( p_input_thread, p_mi );
1036 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
1037 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
1038 var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
1039 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
1040 input_Close( p_input_thread );
1041 media_detach_preparsed_event( p_mi->p_md );
1042 libvlc_printerr( "Input initialization failure" );
1043 return -1;
1045 p_mi->input.p_thread = p_input_thread;
1046 unlock_input(p_mi);
1047 return 0;
1050 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
1052 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
1053 if( !p_input_thread )
1054 return;
1056 if( paused )
1058 if( libvlc_media_player_can_pause( p_mi ) )
1059 var_SetInteger( p_input_thread, "state", PAUSE_S );
1060 else
1061 input_Stop( p_input_thread );
1063 else
1065 var_SetInteger( p_input_thread, "state", PLAYING_S );
1068 vlc_object_release( p_input_thread );
1071 /**************************************************************************
1072 * Toggle pause.
1073 **************************************************************************/
1074 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
1076 libvlc_media_player_set_pause( p_mi, libvlc_media_player_is_playing( p_mi ) );
1079 /**************************************************************************
1080 * Tells whether the media player is currently playing.
1081 **************************************************************************/
1082 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
1084 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
1085 return libvlc_Playing == state;
1088 /**************************************************************************
1089 * Stop playing.
1090 **************************************************************************/
1091 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
1093 lock_input(p_mi);
1094 release_input_thread( p_mi ); /* This will stop the input thread */
1096 /* Force to go to stopped state, in case we were in Ended, or Error
1097 * state. */
1098 if( p_mi->state != libvlc_Stopped )
1100 set_state( p_mi, libvlc_Stopped, false );
1102 /* Construct and send the event */
1103 libvlc_event_t event;
1104 event.type = libvlc_MediaPlayerStopped;
1105 libvlc_event_send( &p_mi->event_manager, &event );
1108 input_resource_Terminate( p_mi->input.p_resource );
1109 unlock_input(p_mi);
1112 int libvlc_media_player_set_renderer( libvlc_media_player_t *p_mi,
1113 libvlc_renderer_item_t *p_litem )
1115 vlc_renderer_item_t *p_item =
1116 p_litem ? libvlc_renderer_item_to_vlc( p_litem ) : NULL;
1118 lock_input( p_mi );
1119 input_thread_t *p_input_thread = p_mi->input.p_thread;
1120 if( p_input_thread )
1121 input_Control( p_input_thread, INPUT_SET_RENDERER, p_item );
1123 if( p_mi->input.p_renderer )
1124 vlc_renderer_item_release( p_mi->input.p_renderer );
1125 p_mi->input.p_renderer = p_item ? vlc_renderer_item_hold( p_item ) : NULL;
1126 unlock_input( p_mi );
1128 return 0;
1131 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
1132 void *(*lock_cb) (void *, void **),
1133 void (*unlock_cb) (void *, void *, void *const *),
1134 void (*display_cb) (void *, void *),
1135 void *opaque )
1137 var_SetAddress( mp, "vmem-lock", lock_cb );
1138 var_SetAddress( mp, "vmem-unlock", unlock_cb );
1139 var_SetAddress( mp, "vmem-display", display_cb );
1140 var_SetAddress( mp, "vmem-data", opaque );
1141 var_SetString( mp, "avcodec-hw", "none" );
1142 var_SetString( mp, "vout", "vmem" );
1143 var_SetString( mp, "window", "dummy" );
1146 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
1147 libvlc_video_format_cb setup,
1148 libvlc_video_cleanup_cb cleanup )
1150 var_SetAddress( mp, "vmem-setup", setup );
1151 var_SetAddress( mp, "vmem-cleanup", cleanup );
1154 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
1155 unsigned width, unsigned height, unsigned pitch )
1157 var_SetString( mp, "vmem-chroma", chroma );
1158 var_SetInteger( mp, "vmem-width", width );
1159 var_SetInteger( mp, "vmem-height", height );
1160 var_SetInteger( mp, "vmem-pitch", pitch );
1163 void libvlc_video_set_opengl_callbacks( libvlc_media_player_t *mp,
1164 libvlc_gl_engine_t gl_engine,
1165 libvlc_gl_setup_cb setup_cb,
1166 libvlc_gl_cleanup_cb cleanup_cb,
1167 libvlc_gl_resize_cb resize_cb,
1168 libvlc_gl_swap_cb swap_cb,
1169 libvlc_gl_makeCurrent_cb makeCurrent_cb,
1170 libvlc_gl_getProcAddress_cb getProcAddress_cb,
1171 void* opaque )
1173 #ifdef __ANDROID__
1174 //use the default android window
1175 var_SetString( mp, "window", "");
1176 #else
1177 var_SetString( mp, "window", "wdummy");
1178 #endif
1180 if( gl_engine == libvlc_gl_engine_gles2 )
1182 var_SetString ( mp, "vout", "gles2" );
1183 var_SetString ( mp, "gles2", "vgl" );
1185 else
1187 var_SetString ( mp, "vout", "gl" );
1188 var_SetString ( mp, "gl", "vgl");
1191 var_SetAddress( mp, "vgl-opaque", opaque );
1192 var_SetAddress( mp, "vgl-setup-cb", setup_cb );
1193 var_SetAddress( mp, "vgl-cleanup-cb", cleanup_cb );
1194 var_SetAddress( mp, "vgl-resize-cb", resize_cb );
1195 var_SetAddress( mp, "vgl-swap-cb", swap_cb );
1196 var_SetAddress( mp, "vgl-get-proc-address-cb", getProcAddress_cb );
1197 var_SetAddress( mp, "vgl-make-current-cb", makeCurrent_cb );
1201 /**************************************************************************
1202 * set_nsobject
1203 **************************************************************************/
1204 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1205 void * drawable )
1207 assert (p_mi != NULL);
1208 #ifdef __APPLE__
1209 var_SetString (p_mi, "avcodec-hw", "");
1210 var_SetString (p_mi, "vout", "");
1211 var_SetString (p_mi, "window", "");
1212 var_SetAddress (p_mi, "drawable-nsobject", drawable);
1213 #else
1214 (void)drawable;
1215 libvlc_printerr ("can't set nsobject: APPLE build required");
1216 assert(false);
1217 var_SetString (p_mi, "vout", "none");
1218 var_SetString (p_mi, "window", "none");
1219 #endif
1222 /**************************************************************************
1223 * get_nsobject
1224 **************************************************************************/
1225 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1227 assert (p_mi != NULL);
1228 #ifdef __APPLE__
1229 return var_GetAddress (p_mi, "drawable-nsobject");
1230 #else
1231 (void) p_mi;
1232 return NULL;
1233 #endif
1236 /**************************************************************************
1237 * set_xwindow
1238 **************************************************************************/
1239 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1240 uint32_t drawable )
1242 assert (p_mi != NULL);
1244 var_SetString (p_mi, "avcodec-hw", "");
1245 var_SetString (p_mi, "vout", "");
1246 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1247 var_SetInteger (p_mi, "drawable-xid", drawable);
1250 /**************************************************************************
1251 * get_xwindow
1252 **************************************************************************/
1253 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1255 return var_GetInteger (p_mi, "drawable-xid");
1258 /**************************************************************************
1259 * set_hwnd
1260 **************************************************************************/
1261 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1262 void *drawable )
1264 assert (p_mi != NULL);
1265 #if defined (_WIN32) || defined (__OS2__)
1266 var_SetString (p_mi, "avcodec-hw", "");
1267 var_SetString (p_mi, "vout", "");
1268 var_SetString (p_mi, "window",
1269 (drawable != NULL) ? "embed-hwnd,any" : "");
1270 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1271 #else
1272 (void) drawable;
1273 libvlc_printerr ("can't set hwnd: WIN32 build required");
1274 assert(false);
1275 var_SetString (p_mi, "vout", "none");
1276 var_SetString (p_mi, "window", "none");
1277 #endif
1280 /**************************************************************************
1281 * get_hwnd
1282 **************************************************************************/
1283 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1285 assert (p_mi != NULL);
1286 #if defined (_WIN32) || defined (__OS2__)
1287 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1288 #else
1289 (void) p_mi;
1290 return NULL;
1291 #endif
1294 /**************************************************************************
1295 * set_android_context
1296 **************************************************************************/
1297 void libvlc_media_player_set_android_context( libvlc_media_player_t *p_mi,
1298 void *p_awindow_handler )
1300 assert (p_mi != NULL);
1301 #ifdef __ANDROID__
1302 var_SetAddress (p_mi, "drawable-androidwindow", p_awindow_handler);
1303 #else
1304 (void) p_awindow_handler;
1305 libvlc_printerr ("can't set android context: ANDROID build required");
1306 assert(false);
1307 var_SetString (p_mi, "vout", "none");
1308 var_SetString (p_mi, "window", "none");
1309 #endif
1312 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1313 libvlc_audio_play_cb play_cb,
1314 libvlc_audio_pause_cb pause_cb,
1315 libvlc_audio_resume_cb resume_cb,
1316 libvlc_audio_flush_cb flush_cb,
1317 libvlc_audio_drain_cb drain_cb,
1318 void *opaque )
1320 var_SetAddress( mp, "amem-play", play_cb );
1321 var_SetAddress( mp, "amem-pause", pause_cb );
1322 var_SetAddress( mp, "amem-resume", resume_cb );
1323 var_SetAddress( mp, "amem-flush", flush_cb );
1324 var_SetAddress( mp, "amem-drain", drain_cb );
1325 var_SetAddress( mp, "amem-data", opaque );
1326 var_SetString( mp, "aout", "amem,none" );
1328 input_resource_ResetAout(mp->input.p_resource);
1331 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1332 libvlc_audio_set_volume_cb cb )
1334 var_SetAddress( mp, "amem-set-volume", cb );
1336 input_resource_ResetAout(mp->input.p_resource);
1339 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1340 libvlc_audio_setup_cb setup,
1341 libvlc_audio_cleanup_cb cleanup )
1343 var_SetAddress( mp, "amem-setup", setup );
1344 var_SetAddress( mp, "amem-cleanup", cleanup );
1346 input_resource_ResetAout(mp->input.p_resource);
1349 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1350 unsigned rate, unsigned channels )
1352 var_SetString( mp, "amem-format", format );
1353 var_SetInteger( mp, "amem-rate", rate );
1354 var_SetInteger( mp, "amem-channels", channels );
1356 input_resource_ResetAout(mp->input.p_resource);
1360 /**************************************************************************
1361 * Getters for stream information
1362 **************************************************************************/
1363 libvlc_time_t libvlc_media_player_get_length(
1364 libvlc_media_player_t *p_mi )
1366 input_thread_t *p_input_thread;
1367 libvlc_time_t i_time;
1369 p_input_thread = libvlc_get_input_thread ( p_mi );
1370 if( !p_input_thread )
1371 return -1;
1373 i_time = from_mtime(var_GetInteger( p_input_thread, "length" ));
1374 vlc_object_release( p_input_thread );
1376 return i_time;
1379 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1381 input_thread_t *p_input_thread;
1382 libvlc_time_t i_time;
1384 p_input_thread = libvlc_get_input_thread ( p_mi );
1385 if( !p_input_thread )
1386 return -1;
1388 i_time = from_mtime(var_GetInteger( p_input_thread , "time" ));
1389 vlc_object_release( p_input_thread );
1390 return i_time;
1393 int libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1394 libvlc_time_t i_time, bool b_fast )
1396 input_thread_t *p_input_thread;
1398 p_input_thread = libvlc_get_input_thread ( p_mi );
1399 if( !p_input_thread )
1400 return -1;
1402 input_SetTime( p_input_thread, to_mtime(i_time), b_fast );
1403 vlc_object_release( p_input_thread );
1404 return 0;
1407 int libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1408 float position, bool b_fast )
1410 input_thread_t *p_input_thread;
1412 p_input_thread = libvlc_get_input_thread ( p_mi );
1413 if( !p_input_thread )
1414 return -1;
1416 input_SetPosition( p_input_thread, position, b_fast );
1417 vlc_object_release( p_input_thread );
1418 return 0;
1421 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1423 input_thread_t *p_input_thread;
1424 float f_position;
1426 p_input_thread = libvlc_get_input_thread ( p_mi );
1427 if( !p_input_thread )
1428 return -1.0;
1430 f_position = var_GetFloat( p_input_thread, "position" );
1431 vlc_object_release( p_input_thread );
1433 return f_position;
1436 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1437 int chapter )
1439 input_thread_t *p_input_thread;
1441 p_input_thread = libvlc_get_input_thread ( p_mi );
1442 if( !p_input_thread )
1443 return;
1445 var_SetInteger( p_input_thread, "chapter", chapter );
1446 vlc_object_release( p_input_thread );
1449 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1451 input_thread_t *p_input_thread;
1452 int i_chapter;
1454 p_input_thread = libvlc_get_input_thread ( p_mi );
1455 if( !p_input_thread )
1456 return -1;
1458 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1459 vlc_object_release( p_input_thread );
1461 return i_chapter;
1464 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1466 input_thread_t *p_input_thread;
1467 size_t val;
1469 p_input_thread = libvlc_get_input_thread ( p_mi );
1470 if( !p_input_thread )
1471 return -1;
1473 int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val );
1474 vlc_object_release( p_input_thread );
1476 return i_ret == VLC_SUCCESS ? (ssize_t)val : -1;
1479 int libvlc_media_player_get_chapter_count_for_title(
1480 libvlc_media_player_t *p_mi,
1481 int i_title )
1483 size_t val;
1485 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1486 if( !p_input_thread )
1487 return -1;
1489 char psz_name[sizeof ("title ") + 3 * sizeof (int)];
1490 sprintf( psz_name, "title %2u", i_title );
1492 int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val );
1493 vlc_object_release( p_input_thread );
1495 return i_ret == VLC_SUCCESS ? (ssize_t)val : -1;
1498 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1499 int i_title )
1501 input_thread_t *p_input_thread;
1503 p_input_thread = libvlc_get_input_thread ( p_mi );
1504 if( !p_input_thread )
1505 return;
1507 var_SetInteger( p_input_thread, "title", i_title );
1508 vlc_object_release( p_input_thread );
1510 //send event
1511 libvlc_event_t event;
1512 event.type = libvlc_MediaPlayerTitleChanged;
1513 event.u.media_player_title_changed.new_title = i_title;
1514 libvlc_event_send( &p_mi->event_manager, &event );
1517 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1519 input_thread_t *p_input_thread;
1520 int i_title;
1522 p_input_thread = libvlc_get_input_thread ( p_mi );
1523 if( !p_input_thread )
1524 return -1;
1526 i_title = var_GetInteger( p_input_thread, "title" );
1527 vlc_object_release( p_input_thread );
1529 return i_title;
1532 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1534 input_thread_t *p_input_thread;
1535 size_t val;
1537 p_input_thread = libvlc_get_input_thread ( p_mi );
1538 if( !p_input_thread )
1539 return -1;
1541 int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val );
1542 vlc_object_release( p_input_thread );
1544 return i_ret == VLC_SUCCESS ? (ssize_t)val : -1;
1547 int libvlc_media_player_get_full_title_descriptions( libvlc_media_player_t *p_mi,
1548 libvlc_title_description_t *** pp_titles )
1550 assert( p_mi );
1552 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1554 if( !p_input_thread )
1555 return -1;
1557 input_title_t **p_input_title;
1558 int count;
1560 /* fetch data */
1561 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO,
1562 &p_input_title, &count );
1563 vlc_object_release( p_input_thread );
1564 if( ret != VLC_SUCCESS )
1565 return -1;
1567 libvlc_title_description_t **titles = vlc_alloc( count, sizeof (*titles) );
1568 if( count > 0 && unlikely(titles == NULL) )
1569 return -1;
1571 /* fill array */
1572 for( int i = 0; i < count; i++)
1574 libvlc_title_description_t *title = malloc( sizeof (*title) );
1575 if( unlikely(title == NULL) )
1577 libvlc_title_descriptions_release( titles, i );
1578 return -1;
1580 titles[i] = title;
1582 /* we want to return milliseconds to match the rest of the API */
1583 title->i_duration = MS_FROM_VLC_TICK(p_input_title[i]->i_length);
1584 title->i_flags = p_input_title[i]->i_flags;
1585 if( p_input_title[i]->psz_name )
1586 title->psz_name = strdup( p_input_title[i]->psz_name );
1587 else
1588 title->psz_name = NULL;
1589 vlc_input_title_Delete( p_input_title[i] );
1591 free( p_input_title );
1593 *pp_titles = titles;
1594 return count;
1597 void libvlc_title_descriptions_release( libvlc_title_description_t **p_titles,
1598 unsigned i_count )
1600 for (unsigned i = 0; i < i_count; i++ )
1602 if ( !p_titles[i] )
1603 continue;
1605 free( p_titles[i]->psz_name );
1606 free( p_titles[i] );
1608 free( p_titles );
1611 int libvlc_media_player_get_full_chapter_descriptions( libvlc_media_player_t *p_mi,
1612 int i_chapters_of_title,
1613 libvlc_chapter_description_t *** pp_chapters )
1615 assert( p_mi );
1617 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
1619 if( !p_input_thread )
1620 return -1;
1622 seekpoint_t **p_seekpoint = NULL;
1623 input_title_t **pp_title, *p_title = NULL;
1624 int i_title_count = 0, ci_chapter_count = 0;
1625 int ret = input_Control( p_input_thread, INPUT_GET_FULL_TITLE_INFO, &pp_title,
1626 &i_title_count );
1627 vlc_object_release( p_input_thread );
1629 if( ret != VLC_SUCCESS || i_chapters_of_title >= i_title_count )
1630 goto error;
1632 p_title = pp_title[i_chapters_of_title];
1633 int64_t i_title_duration = MS_FROM_VLC_TICK(p_title->i_length);
1634 p_seekpoint = p_title->seekpoint;
1635 ci_chapter_count = p_title->i_seekpoint;
1637 if( ci_chapter_count == 0 || p_seekpoint == NULL)
1638 goto error;
1640 *pp_chapters = calloc( ci_chapter_count, sizeof(**pp_chapters) );
1641 if( !*pp_chapters )
1643 goto error;
1646 /* fill array */
1647 for( int i = 0; i < ci_chapter_count; ++i )
1649 libvlc_chapter_description_t *p_chapter = malloc( sizeof(*p_chapter) );
1650 if( unlikely(p_chapter == NULL) )
1652 goto error;
1654 (*pp_chapters)[i] = p_chapter;
1656 p_chapter->i_time_offset = MS_FROM_VLC_TICK( p_seekpoint[i]->i_time_offset );
1658 if( i < ci_chapter_count - 1 )
1660 p_chapter->i_duration = MS_FROM_VLC_TICK( p_seekpoint[i + 1]->i_time_offset ) -
1661 p_chapter->i_time_offset;
1663 else
1665 if ( i_title_duration )
1666 p_chapter->i_duration = i_title_duration - p_chapter->i_time_offset;
1667 else
1668 p_chapter->i_duration = 0;
1671 if( p_seekpoint[i]->psz_name )
1673 p_chapter->psz_name = strdup( p_seekpoint[i]->psz_name );
1675 else
1677 p_chapter->psz_name = NULL;
1679 vlc_seekpoint_Delete( p_seekpoint[i] );
1680 p_seekpoint[i] = NULL;
1683 free( p_seekpoint );
1684 return ci_chapter_count;
1686 error:
1687 if( *pp_chapters )
1688 libvlc_chapter_descriptions_release( *pp_chapters, ci_chapter_count );
1689 for( int i = 0; i < i_title_count; i++ )
1690 vlc_input_title_Delete( pp_title[i] );
1691 free( pp_title ) ;
1692 return -1;
1695 void libvlc_chapter_descriptions_release( libvlc_chapter_description_t **p_chapters,
1696 unsigned i_count )
1698 for (unsigned i = 0; i < i_count; i++ )
1700 if ( !p_chapters[i] )
1701 continue;
1703 free( p_chapters[i]->psz_name );
1704 free( p_chapters[i] );
1706 free( p_chapters );
1709 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1711 input_thread_t *p_input_thread;
1713 p_input_thread = libvlc_get_input_thread ( p_mi );
1714 if( !p_input_thread )
1715 return;
1717 int i_type = var_Type( p_input_thread, "next-chapter" );
1718 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1719 "next-chapter":"next-title" );
1721 vlc_object_release( p_input_thread );
1724 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1726 input_thread_t *p_input_thread;
1728 p_input_thread = libvlc_get_input_thread ( p_mi );
1729 if( !p_input_thread )
1730 return;
1732 int i_type = var_Type( p_input_thread, "next-chapter" );
1733 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1734 "prev-chapter":"prev-title" );
1736 vlc_object_release( p_input_thread );
1739 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1741 input_thread_t *p_input_thread =
1742 libvlc_get_input_thread ( p_mi );
1743 if ( !p_input_thread )
1744 return false;
1746 int state = var_GetInteger( p_input_thread, "state" );
1747 vlc_object_release( p_input_thread );
1749 return state != END_S && state != ERROR_S;
1752 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1754 var_SetFloat (p_mi, "rate", rate);
1756 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1757 if( !p_input_thread )
1758 return 0;
1759 var_SetFloat( p_input_thread, "rate", rate );
1760 vlc_object_release( p_input_thread );
1761 return 0;
1764 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1766 return var_GetFloat (p_mi, "rate");
1769 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1771 lock(p_mi);
1772 libvlc_state_t state = p_mi->state;
1773 unlock(p_mi);
1774 return state;
1777 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1779 input_thread_t *p_input_thread;
1780 bool b_seekable;
1782 p_input_thread = libvlc_get_input_thread ( p_mi );
1783 if ( !p_input_thread )
1784 return false;
1785 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1786 vlc_object_release( p_input_thread );
1788 return b_seekable;
1791 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1792 unsigned navigate )
1794 static const int map[] =
1796 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1797 INPUT_NAV_LEFT, INPUT_NAV_RIGHT, INPUT_NAV_POPUP,
1800 if( navigate >= sizeof(map) / sizeof(map[0]) )
1801 return;
1803 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1804 if ( p_input == NULL )
1805 return;
1807 input_Control( p_input, map[navigate], NULL );
1808 vlc_object_release( p_input );
1811 /* internal function, used by audio, video */
1812 libvlc_track_description_t *
1813 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1814 const char *psz_variable )
1816 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1817 libvlc_track_description_t *ret, **pp = &ret;
1819 if( !p_input )
1820 return NULL;
1822 vlc_value_t *val_list;
1823 char **text_list;
1824 size_t count;
1826 int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES,
1827 &count, &val_list, &text_list );
1828 if( i_ret != VLC_SUCCESS )
1829 return NULL;
1831 for (size_t i = 0; i < count; i++)
1833 libvlc_track_description_t *tr = malloc(sizeof (*tr));
1834 if (unlikely(tr == NULL))
1836 libvlc_printerr("Not enough memory");
1837 continue;
1840 *pp = tr;
1841 tr->i_id = val_list[i].i_int;
1842 tr->psz_name = text_list[i];
1843 pp = &tr->p_next;
1846 *pp = NULL;
1847 free(val_list);
1848 free(text_list);
1849 vlc_object_release( p_input );
1851 return ret;
1854 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1856 libvlc_track_description_t *p_actual, *p_before;
1857 p_actual = p_td;
1859 while ( p_actual )
1861 free( p_actual->psz_name );
1862 p_before = p_actual;
1863 p_actual = p_before->p_next;
1864 free( p_before );
1868 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1870 input_thread_t *p_input_thread;
1871 bool b_can_pause;
1873 p_input_thread = libvlc_get_input_thread ( p_mi );
1874 if ( !p_input_thread )
1875 return false;
1876 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1877 vlc_object_release( p_input_thread );
1879 return b_can_pause;
1882 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1884 input_thread_t *p_input_thread;
1885 bool b_program_scrambled;
1887 p_input_thread = libvlc_get_input_thread ( p_mi );
1888 if ( !p_input_thread )
1889 return false;
1890 b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1891 vlc_object_release( p_input_thread );
1893 return b_program_scrambled;
1896 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1898 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1899 if( p_input_thread != NULL )
1901 var_TriggerCallback( p_input_thread, "frame-next" );
1902 vlc_object_release( p_input_thread );
1907 * Private lookup table to get subpicture alignment flag values corresponding
1908 * to a libvlc_position_t enumerated value.
1910 static const unsigned char position_subpicture_alignment[] = {
1911 [libvlc_position_center] = 0,
1912 [libvlc_position_left] = SUBPICTURE_ALIGN_LEFT,
1913 [libvlc_position_right] = SUBPICTURE_ALIGN_RIGHT,
1914 [libvlc_position_top] = SUBPICTURE_ALIGN_TOP,
1915 [libvlc_position_top_left] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1916 [libvlc_position_top_right] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1917 [libvlc_position_bottom] = SUBPICTURE_ALIGN_BOTTOM,
1918 [libvlc_position_bottom_left] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1919 [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1922 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1924 assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1926 if ( position != libvlc_position_disable )
1928 var_SetBool( p_mi, "video-title-show", true );
1929 var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1930 var_SetInteger( p_mi, "video-title-timeout", timeout );
1932 else
1934 var_SetBool( p_mi, "video-title-show", false );
1938 int libvlc_media_player_add_slave( libvlc_media_player_t *p_mi,
1939 libvlc_media_slave_type_t i_type,
1940 const char *psz_uri, bool b_select )
1942 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1944 if( p_input_thread == NULL )
1946 libvlc_media_t *p_media = libvlc_media_player_get_media( p_mi );
1947 if( p_media == NULL )
1948 return -1;
1950 int i_ret = libvlc_media_slaves_add( p_media, i_type, 4, psz_uri );
1951 libvlc_media_release( p_media );
1952 return i_ret;
1954 else
1956 int i_ret = input_AddSlave( p_input_thread, (enum slave_type) i_type,
1957 psz_uri, b_select, false, false );
1958 vlc_object_release( p_input_thread );
1960 return i_ret == VLC_SUCCESS ? 0 : -1;
1965 * Maximum size of a formatted equalizer amplification band frequency value.
1967 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1969 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1970 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1971 * terminator).
1973 #define EQZ_BAND_VALUE_SIZE 12
1975 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1977 char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1979 if( p_equalizer != NULL )
1981 for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1983 c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
1984 p_equalizer->f_amp[i] );
1985 if( unlikely(c >= sizeof(bands)) )
1986 return -1;
1989 var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
1990 var_SetString( p_mi, "equalizer-bands", bands );
1992 var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1994 audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
1995 if( p_aout != NULL )
1997 if( p_equalizer != NULL )
1999 var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
2000 var_SetString( p_aout, "equalizer-bands", bands );
2003 var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
2004 vlc_object_release( p_aout );
2007 return 0;
2010 static const char roles[][16] =
2012 [libvlc_role_Music] = "music",
2013 [libvlc_role_Video] = "video",
2014 [libvlc_role_Communication] = "communication",
2015 [libvlc_role_Game] = "game",
2016 [libvlc_role_Notification] = "notification",
2017 [libvlc_role_Animation] = "animation",
2018 [libvlc_role_Production] = "production",
2019 [libvlc_role_Accessibility] = "accessibility",
2020 [libvlc_role_Test] = "test",
2023 int libvlc_media_player_set_role(libvlc_media_player_t *mp, unsigned role)
2025 if (role >= ARRAY_SIZE(roles)
2026 || var_SetString(mp, "role", roles[role]) != VLC_SUCCESS)
2027 return -1;
2028 return 0;
2031 int libvlc_media_player_get_role(libvlc_media_player_t *mp)
2033 int ret = -1;
2034 char *str = var_GetString(mp, "role");
2035 if (str == NULL)
2036 return 0;
2038 for (size_t i = 0; i < ARRAY_SIZE(roles); i++)
2039 if (!strcmp(roles[i], str))
2041 ret = i;
2042 break;
2045 free(str);
2046 return ret;