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>
39 #include <vlc_modules.h>
41 #include "libvlc_internal.h"
42 #include "media_player_internal.h"
45 * Remember to release the returned audio_output_t since it is locked at
46 * the end of this function.
48 static audio_output_t *GetAOut( libvlc_media_player_t *mp )
52 audio_output_t *p_aout = input_resource_HoldAout( mp->input.p_resource );
54 libvlc_printerr( "No active audio output" );
58 /*****************************************
59 * Get the list of available audio outputs
60 *****************************************/
61 libvlc_audio_output_t *
62 libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
65 module_t **module_list = module_list_get( &count );
66 libvlc_audio_output_t *list = NULL;
68 for (size_t i = 0; i < count; i++)
70 module_t *module = module_list[i];
72 if( !module_provides( module, "audio output" ) )
75 libvlc_audio_output_t *item = malloc( sizeof( *item ) );
76 if( unlikely(item == NULL) )
79 libvlc_printerr( "Not enough memory" );
80 libvlc_audio_output_list_release( list );
85 item->psz_name = strdup( module_get_object( module ) );
86 item->psz_description = strdup( module_get_name( module, true ) );
87 if( unlikely(item->psz_name == NULL || item->psz_description == NULL) )
95 module_list_free( module_list );
97 VLC_UNUSED( p_instance );
101 /********************************************
102 * Free the list of available audio outputs
103 ***********************************************/
104 void libvlc_audio_output_list_release( libvlc_audio_output_t *list )
106 while( list != NULL )
108 libvlc_audio_output_t *next = list->p_next;
110 free( list->psz_name );
111 free( list->psz_description );
118 /***********************
119 * Set the audio output.
120 ***********************/
121 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
125 if( !module_exists( psz_name )
126 || asprintf( &value, "%s,none", psz_name ) == -1 )
128 var_SetString( mp, "aout", value );
133 libvlc_audio_output_device_t *
134 libvlc_audio_output_device_list_get( libvlc_instance_t *p_instance,
138 if( (size_t)snprintf( varname, sizeof(varname), "%s-output-device", aout )
142 libvlc_audio_output_device_t *list = NULL, **pp = &list;
143 char **values, **texts;
144 ssize_t count = config_GetPszChoices( VLC_OBJECT(p_instance->p_libvlc_int),
145 varname, &values, &texts );
146 for( ssize_t i = 0; i < count; i++ )
148 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
149 if( unlikely(item == NULL) )
154 item->psz_device = values[i];
155 item->psz_description = texts[i];
165 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
169 libvlc_audio_output_device_t *next = l->p_next;
171 free( l->psz_description );
172 free( l->psz_device );
178 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
179 const char *psz_audio_output )
181 (void) p_instance; (void) psz_audio_output;
185 char *libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
186 const char *psz_audio_output,
189 (void) p_instance; (void) psz_audio_output; (void) i_device;
193 char *libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
194 const char *psz_audio_output,
197 (void) p_instance; (void) psz_audio_output; (void) i_device;
201 /*****************************
202 * Set device for using
203 *****************************/
204 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
205 const char *psz_audio_output,
206 const char *psz_device_id )
208 char *psz_config_name;
209 if( !psz_audio_output || !psz_device_id )
211 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
213 if( !var_Type( mp, psz_config_name ) )
214 /* Don't recreate the same variable over and over and over... */
215 var_Create( mp, psz_config_name, VLC_VAR_STRING );
216 var_SetString( mp, psz_config_name, psz_device_id );
217 free( psz_config_name );
220 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
223 return libvlc_AudioOutputDevice_Error;
226 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
229 (void) mp; (void) device_type;
232 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
234 int mute = libvlc_audio_get_mute( mp );
236 libvlc_audio_set_mute( mp, !mute );
239 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
243 audio_output_t *aout = GetAOut( mp );
246 mute = aout_MuteGet( aout );
247 vlc_object_release( aout );
252 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
254 audio_output_t *aout = GetAOut( mp );
257 mute = aout_MuteSet( aout, mute );
258 vlc_object_release( aout );
262 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
266 audio_output_t *aout = GetAOut( mp );
269 float vol = aout_VolumeGet( aout );
270 vlc_object_release( aout );
271 volume = lroundf( vol * 100.f );
276 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
278 float vol = volume / 100.f;
281 libvlc_printerr( "Volume out of range" );
286 audio_output_t *aout = GetAOut( mp );
289 ret = aout_VolumeSet( aout, vol );
290 vlc_object_release( aout );
295 /*****************************************************************************
296 * libvlc_audio_get_track_count : Get the number of available audio tracks
297 *****************************************************************************/
298 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
300 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
303 if( !p_input_thread )
306 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
308 vlc_object_release( p_input_thread );
309 return i_track_count;
312 /*****************************************************************************
313 * libvlc_audio_get_track_description : Get the description of available audio tracks
314 *****************************************************************************/
315 libvlc_track_description_t *
316 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
318 return libvlc_get_track_description( p_mi, "audio-es" );
321 /*****************************************************************************
322 * libvlc_audio_get_track : Get the current audio track
323 *****************************************************************************/
324 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
326 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
327 if( !p_input_thread )
330 int id = var_GetInteger( p_input_thread, "audio-es" );
331 vlc_object_release( p_input_thread );
335 /*****************************************************************************
336 * libvlc_audio_set_track : Set the current audio track
337 *****************************************************************************/
338 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
340 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
341 vlc_value_t val_list;
344 if( !p_input_thread )
347 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
348 for( int i = 0; i < val_list.p_list->i_count; i++ )
350 if( i_track == val_list.p_list->p_values[i].i_int )
352 if( var_SetInteger( p_input_thread, "audio-es", i_track ) < 0 )
358 libvlc_printerr( "Track identifier not found" );
360 var_FreeList( &val_list, NULL );
361 vlc_object_release( p_input_thread );
365 /*****************************************************************************
366 * libvlc_audio_get_channel : Get the current audio channel
367 *****************************************************************************/
368 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
370 audio_output_t *p_aout = GetAOut( mp );
374 int val = var_GetInteger( p_aout, "stereo-mode" );
375 vlc_object_release( p_aout );
379 /*****************************************************************************
380 * libvlc_audio_set_channel : Set the current audio channel
381 *****************************************************************************/
382 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
384 audio_output_t *p_aout = GetAOut( mp );
390 if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
392 libvlc_printerr( "Audio channel out of range" );
395 vlc_object_release( p_aout );
399 /*****************************************************************************
400 * libvlc_audio_get_delay : Get the current audio delay
401 *****************************************************************************/
402 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
404 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
406 if( p_input_thread != NULL )
408 val = var_GetTime( p_input_thread, "audio-delay" );
409 vlc_object_release( p_input_thread );
414 /*****************************************************************************
415 * libvlc_audio_set_delay : Set the current audio delay
416 *****************************************************************************/
417 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
419 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
421 if( p_input_thread != NULL )
423 var_SetTime( p_input_thread, "audio-delay", i_delay );
424 vlc_object_release( p_input_thread );