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 /*****************************************************************************
221 * libvlc_audio_output_get_device_type : Get the current audio device type
222 *****************************************************************************/
223 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
225 audio_output_t *p_aout = GetAOut( mp );
228 int i_device_type = var_GetInteger( p_aout, "audio-device" );
229 vlc_object_release( p_aout );
230 return i_device_type;
232 return libvlc_AudioOutputDevice_Error;
235 /*****************************************************************************
236 * libvlc_audio_output_set_device_type : Set the audio device type
237 *****************************************************************************/
238 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
241 audio_output_t *p_aout = GetAOut( mp );
244 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
245 libvlc_printerr( "Error setting audio device" );
246 vlc_object_release( p_aout );
249 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
251 int mute = libvlc_audio_get_mute( mp );
253 libvlc_audio_set_mute( mp, !mute );
256 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
260 audio_output_t *aout = GetAOut( mp );
263 mute = aout_MuteGet( aout );
264 vlc_object_release( aout );
269 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
271 audio_output_t *aout = GetAOut( mp );
274 mute = aout_MuteSet( aout, mute );
275 vlc_object_release( aout );
279 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
283 audio_output_t *aout = GetAOut( mp );
286 float vol = aout_VolumeGet( aout );
287 vlc_object_release( aout );
288 volume = lroundf( vol * 100.f );
293 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
295 float vol = volume / 100.f;
298 libvlc_printerr( "Volume out of range" );
303 audio_output_t *aout = GetAOut( mp );
306 ret = aout_VolumeSet( aout, vol );
307 vlc_object_release( aout );
312 /*****************************************************************************
313 * libvlc_audio_get_track_count : Get the number of available audio tracks
314 *****************************************************************************/
315 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
317 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
320 if( !p_input_thread )
323 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
325 vlc_object_release( p_input_thread );
326 return i_track_count;
329 /*****************************************************************************
330 * libvlc_audio_get_track_description : Get the description of available audio tracks
331 *****************************************************************************/
332 libvlc_track_description_t *
333 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
335 return libvlc_get_track_description( p_mi, "audio-es" );
338 /*****************************************************************************
339 * libvlc_audio_get_track : Get the current audio track
340 *****************************************************************************/
341 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
343 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
344 if( !p_input_thread )
347 int id = var_GetInteger( p_input_thread, "audio-es" );
348 vlc_object_release( p_input_thread );
352 /*****************************************************************************
353 * libvlc_audio_set_track : Set the current audio track
354 *****************************************************************************/
355 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
357 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
358 vlc_value_t val_list;
361 if( !p_input_thread )
364 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
365 for( int i = 0; i < val_list.p_list->i_count; i++ )
367 if( i_track == val_list.p_list->p_values[i].i_int )
369 if( var_SetInteger( p_input_thread, "audio-es", i_track ) < 0 )
375 libvlc_printerr( "Track identifier not found" );
377 var_FreeList( &val_list, NULL );
378 vlc_object_release( p_input_thread );
382 /*****************************************************************************
383 * libvlc_audio_get_channel : Get the current audio channel
384 *****************************************************************************/
385 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
387 audio_output_t *p_aout = GetAOut( mp );
391 int val = var_GetInteger( p_aout, "stereo-mode" );
392 vlc_object_release( p_aout );
396 /*****************************************************************************
397 * libvlc_audio_set_channel : Set the current audio channel
398 *****************************************************************************/
399 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
401 audio_output_t *p_aout = GetAOut( mp );
407 if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
409 libvlc_printerr( "Audio channel out of range" );
412 vlc_object_release( p_aout );
416 /*****************************************************************************
417 * libvlc_audio_get_delay : Get the current audio delay
418 *****************************************************************************/
419 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
421 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
423 if( p_input_thread != NULL )
425 val = var_GetTime( p_input_thread, "audio-delay" );
426 vlc_object_release( p_input_thread );
431 /*****************************************************************************
432 * libvlc_audio_set_delay : Set the current audio delay
433 *****************************************************************************/
434 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
436 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
438 if( p_input_thread != NULL )
440 var_SetTime( p_input_thread, "audio-delay", i_delay );
441 vlc_object_release( p_input_thread );