]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
5aeccfef6bab0f6329860484815002f382034277
[vlc] / src / audio_output / intf.c
1 /*****************************************************************************
2  * intf.c : audio output API towards the interface modules
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #include "audio_output.h"
33 #include "aout_internal.h"
34
35
36 /*
37  * Volume management
38  *
39  * The hardware volume cannot be set if the output module gets deleted, so
40  * we must take the mixer lock. The software volume cannot be set while the
41  * mixer is running, so we need the mixer lock (too).
42  *
43  * Here is a schematic of the i_volume range :
44  *
45  * |------------------------------+---------------------------------------|
46  * 0                           pi_soft                                   1024
47  *
48  * Between 0 and pi_soft, the volume is done in hardware by the output
49  * module. Above, the output module will change p_aout->mixer.i_multiplier
50  * (done in software). This scaling may result * in cropping errors and
51  * should be avoided as much as possible.
52  *
53  * It is legal to have *pi_soft == 0, and do everything in software.
54  * It is also legal to have *pi_soft == 1024, and completely avoid
55  * software scaling. However, some streams (esp. A/52) are encoded with
56  * a very low volume and users may complain.
57  */
58
59 /*****************************************************************************
60  * aout_VolumeGet : get the volume of the output device
61  *****************************************************************************/
62 int __aout_VolumeGet( vlc_object_t * p_object, audio_volume_t * pi_volume )
63 {
64     int i;
65
66     i = config_GetInt( p_object, "volume" );
67     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
68
69     return 0;
70 }
71
72 /*****************************************************************************
73  * aout_VolumeSet : set the volume of the output device
74  *****************************************************************************/
75 int __aout_VolumeSet( vlc_object_t * p_object, audio_volume_t i_volume )
76 {
77     vlc_value_t val;
78     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
79                                                 FIND_ANYWHERE );
80     int i_result = 0;
81
82     config_PutInt( p_object, "volume", i_volume );
83
84     if ( p_aout == NULL ) return 0;
85
86     vlc_mutex_lock( &p_aout->mixer_lock );
87     if ( !p_aout->mixer.b_error )
88     {
89         i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
90     }
91     vlc_mutex_unlock( &p_aout->mixer_lock );
92
93     vlc_object_release( p_aout );
94
95     val.b_bool = VLC_TRUE;
96     var_Set( p_aout, "intf-change", val );
97     return i_result;
98 }
99
100 /*****************************************************************************
101  * aout_VolumeInfos : get the boundary pi_soft
102  *****************************************************************************/
103 int __aout_VolumeInfos( vlc_object_t * p_object, audio_volume_t * pi_soft )
104 {
105     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
106                                                 FIND_ANYWHERE );
107     int i_result;
108
109     if ( p_aout == NULL ) return 0;
110
111     vlc_mutex_lock( &p_aout->mixer_lock );
112     if ( p_aout->mixer.b_error )
113     {
114         /* The output module is destroyed. */
115         i_result = -1;
116     }
117     else
118     {
119         i_result = p_aout->output.pf_volume_infos( p_aout, pi_soft );
120     }
121     vlc_mutex_unlock( &p_aout->mixer_lock );
122
123     vlc_object_release( p_aout );
124     return i_result;
125 }
126
127 /*****************************************************************************
128  * aout_VolumeUp : raise the output volume
129  *****************************************************************************
130  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
131  * function.
132  *****************************************************************************/
133 int __aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
134                    audio_volume_t * pi_volume )
135 {
136     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
137                                                 FIND_ANYWHERE );
138     int i_result = 0, i;
139
140     i = config_GetInt( p_object, "volume" );
141     i += AOUT_VOLUME_STEP * i_nb_steps;
142     if ( i > AOUT_VOLUME_MAX )
143     {
144         i = AOUT_VOLUME_MAX;
145     }
146     config_PutInt( p_object, "volume", i );
147     var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
148     var_SetInteger( p_object->p_libvlc, "saved-volume" , i );
149     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
150
151     if ( p_aout == NULL ) return 0;
152
153     vlc_mutex_lock( &p_aout->mixer_lock );
154     if ( !p_aout->mixer.b_error )
155     {
156         i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t)i );
157     }
158     vlc_mutex_unlock( &p_aout->mixer_lock );
159
160     vlc_object_release( p_aout );
161     return i_result;
162 }
163
164 /*****************************************************************************
165  * aout_VolumeDown : lower the output volume
166  *****************************************************************************
167  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
168  * function.
169  *****************************************************************************/
170 int __aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
171                      audio_volume_t * pi_volume )
172 {
173     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
174                                                 FIND_ANYWHERE );
175     int i_result = 0, i;
176
177     i = config_GetInt( p_object, "volume" );
178     i -= AOUT_VOLUME_STEP * i_nb_steps;
179     if ( i < AOUT_VOLUME_MIN )
180     {
181         i = AOUT_VOLUME_MIN;
182     }
183     config_PutInt( p_object, "volume", i );
184     var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
185     var_SetInteger( p_object->p_libvlc, "saved-volume", i );
186     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
187
188     if ( p_aout == NULL ) return 0;
189
190     vlc_mutex_lock( &p_aout->mixer_lock );
191     if ( !p_aout->mixer.b_error )
192     {
193         i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t)i );
194     }
195     vlc_mutex_unlock( &p_aout->mixer_lock );
196
197     vlc_object_release( p_aout );
198     return i_result;
199 }
200
201 /*****************************************************************************
202  * aout_VolumeMute : Mute/un-mute the output volume
203  *****************************************************************************
204  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
205  * function (muted => 0).
206  *****************************************************************************/
207 int __aout_VolumeMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
208 {
209     int i_result;
210     audio_volume_t i_volume;
211
212     i_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
213     if ( i_volume != 0 )
214     {
215         /* Mute */
216         i_result = aout_VolumeSet( p_object, AOUT_VOLUME_MIN );
217         var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
218         var_SetInteger( p_object->p_libvlc, "saved-volume", (int)i_volume );
219         if ( pi_volume != NULL ) *pi_volume = AOUT_VOLUME_MIN;
220     }
221     else
222     {
223         /* Un-mute */
224         var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
225         i_volume = (audio_volume_t)var_GetInteger( p_object->p_libvlc,
226                                                    "saved-volume" );
227         i_result = aout_VolumeSet( p_object, i_volume );
228         if ( pi_volume != NULL ) *pi_volume = i_volume;
229     }
230
231     return i_result;
232 }
233
234 /*
235  * The next functions are not supposed to be called by the interface, but
236  * are placeholders for software-only scaling.
237  */
238
239 /* Meant to be called by the output plug-in's Open(). */
240 void aout_VolumeSoftInit( aout_instance_t * p_aout )
241 {
242     int i_volume;
243
244     p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
245     p_aout->output.pf_volume_get = aout_VolumeSoftGet;
246     p_aout->output.pf_volume_set = aout_VolumeSoftSet;
247
248     i_volume = config_GetInt( p_aout, "volume" );
249     if ( i_volume < AOUT_VOLUME_MIN )
250     {
251         i_volume = AOUT_VOLUME_DEFAULT;
252     }
253     else if ( i_volume > AOUT_VOLUME_MAX )
254     {
255         i_volume = AOUT_VOLUME_MAX;
256     }
257
258     aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
259 }
260
261 /* Placeholder for pf_volume_infos(). */
262 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
263 {
264     *pi_soft = 0;
265     return 0;
266 }
267
268 /* Placeholder for pf_volume_get(). */
269 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
270 {
271     *pi_volume = p_aout->output.i_volume;
272     return 0;
273 }
274
275
276 /* Placeholder for pf_volume_set(). */
277 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
278 {
279     aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
280     p_aout->output.i_volume = i_volume;
281     return 0;
282 }
283
284 /*
285  * The next functions are not supposed to be called by the interface, but
286  * are placeholders for unsupported scaling.
287  */
288
289 /* Meant to be called by the output plug-in's Open(). */
290 void aout_VolumeNoneInit( aout_instance_t * p_aout )
291 {
292     p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
293     p_aout->output.pf_volume_get = aout_VolumeNoneGet;
294     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
295 }
296
297 /* Placeholder for pf_volume_infos(). */
298 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
299 {
300     return -1;
301 }
302
303 /* Placeholder for pf_volume_get(). */
304 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
305 {
306     return -1;
307 }
308
309 /* Placeholder for pf_volume_set(). */
310 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
311 {
312     return -1;
313 }
314
315
316 /*
317  * Pipelines management
318  */
319
320 /*****************************************************************************
321  * aout_Restart : re-open the output device and rebuild the input and output
322  *                pipelines
323  *****************************************************************************
324  * This function is used whenever the parameters of the output plug-in are
325  * changed (eg. selecting S/PDIF or PCM).
326  *****************************************************************************/
327 int aout_Restart( aout_instance_t * p_aout )
328 {
329     int i;
330     vlc_bool_t b_error = 0;
331
332     vlc_mutex_lock( &p_aout->mixer_lock );
333
334     if ( p_aout->i_nb_inputs == 0 )
335     {
336         vlc_mutex_unlock( &p_aout->mixer_lock );
337         msg_Err( p_aout, "no decoder thread" );
338         return -1;
339     }
340
341     /* Lock all inputs. */
342     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
343     {
344         vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
345         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
346     }
347
348     aout_MixerDelete( p_aout );
349
350     /* Re-open the output plug-in. */
351     aout_OutputDelete( p_aout );
352
353     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
354     {
355         /* Release all locks and report the error. */
356         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
357         {
358             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
359         }
360         vlc_mutex_unlock( &p_aout->mixer_lock );
361         return -1;
362     }
363
364     if ( aout_MixerNew( p_aout ) == -1 )
365     {
366         aout_OutputDelete( p_aout );
367         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
368         {
369             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
370         }
371         vlc_mutex_unlock( &p_aout->mixer_lock );
372         return -1;
373     }
374
375     /* Re-open all inputs. */
376     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
377     {
378         aout_input_t * p_input = p_aout->pp_inputs[i];
379
380         b_error |= aout_InputNew( p_aout, p_input );
381         p_input->b_changed = 1;
382         vlc_mutex_unlock( &p_input->lock );
383     }
384
385     vlc_mutex_unlock( &p_aout->mixer_lock );
386
387     return b_error;
388 }
389
390 /*****************************************************************************
391  * aout_FindAndRestart : find the audio output instance and restart
392  *****************************************************************************
393  * This is used for callbacks of the configuration variables, and we believe
394  * that when those are changed, it is a significant change which implies
395  * rebuilding the audio-device and audio-channels variables.
396  *****************************************************************************/
397 int aout_FindAndRestart( vlc_object_t * p_this, const char *psz_name,
398                          vlc_value_t oldval, vlc_value_t val, void *p_data )
399 {
400     aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
401                                                 FIND_ANYWHERE );
402
403     if ( p_aout == NULL ) return VLC_SUCCESS;
404
405     if ( var_Type( p_aout, "audio-device" ) != 0 )
406     {
407         var_Destroy( p_aout, "audio-device" );
408     }
409     if ( var_Type( p_aout, "audio-channels" ) != 0 )
410     {
411         var_Destroy( p_aout, "audio-channels" );
412     }
413
414     aout_Restart( p_aout );
415     vlc_object_release( p_aout );
416
417     return VLC_SUCCESS;
418 }
419
420 /*****************************************************************************
421  * aout_ChannelsRestart : change the audio device or channels and restart
422  *****************************************************************************/
423 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
424                           vlc_value_t old_value, vlc_value_t new_value,
425                           void * unused )
426 {
427     aout_instance_t * p_aout = (aout_instance_t *)p_this;
428
429     if ( !strcmp( psz_variable, "audio-device" ) )
430     {
431         /* This is supposed to be a significant change and supposes
432          * rebuilding the channel choices. */
433         if ( var_Type( p_aout, "audio-channels" ) >= 0 )
434         {
435             var_Destroy( p_aout, "audio-channels" );
436         }
437     }
438     aout_Restart( p_aout );
439     return 0;
440 }