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