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))
61 /** Start a volume change transaction. */
62 static void prepareVolume (vlc_object_t *obj, aout_instance_t **aoutp,
63 audio_volume_t *volp, bool *mutep)
65 aout_instance_t *aout = findAout (obj);
67 /* FIXME: we need interlocking even if aout does not exist! */
70 aout_lock_volume (aout);
72 *volp = config_GetInt (obj, "volume");
74 *mutep = var_GetBool (obj, "volume-muted");
77 /** Commit a volume change transaction. */
78 static int commitVolume (vlc_object_t *obj, aout_instance_t *aout,
79 audio_volume_t volume, bool mute)
83 config_PutInt (obj, "volume", volume);
85 volume = AOUT_VOLUME_MIN;
86 var_SetBool (obj, "volume-muted", mute);
90 aout_lock_mixer (aout);
91 aout_lock_input_fifos (aout);
92 if (aout->p_mixer != NULL)
93 ret = aout->output.pf_volume_set (aout, volume);
94 aout_unlock_input_fifos (aout);
95 aout_unlock_mixer (aout);
98 var_SetBool (aout, "intf-change", true);
99 aout_unlock_volume (aout);
100 vlc_object_release (aout);
103 /* trigger callbacks */
104 var_TriggerCallback (obj, "volume-change");
110 /** Cancel a volume change transaction. */
111 static void cancelVolume (vlc_object_t *obj, aout_instance_t *aout)
116 aout_unlock_volume (aout);
117 vlc_object_release (aout);
122 #undef aout_VolumeGet
124 * Gets the volume of the output device (independent of mute).
126 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
129 aout_instance_t *aout;
130 audio_volume_t volume;
132 prepareVolume (obj, &aout, &volume, NULL);
133 cancelVolume (obj, aout);
136 return config_GetInt (obj, "volume");
140 #undef aout_VolumeSet
142 * Sets the volume of the output device.
143 * The mute status is not changed.
145 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
147 aout_instance_t *aout;
150 prepareVolume (obj, &aout, NULL, &mute);
151 return commitVolume (obj, aout, volume, mute);
157 * \param volp if non-NULL, will contain contain the resulting volume
159 int aout_VolumeUp (vlc_object_t *obj, int steps, audio_volume_t *volp)
161 aout_instance_t *aout;
163 int stepsize = var_InheritInteger (obj, "volume-step");
164 audio_volume_t volume;
167 prepareVolume (obj, &aout, &volume, &mute);
168 volume += stepsize * steps;
169 ret = commitVolume (obj, aout, volume, mute);
175 #undef aout_VolumeDown
177 * Lowers the volume. See aout_VolumeUp().
179 int aout_VolumeDown (vlc_object_t *obj, int steps, audio_volume_t *volp)
181 return aout_VolumeUp (obj, -steps, volp);
184 #undef aout_ToggleMute
186 * Toggles the mute state.
188 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
190 aout_instance_t *aout;
192 audio_volume_t volume;
195 prepareVolume (obj, &aout, &volume, &mute);
197 ret = commitVolume (obj, aout, volume, mute);
199 *volp = mute ? AOUT_VOLUME_MIN : volume;
204 * Gets the output mute status.
206 bool aout_IsMuted (vlc_object_t *obj)
209 aout_instance_t *aout;
212 prepareVolume (obj, &aout, NULL, &mute);
213 cancelVolume (obj, aout);
216 return var_GetBool (obj, "volume-muted");
223 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
225 aout_instance_t *aout;
227 audio_volume_t volume;
229 prepareVolume (obj, &aout, &volume, NULL);
230 ret = commitVolume (obj, aout, volume, mute);
232 *volp = mute ? AOUT_VOLUME_MIN : volume;
237 * The next functions are not supposed to be called by the interface, but
238 * are placeholders for software-only scaling.
241 /* Meant to be called by the output plug-in's Open(). */
242 void aout_VolumeSoftInit( aout_instance_t * p_aout )
246 p_aout->output.pf_volume_get = aout_VolumeSoftGet;
247 p_aout->output.pf_volume_set = aout_VolumeSoftSet;
249 i_volume = config_GetInt( p_aout, "volume" );
250 if ( i_volume < AOUT_VOLUME_MIN )
252 i_volume = AOUT_VOLUME_DEFAULT;
254 else if ( i_volume > AOUT_VOLUME_MAX )
256 i_volume = AOUT_VOLUME_MAX;
259 aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
262 /* Placeholder for pf_volume_get(). */
263 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
265 *pi_volume = p_aout->output.i_volume;
270 /* Placeholder for pf_volume_set(). */
271 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
273 aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
274 p_aout->output.i_volume = i_volume;
279 * The next functions are not supposed to be called by the interface, but
280 * are placeholders for unsupported scaling.
283 /* Meant to be called by the output plug-in's Open(). */
284 void aout_VolumeNoneInit( aout_instance_t * p_aout )
286 p_aout->output.pf_volume_get = aout_VolumeNoneGet;
287 p_aout->output.pf_volume_set = aout_VolumeNoneSet;
290 /* Placeholder for pf_volume_get(). */
291 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
293 (void)p_aout; (void)pi_volume;
297 /* Placeholder for pf_volume_set(). */
298 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
300 (void)p_aout; (void)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 static int aout_Restart( aout_instance_t * p_aout )
321 aout_lock_mixer( p_aout );
323 if ( p_aout->i_nb_inputs == 0 )
325 aout_unlock_mixer( p_aout );
326 msg_Err( p_aout, "no decoder thread" );
330 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
332 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
333 aout_lock_input_fifos( p_aout );
334 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
335 aout_unlock_input_fifos( p_aout );
338 /* Lock all inputs. */
339 aout_lock_input_fifos( p_aout );
340 aout_MixerDelete( p_aout );
342 /* Re-open the output plug-in. */
343 aout_OutputDelete( p_aout );
345 if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
347 /* Release all locks and report the error. */
348 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
350 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
352 aout_unlock_input_fifos( p_aout );
353 aout_unlock_mixer( p_aout );
357 if ( aout_MixerNew( p_aout ) == -1 )
359 aout_OutputDelete( p_aout );
360 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
362 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
364 aout_unlock_input_fifos( p_aout );
365 aout_unlock_mixer( p_aout );
369 /* Re-open all inputs. */
370 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
372 aout_input_t * p_input = p_aout->pp_inputs[i];
373 b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
374 p_input->b_changed = 1;
375 aout_unlock_input( p_aout, p_input );
378 aout_unlock_input_fifos( p_aout );
379 aout_unlock_mixer( p_aout );
384 /*****************************************************************************
385 * aout_ChannelsRestart : change the audio device or channels and restart
386 *****************************************************************************/
387 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
388 vlc_value_t oldval, vlc_value_t newval,
391 aout_instance_t * p_aout = (aout_instance_t *)p_this;
392 (void)oldval; (void)newval; (void)p_data;
394 if ( !strcmp( psz_variable, "audio-device" ) )
396 /* This is supposed to be a significant change and supposes
397 * rebuilding the channel choices. */
398 var_Destroy( p_aout, "audio-channels" );
400 aout_Restart( p_aout );
404 #undef aout_EnableFilter
405 /** Enable or disable an audio filter
406 * \param p_this a vlc object
407 * \param psz_name name of the filter
408 * \param b_add are we adding or removing the filter ?
410 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
413 aout_instance_t *p_aout = findAout( p_this );
415 if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
418 AoutInputsMarkToRestart( p_aout );
422 vlc_object_release( p_aout );