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