1 /*****************************************************************************
2 * intf.c : audio output API towards the interface modules
3 *****************************************************************************
4 * Copyright (C) 2002-2007 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 *****************************************************************************/
32 #include <vlc_common.h>
35 #include <stdlib.h> /* calloc(), malloc(), free() */
39 #include "aout_internal.h"
41 #include <vlc_playlist.h>
43 static aout_instance_t *findAout (vlc_object_t *obj)
45 input_thread_t *(*pf_find_input) (vlc_object_t *);
47 pf_find_input = var_GetAddress (obj, "find-input-callback");
48 if (unlikely(pf_find_input == NULL))
51 input_thread_t *p_input = pf_find_input (obj);
55 aout_instance_t *p_aout = input_GetAout (p_input);
56 vlc_object_release (p_input);
59 #define findAout(o) findAout(VLC_OBJECT(o))
68 /*****************************************************************************
69 * doVolumeChanges : handle all volume changes. Internal use only to ease
71 *****************************************************************************/
73 int doVolumeChanges( unsigned action, vlc_object_t * p_object, int i_nb_steps,
74 audio_volume_t i_volume, audio_volume_t * i_return_volume,
77 int i_result = VLC_SUCCESS;
78 int i_volume_step = 1, i_new_volume = 0;
79 bool b_var_mute = false;
80 aout_instance_t *p_aout = findAout( p_object );
82 if ( p_aout ) aout_lock_volume( p_aout );
84 b_var_mute = var_GetBool( p_object, "volume-muted");
86 const bool b_unmute_condition = ( b_var_mute
87 && ( /* Unmute: on increments */
88 ( action == INCREMENT_VOLUME )
89 || /* On explicit unmute */
90 ( ( action == SET_MUTE ) && !b_mute )
91 || /* On toggle from muted */
92 ( action == TOGGLE_MUTE )
95 const bool b_mute_condition = ( !b_var_mute
97 ( ( action == SET_MUTE ) && b_mute )
99 ( action == TOGGLE_MUTE )
102 /* If muting or unmuting when play hasn't started */
103 if ( action == SET_MUTE && !b_unmute_condition && !b_mute_condition )
107 aout_unlock_volume( p_aout );
108 vlc_object_release( p_aout );
114 if ( b_unmute_condition )
116 /* Restore saved volume */
117 i_volume = var_GetInteger( p_object, "saved-volume" );
118 var_SetBool( p_object, "volume-muted", false );
120 else if ( b_mute_condition )
122 /* We need an initial value to backup later */
123 i_volume = config_GetInt( p_object, "volume" );
126 if ( action == INCREMENT_VOLUME )
128 i_volume_step = var_InheritInteger( p_object, "volume-step" );
130 if ( !b_unmute_condition )
131 i_volume = config_GetInt( p_object, "volume" );
133 i_new_volume = (int) i_volume + i_volume_step * i_nb_steps;
135 if ( i_new_volume > AOUT_VOLUME_MAX )
136 i_volume = AOUT_VOLUME_MAX;
137 else if ( i_new_volume < AOUT_VOLUME_MIN )
138 i_volume = AOUT_VOLUME_MIN;
140 i_volume = i_new_volume;
143 var_SetInteger( p_object, "saved-volume" , i_volume );
146 if ( b_mute_condition )
148 i_volume = AOUT_VOLUME_MIN;
149 var_SetBool( p_object, "volume-muted", true );
152 /* Commit volume changes */
153 config_PutInt( p_object, "volume", i_volume );
157 aout_lock_mixer( p_aout );
158 aout_lock_input_fifos( p_aout );
159 if ( p_aout->p_mixer )
160 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
161 aout_unlock_input_fifos( p_aout );
162 aout_unlock_mixer( p_aout );
165 /* trigger callbacks */
166 var_TriggerCallback( p_object, "volume-change" );
169 var_SetBool( p_aout, "intf-change", true );
170 aout_unlock_volume( p_aout );
171 vlc_object_release( p_aout );
174 if ( i_return_volume != NULL )
175 *i_return_volume = i_volume;
179 #undef aout_VolumeGet
180 /*****************************************************************************
181 * aout_VolumeGet : get the volume of the output device
182 *****************************************************************************/
183 int aout_VolumeGet( vlc_object_t * p_object, audio_volume_t * pi_volume )
186 aout_instance_t * p_aout = findAout( p_object );
188 if ( pi_volume == NULL ) return -1;
190 if ( p_aout == NULL )
192 *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
196 aout_lock_volume( p_aout );
197 aout_lock_mixer( p_aout );
198 if ( p_aout->p_mixer )
200 i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
204 *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
206 aout_unlock_mixer( p_aout );
207 aout_unlock_volume( p_aout );
209 vlc_object_release( p_aout );
213 #undef aout_VolumeSet
214 /*****************************************************************************
215 * aout_VolumeSet : set the volume of the output device
216 *****************************************************************************/
217 int aout_VolumeSet( vlc_object_t * p_object, audio_volume_t i_volume )
219 return doVolumeChanges( SET_VOLUME, p_object, 1, i_volume, NULL, true );
223 /*****************************************************************************
224 * aout_VolumeUp : raise the output volume
225 *****************************************************************************
226 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
228 *****************************************************************************/
229 int aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
230 audio_volume_t * pi_volume )
232 return doVolumeChanges( INCREMENT_VOLUME, p_object, i_nb_steps, 0, pi_volume, true );
235 #undef aout_VolumeDown
236 /*****************************************************************************
237 * aout_VolumeDown : lower the output volume
238 *****************************************************************************
239 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
241 *****************************************************************************/
242 int aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
243 audio_volume_t * pi_volume )
245 return aout_VolumeUp( p_object, -i_nb_steps, pi_volume );
248 #undef aout_ToggleMute
249 /*****************************************************************************
250 * aout_ToggleMute : Mute/un-mute the output volume
251 *****************************************************************************
252 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
253 * function (muted => 0).
254 *****************************************************************************/
255 int aout_ToggleMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
257 return doVolumeChanges( TOGGLE_MUTE, p_object, 1, 0, pi_volume, true );
260 /*****************************************************************************
261 * aout_IsMuted : Get the output volume mute status
262 *****************************************************************************/
263 bool aout_IsMuted( vlc_object_t * p_object )
266 aout_instance_t * p_aout = findAout( p_object );
267 if ( p_aout ) aout_lock_volume( p_aout );
268 b_return_val = var_GetBool( p_object, "volume-muted");
271 aout_unlock_volume( p_aout );
272 vlc_object_release( p_aout );
277 /*****************************************************************************
278 * aout_SetMute : Sets mute status
279 *****************************************************************************
280 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
281 * function (muted => 0).
282 *****************************************************************************/
283 int aout_SetMute( vlc_object_t * p_object, audio_volume_t * pi_volume,
286 return doVolumeChanges( SET_MUTE, p_object, 1, 0, pi_volume, b_mute );
290 * The next functions are not supposed to be called by the interface, but
291 * are placeholders for software-only scaling.
294 /* Meant to be called by the output plug-in's Open(). */
295 void aout_VolumeSoftInit( aout_instance_t * p_aout )
299 p_aout->output.pf_volume_get = aout_VolumeSoftGet;
300 p_aout->output.pf_volume_set = aout_VolumeSoftSet;
302 i_volume = config_GetInt( p_aout, "volume" );
303 if ( i_volume < AOUT_VOLUME_MIN )
305 i_volume = AOUT_VOLUME_DEFAULT;
307 else if ( i_volume > AOUT_VOLUME_MAX )
309 i_volume = AOUT_VOLUME_MAX;
312 aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
315 /* Placeholder for pf_volume_get(). */
316 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
318 *pi_volume = p_aout->output.i_volume;
323 /* Placeholder for pf_volume_set(). */
324 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
326 aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
327 p_aout->output.i_volume = i_volume;
332 * The next functions are not supposed to be called by the interface, but
333 * are placeholders for unsupported scaling.
336 /* Meant to be called by the output plug-in's Open(). */
337 void aout_VolumeNoneInit( aout_instance_t * p_aout )
339 p_aout->output.pf_volume_get = aout_VolumeNoneGet;
340 p_aout->output.pf_volume_set = aout_VolumeNoneSet;
343 /* Placeholder for pf_volume_get(). */
344 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
346 (void)p_aout; (void)pi_volume;
350 /* Placeholder for pf_volume_set(). */
351 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
353 (void)p_aout; (void)i_volume;
359 * Pipelines management
362 /*****************************************************************************
363 * aout_Restart : re-open the output device and rebuild the input and output
365 *****************************************************************************
366 * This function is used whenever the parameters of the output plug-in are
367 * changed (eg. selecting S/PDIF or PCM).
368 *****************************************************************************/
369 static int aout_Restart( aout_instance_t * p_aout )
374 aout_lock_mixer( p_aout );
376 if ( p_aout->i_nb_inputs == 0 )
378 aout_unlock_mixer( p_aout );
379 msg_Err( p_aout, "no decoder thread" );
383 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
385 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
386 aout_lock_input_fifos( p_aout );
387 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
388 aout_unlock_input_fifos( p_aout );
391 /* Lock all inputs. */
392 aout_lock_input_fifos( p_aout );
393 aout_MixerDelete( p_aout );
395 /* Re-open the output plug-in. */
396 aout_OutputDelete( p_aout );
398 if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
400 /* Release all locks and report the error. */
401 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
403 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
405 aout_unlock_input_fifos( p_aout );
406 aout_unlock_mixer( p_aout );
410 if ( aout_MixerNew( p_aout ) == -1 )
412 aout_OutputDelete( p_aout );
413 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
415 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
417 aout_unlock_input_fifos( p_aout );
418 aout_unlock_mixer( p_aout );
422 /* Re-open all inputs. */
423 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
425 aout_input_t * p_input = p_aout->pp_inputs[i];
426 b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
427 p_input->b_changed = 1;
428 aout_unlock_input( p_aout, p_input );
431 aout_unlock_input_fifos( p_aout );
432 aout_unlock_mixer( p_aout );
437 /*****************************************************************************
438 * aout_ChannelsRestart : change the audio device or channels and restart
439 *****************************************************************************/
440 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
441 vlc_value_t oldval, vlc_value_t newval,
444 aout_instance_t * p_aout = (aout_instance_t *)p_this;
445 (void)oldval; (void)newval; (void)p_data;
447 if ( !strcmp( psz_variable, "audio-device" ) )
449 /* This is supposed to be a significant change and supposes
450 * rebuilding the channel choices. */
451 var_Destroy( p_aout, "audio-channels" );
453 aout_Restart( p_aout );
457 #undef aout_EnableFilter
458 /** Enable or disable an audio filter
459 * \param p_this a vlc object
460 * \param psz_name name of the filter
461 * \param b_add are we adding or removing the filter ?
463 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
466 aout_instance_t *p_aout = findAout( p_this );
468 if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
471 AoutInputsMarkToRestart( p_aout );
475 vlc_object_release( p_aout );