waveout.c 27.7 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * waveout.c : Windows waveOut plugin for vlc
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Gildas Bazin <gbazin@videolan.org>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *      
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <string.h>                                            /* strerror() */
#include <stdlib.h>                            /* calloc(), malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/aout.h>
gbazin's avatar
   
gbazin committed
32
#include "aout_internal.h"
33

gbazin's avatar
   
gbazin committed
34
#include <windows.h>
35
36
#include <mmsystem.h>

gbazin's avatar
   
gbazin committed
37
#define FRAME_SIZE 1024              /* The size is in samples, not in bytes */
gbazin's avatar
   
gbazin committed
38
#define FRAMES_NUM 8
gbazin's avatar
   
gbazin committed
39
40
41
42

/*****************************************************************************
 * Useful macros
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
43
44
45
46
#ifdef UNDER_CE
#   define DWORD_PTR DWORD
#endif

gbazin's avatar
   
gbazin committed
47
48
49
#ifndef WAVE_FORMAT_IEEE_FLOAT
#   define WAVE_FORMAT_IEEE_FLOAT 0x0003
#endif
gbazin's avatar
   
gbazin committed
50

gbazin's avatar
   
gbazin committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
#ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
#   define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
#endif

#ifndef WAVE_FORMAT_EXTENSIBLE
#define  WAVE_FORMAT_EXTENSIBLE   0xFFFE
#endif

#ifndef SPEAKER_FRONT_LEFT
#   define SPEAKER_FRONT_LEFT             0x1
#   define SPEAKER_FRONT_RIGHT            0x2
#   define SPEAKER_FRONT_CENTER           0x4
#   define SPEAKER_LOW_FREQUENCY          0x8
#   define SPEAKER_BACK_LEFT              0x10
#   define SPEAKER_BACK_RIGHT             0x20
#   define SPEAKER_FRONT_LEFT_OF_CENTER   0x40
#   define SPEAKER_FRONT_RIGHT_OF_CENTER  0x80
#   define SPEAKER_BACK_CENTER            0x100
#   define SPEAKER_SIDE_LEFT              0x200
#   define SPEAKER_SIDE_RIGHT             0x400
#   define SPEAKER_TOP_CENTER             0x800
#   define SPEAKER_TOP_FRONT_LEFT         0x1000
#   define SPEAKER_TOP_FRONT_CENTER       0x2000
#   define SPEAKER_TOP_FRONT_RIGHT        0x4000
#   define SPEAKER_TOP_BACK_LEFT          0x8000
#   define SPEAKER_TOP_BACK_CENTER        0x10000
#   define SPEAKER_TOP_BACK_RIGHT         0x20000
#   define SPEAKER_RESERVED               0x80000000
#endif

#ifndef _WAVEFORMATEXTENSIBLE_
typedef struct {
    WAVEFORMATEX    Format;
    union {
        WORD wValidBitsPerSample;       /* bits of precision  */
        WORD wSamplesPerBlock;          /* valid if wBitsPerSample==0 */
        WORD wReserved;                 /* If neither applies, set to zero. */
    } Samples;
    DWORD           dwChannelMask;      /* which channels are */
                                        /* present in stream  */
    GUID            SubFormat;
} WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
#endif

95
96
97
static const GUID __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = {WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
static const GUID __KSDATAFORMAT_SUBTYPE_PCM = {WAVE_FORMAT_PCM, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
static const GUID __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF = {WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
gbazin's avatar
   
gbazin committed
98

99
100
101
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
102
103
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );
104
static void Play         ( aout_instance_t * );
105

gbazin's avatar
   
gbazin committed
106
107
108
109
110
111
112
113
114
115
/*****************************************************************************
 * notification_thread_t: waveOut event thread
 *****************************************************************************/
typedef struct notification_thread_t
{
    VLC_COMMON_MEMBERS
    aout_instance_t *p_aout;

} notification_thread_t;

116
/* local functions */
gbazin's avatar
   
gbazin committed
117
118
119
static void Probe        ( aout_instance_t * );
static int OpenWaveOut   ( aout_instance_t *, int, int, int, int, vlc_bool_t );
static int OpenWaveOutPCM( aout_instance_t *, int*, int, int, int, vlc_bool_t );
gbazin's avatar
   
gbazin committed
120
121
static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
                           aout_buffer_t * );
gbazin's avatar
   
gbazin committed
122
123

static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
gbazin's avatar
   
gbazin committed
124
static void WaveOutThread( notification_thread_t * );
gbazin's avatar
   
gbazin committed
125

126
127
128
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
129
130
131
132
133
#define FLOAT_TEXT N_("Use float32 output")
#define FLOAT_LONGTEXT N_( \
    "The option allows you to enable or disable the high-quality float32 " \
    "audio output mode (which is not well supported by some soundcards)." )

