1 /*****************************************************************************
2 * libvlc_audio.c: New libvlc audio control API
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
7 * Authors: Filippo Carone <filippo@carone.org>
8 * Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
32 #include <vlc/libvlc.h>
33 #include <vlc/libvlc_media.h>
34 #include <vlc/libvlc_media_player.h>
36 #include <vlc_common.h>
37 #include <vlc_input.h>
38 #include <vlc_aout_intf.h>
40 #include <vlc_modules.h>
42 #include "libvlc_internal.h"
43 #include "media_player_internal.h"
46 * Remember to release the returned audio_output_t since it is locked at
47 * the end of this function.
49 static audio_output_t *GetAOut( libvlc_media_player_t *mp )
53 input_thread_t *p_input = libvlc_get_input_thread( mp );
57 audio_output_t * p_aout = input_GetAout( p_input );
58 vlc_object_release( p_input );
60 libvlc_printerr( "No active audio output" );
64 /*****************************************
65 * Get the list of available audio outputs
66 *****************************************/
67 libvlc_audio_output_t *
68 libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
70 VLC_UNUSED( p_instance );
71 libvlc_audio_output_t *p_list = NULL,
74 module_t **module_list = module_list_get( NULL );
76 for (size_t i = 0; module_list[i]; i++)
78 module_t *p_module = module_list[i];
80 if( module_provides( p_module, "audio output" ) )
84 p_actual = ( libvlc_audio_output_t * )
85 malloc( sizeof( libvlc_audio_output_t ) );
86 if( p_actual == NULL )
88 libvlc_printerr( "Not enough memory" );
89 libvlc_audio_output_list_release( p_list );
90 module_list_free( module_list );
96 p_previous = p_actual;
99 p_actual->psz_name = strdup( module_get_object( p_module ) );
100 p_actual->psz_description = strdup( module_get_name( p_module, true ) );
101 p_actual->p_next = NULL;
102 if( p_previous != p_actual ) /* not first item */
103 p_previous->p_next = p_actual;
104 p_previous = p_actual;
105 p_actual = p_actual->p_next;
109 module_list_free( module_list );
114 /********************************************
115 * Free the list of available audio outputs
116 ***********************************************/
117 void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
119 libvlc_audio_output_t *p_actual, *p_before;
124 free( p_actual->psz_name );
125 free( p_actual->psz_description );
127 p_actual = p_before->p_next;
133 /***********************
134 * Set the audio output.
135 ***********************/
136 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
140 if( !module_exists( psz_name )
141 || asprintf( &value, "%s,none", psz_name ) == -1 )
143 var_SetString( mp, "aout", value );
148 /****************************
149 * Get count of devices.
150 *****************************/
151 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
152 const char *psz_audio_output )
154 char *psz_config_name;
155 if( !psz_audio_output )
157 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
160 module_config_t *p_module_config = config_FindConfig(
161 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
163 if( p_module_config && p_module_config->pf_update_list )
166 val.psz_string = strdup( p_module_config->value.psz );
168 p_module_config->pf_update_list(
169 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
170 free( val.psz_string );
171 free( psz_config_name );
173 return p_module_config->i_list;
176 free( psz_config_name );
180 /********************************
181 * Get long name of device
182 *********************************/
183 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
184 const char *psz_audio_output,
187 char *psz_config_name;
188 if( !psz_audio_output )
190 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
193 module_config_t *p_module_config = config_FindConfig(
194 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
196 if( p_module_config )
198 // refresh if there arent devices
199 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
202 val.psz_string = strdup( p_module_config->value.psz );
204 p_module_config->pf_update_list(
205 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
206 free( val.psz_string );
209 if( i_device >= 0 && i_device < p_module_config->i_list )
211 free( psz_config_name );
213 if( p_module_config->ppsz_list_text[i_device] )
214 return strdup( p_module_config->ppsz_list_text[i_device] );
216 return strdup( p_module_config->ppsz_list[i_device] );
220 free( psz_config_name );
224 /********************************
225 * Get id name of device
226 *********************************/
227 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
228 const char *psz_audio_output,
231 char *psz_config_name;
232 if( !psz_audio_output )
234 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
237 module_config_t *p_module_config = config_FindConfig(
238 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
240 if( p_module_config )
242 // refresh if there arent devices
243 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
246 val.psz_string = strdup( p_module_config->value.psz );
248 p_module_config->pf_update_list(
249 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
250 free( val.psz_string );
253 if( i_device >= 0 && i_device < p_module_config->i_list )
255 free( psz_config_name );
256 return strdup( p_module_config->ppsz_list[i_device] );
260 free( psz_config_name );
264 /*****************************
265 * Set device for using
266 *****************************/
267 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
268 const char *psz_audio_output,
269 const char *psz_device_id )
271 char *psz_config_name;
272 if( !psz_audio_output || !psz_device_id )
274 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
276 if( !var_Type( mp, psz_config_name ) )
277 /* Don't recreate the same variable over and over and over... */
278 var_Create( mp, psz_config_name, VLC_VAR_STRING );
279 var_SetString( mp, psz_config_name, psz_device_id );
280 free( psz_config_name );
283 /*****************************************************************************
284 * libvlc_audio_output_get_device_type : Get the current audio device type
285 *****************************************************************************/
286 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
288 audio_output_t *p_aout = GetAOut( mp );
291 int i_device_type = var_GetInteger( p_aout, "audio-device" );
292 vlc_object_release( p_aout );
293 return i_device_type;
295 return libvlc_AudioOutputDevice_Error;
298 /*****************************************************************************
299 * libvlc_audio_output_set_device_type : Set the audio device type
300 *****************************************************************************/
301 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
304 audio_output_t *p_aout = GetAOut( mp );
307 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
308 libvlc_printerr( "Error setting audio device" );
309 vlc_object_release( p_aout );
312 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
314 aout_MuteToggle( mp );
317 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
319 return aout_MuteGet( mp );
322 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
324 aout_MuteSet( VLC_OBJECT(mp), mute != 0 );
327 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
329 float vol = aout_VolumeGet( mp );
330 return ( vol >= 0.f ) ? lroundf( vol * 100.f ) : -1;
333 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
335 float vol = volume / 100.f;
338 libvlc_printerr( "Volume out of range" );
341 aout_VolumeSet (mp, vol);
345 /*****************************************************************************
346 * libvlc_audio_get_track_count : Get the number of available audio tracks
347 *****************************************************************************/
348 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
350 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
353 if( !p_input_thread )
356 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
358 vlc_object_release( p_input_thread );
359 return i_track_count;
362 /*****************************************************************************
363 * libvlc_audio_get_track_description : Get the description of available audio tracks
364 *****************************************************************************/
365 libvlc_track_description_t *
366 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
368 return libvlc_get_track_description( p_mi, "audio-es" );
371 /*****************************************************************************
372 * libvlc_audio_get_track : Get the current audio track
373 *****************************************************************************/
374 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
376 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
377 vlc_value_t val_list;
382 if( !p_input_thread )
385 if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
387 vlc_object_release( p_input_thread );
388 libvlc_printerr( "Audio track information not found" );
392 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
393 for( i = 0; i < val_list.p_list->i_count; i++ )
395 if( val_list.p_list->p_values[i].i_int == val.i_int )
401 var_FreeList( &val_list, NULL );
402 vlc_object_release( p_input_thread );
406 /*****************************************************************************
407 * libvlc_audio_set_track : Set the current audio track
408 *****************************************************************************/
409 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
411 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
412 vlc_value_t val_list;
416 if( !p_input_thread )
419 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
420 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
422 libvlc_printerr( "Audio track out of range" );
427 newval = val_list.p_list->p_values[i_track];
428 i_ret = var_Set( p_input_thread, "audio-es", newval );
431 libvlc_printerr( "Audio track out of range" ); /* Race... */
438 var_FreeList( &val_list, NULL );
439 vlc_object_release( p_input_thread );
443 /*****************************************************************************
444 * libvlc_audio_get_channel : Get the current audio channel
445 *****************************************************************************/
446 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
448 audio_output_t *p_aout = GetAOut( mp );
452 int val = var_GetInteger( p_aout, "audio-channels" );
453 vlc_object_release( p_aout );
457 /*****************************************************************************
458 * libvlc_audio_set_channel : Set the current audio channel
459 *****************************************************************************/
460 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
462 audio_output_t *p_aout = GetAOut( mp );
468 if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
470 libvlc_printerr( "Audio channel out of range" );
473 vlc_object_release( p_aout );
477 /*****************************************************************************
478 * libvlc_audio_get_delay : Get the current audio delay
479 *****************************************************************************/
480 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
482 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
484 if( p_input_thread != NULL )
486 val = var_GetTime( p_input_thread, "audio-delay" );
487 vlc_object_release( p_input_thread );
492 /*****************************************************************************
493 * libvlc_audio_set_delay : Set the current audio delay
494 *****************************************************************************/
495 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
497 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
499 if( p_input_thread != NULL )
501 var_SetTime( p_input_thread, "audio-delay", i_delay );
502 vlc_object_release( p_input_thread );