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! */
72 obj = VLC_OBJECT(aout); /* use aout volume if aout exists */
73 aout_lock_volume (aout);
76 *volp = var_InheritInteger (obj, "volume");
78 *mutep = var_InheritBool (obj, "mute");
81 /** Commit a volume change transaction. */
82 static int commitVolume (vlc_object_t *obj, audio_output_t *aout,
83 audio_volume_t volume, bool mute)
87 /* update caller (input manager) volume */
88 var_SetInteger (obj, "volume", volume);
89 var_SetBool (obj, "mute", mute);
93 aout_owner_t *owner = aout_owner (aout);
94 float vol = volume / (float)AOUT_VOLUME_DEFAULT;
96 /* apply volume to the pipeline */
98 if (owner->module != NULL)
99 ret = aout->pf_volume_set (aout, vol, mute);
102 /* update aout volume if it maintains its own */
103 var_SetInteger (aout, "volume", volume);
104 var_SetBool (aout, "mute", mute);
105 aout_unlock_volume (aout);
108 var_TriggerCallback (aout, "intf-change");
109 vlc_object_release (aout);
114 /** Cancel a volume change transaction. */
115 static void cancelVolume (vlc_object_t *obj, audio_output_t *aout)
120 aout_unlock_volume (aout);
121 vlc_object_release (aout);
125 #undef aout_VolumeGet
127 * Gets the volume of the output device (independent of mute).
129 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
131 audio_output_t *aout;
132 audio_volume_t volume;
134 prepareVolume (obj, &aout, &volume, NULL);
135 cancelVolume (obj, aout);
139 #undef aout_VolumeSet
141 * Sets the volume of the output device.
142 * The mute status is not changed.
144 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
146 audio_output_t *aout;
149 prepareVolume (obj, &aout, NULL, &mute);
150 return commitVolume (obj, aout, volume, mute);
156 * \param value how much to increase (> 0) or decrease (< 0) the volume
157 * \param volp if non-NULL, will contain contain the resulting volume
159 int aout_VolumeUp (vlc_object_t *obj, int value, audio_volume_t *volp)
161 audio_output_t *aout;
163 audio_volume_t volume;
166 value *= var_InheritInteger (obj, "volume-step");
168 prepareVolume (obj, &aout, &volume, &mute);
173 if (value > AOUT_VOLUME_MAX)
174 volume = AOUT_VOLUME_MAX;
177 ret = commitVolume (obj, aout, volume, mute);
183 #undef aout_ToggleMute
185 * Toggles the mute state.
187 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
189 audio_output_t *aout;
191 audio_volume_t volume;
194 prepareVolume (obj, &aout, &volume, &mute);
196 ret = commitVolume (obj, aout, volume, mute);
198 *volp = mute ? 0 : volume;
203 * Gets the output mute status.
205 bool aout_IsMuted (vlc_object_t *obj)
207 audio_output_t *aout;
210 prepareVolume (obj, &aout, NULL, &mute);
211 cancelVolume (obj, aout);
218 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
220 audio_output_t *aout;
222 audio_volume_t volume;
224 prepareVolume (obj, &aout, &volume, NULL);
225 ret = commitVolume (obj, aout, volume, mute);
227 *volp = mute ? 0 : volume;
233 * Pipelines management
237 * Marks the audio output for restart, to update any parameter of the output
238 * plug-in (e.g. output device or channel mapping).
240 static void aout_Restart (audio_output_t *aout)
242 aout_owner_t *owner = aout_owner (aout);
245 if (owner->input != NULL)
246 owner->need_restart = true;
250 /*****************************************************************************
251 * aout_ChannelsRestart : change the audio device or channels and restart
252 *****************************************************************************/
253 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
254 vlc_value_t oldval, vlc_value_t newval,
257 audio_output_t * p_aout = (audio_output_t *)p_this;
258 (void)oldval; (void)newval; (void)p_data;
260 if ( !strcmp( psz_variable, "audio-device" ) )
262 /* This is supposed to be a significant change and supposes
263 * rebuilding the channel choices. */
264 var_Destroy( p_aout, "audio-channels" );
266 aout_Restart( p_aout );
270 #undef aout_EnableFilter
271 /** Enable or disable an audio filter
272 * \param p_this a vlc object
273 * \param psz_name name of the filter
274 * \param b_add are we adding or removing the filter ?
276 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
279 audio_output_t *p_aout = findAout( p_this );
281 if( aout_ChangeFilterString( p_this, VLC_OBJECT(p_aout), "audio-filter", psz_name, b_add ) )
284 aout_InputRequestRestart( p_aout );
288 vlc_object_release( p_aout );