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