]> git.sesse.net Git - vlc/blob - src/audio_output/intf.c
For consistency, remove references to vlc from libvlc
[vlc] / src / audio_output / intf.c
1 /*****************************************************************************
2  * intf.c : audio output API towards the interface modules
3  *****************************************************************************
4  * Copyright (C) 2002-2004 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 #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_result = 0;
65     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
66                                                 FIND_ANYWHERE );
67
68     if ( pi_volume == NULL ) return -1;
69
70     if ( p_aout == NULL )
71     {
72         *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
73         return 0;
74     }
75
76     vlc_mutex_lock( &p_aout->mixer_lock );
77     if ( !p_aout->mixer.b_error )
78     {
79         i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
80     }
81     else
82     {
83         *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
84     }
85     vlc_mutex_unlock( &p_aout->mixer_lock );
86
87     vlc_object_release( p_aout );
88     return i_result;
89 }
90
91 /*****************************************************************************
92  * aout_VolumeSet : set the volume of the output device
93  *****************************************************************************/
94 int __aout_VolumeSet( vlc_object_t * p_object, audio_volume_t i_volume )
95 {
96     vlc_value_t val;
97     aout_instance_t *p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT, FIND_ANYWHERE );
98     int i_result = 0;
99
100     config_PutInt( p_object, "volume", i_volume );
101
102     val.b_bool = VLC_TRUE;
103     var_Set( p_object->p_libvlc, "volume-change", val );
104
105     if ( p_aout == NULL ) return 0;
106
107     vlc_mutex_lock( &p_aout->mixer_lock );
108     if ( !p_aout->mixer.b_error )
109     {
110         i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
111     }
112     vlc_mutex_unlock( &p_aout->mixer_lock );
113
114     var_Set( p_aout, "intf-change", val );
115     vlc_object_release( p_aout );
116     return i_result;
117 }
118
119 /*****************************************************************************
120  * aout_VolumeInfos : get the boundary pi_soft
121  *****************************************************************************/
122 int __aout_VolumeInfos( vlc_object_t * p_object, audio_volume_t * pi_soft )
123 {
124     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
125                                                 FIND_ANYWHERE );
126     int i_result;
127
128     if ( p_aout == NULL ) return 0;
129
130     vlc_mutex_lock( &p_aout->mixer_lock );
131     if ( p_aout->mixer.b_error )
132     {
133         /* The output module is destroyed. */
134         i_result = -1;
135     }
136     else
137     {
138         i_result = p_aout->output.pf_volume_infos( p_aout, pi_soft );
139     }
140     vlc_mutex_unlock( &p_aout->mixer_lock );
141
142     vlc_object_release( p_aout );
143     return i_result;
144 }
145
146 /*****************************************************************************
147  * aout_VolumeUp : raise the output volume
148  *****************************************************************************
149  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
150  * function.
151  *****************************************************************************/
152 int __aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
153                    audio_volume_t * pi_volume )
154 {
155     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
156                                                 FIND_ANYWHERE );
157     int i_result = 0, i_volume = 0, i_volume_step = 0;
158
159     i_volume_step = config_GetInt( p_object->p_libvlc, "volume-step" );
160     i_volume = config_GetInt( p_object, "volume" );
161     i_volume += i_volume_step * i_nb_steps;
162     if ( i_volume > AOUT_VOLUME_MAX )
163     {
164         i_volume = AOUT_VOLUME_MAX;
165     }
166     config_PutInt( p_object, "volume", i_volume );
167     var_Create( p_object->p_libvlc_global, "saved-volume", VLC_VAR_INTEGER );
168     var_SetInteger( p_object->p_libvlc_global, "saved-volume" ,
169                     (audio_volume_t) i_volume );
170     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t) i_volume;
171
172     if ( p_aout == NULL ) return 0;
173
174     vlc_mutex_lock( &p_aout->mixer_lock );
175     if ( !p_aout->mixer.b_error )
176     {
177         i_result = p_aout->output.pf_volume_set( p_aout,
178                                                 (audio_volume_t) i_volume );
179     }
180     vlc_mutex_unlock( &p_aout->mixer_lock );
181
182     vlc_object_release( p_aout );
183     return i_result;
184 }
185
186 /*****************************************************************************
187  * aout_VolumeDown : lower the output volume
188  *****************************************************************************
189  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
190  * function.
191  *****************************************************************************/
192 int __aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
193                      audio_volume_t * pi_volume )
194 {
195     aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
196                                                 FIND_ANYWHERE );
197     int i_result = 0, i_volume = 0, i_volume_step = 0;
198
199     i_volume_step = config_GetInt( p_object->p_libvlc, "volume-step" );
200     i_volume = config_GetInt( p_object, "volume" );
201     i_volume -= i_volume_step * i_nb_steps;
202     if ( i_volume < AOUT_VOLUME_MIN )
203     {
204         i_volume = AOUT_VOLUME_MIN;
205     }
206     config_PutInt( p_object, "volume", i_volume );
207     var_Create( p_object->p_libvlc_global, "saved-volume", VLC_VAR_INTEGER );
208     var_SetInteger( p_object->p_libvlc_global, "saved-volume", (audio_volume_t) i_volume );
209     if ( pi_volume != NULL ) *pi_volume = (audio_volume_t) i_volume;
210
211     if ( p_aout == NULL ) return 0;
212
213     vlc_mutex_lock( &p_aout->mixer_lock );
214     if ( !p_aout->mixer.b_error )
215     {
216         i_result = p_aout->output.pf_volume_set( p_aout, (audio_volume_t) i_volume );
217     }
218     vlc_mutex_unlock( &p_aout->mixer_lock );
219
220     vlc_object_release( p_aout );
221     return i_result;
222 }
223
224 /*****************************************************************************
225  * aout_VolumeMute : Mute/un-mute the output volume
226  *****************************************************************************
227  * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
228  * function (muted => 0).
229  *****************************************************************************/
230 int __aout_VolumeMute( vlc_object_t * p_object, audio_volume_t * pi_volume )
231 {
232     int i_result;
233     audio_volume_t i_volume;
234
235     i_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
236     if ( i_volume != 0 )
237     {
238         /* Mute */
239         i_result = aout_VolumeSet( p_object, AOUT_VOLUME_MIN );
240         var_Create( p_object->p_libvlc_global, "saved-volume", VLC_VAR_INTEGER );
241         var_SetInteger( p_object->p_libvlc_global, "saved-volume", (int)i_volume );
242         if ( pi_volume != NULL ) *pi_volume = AOUT_VOLUME_MIN;
243     }
244     else
245     {
246         /* Un-mute */
247         var_Create( p_object->p_libvlc_global, "saved-volume", VLC_VAR_INTEGER );
248         i_volume = (audio_volume_t)var_GetInteger( p_object->p_libvlc_global,
249                                                    "saved-volume" );
250         i_result = aout_VolumeSet( p_object, i_volume );
251         if ( pi_volume != NULL ) *pi_volume = i_volume;
252     }
253
254     return i_result;
255 }
256
257 /*
258  * The next functions are not supposed to be called by the interface, but
259  * are placeholders for software-only scaling.
260  */
261
262 /* Meant to be called by the output plug-in's Open(). */
263 void aout_VolumeSoftInit( aout_instance_t * p_aout )
264 {
265     int i_volume;
266
267     p_aout->output.pf_volume_infos = aout_VolumeSoftInfos;
268     p_aout->output.pf_volume_get = aout_VolumeSoftGet;
269     p_aout->output.pf_volume_set = aout_VolumeSoftSet;
270
271     i_volume = config_GetInt( p_aout, "volume" );
272     if ( i_volume < AOUT_VOLUME_MIN )
273     {
274         i_volume = AOUT_VOLUME_DEFAULT;
275     }
276     else if ( i_volume > AOUT_VOLUME_MAX )
277     {
278         i_volume = AOUT_VOLUME_MAX;
279     }
280
281     aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
282 }
283
284 /* Placeholder for pf_volume_infos(). */
285 int aout_VolumeSoftInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
286 {
287     *pi_soft = 0;
288     return 0;
289 }
290
291 /* Placeholder for pf_volume_get(). */
292 int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
293 {
294     *pi_volume = p_aout->output.i_volume;
295     return 0;
296 }
297
298
299 /* Placeholder for pf_volume_set(). */
300 int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
301 {
302     aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
303     p_aout->output.i_volume = i_volume;
304     return 0;
305 }
306
307 /*
308  * The next functions are not supposed to be called by the interface, but
309  * are placeholders for unsupported scaling.
310  */
311
312 /* Meant to be called by the output plug-in's Open(). */
313 void aout_VolumeNoneInit( aout_instance_t * p_aout )
314 {
315     p_aout->output.pf_volume_infos = aout_VolumeNoneInfos;
316     p_aout->output.pf_volume_get = aout_VolumeNoneGet;
317     p_aout->output.pf_volume_set = aout_VolumeNoneSet;
318 }
319
320 /* Placeholder for pf_volume_infos(). */
321 int aout_VolumeNoneInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
322 {
323     return -1;
324 }
325
326 /* Placeholder for pf_volume_get(). */
327 int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
328 {
329     return -1;
330 }
331
332 /* Placeholder for pf_volume_set(). */
333 int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
334 {
335     return -1;
336 }
337
338
339 /*
340  * Pipelines management
341  */
342
343 /*****************************************************************************
344  * aout_Restart : re-open the output device and rebuild the input and output
345  *                pipelines
346  *****************************************************************************
347  * This function is used whenever the parameters of the output plug-in are
348  * changed (eg. selecting S/PDIF or PCM).
349  *****************************************************************************/
350 int aout_Restart( aout_instance_t * p_aout )
351 {
352     int i;
353     vlc_bool_t b_error = 0;
354
355     vlc_mutex_lock( &p_aout->mixer_lock );
356
357     if ( p_aout->i_nb_inputs == 0 )
358     {
359         vlc_mutex_unlock( &p_aout->mixer_lock );
360         msg_Err( p_aout, "no decoder thread" );
361         return -1;
362     }
363
364     /* Lock all inputs. */
365     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
366     {
367         vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
368         aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
369     }
370
371     aout_MixerDelete( p_aout );
372
373     /* Re-open the output plug-in. */
374     aout_OutputDelete( p_aout );
375
376     if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
377     {
378         /* Release all locks and report the error. */
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     if ( aout_MixerNew( p_aout ) == -1 )
388     {
389         aout_OutputDelete( p_aout );
390         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
391         {
392             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
393         }
394         vlc_mutex_unlock( &p_aout->mixer_lock );
395         return -1;
396     }
397
398     /* Re-open all inputs. */
399     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
400     {
401         aout_input_t * p_input = p_aout->pp_inputs[i];
402
403         b_error |= aout_InputNew( p_aout, p_input );
404         p_input->b_changed = 1;
405         vlc_mutex_unlock( &p_input->lock );
406     }
407
408     vlc_mutex_unlock( &p_aout->mixer_lock );
409
410     return b_error;
411 }
412
413 /*****************************************************************************
414  * aout_FindAndRestart : find the audio output instance and restart
415  *****************************************************************************
416  * This is used for callbacks of the configuration variables, and we believe
417  * that when those are changed, it is a significant change which implies
418  * rebuilding the audio-device and audio-channels variables.
419  *****************************************************************************/
420 int aout_FindAndRestart( vlc_object_t * p_this, const char *psz_name,
421                          vlc_value_t oldval, vlc_value_t val, void *p_data )
422 {
423     aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
424                                                 FIND_ANYWHERE );
425
426     if ( p_aout == NULL ) return VLC_SUCCESS;
427
428     if ( var_Type( p_aout, "audio-device" ) != 0 )
429     {
430         var_Destroy( p_aout, "audio-device" );
431     }
432     if ( var_Type( p_aout, "audio-channels" ) != 0 )
433     {
434         var_Destroy( p_aout, "audio-channels" );
435     }
436
437     aout_Restart( p_aout );
438     vlc_object_release( p_aout );
439
440     return VLC_SUCCESS;
441 }
442
443 /*****************************************************************************
444  * aout_ChannelsRestart : change the audio device or channels and restart
445  *****************************************************************************/
446 int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
447                           vlc_value_t old_value, vlc_value_t new_value,
448                           void * unused )
449 {
450     aout_instance_t * p_aout = (aout_instance_t *)p_this;
451
452     if ( !strcmp( psz_variable, "audio-device" ) )
453     {
454         /* This is supposed to be a significant change and supposes
455          * rebuilding the channel choices. */
456         if ( var_Type( p_aout, "audio-channels" ) >= 0 )
457         {
458             var_Destroy( p_aout, "audio-channels" );
459         }
460     }
461     aout_Restart( p_aout );
462     return 0;
463 }
464
465 /** Enable or disable an audio filter
466  * \param p_this a vlc object
467  * \param psz_name name of the filter
468  * \param b_add are we adding or removing the filter ?
469  */
470 void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
471                         vlc_bool_t b_add )
472 {
473     char *psz_parser, *psz_string;
474     aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
475                                                 FIND_ANYWHERE );
476
477     if( p_aout )
478         psz_string = var_GetString( p_aout, "audio-filter" );
479     else
480         psz_string = config_GetPsz( p_this, "audio-filter" );
481
482     if( !psz_string ) psz_string = strdup("");
483
484     psz_parser = strstr( psz_string, psz_name );
485
486     if( b_add )
487     {
488         if( !psz_parser )
489         {
490             psz_parser = psz_string;
491             asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
492                             psz_string, psz_name );
493             free( psz_parser );
494         }
495         else
496         {
497             vlc_object_release( p_aout );
498             return;
499         }
500     }
501     else
502     {
503         if( psz_parser )
504         {
505             memmove( psz_parser, psz_parser + strlen(psz_name) +
506                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
507                             strlen(psz_parser + strlen(psz_name)) + 1 );
508
509             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
510             {
511                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
512             }
513          }
514          else
515          {
516              free( psz_string );
517              return;
518          }
519     }
520
521     if( p_aout == NULL )
522         config_PutPsz( p_this, "audio-filter", psz_string );
523     else
524     {
525         var_SetString( p_aout, "audio-filter", psz_string );
526         for( int i = 0; i < p_aout->i_nb_inputs; i++ )
527             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
528         vlc_object_release( p_aout );
529     }
530     free( psz_string );
531 }