intf.c 16.7 KB
Newer Older
1
2
3
/*****************************************************************************
 * intf.c : audio output API towards the interface modules
 *****************************************************************************
4
 * Copyright (C) 2002-2007 the VideoLAN team
5
 * $Id$
6
7
8
9
10
11
12
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
13
 *
14
15
16
17
18
19
20
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
27

28
29
30
31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
33

34
#include <stdio.h>
35
36
37
#include <stdlib.h>                            /* calloc(), malloc(), free() */
#include <string.h>

zorglub's avatar
zorglub committed
38
#include <vlc_aout.h>
39
40
41
42
43
44
45
46
47
48
#include "aout_internal.h"

/*
 * Volume management
 *
 * The hardware volume cannot be set if the output module gets deleted, so
 * we must take the mixer lock. The software volume cannot be set while the
 * mixer is running, so we need the mixer lock (too).
 *
 * Here is a schematic of the i_volume range :
49
 *
50
51
 * |------------------------------+---------------------------------------|
 * 0                           pi_soft                                   1024
52
 *
53
54
55
56
 * Between 0 and pi_soft, the volume is done in hardware by the output
 * module. Above, the output module will change p_aout->mixer.i_multiplier
 * (done in software). This scaling may result * in cropping errors and
 * should be avoided as much as possible.
57
 *
58
59
60
61
 * It is legal to have *pi_soft == 0, and do everything in software.
 * It is also legal to have *pi_soft == 1024, and completely avoid
 * software scaling. However, some streams (esp. A/52) are encoded with
 * a very low volume and users may complain.
62
63
 */

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
enum {
    SET_MUTE=1,
    SET_VOLUME=2,
    INCREMENT_VOLUME=4,
    TOGGLE_MUTE=8
};
/*****************************************************************************
 * doVolumeChanges : handle all volume changes. Internal use only to ease
 *                   variables locking.
 *****************************************************************************/
int doVolumeChanges( unsigned action, vlc_object_t * p_object, int i_nb_steps,
                audio_volume_t i_volume, audio_volume_t * i_return_volume,
                bool b_mute )
{
    int i_result = VLC_SUCCESS;
JPeg's avatar
JPeg committed
79
    int i_volume_step = 1, i_new_volume = 0;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    bool b_var_mute = false;
    aout_instance_t *p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
                                               FIND_ANYWHERE );

    if ( p_aout ) aout_lock_volume( p_aout );

    b_var_mute = (bool)var_GetBool( p_object->p_libvlc, "volume-muted");

    const bool b_unmute_condition = ( /* Also unmute on increments */
                    ( action == INCREMENT_VOLUME )
                    || /* On explicit unmute */
                    ( ( action == SET_MUTE ) && ( b_var_mute && !b_mute ) )
                    || /* On toggle from muted */
                    ( ( action == TOGGLE_MUTE ) && b_var_mute ) );

    const bool b_mute_condition = ( !b_var_mute
                    && ( /* explicit */
                        ( ( action == SET_MUTE ) && b_mute )
                        || /* or toggle */
                        ( action == TOGGLE_MUTE )
                    ));

    /* On UnMute */
    if ( b_unmute_condition )
    {
        /* Restore saved volume */
        var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
        i_volume = (audio_volume_t)var_GetInteger( p_object->p_libvlc,
                                                   "saved-volume" );
        var_SetBool( p_object->p_libvlc, "volume-muted", false );
    }
    else if ( b_mute_condition )
    {
        /* We need an initial value to backup later */
        i_volume = config_GetInt( p_object, "volume" );
    }

    if ( action == INCREMENT_VOLUME )
    {
        i_volume_step = config_GetInt( p_object->p_libvlc, "volume-step" );

121
122
        if ( !b_unmute_condition )
            i_volume = config_GetInt( p_object, "volume" );
123

JPeg's avatar
JPeg committed
124
125
126
        i_new_volume = (int) i_volume + i_volume_step * i_nb_steps;

        if ( i_new_volume > AOUT_VOLUME_MAX )
127
            i_volume = AOUT_VOLUME_MAX;
JPeg's avatar
JPeg committed
128
        else if ( i_new_volume < AOUT_VOLUME_MIN )
129
            i_volume = AOUT_VOLUME_MIN;
JPeg's avatar
JPeg committed
130
131
132
        else
            i_volume = i_new_volume;

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        if ( i_return_volume != NULL )
            *i_return_volume = i_volume;
    }

    var_Create( p_object->p_libvlc, "saved-volume", VLC_VAR_INTEGER );
    var_SetInteger( p_object->p_libvlc, "saved-volume" , i_volume );

    /* On Mute */
    if ( b_mute_condition )
    {
        i_volume = AOUT_VOLUME_MIN;
        var_SetBool( p_object->p_libvlc, "volume-muted", true );
    }

    /* Commit volume changes */
    config_PutInt( p_object, "volume", i_volume );

    if ( p_aout )
    {
        aout_lock_mixer( p_aout );
        aout_lock_input_fifos( p_aout );
            if ( p_aout->p_mixer )
                i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
        aout_unlock_input_fifos( p_aout );
        aout_unlock_mixer( p_aout );
    }

    /* trigger callbacks */
