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 *****************************************************************************/
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_media.h>
31 #include <vlc/libvlc_media_player.h>
33 #include <vlc_common.h>
34 #include <vlc_input.h>
37 #include "libvlc_internal.h"
38 #include "media_player_internal.h"
41 * Remember to release the returned aout_instance_t since it is locked at
42 * the end of this function.
44 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
45 libvlc_exception_t *p_exception )
50 aout_instance_t * p_aout = NULL;
52 p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
55 libvlc_exception_raise( p_exception, "No active audio output" );
62 /*****************************************
63 * Get the list of available audio outputs
64 *****************************************/
65 libvlc_audio_output_t *
66 libvlc_audio_output_list_get( libvlc_instance_t *p_instance,
67 libvlc_exception_t *p_e )
69 VLC_UNUSED( p_instance );
70 libvlc_audio_output_t *p_list = NULL,
73 module_t **module_list = module_list_get( NULL );
75 for (size_t i = 0; module_list[i]; i++)
77 module_t *p_module = module_list[i];
79 if( module_provides( p_module, "audio output" ) )
83 p_actual = ( libvlc_audio_output_t * )
84 malloc( sizeof( libvlc_audio_output_t ) );
85 if( p_actual == NULL )
87 libvlc_exception_raise( p_e, "Not enough memory" );
88 libvlc_audio_output_list_release( p_list );
89 module_list_free( module_list );
95 p_previous = p_actual;
98 p_actual->psz_name = strdup( module_get_object( p_module ) );
99 p_actual->psz_description = strdup( module_get_name( p_module, true ) );
100 p_actual->p_next = NULL;
101 if( p_previous != p_actual ) /* not first item */
102 p_previous->p_next = p_actual;
103 p_previous = p_actual;
104 p_actual = p_actual->p_next;
108 module_list_free( module_list );
113 /********************************************
114 * Free the list of available audio outputs
115 ***********************************************/
116 void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
118 libvlc_audio_output_t *p_actual, *p_before;
123 free( p_actual->psz_name );
124 free( p_actual->psz_description );
126 p_actual = p_before->p_next;
132 /***********************
133 * Set the audio output.
134 ***********************/
135 int libvlc_audio_output_set( libvlc_instance_t *p_instance,
136 const char *psz_name )
138 if( module_exists( psz_name ) )
140 config_PutPsz( p_instance->p_libvlc_int, "aout", psz_name );
147 /****************************
148 * Get count of devices.
149 *****************************/
150 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
151 const char *psz_audio_output )
153 char *psz_config_name;
154 if( !psz_audio_output )
156 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
159 module_config_t *p_module_config = config_FindConfig(
160 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
162 if( p_module_config && p_module_config->pf_update_list )
165 val.psz_string = strdup( p_module_config->value.psz );
167 p_module_config->pf_update_list(
168 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
169 free( val.psz_string );
170 free( psz_config_name );
172 return p_module_config->i_list;
175 free( psz_config_name );
179 /********************************
180 * Get long name of device
181 *********************************/
182 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
183 const char *psz_audio_output,
186 char *psz_config_name;
187 if( !psz_audio_output )
189 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
192 module_config_t *p_module_config = config_FindConfig(
193 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
195 if( p_module_config )
197 // refresh if there arent devices
198 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
201 val.psz_string = strdup( p_module_config->value.psz );
203 p_module_config->pf_update_list(
204 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
205 free( val.psz_string );
207 free( psz_config_name );
209 if( i_device >= 0 && i_device < p_module_config->i_list )
211 if( p_module_config->ppsz_list_text[i_device] )
212 return strdup( p_module_config->ppsz_list_text[i_device] );
214 return strdup( p_module_config->ppsz_list[i_device] );
218 free( psz_config_name );
222 /********************************
223 * Get id name of device
224 *********************************/
225 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
226 const char *psz_audio_output,
229 char *psz_config_name;
230 if( !psz_audio_output )
232 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
235 module_config_t *p_module_config = config_FindConfig(
236 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
238 if( p_module_config )
240 // refresh if there arent devices
241 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
244 val.psz_string = strdup( p_module_config->value.psz );
246 p_module_config->pf_update_list(
247 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
248 free( val.psz_string );
250 free( psz_config_name );
252 if( i_device >= 0 && i_device < p_module_config->i_list )
253 return strdup( p_module_config->ppsz_list[i_device] );
257 free( psz_config_name );
261 /*****************************
262 * Set device for using
263 *****************************/
264 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 );
286 int i_device_type = var_GetInteger( p_aout, "audio-device" );
287 vlc_object_release( p_aout );
288 return i_device_type;
290 libvlc_exception_raise( p_e, "Unable to get audio output" );
291 return libvlc_AudioOutputDevice_Error;
294 /*****************************************************************************
295 * libvlc_audio_output_set_device_type : Set the audio device type
296 *****************************************************************************/
297 void libvlc_audio_output_set_device_type( libvlc_instance_t *p_instance,
299 libvlc_exception_t *p_e )
301 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
304 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
305 libvlc_exception_raise( p_e, "Failed setting audio device" );
306 vlc_object_release( p_aout );
310 /*****************************************************************************
311 * libvlc_audio_get_mute : Get the volume state, true if muted
312 *****************************************************************************/
313 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
314 libvlc_exception_t *p_e )
318 aout_ToggleMute( p_instance->p_libvlc_int, NULL );
321 int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
322 libvlc_exception_t *p_e )
324 return (libvlc_audio_get_volume(p_instance, p_e) == 0);
327 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
328 libvlc_exception_t *p_e )
330 if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
332 aout_ToggleMute( p_instance->p_libvlc_int, NULL );
336 /*****************************************************************************
337 * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
338 *****************************************************************************/
339 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
340 libvlc_exception_t *p_e )
344 audio_volume_t i_volume;
346 aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
348 return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
352 /*****************************************************************************
353 * libvlc_audio_set_volume : Set the current volume
354 *****************************************************************************/
355 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
356 libvlc_exception_t *p_e )
358 if( i_volume >= 0 && i_volume <= 200 )
360 i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
362 aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
366 libvlc_exception_raise( p_e, "Volume out of range" );
370 /*****************************************************************************
371 * libvlc_audio_get_track_count : Get the number of available audio tracks
372 *****************************************************************************/
373 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
374 libvlc_exception_t *p_e )
376 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
379 if( !p_input_thread )
382 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
384 vlc_object_release( p_input_thread );
385 return i_track_count;
388 /*****************************************************************************
389 * libvlc_audio_get_track_description : Get the description of available audio tracks
390 *****************************************************************************/
391 libvlc_track_description_t *
392 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
393 libvlc_exception_t *p_e )
395 return libvlc_get_track_description( p_mi, "audio-es", p_e);
398 /*****************************************************************************
399 * libvlc_audio_get_track : Get the current audio track
400 *****************************************************************************/
401 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
402 libvlc_exception_t *p_e )
404 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
405 vlc_value_t val_list;
411 if( !p_input_thread )
414 i_ret = var_Get( p_input_thread, "audio-es", &val );
417 libvlc_exception_raise( p_e, "Getting Audio track information failed" );
418 vlc_object_release( p_input_thread );
422 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
423 for( i = 0; i < val_list.p_list->i_count; i++ )
425 if( val_list.p_list->p_values[i].i_int == val.i_int )
431 var_FreeList( &val_list, NULL );
432 vlc_object_release( p_input_thread );
436 /*****************************************************************************
437 * libvlc_audio_set_track : Set the current audio track
438 *****************************************************************************/
439 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
440 libvlc_exception_t *p_e )
442 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
443 vlc_value_t val_list;
447 if( !p_input_thread )
450 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
451 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
453 libvlc_exception_raise( p_e, "Audio track out of range" );
457 newval = val_list.p_list->p_values[i_track];
458 i_ret = var_Set( p_input_thread, "audio-es", newval );
460 libvlc_exception_raise( p_e, "Setting audio track failed" );
463 var_FreeList( &val_list, NULL );
464 vlc_object_release( p_input_thread );
467 /*****************************************************************************
468 * libvlc_audio_get_channel : Get the current audio channel
469 *****************************************************************************/
470 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
471 libvlc_exception_t *p_e )
473 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
478 var_Get( p_aout, "audio-channels", &val );
479 vlc_object_release( p_aout );
482 libvlc_exception_raise( p_e, "Unable to get audio output" );
483 return libvlc_AudioChannel_Error;
486 /*****************************************************************************
487 * libvlc_audio_set_channel : Set the current audio channel
488 *****************************************************************************/
489 void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
491 libvlc_exception_t *p_e )
493 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
499 if( var_Set( p_aout, "audio-channels", val ) < 0 )
500 libvlc_exception_raise( p_e, "Failed setting audio channel" );
502 vlc_object_release( p_aout );