]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
aout: privatize some attributes
[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         aout_owner_t *owner = aout_owner (aout);
90         float vol = volume / (float)AOUT_VOLUME_DEFAULT;
91
92         aout_lock (aout);
93 #warning FIXME: wrong test. Need to check that aout_output is ready.
94         if (owner->volume.mixer != NULL)
95             ret = aout->pf_volume_set (aout, vol, mute);
96         aout_unlock (aout);
97
98         if (ret == 0)
99             var_TriggerCallback (aout, "intf-change");
100         aout_unlock_volume (aout);
101         vlc_object_release (aout);
102     }
103     return ret;
104 }
105
106 #if 0
107 /** Cancel a volume change transaction. */
108 static void cancelVolume (vlc_object_t *obj, audio_output_t *aout)
109 {
110     (void) obj;
111     if (aout != NULL)
112     {
113         aout_unlock_volume (aout);
114         vlc_object_release (aout);
115     }
116 }
117 #endif
118
119 #undef aout_VolumeGet
120 /**
121  * Gets the volume of the output device (independent of mute).
122  */
123 audio_volume_t aout_VolumeGet (vlc_object_t *obj)
124 {
125 #if 0
126     audio_output_t *aout;
127     audio_volume_t volume;
128
129     prepareVolume (obj, &aout, &volume, NULL);
130     cancelVolume (obj, aout);
131     return 0;
132 #else
133     return var_GetInteger (obj, "volume");
134 #endif
135 }
136
137 #undef aout_VolumeSet
138 /**
139  * Sets the volume of the output device.
140  * The mute status is not changed.
141  */
142 int aout_VolumeSet (vlc_object_t *obj, audio_volume_t volume)
143 {
144     audio_output_t *aout;
145     bool mute;
146
147     prepareVolume (obj, &aout, NULL, &mute);
148     return commitVolume (obj, aout, volume, mute);
149 }
150
151 #undef aout_VolumeUp
152 /**
153  * Raises the volume.
154  * \param value how much to increase (> 0) or decrease (< 0) the volume
155  * \param volp if non-NULL, will contain contain the resulting volume
156  */
157 int aout_VolumeUp (vlc_object_t *obj, int value, audio_volume_t *volp)
158 {
159     audio_output_t *aout;
160     int ret;
161     audio_volume_t volume;
162     bool mute;
163
164     value *= var_InheritInteger (obj, "volume-step");
165
166     prepareVolume (obj, &aout, &volume, &mute);
167     value += volume;
168     if (value < 0)
169         volume = 0;
170     else
171     if (value > AOUT_VOLUME_MAX)
172         volume = AOUT_VOLUME_MAX;
173     else
174         volume = value;
175     ret = commitVolume (obj, aout, volume, mute);
176     if (volp != NULL)
177         *volp = volume;
178     return ret;
179 }
180
181 #undef aout_ToggleMute
182 /**
183  * Toggles the mute state.
184  */
185 int aout_ToggleMute (vlc_object_t *obj, audio_volume_t *volp)
186 {
187     audio_output_t *aout;
188     int ret;
189     audio_volume_t volume;
190     bool mute;
191
192     prepareVolume (obj, &aout, &volume, &mute);
193     mute = !mute;
194     ret = commitVolume (obj, aout, volume, mute);
195     if (volp != NULL)
196         *volp = mute ? 0 : volume;
197     return ret;
198 }
199
200 /**
201  * Gets the output mute status.
202  */
203 bool aout_IsMuted (vlc_object_t *obj)
204 {
205 #if 0
206     audio_output_t *aout;
207     bool mute;
208
209     prepareVolume (obj, &aout, NULL, &mute);
210     cancelVolume (obj, aout);
211     return mute;
212 #else
213     return var_GetBool (obj, "mute");
214 #endif
215 }
216
217 /**
218  * Sets mute status.
219  */
220 int aout_SetMute (vlc_object_t *obj, audio_volume_t *volp, bool mute)
221 {
222     audio_output_t *aout;
223     int ret;
224     audio_volume_t volume;
225
226     prepareVolume (obj, &aout, &volume, NULL);
227     ret = commitVolume (obj, aout, volume, mute);
228     if (volp != NULL)
229         *volp = mute ? 0 : volume;
230     return ret;
231 }
232
233
234 /*
235  * Pipelines management
236  */
237
238 /*****************************************************************************
239  * aout_Restart : re-open the output device and rebuild the input and output
240  *                pipelines
241  *****************************************************************************
242  * This function is used whenever the parameters of the output plug-in are
243  * changed (eg. selecting S/PDIF or PCM).
244  *****************************************************************************/
245 static int aout_Restart( audio_output_t * p_aout )
246 {
247     aout_input_t *p_input;
248     aout_owner_t *owner = aout_owner (p_aout);
249
250     aout_lock( p_aout );
251     p_input = owner->input;
252     if( p_input == NULL )
253     {
254         aout_unlock( p_aout );
255         msg_Err( p_aout, "no decoder thread" );
256         return -1;
257     }
258
259     /* Reinitializes the output */
260     aout_InputDelete( p_aout, p_input );
261     aout_MixerDelete (owner->volume.mixer);
262     owner->volume.mixer = NULL;
263     aout_OutputDelete( p_aout );
264
265     /* FIXME: This function is notoriously dangerous/unsafe.
266      * By the way, if OutputNew or MixerNew fails, we are totally screwed. */
267     if ( aout_OutputNew( p_aout, &p_input->input ) == -1 )
268     {
269         /* Release all locks and report the error. */
270         aout_unlock( p_aout );
271         return -1;
272     }
273
274     owner->volume.mixer = aout_MixerNew (p_aout, &owner->mixer_format);
275     if (owner->volume.mixer == NULL)
276     {
277         aout_OutputDelete( p_aout );
278         aout_unlock( p_aout );
279         return -1;
280     }
281
282     if( aout_InputNew( p_aout, p_input, &p_input->request_vout ) )
283     {
284 #warning FIXME: deal with errors
285         aout_unlock( p_aout );
286         return -1;
287     }
288     aout_unlock( p_aout );
289     return 0;
290 }
291
292 /*****************************************************************************
293  * aout_ChannelsRestart : change the audio device or channels and restart
294  *****************************************************************************/
295 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
296                           vlc_value_t oldval, vlc_value_t newval,
297                           void *p_data )
298 {
299     audio_output_t * p_aout = (audio_output_t *)p_this;
300     (void)oldval; (void)newval; (void)p_data;
301
302     if ( !strcmp( psz_variable, "audio-device" ) )
303     {
304         /* This is supposed to be a significant change and supposes
305          * rebuilding the channel choices. */
306         var_Destroy( p_aout, "audio-channels" );
307     }
308     aout_Restart( p_aout );
309     return 0;
310 }
311
312 #undef aout_EnableFilter
313 /** Enable or disable an audio filter
314  * \param p_this a vlc object
315  * \param psz_name name of the filter
316  * \param b_add are we adding or removing the filter ?
317  */
318 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
319                         bool b_add )
320 {
321     audio_output_t *p_aout = findAout( p_this );
322
323     if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
324     {
325         if( p_aout )
326             AoutInputsMarkToRestart( p_aout );
327     }
328
329     if( p_aout )
330         vlc_object_release( p_aout );
331 }