1 /*****************************************************************************
2 * intf.c : audio output API towards the interface modules
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: intf.c,v 1.15 2003/01/21 10:29:12 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( aout_instance_t * p_aout, audio_volume_t * pi_volume )
66 vlc_mutex_lock( &p_aout->mixer_lock );
68 if ( p_aout->mixer.b_error )
71 /* The output module is destroyed. */
72 vlc_mutex_unlock( &p_aout->mixer_lock );
73 i = config_GetInt( p_aout, "volume" );
74 if (pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
78 i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
80 vlc_mutex_unlock( &p_aout->mixer_lock );
84 /*****************************************************************************
85 * aout_VolumeSet : set the volume of the output device
86 *****************************************************************************/
87 int aout_VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
91 vlc_mutex_lock( &p_aout->mixer_lock );
93 if ( p_aout->mixer.b_error )
95 /* The output module is destroyed. */
96 vlc_mutex_unlock( &p_aout->mixer_lock );
97 config_PutInt( p_aout, "volume", i_volume );
101 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
103 vlc_mutex_unlock( &p_aout->mixer_lock );
107 /*****************************************************************************
108 * aout_VolumeInfos : get the boundary pi_soft
109 *****************************************************************************/
110 int aout_VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
114 vlc_mutex_lock( &p_aout->mixer_lock );
116 if ( p_aout->mixer.b_error )
118 /* The output module is destroyed. */
119 vlc_mutex_unlock( &p_aout->mixer_lock );
120 msg_Err( p_aout, "VolumeInfos called without output module" );
124 i_result = p_aout->output.pf_volume_infos( p_aout, pi_soft );
126 vlc_mutex_unlock( &p_aout->mixer_lock );
130 /*****************************************************************************
131 * aout_VolumeUp : raise the output volume
132 *****************************************************************************
133 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
135 *****************************************************************************/
136 int aout_VolumeUp( aout_instance_t * p_aout, int i_nb_steps,
137 audio_volume_t * pi_volume )
140 audio_volume_t i_volume;
142 vlc_mutex_lock( &p_aout->mixer_lock );
144 if ( p_aout->mixer.b_error )
147 /* The output module is destroyed. */
148 vlc_mutex_unlock( &p_aout->mixer_lock );
149 i = config_GetInt( p_aout, "volume" );
150 i += AOUT_VOLUME_STEP * i_nb_steps;
151 if ( i > AOUT_VOLUME_MAX )
155 config_PutInt( p_aout, "volume", i );
156 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
160 if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
162 vlc_mutex_unlock( &p_aout->mixer_lock );
166 i_volume += AOUT_VOLUME_STEP * i_nb_steps;
167 if ( i_volume > AOUT_VOLUME_MAX ) i_volume = AOUT_VOLUME_MAX;
169 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
171 vlc_mutex_unlock( &p_aout->mixer_lock );
173 if ( pi_volume != NULL ) *pi_volume = i_volume;
177 /*****************************************************************************
178 * aout_VolumeDown : lower the output volume
179 *****************************************************************************
180 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
182 *****************************************************************************/
183 int aout_VolumeDown( aout_instance_t * p_aout, int i_nb_steps,
184 audio_volume_t * pi_volume )
187 audio_volume_t i_volume;
189 vlc_mutex_lock( &p_aout->mixer_lock );
191 if ( p_aout->mixer.b_error )
194 /* The output module is destroyed. */
195 vlc_mutex_unlock( &p_aout->mixer_lock );
196 i = config_GetInt( p_aout, "volume" );
197 i -= AOUT_VOLUME_STEP * i_nb_steps;
202 config_PutInt( p_aout, "volume", i );
203 if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i;
207 if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
209 vlc_mutex_unlock( &p_aout->mixer_lock );
213 if ( i_volume < AOUT_VOLUME_STEP * i_nb_steps )
216 i_volume -= AOUT_VOLUME_STEP * i_nb_steps;
218 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
220 vlc_mutex_unlock( &p_aout->mixer_lock );
222 if ( pi_volume != NULL ) *pi_volume = i_volume;
226 /*****************************************************************************
227 * aout_VolumeMute : Mute/un-mute the output volume
228 *****************************************************************************
229 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
230 * function (muted => 0).
231 *****************************************************************************/
232 int aout_VolumeMute( aout_instance_t * p_aout, audio_volume_t * pi_volume )
235 audio_volume_t i_volume;
237 vlc_mutex_lock( &p_aout->mixer_lock );
239 if ( p_aout->mixer.b_error )
241 /* The output module is destroyed. */
242 vlc_mutex_unlock( &p_aout->mixer_lock );
243 config_PutInt( p_aout, "volume", 0 );
244 if ( pi_volume != NULL ) *pi_volume = 0;
248 if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
250 vlc_mutex_unlock( &p_aout->mixer_lock );
256 i_volume = p_aout->output.i_saved_volume;
260 p_aout->output.i_saved_volume = i_volume;
264 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
266 vlc_mutex_unlock( &p_aout->mixer_lock );
268 if ( pi_volume != NULL ) *pi_volume = i_volume;
273 * The next functions are not supposed to be called by the interface, but
274 * are placeholders for software-only scaling.
277 /* Meant to be called by the output plug-in's Open(). */
278 void aout_VolumeSoftInit( aout_instance_t * p_aout )
282 p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
283 p_aout->output.pf_volume_get = aout_VolumeSoftGet;
284 p_aout->output.pf_volume_set = aout_VolumeSoftSet;
286 i_volume = config_GetInt( p_aout, "volume" );
289 i_volume = AOUT_VOLUME_DEFAULT;
291 else if ( i_volume > AOUT_VOLUME_MAX )
293 i_volume = AOUT_VOLUME_MAX;
296 aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
299 /* Placeholder for pf_volume_infos(). */
300 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
306 /* Placeholder for pf_volume_get(). */
307 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
309 *pi_volume = p_aout->output.i_volume;
314 /* Placeholder for pf_volume_set(). */
315 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
317 aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
318 p_aout->output.i_volume = i_volume;
323 * The next functions are not supposed to be called by the interface, but
324 * are placeholders for unsupported scaling.
327 /* Meant to be called by the output plug-in's Open(). */
328 void aout_VolumeNoneInit( aout_instance_t * p_aout )
330 p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
331 p_aout->output.pf_volume_get = aout_VolumeNoneGet;
332 p_aout->output.pf_volume_set = aout_VolumeNoneSet;
335 /* Placeholder for pf_volume_infos(). */
336 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
341 /* Placeholder for pf_volume_get(). */
342 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
347 /* Placeholder for pf_volume_set(). */
348 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
355 * Pipelines management
358 /*****************************************************************************
359 * aout_Restart : re-open the output device and rebuild the input and output
361 *****************************************************************************
362 * This function is used whenever the parameters of the output plug-in are
363 * changed (eg. selecting S/PDIF or PCM).
364 *****************************************************************************/
365 int aout_Restart( aout_instance_t * p_aout )
368 vlc_bool_t b_error = 0;
370 vlc_mutex_lock( &p_aout->mixer_lock );
372 if ( p_aout->i_nb_inputs == 0 )
374 vlc_mutex_unlock( &p_aout->mixer_lock );
375 msg_Err( p_aout, "no decoder thread" );
379 /* Lock all inputs. */
380 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
382 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
383 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
386 aout_MixerDelete( p_aout );
388 /* Re-open the output plug-in. */
389 aout_OutputDelete( p_aout );
391 if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
393 /* Release all locks and report the error. */
394 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
396 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
398 vlc_mutex_unlock( &p_aout->mixer_lock );
402 if ( aout_MixerNew( p_aout ) == -1 )
404 aout_OutputDelete( p_aout );
405 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
407 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
409 vlc_mutex_unlock( &p_aout->mixer_lock );
413 /* Re-open all inputs. */
414 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
416 aout_input_t * p_input = p_aout->pp_inputs[i];
418 b_error |= aout_InputNew( p_aout, p_input );
419 p_input->b_changed = 1;
420 vlc_mutex_unlock( &p_input->lock );
423 vlc_mutex_unlock( &p_aout->mixer_lock );
428 /*****************************************************************************
429 * aout_FindAndRestart : find the audio output instance and restart
430 *****************************************************************************
431 * This is used for callbacks of the configuration variables, and we believe
432 * that when those are changed, it is a significant change which implies
433 * rebuilding the audio-device and audio-channels variables.
434 *****************************************************************************/
435 void aout_FindAndRestart( vlc_object_t * p_this )
437 aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
440 if ( p_aout == NULL ) return;
442 if ( var_Type( p_aout, "audio-device" ) != 0 )
444 var_Destroy( p_aout, "audio-device" );
446 if ( var_Type( p_aout, "audio-channels" ) != 0 )
448 var_Destroy( p_aout, "audio-channels" );
451 aout_Restart( p_aout );
452 vlc_object_release( p_aout );
455 /*****************************************************************************
456 * aout_ChannelsRestart : change the audio device or channels and restart
457 *****************************************************************************/
458 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
459 vlc_value_t old_value, vlc_value_t new_value,
462 aout_instance_t * p_aout = (aout_instance_t *)p_this;
464 if ( !strcmp( psz_variable, "audio-device" ) )
466 /* This is supposed to be a significant change and supposes
467 * rebuilding the channel choices. */
468 if ( var_Type( p_aout, "audio-channels" ) >= 0 )
470 var_Destroy( p_aout, "audio-channels" );
473 aout_Restart( p_aout );