]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
aout_VolumeDown: convert to a macro
[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 #include <vlc_aout_intf.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>                            /* calloc(), malloc(), free() */
37 #include <string.h>
38
39 #include <vlc_aout.h>
40 #include "aout_internal.h"
41
42 #include <vlc_playlist.h>
43
44 static audio_output_t *findAout (vlc_object_t *obj)
45 {
46     input_thread_t *(*pf_find_input) (vlc_object_t *);
47
48     pf_find_input = var_GetAddress (obj, "find-input-callback");
49     if (unlikely(pf_find_input == NULL))
50         return NULL;
51
52     input_thread_t *p_input = pf_find_input (obj);
53     if (p_input == NULL)
54        return NULL;
55
56     audio_output_t *p_aout = input_GetAout (p_input);
57     vlc_object_release (p_input);
58     return p_aout;
59 }
60 #define findAout(o) findAout(VLC_OBJECT(o))
61
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)
65 {
66     audio_output_t *aout = findAout (obj);
67
68     /* FIXME: we need interlocking even if aout does not exist! */
69     *aoutp = aout;
70     if (aout != NULL)
71         aout_lock_volume (aout);
72     if (volp != NULL)
73         *volp = var_GetInteger (obj, "volume");
74     if (mutep != NULL)
75         *mutep = var_GetBool (obj, "mute");
76 }
77
78 /** Commit a volume change transaction. */
79 static int commitVolume (vlc_object_t *obj, audio_output_t *aout,
80                          audio_volume_t volume, bool mute)
81 {
82     int ret = 0;
83
84     var_SetInteger (obj, "volume", volume);
85     var_SetBool (obj, "mute", mute);
86
87     if (aout != NULL)
88     {
89         float vol = volume / (float)AOUT_VOLUME_DEFAULT;
90
91         aout_lock (aout);
92 #warning FIXME: wrong test. Need to check that aout_output is ready.
93         if (aout->p_mixer != NULL)
94             ret = aout->pf_volume_set (aout, vol, mute);
95         aout_unlock (aout);
96
97         if (ret == 0)
98             var_TriggerCallback (aout, "intf-change");
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, audio_output_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     audio_output_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     audio_output_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 value how much to increase (> 0) or decrease (< 0) the volume
154  * \param volp if non-NULL, will contain contain the resulting volume
155  */
156 int aout_VolumeUp (vlc_object_t *obj, int value, audio_volume_t *volp)
157 {
158     audio_output_t *aout;
159     int ret;
160     audio_volume_t volume;
161     bool mute;
162
163     value *= var_InheritInteger (obj, "volume-step");
164
165     prepareVolume (obj, &aout, &volume, &mute);
166     value += volume;
167     if (value < 0)
168         volume = 0;
169     else
170     if (value > AOUT_VOLUME_MAX)
171         volume = AOUT_VOLUME_MAX;
172     else
173         volume = value;
174     ret = commitVolume (obj, aout, volume, mute);
175     if (volp != NULL)
176         *volp = volume;
177     return ret;
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     audio_output_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 ? 0 : 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     audio_output_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     audio_output_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 ? 0 : volume;
229     return ret;
230 }
231
232
233 /*
234  * The next functions are not supposed to be called by the interface, but
235  * are placeholders for software-only scaling.
236  */
237 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
238 {
239     aout->mixer_multiplier = mute ? 0. : volume;
240     return 0;
241 }
242
243 /* Meant to be called by the output plug-in's Open(). */
244 void aout_VolumeSoftInit (audio_output_t *aout)
245 {
246     audio_volume_t volume = var_InheritInteger (aout, "volume");
247     bool mute = var_InheritBool (aout, "mute");
248
249     aout->pf_volume_set = aout_VolumeSoftSet;
250     aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
251 }
252
253
254 /*
255  * The next functions are not supposed to be called by the interface, but
256  * are placeholders for unsupported scaling.
257  */
258 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
259 {
260     (void)aout; (void)volume; (void)mute;
261     return -1;
262 }
263
264 /* Meant to be called by the output plug-in's Open(). */
265 void aout_VolumeNoneInit( audio_output_t * p_aout )
266 {
267     p_aout->pf_volume_set = aout_VolumeNoneSet;
268 }
269
270
271 /*
272  * Pipelines management
273  */
274
275 /*****************************************************************************
276  * aout_Restart : re-open the output device and rebuild the input and output
277  *                pipelines
278  *****************************************************************************
279  * This function is used whenever the parameters of the output plug-in are
280  * changed (eg. selecting S/PDIF or PCM).
281  *****************************************************************************/
282 static int aout_Restart( audio_output_t * p_aout )
283 {
284     aout_input_t *p_input;
285
286     aout_lock( p_aout );
287     p_input = p_aout->p_input;
288     if( p_input == NULL )
289     {
290         aout_unlock( p_aout );
291         msg_Err( p_aout, "no decoder thread" );
292         return -1;
293     }
294
295     /* Reinitializes the output */
296     aout_InputDelete( p_aout, p_input );
297     aout_MixerDelete( p_aout );
298     aout_OutputDelete( p_aout );
299
300     /* FIXME: This function is notoriously dangerous/unsafe.
301      * By the way, if OutputNew or MixerNew fails, we are totally screwed. */
302     if ( aout_OutputNew( p_aout, &p_input->input ) == -1 )
303     {
304         /* Release all locks and report the error. */
305         aout_unlock( p_aout );
306         return -1;
307     }
308
309     if ( aout_MixerNew( p_aout ) == -1 )
310     {
311         aout_OutputDelete( p_aout );
312         aout_unlock( p_aout );
313         return -1;
314     }
315
316     if( aout_InputNew( p_aout, p_input, &p_input->request_vout ) )
317     {
318 #warning FIXME: deal with errors
319         aout_unlock( p_aout );
320         return -1;
321     }
322     aout_unlock( p_aout );
323     return 0;
324 }
325
326 /*****************************************************************************
327  * aout_ChannelsRestart : change the audio device or channels and restart
328  *****************************************************************************/
329 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
330                           vlc_value_t oldval, vlc_value_t newval,
331                           void *p_data )
332 {
333     audio_output_t * p_aout = (audio_output_t *)p_this;
334     (void)oldval; (void)newval; (void)p_data;
335
336     if ( !strcmp( psz_variable, "audio-device" ) )
337     {
338         /* This is supposed to be a significant change and supposes
339          * rebuilding the channel choices. */
340         var_Destroy( p_aout, "audio-channels" );
341     }
342     aout_Restart( p_aout );
343     return 0;
344 }
345
346 #undef aout_EnableFilter
347 /** Enable or disable an audio filter
348  * \param p_this a vlc object
349  * \param psz_name name of the filter
350  * \param b_add are we adding or removing the filter ?
351  */
352 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
353                         bool b_add )
354 {
355     audio_output_t *p_aout = findAout( p_this );
356
357     if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
358     {
359         if( p_aout )
360             AoutInputsMarkToRestart( p_aout );
361     }
362
363     if( p_aout )
364         vlc_object_release( p_aout );
365 }