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 *****************************************************************************/
31 #include <vlc/libvlc.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_media_player.h>
35 #include <vlc_common.h>
36 #include <vlc_input.h>
39 #include "libvlc_internal.h"
40 #include "media_player_internal.h"
43 * Remember to release the returned aout_instance_t since it is locked at
44 * the end of this function.
46 static aout_instance_t *GetAOut( libvlc_media_player_t *mp )
50 input_thread_t *p_input = libvlc_get_input_thread( mp );
54 aout_instance_t * p_aout = input_GetAout( p_input );
55 vlc_object_release( p_input );
57 libvlc_printerr( "No active audio output" );
61 /*****************************************
62 * Get the list of available audio outputs
63 *****************************************/
64 libvlc_audio_output_t *
65 libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
67 VLC_UNUSED( p_instance );
68 libvlc_audio_output_t *p_list = NULL,
71 module_t **module_list = module_list_get( NULL );
73 for (size_t i = 0; module_list[i]; i++)
75 module_t *p_module = module_list[i];
77 if( module_provides( p_module, "audio output" ) )
81 p_actual = ( libvlc_audio_output_t * )
82 malloc( sizeof( libvlc_audio_output_t ) );
83 if( p_actual == NULL )
85 libvlc_printerr( "Not enough memory" );
86 libvlc_audio_output_list_release( p_list );
87 module_list_free( module_list );
93 p_previous = p_actual;
96 p_actual->psz_name = strdup( module_get_object( p_module ) );
97 p_actual->psz_description = strdup( module_get_name( p_module, true ) );
98 p_actual->p_next = NULL;
99 if( p_previous != p_actual ) /* not first item */
100 p_previous->p_next = p_actual;
101 p_previous = p_actual;
102 p_actual = p_actual->p_next;
106 module_list_free( module_list );
111 /********************************************
112 * Free the list of available audio outputs
113 ***********************************************/
114 void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
116 libvlc_audio_output_t *p_actual, *p_before;
121 free( p_actual->psz_name );
122 free( p_actual->psz_description );
124 p_actual = p_before->p_next;
130 /***********************
131 * Set the audio output.
132 ***********************/
133 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
135 if( !module_exists( psz_name ) )
137 var_SetString( mp, "aout", psz_name );
141 /****************************
142 * Get count of devices.
143 *****************************/
144 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
145 const char *psz_audio_output )
147 char *psz_config_name;
148 if( !psz_audio_output )
150 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
153 module_config_t *p_module_config = config_FindConfig(
154 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
156 if( p_module_config && p_module_config->pf_update_list )
159 val.psz_string = strdup( p_module_config->value.psz );
161 p_module_config->pf_update_list(
162 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
163 free( val.psz_string );
164 free( psz_config_name );
166 return p_module_config->i_list;
169 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;
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 );
216 /********************************
217 * Get id name of device
218 *********************************/
219 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
220 const char *psz_audio_output,
223 char *psz_config_name;
224 if( !psz_audio_output )
226 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
229 module_config_t *p_module_config = config_FindConfig(
230 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
232 if( p_module_config )
234 // refresh if there arent devices
235 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
238 val.psz_string = strdup( p_module_config->value.psz );
240 p_module_config->pf_update_list(
241 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
242 free( val.psz_string );
244 free( psz_config_name );
246 if( i_device >= 0 && i_device < p_module_config->i_list )
247 return strdup( p_module_config->ppsz_list[i_device] );
251 free( psz_config_name );
255 /*****************************
256 * Set device for using
257 *****************************/
258 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
259 const char *psz_audio_output,
260 const char *psz_device_id )
262 char *psz_config_name;
263 if( !psz_audio_output || !psz_device_id )
265 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
267 if( !var_Type( mp, psz_audio_output ) )
268 /* Don't recreate the same variable over and over and over... */
269 var_Create( mp, psz_audio_output, VLC_VAR_STRING );
270 var_SetString( mp, psz_config_name, psz_device_id );
271 free( psz_config_name );
274 /*****************************************************************************
275 * libvlc_audio_output_get_device_type : Get the current audio device type
276 *****************************************************************************/
277 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
279 aout_instance_t *p_aout = GetAOut( mp );
282 int i_device_type = var_GetInteger( p_aout, "audio-device" );
283 vlc_object_release( p_aout );
284 return i_device_type;
286 return libvlc_AudioOutputDevice_Error;
289 /*****************************************************************************
290 * libvlc_audio_output_set_device_type : Set the audio device type
291 *****************************************************************************/
292 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
295 aout_instance_t *p_aout = GetAOut( mp );
298 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
299 libvlc_printerr( "Error setting audio device" );
300 vlc_object_release( p_aout );
303 /*****************************************************************************
304 * libvlc_audio_get_mute : Get the volume state, true if muted
305 *****************************************************************************/
306 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
308 aout_ToggleMute( mp, NULL );
311 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
313 return (libvlc_audio_get_volume(mp) == 0);
316 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
318 aout_SetMute( VLC_OBJECT(mp), NULL, !!mute );
321 /*****************************************************************************
322 * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
323 *****************************************************************************/
324 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
326 audio_volume_t i_volume;
328 aout_VolumeGet( mp, &i_volume );
330 return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
334 /*****************************************************************************
335 * libvlc_audio_set_volume : Set the current volume
336 *****************************************************************************/
337 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int i_volume )
339 if( i_volume < 0 || i_volume > 200 )
341 libvlc_printerr( "Volume out of range" );
345 i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
346 aout_VolumeSet( mp, i_volume );
350 /*****************************************************************************
351 * libvlc_audio_get_track_count : Get the number of available audio tracks
352 *****************************************************************************/
353 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
355 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
358 if( !p_input_thread )
361 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
363 vlc_object_release( p_input_thread );
364 return i_track_count;
367 /*****************************************************************************
368 * libvlc_audio_get_track_description : Get the description of available audio tracks
369 *****************************************************************************/
370 libvlc_track_description_t *
371 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
373 return libvlc_get_track_description( p_mi, "audio-es" );
376 /*****************************************************************************
377 * libvlc_audio_get_track : Get the current audio track
378 *****************************************************************************/
379 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
381 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
382 vlc_value_t val_list;
387 if( !p_input_thread )
390 if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
392 vlc_object_release( p_input_thread );
393 libvlc_printerr( "Audio track information not found" );
397 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
398 for( i = 0; i < val_list.p_list->i_count; i++ )
400 if( val_list.p_list->p_values[i].i_int == val.i_int )
406 var_FreeList( &val_list, NULL );
407 vlc_object_release( p_input_thread );
411 /*****************************************************************************
412 * libvlc_audio_set_track : Set the current audio track
413 *****************************************************************************/
414 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
416 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
417 vlc_value_t val_list;
421 if( !p_input_thread )
424 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
425 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
427 libvlc_printerr( "Audio track out of range" );
432 newval = val_list.p_list->p_values[i_track];
433 i_ret = var_Set( p_input_thread, "audio-es", newval );
436 libvlc_printerr( "Audio track out of range" ); /* Race... */
443 var_FreeList( &val_list, NULL );
444 vlc_object_release( p_input_thread );
448 /*****************************************************************************
449 * libvlc_audio_get_channel : Get the current audio channel
450 *****************************************************************************/
451 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
453 aout_instance_t *p_aout = GetAOut( mp );
457 int val = var_GetInteger( p_aout, "audio-channels" );
458 vlc_object_release( p_aout );
462 /*****************************************************************************
463 * libvlc_audio_set_channel : Set the current audio channel
464 *****************************************************************************/
465 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
467 aout_instance_t *p_aout = GetAOut( mp );
473 if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
475 libvlc_printerr( "Audio channel out of range" );
478 vlc_object_release( p_aout );