1 /*****************************************************************************
2 * intf.c : audio output API towards the interface modules
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include "audio_output.h"
33 #include "aout_internal.h"
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).
43 * Here is a schematic of the i_volume range :
45 * |------------------------------+---------------------------------------|
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.
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.
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 )
64 int i_volume, i_result = 0;
65 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
68 i_volume = config_GetInt( p_object, "volume" );
69 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i_volume;
71 if ( p_aout == NULL ) return 0;
73 vlc_mutex_lock( &p_aout->mixer_lock );
74 if ( !p_aout->mixer.b_error )
76 i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
78 vlc_mutex_unlock( &p_aout->mixer_lock );
80 vlc_object_release( p_aout );
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 )
90 aout_instance_t *p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT, FIND_ANYWHERE );
93 config_PutInt( p_object, "volume", i_volume );
95 val.b_bool = VLC_TRUE;
96 var_Set( p_object->p_vlc, "volume-change", val );
98 if ( p_aout == NULL ) return 0;
100 vlc_mutex_lock( &p_aout->mixer_lock );
101 if ( !p_aout->mixer.b_error )
103 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
105 vlc_mutex_unlock( &p_aout->mixer_lock );
107 var_Set( p_aout, "intf-change", val );
108 vlc_object_release( p_aout );
112 /*****************************************************************************
113 * aout_VolumeInfos : get the boundary pi_soft
114 *****************************************************************************/
115 int __aout_VolumeInfos( vlc_object_t * p_object, audio_volume_t * pi_soft )
117 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
121 if ( p_aout == NULL ) return 0;
123 vlc_mutex_lock( &p_aout->mixer_lock );
124 if ( p_aout->mixer.b_error )
126 /* The output module is destroyed. */
131 i_result = p_aout->output.pf_volume_infos( p_aout, pi_soft );
133 vlc_mutex_unlock( &p_aout->mixer_lock );
135 vlc_object_release( p_aout );
139 /*****************************************************************************
140 * aout_VolumeUp : raise the output volume
141 *****************************************************************************
142 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
144 *****************************************************************************/
145 int __aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
146 audio_volume_t * pi_volume )
148 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
150 int i_result = 0, i_volume = 0, i_volume_step = 0;
152 i_volume_step = config_GetInt( p_object->p_vlc, "volume-step" );
153 i_volume = config_GetInt( p_object, "volume" );
154 i_volume += i_volume_step * i_nb_steps;
155 if ( i_volume > AOUT_VOLUME_MAX )
157 i_volume = AOUT_VOLUME_MAX;
159 config_PutInt( p_object, "volume", i_volume );
160 var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
161 var_SetInteger( p_object->p_libvlc, "saved-volume" ,
162 (audio_volume_t) i_volume );
163 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t) i_volume;
165 if ( p_aout == NULL ) return 0;
167 vlc_mutex_lock( &p_aout->mixer_lock );
168 if ( !p_aout->mixer.b_error )
170 i_result = p_aout->output.pf_volume_set( p_aout,
171 (audio_volume_t) i_volume );
173 vlc_mutex_unlock( &p_aout->mixer_lock );
175 vlc_object_release( p_aout );
179 /*****************************************************************************
180 * aout_VolumeDown : lower the output volume
181 *****************************************************************************
182 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
184 *****************************************************************************/
185 int __aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
186 audio_volume_t * pi_volume )
188 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
190 int i_result = 0, i_volume = 0, i_volume_step = 0;
192 i_volume_step = config_GetInt( p_object->p_vlc, "volume-step" );
193 i_volume = config_GetInt( p_object, "volume" );
194 i_volume -= i_volume_step * i_nb_steps;
195 if ( i_volume < AOUT_VOLUME_MIN )
197 i_volume = AOUT_VOLUME_MIN;
199 config_PutInt( p_object, "volume", i_volume );
200 var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
201 var_SetInteger( p_object->p_libvlc, "saved-volume", (audio_volume_t) i_volume );
202 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t) i_volume;
204 if ( p_aout == NULL ) return 0;
206 vlc_mutex_lock( &p_aout->mixer_lock );
207 if ( !p_aout->mixer.b_error )
209 i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t) i_volume );
211 vlc_mutex_unlock( &p_aout->mixer_lock );
213 vlc_object_release( p_aout );
217 /*****************************************************************************
218 * aout_VolumeMute : Mute/un-mute the output volume
219 *****************************************************************************
220 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
221 * function (muted => 0).
222 *****************************************************************************/
223 int __aout_VolumeMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
226 audio_volume_t i_volume;
228 i_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
232 i_result = aout_VolumeSet( p_object, AOUT_VOLUME_MIN );
233 var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
234 var_SetInteger( p_object->p_libvlc, "saved-volume", (int)i_volume );
235 if ( pi_volume != NULL ) *pi_volume = AOUT_VOLUME_MIN;
240 var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
241 i_volume = (audio_volume_t)var_GetInteger( p_object->p_libvlc,
243 i_result = aout_VolumeSet( p_object, i_volume );
244 if ( pi_volume != NULL ) *pi_volume = i_volume;
251 * The next functions are not supposed to be called by the interface, but
252 * are placeholders for software-only scaling.
255 /* Meant to be called by the output plug-in's Open(). */
256 void aout_VolumeSoftInit( aout_instance_t * p_aout )
260 p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
261 p_aout->output.pf_volume_get = aout_VolumeSoftGet;
262 p_aout->output.pf_volume_set = aout_VolumeSoftSet;
264 i_volume = config_GetInt( p_aout, "volume" );
265 if ( i_volume < AOUT_VOLUME_MIN )
267 i_volume = AOUT_VOLUME_DEFAULT;
269 else if ( i_volume > AOUT_VOLUME_MAX )
271 i_volume = AOUT_VOLUME_MAX;
274 aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
277 /* Placeholder for pf_volume_infos(). */
278 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
284 /* Placeholder for pf_volume_get(). */
285 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
287 *pi_volume = p_aout->output.i_volume;
292 /* Placeholder for pf_volume_set(). */
293 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
295 aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
296 p_aout->output.i_volume = i_volume;
301 * The next functions are not supposed to be called by the interface, but
302 * are placeholders for unsupported scaling.
305 /* Meant to be called by the output plug-in's Open(). */
306 void aout_VolumeNoneInit( aout_instance_t * p_aout )
308 p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
309 p_aout->output.pf_volume_get = aout_VolumeNoneGet;
310 p_aout->output.pf_volume_set = aout_VolumeNoneSet;
313 /* Placeholder for pf_volume_infos(). */
314 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
319 /* Placeholder for pf_volume_get(). */
320 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
325 /* Placeholder for pf_volume_set(). */
326 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
333 * Pipelines management
336 /*****************************************************************************
337 * aout_Restart : re-open the output device and rebuild the input and output
339 *****************************************************************************
340 * This function is used whenever the parameters of the output plug-in are
341 * changed (eg. selecting S/PDIF or PCM).
342 *****************************************************************************/
343 int aout_Restart( aout_instance_t * p_aout )
346 vlc_bool_t b_error = 0;
348 vlc_mutex_lock( &p_aout->mixer_lock );
350 if ( p_aout->i_nb_inputs == 0 )
352 vlc_mutex_unlock( &p_aout->mixer_lock );
353 msg_Err( p_aout, "no decoder thread" );
357 /* Lock all inputs. */
358 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
360 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
361 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
364 aout_MixerDelete( p_aout );
366 /* Re-open the output plug-in. */
367 aout_OutputDelete( p_aout );
369 if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
371 /* Release all locks and report the error. */
372 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
374 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
376 vlc_mutex_unlock( &p_aout->mixer_lock );
380 if ( aout_MixerNew( p_aout ) == -1 )
382 aout_OutputDelete( p_aout );
383 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
385 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
387 vlc_mutex_unlock( &p_aout->mixer_lock );
391 /* Re-open all inputs. */
392 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
394 aout_input_t * p_input = p_aout->pp_inputs[i];
396 b_error |= aout_InputNew( p_aout, p_input );
397 p_input->b_changed = 1;
398 vlc_mutex_unlock( &p_input->lock );
401 vlc_mutex_unlock( &p_aout->mixer_lock );
406 /*****************************************************************************
407 * aout_FindAndRestart : find the audio output instance and restart
408 *****************************************************************************
409 * This is used for callbacks of the configuration variables, and we believe
410 * that when those are changed, it is a significant change which implies
411 * rebuilding the audio-device and audio-channels variables.
412 *****************************************************************************/
413 int aout_FindAndRestart( vlc_object_t * p_this, const char *psz_name,
414 vlc_value_t oldval, vlc_value_t val, void *p_data )
416 aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
419 if ( p_aout == NULL ) return VLC_SUCCESS;
421 if ( var_Type( p_aout, "audio-device" ) != 0 )
423 var_Destroy( p_aout, "audio-device" );
425 if ( var_Type( p_aout, "audio-channels" ) != 0 )
427 var_Destroy( p_aout, "audio-channels" );
430 aout_Restart( p_aout );
431 vlc_object_release( p_aout );
436 /*****************************************************************************
437 * aout_ChannelsRestart : change the audio device or channels and restart
438 *****************************************************************************/
439 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
440 vlc_value_t old_value, vlc_value_t new_value,
443 aout_instance_t * p_aout = (aout_instance_t *)p_this;
445 if ( !strcmp( psz_variable, "audio-device" ) )
447 /* This is supposed to be a significant change and supposes
448 * rebuilding the channel choices. */
449 if ( var_Type( p_aout, "audio-channels" ) >= 0 )
451 var_Destroy( p_aout, "audio-channels" );
454 aout_Restart( p_aout );