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 = var_GetInteger (obj, "volume");
74 *mutep = var_GetBool (obj, "mute");
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 var_SetInteger (obj, "volume", volume);
84 var_SetBool (obj, "mute", mute);
88 aout_lock_mixer (aout);
89 aout_lock_input_fifos (aout);
90 if (aout->p_mixer != NULL)
91 ret = aout->output.pf_volume_set (aout, volume, mute);
92 aout_unlock_input_fifos (aout);
93 aout_unlock_mixer (aout);
96 var_TriggerCallback (aout, "intf-change");
97 aout_unlock_volume (aout);
98 vlc_object_release (aout);
104 /** Cancel a volume change transaction. */
105 static void cancelVolume (vlc_object_t *obj, aout_instance_t *aout)
110 aout_unlock_volume (aout);
111 vlc_object_release (aout);
116 #undef aout_VolumeGet
118 * Gets the volume of the output device (independent of mute).
120 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
123 aout_instance_t *aout;
124 audio_volume_t volume;
126 prepareVolume (obj, &aout, &volume, NULL);
127 cancelVolume (obj, aout);
130 return var_GetInteger (obj, "volume");
134 #undef aout_VolumeSet
136 * Sets the volume of the output device.
137 * The mute status is not changed.
139 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
141 aout_instance_t *aout;
144 prepareVolume (obj, &aout, NULL, &mute);
145 return commitVolume (obj, aout, volume, mute);
151 * \param volp if non-NULL, will contain contain the resulting volume
153 int aout_VolumeUp (vlc_object_t *obj, int steps, audio_volume_t *volp)
155 aout_instance_t *aout;
157 int stepsize = var_InheritInteger (obj, "volume-step");
158 audio_volume_t volume;
161 prepareVolume (obj, &aout, &volume, &mute);
162 volume += stepsize * steps;
163 ret = commitVolume (obj, aout, volume, mute);
169 #undef aout_VolumeDown
171 * Lowers the volume. See aout_VolumeUp().
173 int aout_VolumeDown (vlc_object_t *obj, int steps, audio_volume_t *volp)
175 return aout_VolumeUp (obj, -steps, volp);
178 #undef aout_ToggleMute
180 * Toggles the mute state.
182 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
184 aout_instance_t *aout;
186 audio_volume_t volume;
189 prepareVolume (obj, &aout, &volume, &mute);
191 ret = commitVolume (obj, aout, volume, mute);
193 *volp = mute ? AOUT_VOLUME_MIN : volume;
198 * Gets the output mute status.
200 bool aout_IsMuted (vlc_object_t *obj)
203 aout_instance_t *aout;
206 prepareVolume (obj, &aout, NULL, &mute);
207 cancelVolume (obj, aout);
210 return var_GetBool (obj, "mute");
217 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
219 aout_instance_t *aout;
221 audio_volume_t volume;
223 prepareVolume (obj, &aout, &volume, NULL);
224 ret = commitVolume (obj, aout, volume, mute);
226 *volp = mute ? AOUT_VOLUME_MIN : volume;
232 * The next functions are not supposed to be called by the interface, but
233 * are placeholders for software-only scaling.
235 static int aout_VolumeSoftSet (aout_instance_t *aout, audio_volume_t volume,
238 float f = mute ? 0. : (volume / (float)AOUT_VOLUME_DEFAULT);
239 aout_MixerMultiplierSet (aout, f);
240 aout->output.i_volume = volume;
244 /* Meant to be called by the output plug-in's Open(). */
245 void aout_VolumeSoftInit (aout_instance_t *aout)
247 audio_volume_t volume = var_InheritInteger (aout, "volume");
248 bool mute = var_InheritBool (aout, "mute");
250 aout->output.pf_volume_set = aout_VolumeSoftSet;
251 aout_VolumeSoftSet (aout, volume, mute);
256 * The next functions are not supposed to be called by the interface, but
257 * are placeholders for unsupported scaling.
259 static int aout_VolumeNoneSet (aout_instance_t *aout, audio_volume_t volume,
262 (void)aout; (void)volume; (void)mute;
266 /* Meant to be called by the output plug-in's Open(). */
267 void aout_VolumeNoneInit( aout_instance_t * p_aout )
269 p_aout->output.pf_volume_set = aout_VolumeNoneSet;
274 * Pipelines management
277 /*****************************************************************************
278 * aout_Restart : re-open the output device and rebuild the input and output
280 *****************************************************************************
281 * This function is used whenever the parameters of the output plug-in are
282 * changed (eg. selecting S/PDIF or PCM).
283 *****************************************************************************/
284 static int aout_Restart( aout_instance_t * p_aout )
289 aout_lock_mixer( p_aout );
291 if ( p_aout->i_nb_inputs == 0 )
293 aout_unlock_mixer( p_aout );
294 msg_Err( p_aout, "no decoder thread" );
298 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
300 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
301 aout_lock_input_fifos( p_aout );
302 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
303 aout_unlock_input_fifos( p_aout );
306 /* Lock all inputs. */
307 aout_lock_input_fifos( p_aout );
308 aout_MixerDelete( p_aout );
310 /* Re-open the output plug-in. */
311 aout_OutputDelete( p_aout );
313 if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
315 /* Release all locks and report the error. */
316 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
318 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
320 aout_unlock_input_fifos( p_aout );
321 aout_unlock_mixer( p_aout );
325 if ( aout_MixerNew( p_aout ) == -1 )
327 aout_OutputDelete( p_aout );
328 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
330 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
332 aout_unlock_input_fifos( p_aout );
333 aout_unlock_mixer( p_aout );
337 /* Re-open all inputs. */
338 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
340 aout_input_t * p_input = p_aout->pp_inputs[i];
341 b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
342 p_input->b_changed = 1;
343 aout_unlock_input( p_aout, p_input );
346 aout_unlock_input_fifos( p_aout );
347 aout_unlock_mixer( p_aout );
352 /*****************************************************************************
353 * aout_ChannelsRestart : change the audio device or channels and restart
354 *****************************************************************************/
355 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
356 vlc_value_t oldval, vlc_value_t newval,
359 aout_instance_t * p_aout = (aout_instance_t *)p_this;
360 (void)oldval; (void)newval; (void)p_data;
362 if ( !strcmp( psz_variable, "audio-device" ) )
364 /* This is supposed to be a significant change and supposes
365 * rebuilding the channel choices. */
366 var_Destroy( p_aout, "audio-channels" );
368 aout_Restart( p_aout );
372 #undef aout_EnableFilter
373 /** Enable or disable an audio filter
374 * \param p_this a vlc object
375 * \param psz_name name of the filter
376 * \param b_add are we adding or removing the filter ?
378 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
381 aout_instance_t *p_aout = findAout( p_this );
383 if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
386 AoutInputsMarkToRestart( p_aout );
390 vlc_object_release( p_aout );