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