audio.c 16.7 KB
Newer Older
littlejohn's avatar
littlejohn committed
1
2
3
/*****************************************************************************
 * libvlc_audio.c: New libvlc audio control API
 *****************************************************************************
littlejohn's avatar
littlejohn committed
4
5
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
littlejohn's avatar
littlejohn committed
6
7
 *
 * Authors: Filippo Carone <filippo@carone.org>
8
 *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
littlejohn's avatar
littlejohn committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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.
 *
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

25
26
27
28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

littlejohn's avatar
littlejohn committed
29
#include <vlc/libvlc.h>
30
31
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_player.h>
littlejohn's avatar
littlejohn committed
32

33
#include <vlc_common.h>
34
#include <vlc_input.h>
zorglub's avatar
zorglub committed
35
#include <vlc_aout.h>
littlejohn's avatar
littlejohn committed
36

37
38
#include "libvlc_internal.h"
#include "media_player_internal.h"
39

40
41
42
43
44
45
46
/*
 * Remember to release the returned aout_instance_t since it is locked at
 * the end of this function.
 */
static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
                                 libvlc_exception_t *p_exception )
{
lukas.durfina's avatar
lukas.durfina committed
47
48
49
    if( !p_instance )
        return NULL;

50
51
52
53
54
55
56
57
58
59
60
61
    aout_instance_t * p_aout = NULL;

    p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
    if( !p_aout )
    {
        libvlc_exception_raise( p_exception, "No active audio output" );
        return NULL;
    }

    return p_aout;
}

lukas.durfina's avatar
lukas.durfina committed
62
63
64
/*****************************************
 * Get the list of available audio outputs
 *****************************************/
65
libvlc_audio_output_t *
lukas.durfina's avatar
lukas.durfina committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        libvlc_audio_output_list_get( libvlc_instance_t *p_instance,
                                      libvlc_exception_t *p_e )
{
    VLC_UNUSED( p_instance );
    libvlc_audio_output_t *p_list = NULL,
                          *p_actual = NULL,
                          *p_previous = NULL;
    module_t **module_list = module_list_get( NULL );

    for (size_t i = 0; module_list[i]; i++)
    {
        module_t *p_module = module_list[i];

        if( module_provides( p_module, "audio output" ) )
        {
            if( p_actual == NULL)
            {
                p_actual = ( libvlc_audio_output_t * )
                    malloc( sizeof( libvlc_audio_output_t ) );
                if( p_actual == NULL )
                {
                    libvlc_exception_raise( p_e, "Not enough memory" );
                    libvlc_audio_output_list_release( p_list );
                    module_list_free( module_list );
                    return NULL;
                }
                if( p_list == NULL )
                {
                    p_list = p_actual;
                    p_previous = p_actual;
                }
            }
98
            p_actual->psz_name = strdup( module_get_object( p_module ) );
lukas.durfina's avatar
lukas.durfina committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
            p_actual->psz_description = strdup( module_get_name( p_module, true )  );
            p_actual->p_next = NULL;
            if( p_previous != p_actual ) /* not first item */
                p_previous->p_next = p_actual;
            p_previous = p_actual;
            p_actual = p_actual->p_next;
        }
    }

    module_list_free( module_list );

    return p_list;
}

/********************************************
 * Free the list of available audio outputs
 ***********************************************/
116
void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
lukas.durfina's avatar
lukas.durfina committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{
    libvlc_audio_output_t *p_actual, *p_before;
    p_actual = p_list;

    while ( p_actual )
    {
        free( p_actual->psz_name );
        free( p_actual->psz_description );
        p_before = p_actual;
        p_actual = p_before->p_next;
        free( p_before );
    }
}


/***********************
 * Set the audio output.
 ***********************/
135
int libvlc_audio_output_set( libvlc_instance_t *p_instance,
lukas.durfina's avatar
lukas.durfina committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                                            const char *psz_name )
{
    if( module_exists( psz_name ) )
    {
        config_PutPsz( p_instance->p_libvlc_int, "aout", psz_name );
        return true;
    }
    else
        return false;
}

