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 aout_owner_t *owner = aout_owner (aout);
90 float vol = volume / (float)AOUT_VOLUME_DEFAULT;
93 #warning FIXME: wrong test. Need to check that aout_output is ready.
94 if (owner->volume.mixer != NULL)
95 ret = aout->pf_volume_set (aout, vol, mute);
99 var_TriggerCallback (aout, "intf-change");
100 aout_unlock_volume (aout);
101 vlc_object_release (aout);
107 /** Cancel a volume change transaction. */
108 static void cancelVolume (vlc_object_t *obj, audio_output_t *aout)
113 aout_unlock_volume (aout);
114 vlc_object_release (aout);
119 #undef aout_VolumeGet
121 * Gets the volume of the output device (independent of mute).
123 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
126 audio_output_t *aout;
127 audio_volume_t volume;
129 prepareVolume (obj, &aout, &volume, NULL);
130 cancelVolume (obj, aout);
133 return var_GetInteger (obj, "volume");
137 #undef aout_VolumeSet
139 * Sets the volume of the output device.
140 * The mute status is not changed.
142 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
144 audio_output_t *aout;
147 prepareVolume (obj, &aout, NULL, &mute);
148 return commitVolume (obj, aout, volume, mute);
154 * \param value how much to increase (> 0) or decrease (< 0) the volume
155 * \param volp if non-NULL, will contain contain the resulting volume
157 int aout_VolumeUp (vlc_object_t *obj, int value, audio_volume_t *volp)
159 audio_output_t *aout;
161 audio_volume_t volume;
164 value *= var_InheritInteger (obj, "volume-step");
166 prepareVolume (obj, &aout, &volume, &mute);
171 if (value > AOUT_VOLUME_MAX)
172 volume = AOUT_VOLUME_MAX;
175 ret = commitVolume (obj, aout, volume, mute);
181 #undef aout_ToggleMute
183 * Toggles the mute state.
185 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
187 audio_output_t *aout;
189 audio_volume_t volume;
192 prepareVolume (obj, &aout, &volume, &mute);
194 ret = commitVolume (obj, aout, volume, mute);
196 *volp = mute ? 0 : volume;
201 * Gets the output mute status.
203 bool aout_IsMuted (vlc_object_t *obj)
206 audio_output_t *aout;
209 prepareVolume (obj, &aout, NULL, &mute);
210 cancelVolume (obj, aout);
213 return var_GetBool (obj, "mute");
220 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
222 audio_output_t *aout;
224 audio_volume_t volume;
226 prepareVolume (obj, &aout, &volume, NULL);
227 ret = commitVolume (obj, aout, volume, mute);
229 *volp = mute ? 0 : volume;
235 * Pipelines management
238 /*****************************************************************************
239 * aout_Restart : re-open the output device and rebuild the input and output
241 *****************************************************************************
242 * This function is used whenever the parameters of the output plug-in are
243 * changed (eg. selecting S/PDIF or PCM).
244 *****************************************************************************/
245 static int aout_Restart( audio_output_t * p_aout )
247 aout_input_t *p_input;
248 aout_owner_t *owner = aout_owner (p_aout);
251 p_input = owner->input;
252 if( p_input == NULL )
254 aout_unlock( p_aout );
255 msg_Err( p_aout, "no decoder thread" );
259 /* Reinitializes the output */
260 aout_InputDelete( p_aout, p_input );
261 aout_MixerDelete (owner->volume.mixer);
262 owner->volume.mixer = NULL;
263 aout_OutputDelete( p_aout );
265 /* FIXME: This function is notoriously dangerous/unsafe.
266 * By the way, if OutputNew or MixerNew fails, we are totally screwed. */
267 if ( aout_OutputNew( p_aout, &p_input->input ) == -1 )
269 /* Release all locks and report the error. */
270 aout_unlock( p_aout );
274 owner->volume.mixer = aout_MixerNew (p_aout, owner->mixer_format.i_format);
275 if (owner->volume.mixer == NULL)
277 aout_OutputDelete( p_aout );
278 aout_unlock( p_aout );
282 if( aout_InputNew( p_aout, p_input, &p_input->request_vout ) )
284 #warning FIXME: deal with errors
285 aout_unlock( p_aout );
288 aout_unlock( p_aout );
292 /*****************************************************************************
293 * aout_ChannelsRestart : change the audio device or channels and restart
294 *****************************************************************************/
295 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
296 vlc_value_t oldval, vlc_value_t newval,
299 audio_output_t * p_aout = (audio_output_t *)p_this;
300 (void)oldval; (void)newval; (void)p_data;
302 if ( !strcmp( psz_variable, "audio-device" ) )
304 /* This is supposed to be a significant change and supposes
305 * rebuilding the channel choices. */
306 var_Destroy( p_aout, "audio-channels" );
308 aout_Restart( p_aout );
312 #undef aout_EnableFilter
313 /** Enable or disable an audio filter
314 * \param p_this a vlc object
315 * \param psz_name name of the filter
316 * \param b_add are we adding or removing the filter ?
318 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
321 audio_output_t *p_aout = findAout( p_this );
323 if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
326 AoutInputsMarkToRestart( p_aout );
330 vlc_object_release( p_aout );