]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
Move <vlc_aout.h> UI functions into their own <vlc_aout_intf.h>
[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 aout_instance_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     aout_instance_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, aout_instance_t **aoutp,
64                            audio_volume_t *volp, bool *mutep)
65 {
66     aout_instance_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, aout_instance_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         aout_lock (aout);
90 #warning FIXME: wrong test. Need to check that aout_output is ready.
91         if (aout->p_mixer != NULL)
92             ret = aout->output.pf_volume_set (aout, volume, mute);
93         aout_unlock (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 value how much to increase (> 0) or decrease (< 0) the volume
152  * \param volp if non-NULL, will contain contain the resulting volume
153  */
154 int aout_VolumeUp (vlc_object_t *obj, int value, audio_volume_t *volp)
155 {
156     aout_instance_t *aout;
157     int ret;
158     audio_volume_t volume;
159     bool mute;
160
161     value *= var_InheritInteger (obj, "volume-step");
162
163     prepareVolume (obj, &aout, &volume, &mute);
164     value += volume;
165     if (value < AOUT_VOLUME_MIN)
166         volume = AOUT_VOLUME_MIN;
167     else
168     if (value > AOUT_VOLUME_MAX)
169         volume = AOUT_VOLUME_MAX;
170     else
171         volume = value;
172     ret = commitVolume (obj, aout, volume, mute);
173     if (volp != NULL)
174         *volp = volume;
175     return ret;
176 }
177
178 #undef aout_VolumeDown
179 /**
180  * Lowers the volume. See aout_VolumeUp().
181  */
182 int aout_VolumeDown (vlc_object_t *obj, int steps, audio_volume_t *volp)
183 {
184     return aout_VolumeUp (obj, -steps, volp);
185 }
186
187 #undef aout_ToggleMute
188 /**
189  * Toggles the mute state.
190  */
191 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
192 {
193     aout_instance_t *aout;
194     int ret;
195     audio_volume_t volume;
196     bool mute;
197
198     prepareVolume (obj, &aout, &volume, &mute);
199     mute = !mute;
200     ret = commitVolume (obj, aout, volume, mute);
201     if (volp != NULL)
202         *volp = mute ? AOUT_VOLUME_MIN : volume;
203     return ret;
204 }
205
206 /**
207  * Gets the output mute status.
208  */
209 bool aout_IsMuted (vlc_object_t *obj)
210 {
211 #if 0
212     aout_instance_t *aout;
213     bool mute;
214
215     prepareVolume (obj, &aout, NULL, &mute);
216     cancelVolume (obj, aout);
217     return mute;
218 #else
219     return var_GetBool (obj, "mute");
220 #endif
221 }
222
223 /**
224  * Sets mute status.
225  */
226 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
227 {
228     aout_instance_t *aout;
229     int ret;
230     audio_volume_t volume;
231
232     prepareVolume (obj, &aout, &volume, NULL);
233     ret = commitVolume (obj, aout, volume, mute);
234     if (volp != NULL)
235         *volp = mute ? AOUT_VOLUME_MIN : volume;
236     return ret;
237 }
238
239
240 /*
241  * The next functions are not supposed to be called by the interface, but
242  * are placeholders for software-only scaling.
243  */
244 static int aout_VolumeSoftSet (aout_instance_t *aout, audio_volume_t volume,
245                                bool mute)
246 {
247     float f = mute ? 0. : (volume / (float)AOUT_VOLUME_DEFAULT);
248     aout->mixer_multiplier = f;
249     return 0;
250 }
251
252 /* Meant to be called by the output plug-in's Open(). */
253 void aout_VolumeSoftInit (aout_instance_t *aout)
254 {
255     audio_volume_t volume = var_InheritInteger (aout, "volume");
256     bool mute = var_InheritBool (aout, "mute");
257
258     aout->output.pf_volume_set = aout_VolumeSoftSet;
259     aout_VolumeSoftSet (aout, volume, mute);
260 }
261
262
263 /*
264  * The next functions are not supposed to be called by the interface, but
265  * are placeholders for unsupported scaling.
266  */
267 static int aout_VolumeNoneSet (aout_instance_t *aout, audio_volume_t volume,
268                                bool mute)
269 {
270     (void)aout; (void)volume; (void)mute;
271     return -1;
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
281 /*
282  * Pipelines management
283  */
284
285 /*****************************************************************************
286  * aout_Restart : re-open the output device and rebuild the input and output
287  *                pipelines
288  *****************************************************************************
289  * This function is used whenever the parameters of the output plug-in are
290  * changed (eg. selecting S/PDIF or PCM).
291  *****************************************************************************/
292 static int aout_Restart( aout_instance_t * p_aout )
293 {
294     aout_input_t *p_input;
295
296     aout_lock( p_aout );
297     p_input = p_aout->p_input;
298     if( p_input == NULL )
299     {
300         aout_unlock( p_aout );
301         msg_Err( p_aout, "no decoder thread" );
302         return -1;
303     }
304
305     /* Reinitializes the output */
306     aout_InputDelete( p_aout, p_input );
307     aout_MixerDelete( p_aout );
308     aout_OutputDelete( p_aout );
309
310     /* FIXME: This function is notoriously dangerous/unsafe.
311      * By the way, if OutputNew or MixerNew fails, we are totally screwed. */
312     if ( aout_OutputNew( p_aout, &p_input->input ) == -1 )
313     {
314         /* Release all locks and report the error. */
315         aout_unlock( p_aout );
316         return -1;
317     }
318
319     if ( aout_MixerNew( p_aout ) == -1 )
320     {
321         aout_OutputDelete( p_aout );
322         aout_unlock( p_aout );
323         return -1;
324     }
325
326     if( aout_InputNew( p_aout, p_input, &p_input->request_vout ) )
327     {
328 #warning FIXME: deal with errors
329         aout_unlock( p_aout );
330         return -1;
331     }
332     aout_unlock( p_aout );
333     return 0;
334 }
335
336 /*****************************************************************************
337  * aout_ChannelsRestart : change the audio device or channels and restart
338  *****************************************************************************/
339 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
340                           vlc_value_t oldval, vlc_value_t newval,
341                           void *p_data )
342 {
343     aout_instance_t * p_aout = (aout_instance_t *)p_this;
344     (void)oldval; (void)newval; (void)p_data;
345
346     if ( !strcmp( psz_variable, "audio-device" ) )
347     {
348         /* This is supposed to be a significant change and supposes
349          * rebuilding the channel choices. */
350         var_Destroy( p_aout, "audio-channels" );
351     }
352     aout_Restart( p_aout );
353     return 0;
354 }
355
356 #undef aout_EnableFilter
357 /** Enable or disable an audio filter
358  * \param p_this a vlc object
359  * \param psz_name name of the filter
360  * \param b_add are we adding or removing the filter ?
361  */
362 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
363                         bool b_add )
364 {
365     aout_instance_t *p_aout = findAout( p_this );
366
367     if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
368     {
369         if( p_aout )
370             AoutInputsMarkToRestart( p_aout );
371     }
372
373     if( p_aout )
374         vlc_object_release( p_aout );
375 }