]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
Enforce volume min and max (small regression from previous commits)
[vlc] / src / audio_output / intf.c
1 /*****************************************************************************
2  * intf.c : audio output API towards the interface modules
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>                            /* calloc(), malloc(), free() */
36 #include <string.h>
37
38 #include <vlc_aout.h>
39 #include "aout_internal.h"
40
41 #include <vlc_playlist.h>
42
43 static aout_instance_t *findAout (vlc_object_t *obj)
44 {
45     input_thread_t *(*pf_find_input) (vlc_object_t *);
46
47     pf_find_input = var_GetAddress (obj, "find-input-callback");
48     if (unlikely(pf_find_input == NULL))
49         return NULL;
50
51     input_thread_t *p_input = pf_find_input (obj);
52     if (p_input == NULL)
53        return NULL;
54
55     aout_instance_t *p_aout = input_GetAout (p_input);
56     vlc_object_release (p_input);
57     return p_aout;
58 }
59 #define findAout(o) findAout(VLC_OBJECT(o))
60
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)
64 {
65     aout_instance_t *aout = findAout (obj);
66
67     /* FIXME: we need interlocking even if aout does not exist! */
68     *aoutp = aout;
69     if (aout != NULL)
70         aout_lock_volume (aout);
71     if (volp != NULL)
72         *volp = var_GetInteger (obj, "volume");
73     if (mutep != NULL)
74         *mutep = var_GetBool (obj, "mute");
75 }
76
77 /** Commit a volume change transaction. */
78 static int commitVolume (vlc_object_t *obj, aout_instance_t *aout,
79                          audio_volume_t *volp, bool mute)
80 {
81     int ret = 0;
82
83     audio_volume_t volume = *volp;
84     if (volume < AOUT_VOLUME_MIN)
85         volume = AOUT_VOLUME_MIN;
86     if (volume > AOUT_VOLUME_MAX)
87         volume = AOUT_VOLUME_MAX;
88     *volp = volume;
89
90     var_SetInteger (obj, "volume", volume);
91     var_SetBool (obj, "mute", mute);
92
93     if (aout != NULL)
94     {
95         aout_lock_mixer (aout);
96         aout_lock_input_fifos (aout);
97         if (aout->p_mixer != NULL)
98             ret = aout->output.pf_volume_set (aout, volume, mute);
99         aout_unlock_input_fifos (aout);
100         aout_unlock_mixer (aout);
101
102         if (ret == 0)
103             var_TriggerCallback (aout, "intf-change");
104         aout_unlock_volume (aout);
105         vlc_object_release (aout);
106     }
107     return ret;
108 }
109
110 #if 0
111 /** Cancel a volume change transaction. */
112 static void cancelVolume (vlc_object_t *obj, aout_instance_t *aout)
113 {
114     (void) obj;
115     if (aout != NULL)
116     {
117         aout_unlock_volume (aout);
118         vlc_object_release (aout);
119     }
120 }
121 #endif
122
123 #undef aout_VolumeGet
124 /**
125  * Gets the volume of the output device (independent of mute).
126  */
127 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
128 {
129 #if 0
130     aout_instance_t *aout;
131     audio_volume_t volume;
132
133     prepareVolume (obj, &aout, &volume, NULL);
134     cancelVolume (obj, aout);
135     return 0;
136 #else
137     return var_GetInteger (obj, "volume");
138 #endif
139 }
140
141 #undef aout_VolumeSet
142 /**
143  * Sets the volume of the output device.
144  * The mute status is not changed.
145  */
146 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
147 {
148     aout_instance_t *aout;
149     bool mute;
150
151     prepareVolume (obj, &aout, NULL, &mute);
152     return commitVolume (obj, aout, &volume, mute);
153 }
154
155 #undef aout_VolumeUp
156 /**
157  * Raises the volume.
158  * \param volp if non-NULL, will contain contain the resulting volume
159  */
160 int aout_VolumeUp (vlc_object_t *obj, int steps, audio_volume_t *volp)
161 {
162     aout_instance_t *aout;
163     int ret;
164     int stepsize = var_InheritInteger (obj, "volume-step");
165     audio_volume_t volume;
166     bool mute;
167
168     prepareVolume (obj, &aout, &volume, &mute);
169     volume += stepsize * steps;
170     ret = commitVolume (obj, aout, &volume, mute);
171     if (volp != NULL)
172         *volp = volume;
173     return ret;
174 }
175
176 #undef aout_VolumeDown
177 /**
178  * Lowers the volume. See aout_VolumeUp().
179  */
180 int aout_VolumeDown (vlc_object_t *obj, int steps, audio_volume_t *volp)
181 {
182     return aout_VolumeUp (obj, -steps, volp);
183 }
184
185 #undef aout_ToggleMute
186 /**
187  * Toggles the mute state.
188  */
189 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
190 {
191     aout_instance_t *aout;
192     int ret;
193     audio_volume_t volume;
194     bool mute;
195
196     prepareVolume (obj, &aout, &volume, &mute);
197     mute = !mute;
198     ret = commitVolume (obj, aout, &volume, mute);
199     if (volp != NULL)
200         *volp = mute ? AOUT_VOLUME_MIN : volume;
201     return ret;
202 }
203
204 /**
205  * Gets the output mute status.
206  */
207 bool aout_IsMuted (vlc_object_t *obj)
208 {
209 #if 0
210     aout_instance_t *aout;
211     bool mute;
212
213     prepareVolume (obj, &aout, NULL, &mute);
214     cancelVolume (obj, aout);
215     return mute;
216 #else
217     return var_GetBool (obj, "mute");
218 #endif
219 }
220
221 /**
222  * Sets mute status.
223  */
224 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
225 {
226     aout_instance_t *aout;
227     int ret;
228     audio_volume_t volume;
229
230     prepareVolume (obj, &aout, &volume, NULL);
231     ret = commitVolume (obj, aout, &volume, mute);
232     if (volp != NULL)
233         *volp = mute ? AOUT_VOLUME_MIN : volume;
234     return ret;
235 }
236
237
238 /*
239  * The next functions are not supposed to be called by the interface, but
240  * are placeholders for software-only scaling.
241  */
242 static int aout_VolumeSoftSet (aout_instance_t *aout, audio_volume_t volume,
243                                bool mute)
244 {
245     float f = mute ? 0. : (volume / (float)AOUT_VOLUME_DEFAULT);
246     aout_MixerMultiplierSet (aout, f);
247     aout->output.i_volume = volume;
248     return 0;
249 }
250
251 /* Meant to be called by the output plug-in's Open(). */
252 void aout_VolumeSoftInit (aout_instance_t *aout)
253 {
254     audio_volume_t volume = var_InheritInteger (aout, "volume");
255     bool mute = var_InheritBool (aout, "mute");
256
257     aout->output.pf_volume_set = aout_VolumeSoftSet;
258     aout_VolumeSoftSet (aout, volume, mute);
259 }
260
261
262 /*
263  * The next functions are not supposed to be called by the interface, but
264  * are placeholders for unsupported scaling.
265  */
266 static int aout_VolumeNoneSet (aout_instance_t *aout, audio_volume_t volume,
267                                bool mute)
268 {
269     (void)aout; (void)volume; (void)mute;
270     return -1;
271 }
272
273 /* Meant to be called by the output plug-in's Open(). */
274 void aout_VolumeNoneInit( aout_instance_t * p_aout )
275 {
276     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
277 }
278
279
280 /*
281  * Pipelines management
282  */
283
284 /*****************************************************************************
285  * aout_Restart : re-open the output device and rebuild the input and output
286  *                pipelines
287  *****************************************************************************
288  * This function is used whenever the parameters of the output plug-in are
289  * changed (eg. selecting S/PDIF or PCM).
290  *****************************************************************************/
291 static int aout_Restart( aout_instance_t * p_aout )
292 {
293     int i;
294     bool b_error = 0;
295
296     aout_lock_mixer( p_aout );
297
298     if ( p_aout->i_nb_inputs == 0 )
299     {
300         aout_unlock_mixer( p_aout );
301         msg_Err( p_aout, "no decoder thread" );
302         return -1;
303     }
304
305     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
306     {
307         aout_lock_input( p_aout, p_aout->pp_inputs[i] );
308         aout_lock_input_fifos( p_aout );
309         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
310         aout_unlock_input_fifos( p_aout );
311     }
312
313     /* Lock all inputs. */
314     aout_lock_input_fifos( p_aout );
315     aout_MixerDelete( p_aout );
316
317     /* Re-open the output plug-in. */
318     aout_OutputDelete( p_aout );
319
320     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
321     {
322         /* Release all locks and report the error. */
323         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
324         {
325             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
326         }
327         aout_unlock_input_fifos( p_aout );
328         aout_unlock_mixer( p_aout );
329         return -1;
330     }
331
332     if ( aout_MixerNew( p_aout ) == -1 )
333     {
334         aout_OutputDelete( p_aout );
335         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
336         {
337             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
338         }
339         aout_unlock_input_fifos( p_aout );
340         aout_unlock_mixer( p_aout );
341         return -1;
342     }
343
344     /* Re-open all inputs. */
345     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
346     {
347         aout_input_t * p_input = p_aout->pp_inputs[i];
348         b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
349         p_input->b_changed = 1;
350         aout_unlock_input( p_aout, p_input );
351     }
352
353     aout_unlock_input_fifos( p_aout );
354     aout_unlock_mixer( p_aout );
355
356     return b_error;
357 }
358
359 /*****************************************************************************
360  * aout_ChannelsRestart : change the audio device or channels and restart
361  *****************************************************************************/
362 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
363                           vlc_value_t oldval, vlc_value_t newval,
364                           void *p_data )
365 {
366     aout_instance_t * p_aout = (aout_instance_t *)p_this;
367     (void)oldval; (void)newval; (void)p_data;
368
369     if ( !strcmp( psz_variable, "audio-device" ) )
370     {
371         /* This is supposed to be a significant change and supposes
372          * rebuilding the channel choices. */
373         var_Destroy( p_aout, "audio-channels" );
374     }
375     aout_Restart( p_aout );
376     return 0;
377 }
378
379 #undef aout_EnableFilter
380 /** Enable or disable an audio filter
381  * \param p_this a vlc object
382  * \param psz_name name of the filter
383  * \param b_add are we adding or removing the filter ?
384  */
385 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
386                         bool b_add )
387 {
388     aout_instance_t *p_aout = findAout( p_this );
389
390     if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
391     {
392         if( p_aout )
393             AoutInputsMarkToRestart( p_aout );
394     }
395
396     if( p_aout )
397         vlc_object_release( p_aout );
398 }