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