]> git.sesse.net Git - vlc/blob - lib/audio.c
Qt: fallback on the high-resolution icons instead of the xpm
[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 #include <math.h>
31
32 #include <vlc/libvlc.h>
33 #include <vlc/libvlc_media.h>
34 #include <vlc/libvlc_media_player.h>
35
36 #include <vlc_common.h>
37 #include <vlc_input.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     audio_output_t *p_aout = input_resource_HoldAout( mp->input.p_resource );
53     if( p_aout == NULL )
54         libvlc_printerr( "No active audio output" );
55     return p_aout;
56 }
57
58 /*****************************************
59  * Get the list of available audio outputs
60  *****************************************/
61 libvlc_audio_output_t *
62         libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
63 {
64     size_t count;
65     module_t **module_list = module_list_get( &count );
66     libvlc_audio_output_t *list = NULL;
67
68     for (size_t i = 0; i < count; i++)
69     {
70         module_t *module = module_list[i];
71
72         if( !module_provides( module, "audio output" ) )
73             continue;
74
75         libvlc_audio_output_t *item = malloc( sizeof( *item ) );
76         if( unlikely(item == NULL) )
77         {
78     error:
79             libvlc_printerr( "Not enough memory" );
80             libvlc_audio_output_list_release( list );
81             list = NULL;
82             break;
83         }
84
85         item->psz_name = strdup( module_get_object( module ) );
86         item->psz_description = strdup( module_get_name( module, true ) );
87         if( unlikely(item->psz_name == NULL || item->psz_description == NULL) )
88         {
89             free( item );
90             goto error;
91         }
92         item->p_next = list;
93         list = item;
94     }
95     module_list_free( module_list );
96
97     VLC_UNUSED( p_instance );
98     return list;
99 }
100
101 /********************************************
102  * Free the list of available audio outputs
103  ***********************************************/
104 void libvlc_audio_output_list_release( libvlc_audio_output_t *list )
105 {
106     while( list != NULL )
107     {
108         libvlc_audio_output_t *next = list->p_next;
109
110         free( list->psz_name );
111         free( list->psz_description );
112         free( list );
113         list = next;
114     }
115 }
116
117
118 /***********************
119  * Set the audio output.
120  ***********************/
121 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
122 {
123     char *value;
124
125     if( !module_exists( psz_name )
126      || asprintf( &value, "%s,none", psz_name ) == -1 )
127         return -1;
128     var_SetString( mp, "aout", value );
129     free( value );
130     return 0;
131 }
132
133 libvlc_audio_output_device_t *
134 libvlc_audio_output_device_list_get( libvlc_instance_t *p_instance,
135                                      const char *aout )
136 {
137     char varname[32];
138     if( (size_t)snprintf( varname, sizeof(varname), "%s-output-device", aout )
139                                                            >= sizeof(varname) )
140         return NULL;
141
142     libvlc_audio_output_device_t *list = NULL, **pp = &list;
143     char **values, **texts;
144     ssize_t count = config_GetPszChoices( VLC_OBJECT(p_instance->p_libvlc_int),
145                                           varname, &values, &texts );
146     for( ssize_t i = 0; i < count; i++ )
147     {
148         libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
149         if( unlikely(item == NULL) )
150             break;
151
152         *pp = item;
153         pp = &item->p_next;
154         item->psz_device = values[i];
155         item->psz_description = texts[i];
156     }
157
158     *pp = NULL;
159     free( texts );
160     free( values );
161     (void) p_instance;
162     return list;
163 }
164
165 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
166 {
167     while( l != NULL )
168     {
169         libvlc_audio_output_device_t *next = l->p_next;
170
171         free( l->psz_description );
172         free( l->psz_device );
173         free( l );
174         l = next;
175     }
176 }
177
178 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
179                                       const char *psz_audio_output )
180 {
181     (void) p_instance; (void) psz_audio_output;
182     return 0;
183 }
184
185 char *libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
186                                            const char *psz_audio_output,
187                                            int i_device )
188 {
189     (void) p_instance; (void) psz_audio_output; (void) i_device;
190     return NULL;
191 }
192
193 char *libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
194                                      const char *psz_audio_output,
195                                      int i_device )
196 {
197     (void) p_instance; (void) psz_audio_output; (void) i_device;
198     return NULL;
199 }
200
201 /*****************************
202  * Set device for using
203  *****************************/
204 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
205                                      const char *psz_audio_output,
206                                      const char *psz_device_id )
207 {
208     char *psz_config_name;
209     if( !psz_audio_output || !psz_device_id )
210         return;
211     if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
212         return;
213     if( !var_Type( mp, psz_config_name ) )
214         /* Don't recreate the same variable over and over and over... */
215         var_Create( mp, psz_config_name, VLC_VAR_STRING );
216     var_SetString( mp, psz_config_name, psz_device_id );
217     free( psz_config_name );
218 }
219
220 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
221 {
222     (void) mp;
223     return libvlc_AudioOutputDevice_Error;
224 }
225
226 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
227                                           int device_type )
228 {
229     (void) mp; (void) device_type;
230 }
231
232 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
233 {
234     int mute = libvlc_audio_get_mute( mp );
235     if( mute != -1 )
236         libvlc_audio_set_mute( mp, !mute );
237 }
238
239 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
240 {
241     int mute = -1;
242
243     audio_output_t *aout = GetAOut( mp );
244     if( aout != NULL )
245     {
246         mute = aout_MuteGet( aout );
247         vlc_object_release( aout );
248     }
249     return mute;
250 }
251
252 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
253 {
254     audio_output_t *aout = GetAOut( mp );
255     if( aout != NULL )
256     {
257         mute = aout_MuteSet( aout, mute );
258         vlc_object_release( aout );
259     }
260 }
261
262 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
263 {
264     int volume = -1;
265
266     audio_output_t *aout = GetAOut( mp );
267     if( aout != NULL )
268     {
269         float vol = aout_VolumeGet( aout );
270         vlc_object_release( aout );
271         volume = lroundf( vol * 100.f );
272     }
273     return volume;
274 }
275
276 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
277 {
278     float vol = volume / 100.f;
279     if (vol < 0.f)
280     {
281         libvlc_printerr( "Volume out of range" );
282         return -1;
283     }
284
285     int ret = -1;
286     audio_output_t *aout = GetAOut( mp );
287     if( aout != NULL )
288     {
289         ret = aout_VolumeSet( aout, vol );
290         vlc_object_release( aout );
291     }
292     return ret;
293 }
294
295 /*****************************************************************************
296  * libvlc_audio_get_track_count : Get the number of available audio tracks
297  *****************************************************************************/
298 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
299 {
300     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
301     int i_track_count;
302
303     if( !p_input_thread )
304         return -1;
305
306     i_track_count = var_CountChoices( p_input_thread, "audio-es" );
307
308     vlc_object_release( p_input_thread );
309     return i_track_count;
310 }
311
312 /*****************************************************************************
313  * libvlc_audio_get_track_description : Get the description of available audio tracks
314  *****************************************************************************/
315 libvlc_track_description_t *
316         libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
317 {
318     return libvlc_get_track_description( p_mi, "audio-es" );
319 }
320
321 /*****************************************************************************
322  * libvlc_audio_get_track : Get the current audio track
323  *****************************************************************************/
324 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
325 {
326     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
327     if( !p_input_thread )
328         return -1;
329
330     int id = var_GetInteger( p_input_thread, "audio-es" );
331     vlc_object_release( p_input_thread );
332     return id;
333 }
334
335 /*****************************************************************************
336  * libvlc_audio_set_track : Set the current audio track
337  *****************************************************************************/
338 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
339 {
340     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
341     vlc_value_t val_list;
342     int i_ret = -1;
343
344     if( !p_input_thread )
345         return -1;
346
347     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
348     for( int i = 0; i < val_list.p_list->i_count; i++ )
349     {
350         if( i_track == val_list.p_list->p_values[i].i_int )
351         {
352             if( var_SetInteger( p_input_thread, "audio-es", i_track ) < 0 )
353                 break;
354             i_ret = 0;
355             goto end;
356         }
357     }
358     libvlc_printerr( "Track identifier not found" );
359 end:
360     var_FreeList( &val_list, NULL );
361     vlc_object_release( p_input_thread );
362     return i_ret;
363 }
364
365 /*****************************************************************************
366  * libvlc_audio_get_channel : Get the current audio channel
367  *****************************************************************************/
368 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
369 {
370     audio_output_t *p_aout = GetAOut( mp );
371     if( !p_aout )
372         return 0;
373
374     int val = var_GetInteger( p_aout, "stereo-mode" );
375     vlc_object_release( p_aout );
376     return val;
377 }
378
379 /*****************************************************************************
380  * libvlc_audio_set_channel : Set the current audio channel
381  *****************************************************************************/
382 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
383 {
384     audio_output_t *p_aout = GetAOut( mp );
385     int ret = 0;
386
387     if( !p_aout )
388         return -1;
389
390     if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
391     {
392         libvlc_printerr( "Audio channel out of range" );
393         ret = -1;
394     }
395     vlc_object_release( p_aout );
396     return ret;
397 }
398
399 /*****************************************************************************
400  * libvlc_audio_get_delay : Get the current audio delay
401  *****************************************************************************/
402 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
403 {
404     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
405     int64_t val = 0;
406     if( p_input_thread != NULL )
407     {
408       val = var_GetTime( p_input_thread, "audio-delay" );
409       vlc_object_release( p_input_thread );
410     }
411     return val;
412 }
413
414 /*****************************************************************************
415  * libvlc_audio_set_delay : Set the current audio delay
416  *****************************************************************************/
417 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
418 {
419     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
420     int ret = 0;
421     if( p_input_thread != NULL )
422     {
423       var_SetTime( p_input_thread, "audio-delay", i_delay );
424       vlc_object_release( p_input_thread );
425     }
426     else
427     {
428       ret = -1;
429     }
430     return ret;
431 }