]> git.sesse.net Git - vlc/blob - src/control/audio.c
Kill one exception
[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 );
56         libvlc_printerr( "No active audio output" );
57         return NULL;
58     }
59
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_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;
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         free( psz_config_name );
208
209         if( i_device >= 0 && i_device < p_module_config->i_list )
210         {
211             if( p_module_config->ppsz_list_text[i_device] )
212                 return strdup( p_module_config->ppsz_list_text[i_device] );
213             else
214                 return strdup( p_module_config->ppsz_list[i_device] );
215         }
216     }
217
218     free( psz_config_name );
219     return NULL;
220 }
221
222 /********************************
223  * Get id name of device
224  *********************************/
225 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
226                                       const char *psz_audio_output,
227                                       int i_device )
228 {
229     char *psz_config_name;
230     if( !psz_audio_output )
231         return NULL;
232     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
233         return NULL;
234
235     module_config_t *p_module_config = config_FindConfig(
236         VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
237
238     if( p_module_config )
239     {
240         // refresh if there arent devices
241         if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
242         {
243             vlc_value_t val;
244             val.psz_string = strdup( p_module_config->value.psz );
245
246             p_module_config->pf_update_list(
247                 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
248             free( val.psz_string );
249         }
250         free( psz_config_name );
251
252         if( i_device >= 0 && i_device < p_module_config->i_list )
253             return strdup( p_module_config->ppsz_list[i_device] );
254
255     }
256
257     free( psz_config_name );
258     return NULL;
259 }
260
261 /*****************************
262  * Set device for using
263  *****************************/
264 void libvlc_audio_output_device_set( libvlc_instance_t *p_instance,
265                                                     const char *psz_audio_output,
266                                                     const char *psz_device_id )
267 {
268     char *psz_config_name = NULL;
269     if( !psz_audio_output || !psz_device_id )
270         return;
271     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
272         return;
273     config_PutPsz( p_instance->p_libvlc_int, psz_config_name, psz_device_id );
274     free( psz_config_name );
275 }
276
277 /*****************************************************************************
278  * libvlc_audio_output_get_device_type : Get the current audio device type
279  *****************************************************************************/
280 int libvlc_audio_output_get_device_type( libvlc_instance_t *p_instance,
281                                          libvlc_exception_t *p_e )
282 {
283     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
284     if( p_aout )
285     {
286         int i_device_type = var_GetInteger( p_aout, "audio-device" );
287         vlc_object_release( p_aout );
288         return i_device_type;
289     }
290     return libvlc_AudioOutputDevice_Error;
291 }
292
293 /*****************************************************************************
294  * libvlc_audio_output_set_device_type : Set the audio device type
295  *****************************************************************************/
296 void libvlc_audio_output_set_device_type( libvlc_instance_t *p_instance,
297                                           int device_type,
298                                           libvlc_exception_t *p_e )
299 {
300     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
301     if( !p_aout )
302         return;
303     if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
304     {
305         libvlc_exception_raise( p_e );
306         libvlc_printerr( "Error setting audio device" );
307     }
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_instance_t *p_instance )
315 {
316     aout_ToggleMute( p_instance->p_libvlc_int, NULL );
317 }
318
319 int libvlc_audio_get_mute( libvlc_instance_t *p_instance )
320 {
321     return (libvlc_audio_get_volume(p_instance) == 0);
322 }
323
324 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute )
325 {
326     if ( !mute != !libvlc_audio_get_mute( p_instance ) )
327     {
328         aout_ToggleMute( p_instance->p_libvlc_int, NULL );
329     }
330 }
331
332 /*****************************************************************************
333  * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
334  *****************************************************************************/
335 int libvlc_audio_get_volume( libvlc_instance_t *p_instance )
336 {
337     audio_volume_t i_volume;
338
339     aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
340
341     return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
342 }
343
344
345 /*****************************************************************************
346  * libvlc_audio_set_volume : Set the current volume
347  *****************************************************************************/
348 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
349                               libvlc_exception_t *p_e )
350 {
351     if( i_volume >= 0 && i_volume <= 200 )
352     {
353         i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
354
355         aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
356     }
357     else
358     {
359         libvlc_exception_raise( p_e );
360         libvlc_printerr( "Volume out of range" );
361     }
362 }
363
364 /*****************************************************************************
365  * libvlc_audio_get_track_count : Get the number of available audio tracks
366  *****************************************************************************/
367 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
368                                   libvlc_exception_t *p_e )
369 {
370     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
371     int i_track_count;
372
373     if( !p_input_thread )
374         return -1;
375
376     i_track_count = var_CountChoices( p_input_thread, "audio-es" );
377
378     vlc_object_release( p_input_thread );
379     return i_track_count;
380 }
381
382 /*****************************************************************************
383  * libvlc_audio_get_track_description : Get the description of available audio tracks
384  *****************************************************************************/
385 libvlc_track_description_t *
386         libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
387                                             libvlc_exception_t *p_e )
388 {
389     return libvlc_get_track_description( p_mi, "audio-es", p_e);
390 }
391
392 /*****************************************************************************
393  * libvlc_audio_get_track : Get the current audio track
394  *****************************************************************************/
395 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
396                             libvlc_exception_t *p_e )
397 {
398     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
399     vlc_value_t val_list;
400     vlc_value_t val;
401     int i_track = -1;
402     int i;
403
404     if( !p_input_thread )
405         return -1;
406
407     if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
408     {
409         vlc_object_release( p_input_thread );
410         libvlc_exception_raise( p_e );
411         libvlc_printerr( "Audio track information not found" );
412         return -1;
413     }
414
415     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
416     for( i = 0; i < val_list.p_list->i_count; i++ )
417     {
418         if( val_list.p_list->p_values[i].i_int == val.i_int )
419         {
420             i_track = i;
421             break;
422         }
423     }
424     var_FreeList( &val_list, NULL );
425     vlc_object_release( p_input_thread );
426     return i_track;
427 }
428
429 /*****************************************************************************
430  * libvlc_audio_set_track : Set the current audio track
431  *****************************************************************************/
432 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
433                              libvlc_exception_t *p_e )
434 {
435     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
436     vlc_value_t val_list;
437     vlc_value_t newval;
438     int i_ret = -1;
439
440     if( !p_input_thread )
441         return;
442
443     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
444     if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
445     {
446         libvlc_exception_raise( p_e );
447         libvlc_printerr( "Audio track out of range" );
448         goto end;
449     }
450
451     newval = val_list.p_list->p_values[i_track];
452     i_ret = var_Set( p_input_thread, "audio-es", newval );
453     if( i_ret < 0 )
454     {
455         libvlc_exception_raise( p_e );
456         libvlc_printerr( "Audio track out of range" ); /* Race... */
457     }
458
459 end:
460     var_FreeList( &val_list, NULL );
461     vlc_object_release( p_input_thread );
462 }
463
464 /*****************************************************************************
465  * libvlc_audio_get_channel : Get the current audio channel
466  *****************************************************************************/
467 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
468                               libvlc_exception_t *p_e )
469 {
470     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
471     if( !p_aout )
472         return 0;
473
474     int val = var_GetInteger( p_aout, "audio-channels" );
475     vlc_object_release( p_aout );
476     return val;
477 }
478
479 /*****************************************************************************
480  * libvlc_audio_set_channel : Set the current audio channel
481  *****************************************************************************/
482 void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
483                                int channel,
484                                libvlc_exception_t *p_e )
485 {
486     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
487     if( !p_aout )
488         return;
489
490     if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
491     {
492         libvlc_exception_raise( p_e );
493         libvlc_printerr( "Audio channel out of range" );
494     }
495     vlc_object_release( p_aout );
496 }