161
    var_TriggerCallback( p_object->p_libvlc, "volume-change");
162
163
164
165
166
167
168
169
170
171
172
    if ( p_aout ) var_SetBool( p_aout, "intf-change", true );

    if ( p_aout )
    {
        aout_unlock_volume( p_aout );
        vlc_object_release( p_aout );
    }

    return i_result;
}

173
174
175
/*****************************************************************************
 * aout_VolumeGet : get the volume of the output device
 *****************************************************************************/
176
int __aout_VolumeGet( vlc_object_t * p_object, audio_volume_t * pi_volume )
177
{
178
    int i_result = 0;
179
180
181
    aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
                                                FIND_ANYWHERE );

182
    if ( pi_volume == NULL ) return -1;
183

184
185
186
187
188
    if ( p_aout == NULL )
    {
        *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
        return 0;
    }
189

190
    aout_lock_volume( p_aout );
191
    aout_lock_mixer( p_aout );
192
    if ( p_aout->p_mixer )
193
194
195
    {
        i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
    }
196
197
198
199
    else
    {
        *pi_volume = (audio_volume_t)config_GetInt( p_object, "volume" );
    }
200
    aout_unlock_mixer( p_aout );
201
    aout_unlock_volume( p_aout );
202

203
204
    vlc_object_release( p_aout );
    return i_result;
205
206
207
208
209
}

/*****************************************************************************
 * aout_VolumeSet : set the volume of the output device
 *****************************************************************************/
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
217
218
219
220
}

/*****************************************************************************
 * aout_VolumeUp : raise the output volume
 *****************************************************************************
 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
 * function.
 *****************************************************************************/
221
int __aout_VolumeUp( vlc_object_t * p_object, int i_nb_steps,
222
223
                   audio_volume_t * pi_volume )
{
224
    return doVolumeChanges( INCREMENT_VOLUME, p_object, i_nb_steps, 0, pi_volume, true );
225
226
227
228
229
230
231
232
}

/*****************************************************************************
 * aout_VolumeDown : lower the output volume
 *****************************************************************************
 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
 * function.
 *****************************************************************************/
233
int __aout_VolumeDown( vlc_object_t * p_object, int i_nb_steps,
234
235
                     audio_volume_t * pi_volume )
{
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
243
244
 *****************************************************************************
 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
 * function (muted => 0).
 *****************************************************************************/
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
252
253
254
}

/*****************************************************************************
 * aout_IsMuted : Get the output volume mute status
 *****************************************************************************/
bool aout_IsMuted( vlc_object_t * p_object )
{
255
256
257
258
259
    bool b_return_val;
    aout_instance_t * p_aout = vlc_object_find( p_object, VLC_OBJECT_AOUT,
                                                FIND_ANYWHERE );
    if ( p_aout ) aout_lock_volume( p_aout );
    b_return_val = var_GetBool( p_object->p_libvlc, "volume-muted");
260
261
262
263
264
    if ( p_aout )
    {
        aout_unlock_volume( p_aout );
        vlc_object_release( p_aout );
    }
265
    return b_return_val;
266
267
268
269
270
271
272
273
274
275
}

