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 audio_output_t *p_aout = input_resource_HoldAout( mp->input.p_resource );
55 libvlc_printerr( "No active audio output" );
59 /*****************************************
60 * Get the list of available audio outputs
61 *****************************************/
62 libvlc_audio_output_t *
63 libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
66 module_t **module_list = module_list_get( &count );
67 libvlc_audio_output_t *list = NULL;
69 for (size_t i = 0; i < count; i++)
71 module_t *module = module_list[i];
73 if( !module_provides( module, "audio output" ) )
76 libvlc_audio_output_t *item = malloc( sizeof( *item ) );
77 if( unlikely(item == NULL) )
80 libvlc_printerr( "Not enough memory" );
81 libvlc_audio_output_list_release( list );
86 item->psz_name = strdup( module_get_object( module ) );
87 item->psz_description = strdup( module_get_name( module, true ) );
88 if( unlikely(item->psz_name == NULL || item->psz_description == NULL) )
96 module_list_free( module_list );
98 VLC_UNUSED( p_instance );
102 /********************************************
103 * Free the list of available audio outputs
104 ***********************************************/
105 void libvlc_audio_output_list_release( libvlc_audio_output_t *list )
107 while( list != NULL )
109 libvlc_audio_output_t *next = list->p_next;
111 free( list->psz_name );
112 free( list->psz_description );
119 /***********************
120 * Set the audio output.
121 ***********************/
122 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
126 if( !module_exists( psz_name )
127 || asprintf( &value, "%s,none", psz_name ) == -1 )
129 var_SetString( mp, "aout", value );
134 libvlc_audio_output_device_t *
135 libvlc_audio_output_device_list_get( libvlc_instance_t *p_instance,
139 if( (size_t)snprintf( varname, sizeof(varname), "%s-output-device", aout )
143 libvlc_audio_output_device_t *list = NULL, **pp = &list;
144 char **values, **texts;
145 ssize_t count = config_GetPszChoices( VLC_OBJECT(p_instance->p_libvlc_int),
146 varname, &values, &texts );
147 for( ssize_t i = 0; i < count; i++ )
149 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
150 if( unlikely(item == NULL) )
155 item->psz_device = values[i];
156 item->psz_description = texts[i];
166 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
170 libvlc_audio_output_device_t *next = l->p_next;
172 free( l->psz_description );
173 free( l->psz_device );
179 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
180 const char *psz_audio_output )
182 (void) p_instance; (void) psz_audio_output;
186 char *libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
187 const char *psz_audio_output,
190 (void) p_instance; (void) psz_audio_output; (void) i_device;
194 char *libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
195 const char *psz_audio_output,
198 (void) p_instance; (void) psz_audio_output; (void) i_device;
202 /*****************************
203 * Set device for using
204 *****************************/
205 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
206 const char *psz_audio_output,
207 const char *psz_device_id )
209 char *psz_config_name;
210 if( !psz_audio_output || !psz_device_id )
212 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
214 if( !var_Type( mp, psz_config_name ) )
215 /* Don't recreate the same variable over and over and over... */
216 var_Create( mp, psz_config_name, VLC_VAR_STRING );
217 var_SetString( mp, psz_config_name, psz_device_id );
218 free( psz_config_name );
221 /*****************************************************************************
222 * libvlc_audio_output_get_device_type : Get the current audio device type
223 *****************************************************************************/
224 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
226 audio_output_t *p_aout = GetAOut( mp );
229 int i_device_type = var_GetInteger( p_aout, "audio-device" );
230 vlc_object_release( p_aout );
231 return i_device_type;
233 return libvlc_AudioOutputDevice_Error;
236 /*****************************************************************************
237 * libvlc_audio_output_set_device_type : Set the audio device type
238 *****************************************************************************/
239 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
242 audio_output_t *p_aout = GetAOut( mp );
245 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
246 libvlc_printerr( "Error setting audio device" );
247 vlc_object_release( p_aout );
250 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
252 int mute = libvlc_audio_get_mute( mp );
254 libvlc_audio_set_mute( mp, !mute );
257 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
259 return aout_MuteGet( mp );
262 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
264 aout_MuteSet( VLC_OBJECT(mp), mute != 0 );
267 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
269 float vol = aout_VolumeGet( mp );
270 return ( vol >= 0.f ) ? lroundf( vol * 100.f ) : -1;
273 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
275 float vol = volume / 100.f;
278 libvlc_printerr( "Volume out of range" );
281 aout_VolumeSet (mp, vol);
285 /*****************************************************************************
286 * libvlc_audio_get_track_count : Get the number of available audio tracks
287 *****************************************************************************/
288 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
290 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
293 if( !p_input_thread )
296 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
298 vlc_object_release( p_input_thread );
299 return i_track_count;
302 /*****************************************************************************
303 * libvlc_audio_get_track_description : Get the description of available audio tracks
304 *****************************************************************************/
305 libvlc_track_description_t *
306 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
308 return libvlc_get_track_description( p_mi, "audio-es" );
311 /*****************************************************************************
312 * libvlc_audio_get_track : Get the current audio track
313 *****************************************************************************/
314 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
316 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
317 vlc_value_t val_list;
322 if( !p_input_thread )
325 if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
327 vlc_object_release( p_input_thread );
328 libvlc_printerr( "Audio track information not found" );
332 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
333 for( i = 0; i < val_list.p_list->i_count; i++ )
335 if( val_list.p_list->p_values[i].i_int == val.i_int )
341 var_FreeList( &val_list, NULL );
342 vlc_object_release( p_input_thread );
346 /*****************************************************************************
347 * libvlc_audio_set_track : Set the current audio track
348 *****************************************************************************/
349 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
351 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
352 vlc_value_t val_list;
356 if( !p_input_thread )
359 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
360 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
362 libvlc_printerr( "Audio track out of range" );
367 newval = val_list.p_list->p_values[i_track];
368 i_ret = var_Set( p_input_thread, "audio-es", newval );
371 libvlc_printerr( "Audio track out of range" ); /* Race... */
378 var_FreeList( &val_list, NULL );
379 vlc_object_release( p_input_thread );
383 /*****************************************************************************
384 * libvlc_audio_get_channel : Get the current audio channel
385 *****************************************************************************/
386 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
388 audio_output_t *p_aout = GetAOut( mp );
392 int val = var_GetInteger( p_aout, "stereo-mode" );
393 vlc_object_release( p_aout );
397 /*****************************************************************************
398 * libvlc_audio_set_channel : Set the current audio channel
399 *****************************************************************************/
400 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
402 audio_output_t *p_aout = GetAOut( mp );
408 if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
410 libvlc_printerr( "Audio channel out of range" );
413 vlc_object_release( p_aout );
417 /*****************************************************************************
418 * libvlc_audio_get_delay : Get the current audio delay
419 *****************************************************************************/
420 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
422 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
424 if( p_input_thread != NULL )
426 val = var_GetTime( p_input_thread, "audio-delay" );
427 vlc_object_release( p_input_thread );
432 /*****************************************************************************
433 * libvlc_audio_set_delay : Set the current audio delay
434 *****************************************************************************/
435 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
437 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
439 if( p_input_thread != NULL )
441 var_SetTime( p_input_thread, "audio-delay", i_delay );
442 vlc_object_release( p_input_thread );