Fixed typo into invmem error message
[vlc.git] / src / control / audio.c
blob89ee0a76bc77d1aa1ae319562f491f51e89e3d67
1 /*****************************************************************************
2 * libvlc_audio.c: New libvlc audio control API
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
5 * $Id$
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_media.h>
31 #include <vlc/libvlc_media_player.h>
33 #include <vlc_common.h>
34 #include <vlc_input.h>
35 #include <vlc_aout.h>
37 #include "libvlc_internal.h"
38 #include "media_player_internal.h"
41 * Remember to release the returned aout_instance_t since it is locked at
42 * the end of this function.
44 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
45 libvlc_exception_t *p_exception )
47 if( !p_instance )
48 return NULL;
50 aout_instance_t * p_aout = NULL;
52 p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
53 if( !p_aout )
55 libvlc_exception_raise( p_exception );
56 libvlc_printerr( "No active audio output" );
57 return NULL;
60 return p_aout;
63 /*****************************************
64 * Get the list of available audio outputs
65 *****************************************/
66 libvlc_audio_output_t *
67 libvlc_audio_output_list_get( libvlc_instance_t *p_instance,
68 libvlc_exception_t *p_e )
70 VLC_UNUSED( p_instance );
71 libvlc_audio_output_t *p_list = NULL,
72 *p_actual = NULL,
73 *p_previous = NULL;
74 module_t **module_list = module_list_get( NULL );
76 for (size_t i = 0; module_list[i]; i++)
78 module_t *p_module = module_list[i];
80 if( module_provides( p_module, "audio output" ) )
82 if( p_actual == NULL)
84 p_actual = ( libvlc_audio_output_t * )
85 malloc( sizeof( libvlc_audio_output_t ) );
86 if( p_actual == NULL )
88 libvlc_exception_raise( p_e );
89 libvlc_printerr( "Not enough memory" );
90 libvlc_audio_output_list_release( p_list );
91 module_list_free( module_list );
92 return NULL;
94 if( p_list == NULL )
96 p_list = p_actual;
97 p_previous = p_actual;
100 p_actual->psz_name = strdup( module_get_object( p_module ) );
101 p_actual->psz_description = strdup( module_get_name( p_module, true ) );
102 p_actual->p_next = NULL;
103 if( p_previous != p_actual ) /* not first item */
104 p_previous->p_next = p_actual;
105 p_previous = p_actual;
106 p_actual = p_actual->p_next;
110 module_list_free( module_list );
112 return p_list;
115 /********************************************
116 * Free the list of available audio outputs
117 ***********************************************/
118 void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
120 libvlc_audio_output_t *p_actual, *p_before;
121 p_actual = p_list;
123 while ( p_actual )
125 free( p_actual->psz_name );
126 free( p_actual->psz_description );
127 p_before = p_actual;
128 p_actual = p_before->p_next;
129 free( p_before );
134 /***********************
135 * Set the audio output.
136 ***********************/
137 int libvlc_audio_output_set( libvlc_instance_t *p_instance,
138 const char *psz_name )
140 if( module_exists( psz_name ) )
142 config_PutPsz( p_instance->p_libvlc_int, "aout", psz_name );
143 return true;
145 else
146 return false;
149 /****************************
150 * Get count of devices.
151 *****************************/
152 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
153 const char *psz_audio_output )
155 char *psz_config_name;
156 if( !psz_audio_output )
157 return 0;
158 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
159 return 0;
161 module_config_t *p_module_config = config_FindConfig(
162 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
164 if( p_module_config && p_module_config->pf_update_list )
166 vlc_value_t val;
167 val.psz_string = strdup( p_module_config->value.psz );
169 p_module_config->pf_update_list(
170 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
171 free( val.psz_string );
172 free( psz_config_name );
174 return p_module_config->i_list;
177 free( psz_config_name );
178 return 0;
181 /********************************
182 * Get long name of device
183 *********************************/
184 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
185 const char *psz_audio_output,
186 int i_device )
188 char *psz_config_name;
189 if( !psz_audio_output )
190 return NULL;
191 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
192 return NULL;
194 module_config_t *p_module_config = config_FindConfig(
195 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
197 if( p_module_config )
199 // refresh if there arent devices
200 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
202 vlc_value_t val;
203 val.psz_string = strdup( p_module_config->value.psz );
205 p_module_config->pf_update_list(
206 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
207 free( val.psz_string );
209 free( psz_config_name );
211 if( i_device >= 0 && i_device < p_module_config->i_list )
213 if( p_module_config->ppsz_list_text[i_device] )
214 return strdup( p_module_config->ppsz_list_text[i_device] );
215 else
216 return strdup( p_module_config->ppsz_list[i_device] );
220 free( psz_config_name );
221 return NULL;
224 /********************************
225 * Get id name of device
226 *********************************/
227 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
228 const char *psz_audio_output,
229 int i_device )
231 char *psz_config_name;
232 if( !psz_audio_output )
233 return NULL;
234 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
235 return NULL;
237 module_config_t *p_module_config = config_FindConfig(
238 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
240 if( p_module_config )
242 // refresh if there arent devices
243 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
245 vlc_value_t val;
246 val.psz_string = strdup( p_module_config->value.psz );
248 p_module_config->pf_update_list(
249 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
250 free( val.psz_string );
252 free( psz_config_name );
254 if( i_device >= 0 && i_device < p_module_config->i_list )
255 return strdup( p_module_config->ppsz_list[i_device] );
259 free( psz_config_name );
260 return NULL;
263 /*****************************
264 * Set device for using
265 *****************************/
266 void libvlc_audio_output_device_set( libvlc_instance_t *p_instance,
267 const char *psz_audio_output,
268 const char *psz_device_id )
270 char *psz_config_name = NULL;
271 if( !psz_audio_output || !psz_device_id )
272 return;
273 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
274 return;
275 config_PutPsz( p_instance->p_libvlc_int, psz_config_name, psz_device_id );
276 free( psz_config_name );
279 /*****************************************************************************
280 * libvlc_audio_output_get_device_type : Get the current audio device type
281 *****************************************************************************/
282 int libvlc_audio_output_get_device_type( libvlc_instance_t *p_instance,
283 libvlc_exception_t *p_e )
285 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
286 if( p_aout )
288 int i_device_type = var_GetInteger( p_aout, "audio-device" );
289 vlc_object_release( p_aout );
290 return i_device_type;
292 return libvlc_AudioOutputDevice_Error;
295 /*****************************************************************************
296 * libvlc_audio_output_set_device_type : Set the audio device type
297 *****************************************************************************/
298 void libvlc_audio_output_set_device_type( libvlc_instance_t *p_instance,
299 int device_type,
300 libvlc_exception_t *p_e )
302 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
303 if( !p_aout )
304 return;
305 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
307 libvlc_exception_raise( p_e );
308 libvlc_printerr( "Error setting audio device" );
310 vlc_object_release( p_aout );
313 /*****************************************************************************
314 * libvlc_audio_get_mute : Get the volume state, true if muted
315 *****************************************************************************/
316 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
317 libvlc_exception_t *p_e )
319 VLC_UNUSED(p_e);
321 aout_ToggleMute( p_instance->p_libvlc_int, NULL );
324 int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
325 libvlc_exception_t *p_e )
327 return (libvlc_audio_get_volume(p_instance, p_e) == 0);
330 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
331 libvlc_exception_t *p_e )
333 if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
335 aout_ToggleMute( p_instance->p_libvlc_int, NULL );
339 /*****************************************************************************
340 * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
341 *****************************************************************************/
342 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
343 libvlc_exception_t *p_e )
345 VLC_UNUSED(p_e);
347 audio_volume_t i_volume;
349 aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
351 return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
355 /*****************************************************************************
356 * libvlc_audio_set_volume : Set the current volume
357 *****************************************************************************/
358 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
359 libvlc_exception_t *p_e )
361 if( i_volume >= 0 && i_volume <= 200 )
363 i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
365 aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
367 else
369 libvlc_exception_raise( p_e );
370 libvlc_printerr( "Volume out of range" );
374 /*****************************************************************************
375 * libvlc_audio_get_track_count : Get the number of available audio tracks
376 *****************************************************************************/
377 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
378 libvlc_exception_t *p_e )
380 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
381 int i_track_count;
383 if( !p_input_thread )
384 return -1;
386 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
388 vlc_object_release( p_input_thread );
389 return i_track_count;
392 /*****************************************************************************
393 * libvlc_audio_get_track_description : Get the description of available audio tracks
394 *****************************************************************************/
395 libvlc_track_description_t *
396 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
397 libvlc_exception_t *p_e )
399 return libvlc_get_track_description( p_mi, "audio-es", p_e);
402 /*****************************************************************************
403 * libvlc_audio_get_track : Get the current audio track
404 *****************************************************************************/
405 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
406 libvlc_exception_t *p_e )
408 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
409 vlc_value_t val_list;
410 vlc_value_t val;
411 int i_track = -1;
412 int i_ret = -1;
413 int i;
415 if( !p_input_thread )
416 return -1;
418 i_ret = var_Get( p_input_thread, "audio-es", &val );
419 if( i_ret < 0 )
421 vlc_object_release( p_input_thread );
422 libvlc_exception_raise( p_e );
423 libvlc_printerr( "Audio track information not found" );
424 return i_ret;
427 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
428 for( i = 0; i < val_list.p_list->i_count; i++ )
430 if( val_list.p_list->p_values[i].i_int == val.i_int )
432 i_track = i;
433 break;
436 var_FreeList( &val_list, NULL );
437 vlc_object_release( p_input_thread );
438 return i_track;
441 /*****************************************************************************
442 * libvlc_audio_set_track : Set the current audio track
443 *****************************************************************************/
444 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
445 libvlc_exception_t *p_e )
447 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
448 vlc_value_t val_list;
449 vlc_value_t newval;
450 int i_ret = -1;
452 if( !p_input_thread )
453 return;
455 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
456 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
458 libvlc_exception_raise( p_e );
459 libvlc_printerr( "Audio track out of range" );
460 goto end;
463 newval = val_list.p_list->p_values[i_track];
464 i_ret = var_Set( p_input_thread, "audio-es", newval );
465 if( i_ret < 0 )
467 libvlc_exception_raise( p_e );
468 libvlc_printerr( "Audio track out of range" ); /* Race... */
471 end:
472 var_FreeList( &val_list, NULL );
473 vlc_object_release( p_input_thread );
476 /*****************************************************************************
477 * libvlc_audio_get_channel : Get the current audio channel
478 *****************************************************************************/
479 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
480 libvlc_exception_t *p_e )
482 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
483 if( !p_aout )
484 return 0;
486 int val = var_GetInteger( p_aout, "audio-channels" );
487 vlc_object_release( p_aout );
488 return val;
491 /*****************************************************************************
492 * libvlc_audio_set_channel : Set the current audio channel
493 *****************************************************************************/
494 void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
495 int channel,
496 libvlc_exception_t *p_e )
498 aout_instance_t *p_aout = GetAOut( p_instance, p_e );
499 if( !p_aout )
500 return;
502 if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
504 libvlc_exception_raise( p_e );
505 libvlc_printerr( "Audio channel out of range" );
507 vlc_object_release( p_aout );