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 "libvlc_internal.h"
26 #include <vlc/libvlc.h>
28 #include <vlc_input.h>
33 * Remember to release the returned aout_instance_t since it is locked at
34 * the end of this function.
36 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
37 libvlc_exception_t *p_exception )
42 aout_instance_t * p_aout = NULL;
44 p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
47 libvlc_exception_raise( p_exception, "No active audio output" );
54 /*****************************************
55 * Get the list of available audio outputs
56 *****************************************/
57 VLC_PUBLIC_API libvlc_audio_output_t *
58 libvlc_audio_output_list_get( libvlc_instance_t *p_instance,
59 libvlc_exception_t *p_e )
61 VLC_UNUSED( p_instance );
62 libvlc_audio_output_t *p_list = NULL,
65 module_t **module_list = module_list_get( NULL );
67 for (size_t i = 0; module_list[i]; i++)
69 module_t *p_module = module_list[i];
71 if( module_provides( p_module, "audio output" ) )
75 p_actual = ( libvlc_audio_output_t * )
76 malloc( sizeof( libvlc_audio_output_t ) );
77 if( p_actual == NULL )
79 libvlc_exception_raise( p_e, "Not enough memory" );
80 libvlc_audio_output_list_release( p_list );
81 module_list_free( module_list );
87 p_previous = p_actual;
90 p_actual->psz_name = strdup( module_get_object( p_module ) );
91 p_actual->psz_description = strdup( module_get_name( p_module, true ) );
92 p_actual->p_next = NULL;
93 if( p_previous != p_actual ) /* not first item */
94 p_previous->p_next = p_actual;
95 p_previous = p_actual;
96 p_actual = p_actual->p_next;
100 module_list_free( module_list );
105 /********************************************
106 * Free the list of available audio outputs
107 ***********************************************/
108 VLC_PUBLIC_API void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
110 libvlc_audio_output_t *p_actual, *p_before;
115 free( p_actual->psz_name );
116 free( p_actual->psz_description );
118 p_actual = p_before->p_next;
124 /***********************
125 * Set the audio output.
126 ***********************/
127 VLC_PUBLIC_API int libvlc_audio_output_set( libvlc_instance_t *p_instance,
128 const char *psz_name )
130 if( module_exists( psz_name ) )
132 config_PutPsz( p_instance->p_libvlc_int, "aout", psz_name );
139 /****************************
140 * Get count of devices.
141 *****************************/
142 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
143 const char *psz_audio_output )
145 char *psz_config_name = NULL;
146 if( !psz_audio_output )
148 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
151 module_config_t *p_module_config = config_FindConfig(
152 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
154 if( p_module_config && p_module_config->pf_update_list )
157 val.psz_string = strdup( p_module_config->value.psz );
159 p_module_config->pf_update_list(
160 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
161 free( val.psz_string );
162 free( psz_config_name );
164 return p_module_config->i_list;
167 free( psz_config_name );
173 /********************************
174 * Get long name of device
175 *********************************/
176 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
177 const char *psz_audio_output,
180 char *psz_config_name = NULL;
181 if( !psz_audio_output )
183 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
186 module_config_t *p_module_config = config_FindConfig(
187 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
189 if( p_module_config )
191 // refresh if there arent devices
192 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
195 val.psz_string = strdup( p_module_config->value.psz );
197 p_module_config->pf_update_list(
198 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
199 free( val.psz_string );
201 free( psz_config_name );
203 if( i_device >= 0 && i_device < p_module_config->i_list )
205 if( p_module_config->ppsz_list_text[i_device] )
206 return strdup( p_module_config->ppsz_list_text[i_device] );
208 return strdup( p_module_config->ppsz_list[i_device] );
212 free( psz_config_name );
217 /********************************
218 * Get id name of device
219 *********************************/
220 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
221 const char *psz_audio_output,
224 char *psz_config_name = NULL;
225 if( !psz_audio_output )
227 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
230 module_config_t *p_module_config = config_FindConfig(
231 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
233 if( p_module_config )
235 // refresh if there arent devices
236 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
239 val.psz_string = strdup( p_module_config->value.psz );
241 p_module_config->pf_update_list(
242 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
243 free( val.psz_string );
245 free( psz_config_name );
247 if( i_device >= 0 && i_device < p_module_config->i_list )
248 return strdup( p_module_config->ppsz_list[i_device] );
252 free( psz_config_name );
257 /*****************************
258 * Set device for using
259 *****************************/
260 VLC_PUBLIC_API void libvlc_audio_output_device_set( libvlc_instance_t *p_instance,
261 const char *psz_audio_output,
262 const char *psz_device_id )
264 char *psz_config_name = NULL;
265 if( !psz_audio_output || !psz_device_id )
267 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
269 config_PutPsz( p_instance->p_libvlc_int, psz_config_name, psz_device_id );
270 free( psz_config_name );
273 /*****************************************************************************
274 * libvlc_audio_output_get_device_type : Get the current audio device type
275 *****************************************************************************/
276 int libvlc_audio_output_get_device_type( libvlc_instance_t *p_instance,
277 libvlc_exception_t *p_e )
279 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
284 var_Get( p_aout, "audio-device", &val );
285 vlc_object_release( p_aout );
288 libvlc_exception_raise( p_e, "Unable to get audio output" );
289 return libvlc_AudioOutputDevice_Error;
292 /*****************************************************************************
293 * libvlc_audio_output_set_device_type : Set the audio device type
294 *****************************************************************************/
295 void libvlc_audio_output_set_device_type( libvlc_instance_t *p_instance,
297 libvlc_exception_t *p_e )
299 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
302 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
303 libvlc_exception_raise( p_e, "Failed setting audio device" );
304 vlc_object_release( p_aout );
308 /*****************************************************************************
309 * libvlc_audio_get_mute : Get the volume state, true if muted
310 *****************************************************************************/
311 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
312 libvlc_exception_t *p_e )
316 aout_VolumeMute( p_instance->p_libvlc_int, NULL );
319 int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
320 libvlc_exception_t *p_e )
322 return (libvlc_audio_get_volume(p_instance, p_e) == 0);
325 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
326 libvlc_exception_t *p_e )
328 if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
330 aout_VolumeMute( p_instance->p_libvlc_int, NULL );
334 /*****************************************************************************
335 * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
336 *****************************************************************************/
337 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
338 libvlc_exception_t *p_e )
342 audio_volume_t i_volume;
344 aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
346 return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
350 /*****************************************************************************
351 * libvlc_audio_set_volume : Set the current volume
352 *****************************************************************************/
353 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
354 libvlc_exception_t *p_e )
356 if( i_volume >= 0 && i_volume <= 200 )
358 i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
360 aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
364 libvlc_exception_raise( p_e, "Volume out of range" );
368 /*****************************************************************************
369 * libvlc_audio_get_track_count : Get the number of available audio tracks
370 *****************************************************************************/
371 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
372 libvlc_exception_t *p_e )
374 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
377 if( !p_input_thread )
380 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
382 vlc_object_release( p_input_thread );
383 return i_track_count;
386 /*****************************************************************************
387 * libvlc_audio_get_track_description : Get the description of available audio tracks
388 *****************************************************************************/
389 libvlc_track_description_t *
390 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
391 libvlc_exception_t *p_e )
393 return libvlc_get_track_description( p_mi, "audio-es", p_e);
396 /*****************************************************************************
397 * libvlc_audio_get_track : Get the current audio track
398 *****************************************************************************/
399 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
400 libvlc_exception_t *p_e )
402 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
403 vlc_value_t val_list;
409 if( !p_input_thread )
412 i_ret = var_Get( p_input_thread, "audio-es", &val );
415 libvlc_exception_raise( p_e, "Getting Audio track information failed" );
416 vlc_object_release( p_input_thread );
420 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
421 for( i = 0; i < val_list.p_list->i_count; i++ )
423 if( val_list.p_list->p_values[i].i_int == val.i_int )
429 var_FreeList( &val_list, NULL );
430 vlc_object_release( p_input_thread );
434 /*****************************************************************************
435 * libvlc_audio_set_track : Set the current audio track
436 *****************************************************************************/
437 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
438 libvlc_exception_t *p_e )
440 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
441 vlc_value_t val_list;
445 if( !p_input_thread )
448 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
449 if( (i_track < 0) && (i_track > val_list.p_list->i_count) )
451 libvlc_exception_raise( p_e, "Audio track out of range" );
455 newval = val_list.p_list->p_values[i_track];
456 i_ret = var_Set( p_input_thread, "audio-es", newval );
458 libvlc_exception_raise( p_e, "Setting audio track failed" );
461 var_FreeList( &val_list, NULL );
462 vlc_object_release( p_input_thread );
465 /*****************************************************************************
466 * libvlc_audio_get_channel : Get the current audio channel
467 *****************************************************************************/
468 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
469 libvlc_exception_t *p_e )
471 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
476 var_Get( p_aout, "audio-channels", &val );
477 vlc_object_release( p_aout );
480 libvlc_exception_raise( p_e, "Unable to get audio output" );
481 return libvlc_AudioChannel_Error;
484 /*****************************************************************************
485 * libvlc_audio_set_channel : Set the current audio channel
486 *****************************************************************************/
487 void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
489 libvlc_exception_t *p_e )
491 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
498 i_ret = var_Set( p_aout, "audio-channels", val );
500 libvlc_exception_raise( p_e, "Failed setting audio channel" );
502 vlc_object_release( p_aout );