]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
Really fix audio volume underflow (audio_volume_t is unsigned)
[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 volume, bool mute)
80 {
81     int ret = 0;
82
83     var_SetInteger (obj, "volume", volume);
84     var_SetBool (obj, "mute", mute);
85
86     if (aout != NULL)
87     {
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);
94
95         if (ret == 0)
96             var_TriggerCallback (aout, "intf-change");
97         aout_unlock_volume (aout);
98         vlc_object_release (aout);
99     }
100     return ret;
101 }
102
103 #if 0
104 /** Cancel a volume change transaction. */
105 static void cancelVolume (vlc_object_t *obj, aout_instance_t *aout)
106 {
107     (void) obj;
108     if (aout != NULL)
109     {
110         aout_unlock_volume (aout);
111         vlc_object_release (aout);
112     }
113 }
114 #endif
115
116 #undef aout_VolumeGet
117 /**
118  * Gets the volume of the output device (independent of mute).
119  */
120 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
121 {
122 #if 0
123     aout_instance_t *aout;
124     audio_volume_t volume;
125
126     prepareVolume (obj, &aout, &volume, NULL);
127     cancelVolume (obj, aout);
128     return 0;
129 #else
130     return var_GetInteger (obj, "volume");
131 #endif
132 }
133
134 #undef aout_VolumeSet
135 /**
136  * Sets the volume of the output device.
137  * The mute status is not changed.
138  */
139 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
140 {
141     aout_instance_t *aout;
142     bool mute;
143
144     prepareVolume (obj, &aout, NULL, &mute);
145     return commitVolume (obj, aout, volume, mute);
146 }
147
148 #undef aout_VolumeUp
149 /**
150  * Raises the volume.
151  * \param volp if non-NULL, will contain contain the resulting volume
152  */
153 int aout_VolumeUp (vlc_object_t *obj, int steps, audio_volume_t *volp)
154 {
155     aout_instance_t *aout;
156     int ret;
157     int volume;
158     bool mute;
159
160     steps *= var_InheritInteger (obj, "volume-step");
161
162     prepareVolume (obj, &aout, &volume, &mute);
163     volume += steps;
164     if (volume < AOUT_VOLUME_MIN)
165         volume = AOUT_VOLUME_MIN;
166     if (volume > AOUT_VOLUME_MAX)
167         volume = AOUT_VOLUME_MAX;
168     ret = commitVolume (obj, aout, volume, mute);
169     if (volp != NULL)
170         *volp = volume;
171     return ret;
172 }
173
174 #undef aout_VolumeDown
175 /**
176  * Lowers the volume. See aout_VolumeUp().
177  */
178 int aout_VolumeDown (vlc_object_t *obj, int steps, audio_volume_t *volp)
179 {
180     return aout_VolumeUp (obj, -steps, volp);
181 }
182
183 #undef aout_ToggleMute
184 /**
185  * Toggles the mute state.
186  */
187 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
188 {
189     aout_instance_t *aout;
190     int ret;
191     audio_volume_t volume;
192     bool mute;
193
194     prepareVolume (obj, &aout, &volume, &mute);
195     mute = !mute;
196     ret = commitVolume (obj, aout, volume, mute);
197     if (volp != NULL)
198         *volp = mute ? AOUT_VOLUME_MIN : volume;
199     return ret;
200 }
201
202 /**
203  * Gets the output mute status.
204  */
205 bool aout_IsMuted (vlc_object_t *obj)
206 {
207 #if 0
208     aout_instance_t *aout;
209     bool mute;
210
211     prepareVolume (obj, &aout, NULL, &mute);
212     cancelVolume (obj, aout);
213     return mute;
214 #else
215     return var_GetBool (obj, "mute");
216 #endif
217 }
218
219 /**
220  * Sets mute status.
221  */
222 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
223 {
224     aout_instance_t *aout;
225     int ret;
226     audio_volume_t volume;
227
228     prepareVolume (obj, &aout, &volume, NULL);
229     ret = commitVolume (obj, aout, volume, mute);
230     if (volp != NULL)
231         *volp = mute ? AOUT_VOLUME_MIN : volume;
232     return ret;
233 }
234
235
236 /*
237  * The next functions are not supposed to be called by the interface, but
238  * are placeholders for software-only scaling.
239  */
240 static int aout_VolumeSoftSet (aout_instance_t *aout, audio_volume_t volume,
241                                bool mute)
242 {
243     float f = mute ? 0. : (volume / (float)AOUT_VOLUME_DEFAULT);
244     aout_MixerMultiplierSet (aout, f);
245     return 0;
246 }
247
248 /* Meant to be called by the output plug-in's Open(). */
249 void aout_VolumeSoftInit (aout_instance_t *aout)
250 {
251     audio_volume_t volume = var_InheritInteger (aout, "volume");
252     bool mute = var_InheritBool (aout, "mute");
253
254     aout->output.pf_volume_set = aout_VolumeSoftSet;
255     aout_VolumeSoftSet (aout, volume, mute);
256 }
257
258
259 /*
260  * The next functions are not supposed to be called by the interface, but
261  * are placeholders for unsupported scaling.
262  */
263 static int aout_VolumeNoneSet (aout_instance_t *aout, audio_volume_t volume,
264                                bool mute)
265 {
266     (void)aout; (void)volume; (void)mute;
267     return -1;
268 }
269
270 /* Meant to be called by the output plug-in's Open(). */
271 void aout_VolumeNoneInit( aout_instance_t * p_aout )
272 {
273     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
274 }
275
276
277 /*
278  * Pipelines management
279  */
280
281 /*****************************************************************************
282  * aout_Restart : re-open the output device and rebuild the input and output
283  *                pipelines
284  *****************************************************************************
285  * This function is used whenever the parameters of the output plug-in are
286  * changed (eg. selecting S/PDIF or PCM).
287  *****************************************************************************/
288 static int aout_Restart( aout_instance_t * p_aout )
289 {
290     int i;
291     bool b_error = 0;
292
293     aout_lock_mixer( p_aout );
294
295     if ( p_aout->i_nb_inputs == 0 )
296     {
297         aout_unlock_mixer( p_aout );
298         msg_Err( p_aout, "no decoder thread" );
299         return -1;
300     }
301
302     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
303     {
304         aout_lock_input( p_aout, p_aout->pp_inputs[i] );
305         aout_lock_input_fifos( p_aout );
306         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
307         aout_unlock_input_fifos( p_aout );
308     }
309
310     /* Lock all inputs. */
311     aout_lock_input_fifos( p_aout );
312     aout_MixerDelete( p_aout );
313
314     /* Re-open the output plug-in. */
315     aout_OutputDelete( p_aout );
316
317     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
318     {
319         /* Release all locks and report the error. */
320         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
321         {
322             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
323         }
324         aout_unlock_input_fifos( p_aout );
325         aout_unlock_mixer( p_aout );
326         return -1;
327     }
328
329     if ( aout_MixerNew( p_aout ) == -1 )
330     {
331         aout_OutputDelete( p_aout );
332         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
333         {
334             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
335         }
336         aout_unlock_input_fifos( p_aout );
337         aout_unlock_mixer( p_aout );
338         return -1;
339     }
340
341     /* Re-open all inputs. */
342     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
343     {
344         aout_input_t * p_input = p_aout->pp_inputs[i];
345         b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
346         p_input->b_changed = 1;
347         aout_unlock_input( p_aout, p_input );
348     }
349
350     aout_unlock_input_fifos( p_aout );
351     aout_unlock_mixer( p_aout );
352
353     return b_error;
354 }
355
356 /*****************************************************************************
357  * aout_ChannelsRestart : change the audio device or channels and restart
358  *****************************************************************************/
359 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
360                           vlc_value_t oldval, vlc_value_t newval,
361                           void *p_data )
362 {
363     aout_instance_t * p_aout = (aout_instance_t *)p_this;
364     (void)oldval; (void)newval; (void)p_data;
365
366     if ( !strcmp( psz_variable, "audio-device" ) )
367     {
368         /* This is supposed to be a significant change and supposes
369          * rebuilding the channel choices. */
370         var_Destroy( p_aout, "audio-channels" );
371     }
372     aout_Restart( p_aout );
373     return 0;
374 }
375
376 #undef aout_EnableFilter
377 /** Enable or disable an audio filter
378  * \param p_this a vlc object
379  * \param psz_name name of the filter
380  * \param b_add are we adding or removing the filter ?
381  */
382 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
383                         bool b_add )
384 {
385     aout_instance_t *p_aout = findAout( p_this );
386
387     if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
388     {
389         if( p_aout )
390             AoutInputsMarkToRestart( p_aout );
391     }
392
393     if( p_aout )
394         vlc_object_release( p_aout );
395 }