]> git.sesse.net Git - vlc/blob - src/control/audio.c
Merge branch 1.0-bugfix into master
[vlc] / src / control / audio.c
1 /*****************************************************************************
2  * libvlc_audio.c: New libvlc audio control API
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Filippo Carone <filippo@carone.org>
8  *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27
28 #include <vlc_input.h>
29 #include <vlc_aout.h>
30
31
32 /*
33  * Remember to release the returned aout_instance_t since it is locked at
34  * the end of this function.
35  */
36 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
37                                  libvlc_exception_t *p_exception )
38 {
39     if( !p_instance )
40         return NULL;
41
42     aout_instance_t * p_aout = NULL;
43
44     p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
45     if( !p_aout )
46     {
47         libvlc_exception_raise( p_exception, "No active audio output" );
48         return NULL;
49     }
50
51     return p_aout;
52 }
53
54 /*****************************************
55  * Get the list of available audio outputs
56  *****************************************/
57 VLC_PUBLIC_API libvlc_audio_output_t *
58         libvlc_audio_output_list_get( libvlc_instance_t *p_instance,
59                                       libvlc_exception_t *p_e )
60 {
61     VLC_UNUSED( p_instance );
62     libvlc_audio_output_t *p_list = NULL,
63                           *p_actual = NULL,
64                           *p_previous = NULL;
65     module_t **module_list = module_list_get( NULL );
66
67     for (size_t i = 0; module_list[i]; i++)
68     {
69         module_t *p_module = module_list[i];
70
71         if( module_provides( p_module, "audio output" ) )
72         {
73             if( p_actual == NULL)
74             {
75                 p_actual = ( libvlc_audio_output_t * )
76                     malloc( sizeof( libvlc_audio_output_t ) );
77                 if( p_actual == NULL )
78                 {
79                     libvlc_exception_raise( p_e, "Not enough memory" );
80                     libvlc_audio_output_list_release( p_list );
81                     module_list_free( module_list );
82                     return NULL;
83                 }
84                 if( p_list == NULL )
85                 {
86                     p_list = p_actual;
87                     p_previous = p_actual;
88                 }
89             }
90             p_actual->psz_name = strdup( module_get_object( p_module ) );
91             p_actual->psz_description = strdup( module_get_name( p_module, true )  );
92             p_actual->p_next = NULL;
93             if( p_previous != p_actual ) /* not first item */
94                 p_previous->p_next = p_actual;
95             p_previous = p_actual;
96             p_actual = p_actual->p_next;
97         }
98     }
99
100     module_list_free( module_list );
101
102     return p_list;
103 }
104
105 /********************************************
106  * Free the list of available audio outputs
107  ***********************************************/
108 VLC_PUBLIC_API void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
109 {
110     libvlc_audio_output_t *p_actual, *p_before;
111     p_actual = p_list;
112
113     while ( p_actual )
114     {
115         free( p_actual->psz_name );
116         free( p_actual->psz_description );
117         p_before = p_actual;
118         p_actual = p_before->p_next;
119         free( p_before );
120     }
121 }
122
123
124 /***********************
125  * Set the audio output.
126  ***********************/
127 VLC_PUBLIC_API int libvlc_audio_output_set( libvlc_instance_t *p_instance,
128                                             const char *psz_name )
129 {
130     if( module_exists( psz_name ) )
131     {
132         config_PutPsz( p_instance->p_libvlc_int, "aout", psz_name );
133         return true;
134     }
135     else
136         return false;
137 }
138
139 /****************************
140  * Get count of devices.
141  *****************************/
142 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
143                                       const char *psz_audio_output )
144 {
145     char *psz_config_name = NULL;
146     if( !psz_audio_output )
147         return 0;
148     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
149         return 0;
150
151     module_config_t *p_module_config = config_FindConfig(
152         VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
153
154     if( p_module_config && p_module_config->pf_update_list )
155     {
156         vlc_value_t val;
157         val.psz_string = strdup( p_module_config->value.psz );
158
159         p_module_config->pf_update_list(
160             VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
161         free( val.psz_string );
162         free( psz_config_name );
163
164         return p_module_config->i_list;
165     }
166     else
167         free( psz_config_name );
168
169
170     return 0;
171 }
172
173 /********************************
174  * Get long name of device
175  *********************************/
176 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
177                                             const char *psz_audio_output,
178                                             int i_device )
179 {
180     char *psz_config_name = NULL;
181     if( !psz_audio_output )
182         return NULL;
183     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
184         return NULL;
185
186     module_config_t *p_module_config = config_FindConfig(
187         VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
188
189     if( p_module_config )
190     {
191         // refresh if there arent devices
192         if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
193         {
194             vlc_value_t val;
195             val.psz_string = strdup( p_module_config->value.psz );
196
197             p_module_config->pf_update_list(
198                 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
199             free( val.psz_string );
200         }
201         free( psz_config_name );
202
203         if( i_device >= 0 && i_device < p_module_config->i_list )
204         {
205             if( p_module_config->ppsz_list_text[i_device] )
206                 return strdup( p_module_config->ppsz_list_text[i_device] );
207             else
208                 return strdup( p_module_config->ppsz_list[i_device] );
209         }
210     }
211     else
212         free( psz_config_name );
213
214     return NULL;
215 }
216
217 /********************************
218  * Get id name of device
219  *********************************/
220 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
221                                       const char *psz_audio_output,
222                                       int i_device )
223 {
224     char *psz_config_name = NULL;
225     if( !psz_audio_output )
226         return NULL;
227     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
228         return NULL;
229
230     module_config_t *p_module_config = config_FindConfig(
231         VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
232
233     if( p_module_config )
234     {
235         // refresh if there arent devices
236         if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
237         {
238             vlc_value_t val;
239             val.psz_string = strdup( p_module_config->value.psz );
240
241             p_module_config->pf_update_list(
242                 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
243             free( val.psz_string );
244         }
245         free( psz_config_name );
246
247         if( i_device >= 0 && i_device < p_module_config->i_list )
248             return strdup( p_module_config->ppsz_list[i_device] );
249
250     }
251     else
252         free( psz_config_name );
253
254     return NULL;
255 }
256
257 /*****************************
258  * Set device for using
259  *****************************/
260 VLC_PUBLIC_API void libvlc_audio_output_device_set( libvlc_instance_t *p_instance,
261                                                     const char *psz_audio_output,
262                                                     const char *psz_device_id )
263 {
264     char *psz_config_name = NULL;
265     if( !psz_audio_output || !psz_device_id )
266         return;
267     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
268         return;
269     config_PutPsz( p_instance->p_libvlc_int, psz_config_name, psz_device_id );
270     free( psz_config_name );
271 }
272
273 /*****************************************************************************
274  * libvlc_audio_output_get_device_type : Get the current audio device type
275  *****************************************************************************/
276 int libvlc_audio_output_get_device_type( libvlc_instance_t *p_instance,
277                                          libvlc_exception_t *p_e )
278 {
279     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
280     if( p_aout )
281     {
282         vlc_value_t val;
283
284         var_Get( p_aout, "audio-device", &val );
285         vlc_object_release( p_aout );
286         return val.i_int;
287     }
288     libvlc_exception_raise( p_e, "Unable to get audio output" );
289     return libvlc_AudioOutputDevice_Error;
290 }
291
292 /*****************************************************************************
293  * libvlc_audio_output_set_device_type : Set the audio device type
294  *****************************************************************************/
295 void libvlc_audio_output_set_device_type( libvlc_instance_t *p_instance,
296                                           int device_type,
297                                           libvlc_exception_t *p_e )
298 {
299     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
300     if( p_aout )
301     {
302         if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
303             libvlc_exception_raise( p_e, "Failed setting audio device" );
304         vlc_object_release( p_aout );
305     }
306 }
307
308 /*****************************************************************************
309  * libvlc_audio_get_mute : Get the volume state, true if muted
310  *****************************************************************************/
311 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
312                                libvlc_exception_t *p_e )
313 {
314     VLC_UNUSED(p_e);
315
316     aout_VolumeMute( p_instance->p_libvlc_int, NULL );
317 }
318
319 int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
320                            libvlc_exception_t *p_e )
321 {
322     return (libvlc_audio_get_volume(p_instance, p_e) == 0);
323 }
324
325 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
326                             libvlc_exception_t *p_e )
327 {
328     if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
329     {
330         aout_VolumeMute( p_instance->p_libvlc_int, NULL );
331     }
332 }
333
334 /*****************************************************************************
335  * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
336  *****************************************************************************/
337 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
338                              libvlc_exception_t *p_e )
339 {
340     VLC_UNUSED(p_e);
341
342     audio_volume_t i_volume;
343
344     aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
345
346     return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
347 }
348
349
350 /*****************************************************************************
351  * libvlc_audio_set_volume : Set the current volume
352  *****************************************************************************/
353 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
354                               libvlc_exception_t *p_e )
355 {
356     if( i_volume >= 0 && i_volume <= 200 )
357     {
358         i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
359
360         aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
361     }
362     else
363     {
364         libvlc_exception_raise( p_e, "Volume out of range" );
365     }
366 }
367
368 /*****************************************************************************
369  * libvlc_audio_get_track_count : Get the number of available audio tracks
370  *****************************************************************************/
371 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
372                                   libvlc_exception_t *p_e )
373 {
374     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
375     int i_track_count;
376
377     if( !p_input_thread )
378         return -1;
379
380     i_track_count = var_CountChoices( p_input_thread, "audio-es" );
381
382     vlc_object_release( p_input_thread );
383     return i_track_count;
384 }
385
386 /*****************************************************************************
387  * libvlc_audio_get_track_description : Get the description of available audio tracks
388  *****************************************************************************/
389 libvlc_track_description_t *
390         libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
391                                             libvlc_exception_t *p_e )
392 {
393     return libvlc_get_track_description( p_mi, "audio-es", p_e);
394 }
395
396 /*****************************************************************************
397  * libvlc_audio_get_track : Get the current audio track
398  *****************************************************************************/
399 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
400                             libvlc_exception_t *p_e )
401 {
402     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
403     vlc_value_t val_list;
404     vlc_value_t val;
405     int i_track = -1;
406     int i_ret = -1;
407     int i;
408
409     if( !p_input_thread )
410         return -1;
411
412     i_ret = var_Get( p_input_thread, "audio-es", &val );
413     if( i_ret < 0 )
414     {
415         libvlc_exception_raise( p_e, "Getting Audio track information failed" );
416         vlc_object_release( p_input_thread );
417         return i_ret;
418     }
419
420     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
421     for( i = 0; i < val_list.p_list->i_count; i++ )
422     {
423         if( val_list.p_list->p_values[i].i_int == val.i_int )
424         {
425             i_track = i;
426             break;
427         }
428     }
429     var_FreeList( &val_list, NULL );
430     vlc_object_release( p_input_thread );
431     return i_track;
432 }
433
434 /*****************************************************************************
435  * libvlc_audio_set_track : Set the current audio track
436  *****************************************************************************/
437 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
438                              libvlc_exception_t *p_e )
439 {
440     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
441     vlc_value_t val_list;
442     vlc_value_t newval;
443     int i_ret = -1;
444
445     if( !p_input_thread )
446         return;
447
448     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
449     if( (i_track < 0) && (i_track > val_list.p_list->i_count) )
450     {
451         libvlc_exception_raise( p_e, "Audio track out of range" );
452         goto end;
453     }
454
455     newval = val_list.p_list->p_values[i_track];
456     i_ret = var_Set( p_input_thread, "audio-es", newval );
457     if( i_ret < 0 )
458         libvlc_exception_raise( p_e, "Setting audio track failed" );
459
460 end:
461     var_FreeList( &val_list, NULL );
462     vlc_object_release( p_input_thread );
463 }
464
465 /*****************************************************************************
466  * libvlc_audio_get_channel : Get the current audio channel
467  *****************************************************************************/
468 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
469                               libvlc_exception_t *p_e )
470 {
471     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
472     if( p_aout )
473     {
474         vlc_value_t val;
475
476         var_Get( p_aout, "audio-channels", &val );
477         vlc_object_release( p_aout );
478         return val.i_int;
479     }
480     libvlc_exception_raise( p_e, "Unable to get audio output" );
481     return libvlc_AudioChannel_Error;
482 }
483
484 /*****************************************************************************
485  * libvlc_audio_set_channel : Set the current audio channel
486  *****************************************************************************/
487 void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
488                                int channel,
489                                libvlc_exception_t *p_e )
490 {
491     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
492     if( p_aout )
493     {
494         vlc_value_t val;
495         int i_ret = -1;
496
497         val.i_int = channel;
498         i_ret = var_Set( p_aout, "audio-channels", val );
499         if( i_ret < 0 )
500             libvlc_exception_raise( p_e, "Failed setting audio channel" );
501
502         vlc_object_release( p_aout );
503     }
504
505 }