]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
1c6bf14308640d67968e96d6fd04a2feb6dd28d9
[vlc] / src / audio_output / intf.c
1 /*****************************************************************************
2  * intf.c : audio output API towards the interface modules
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: intf.c,v 1.15 2003/01/21 10:29:12 massiot Exp $
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( aout_instance_t * p_aout, audio_volume_t * pi_volume )
63 {
64     int i_result;
65
66     vlc_mutex_lock( &p_aout->mixer_lock );
67
68     if ( p_aout->mixer.b_error )
69     {
70         int i;
71         /* The output module is destroyed. */
72         vlc_mutex_unlock( &p_aout->mixer_lock );
73         i = config_GetInt( p_aout, "volume" );
74         if (pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
75         return 0;
76     }
77
78     i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
79
80     vlc_mutex_unlock( &p_aout->mixer_lock );
81     return i_result;
82 }
83
84 /*****************************************************************************
85  * aout_VolumeSet : set the volume of the output device
86  *****************************************************************************/
87 int aout_VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
88 {
89     int i_result;
90
91     vlc_mutex_lock( &p_aout->mixer_lock );
92
93     if ( p_aout->mixer.b_error )
94     {
95         /* The output module is destroyed. */
96         vlc_mutex_unlock( &p_aout->mixer_lock );
97         config_PutInt( p_aout, "volume", i_volume );
98         return 0;
99     }
100
101     i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
102
103     vlc_mutex_unlock( &p_aout->mixer_lock );
104     return i_result;
105 }
106
107 /*****************************************************************************
108  * aout_VolumeInfos : get the boundary pi_soft
109  *****************************************************************************/
110 int aout_VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
111 {
112     int i_result;
113
114     vlc_mutex_lock( &p_aout->mixer_lock );
115
116     if ( p_aout->mixer.b_error )
117     {
118         /* The output module is destroyed. */
119         vlc_mutex_unlock( &p_aout->mixer_lock );
120         msg_Err( p_aout, "VolumeInfos called without output module" );
121         return -1;
122     }
123
124     i_result = p_aout->output.pf_volume_infos( p_aout, pi_soft );
125
126     vlc_mutex_unlock( &p_aout->mixer_lock );
127     return i_result;
128 }
129
130 /*****************************************************************************
131  * aout_VolumeUp : raise the output volume
132  *****************************************************************************
133  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
134  * function.
135  *****************************************************************************/
136 int aout_VolumeUp( aout_instance_t * p_aout, int i_nb_steps,
137                    audio_volume_t * pi_volume )
138 {
139     int i_result;
140     audio_volume_t i_volume;
141
142     vlc_mutex_lock( &p_aout->mixer_lock );
143
144     if ( p_aout->mixer.b_error )
145     {
146         int i;
147         /* The output module is destroyed. */
148         vlc_mutex_unlock( &p_aout->mixer_lock );
149         i = config_GetInt( p_aout, "volume" );
150         i += AOUT_VOLUME_STEP * i_nb_steps;
151         if ( i > AOUT_VOLUME_MAX )
152         {
153             i = AOUT_VOLUME_MAX;
154         }
155         config_PutInt( p_aout, "volume", i );
156         if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
157         return 0;
158     }
159
160     if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
161     {
162         vlc_mutex_unlock( &p_aout->mixer_lock );
163         return -1;
164     }
165
166     i_volume += AOUT_VOLUME_STEP * i_nb_steps;
167     if ( i_volume > AOUT_VOLUME_MAX ) i_volume = AOUT_VOLUME_MAX;
168
169     i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
170
171     vlc_mutex_unlock( &p_aout->mixer_lock );
172
173     if ( pi_volume != NULL ) *pi_volume = i_volume;
174     return i_result;
175 }
176
177 /*****************************************************************************
178  * aout_VolumeDown : lower the output volume
179  *****************************************************************************
180  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
181  * function.
182  *****************************************************************************/
183 int aout_VolumeDown( aout_instance_t * p_aout, int i_nb_steps,
184                      audio_volume_t * pi_volume )
185 {
186     int i_result;
187     audio_volume_t i_volume;
188
189     vlc_mutex_lock( &p_aout->mixer_lock );
190
191     if ( p_aout->mixer.b_error )
192     {
193         int i;
194         /* The output module is destroyed. */
195         vlc_mutex_unlock( &p_aout->mixer_lock );
196         i = config_GetInt( p_aout, "volume" );
197         i -= AOUT_VOLUME_STEP * i_nb_steps;
198         if ( i < 0 )
199         {
200             i = AOUT_VOLUME_MAX;
201         }
202         config_PutInt( p_aout, "volume", i );
203         if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
204         return 0;
205     }
206
207     if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
208     {
209         vlc_mutex_unlock( &p_aout->mixer_lock );
210         return -1;
211     }
212
213     if ( i_volume < AOUT_VOLUME_STEP * i_nb_steps )
214         i_volume = 0;
215     else
216         i_volume -= AOUT_VOLUME_STEP * i_nb_steps;
217
218     i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
219
220     vlc_mutex_unlock( &p_aout->mixer_lock );
221
222     if ( pi_volume != NULL ) *pi_volume = i_volume;
223     return i_result;
224 }
225
226 /*****************************************************************************
227  * aout_VolumeMute : Mute/un-mute the output volume
228  *****************************************************************************
229  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
230  * function (muted => 0).
231  *****************************************************************************/
232 int aout_VolumeMute( aout_instance_t * p_aout, audio_volume_t * pi_volume )
233 {
234     int i_result;
235     audio_volume_t i_volume;
236
237     vlc_mutex_lock( &p_aout->mixer_lock );
238
239     if ( p_aout->mixer.b_error )
240     {
241         /* The output module is destroyed. */
242         vlc_mutex_unlock( &p_aout->mixer_lock );
243         config_PutInt( p_aout, "volume", 0 );
244         if ( pi_volume != NULL ) *pi_volume = 0;
245         return 0;
246     }
247
248     if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
249     {
250         vlc_mutex_unlock( &p_aout->mixer_lock );
251         return -1;
252     }
253
254     if ( i_volume == 0 )
255     {
256         i_volume = p_aout->output.i_saved_volume;
257     }
258     else
259     {
260         p_aout->output.i_saved_volume = i_volume;
261         i_volume = 0;
262     }
263
264     i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
265
266     vlc_mutex_unlock( &p_aout->mixer_lock );
267
268     if ( pi_volume != NULL ) *pi_volume = i_volume;
269     return i_result;
270 }
271
272 /*
273  * The next functions are not supposed to be called by the interface, but
274  * are placeholders for software-only scaling.
275  */
276
277 /* Meant to be called by the output plug-in's Open(). */
278 void aout_VolumeSoftInit( aout_instance_t * p_aout )
279 {
280     int i_volume;
281
282     p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
283     p_aout->output.pf_volume_get = aout_VolumeSoftGet;
284     p_aout->output.pf_volume_set = aout_VolumeSoftSet;
285
286     i_volume = config_GetInt( p_aout, "volume" );
287     if ( i_volume < 0 )
288     {
289         i_volume = AOUT_VOLUME_DEFAULT;
290     }
291     else if ( i_volume > AOUT_VOLUME_MAX )
292     {
293         i_volume = AOUT_VOLUME_MAX;
294     }
295
296     aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
297 }
298
299 /* Placeholder for pf_volume_infos(). */
300 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
301 {
302     *pi_soft = 0;
303     return 0;
304 }
305
306 /* Placeholder for pf_volume_get(). */
307 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
308 {
309     *pi_volume = p_aout->output.i_volume;
310     return 0;
311 }
312
313
314 /* Placeholder for pf_volume_set(). */
315 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
316 {
317     aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
318     p_aout->output.i_volume = i_volume;
319     return 0;
320 }
321
322 /*
323  * The next functions are not supposed to be called by the interface, but
324  * are placeholders for unsupported scaling.
325  */
326
327 /* Meant to be called by the output plug-in's Open(). */
328 void aout_VolumeNoneInit( aout_instance_t * p_aout )
329 {
330     p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
331     p_aout->output.pf_volume_get = aout_VolumeNoneGet;
332     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
333 }
334
335 /* Placeholder for pf_volume_infos(). */
336 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
337 {
338     return -1;
339 }
340
341 /* Placeholder for pf_volume_get(). */
342 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
343 {
344     return -1;
345 }
346
347 /* Placeholder for pf_volume_set(). */
348 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
349 {
350     return -1;
351 }
352
353
354 /*
355  * Pipelines management
356  */
357
358 /*****************************************************************************
359  * aout_Restart : re-open the output device and rebuild the input and output
360  *                pipelines
361  *****************************************************************************
362  * This function is used whenever the parameters of the output plug-in are
363  * changed (eg. selecting S/PDIF or PCM).
364  *****************************************************************************/
365 int aout_Restart( aout_instance_t * p_aout )
366 {
367     int i;
368     vlc_bool_t b_error = 0;
369
370     vlc_mutex_lock( &p_aout->mixer_lock );
371
372     if ( p_aout->i_nb_inputs == 0 )
373     {
374         vlc_mutex_unlock( &p_aout->mixer_lock );
375         msg_Err( p_aout, "no decoder thread" );
376         return -1;
377     }
378
379     /* Lock all inputs. */
380     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
381     {
382         vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
383         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
384     }
385
386     aout_MixerDelete( p_aout );
387
388     /* Re-open the output plug-in. */
389     aout_OutputDelete( p_aout );
390
391     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
392     {
393         /* Release all locks and report the error. */
394         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
395         {
396             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
397         }
398         vlc_mutex_unlock( &p_aout->mixer_lock );
399         return -1;
400     }
401
402     if ( aout_MixerNew( p_aout ) == -1 )
403     {
404         aout_OutputDelete( p_aout );
405         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
406         {
407             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
408         }
409         vlc_mutex_unlock( &p_aout->mixer_lock );
410         return -1;
411     }
412
413     /* Re-open all inputs. */
414     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
415     {
416         aout_input_t * p_input = p_aout->pp_inputs[i];
417
418         b_error |= aout_InputNew( p_aout, p_input );
419         p_input->b_changed = 1;
420         vlc_mutex_unlock( &p_input->lock );
421     }
422
423     vlc_mutex_unlock( &p_aout->mixer_lock );
424
425     return b_error;
426 }
427
428 /*****************************************************************************
429  * aout_FindAndRestart : find the audio output instance and restart
430  *****************************************************************************
431  * This is used for callbacks of the configuration variables, and we believe
432  * that when those are changed, it is a significant change which implies
433  * rebuilding the audio-device and audio-channels variables.
434  *****************************************************************************/
435 void aout_FindAndRestart( vlc_object_t * p_this )
436 {
437     aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
438                                                 FIND_ANYWHERE );
439
440     if ( p_aout == NULL ) return;
441
442     if ( var_Type( p_aout, "audio-device" ) != 0 )
443     {
444         var_Destroy( p_aout, "audio-device" );
445     }
446     if ( var_Type( p_aout, "audio-channels" ) != 0 )
447     {
448         var_Destroy( p_aout, "audio-channels" );
449     }
450
451     aout_Restart( p_aout );
452     vlc_object_release( p_aout );
453 }
454
455 /*****************************************************************************
456  * aout_ChannelsRestart : change the audio device or channels and restart
457  *****************************************************************************/
458 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
459                           vlc_value_t old_value, vlc_value_t new_value,
460                           void * unused )
461 {
462     aout_instance_t * p_aout = (aout_instance_t *)p_this;
463
464     if ( !strcmp( psz_variable, "audio-device" ) )
465     {
466         /* This is supposed to be a significant change and supposes
467          * rebuilding the channel choices. */
468         if ( var_Type( p_aout, "audio-channels" ) >= 0 )
469         {
470             var_Destroy( p_aout, "audio-channels" );
471         }
472     }
473     aout_Restart( p_aout );
474     return 0;
475 }