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>
33 #include <vlc_aout_intf.h>
36 #include <stdlib.h> /* calloc(), malloc(), free() */
40 #include "aout_internal.h"
42 #include <vlc_playlist.h>
44 static audio_output_t *findAout (vlc_object_t *obj)
46 input_thread_t *(*pf_find_input) (vlc_object_t *);
48 pf_find_input = var_GetAddress (obj, "find-input-callback");
49 if (unlikely(pf_find_input == NULL))
52 input_thread_t *p_input = pf_find_input (obj);
56 audio_output_t *p_aout = input_GetAout (p_input);
57 vlc_object_release (p_input);
60 #define findAout(o) findAout(VLC_OBJECT(o))
62 /** Start a volume change transaction. */
63 static void prepareVolume (vlc_object_t *obj, audio_output_t **aoutp,
64 audio_volume_t *volp, bool *mutep)
66 audio_output_t *aout = findAout (obj);
68 /* FIXME: we need interlocking even if aout does not exist! */
71 aout_lock_volume (aout);
73 *volp = var_GetInteger (obj, "volume");
75 *mutep = var_GetBool (obj, "mute");
78 /** Commit a volume change transaction. */
79 static int commitVolume (vlc_object_t *obj, audio_output_t *aout,
80 audio_volume_t volume, bool mute)
84 var_SetInteger (obj, "volume", volume);
85 var_SetBool (obj, "mute", mute);
89 float vol = volume / (float)AOUT_VOLUME_DEFAULT;
92 #warning FIXME: wrong test. Need to check that aout_output is ready.
93 if (aout->p_mixer != NULL)
94 ret = aout->pf_volume_set (aout, vol, mute);
98 var_TriggerCallback (aout, "intf-change");
99 aout_unlock_volume (aout);
100 vlc_object_release (aout);
106 /** Cancel a volume change transaction. */
107 static void cancelVolume (vlc_object_t *obj, audio_output_t *aout)
112 aout_unlock_volume (aout);
113 vlc_object_release (aout);
118 #undef aout_VolumeGet
120 * Gets the volume of the output device (independent of mute).
122 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
125 audio_output_t *aout;
126 audio_volume_t volume;
128 prepareVolume (obj, &aout, &volume, NULL);
129 cancelVolume (obj, aout);
132 return var_GetInteger (obj, "volume");
136 #undef aout_VolumeSet
138 * Sets the volume of the output device.
139 * The mute status is not changed.
141 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
143 audio_output_t *aout;
146 prepareVolume (obj, &aout, NULL, &mute);
147 return commitVolume (obj, aout, volume, mute);
153 * \param value how much to increase (> 0) or decrease (< 0) the volume
154 * \param volp if non-NULL, will contain contain the resulting volume
156 int aout_VolumeUp (vlc_object_t *obj, int value, audio_volume_t *volp)
158 audio_output_t *aout;
160 audio_volume_t volume;
163 value *= var_InheritInteger (obj, "volume-step");
165 prepareVolume (obj, &aout, &volume, &mute);
170 if (value > AOUT_VOLUME_MAX)
171 volume = AOUT_VOLUME_MAX;
174 ret = commitVolume (obj, aout, volume, mute);
180 #undef aout_ToggleMute
182 * Toggles the mute state.
184 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
186 audio_output_t *aout;
188 audio_volume_t volume;
191 prepareVolume (obj, &aout, &volume, &mute);
193 ret = commitVolume (obj, aout, volume, mute);
195 *volp = mute ? 0 : volume;
200 * Gets the output mute status.
202 bool aout_IsMuted (vlc_object_t *obj)
205 audio_output_t *aout;
208 prepareVolume (obj, &aout, NULL, &mute);
209 cancelVolume (obj, aout);
212 return var_GetBool (obj, "mute");
219 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
221 audio_output_t *aout;
223 audio_volume_t volume;
225 prepareVolume (obj, &aout, &volume, NULL);
226 ret = commitVolume (obj, aout, volume, mute);
228 *volp = mute ? 0 : volume;
234 * Pipelines management
237 /*****************************************************************************
238 * aout_Restart : re-open the output device and rebuild the input and output
240 *****************************************************************************
241 * This function is used whenever the parameters of the output plug-in are
242 * changed (eg. selecting S/PDIF or PCM).
243 *****************************************************************************/
244 static int aout_Restart( audio_output_t * p_aout )
246 aout_input_t *p_input;
249 p_input = p_aout->p_input;
250 if( p_input == NULL )
252 aout_unlock( p_aout );
253 msg_Err( p_aout, "no decoder thread" );
257 /* Reinitializes the output */
258 aout_InputDelete( p_aout, p_input );
259 aout_MixerDelete( p_aout );
260 aout_OutputDelete( p_aout );
262 /* FIXME: This function is notoriously dangerous/unsafe.
263 * By the way, if OutputNew or MixerNew fails, we are totally screwed. */
264 if ( aout_OutputNew( p_aout, &p_input->input ) == -1 )
266 /* Release all locks and report the error. */
267 aout_unlock( p_aout );
271 if ( aout_MixerNew( p_aout ) == -1 )
273 aout_OutputDelete( p_aout );
274 aout_unlock( p_aout );
278 if( aout_InputNew( p_aout, p_input, &p_input->request_vout ) )
280 #warning FIXME: deal with errors
281 aout_unlock( p_aout );
284 aout_unlock( p_aout );
288 /*****************************************************************************
289 * aout_ChannelsRestart : change the audio device or channels and restart
290 *****************************************************************************/
291 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
292 vlc_value_t oldval, vlc_value_t newval,
295 audio_output_t * p_aout = (audio_output_t *)p_this;
296 (void)oldval; (void)newval; (void)p_data;
298 if ( !strcmp( psz_variable, "audio-device" ) )
300 /* This is supposed to be a significant change and supposes
301 * rebuilding the channel choices. */
302 var_Destroy( p_aout, "audio-channels" );
304 aout_Restart( p_aout );
308 #undef aout_EnableFilter
309 /** Enable or disable an audio filter
310 * \param p_this a vlc object
311 * \param psz_name name of the filter
312 * \param b_add are we adding or removing the filter ?
314 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
317 audio_output_t *p_aout = findAout( p_this );
319 if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
322 AoutInputsMarkToRestart( p_aout );
326 vlc_object_release( p_aout );