134
vlc_module_begin();
135
    set_description( _("Win32 waveOut extension output") );
gbazin's avatar
   
gbazin committed
136
    set_capability( "audio output", 50 );
gbazin's avatar
gbazin committed
137
    add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, VLC_TRUE );
138
139
140
141
142
143
144
145
146
147
148
149
150
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * aout_sys_t: waveOut audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the waveOut specific properties of an audio device.
 *****************************************************************************/
struct aout_sys_t
{
    HWAVEOUT h_waveout;                        /* handle to waveout instance */

gbazin's avatar
   
gbazin committed
151
    WAVEFORMATEXTENSIBLE waveformat;                         /* audio format */
152

gbazin's avatar
   
gbazin committed
153
    WAVEHDR waveheader[FRAMES_NUM];
154

gbazin's avatar
   
gbazin committed
155
156
157
    notification_thread_t *p_notif;                      /* WaveOutThread id */
    HANDLE event;

gbazin's avatar
   
gbazin committed
158
    int i_buffer_size;
159

gbazin's avatar
   
gbazin committed
160
    byte_t *p_silence_buffer;               /* buffer we use to play silence */
gbazin's avatar
   
gbazin committed
161
162

    vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
gbazin's avatar
gbazin committed
163
    int pi_chan_table[AOUT_CHAN_MAX];
164
165
};

gbazin's avatar
gbazin committed
166
static const uint32_t pi_channels_src[] =
gbazin's avatar
   
