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