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 ( p_aout == NULL )
190 *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
194 aout_lock_volume( p_aout );
195 aout_lock_mixer( p_aout );
196 if ( p_aout->p_mixer )
198 i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
202 *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
204 aout_unlock_mixer( p_aout );
205 aout_unlock_volume( p_aout );
207 vlc_object_release( p_aout );
211 #undef aout_VolumeSet
212 /*****************************************************************************
213 * aout_VolumeSet : set the volume of the output device
214 *****************************************************************************/
215 int aout_VolumeSet( vlc_object_t * p_object, audio_volume_t i_volume )
217 return doVolumeChanges( SET_VOLUME, p_object, 1, i_volume, NULL, true );
221 /*****************************************************************************
222 * aout_VolumeUp : raise the output volume
223 *****************************************************************************
224 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
226 *****************************************************************************/
227 int aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
228 audio_volume_t * pi_volume )
230 return doVolumeChanges( INCREMENT_VOLUME, p_object, i_nb_steps, 0, pi_volume, true );
233 #undef aout_VolumeDown
234 /*****************************************************************************
235 * aout_VolumeDown : lower the output volume
236 *****************************************************************************
237 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
239 *****************************************************************************/
240 int aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
241 audio_volume_t * pi_volume )
243 return aout_VolumeUp( p_object, -i_nb_steps, pi_volume );
246 #undef aout_ToggleMute
247 /*****************************************************************************
248 * aout_ToggleMute : Mute/un-mute the output volume
249 *****************************************************************************
250 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
251 * function (muted => 0).
252 *****************************************************************************/
253 int aout_ToggleMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
255 return doVolumeChanges( TOGGLE_MUTE, p_object, 1, 0, pi_volume, true );
258 /*****************************************************************************
259 * aout_IsMuted : Get the output volume mute status
260 *****************************************************************************/
261 bool aout_IsMuted( vlc_object_t * p_object )
264 aout_instance_t * p_aout = findAout( p_object );
265 if ( p_aout ) aout_lock_volume( p_aout );
266 b_return_val = var_GetBool( p_object, "volume-muted");
269 aout_unlock_volume( p_aout );
270 vlc_object_release( p_aout );
275 /*****************************************************************************
276 * aout_SetMute : Sets mute status
277 *****************************************************************************
278 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
279 * function (muted => 0).
280 *****************************************************************************/
281 int aout_SetMute( vlc_object_t * p_object, audio_volume_t * pi_volume,
284 return doVolumeChanges( SET_MUTE, p_object, 1, 0, pi_volume, b_mute );
288 * The next functions are not supposed to be called by the interface, but
289 * are placeholders for software-only scaling.
292 /* Meant to be called by the output plug-in's Open(). */
293 void aout_VolumeSoftInit( aout_instance_t * p_aout )
297 p_aout->output.pf_volume_get = aout_VolumeSoftGet;
298 p_aout->output.pf_volume_set = aout_VolumeSoftSet;
300 i_volume = config_GetInt( p_aout, "volume" );
301 if ( i_volume < AOUT_VOLUME_MIN )
303 i_volume = AOUT_VOLUME_DEFAULT;
305 else if ( i_volume > AOUT_VOLUME_MAX )
307 i_volume = AOUT_VOLUME_MAX;
310 aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
313 /* Placeholder for pf_volume_get(). */
314 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
316 *pi_volume = p_aout->output.i_volume;
321 /* Placeholder for pf_volume_set(). */
322 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
324 aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
325 p_aout->output.i_volume = i_volume;
330 * The next functions are not supposed to be called by the interface, but
331 * are placeholders for unsupported scaling.
334 /* Meant to be called by the output plug-in's Open(). */
335 void aout_VolumeNoneInit( aout_instance_t * p_aout )
337 p_aout->output.pf_volume_get = aout_VolumeNoneGet;
338 p_aout->output.pf_volume_set = aout_VolumeNoneSet;
341 /* Placeholder for pf_volume_get(). */
342 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
344 (void)p_aout; (void)pi_volume;
348 /* Placeholder for pf_volume_set(). */
349 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
351 (void)p_aout; (void)i_volume;
357 * Pipelines management
360 /*****************************************************************************
361 * aout_Restart : re-open the output device and rebuild the input and output
363 *****************************************************************************
364 * This function is used whenever the parameters of the output plug-in are
365 * changed (eg. selecting S/PDIF or PCM).
366 *****************************************************************************/
367 static int aout_Restart( aout_instance_t * p_aout )
372 aout_lock_mixer( p_aout );
374 if ( p_aout->i_nb_inputs == 0 )
376 aout_unlock_mixer( p_aout );
377 msg_Err( p_aout, "no decoder thread" );
381 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
383 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
384 aout_lock_input_fifos( p_aout );
385 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
386 aout_unlock_input_fifos( p_aout );
389 /* Lock all inputs. */
390 aout_lock_input_fifos( p_aout );
391 aout_MixerDelete( p_aout );
393 /* Re-open the output plug-in. */
394 aout_OutputDelete( p_aout );
396 if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
398 /* Release all locks and report the error. */
399 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
401 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
403 aout_unlock_input_fifos( p_aout );
404 aout_unlock_mixer( p_aout );
408 if ( aout_MixerNew( p_aout ) == -1 )
410 aout_OutputDelete( p_aout );
411 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
413 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
415 aout_unlock_input_fifos( p_aout );
416 aout_unlock_mixer( p_aout );
420 /* Re-open all inputs. */
421 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
423 aout_input_t * p_input = p_aout->pp_inputs[i];
424 b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
425 p_input->b_changed = 1;
426 aout_unlock_input( p_aout, p_input );
429 aout_unlock_input_fifos( p_aout );
430 aout_unlock_mixer( p_aout );
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 oldval, vlc_value_t newval,
442 aout_instance_t * p_aout = (aout_instance_t *)p_this;
443 (void)oldval; (void)newval; (void)p_data;
445 if ( !strcmp( psz_variable, "audio-device" ) )
447 /* This is supposed to be a significant change and supposes
448 * rebuilding the channel choices. */
449 var_Destroy( p_aout, "audio-channels" );
451 aout_Restart( p_aout );
455 #undef aout_EnableFilter
456 /** Enable or disable an audio filter
457 * \param p_this a vlc object
458 * \param psz_name name of the filter
459 * \param b_add are we adding or removing the filter ?
461 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
464 aout_instance_t *p_aout = findAout( p_this );
466 if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
469 AoutInputsMarkToRestart( p_aout );
473 vlc_object_release( p_aout );