gbazin committed
167
    { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
gbazin's avatar
gbazin committed
168
      AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
gbazin's avatar
   
gbazin committed
169
      AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
gbazin's avatar
gbazin committed
170
171
172
173
174
175
      AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
static const uint32_t pi_channels_in[] =
    { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
      SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
      SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
      SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
gbazin's avatar
   
gbazin committed
176
177
static const uint32_t pi_channels_out[] =
    { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
gbazin's avatar
gbazin committed
178
      SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
gbazin's avatar
   
gbazin committed
179
      SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
gbazin's avatar
gbazin committed
180
      SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
gbazin's avatar
   
gbazin committed
181

182
183
184
185
186
187
/*****************************************************************************
 * Open: open the audio device
 *****************************************************************************
 * This function opens and setups Win32 waveOut
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
gbazin's avatar
   
gbazin committed
188
{
gbazin's avatar
   
gbazin committed
189
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
gbazin's avatar
   
gbazin committed
190
191
    vlc_value_t val;
    int i;
192
193

    /* Allocate structure */
gbazin's avatar
   
gbazin committed
194
    p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
195

gbazin's avatar
   
gbazin committed
196
    if( p_aout->output.p_sys == NULL )
197
198
    {
        msg_Err( p_aout, "out of memory" );
199
        return VLC_EGENERIC;
200
201
    }

gbazin's avatar
   
gbazin committed
202
    p_aout->output.pf_play = Play;
gbazin's avatar
   
gbazin committed
203
    p_aout->b_die = VLC_FALSE;
204

gbazin's avatar
   
gbazin committed
205
    if( var_Type( p_aout, "audio-device" ) == 0 )
gbazin's avatar
   
gbazin committed
206
    {
gbazin's avatar
   
gbazin committed
207
        Probe( p_aout );
gbazin's avatar
   
gbazin committed
208
    }
gbazin's avatar
   
gbazin committed
209
210

    if( var_Get( p_aout, "audio-device", &val ) < 0 )
gbazin's avatar
   
gbazin committed
211
    {
gbazin's avatar
   
gbazin committed
212
213
214
        /* Probe() has failed. */
        free( p_aout->output.p_sys );
        return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
215
216
    }

217
218
    var_Create( p_aout, "waveout-float32", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

gbazin's avatar
   
gbazin committed
219
    /* Open the device */
gbazin's avatar
   
gbazin committed
220
    if( val.i_int == AOUT_VAR_SPDIF )
gbazin's avatar
   
gbazin committed
221
    {
gbazin's avatar
   
gbazin committed
222
223
224
225
226
227
228
229
230
231
232
233
        p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');

        if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
                         p_aout->output.output.i_physical_channels,
                         aout_FormatNbChannels( &p_aout->output.output ),
                         p_aout->output.output.i_rate, VLC_FALSE )
            != VLC_SUCCESS )
        {
            msg_Err( p_aout, "cannot open waveout audio device" );
            free( p_aout->output.p_sys );
            return VLC_EGENERIC;
        }
gbazin's avatar
   
gbazin committed
234
235
236
237
238
239
240
241
242

        /* Calculate the frame size in bytes */
        p_aout->output.i_nb_samples = A52_FRAME_NB;
        p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
        p_aout->output.output.i_frame_length = A52_FRAME_NB;
        p_aout->output.p_sys->i_buffer_size =
            p_aout->output.output.i_bytes_per_frame;

        aout_VolumeNoneInit( p_aout );
gbazin's avatar
   
gbazin committed
243
244
245
    }
    else
    {
gbazin's avatar
   
gbazin committed
246
        if( val.i_int == AOUT_VAR_5_1 )
gbazin's avatar
   
gbazin committed
247
248
249
250
251
252
        {
            p_aout->output.output.i_physical_channels
                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
                   | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
                   | AOUT_CHAN_LFE;
        }
gbazin's avatar
   
gbazin committed
253
        else if( val.i_int == AOUT_VAR_2F2R )
gbazin's avatar
   
gbazin committed
254
255
256
257
258
        {
            p_aout->output.output.i_physical_channels
                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
                   | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
        }
gbazin's avatar
   
gbazin committed
259
        else if( val.i_int == AOUT_VAR_MONO )
gbazin's avatar
   
gbazin committed
260
261
262
263
264
265
266
267
        {
            p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
        }
        else
        {
            p_aout->output.output.i_physical_channels
                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
        }
gbazin's avatar
   
gbazin committed
268

gbazin's avatar
   
gbazin committed
269
270
271
272
273
        if( OpenWaveOutPCM( p_aout, &p_aout->output.output.i_format,
                            p_aout->output.output.i_physical_channels,
                            aout_FormatNbChannels( &p_aout->output.output ),
                            p_aout->output.output.i_rate, VLC_FALSE )
            != VLC_SUCCESS )
gbazin's avatar
   
gbazin committed
274
        {
gbazin's avatar
   
gbazin committed
275
            msg_Err( p_aout, "cannot open waveout audio device" );
gbazin's avatar
   
gbazin committed
276
            free( p_aout->output.p_sys );
277
            return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
278
        }
gbazin's avatar
   
gbazin committed
279
280
281
282
283
284
285
286

        /* Calculate the frame size in bytes */
        p_aout->output.i_nb_samples = FRAME_SIZE;
        aout_FormatPrepare( &p_aout->output.output );
        p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
            p_aout->output.output.i_bytes_per_frame;

        aout_VolumeSoftInit( p_aout );
gbazin's avatar
   
gbazin committed
287
288
    }

gbazin's avatar
   
gbazin committed
289

gbazin's avatar
   
gbazin committed
290
291
    waveOutReset( p_aout->output.p_sys->h_waveout );

gbazin's avatar
   
gbazin committed
292
    /* Allocate silence buffer */
293
294
    p_aout->output.p_sys->p_silence_buffer =
        malloc( p_aout->output.p_sys->i_buffer_size );
gbazin's avatar
   
gbazin committed
295
296
    if( p_aout->output.p_sys->p_silence_buffer == NULL )
    {
gbazin's avatar
   
gbazin committed
297
        free( p_aout->output.p_sys );
gbazin's avatar
   
gbazin committed
298
299
300
        msg_Err( p_aout, "out of memory" );
        return 1;
    }
gbazin's avatar
   
gbazin committed
301
302

    /* Zero the buffer. WinCE doesn't have calloc(). */
303
304
    memset( p_aout->output.p_sys->p_silence_buffer, 0,
            p_aout->output.p_sys->i_buffer_size );
305

gbazin's avatar
   
gbazin committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    /* Now we need to setup our waveOut play notification structure */
    p_aout->output.p_sys->p_notif =
        vlc_object_create( p_aout, sizeof(notification_thread_t) );
    p_aout->output.p_sys->p_notif->p_aout = p_aout;
    p_aout->output.p_sys->event = CreateEvent( NULL, FALSE, FALSE, NULL );

    /* Then launch the notification thread */
    if( vlc_thread_create( p_aout->output.p_sys->p_notif,
                           "waveOut Notification Thread", WaveOutThread,
                           VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
    {
        msg_Err( p_aout, "cannot create WaveOutThread" );
    }

gbazin's avatar
   
gbazin committed
320
321
    /* We need to kick off the playback in order to have the callback properly
     * working */
gbazin's avatar
   
gbazin committed
322
323
    for( i = 0; i < FRAMES_NUM; i++ )
    {
gbazin's avatar
   
gbazin committed
324
        p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
gbazin's avatar
gbazin committed
325
        p_aout->output.p_sys->waveheader[i].dwUser = 0;
gbazin's avatar
   
gbazin committed
326
    }
gbazin's avatar
   
gbazin committed
327
328
329
    PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
                 &p_aout->output.p_sys->waveheader[0], NULL );

gbazin's avatar
   
gbazin committed
330
    return 0;
331
332
}

gbazin's avatar
   
gbazin committed
333
334
335
336
337
/*****************************************************************************
 * Probe: probe the audio device for available formats and channels
 *****************************************************************************/
static void Probe( aout_instance_t * p_aout )
{
gbazin's avatar
   
gbazin committed
338
    vlc_value_t val, text;
gbazin's avatar
   
gbazin committed
339
340
341
    int i_format;
    unsigned int i_physical_channels;

gbazin's avatar
   
gbazin committed
342
    var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
343
    text.psz_string = _("Audio Device");
gbazin's avatar
   
gbazin committed
344
    var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
gbazin's avatar
   
gbazin committed
345
346
347
348
349
350
351
352
353
354
355
356

    /* Test for 5.1 support */
    i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
                          AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
                          AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
    if( p_aout->output.output.i_physical_channels == i_physical_channels )
    {
        if( OpenWaveOutPCM( p_aout, &i_format,
                            i_physical_channels, 6,
                            p_aout->output.output.i_rate, VLC_TRUE )
            == VLC_SUCCESS )
        {
gbazin's avatar
   
gbazin committed
357
358
359
360
            val.i_int = AOUT_VAR_5_1;
            text.psz_string = N_("5.1");
            var_Change( p_aout, "audio-device",
                        VLC_VAR_ADDCHOICE, &val, &text );
gbazin's avatar
   
gbazin committed
361
            msg_Dbg( p_aout, "device supports 5.1 channels" );
gbazin's avatar
   
gbazin committed
362
        }
gbazin's avatar
   
gbazin committed
363
364
365
366
367
    }

    /* Test for 2 Front 2 Rear support */
    i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
                          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
gbazin's avatar
   
gbazin committed
368
369
    if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
        == i_physical_channels )
gbazin's avatar
   
gbazin committed
370
371
372
373
374
375
    {
        if( OpenWaveOutPCM( p_aout, &i_format,
                            i_physical_channels, 4,
                            p_aout->output.output.i_rate, VLC_TRUE )
            == VLC_SUCCESS )
        {
gbazin's avatar
   
gbazin committed
376
377
378
379
            val.i_int = AOUT_VAR_2F2R;
            text.psz_string = N_("2 Front 2 Rear");
            var_Change( p_aout, "audio-device",
                        VLC_VAR_ADDCHOICE, &val, &text );
gbazin's avatar
   
gbazin committed
380
            msg_Dbg( p_aout, "device supports 4 channels" );
gbazin's avatar
   
gbazin committed
381
        }
gbazin's avatar
   
gbazin committed
382
383
384
385
386
387
388
389
390
    }

    /* Test for stereo support */
    i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
    if( OpenWaveOutPCM( p_aout, &i_format,
                        i_physical_channels, 2,
                        p_aout->output.output.i_rate, VLC_TRUE )
        == VLC_SUCCESS )
    {
gbazin's avatar
   
gbazin committed
391
392
393
        val.i_int = AOUT_VAR_STEREO;
        text.psz_string = N_("Stereo");
        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
gbazin's avatar
   
gbazin committed
394
        msg_Dbg( p_aout, "device supports 2 channels" );
gbazin's avatar
   
gbazin committed
395
    }
gbazin's avatar
   
gbazin committed
396
397
398
399
400
401
402
403

    /* Test for mono support */
    i_physical_channels = AOUT_CHAN_CENTER;
    if( OpenWaveOutPCM( p_aout, &i_format,
                        i_physical_channels, 1,
                        p_aout->output.output.i_rate, VLC_TRUE )
        == VLC_SUCCESS )
    {
gbazin's avatar
   
gbazin committed
404
405
406
        val.i_int = AOUT_VAR_MONO;
        text.psz_string = N_("Mono");
        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
gbazin's avatar
   
gbazin committed
407
        msg_Dbg( p_aout, "device supports 1 channel" );
gbazin's avatar
   
gbazin committed
408
    }
gbazin's avatar
   
gbazin committed
409

gbazin's avatar
   
gbazin committed
410
411
412
413
414
415
416
417
418
419
    /* Test for SPDIF support */
    if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
    {
        if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
                         p_aout->output.output.i_physical_channels,
                         aout_FormatNbChannels( &p_aout->output.output ),
                         p_aout->output.output.i_rate, VLC_TRUE )
            == VLC_SUCCESS )
        {
            msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
gbazin's avatar
   
gbazin committed
420
421
422
423
            val.i_int = AOUT_VAR_SPDIF;
            text.psz_string = N_("A/52 over S/PDIF");
            var_Change( p_aout, "audio-device",
                        VLC_VAR_ADDCHOICE, &val, &text );
gbazin's avatar
   
gbazin committed
424
425
426
427
428
            if( config_GetInt( p_aout, "spdif" ) )
                var_Set( p_aout, "audio-device", val );
        }
    }

