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