]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
Remove outdated comment
[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 ( pi_volume == NULL ) return -1;
189
190     if ( p_aout == NULL )
191     {
192         *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
193         return 0;
194     }
195
196     aout_lock_volume( p_aout );
197     aout_lock_mixer( p_aout );
198     if ( p_aout->p_mixer )
199     {
200         i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
201     }
202     else
203     {
204         *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
205     }
206     aout_unlock_mixer( p_aout );
207     aout_unlock_volume( p_aout );
208
209     vlc_object_release( p_aout );
210     return i_result;
211 }
212
213 #undef aout_VolumeSet
214 /*****************************************************************************
215  * aout_VolumeSet : set the volume of the output device
216  *****************************************************************************/
217 int aout_VolumeSet( vlc_object_t * p_object, audio_volume_t i_volume )
218 {
219     return doVolumeChanges( SET_VOLUME, p_object, 1, i_volume, NULL, true );
220 }
221
222 #undef aout_VolumeUp
223 /*****************************************************************************
224  * aout_VolumeUp : raise the output volume
225  *****************************************************************************
226  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
227  * function.
228  *****************************************************************************/
229 int aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
230                    audio_volume_t * pi_volume )
231 {
232     return doVolumeChanges( INCREMENT_VOLUME, p_object, i_nb_steps, 0, pi_volume, true );
233 }
234
235 #undef aout_VolumeDown
236 /*****************************************************************************
237  * aout_VolumeDown : lower the output volume
238  *****************************************************************************
239  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
240  * function.
241  *****************************************************************************/
242 int aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
243                      audio_volume_t * pi_volume )
244 {
245     return aout_VolumeUp( p_object, -i_nb_steps, pi_volume );
246 }
247
248 #undef aout_ToggleMute
249 /*****************************************************************************
250  * aout_ToggleMute : Mute/un-mute the output volume
251  *****************************************************************************
252  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
253  * function (muted => 0).
254  *****************************************************************************/
255 int aout_ToggleMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
256 {
257     return doVolumeChanges( TOGGLE_MUTE, p_object, 1, 0, pi_volume, true );
258 }
259
260 /*****************************************************************************
261  * aout_IsMuted : Get the output volume mute status
262  *****************************************************************************/
263 bool aout_IsMuted( vlc_object_t * p_object )
264 {
265     bool b_return_val;
266     aout_instance_t * p_aout = findAout( p_object );
267     if ( p_aout ) aout_lock_volume( p_aout );
268     b_return_val = var_GetBool( p_object, "volume-muted");
269     if ( p_aout )
270     {
271         aout_unlock_volume( p_aout );
272         vlc_object_release( p_aout );
273     }
274     return b_return_val;
275 }
276
277 /*****************************************************************************
278  * aout_SetMute : Sets mute status
279  *****************************************************************************
280  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
281  * function (muted => 0).
282  *****************************************************************************/
283 int aout_SetMute( vlc_object_t * p_object, audio_volume_t * pi_volume,
284                   bool b_mute )
285 {
286     return doVolumeChanges( SET_MUTE, p_object, 1, 0, pi_volume, b_mute );
287 }
288
289 /*
290  * The next functions are not supposed to be called by the interface, but
291  * are placeholders for software-only scaling.
292  */
293
294 /* Meant to be called by the output plug-in's Open(). */
295 void aout_VolumeSoftInit( aout_instance_t * p_aout )
296 {
297     int i_volume;
298
299     p_aout->output.pf_volume_get = aout_VolumeSoftGet;
300     p_aout->output.pf_volume_set = aout_VolumeSoftSet;
301
302     i_volume = config_GetInt( p_aout, "volume" );
303     if ( i_volume < AOUT_VOLUME_MIN )
304     {
305         i_volume = AOUT_VOLUME_DEFAULT;
306     }
307     else if ( i_volume > AOUT_VOLUME_MAX )
308     {
309         i_volume = AOUT_VOLUME_MAX;
310     }
311
312     aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
313 }
314
315 /* Placeholder for pf_volume_get(). */
316 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
317 {
318     *pi_volume = p_aout->output.i_volume;
319     return 0;
320 }
321
322
323 /* Placeholder for pf_volume_set(). */
324 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
325 {
326     aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
327     p_aout->output.i_volume = i_volume;
328     return 0;
329 }
330
331 /*
332  * The next functions are not supposed to be called by the interface, but
333  * are placeholders for unsupported scaling.
334  */
335
336 /* Meant to be called by the output plug-in's Open(). */
337 void aout_VolumeNoneInit( aout_instance_t * p_aout )
338 {
339     p_aout->output.pf_volume_get = aout_VolumeNoneGet;
340     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
341 }
342
343 /* Placeholder for pf_volume_get(). */
344 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
345 {
346     (void)p_aout; (void)pi_volume;
347     return -1;
348 }
349
350 /* Placeholder for pf_volume_set(). */
351 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
352 {
353     (void)p_aout; (void)i_volume;
354     return -1;
355 }
356
357
358 /*
359  * Pipelines management
360  */
361
362 /*****************************************************************************
363  * aout_Restart : re-open the output device and rebuild the input and output
364  *                pipelines
365  *****************************************************************************
366  * This function is used whenever the parameters of the output plug-in are
367  * changed (eg. selecting S/PDIF or PCM).
368  *****************************************************************************/
369 static int aout_Restart( aout_instance_t * p_aout )
370 {
371     int i;
372     bool b_error = 0;
373
374     aout_lock_mixer( p_aout );
375
376     if ( p_aout->i_nb_inputs == 0 )
377     {
378         aout_unlock_mixer( p_aout );
379         msg_Err( p_aout, "no decoder thread" );
380         return -1;
381     }
382
383     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
384     {
385         aout_lock_input( p_aout, p_aout->pp_inputs[i] );
386         aout_lock_input_fifos( p_aout );
387         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
388         aout_unlock_input_fifos( p_aout );
389     }
390
391     /* Lock all inputs. */
392     aout_lock_input_fifos( p_aout );
393     aout_MixerDelete( p_aout );
394
395     /* Re-open the output plug-in. */
396     aout_OutputDelete( p_aout );
397
398     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
399     {
400         /* Release all locks and report the error. */
401         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
402         {
403             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
404         }
405         aout_unlock_input_fifos( p_aout );
406         aout_unlock_mixer( p_aout );
407         return -1;
408     }
409
410     if ( aout_MixerNew( p_aout ) == -1 )
411     {
412         aout_OutputDelete( p_aout );
413         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
414         {
415             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
416         }
417         aout_unlock_input_fifos( p_aout );
418         aout_unlock_mixer( p_aout );
419         return -1;
420     }
421
422     /* Re-open all inputs. */
423     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
424     {
425         aout_input_t * p_input = p_aout->pp_inputs[i];
426         b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
427         p_input->b_changed = 1;
428         aout_unlock_input( p_aout, p_input );
429     }
430
431     aout_unlock_input_fifos( p_aout );
432     aout_unlock_mixer( p_aout );
433
434     return b_error;
435 }
436
437 /*****************************************************************************
438  * aout_ChannelsRestart : change the audio device or channels and restart
439  *****************************************************************************/
440 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
441                           vlc_value_t oldval, vlc_value_t newval,
442                           void *p_data )
443 {
444     aout_instance_t * p_aout = (aout_instance_t *)p_this;
445     (void)oldval; (void)newval; (void)p_data;
446
447     if ( !strcmp( psz_variable, "audio-device" ) )
448     {
449         /* This is supposed to be a significant change and supposes
450          * rebuilding the channel choices. */
451         var_Destroy( p_aout, "audio-channels" );
452     }
453     aout_Restart( p_aout );
454     return 0;
455 }
456
457 #undef aout_EnableFilter
458 /** Enable or disable an audio filter
459  * \param p_this a vlc object
460  * \param psz_name name of the filter
461  * \param b_add are we adding or removing the filter ?
462  */
463 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
464                         bool b_add )
465 {
466     aout_instance_t *p_aout = findAout( p_this );
467
468     if( aout_ChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
469     {
470         if( p_aout )
471             AoutInputsMarkToRestart( p_aout );
472     }
473
474     if( p_aout )
475         vlc_object_release( p_aout );
476 }