gbazin's avatar
   
gbazin committed
429
430
431
432
433
434
435
436
    var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
    if( val.i_int <= 0 )
    {
        /* Probe() has failed. */
        var_Destroy( p_aout, "audio-device" );
        return;
    }

gbazin's avatar
   
gbazin committed
437
438
439
440
441
442
    var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );

    val.b_bool = VLC_TRUE;
    var_Set( p_aout, "intf-change", val );
}

443
444
445
/*****************************************************************************
 * Play: play a sound buffer
 *****************************************************************************
gbazin's avatar
   
gbazin committed
446
447
 * This doesn't actually play the buffer. This just stores the buffer so it
 * can be played by the callback thread.
448
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
449
static void Play( aout_instance_t *_p_aout )
450
451
452
453
454
455
456
{
}

/*****************************************************************************
 * Close: close the audio device
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
gbazin's avatar
   
gbazin committed
457
{
gbazin's avatar
   
gbazin committed
458
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
gbazin's avatar
gbazin committed
459
    aout_sys_t *p_sys = p_aout->output.p_sys;
460
461

    /* Before calling waveOutClose we must reset the device */
gbazin's avatar
   
gbazin committed
462
463
    p_aout->b_die = VLC_TRUE;

gbazin's avatar
gbazin committed
464
    waveOutReset( p_sys->h_waveout );