/****************************
 * Get count of devices.
 *****************************/
int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
                                      const char *psz_audio_output )
{
153
    char *psz_config_name;
lukas.durfina's avatar
lukas.durfina committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    if( !psz_audio_output )
        return 0;
    if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
        return 0;

    module_config_t *p_module_config = config_FindConfig(
        VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );

    if( p_module_config && p_module_config->pf_update_list )
    {
        vlc_value_t val;
        val.psz_string = strdup( p_module_config->value.psz );

        p_module_config->pf_update_list(
            VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
        free( val.psz_string );
        free( psz_config_name );

        return p_module_config->i_list;
    }

175
    free( psz_config_name );
lukas.durfina's avatar
lukas.durfina committed
176
177
178
179
180
181
182
183
184
185
    return 0;
}

/********************************
 * Get long name of device
 *********************************/
char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
                                            const char *psz_audio_output,
                                            int i_device )
{
186
    char *psz_config_name;
lukas.durfina's avatar
lukas.durfina committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    if( !psz_audio_output )
        return NULL;
    if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
        return NULL;

    module_config_t *p_module_config = config_FindConfig(
        VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );

    if( p_module_config )
    {
        // refresh if there arent devices
        if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
        {
            vlc_value_t val;
            val.psz_string = strdup( p_module_config->value.psz );

            p_module_config->pf_update_list(
                VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
            free( val.psz_string );
        }
        free( psz_config_name );

        if( i_device >= 0 && i_device < p_module_config->i_list )
        {
            if( p_module_config->ppsz_list_text[i_device] )
                return strdup( p_module_config->ppsz_list_text[i_device] );
            else
                return strdup( p_module_config->ppsz_list[i_device] );
        }
    }

218
    free( psz_config_name );
lukas.durfina's avatar
lukas.durfina committed
219
220
221
222
223
224
225
226
227
228
    return NULL;
}

/********************************
 * Get id name of device
 *********************************/
char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
                                      const char *psz_audio_output,
                                      int i_device )
{
229
    char *psz_config_name;
lukas.durfina's avatar
lukas.durfina committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    if( !psz_audio_output )
        return NULL;
    if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
        return NULL;

    module_config_t *p_module_config = config_FindConfig(
        VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );

    if( p_module_config )
    {
        // refresh if there arent devices
        if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
        {
            vlc_value_t val;
            val.psz_string = strdup( p_module_config->value.psz );

            p_module_config->pf_update_list(
                VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
            free( val.psz_string );
        }
        free( psz_config_name );

        if( i_device >= 0 && i_device < p_module_config->i_list )
            return strdup( p_module_config->ppsz_list[i_device] );

    }

257
    free( psz_config_name );
lukas.durfina's avatar
lukas.durfina committed
258
259
260
261
262
263
    return NULL;
}

/*****************************
 * Set device for using
 *****************************/
264
void libvlc_audio_output_device_set( libvlc_instance_t *p_instance,
lukas.durfina's avatar
lukas.durfina committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
                                                    const char *psz_audio_output,
                                                    const char *psz_device_id )
{
    char *psz_config_name = NULL;
    if( !psz_audio_output || !psz_device_id )
        return;
    if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
        return;
    config_PutPsz( p_instance->p_libvlc_int, psz_config_name, psz_device_id );
    free( psz_config_name );
}

/*****************************************************************************
 * libvlc_audio_output_get_device_type : Get the current audio device type
 *****************************************************************************/
int libvlc_audio_output_get_device_type( libvlc_instance_t *p_instance,
                                         libvlc_exception_t *p_e )
{
    aout_instance_t *p_aout = GetAOut( p_instance, p_e );
    if( p_aout )
    {
286
        int i_device_type = var_GetInteger( p_aout, "audio-device" );
lukas.durfina's avatar
lukas.durfina committed
287
        vlc_object_release( p_aout );
288
        return i_device_type;
lukas.durfina's avatar
lukas.durfina committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    }
    libvlc_exception_raise( p_e, "Unable to get audio output" );
    return libvlc_AudioOutputDevice_Error;
}

/*****************************************************************************
 * libvlc_audio_output_set_device_type : Set the audio device type
 *****************************************************************************/
void libvlc_audio_output_set_device_type( libvlc_instance_t *p_instance,
                                          int device_type,
                                          libvlc_exception_t *p_e )
{
    aout_instance_t *p_aout = GetAOut( p_instance, p_e );
    if( p_aout )
    {
ivoire's avatar
ivoire committed
304
        if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
lukas.durfina's avatar
lukas.durfina committed
305
306
307
308
            libvlc_exception_raise( p_e, "Failed setting audio device" );
        vlc_object_release( p_aout );
    }
}
309

littlejohn's avatar
littlejohn committed
310
311
312
/*****************************************************************************
 * libvlc_audio_get_mute : Get the volume state, true if muted
 *****************************************************************************/
zorglub's avatar
zorglub committed
313
void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
314
                               libvlc_exception_t *p_e )