/*****************************************************************************
 * aout_SetMute : Sets mute status
 *****************************************************************************
 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
 * function (muted => 0).
 *****************************************************************************/
int aout_SetMute( vlc_object_t * p_object, audio_volume_t * pi_volume,
                  bool b_mute )
276
{
277
    return doVolumeChanges( SET_MUTE, p_object, 1, 0, pi_volume, b_mute );
278
279
280
281
282
283
284
285
286
287
}

/*
 * The next functions are not supposed to be called by the interface, but
 * are placeholders for software-only scaling.
 */

/* Meant to be called by the output plug-in's Open(). */
void aout_VolumeSoftInit( aout_instance_t * p_aout )
{
gbazin's avatar
   
gbazin committed
288
    int i_volume;
289

290
291
292
    p_aout->output.pf_volume_get = aout_VolumeSoftGet;
    p_aout->output.pf_volume_set = aout_VolumeSoftSet;

293
    i_volume = config_GetInt( p_aout, "volume" );
294
    if ( i_volume < AOUT_VOLUME_MIN )
295
    {
296
        i_volume = AOUT_VOLUME_DEFAULT;
297
    }
gbazin's avatar
   
gbazin committed
298
299
300
301
    else if ( i_volume > AOUT_VOLUME_MAX )
    {
        i_volume = AOUT_VOLUME_MAX;
    }
302

gbazin's avatar
   
gbazin committed
303
    aout_VolumeSoftSet( p_aout, (audio_volume_t)i_volume );
304
305
306
}

/* Placeholder for pf_volume_get(). */
307
int aout_VolumeSoftGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
308
309
310
311
312
313
314
{
    *pi_volume = p_aout->output.i_volume;
    return 0;
}


/* Placeholder for pf_volume_set(). */
315
int aout_VolumeSoftSet( aout_instance_t * p_aout, audio_volume_t i_volume )
316
{
317
318
    aout_MixerMultiplierSet( p_aout, (float)i_volume / AOUT_VOLUME_DEFAULT );
    p_aout->output.i_volume = i_volume;
319
320
321
    return 0;
}

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * The next functions are not supposed to be called by the interface, but
 * are placeholders for unsupported scaling.
 */

/* Meant to be called by the output plug-in's Open(). */
void aout_VolumeNoneInit( aout_instance_t * p_aout )
{
    p_aout->output.pf_volume_get = aout_VolumeNoneGet;
    p_aout->output.pf_volume_set = aout_VolumeNoneSet;
}

/* Placeholder for pf_volume_get(). */
int aout_VolumeNoneGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
{
337
    (void)p_aout; (void)pi_volume;
338
339
340
341
342
343
    return -1;
}

/* Placeholder for pf_volume_set(). */
int aout_VolumeNoneSet( aout_instance_t * p_aout, audio_volume_t i_volume )
{
344
    (void)p_aout; (void)i_volume;
345
346
347
    return -1;
}

348
349
350
351
352
353
354
355
356
357
358
359

/*
 * Pipelines management
 */

/*****************************************************************************
 * aout_Restart : re-open the output device and rebuild the input and output
 *                pipelines
 *****************************************************************************
 * This function is used whenever the parameters of the output plug-in are
 * changed (eg. selecting S/PDIF or PCM).
 *****************************************************************************/
