1 /*****************************************************************************
2 * libvlc_audio.c: New libvlc audio control API
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include <vlc/libvlc.h>
26 #include <vlc/libvlc_media.h>
27 #include <vlc/libvlc_media_player.h>
29 #include <vlc_common.h>
30 #include <vlc_input.h>
33 #include "libvlc_internal.h"
34 #include "media_player_internal.h"
37 * Remember to release the returned aout_instance_t since it is locked at
38 * the end of this function.
40 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
41 libvlc_exception_t *p_exception )
46 aout_instance_t * p_aout = NULL;
48 p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
51 libvlc_exception_raise( p_exception, "No active audio output" );
58 /*****************************************
59 * Get the list of available audio outputs
60 *****************************************/
61 VLC_PUBLIC_API libvlc_audio_output_t *
62 libvlc_audio_output_list_get( libvlc_instance_t *p_instance,
63 libvlc_exception_t *p_e )
65 VLC_UNUSED( p_instance );
66 libvlc_audio_output_t *p_list = NULL,
69 module_t **module_list = module_list_get( NULL );
71 for (size_t i = 0; module_list[i]; i++)
73 module_t *p_module = module_list[i];
75 if( module_provides( p_module, "audio output" ) )
79 p_actual = ( libvlc_audio_output_t * )
80 malloc( sizeof( libvlc_audio_output_t ) );
81 if( p_actual == NULL )
83 libvlc_exception_raise( p_e, "Not enough memory" );
84 libvlc_audio_output_list_release( p_list );
85 module_list_free( module_list );
91 p_previous = p_actual;
94 p_actual->psz_name = strdup( module_get_object( p_module ) );
95 p_actual->psz_description = strdup( module_get_name( p_module, true ) );
96 p_actual->p_next = NULL;
97 if( p_previous != p_actual ) /* not first item */
98 p_previous->p_next = p_actual;
99 p_previous = p_actual;
100 p_actual = p_actual->p_next;
104 module_list_free( module_list );
109 /********************************************
110 * Free the list of available audio outputs
111 ***********************************************/
112 VLC_PUBLIC_API void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
114 libvlc_audio_output_t *p_actual, *p_before;
119 free( p_actual->psz_name );
120 free( p_actual->psz_description );
122 p_actual = p_before->p_next;
128 /***********************
129 * Set the audio output.
130 ***********************/
131 VLC_PUBLIC_API int libvlc_audio_output_set( libvlc_instance_t *p_instance,
132 const char *psz_name )
134 if( module_exists( psz_name ) )
136 config_PutPsz( p_instance->p_libvlc_int, "aout", psz_name );
143 /****************************
144 * Get count of devices.
145 *****************************/
146 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
147 const char *psz_audio_output )
149 char *psz_config_name = NULL;
150 if( !psz_audio_output )
152 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
155 module_config_t *p_module_config = config_FindConfig(
156 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
158 if( p_module_config && p_module_config->pf_update_list )
161 val.psz_string = strdup( p_module_config->value.psz );
163 p_module_config->pf_update_list(
164 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
165 free( val.psz_string );
166 free( psz_config_name );
168 return p_module_config->i_list;
171 free( psz_config_name );
177 /********************************
178 * Get long name of device
179 *********************************/
180 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
181 const char *psz_audio_output,
184 char *psz_config_name = NULL;
185 if( !psz_audio_output )
187 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
190 module_config_t *p_module_config = config_FindConfig(
191 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
193 if( p_module_config )
195 // refresh if there arent devices
196 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
199 val.psz_string = strdup( p_module_config->value.psz );
201 p_module_config->pf_update_list(
202 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
203 free( val.psz_string );
205 free( psz_config_name );
207 if( i_device >= 0 && i_device < p_module_config->i_list )
209 if( p_module_config->ppsz_list_text[i_device] )
210 return strdup( p_module_config->ppsz_list_text[i_device] );
212 return strdup( p_module_config->ppsz_list[i_device] );
216 free( psz_config_name );
221 /********************************
222 * Get id name of device
223 *********************************/
224 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
225 const char *psz_audio_output,
228 char *psz_config_name = NULL;
229 if( !psz_audio_output )
231 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
234 module_config_t *p_module_config = config_FindConfig(
235 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
237 if( p_module_config )
239 // refresh if there arent devices
240 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
243 val.psz_string = strdup( p_module_config->value.psz );
245 p_module_config->pf_update_list(
246 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
247 free( val.psz_string );
249 free( psz_config_name );
251 if( i_device >= 0 && i_device < p_module_config->i_list )
252 return strdup( p_module_config->ppsz_list[i_device] );
256 free( psz_config_name );
261 /*****************************
262 * Set device for using
263 *****************************/
264 VLC_PUBLIC_API void libvlc_audio_output_device_set( libvlc_instance_t *p_instance,
265 const char *psz_audio_output,
266 const char *psz_device_id )
268 char *psz_config_name = NULL;
269 if( !psz_audio_output || !psz_device_id )
271 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
273 config_PutPsz( p_instance->p_libvlc_int, psz_config_name, psz_device_id );
274 free( psz_config_name );
277 /*****************************************************************************
278 * libvlc_audio_output_get_device_type : Get the current audio device type
279 *****************************************************************************/
280 int libvlc_audio_output_get_device_type( libvlc_instance_t *p_instance,
281 libvlc_exception_t *p_e )
283 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
288 var_Get( p_aout, "audio-device", &val );
289 vlc_object_release( p_aout );
292 libvlc_exception_raise( p_e, "Unable to get audio output" );
293 return libvlc_AudioOutputDevice_Error;
296 /*****************************************************************************
297 * libvlc_audio_output_set_device_type : Set the audio device type
298 *****************************************************************************/
299 void libvlc_audio_output_set_device_type( libvlc_instance_t *p_instance,
301 libvlc_exception_t *p_e )
303 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
306 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
307 libvlc_exception_raise( p_e, "Failed setting audio device" );
308 vlc_object_release( p_aout );
312 /*****************************************************************************
313 * libvlc_audio_get_mute : Get the volume state, true if muted
314 *****************************************************************************/
315 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
316 libvlc_exception_t *p_e )
320 aout_VolumeMute( p_instance->p_libvlc_int, NULL );
323 int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
324 libvlc_exception_t *p_e )
326 return (libvlc_audio_get_volume(p_instance, p_e) == 0);
329 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
330 libvlc_exception_t *p_e )
332 if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
334 aout_VolumeMute( p_instance->p_libvlc_int, NULL );
338 /*****************************************************************************
339 * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
340 *****************************************************************************/
341 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
342 libvlc_exception_t *p_e )
346 audio_volume_t i_volume;
348 aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
350 return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
354 /*****************************************************************************
355 * libvlc_audio_set_volume : Set the current volume
356 *****************************************************************************/
357 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
358 libvlc_exception_t *p_e )
360 if( i_volume >= 0 && i_volume <= 200 )
362 i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
364 aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
368 libvlc_exception_raise( p_e, "Volume out of range" );
372 /*****************************************************************************
373 * libvlc_audio_get_track_count : Get the number of available audio tracks
374 *****************************************************************************/
375 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
376 libvlc_exception_t *p_e )
378 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
381 if( !p_input_thread )
384 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
386 vlc_object_release( p_input_thread );
387 return i_track_count;
390 /*****************************************************************************
391 * libvlc_audio_get_track_description : Get the description of available audio tracks
392 *****************************************************************************/
393 libvlc_track_description_t *
394 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
395 libvlc_exception_t *p_e )
397 return libvlc_get_track_description( p_mi, "audio-es", p_e);
400 /*****************************************************************************
401 * libvlc_audio_get_track : Get the current audio track
402 *****************************************************************************/
403 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
404 libvlc_exception_t *p_e )
406 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
407 vlc_value_t val_list;
413 if( !p_input_thread )
416 i_ret = var_Get( p_input_thread, "audio-es", &val );
419 libvlc_exception_raise( p_e, "Getting Audio track information failed" );
420 vlc_object_release( p_input_thread );
424 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
425 for( i = 0; i < val_list.p_list->i_count; i++ )
427 if( val_list.p_list->p_values[i].i_int == val.i_int )
433 var_FreeList( &val_list, NULL );
434 vlc_object_release( p_input_thread );
438 /*****************************************************************************
439 * libvlc_audio_set_track : Set the current audio track
440 *****************************************************************************/
441 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
442 libvlc_exception_t *p_e )
444 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
445 vlc_value_t val_list;
449 if( !p_input_thread )
452 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
453 if( (i_track < 0) && (i_track > val_list.p_list->i_count) )
455 libvlc_exception_raise( p_e, "Audio track out of range" );
459 newval = val_list.p_list->p_values[i_track];
460 i_ret = var_Set( p_input_thread, "audio-es", newval );
462 libvlc_exception_raise( p_e, "Setting audio track failed" );
465 var_FreeList( &val_list, NULL );
466 vlc_object_release( p_input_thread );
469 /*****************************************************************************
470 * libvlc_audio_get_channel : Get the current audio channel
471 *****************************************************************************/
472 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
473 libvlc_exception_t *p_e )
475 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
480 var_Get( p_aout, "audio-channels", &val );
481 vlc_object_release( p_aout );
484 libvlc_exception_raise( p_e, "Unable to get audio output" );
485 return libvlc_AudioChannel_Error;
488 /*****************************************************************************
489 * libvlc_audio_set_channel : Set the current audio channel
490 *****************************************************************************/
491 void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
493 libvlc_exception_t *p_e )
495 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
502 i_ret = var_Set( p_aout, "audio-channels", val );
504 libvlc_exception_raise( p_e, "Failed setting audio channel" );
506 vlc_object_release( p_aout );