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