]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
Remove impossible error
[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 enum {
62     SET_MUTE=1,
63     SET_VOLUME=2,
64     INCREMENT_VOLUME=4,
65     TOGGLE_MUTE=8
66 };
67
68 /*****************************************************************************
69  * doVolumeChanges : handle all volume changes. Internal use only to ease
70  *                   variables locking.
71  *****************************************************************************/
72 static
73 int doVolumeChanges( unsigned action, vlc_object_t * p_object, int i_nb_steps,
74                 audio_volume_t i_volume, audio_volume_t * i_return_volume,
75                 bool b_mute )
76 {
77     int i_result = VLC_SUCCESS;
78     int i_volume_step = 1, i_new_volume = 0;
79     bool b_var_mute = false;
80     aout_instance_t *p_aout = findAout( p_object );
81
82     if ( p_aout ) aout_lock_volume( p_aout );
83
84     b_var_mute = var_GetBool( p_object, "volume-muted");
85
86     const bool b_unmute_condition = ( b_var_mute
87                 && ( /* Unmute: on increments */
88                     ( action == INCREMENT_VOLUME )
89                     || /* On explicit unmute */
90                     ( ( action == SET_MUTE ) && !b_mute )
91                     || /* On toggle from muted */
92                     ( action == TOGGLE_MUTE )
93                 ));
94
95     const bool b_mute_condition = ( !b_var_mute
96                     && ( /* explicit */
97                         ( ( action == SET_MUTE ) && b_mute )
98                         || /* or toggle */
99                         ( action == TOGGLE_MUTE )
100                     ));
101
102     /* If muting or unmuting when play hasn't started */
103     if ( action == SET_MUTE && !b_unmute_condition && !b_mute_condition )
104     {
105         if ( p_aout )
106         {
107             aout_unlock_volume( p_aout );
108             vlc_object_release( p_aout );
109         }
110         return i_result;
111     }
112
113     /* On UnMute */
114     if ( b_unmute_condition )
115     {
116         /* Restore saved volume */
117         i_volume = var_GetInteger( p_object, "saved-volume" );
118         var_SetBool( p_object, "volume-muted", false );
119     }
120     else if ( b_mute_condition )
121     {
122         /* We need an initial value to backup later */
123         i_volume = config_GetInt( p_object, "volume" );
124     }
125
126     if ( action == INCREMENT_VOLUME )
127     {
128         i_volume_step = var_InheritInteger( p_object, "volume-step" );
129
130         if ( !b_unmute_condition )
131             i_volume = config_GetInt( p_object, "volume" );
132
133         i_new_volume = (int) i_volume + i_volume_step * i_nb_steps;
134
135         if ( i_new_volume > AOUT_VOLUME_MAX )
136             i_volume = AOUT_VOLUME_MAX;
137         else if ( i_new_volume < AOUT_VOLUME_MIN )
138             i_volume = AOUT_VOLUME_MIN;
139         else
140             i_volume = i_new_volume;
141     }
142
143     var_SetInteger( p_object, "saved-volume" , i_volume );
144
145     /* On Mute */
146     if ( b_mute_condition )
147     {
148         i_volume = AOUT_VOLUME_MIN;
149         var_SetBool( p_object, "volume-muted", true );
150     }
151
152     /* Commit volume changes */
153     config_PutInt( p_object, "volume", i_volume );
154
155     if ( p_aout )
156     {
157         aout_lock_mixer( p_aout );
158         aout_lock_input_fifos( p_aout );
159             if ( p_aout->p_mixer )
160                 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
161         aout_unlock_input_fifos( p_aout );
162         aout_unlock_mixer( p_aout );
163     }
164
165     /* trigger callbacks */
166     var_TriggerCallback( p_object, "volume-change" );
167     if ( p_aout )
168     {
169         var_SetBool( p_aout, "intf-change", true );
170         aout_unlock_volume( p_aout );
171         vlc_object_release( p_aout );
172     }
173
174     if ( i_return_volume != NULL )
175          *i_return_volume = i_volume;
176     return i_result;
177 }
178
179 #undef aout_VolumeGet
180 /*****************************************************************************
181  * aout_VolumeGet : get the volume of the output device
182  *****************************************************************************/
183 int aout_VolumeGet( vlc_object_t * p_object, audio_volume_t * pi_volume )
184 {
185     int i_result = 0;
186     aout_instance_t * p_aout = findAout( p_object );
187
188     if ( p_aout == NULL )
189     {
190         *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
191         return 0;
192     }
193
194     aout_lock_volume( p_aout );
195     aout_lock_mixer( p_aout );
196     if ( p_aout->p_mixer )
197     {
198         i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
199     }
200     else
201     {
202         *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
203     }
204     aout_unlock_mixer( p_aout );
205     aout_unlock_volume( p_aout );
206
207     vlc_object_release( p_aout );
208     return i_result;
209 }
210
211 #undef aout_VolumeSet
212 /*****************************************************************************
213  * aout_VolumeSet : set the volume of the output device
214  *****************************************************************************/
215 int aout_VolumeSet( vlc_object_t * p_object, audio_volume_t i_volume )
216 {
217     return doVolumeChanges( SET_VOLUME, p_object, 1, i_volume, NULL, true );
218 }
219
220 #undef aout_VolumeUp
221 /*****************************************************************************
222  * aout_VolumeUp : raise the output volume
223  *****************************************************************************
224  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
225  * function.
226  *****************************************************************************/
227 int aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
228                    audio_volume_t * pi_volume )
229 {
230     return doVolumeChanges( INCREMENT_VOLUME, p_object, i_nb_steps, 0, pi_volume, true );
231 }
232
233 #undef aout_VolumeDown
234 /*****************************************************************************
235  * aout_VolumeDown : lower the output volume
236  *****************************************************************************
237  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
238  * function.
239  *****************************************************************************/
240 int aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
241                      audio_volume_t * pi_volume )
242 {
243     return aout_VolumeUp( p_object, -i_nb_steps, pi_volume );
244 }
245
246 #undef aout_ToggleMute
247 /*****************************************************************************
248  * aout_ToggleMute : Mute/un-mute the output volume
249  *****************************************************************************
250  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
251  * function (muted => 0).
252  *****************************************************************************/
253 int aout_ToggleMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
254 {
255     return doVolumeChanges( TOGGLE_MUTE, p_object, 1, 0, pi_volume, true );
256 }
257
258 /*****************************************************************************
259  * aout_IsMuted : Get the output volume mute status
260  *****************************************************************************/
261 bool aout_IsMuted( vlc_object_t * p_object )
262 {
263     bool b_return_val;
264     aout_instance_t * p_aout = findAout( p_object );
265     if ( p_aout ) aout_lock_volume( p_aout );
266     b_return_val = var_GetBool( p_object, "volume-muted");
267     if ( p_aout )
268     {
269         aout_unlock_volume( p_aout );
270         vlc_object_release( p_aout );
271     }
272     return b_return_val;
273 }
274
275 /*****************************************************************************
276  * aout_SetMute : Sets mute status
277  *****************************************************************************
278  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
279  * function (muted => 0).
280  *****************************************************************************/
281 int aout_SetMute( vlc_object_t * p_object, audio_volume_t * pi_volume,
282                   bool b_mute )
283 {
284     return doVolumeChanges( SET_MUTE, p_object, 1, 0, pi_volume, b_mute );
285 }
286
287 /*
288  * The next functions are not supposed to be called by the interface, but
289  * are placeholders for software-only scaling.
290  */
291
292 /* Meant to be called by the output plug-in's Open(). */
293 void aout_VolumeSoftInit( aout_instance_t * p_aout )
294 {
295     int i_volume;
296
297     p_aout->output.pf_volume_get = aout_VolumeSoftGet;
298     p_aout->output.pf_volume_set = aout_VolumeSoftSet;
299
300     i_volume = config_GetInt( p_aout, "volume" );
301     if ( i_volume < AOUT_VOLUME_MIN )
302     {
303         i_volume = AOUT_VOLUME_DEFAULT;
304     }
305     else if ( i_volume > AOUT_VOLUME_MAX )
306     {
307         i_volume = AOUT_VOLUME_MAX;
308     }
309
310     aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
311 }
312
313 /* Placeholder for pf_volume_get(). */
314 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
315 {
316     *pi_volume = p_aout->output.i_volume;
317     return 0;
318 }
319
320
321 /* Placeholder for pf_volume_set(). */
322 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
323 {
324     aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
325     p_aout->output.i_volume = i_volume;
326     return 0;
327 }
328
329 /*
330  * The next functions are not supposed to be called by the interface, but
331  * are placeholders for unsupported scaling.
332  */
333
334 /* Meant to be called by the output plug-in's Open(). */
335 void aout_VolumeNoneInit( aout_instance_t * p_aout )
336 {
337     p_aout->output.pf_volume_get = aout_VolumeNoneGet;
338     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
339 }
340
341 /* Placeholder for pf_volume_get(). */
342 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
343 {
344     (void)p_aout; (void)pi_volume;
345     return -1;
346 }
347
348 /* Placeholder for pf_volume_set(). */
349 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
350 {
351     (void)p_aout; (void)i_volume;
352     return -1;
353 }
354
355
356 /*
357  * Pipelines management
358  */
359
360 /*****************************************************************************
361  * aout_Restart : re-open the output device and rebuild the input and output
362  *                pipelines
363  *****************************************************************************
364  * This function is used whenever the parameters of the output plug-in are
365  * changed (eg. selecting S/PDIF or PCM).
366  *****************************************************************************/
367 static int aout_Restart( aout_instance_t * p_aout )
368 {
369     int i;
370     bool b_error = 0;
371
372     aout_lock_mixer( p_aout );
373
374     if ( p_aout->i_nb_inputs == 0 )
375     {
376         aout_unlock_mixer( p_aout );
377         msg_Err( p_aout, "no decoder thread" );
378         return -1;
379     }
380
381     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
382     {
383         aout_lock_input( p_aout, p_aout->pp_inputs[i] );
384         aout_lock_input_fifos( p_aout );
385         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
386         aout_unlock_input_fifos( p_aout );
387     }
388
389     /* Lock all inputs. */
390     aout_lock_input_fifos( p_aout );
391     aout_MixerDelete( p_aout );
392
393     /* Re-open the output plug-in. */
394     aout_OutputDelete( p_aout );
395
396     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
397     {
398         /* Release all locks and report the error. */
399         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
400         {
401             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
402         }
403         aout_unlock_input_fifos( p_aout );
404         aout_unlock_mixer( p_aout );
405         return -1;
406     }
407
408     if ( aout_MixerNew( p_aout ) == -1 )
409     {
410         aout_OutputDelete( p_aout );
411         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
412         {
413             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
414         }
415         aout_unlock_input_fifos( p_aout );
416         aout_unlock_mixer( p_aout );
417         return -1;
418     }
419
420     /* Re-open all inputs. */
421     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
422     {
423         aout_input_t * p_input = p_aout->pp_inputs[i];
424         b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
425         p_input->b_changed = 1;
426         aout_unlock_input( p_aout, p_input );
427     }
428
429     aout_unlock_input_fifos( p_aout );
430     aout_unlock_mixer( p_aout );
431
432     return b_error;
433 }
434
435 /*****************************************************************************
436  * aout_ChannelsRestart : change the audio device or channels and restart
437  *****************************************************************************/
438 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
439                           vlc_value_t oldval, vlc_value_t newval,
440                           void *p_data )
441 {
442     aout_instance_t * p_aout = (aout_instance_t *)p_this;
443     (void)oldval; (void)newval; (void)p_data;
444
445     if ( !strcmp( psz_variable, "audio-device" ) )
446     {
447         /* This is supposed to be a significant change and supposes
448          * rebuilding the channel choices. */
449         var_Destroy( p_aout, "audio-channels" );
450     }
451     aout_Restart( p_aout );
452     return 0;
453 }
454
455 #undef aout_EnableFilter
456 /** Enable or disable an audio filter
457  * \param p_this a vlc object
458  * \param psz_name name of the filter
459  * \param b_add are we adding or removing the filter ?
460  */
461 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
462                         bool b_add )
463 {
464     aout_instance_t *p_aout = findAout( p_this );
465
466     if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
467     {
468         if( p_aout )
469             AoutInputsMarkToRestart( p_aout );
470     }
471
472     if( p_aout )
473         vlc_object_release( p_aout );
474 }