1 /*****************************************************************************
2 * intf.c : audio output API towards the interface modules
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: intf.c,v 1.16 2003/02/09 01:13:43 massiot Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 )
66 i = config_GetInt( p_object, "volume" );
67 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
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 )
77 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
81 config_PutInt( p_object, "volume", i_volume );
83 if ( p_aout == NULL ) return 0;
85 vlc_mutex_lock( &p_aout->mixer_lock );
86 if ( !p_aout->mixer.b_error )
88 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
90 vlc_mutex_unlock( &p_aout->mixer_lock );
92 vlc_object_release( p_aout );
96 /*****************************************************************************
97 * aout_VolumeInfos : get the boundary pi_soft
98 *****************************************************************************/
99 int __aout_VolumeInfos( vlc_object_t * p_object, audio_volume_t * pi_soft )
101 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
105 if ( p_aout == NULL ) return 0;
107 vlc_mutex_lock( &p_aout->mixer_lock );
108 if ( p_aout->mixer.b_error )
110 /* The output module is destroyed. */
115 i_result = p_aout->output.pf_volume_infos( p_aout, pi_soft );
117 vlc_mutex_unlock( &p_aout->mixer_lock );
119 vlc_object_release( p_aout );
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
128 *****************************************************************************/
129 int __aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
130 audio_volume_t * pi_volume )
132 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
136 i = config_GetInt( p_object, "volume" );
137 i += AOUT_VOLUME_STEP * i_nb_steps;
138 if ( i > AOUT_VOLUME_MAX )
142 config_PutInt( p_object, "volume", i );
143 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
145 if ( p_aout == NULL ) return 0;
147 vlc_mutex_lock( &p_aout->mixer_lock );
148 if ( !p_aout->mixer.b_error )
150 i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t)i );
152 vlc_mutex_unlock( &p_aout->mixer_lock );
154 vlc_object_release( p_aout );
158 /*****************************************************************************
159 * aout_VolumeDown : lower the output volume
160 *****************************************************************************
161 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
163 *****************************************************************************/
164 int __aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
165 audio_volume_t * pi_volume )
167 aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
171 i = config_GetInt( p_object, "volume" );
172 i -= AOUT_VOLUME_STEP * i_nb_steps;
173 if ( i < AOUT_VOLUME_MIN )
177 config_PutInt( p_object, "volume", i );
178 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
180 if ( p_aout == NULL ) return 0;
182 vlc_mutex_lock( &p_aout->mixer_lock );
183 if ( !p_aout->mixer.b_error )
185 i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t)i );
187 vlc_mutex_unlock( &p_aout->mixer_lock );
189 vlc_object_release( p_aout );
193 /*****************************************************************************
194 * aout_VolumeMute : Mute/un-mute the output volume
195 *****************************************************************************
196 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
197 * function (muted => 0).
198 *****************************************************************************/
199 int __aout_VolumeMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
202 audio_volume_t i_volume;
204 i_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
208 i_result = aout_VolumeSet( p_object, AOUT_VOLUME_MIN );
209 config_PutInt( p_object, "saved-volume", (int)i_volume );
210 if ( pi_volume != NULL ) *pi_volume = AOUT_VOLUME_MIN;
215 i_volume = (audio_volume_t)config_GetInt( p_object, "saved-volume" );
216 i_result = aout_VolumeSet( p_object, i_volume );
217 if ( pi_volume != NULL ) *pi_volume = i_volume;
224 * The next functions are not supposed to be called by the interface, but
225 * are placeholders for software-only scaling.
228 /* Meant to be called by the output plug-in's Open(). */
229 void aout_VolumeSoftInit( aout_instance_t * p_aout )
233 p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
234 p_aout->output.pf_volume_get = aout_VolumeSoftGet;
235 p_aout->output.pf_volume_set = aout_VolumeSoftSet;
237 i_volume = config_GetInt( p_aout, "volume" );
238 if ( i_volume < AOUT_VOLUME_MIN )
240 i_volume = AOUT_VOLUME_DEFAULT;
242 else if ( i_volume > AOUT_VOLUME_MAX )
244 i_volume = AOUT_VOLUME_MAX;
247 aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
250 /* Placeholder for pf_volume_infos(). */
251 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
257 /* Placeholder for pf_volume_get(). */
258 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
260 *pi_volume = p_aout->output.i_volume;
265 /* Placeholder for pf_volume_set(). */
266 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
268 aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
269 p_aout->output.i_volume = i_volume;
274 * The next functions are not supposed to be called by the interface, but
275 * are placeholders for unsupported scaling.
278 /* Meant to be called by the output plug-in's Open(). */
279 void aout_VolumeNoneInit( aout_instance_t * p_aout )
281 p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
282 p_aout->output.pf_volume_get = aout_VolumeNoneGet;
283 p_aout->output.pf_volume_set = aout_VolumeNoneSet;
286 /* Placeholder for pf_volume_infos(). */
287 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
292 /* Placeholder for pf_volume_get(). */
293 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
298 /* Placeholder for pf_volume_set(). */
299 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
306 * Pipelines management
309 /*****************************************************************************
310 * aout_Restart : re-open the output device and rebuild the input and output
312 *****************************************************************************
313 * This function is used whenever the parameters of the output plug-in are
314 * changed (eg. selecting S/PDIF or PCM).
315 *****************************************************************************/
316 int aout_Restart( aout_instance_t * p_aout )
319 vlc_bool_t b_error = 0;
321 vlc_mutex_lock( &p_aout->mixer_lock );
323 if ( p_aout->i_nb_inputs == 0 )
325 vlc_mutex_unlock( &p_aout->mixer_lock );
326 msg_Err( p_aout, "no decoder thread" );
330 /* Lock all inputs. */
331 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
333 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
334 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
337 aout_MixerDelete( p_aout );
339 /* Re-open the output plug-in. */
340 aout_OutputDelete( p_aout );
342 if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
344 /* Release all locks and report the error. */
345 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
347 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
349 vlc_mutex_unlock( &p_aout->mixer_lock );
353 if ( aout_MixerNew( p_aout ) == -1 )
355 aout_OutputDelete( p_aout );
356 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
358 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
360 vlc_mutex_unlock( &p_aout->mixer_lock );
364 /* Re-open all inputs. */
365 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
367 aout_input_t * p_input = p_aout->pp_inputs[i];
369 b_error |= aout_InputNew( p_aout, p_input );
370 p_input->b_changed = 1;
371 vlc_mutex_unlock( &p_input->lock );
374 vlc_mutex_unlock( &p_aout->mixer_lock );
379 /*****************************************************************************
380 * aout_FindAndRestart : find the audio output instance and restart
381 *****************************************************************************
382 * This is used for callbacks of the configuration variables, and we believe
383 * that when those are changed, it is a significant change which implies
384 * rebuilding the audio-device and audio-channels variables.
385 *****************************************************************************/
386 void aout_FindAndRestart( vlc_object_t * p_this )
388 aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
391 if ( p_aout == NULL ) return;
393 if ( var_Type( p_aout, "audio-device" ) != 0 )
395 var_Destroy( p_aout, "audio-device" );
397 if ( var_Type( p_aout, "audio-channels" ) != 0 )
399 var_Destroy( p_aout, "audio-channels" );
402 aout_Restart( p_aout );
403 vlc_object_release( p_aout );
406 /*****************************************************************************
407 * aout_ChannelsRestart : change the audio device or channels and restart
408 *****************************************************************************/
409 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
410 vlc_value_t old_value, vlc_value_t new_value,
413 aout_instance_t * p_aout = (aout_instance_t *)p_this;
415 if ( !strcmp( psz_variable, "audio-device" ) )
417 /* This is supposed to be a significant change and supposes
418 * rebuilding the channel choices. */
419 if ( var_Type( p_aout, "audio-channels" ) >= 0 )
421 var_Destroy( p_aout, "audio-channels" );
424 aout_Restart( p_aout );