]> git.sesse.net Git - vlc/blob - src/control/audio.c
libvlc: better audio handling
[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_name( p_module, false ) );
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         vlc_value_t val;
303         int i_ret = -1;
304
305         val.i_int = (int) device_type;
306         i_ret = var_Set( p_aout, "audio-device", val );
307         if( i_ret < 0 )
308         {
309             libvlc_exception_raise( p_e, "Failed setting audio device" );
310             vlc_object_release( p_aout );
311             return;
312         }
313
314         vlc_object_release( p_aout );
315     }
316 }
317
318 /*****************************************************************************
319  * libvlc_audio_get_mute : Get the volume state, true if muted
320  *****************************************************************************/
321 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
322                                libvlc_exception_t *p_e )
323 {
324     VLC_UNUSED(p_e);
325
326     aout_VolumeMute( p_instance->p_libvlc_int, NULL );
327 }
328
329 int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
330                            libvlc_exception_t *p_e )
331 {
332     /*
333      * If the volume level is 0, then the channel is muted
334      */
335     audio_volume_t i_volume;
336
337     i_volume = libvlc_audio_get_volume(p_instance, p_e);
338     if ( i_volume == 0 )
339         return true;
340     return false;
341 }
342
343 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
344                             libvlc_exception_t *p_e )
345 {
346     if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
347     {
348         aout_VolumeMute( p_instance->p_libvlc_int, NULL );
349     }
350 }
351
352 /*****************************************************************************
353  * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
354  *****************************************************************************/
355 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
356                              libvlc_exception_t *p_e )
357 {
358     VLC_UNUSED(p_e);
359
360     audio_volume_t i_volume;
361
362     aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
363
364     return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
365 }
366
367
368 /*****************************************************************************
369  * libvlc_audio_set_volume : Set the current volume
370  *****************************************************************************/
371 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
372                               libvlc_exception_t *p_e )
373 {
374     if( i_volume >= 0 && i_volume <= 200 )
375     {
376         i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
377
378         aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
379     }
380     else
381     {
382         libvlc_exception_raise( p_e, "Volume out of range" );
383     }
384 }
385
386 /*****************************************************************************
387  * libvlc_audio_get_track_count : Get the number of available audio tracks
388  *****************************************************************************/
389 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
390                                   libvlc_exception_t *p_e )
391 {
392     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
393     vlc_value_t val_list;
394
395     if( !p_input_thread )
396         return -1;
397
398     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
399     vlc_object_release( p_input_thread );
400     return val_list.p_list->i_count;
401 }
402
403 /*****************************************************************************
404  * libvlc_audio_get_track_description : Get the description of available audio tracks
405  *****************************************************************************/
406 libvlc_track_description_t *
407         libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
408                                             libvlc_exception_t *p_e )
409 {
410     return libvlc_get_track_description( p_mi, "audio-es", p_e);
411 }
412
413 /*****************************************************************************
414  * libvlc_audio_get_track : Get the current audio track
415  *****************************************************************************/
416 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
417                             libvlc_exception_t *p_e )
418 {
419     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
420     vlc_value_t val_list;
421     vlc_value_t val;
422     int i_track = -1;
423     int i_ret = -1;
424     int i;
425
426     if( !p_input_thread )
427         return -1;
428
429     i_ret = var_Get( p_input_thread, "audio-es", &val );
430     if( i_ret < 0 )
431     {
432         libvlc_exception_raise( p_e, "Getting Audio track information failed" );
433         vlc_object_release( p_input_thread );
434         return i_ret;
435     }
436
437     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
438     for( i = 0; i < val_list.p_list->i_count; i++ )
439     {
440         vlc_value_t track_val = val_list.p_list->p_values[i];
441         if( track_val.i_int == val.i_int )
442         {
443             i_track = i;
444             break;
445        }
446     }
447     vlc_object_release( p_input_thread );
448     return i_track;
449 }
450
451 /*****************************************************************************
452  * libvlc_audio_set_track : Set the current audio track
453  *****************************************************************************/
454 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
455                              libvlc_exception_t *p_e )
456 {
457     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
458     vlc_value_t val_list;
459     vlc_value_t newval;
460     int i_ret = -1;
461
462     if( !p_input_thread )
463         return;
464
465     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
466     if( (i_track < 0) && (i_track > val_list.p_list->i_count) )
467     {
468         libvlc_exception_raise( p_e, "Audio track out of range" );
469         vlc_object_release( p_input_thread );
470         return;
471     }
472
473     newval = val_list.p_list->p_values[i_track];
474     i_ret = var_Set( p_input_thread, "audio-es", newval );
475     if( i_ret < 0 )
476     {
477         libvlc_exception_raise( p_e, "Setting audio track failed" );
478     }
479     vlc_object_release( p_input_thread );
480 }
481
482 /*****************************************************************************
483  * libvlc_audio_get_channel : Get the current audio channel
484  *****************************************************************************/
485 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
486                               libvlc_exception_t *p_e )
487 {
488     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
489     if( p_aout )
490     {
491         vlc_value_t val;
492
493         var_Get( p_aout, "audio-channels", &val );
494         vlc_object_release( p_aout );
495         return val.i_int;
496     }
497     libvlc_exception_raise( p_e, "Unable to get audio output" );
498     return libvlc_AudioChannel_Error;
499 }
500
501 /*****************************************************************************
502  * libvlc_audio_set_channel : Set the current audio channel
503  *****************************************************************************/
504 void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
505                                int channel,
506                                libvlc_exception_t *p_e )
507 {
508     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
509     if( p_aout )
510     {
511         vlc_value_t val;
512         int i_ret = -1;
513
514         val.i_int = channel;
515         i_ret = var_Set( p_aout, "audio-channels", val );
516         if( i_ret < 0 )
517             libvlc_exception_raise( p_e, "Failed setting audio channel" );
518
519         vlc_object_release( p_aout );
520     }
521
522 }