]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
9e1914c98a9a083b9cdf79e5e277f4d8ffa364f5
[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.7 2002/11/14 14:08:01 gbazin 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 boundaries pi_low_soft and pi_high_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         /* The output module is destroyed. */
145         vlc_mutex_unlock( &p_aout->mixer_lock );
146         msg_Err( p_aout, "VolumeUp called without output module" );
147         return -1;
148     }
149
150     if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
151     {
152         vlc_mutex_unlock( &p_aout->mixer_lock );
153         return -1;
154     }
155
156     i_volume += AOUT_VOLUME_STEP * i_nb_steps;
157     if ( i_volume > 1024 ) i_volume = 1024;
158
159     i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
160
161     vlc_mutex_unlock( &p_aout->mixer_lock );
162
163     if ( pi_volume != NULL ) *pi_volume = i_volume;
164     return i_result;
165 }
166
167 /*****************************************************************************
168  * aout_VolumeDown : lower the output volume
169  *****************************************************************************
170  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
171  * function.
172  *****************************************************************************/
173 int aout_VolumeDown( aout_instance_t * p_aout, int i_nb_steps,
174                      audio_volume_t * pi_volume )
175 {
176     int i_result;
177     audio_volume_t i_volume;
178
179     vlc_mutex_lock( &p_aout->mixer_lock );
180
181     if ( p_aout->mixer.b_error )
182     {
183         /* The output module is destroyed. */
184         vlc_mutex_unlock( &p_aout->mixer_lock );
185         msg_Err( p_aout, "VolumeUp called without output module" );
186         return -1;
187     }
188
189     if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
190     {
191         vlc_mutex_unlock( &p_aout->mixer_lock );
192         return -1;
193     }
194
195     if ( i_volume < AOUT_VOLUME_STEP * i_nb_steps )
196         i_volume = 0;
197     else
198         i_volume -= AOUT_VOLUME_STEP * i_nb_steps;
199
200     i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
201
202     vlc_mutex_unlock( &p_aout->mixer_lock );
203
204     if ( pi_volume != NULL ) *pi_volume = i_volume;
205     return i_result;
206 }
207
208 /*
209  * The next functions are not supposed to be called by the interface, but
210  * are placeholders for software-only scaling.
211  */
212
213 /* Meant to be called by the output plug-in's Open(). */
214 void aout_VolumeSoftInit( aout_instance_t * p_aout )
215 {
216     int i_volume;
217
218     p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
219     p_aout->output.pf_volume_get = aout_VolumeSoftGet;
220     p_aout->output.pf_volume_set = aout_VolumeSoftSet;
221
222     i_volume = config_GetInt( p_aout, "volume" );
223     if ( i_volume < 0 )
224     {
225         i_volume = AOUT_VOLUME_DEFAULT;
226     }
227     else if ( i_volume > AOUT_VOLUME_MAX )
228     {
229         i_volume = AOUT_VOLUME_MAX;
230     }
231
232     aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
233 }
234
235 /* Placeholder for pf_volume_infos(). */
236 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
237 {
238     *pi_soft = 0;
239     return 0;
240 }
241
242 /* Placeholder for pf_volume_get(). */
243 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
244 {
245     *pi_volume = p_aout->output.i_volume;
246     return 0;
247 }
248
249
250 /* Placeholder for pf_volume_set(). */
251 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
252 {
253     aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
254     p_aout->output.i_volume = i_volume;
255     return 0;
256 }
257
258 /*
259  * The next functions are not supposed to be called by the interface, but
260  * are placeholders for unsupported scaling.
261  */
262
263 /* Meant to be called by the output plug-in's Open(). */
264 void aout_VolumeNoneInit( aout_instance_t * p_aout )
265 {
266     p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
267     p_aout->output.pf_volume_get = aout_VolumeNoneGet;
268     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
269 }
270
271 /* Placeholder for pf_volume_infos(). */
272 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
273 {
274     return -1;
275 }
276
277 /* Placeholder for pf_volume_get(). */
278 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
279 {
280     return -1;
281 }
282
283 /* Placeholder for pf_volume_set(). */
284 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
285 {
286     return -1;
287 }
288
289
290 /*
291  * Pipelines management
292  */
293
294 /*****************************************************************************
295  * aout_Restart : re-open the output device and rebuild the input and output
296  *                pipelines
297  *****************************************************************************
298  * This function is used whenever the parameters of the output plug-in are
299  * changed (eg. selecting S/PDIF or PCM).
300  *****************************************************************************/
301 int aout_Restart( aout_instance_t * p_aout )
302 {
303     int i;
304     vlc_bool_t b_error = 0;
305
306     vlc_mutex_lock( &p_aout->mixer_lock );
307
308     if ( p_aout->i_nb_inputs == 0 )
309     {
310         vlc_mutex_unlock( &p_aout->mixer_lock );
311         msg_Err( p_aout, "no decoder thread" );
312         return -1;
313     }
314
315     /* Lock all inputs. */
316     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
317     {
318         vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
319         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
320     }
321
322     aout_MixerDelete( p_aout );
323
324     /* Re-open the output plug-in. */
325     aout_OutputDelete( p_aout );
326     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
327     {
328         /* Release all locks and report the error. */
329         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
330         {
331             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
332         }
333         vlc_mutex_unlock( &p_aout->mixer_lock );
334         return -1;
335     }
336
337     if ( aout_MixerNew( p_aout ) == -1 )
338     {
339         aout_OutputDelete( p_aout );
340         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
341         {
342             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
343         }
344         vlc_mutex_unlock( &p_aout->mixer_lock );
345         return -1;
346     }
347
348     /* Re-open all inputs. */
349     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
350     {
351         aout_input_t * p_input = p_aout->pp_inputs[i];
352
353         b_error |= aout_InputNew( p_aout, p_input );
354         vlc_mutex_unlock( &p_input->lock );
355     }
356
357     vlc_mutex_unlock( &p_aout->mixer_lock );
358
359     return b_error;
360 }
361
362 /*****************************************************************************
363  * aout_FindAndRestart : find the audio output instance and restart
364  *****************************************************************************/
365 void aout_FindAndRestart( vlc_object_t * p_this )
366 {
367     aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
368                                                 FIND_ANYWHERE );
369
370     if ( p_aout == NULL ) return;
371
372     aout_Restart( p_aout );
373     vlc_object_release( p_aout );
374 }