360
static int aout_Restart( aout_instance_t * p_aout )
361
362
{
    int i;
363
    bool b_error = 0;
364

365
    aout_lock_mixer( p_aout );
366
367
368

    if ( p_aout->i_nb_inputs == 0 )
    {
369
        aout_unlock_mixer( p_aout );
370
371
372
373
374
375
        msg_Err( p_aout, "no decoder thread" );
        return -1;
    }

    for ( i = 0; i < p_aout->i_nb_inputs; i++ )
    {
376
        aout_lock_input( p_aout, p_aout->pp_inputs[i] );
377
        aout_lock_input_fifos( p_aout );
378
        aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
379
        aout_unlock_input_fifos( p_aout );
380
381
    }

382
383
    /* Lock all inputs. */
    aout_lock_input_fifos( p_aout );
384
385
386
387
    aout_MixerDelete( p_aout );

    /* Re-open the output plug-in. */
    aout_OutputDelete( p_aout );
388

389
390
391
392
393
394
395
    if ( aout_OutputNew( p_aout, &p_aout->pp_inputs[0]->input ) == -1 )
    {
        /* Release all locks and report the error. */
        for ( i = 0; i < p_aout->i_nb_inputs; i++ )
        {
            vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
        }
396
397
        aout_unlock_input_fifos( p_aout );
        aout_unlock_mixer( p_aout );
398
399
400
401
402
403
404
405
406
407
        return -1;
    }

    if ( aout_MixerNew( p_aout ) == -1 )
    {
        aout_OutputDelete( p_aout );
        for ( i = 0; i < p_aout->i_nb_inputs; i++ )
        {
            vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
        }
408
409
        aout_unlock_input_fifos( p_aout );
        aout_unlock_mixer( p_aout );
410
411
412
413
414
415
416
        return -1;
    }

    /* Re-open all inputs. */
    for ( i = 0; i < p_aout->i_nb_inputs; i++ )
    {
        aout_input_t * p_input = p_aout->pp_inputs[i];
417
        b_error |= aout_InputNew( p_aout, p_input, &p_input->request_vout );
418
        p_input->b_changed = 1;
419
        aout_unlock_input( p_aout, p_input );
420
421
    }

422
423
    aout_unlock_input_fifos( p_aout );
    aout_unlock_mixer( p_aout );
424
425
426
427

    return b_error;
}

428
429
/*****************************************************************************
 * aout_FindAndRestart : find the audio output instance and restart
430
431
432
433
 *****************************************************************************
 * This is used for callbacks of the configuration variables, and we believe
 * that when those are changed, it is a significant change which implies
 * rebuilding the audio-device and audio-channels variables.
434
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
435
int aout_FindAndRestart( vlc_object_t * p_this, const char *psz_name,
436
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
437
438
439
440
{
    aout_instance_t * p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
                                                FIND_ANYWHERE );

441
    (void)psz_name; (void)oldval; (void)newval; (void)p_data;
gbazin's avatar
   
gbazin committed
442
    if ( p_aout == NULL ) return VLC_SUCCESS;
443

444
445
    var_Destroy( p_aout, "audio-device" );
    var_Destroy( p_aout, "audio-channels" );
446

447
448
    aout_Restart( p_aout );
    vlc_object_release( p_aout );
gbazin's avatar
   
gbazin committed
449
450

    return VLC_SUCCESS;
451
}
452
453
454
455
456

/*****************************************************************************
 * aout_ChannelsRestart : change the audio device or channels and restart
 *****************************************************************************/
int aout_ChannelsRestart( vlc_object_t * p_this, const char * psz_variable,
457
458
                          vlc_value_t oldval, vlc_value_t newval,
                          void *p_data )
459
460
{
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
461
    (void)oldval; (void)newval; (void)p_data;
462
463
464
465
466

    if ( !strcmp( psz_variable, "audio-device" ) )
    {
        /* This is supposed to be a significant change and supposes
         * rebuilding the channel choices. */
467
        var_Destroy( p_aout, "audio-channels" );
468
469
470
471
    }
    aout_Restart( p_aout );
    return 0;
}
472

473
#undef aout_EnableFilter
474
475
476
477
478
479
/** Enable or disable an audio filter
 * \param p_this a vlc object
 * \param psz_name name of the filter
 * \param b_add are we adding or removing the filter ?
 */
void aout_EnableFilter( vlc_object_t *p_this, const char *psz_name,
480
                        bool b_add )
481
{
482
483
    aout_instance_t *p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT,
                                               FIND_ANYWHERE );
484

485
    if( AoutChangeFilterString( p_this, p_aout, "audio-filter", psz_name, b_add ) )
486
    {
487
488
        if( p_aout )
            AoutInputsMarkToRestart( p_aout );
489
490
    }

491
    if( p_aout )
492
493
        vlc_object_release( p_aout );
}