465

gbazin's avatar
gbazin committed
466
467
468
469
470
    /* wake up the audio thread */
    SetEvent( p_sys->event );
    vlc_thread_join( p_sys->p_notif );
    vlc_object_destroy( p_sys->p_notif );
    CloseHandle( p_sys->event );
gbazin's avatar
   
gbazin committed
471

472
    /* Close the device */
gbazin's avatar
gbazin committed
473
    if( waveOutClose( p_sys->h_waveout ) != MMSYSERR_NOERROR )
474
475
476
477
    {
        msg_Err( p_aout, "waveOutClose failed" );
    }

gbazin's avatar
gbazin committed
478
479
    free( p_sys->p_silence_buffer );
    free( p_sys );
480
481
482
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
483
 * OpenWaveOut: open the waveout sound device
484
 ****************************************************************************/
gbazin's avatar
   
gbazin committed
485
static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
gbazin's avatar
   
gbazin committed
486
487
                        int i_channels, int i_nb_channels, int i_rate,
                        vlc_bool_t b_probe )
488
489
{
    MMRESULT result;
gbazin's avatar
   
gbazin committed
490
    unsigned int i;
491
492

    /* Set sound format */
gbazin's avatar
   
gbazin committed
493
494
495
496

#define waveformat p_aout->output.p_sys->waveformat

    waveformat.dwChannelMask = 0;
497
    for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
gbazin's avatar
   
gbazin committed
498
    {
499
500
        if( i_channels & pi_channels_src[i] )
            waveformat.dwChannelMask |= pi_channels_in[i];
gbazin's avatar
   
gbazin committed
501
    }
gbazin's avatar
   
gbazin committed
502
503
504

    switch( i_format )
    {
gbazin's avatar
   
gbazin committed
505
506
    case VLC_FOURCC('s','p','d','i'):
        i_nb_channels = 2;
gbazin's avatar
   
gbazin committed
507
508
        /* To prevent channel re-ordering */
        waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
gbazin's avatar
   
gbazin committed
509
510
511
512
        waveformat.Format.wBitsPerSample = 16;
        waveformat.Samples.wValidBitsPerSample =
            waveformat.Format.wBitsPerSample;
        waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
gbazin's avatar
   
gbazin committed
513
        waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
gbazin's avatar
   
gbazin committed
514
515
516
517
518
519
520
        break;

    case VLC_FOURCC('f','l','3','2'):
        waveformat.Format.wBitsPerSample = sizeof(float) * 8;
        waveformat.Samples.wValidBitsPerSample =
            waveformat.Format.wBitsPerSample;
        waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
gbazin's avatar
   
gbazin committed
521
        waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
gbazin's avatar
   
gbazin committed
522
        break;
gbazin's avatar
   
gbazin committed
523
524
525
526
527
528

    case VLC_FOURCC('s','1','6','l'):
        waveformat.Format.wBitsPerSample = 16;
        waveformat.Samples.wValidBitsPerSample =
            waveformat.Format.wBitsPerSample;
        waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
gbazin's avatar
   
gbazin committed
529
        waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_PCM;
gbazin's avatar
   
gbazin committed
530
531
532
        break;
    }

gbazin's avatar
   
gbazin committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
    waveformat.Format.nChannels = i_nb_channels;
    waveformat.Format.nSamplesPerSec = i_rate;
    waveformat.Format.nBlockAlign =
        waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
    waveformat.Format.nAvgBytesPerSec =
        waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;

    /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
    if( i_nb_channels <= 2 )
    {
        waveformat.Format.cbSize = 0;
    }
    else
    {
        waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
        waveformat.Format.cbSize =
            sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
    }
551
552

    /* Open the device */
gbazin's avatar
   
gbazin committed
553
    result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
gbazin's avatar
   
gbazin committed
554
                          (WAVEFORMATEX *)&waveformat,
gbazin's avatar
   
gbazin committed
555
                          (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
gbazin's avatar
   
gbazin committed
556
                          CALLBACK_FUNCTION | (b_probe?WAVE_FORMAT_QUERY:0) );
gbazin's avatar
   
gbazin committed
557
558
    if( result == WAVERR_BADFORMAT )
    {
gbazin's avatar
   
gbazin committed
559
        msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
gbazin's avatar
   
gbazin committed
560
561
562
563
564
565
        return VLC_EGENERIC;
    }
    if( result == MMSYSERR_ALLOCATED )
    {
        msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
        return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
566
    }
567
568
    if( result != MMSYSERR_NOERROR )
    {
gbazin's avatar
   
gbazin committed
569
        msg_Warn( p_aout, "waveOutOpen failed" );
gbazin's avatar
   
gbazin committed
570
        return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
571
572
    }

gbazin's avatar
gbazin committed
573
574
575
576
577
578
579
580
581
    p_aout->output.p_sys->b_chan_reorder =
        aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
                                  waveformat.dwChannelMask, i_nb_channels,
                                  p_aout->output.p_sys->pi_chan_table );

    if( p_aout->output.p_sys->b_chan_reorder )
    {
        msg_Dbg( p_aout, "channel reordering needed" );
    }
