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 *list = NULL;
72 module_t **module_list = module_list_get( NULL );
74 for (size_t i = 0; module_list[i]; i++)
76 module_t *module = module_list[i];
78 if( !module_provides( module, "audio output" ) )
81 libvlc_audio_output_t *item = malloc( sizeof( *item ) );
82 if( unlikely(item == NULL) )
85 libvlc_printerr( "Not enough memory" );
86 libvlc_audio_output_list_release( list );
91 item->psz_name = strdup( module_get_object( module ) );
92 item->psz_description = strdup( module_get_name( module, true ) );
93 if( unlikely(item->psz_name == NULL || item->psz_description == NULL) )
101 module_list_free( module_list );
106 /********************************************
107 * Free the list of available audio outputs
108 ***********************************************/
109 void libvlc_audio_output_list_release( libvlc_audio_output_t *list )
111 while( list != NULL )
113 libvlc_audio_output_t *next = list->p_next;
115 free( list->psz_name );
116 free( list->psz_description );
123 /***********************
124 * Set the audio output.
125 ***********************/
126 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
130 if( !module_exists( psz_name )
131 || asprintf( &value, "%s,none", psz_name ) == -1 )
133 var_SetString( mp, "aout", value );
138 /****************************
139 * Get count of devices.
140 *****************************/
141 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
142 const char *psz_audio_output )
144 char *psz_config_name;
145 if( !psz_audio_output )
147 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
150 module_config_t *p_module_config = config_FindConfig(
151 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
153 if( p_module_config && p_module_config->pf_update_list )
156 val.psz_string = strdup( p_module_config->value.psz );
158 p_module_config->pf_update_list(
159 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
160 free( val.psz_string );
161 free( psz_config_name );
163 return p_module_config->i_list;
166 free( psz_config_name );
170 /********************************
171 * Get long name of device
172 *********************************/
173 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
174 const char *psz_audio_output,
177 char *psz_config_name;
178 if( !psz_audio_output )
180 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
183 module_config_t *p_module_config = config_FindConfig(
184 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
186 if( p_module_config )
188 // refresh if there arent devices
189 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
192 val.psz_string = strdup( p_module_config->value.psz );
194 p_module_config->pf_update_list(
195 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
196 free( val.psz_string );
199 if( i_device >= 0 && i_device < p_module_config->i_list )
201 free( psz_config_name );
203 if( p_module_config->ppsz_list_text[i_device] )
204 return strdup( p_module_config->ppsz_list_text[i_device] );
206 return strdup( p_module_config->ppsz_list[i_device] );
210 free( psz_config_name );
214 /********************************
215 * Get id name of device
216 *********************************/
217 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
218 const char *psz_audio_output,
221 char *psz_config_name;
222 if( !psz_audio_output )
224 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
227 module_config_t *p_module_config = config_FindConfig(
228 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
230 if( p_module_config )
232 // refresh if there arent devices
233 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
236 val.psz_string = strdup( p_module_config->value.psz );
238 p_module_config->pf_update_list(
239 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
240 free( val.psz_string );
243 if( i_device >= 0 && i_device < p_module_config->i_list )
245 free( psz_config_name );
246 return strdup( p_module_config->ppsz_list[i_device] );
250 free( psz_config_name );
254 /*****************************
255 * Set device for using
256 *****************************/
257 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
258 const char *psz_audio_output,
259 const char *psz_device_id )
261 char *psz_config_name;
262 if( !psz_audio_output || !psz_device_id )
264 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
266 if( !var_Type( mp, psz_config_name ) )
267 /* Don't recreate the same variable over and over and over... */
268 var_Create( mp, psz_config_name, VLC_VAR_STRING );
269 var_SetString( mp, 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_media_player_t *mp )
278 audio_output_t *p_aout = GetAOut( mp );
281 int i_device_type = var_GetInteger( p_aout, "audio-device" );
282 vlc_object_release( p_aout );
283 return i_device_type;
285 return libvlc_AudioOutputDevice_Error;
288 /*****************************************************************************
289 * libvlc_audio_output_set_device_type : Set the audio device type
290 *****************************************************************************/
291 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
294 audio_output_t *p_aout = GetAOut( mp );
297 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
298 libvlc_printerr( "Error setting audio device" );
299 vlc_object_release( p_aout );
302 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
304 aout_MuteToggle( mp );
307 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
309 return aout_MuteGet( mp );
312 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
314 aout_MuteSet( VLC_OBJECT(mp), mute != 0 );
317 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
319 float vol = aout_VolumeGet( mp );
320 return ( vol >= 0.f ) ? lroundf( vol * 100.f ) : -1;
323 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
325 float vol = volume / 100.f;
328 libvlc_printerr( "Volume out of range" );
331 aout_VolumeSet (mp, vol);
335 /*****************************************************************************
336 * libvlc_audio_get_track_count : Get the number of available audio tracks
337 *****************************************************************************/
338 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
340 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
343 if( !p_input_thread )
346 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
348 vlc_object_release( p_input_thread );
349 return i_track_count;
352 /*****************************************************************************
353 * libvlc_audio_get_track_description : Get the description of available audio tracks
354 *****************************************************************************/
355 libvlc_track_description_t *
356 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
358 return libvlc_get_track_description( p_mi, "audio-es" );
361 /*****************************************************************************
362 * libvlc_audio_get_track : Get the current audio track
363 *****************************************************************************/
364 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
366 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
367 vlc_value_t val_list;
372 if( !p_input_thread )
375 if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
377 vlc_object_release( p_input_thread );
378 libvlc_printerr( "Audio track information not found" );
382 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
383 for( i = 0; i < val_list.p_list->i_count; i++ )
385 if( val_list.p_list->p_values[i].i_int == val.i_int )
391 var_FreeList( &val_list, NULL );
392 vlc_object_release( p_input_thread );
396 /*****************************************************************************
397 * libvlc_audio_set_track : Set the current audio track
398 *****************************************************************************/
399 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
401 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
402 vlc_value_t val_list;
406 if( !p_input_thread )
409 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
410 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
412 libvlc_printerr( "Audio track out of range" );
417 newval = val_list.p_list->p_values[i_track];
418 i_ret = var_Set( p_input_thread, "audio-es", newval );
421 libvlc_printerr( "Audio track out of range" ); /* Race... */
428 var_FreeList( &val_list, NULL );
429 vlc_object_release( p_input_thread );
433 /*****************************************************************************
434 * libvlc_audio_get_channel : Get the current audio channel
435 *****************************************************************************/
436 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
438 audio_output_t *p_aout = GetAOut( mp );
442 int val = var_GetInteger( p_aout, "audio-channels" );
443 vlc_object_release( p_aout );
447 /*****************************************************************************
448 * libvlc_audio_set_channel : Set the current audio channel
449 *****************************************************************************/
450 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
452 audio_output_t *p_aout = GetAOut( mp );
458 if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
460 libvlc_printerr( "Audio channel out of range" );
463 vlc_object_release( p_aout );
467 /*****************************************************************************
468 * libvlc_audio_get_delay : Get the current audio delay
469 *****************************************************************************/
470 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
472 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
474 if( p_input_thread != NULL )
476 val = var_GetTime( p_input_thread, "audio-delay" );
477 vlc_object_release( p_input_thread );
482 /*****************************************************************************
483 * libvlc_audio_set_delay : Set the current audio delay
484 *****************************************************************************/
485 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
487 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
489 if( p_input_thread != NULL )
491 var_SetTime( p_input_thread, "audio-delay", i_delay );
492 vlc_object_release( p_input_thread );