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