]> git.sesse.net Git - vlc/blob - lib/audio.c
LGPL
[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
31 #include <vlc/libvlc.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_media_player.h>
34
35 #include <vlc_common.h>
36 #include <vlc_input.h>
37 #include <vlc_aout_intf.h>
38 #include <vlc_aout.h>
39 #include <vlc_modules.h>
40
41 #include "libvlc_internal.h"
42 #include "media_player_internal.h"
43
44 /*
45  * Remember to release the returned audio_output_t since it is locked at
46  * the end of this function.
47  */
48 static audio_output_t *GetAOut( libvlc_media_player_t *mp )
49 {
50     assert( mp != NULL );
51
52     input_thread_t *p_input = libvlc_get_input_thread( mp );
53     if( p_input == NULL )
54         return NULL;
55
56     audio_output_t * p_aout = input_GetAout( p_input );
57     vlc_object_release( p_input );
58     if( p_aout == NULL )
59         libvlc_printerr( "No active audio output" );
60     return p_aout;
61 }
62
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 {
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_printerr( "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 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 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
136 {
137     char *value;
138
139     if( !module_exists( psz_name )
140      || asprintf( &value, "%s,none", psz_name ) == -1 )
141         return -1;
142     var_SetString( mp, "aout", value );
143     free( value );
144     return 0;
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;
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
175     free( psz_config_name );
176     return 0;
177 }
178
179 /********************************
180  * Get long name of device
181  *********************************/
182 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
183                                             const char *psz_audio_output,
184                                             int i_device )
185 {
186     char *psz_config_name;
187     if( !psz_audio_output )
188         return NULL;
189     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
190         return NULL;
191
192     module_config_t *p_module_config = config_FindConfig(
193         VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
194
195     if( p_module_config )
196     {
197         // refresh if there arent devices
198         if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
199         {
200             vlc_value_t val;
201             val.psz_string = strdup( p_module_config->value.psz );
202
203             p_module_config->pf_update_list(
204                 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
205             free( val.psz_string );
206         }
207
208         if( i_device >= 0 && i_device < p_module_config->i_list )
209         {
210             free( psz_config_name );
211
212             if( p_module_config->ppsz_list_text[i_device] )
213                 return strdup( p_module_config->ppsz_list_text[i_device] );
214             else
215                 return strdup( p_module_config->ppsz_list[i_device] );
216         }
217     }
218
219     free( psz_config_name );
220     return NULL;
221 }
222
223 /********************************
224  * Get id name of device
225  *********************************/
226 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
227                                       const char *psz_audio_output,
228                                       int i_device )
229 {
230     char *psz_config_name;
231     if( !psz_audio_output )
232         return NULL;
233     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
234         return NULL;
235
236     module_config_t *p_module_config = config_FindConfig(
237         VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
238
239     if( p_module_config )
240     {
241         // refresh if there arent devices
242         if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
243         {
244             vlc_value_t val;
245             val.psz_string = strdup( p_module_config->value.psz );
246
247             p_module_config->pf_update_list(
248                 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
249             free( val.psz_string );
250         }
251
252         if( i_device >= 0 && i_device < p_module_config->i_list )
253         {
254             free( psz_config_name );
255             return strdup( p_module_config->ppsz_list[i_device] );
256         }
257     }
258
259     free( psz_config_name );
260     return NULL;
261 }
262
263 /*****************************
264  * Set device for using
265  *****************************/
266 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
267                                      const char *psz_audio_output,
268                                      const char *psz_device_id )
269 {
270     char *psz_config_name;
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     if( !var_Type( mp, psz_config_name ) )
276         /* Don't recreate the same variable over and over and over... */
277         var_Create( mp, psz_config_name, VLC_VAR_STRING );
278     var_SetString( mp, psz_config_name, psz_device_id );
279     free( psz_config_name );
280 }
281
282 /*****************************************************************************
283  * libvlc_audio_output_get_device_type : Get the current audio device type
284  *****************************************************************************/
285 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
286 {
287     audio_output_t *p_aout = GetAOut( mp );
288     if( p_aout )
289     {
290         int i_device_type = var_GetInteger( p_aout, "audio-device" );
291         vlc_object_release( p_aout );
292         return i_device_type;
293     }
294     return libvlc_AudioOutputDevice_Error;
295 }
296
297 /*****************************************************************************
298  * libvlc_audio_output_set_device_type : Set the audio device type
299  *****************************************************************************/
300 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
301                                           int device_type )
302 {
303     audio_output_t *p_aout = GetAOut( mp );
304     if( !p_aout )
305         return;
306     if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
307         libvlc_printerr( "Error setting audio device" );
308     vlc_object_release( p_aout );
309 }
310
311 /*****************************************************************************
312  * libvlc_audio_get_mute : Get the volume state, true if muted
313  *****************************************************************************/
314 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
315 {
316     aout_ToggleMute( mp, NULL );
317 }
318
319 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
320 {
321     return aout_IsMuted( VLC_OBJECT(mp) );
322 }
323
324 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
325 {
326     aout_SetMute( VLC_OBJECT(mp), NULL, !!mute );
327 }
328
329 /*****************************************************************************
330  * libvlc_audio_get_volume : Get the current volume
331  *****************************************************************************/
332 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
333 {
334     unsigned volume = aout_VolumeGet( mp );
335
336     return (volume * 100 + AOUT_VOLUME_DEFAULT / 2) / AOUT_VOLUME_DEFAULT;
337 }
338
339
340 /*****************************************************************************
341  * libvlc_audio_set_volume : Set the current volume
342  *****************************************************************************/
343 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
344 {
345     volume = (volume * AOUT_VOLUME_DEFAULT + 50) / 100;
346     if (volume < 0 || volume > AOUT_VOLUME_MAX)
347     {
348         libvlc_printerr( "Volume out of range" );
349         return -1;
350     }
351     aout_VolumeSet (mp, volume);
352     return 0;
353 }
354
355 /*****************************************************************************
356  * libvlc_audio_get_track_count : Get the number of available audio tracks
357  *****************************************************************************/
358 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
359 {
360     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
361     int i_track_count;
362
363     if( !p_input_thread )
364         return -1;
365
366     i_track_count = var_CountChoices( p_input_thread, "audio-es" );
367
368     vlc_object_release( p_input_thread );
369     return i_track_count;
370 }
371
372 /*****************************************************************************
373  * libvlc_audio_get_track_description : Get the description of available audio tracks
374  *****************************************************************************/
375 libvlc_track_description_t *
376         libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
377 {
378     return libvlc_get_track_description( p_mi, "audio-es" );
379 }
380
381 /*****************************************************************************
382  * libvlc_audio_get_track : Get the current audio track
383  *****************************************************************************/
384 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
385 {
386     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
387     vlc_value_t val_list;
388     vlc_value_t val;
389     int i_track = -1;
390     int i;
391
392     if( !p_input_thread )
393         return -1;
394
395     if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
396     {
397         vlc_object_release( p_input_thread );
398         libvlc_printerr( "Audio track information not found" );
399         return -1;
400     }
401
402     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
403     for( i = 0; i < val_list.p_list->i_count; i++ )
404     {
405         if( val_list.p_list->p_values[i].i_int == val.i_int )
406         {
407             i_track = i;
408             break;
409         }
410     }
411     var_FreeList( &val_list, NULL );
412     vlc_object_release( p_input_thread );
413     return i_track;
414 }
415
416 /*****************************************************************************
417  * libvlc_audio_set_track : Set the current audio track
418  *****************************************************************************/
419 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
420 {
421     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
422     vlc_value_t val_list;
423     vlc_value_t newval;
424     int i_ret;
425
426     if( !p_input_thread )
427         return -1;
428
429     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
430     if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
431     {
432         libvlc_printerr( "Audio track out of range" );
433         i_ret = -1;
434         goto end;
435     }
436
437     newval = val_list.p_list->p_values[i_track];
438     i_ret = var_Set( p_input_thread, "audio-es", newval );
439     if( i_ret < 0 )
440     {
441         libvlc_printerr( "Audio track out of range" ); /* Race... */
442         i_ret = -1;
443         goto end;
444     }
445     i_ret = 0;
446
447 end:
448     var_FreeList( &val_list, NULL );
449     vlc_object_release( p_input_thread );
450     return i_ret;
451 }
452
453 /*****************************************************************************
454  * libvlc_audio_get_channel : Get the current audio channel
455  *****************************************************************************/
456 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
457 {
458     audio_output_t *p_aout = GetAOut( mp );
459     if( !p_aout )
460         return 0;
461
462     int val = var_GetInteger( p_aout, "audio-channels" );
463     vlc_object_release( p_aout );
464     return val;
465 }
466
467 /*****************************************************************************
468  * libvlc_audio_set_channel : Set the current audio channel
469  *****************************************************************************/
470 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
471 {
472     audio_output_t *p_aout = GetAOut( mp );
473     int ret = 0;
474
475     if( !p_aout )
476         return -1;
477
478     if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
479     {
480         libvlc_printerr( "Audio channel out of range" );
481         ret = -1;
482     }
483     vlc_object_release( p_aout );
484     return ret;
485 }
486
487 /*****************************************************************************
488  * libvlc_audio_get_delay : Get the current audio delay
489  *****************************************************************************/
490 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
491 {
492     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
493     int64_t val = 0;
494     if( p_input_thread != NULL )
495     {
496       val = var_GetTime( p_input_thread, "audio-delay" );
497       vlc_object_release( p_input_thread );
498     }
499     return val;
500 }
501
502 /*****************************************************************************
503  * libvlc_audio_set_delay : Set the current audio delay
504  *****************************************************************************/
505 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
506 {
507     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
508     int ret = 0;
509     if( p_input_thread != NULL )
510     {
511       var_SetTime( p_input_thread, "audio-delay", i_delay );
512       vlc_object_release( p_input_thread );
513     }
514     else
515     {
516       ret = -1;
517     }
518     return ret;
519 }