]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
Copyright fixes
[vlc] / src / audio_output / intf.c
1 /*****************************************************************************
2  * intf.c : audio output API towards the interface modules
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN (Centrale Réseaux) and its contributors
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #include "audio_output.h"
33 #include "aout_internal.h"
34
35
36 /*
37  * Volume management
38  *
39  * The hardware volume cannot be set if the output module gets deleted, so
40  * we must take the mixer lock. The software volume cannot be set while the
41  * mixer is running, so we need the mixer lock (too).
42  *
43  * Here is a schematic of the i_volume range :
44  *
45  * |------------------------------+---------------------------------------|
46  * 0                           pi_soft                                   1024
47  *
48  * Between 0 and pi_soft, the volume is done in hardware by the output
49  * module. Above, the output module will change p_aout->mixer.i_multiplier
50  * (done in software). This scaling may result * in cropping errors and
51  * should be avoided as much as possible.
52  *
53  * It is legal to have *pi_soft == 0, and do everything in software.
54  * It is also legal to have *pi_soft == 1024, and completely avoid
55  * software scaling. However, some streams (esp. A/52) are encoded with
56  * a very low volume and users may complain.
57  */
58
59 /*****************************************************************************
60  * aout_VolumeGet : get the volume of the output device
61  *****************************************************************************/
62 int __aout_VolumeGet( vlc_object_t * p_object, audio_volume_t * pi_volume )
63 {
64     int i_volume, i_result = 0;
65     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
66                                                 FIND_ANYWHERE );
67
68     i_volume = config_GetInt( p_object, "volume" );
69     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t)i_volume;
70
71     if ( p_aout == NULL ) return 0;
72
73     vlc_mutex_lock( &p_aout->mixer_lock );
74     if ( !p_aout->mixer.b_error )
75     {
76         i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
77     }
78     vlc_mutex_unlock( &p_aout->mixer_lock );
79
80     vlc_object_release( p_aout );
81     return i_result;
82 }
83
84 /*****************************************************************************
85  * aout_VolumeSet : set the volume of the output device
86  *****************************************************************************/
87 int __aout_VolumeSet( vlc_object_t * p_object, audio_volume_t i_volume )
88 {
89     vlc_value_t val;
90     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
91                                                 FIND_ANYWHERE );
92     int i_result = 0;
93
94     config_PutInt( p_object, "volume", i_volume );
95
96     if ( p_aout == NULL ) return 0;
97
98     vlc_mutex_lock( &p_aout->mixer_lock );
99     if ( !p_aout->mixer.b_error )
100     {
101         i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
102     }
103     vlc_mutex_unlock( &p_aout->mixer_lock );
104
105     vlc_object_release( p_aout );
106
107     val.b_bool = VLC_TRUE;
108     var_Set( p_aout, "intf-change", val );
109     return i_result;
110 }
111
112 /*****************************************************************************
113  * aout_VolumeInfos : get the boundary pi_soft
114  *****************************************************************************/
115 int __aout_VolumeInfos( vlc_object_t * p_object, audio_volume_t * pi_soft )
116 {
117     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
118                                                 FIND_ANYWHERE );
119     int i_result;
120
121     if ( p_aout == NULL ) return 0;
122
123     vlc_mutex_lock( &p_aout->mixer_lock );
124     if ( p_aout->mixer.b_error )
125     {
126         /* The output module is destroyed. */
127         i_result = -1;
128     }
129     else
130     {
131         i_result = p_aout->output.pf_volume_infos( p_aout, pi_soft );
132     }
133     vlc_mutex_unlock( &p_aout->mixer_lock );
134
135     vlc_object_release( p_aout );
136     return i_result;
137 }
138
139 /*****************************************************************************
140  * aout_VolumeUp : raise the output volume
141  *****************************************************************************
142  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
143  * function.
144  *****************************************************************************/
145 int __aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
146                    audio_volume_t * pi_volume )
147 {
148     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
149                                                 FIND_ANYWHERE );
150     int i_result = 0, i_volume = 0;
151
152     i_volume = config_GetInt( p_object, "volume" );
153     i_volume += AOUT_VOLUME_STEP * i_nb_steps;
154     if ( i_volume > AOUT_VOLUME_MAX )
155     {
156         i_volume = AOUT_VOLUME_MAX;
157     }
158     config_PutInt( p_object, "volume", i_volume );
159     var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
160     var_SetInteger( p_object->p_libvlc, "saved-volume" , (audio_volume_t) i_volume );
161     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t) i_volume;
162
163     if ( p_aout == NULL ) return 0;
164
165     vlc_mutex_lock( &p_aout->mixer_lock );
166     if ( !p_aout->mixer.b_error )
167     {
168         i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t) i_volume );
169     }
170     vlc_mutex_unlock( &p_aout->mixer_lock );
171
172     vlc_object_release( p_aout );
173     return i_result;
174 }
175
176 /*****************************************************************************
177  * aout_VolumeDown : lower the output volume
178  *****************************************************************************
179  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
180  * function.
181  *****************************************************************************/
182 int __aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
183                      audio_volume_t * pi_volume )
184 {
185     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
186                                                 FIND_ANYWHERE );
187     int i_result = 0, i_volume = 0;
188
189     i_volume = config_GetInt( p_object, "volume" );
190     i_volume -= AOUT_VOLUME_STEP * i_nb_steps;
191     if ( i_volume < AOUT_VOLUME_MIN )
192     {
193         i_volume = AOUT_VOLUME_MIN;
194     }
195     config_PutInt( p_object, "volume", i_volume );
196     var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
197     var_SetInteger( p_object->p_libvlc, "saved-volume", (audio_volume_t) i_volume );
198     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t) i_volume;
199
200     if ( p_aout == NULL ) return 0;
201
202     vlc_mutex_lock( &p_aout->mixer_lock );
203     if ( !p_aout->mixer.b_error )
204     {
205         i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t) i_volume );
206     }
207     vlc_mutex_unlock( &p_aout->mixer_lock );
208
209     vlc_object_release( p_aout );
210     return i_result;
211 }
212
213 /*****************************************************************************
214  * aout_VolumeMute : Mute/un-mute the output volume
215  *****************************************************************************
216  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
217  * function (muted => 0).
218  *****************************************************************************/
219 int __aout_VolumeMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
220 {
221     int i_result;
222     audio_volume_t i_volume;
223
224     i_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
225     if ( i_volume != 0 )
226     {
227         /* Mute */
228         i_result = aout_VolumeSet( p_object, AOUT_VOLUME_MIN );
229         var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
230         var_SetInteger( p_object->p_libvlc, "saved-volume", (int)i_volume );
231         if ( pi_volume != NULL ) *pi_volume = AOUT_VOLUME_MIN;
232     }
233     else
234     {
235         /* Un-mute */
236         var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
237         i_volume = (audio_volume_t)var_GetInteger( p_object->p_libvlc,
238                                                    "saved-volume" );
239         i_result = aout_VolumeSet( p_object, i_volume );
240         if ( pi_volume != NULL ) *pi_volume = i_volume;
241     }
242
243     return i_result;
244 }
245
246 /*
247  * The next functions are not supposed to be called by the interface, but
248  * are placeholders for software-only scaling.
249  */
250
251 /* Meant to be called by the output plug-in's Open(). */
252 void aout_VolumeSoftInit( aout_instance_t * p_aout )
253 {
254     int i_volume;
255
256     p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
257     p_aout->output.pf_volume_get = aout_VolumeSoftGet;
258     p_aout->output.pf_volume_set = aout_VolumeSoftSet;
259
260     i_volume = config_GetInt( p_aout, "volume" );
261     if ( i_volume < AOUT_VOLUME_MIN )
262     {
263         i_volume = AOUT_VOLUME_DEFAULT;
264     }
265     else if ( i_volume > AOUT_VOLUME_MAX )
266     {
267         i_volume = AOUT_VOLUME_MAX;
268     }
269
270     aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
271 }
272
273 /* Placeholder for pf_volume_infos(). */
274 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
275 {
276     *pi_soft = 0;
277     return 0;
278 }
279
280 /* Placeholder for pf_volume_get(). */
281 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
282 {
283     *pi_volume = p_aout->output.i_volume;
284     return 0;
285 }
286
287
288 /* Placeholder for pf_volume_set(). */
289 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
290 {
291     aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
292     p_aout->output.i_volume = i_volume;
293     return 0;
294 }
295
296 /*
297  * The next functions are not supposed to be called by the interface, but
298  * are placeholders for unsupported scaling.
299  */
300
301 /* Meant to be called by the output plug-in's Open(). */
302 void aout_VolumeNoneInit( aout_instance_t * p_aout )
303 {
304     p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
305     p_aout->output.pf_volume_get = aout_VolumeNoneGet;
306     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
307 }
308
309 /* Placeholder for pf_volume_infos(). */
310 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
311 {
312     return -1;
313 }
314
315 /* Placeholder for pf_volume_get(). */
316 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
317 {
318     return -1;
319 }
320
321 /* Placeholder for pf_volume_set(). */
322 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
323 {
324     return -1;
325 }
326
327
328 /*
329  * Pipelines management
330  */
331
332 /*****************************************************************************
333  * aout_Restart : re-open the output device and rebuild the input and output
334  *                pipelines
335  *****************************************************************************
336  * This function is used whenever the parameters of the output plug-in are
337  * changed (eg. selecting S/PDIF or PCM).
338  *****************************************************************************/
339 int aout_Restart( aout_instance_t * p_aout )
340 {
341     int i;
342     vlc_bool_t b_error = 0;
343
344     vlc_mutex_lock( &p_aout->mixer_lock );
345
346     if ( p_aout->i_nb_inputs == 0 )
347     {
348         vlc_mutex_unlock( &p_aout->mixer_lock );
349         msg_Err( p_aout, "no decoder thread" );
350         return -1;
351     }
352
353     /* Lock all inputs. */
354     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
355     {
356         vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
357         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
358     }
359
360     aout_MixerDelete( p_aout );
361
362     /* Re-open the output plug-in. */
363     aout_OutputDelete( p_aout );
364
365     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
366     {
367         /* Release all locks and report the error. */
368         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
369         {
370             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
371         }
372         vlc_mutex_unlock( &p_aout->mixer_lock );
373         return -1;
374     }
375
376     if ( aout_MixerNew( p_aout ) == -1 )
377     {
378         aout_OutputDelete( p_aout );
379         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
380         {
381             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
382         }
383         vlc_mutex_unlock( &p_aout->mixer_lock );
384         return -1;
385     }
386
387     /* Re-open all inputs. */
388     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
389     {
390         aout_input_t * p_input = p_aout->pp_inputs[i];
391
392         b_error |= aout_InputNew( p_aout, p_input );
393         p_input->b_changed = 1;
394         vlc_mutex_unlock( &p_input->lock );
395     }
396
397     vlc_mutex_unlock( &p_aout->mixer_lock );
398
399     return b_error;
400 }
401
402 /*****************************************************************************
403  * aout_FindAndRestart : find the audio output instance and restart
404  *****************************************************************************
405  * This is used for callbacks of the configuration variables, and we believe
406  * that when those are changed, it is a significant change which implies
407  * rebuilding the audio-device and audio-channels variables.
408  *****************************************************************************/
409 int aout_FindAndRestart( vlc_object_t * p_this, const char *psz_name,
410                          vlc_value_t oldval, vlc_value_t val, void *p_data )
411 {
412     aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
413                                                 FIND_ANYWHERE );
414
415     if ( p_aout == NULL ) return VLC_SUCCESS;
416
417     if ( var_Type( p_aout, "audio-device" ) != 0 )
418     {
419         var_Destroy( p_aout, "audio-device" );
420     }
421     if ( var_Type( p_aout, "audio-channels" ) != 0 )
422     {
423         var_Destroy( p_aout, "audio-channels" );
424     }
425
426     aout_Restart( p_aout );
427     vlc_object_release( p_aout );
428
429     return VLC_SUCCESS;
430 }
431
432 /*****************************************************************************
433  * aout_ChannelsRestart : change the audio device or channels and restart
434  *****************************************************************************/
435 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
436                           vlc_value_t old_value, vlc_value_t new_value,
437                           void * unused )
438 {
439     aout_instance_t * p_aout = (aout_instance_t *)p_this;
440
441     if ( !strcmp( psz_variable, "audio-device" ) )
442     {
443         /* This is supposed to be a significant change and supposes
444          * rebuilding the channel choices. */
445         if ( var_Type( p_aout, "audio-channels" ) >= 0 )
446         {
447             var_Destroy( p_aout, "audio-channels" );
448         }
449     }
450     aout_Restart( p_aout );
451     return 0;
452 }