gbazin's avatar
   
gbazin committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595

    return VLC_SUCCESS;

#undef waveformat

}

/*****************************************************************************
 * OpenWaveOutPCM: open a PCM waveout sound device
 ****************************************************************************/
static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
                           int i_channels, int i_nb_channels, int i_rate,
                           vlc_bool_t b_probe )
{
596
597
598
599
600
601
    vlc_value_t val;

    var_Get( p_aout, "waveout-float32", &val );

    if( !val.b_bool || OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
                                   i_channels, i_nb_channels, i_rate, b_probe )
gbazin's avatar
   
gbazin committed
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
        != VLC_SUCCESS )
    {
        if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
                          i_channels, i_nb_channels, i_rate, b_probe )
             != VLC_SUCCESS )
        {
            return VLC_EGENERIC;
        }
        else
        {
            *i_format = VLC_FOURCC('s','1','6','l');
            return VLC_SUCCESS;
        }
    }
    else
    {
        *i_format = VLC_FOURCC('f','l','3','2');
        return VLC_SUCCESS;
    }
}

gbazin's avatar
   
gbazin committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*****************************************************************************
 * PlayWaveOut: play a buffer through the WaveOut device
 *****************************************************************************/
static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
                        WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
{
    MMRESULT result;

    /* Prepare the buffer */
    if( p_buffer != NULL )
        p_waveheader->lpData = p_buffer->p_buffer;
    else
        /* Use silence buffer instead */
        p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;

gbazin's avatar
gbazin committed
638
    p_waveheader->dwUser = p_buffer ? (DWORD_PTR)p_buffer : (DWORD_PTR)1;
gbazin's avatar
   
gbazin committed
639
640
641
642
643
644
645
    p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
    p_waveheader->dwFlags = 0;

    result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
    if( result != MMSYSERR_NOERROR )
    {
        msg_Err( p_aout, "waveOutPrepareHeader failed" );
gbazin's avatar
   
gbazin committed
646
        return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
647
648
649
650
651
652
653
    }

    /* Send the buffer to the waveOut queue */
    result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
    if( result != MMSYSERR_NOERROR )
    {
        msg_Err( p_aout, "waveOutWrite failed" );
gbazin's avatar
   
gbazin committed
654
        return VLC_EGENERIC;
655
656
    }

gbazin's avatar
   
gbazin committed
657
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
658
659
660
661
662
663
664
665
666
}