zorglub's avatar
zorglub committed
315
{
316
317
    VLC_UNUSED(p_e);

318
    aout_ToggleMute( p_instance->p_libvlc_int, NULL );
zorglub's avatar
zorglub committed
319
320
}

321
322
int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
                           libvlc_exception_t *p_e )
littlejohn's avatar
littlejohn committed
323
{
ivoire's avatar
ivoire committed
324
    return (libvlc_audio_get_volume(p_instance, p_e) == 0);
littlejohn's avatar
littlejohn committed
325
326
}

327
void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
328
                            libvlc_exception_t *p_e )
littlejohn's avatar
littlejohn committed
329
{
330
    if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
littlejohn's avatar
littlejohn committed
331
    {
332
        aout_ToggleMute( p_instance->p_libvlc_int, NULL );
zorglub's avatar
zorglub committed
333
    }
littlejohn's avatar
littlejohn committed
334
335
336
}

/*****************************************************************************
zorglub's avatar
zorglub committed
337
 * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
littlejohn's avatar
littlejohn committed
338
 *****************************************************************************/
zorglub's avatar
zorglub committed
339
int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
340
                             libvlc_exception_t *p_e )
littlejohn's avatar
littlejohn committed
341
{
342
343
    VLC_UNUSED(p_e);

littlejohn's avatar
littlejohn committed
344
345
    audio_volume_t i_volume;

346
    aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
littlejohn's avatar
littlejohn committed
347

348
    return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
littlejohn's avatar
littlejohn committed
349
350
351
352
}


/*****************************************************************************
zorglub's avatar
zorglub committed
353
 * libvlc_audio_set_volume : Set the current volume
littlejohn's avatar
littlejohn committed
354
 *****************************************************************************/
zorglub's avatar
zorglub committed
355
void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
356
                              libvlc_exception_t *p_e )
littlejohn's avatar
littlejohn committed
357
358
359
{
    if( i_volume >= 0 && i_volume <= 200 )
    {
360
361
        i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;

362
        aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
littlejohn's avatar
littlejohn committed
363
    }
zorglub's avatar
zorglub committed
364
    else
littlejohn's avatar
littlejohn committed
365
    {
366
        libvlc_exception_raise( p_e, "Volume out of range" );
zorglub's avatar
zorglub committed
367
    }
littlejohn's avatar
littlejohn committed
368
369
}

370
371
372
/*****************************************************************************
 * libvlc_audio_get_track_count : Get the number of available audio tracks
 *****************************************************************************/
373
int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
374
375
376
                                  libvlc_exception_t *p_e )
{
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
377
    int i_track_count;
378
379
380
381

    if( !p_input_thread )
        return -1;

382
    i_track_count = var_CountChoices( p_input_thread, "audio-es" );
383

384
    vlc_object_release( p_input_thread );
385
    return i_track_count;
386
387
}

