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