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 libvlc_audio_output_device_t *
139 libvlc_audio_output_device_list_get( libvlc_instance_t *p_instance,
143 if( (size_t)snprintf( varname, sizeof(varname), "%s-output-device", aout )
147 libvlc_audio_output_device_t *list = NULL, **pp = &list;
148 char **values, **texts;
149 ssize_t count = config_GetPszChoices( VLC_OBJECT(p_instance->p_libvlc_int),
150 varname, &values, &texts );
151 for( ssize_t i = 0; i < count; i++ )
153 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
154 if( unlikely(item == NULL) )
159 item->psz_device = values[i];
160 item->psz_description = texts[i];
170 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
174 libvlc_audio_output_device_t *next = l->p_next;
176 free( l->psz_description );
177 free( l->psz_device );
183 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
184 const char *psz_audio_output )
186 (void) p_instance; (void) psz_audio_output;
190 char *libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
191 const char *psz_audio_output,
194 (void) p_instance; (void) psz_audio_output; (void) i_device;
198 char *libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
199 const char *psz_audio_output,
202 (void) p_instance; (void) psz_audio_output; (void) i_device;
206 /*****************************
207 * Set device for using
208 *****************************/
209 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
210 const char *psz_audio_output,
211 const char *psz_device_id )
213 char *psz_config_name;
214 if( !psz_audio_output || !psz_device_id )
216 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
218 if( !var_Type( mp, psz_config_name ) )
219 /* Don't recreate the same variable over and over and over... */
220 var_Create( mp, psz_config_name, VLC_VAR_STRING );
221 var_SetString( mp, psz_config_name, psz_device_id );
222 free( psz_config_name );
225 /*****************************************************************************
226 * libvlc_audio_output_get_device_type : Get the current audio device type
227 *****************************************************************************/
228 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
230 audio_output_t *p_aout = GetAOut( mp );
233 int i_device_type = var_GetInteger( p_aout, "audio-device" );
234 vlc_object_release( p_aout );
235 return i_device_type;
237 return libvlc_AudioOutputDevice_Error;
240 /*****************************************************************************
241 * libvlc_audio_output_set_device_type : Set the audio device type
242 *****************************************************************************/
243 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
246 audio_output_t *p_aout = GetAOut( mp );
249 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
250 libvlc_printerr( "Error setting audio device" );
251 vlc_object_release( p_aout );
254 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
256 aout_MuteToggle( mp );
259 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
261 return aout_MuteGet( mp );
264 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
266 aout_MuteSet( VLC_OBJECT(mp), mute != 0 );
269 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
271 float vol = aout_VolumeGet( mp );
272 return ( vol >= 0.f ) ? lroundf( vol * 100.f ) : -1;
275 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
277 float vol = volume / 100.f;
280 libvlc_printerr( "Volume out of range" );
283 aout_VolumeSet (mp, vol);
287 /*****************************************************************************
288 * libvlc_audio_get_track_count : Get the number of available audio tracks
289 *****************************************************************************/
290 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
292 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
295 if( !p_input_thread )
298 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
300 vlc_object_release( p_input_thread );
301 return i_track_count;
304 /*****************************************************************************
305 * libvlc_audio_get_track_description : Get the description of available audio tracks
306 *****************************************************************************/
307 libvlc_track_description_t *
308 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
310 return libvlc_get_track_description( p_mi, "audio-es" );
313 /*****************************************************************************
314 * libvlc_audio_get_track : Get the current audio track
315 *****************************************************************************/
316 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
318 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
319 vlc_value_t val_list;
324 if( !p_input_thread )
327 if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
329 vlc_object_release( p_input_thread );
330 libvlc_printerr( "Audio track information not found" );
334 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
335 for( i = 0; i < val_list.p_list->i_count; i++ )
337 if( val_list.p_list->p_values[i].i_int == val.i_int )
343 var_FreeList( &val_list, NULL );
344 vlc_object_release( p_input_thread );
348 /*****************************************************************************
349 * libvlc_audio_set_track : Set the current audio track
350 *****************************************************************************/
351 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
353 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
354 vlc_value_t val_list;
358 if( !p_input_thread )
361 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
362 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
364 libvlc_printerr( "Audio track out of range" );
369 newval = val_list.p_list->p_values[i_track];
370 i_ret = var_Set( p_input_thread, "audio-es", newval );
373 libvlc_printerr( "Audio track out of range" ); /* Race... */
380 var_FreeList( &val_list, NULL );
381 vlc_object_release( p_input_thread );
385 /*****************************************************************************
386 * libvlc_audio_get_channel : Get the current audio channel
387 *****************************************************************************/
388 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
390 audio_output_t *p_aout = GetAOut( mp );
394 int val = var_GetInteger( p_aout, "audio-channels" );
395 vlc_object_release( p_aout );
399 /*****************************************************************************
400 * libvlc_audio_set_channel : Set the current audio channel
401 *****************************************************************************/
402 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
404 audio_output_t *p_aout = GetAOut( mp );
410 if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
412 libvlc_printerr( "Audio channel out of range" );
415 vlc_object_release( p_aout );
419 /*****************************************************************************
420 * libvlc_audio_get_delay : Get the current audio delay
421 *****************************************************************************/
422 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
424 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
426 if( p_input_thread != NULL )
428 val = var_GetTime( p_input_thread, "audio-delay" );
429 vlc_object_release( p_input_thread );
434 /*****************************************************************************
435 * libvlc_audio_set_delay : Set the current audio delay
436 *****************************************************************************/
437 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
439 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
441 if( p_input_thread != NULL )
443 var_SetTime( p_input_thread, "audio-delay", i_delay );
444 vlc_object_release( p_input_thread );