/*****************************************************************************
 * WaveOutCallback: what to do once WaveOut has played its sound samples
 *****************************************************************************/
static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
                                      DWORD _p_aout,
                                      DWORD dwParam1, DWORD dwParam2 )
{
gbazin's avatar
   
gbazin committed
667
    aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
gbazin's avatar
   
gbazin committed
668
    int i, i_queued_frames = 0;
gbazin's avatar
   
gbazin committed
669
670
671

    if( uMsg != WOM_DONE ) return;

gbazin's avatar
   
gbazin committed
672
673
    if( p_aout->b_die ) return;

gbazin's avatar
   
gbazin committed
674
675
    /* Find out the current latency */
    for( i = 0; i < FRAMES_NUM; i++ )
gbazin's avatar
   
gbazin committed
676
    {
gbazin's avatar
   
gbazin committed
677
678
679
680
681
        /* Check if frame buf is available */
        if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
        {
            i_queued_frames++;
        }
gbazin's avatar
   
gbazin committed
682
683
    }

gbazin's avatar
   
gbazin committed
684
685
686
    /* Don't wake up the thread too much */
    if( i_queued_frames < FRAMES_NUM / 2 )
        SetEvent( p_aout->output.p_sys->event );
687
}
gbazin's avatar
   
gbazin committed
688

gbazin's avatar
   