388
389
390
391
392
393
394
395
396
397
/*****************************************************************************
 * libvlc_audio_get_track_description : Get the description of available audio tracks
 *****************************************************************************/
libvlc_track_description_t *
        libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
                                            libvlc_exception_t *p_e )
{
    return libvlc_get_track_description( p_mi, "audio-es", p_e);
}

398
399
400
/*****************************************************************************
 * libvlc_audio_get_track : Get the current audio track
 *****************************************************************************/
401
int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
402
403
                            libvlc_exception_t *p_e )
{
404
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
405
406
407
408
409
    vlc_value_t val_list;
    vlc_value_t val;
    int i_track = -1;
    int i_ret = -1;
    int i;
410

411
412
413
    if( !p_input_thread )
        return -1;

414
415
416
417
418
419
420
    i_ret = var_Get( p_input_thread, "audio-es", &val );
    if( i_ret < 0 )
    {
        libvlc_exception_raise( p_e, "Getting Audio track information failed" );
        vlc_object_release( p_input_thread );
        return i_ret;
    }
421

422
423
424
    var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
    for( i = 0; i < val_list.p_list->i_count; i++ )
    {
425
        if( val_list.p_list->p_values[i].i_int == val.i_int )
426
427
428
        {
            i_track = i;
            break;
429
        }
430
    }
431
    var_FreeList( &val_list, NULL );
432
    vlc_object_release( p_input_thread );
433
434
435
436
437
438
    return i_track;
}

/*****************************************************************************
 * libvlc_audio_set_track : Set the current audio track
 *****************************************************************************/
439
void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
440
441
                             libvlc_exception_t *p_e )
{
442
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
443
    vlc_value_t val_list;
444
    vlc_value_t newval;
445
446
    int i_ret = -1;

447
448
449
    if( !p_input_thread )
        return;

450
    var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
ivoire's avatar
ivoire committed
451
    if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
452
    {
453
        libvlc_exception_raise( p_e, "Audio track out of range" );
454
        goto end;
455
456
457
458
459
460
    }

    newval = val_list.p_list->p_values[i_track];
    i_ret = var_Set( p_input_thread, "audio-es", newval );
    if( i_ret < 0 )
        libvlc_exception_raise( p_e, "Setting audio track failed" );
461
462

end:
463
    var_FreeList( &val_list, NULL );
464
    vlc_object_release( p_input_thread );
465
466
467
468
469
}

/*****************************************************************************
 * libvlc_audio_get_channel : Get the current audio channel
 *****************************************************************************/
470
int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
lukas.durfina's avatar
lukas.durfina committed
471
                              libvlc_exception_t *p_e )
472
{
473
    aout_instance_t *p_aout = GetAOut( p_instance, p_e );
474
475
476
    if( p_aout )
    {
        vlc_value_t val;
477

478
479
480
481
        var_Get( p_aout, "audio-channels", &val );
        vlc_object_release( p_aout );
        return val.i_int;
    }
lukas.durfina's avatar
lukas.durfina committed
482
483
    libvlc_exception_raise( p_e, "Unable to get audio output" );
    return libvlc_AudioChannel_Error;
484
485
486
487
488
}

/*****************************************************************************
 * libvlc_audio_set_channel : Set the current audio channel
 *****************************************************************************/
lukas.durfina's avatar
lukas.durfina committed
489
490
void libvlc_audio_set_channel( libvlc_instance_t *p_instance,
                               int channel,
491
492
                               libvlc_exception_t *p_e )
{
493
    aout_instance_t *p_aout = GetAOut( p_instance, p_e );
494
    if( p_aout )
495
    {
496
497
        vlc_value_t val;

lukas.durfina's avatar
lukas.durfina committed
498
        val.i_int = channel;
499
        if( var_Set( p_aout, "audio-channels", val ) < 0 )
lukas.durfina's avatar
lukas.durfina committed
500
501
            libvlc_exception_raise( p_e, "Failed setting audio channel" );

502
        vlc_object_release( p_aout );
503
504
    }
}