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