gbazin committed
689
690
691
692
693
694
695
696
697
698
/*****************************************************************************
 * WaveOutThread: this thread will capture play notification events. 
 *****************************************************************************
 * We use this thread to feed new audio samples to the sound card because
 * we are not authorized to use waveOutWrite() directly in the waveout
 * callback.
 *****************************************************************************/
static void WaveOutThread( notification_thread_t *p_notif )
{
    aout_instance_t *p_aout = p_notif->p_aout;
gbazin's avatar
gbazin committed
699
    aout_sys_t *p_sys = p_aout->output.p_sys;
gbazin's avatar
   
gbazin committed
700
    aout_buffer_t *p_buffer = NULL;
gbazin's avatar
gbazin committed
701
    WAVEHDR *p_waveheader = p_sys->waveheader;
gbazin's avatar
   
gbazin committed
702
    int i, i_queued_frames;
gbazin's avatar
gbazin committed
703
    vlc_bool_t b_sleek;
gbazin's avatar
   
gbazin committed
704
705
706
707
708
709

    /* We don't want any resampling when using S/PDIF */
    b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');

    while( 1 )
    {
gbazin's avatar
gbazin committed
710
        WaitForSingleObject( p_sys->event, INFINITE );
gbazin's avatar
   
gbazin committed
711

gbazin's avatar
gbazin committed
712
        /* Cleanup and find out the current latency */
gbazin's avatar
   
gbazin committed
713
714
715
        i_queued_frames = 0;
        for( i = 0; i < FRAMES_NUM; i++ )
        {
gbazin's avatar
gbazin committed
716
717
718
719
720
721
722
723
724
725
726
727
728
            if( (p_waveheader[i].dwFlags & WHDR_DONE) &&
                p_waveheader[i].dwUser )
            {
                /* Unprepare and free the buffers which has just been played */
                waveOutUnprepareHeader( p_sys->h_waveout, &p_waveheader[i],
                                        sizeof(WAVEHDR) );

                if( p_waveheader[i].dwUser != 1 )
                    aout_BufferFree( (aout_buffer_t *)p_waveheader[i].dwUser );

                p_waveheader[i].dwUser = 0;
            }

gbazin's avatar
   
gbazin committed
729
            /* Check if frame buf is available */
gbazin's avatar
gbazin committed
730
            if( !(p_waveheader[i].dwFlags & WHDR_DONE) )
gbazin's avatar
   
gbazin committed
731
732
733
734
735
            {
                i_queued_frames++;
            }
        }

gbazin's avatar
gbazin committed
736
737
        if( p_aout->b_die ) return;

gbazin's avatar
   
gbazin committed
738
739
740
741
        /* Try to fill in as many frame buffers as possible */
        for( i = 0; i < FRAMES_NUM; i++ )
        {
            /* Check if frame buf is available */
gbazin's avatar
gbazin committed
742
            if( p_waveheader[i].dwFlags & WHDR_DONE )
gbazin's avatar
   
gbazin committed
743
744
745
746
747
748
749
750
751
752
753
754
755
            {
                /* Take into account the latency */
                p_buffer = aout_OutputNextBuffer( p_aout,
                    mdate() + 1000000 * i_queued_frames /
                    p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
                    b_sleek );

                if( !p_buffer && i_queued_frames )
                {
                    /* We aren't late so no need to play a blank sample */
                    break;
                }

gbazin's avatar
gbazin committed
756
757
                /* Do the channel reordering */
                if( p_buffer && p_sys->b_chan_reorder )
gbazin's avatar
   
gbazin committed
758
                {
gbazin's avatar
gbazin committed
759
760
761
762
763
                    aout_ChannelReorder( p_buffer->p_buffer,
                        p_buffer->i_nb_bytes,
                        p_sys->waveformat.Format.nChannels,
                        p_sys->pi_chan_table,
                        p_sys->waveformat.Format.wBitsPerSample );
gbazin's avatar
   
gbazin committed
764
765
                }

gbazin's avatar
gbazin committed
766
767
                PlayWaveOut( p_aout, p_sys->h_waveout,
                             &p_waveheader[i], p_buffer );
gbazin's avatar
   
gbazin committed
768
769
770
771
772
773

                i_queued_frames++;
            }
        }
    }
}