]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
faac4c4e80303f2c2ce326cf8b9331905e347f46
[vlc] / modules / audio_output / waveout.c
1 /*****************************************************************************
2  * waveout.c : Windows waveOut plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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
28 #include <vlc/vlc.h>
29 #include <vlc_aout.h>
30
31 #include <windows.h>
32 #include <mmsystem.h>
33
34 #define FRAME_SIZE 4096              /* The size is in samples, not in bytes */
35 #define FRAMES_NUM 8
36
37 /*****************************************************************************
38  * Useful macros
39  *****************************************************************************/
40 #ifdef UNDER_CE
41 #   define DWORD_PTR DWORD
42 #   ifdef waveOutGetDevCaps
43 #       undef waveOutGetDevCaps
44         MMRESULT WINAPI waveOutGetDevCaps(UINT, LPWAVEOUTCAPS, UINT);
45 #   endif
46 #endif
47
48 #ifndef WAVE_FORMAT_IEEE_FLOAT
49 #   define WAVE_FORMAT_IEEE_FLOAT 0x0003
50 #endif
51
52 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
53 #   define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
54 #endif
55
56 #ifndef WAVE_FORMAT_EXTENSIBLE
57 #define  WAVE_FORMAT_EXTENSIBLE   0xFFFE
58 #endif
59
60 #ifndef SPEAKER_FRONT_LEFT
61 #   define SPEAKER_FRONT_LEFT             0x1
62 #   define SPEAKER_FRONT_RIGHT            0x2
63 #   define SPEAKER_FRONT_CENTER           0x4
64 #   define SPEAKER_LOW_FREQUENCY          0x8
65 #   define SPEAKER_BACK_LEFT              0x10
66 #   define SPEAKER_BACK_RIGHT             0x20
67 #   define SPEAKER_FRONT_LEFT_OF_CENTER   0x40
68 #   define SPEAKER_FRONT_RIGHT_OF_CENTER  0x80
69 #   define SPEAKER_BACK_CENTER            0x100
70 #   define SPEAKER_SIDE_LEFT              0x200
71 #   define SPEAKER_SIDE_RIGHT             0x400
72 #   define SPEAKER_TOP_CENTER             0x800
73 #   define SPEAKER_TOP_FRONT_LEFT         0x1000
74 #   define SPEAKER_TOP_FRONT_CENTER       0x2000
75 #   define SPEAKER_TOP_FRONT_RIGHT        0x4000
76 #   define SPEAKER_TOP_BACK_LEFT          0x8000
77 #   define SPEAKER_TOP_BACK_CENTER        0x10000
78 #   define SPEAKER_TOP_BACK_RIGHT         0x20000
79 #   define SPEAKER_RESERVED               0x80000000
80 #endif
81
82 #ifndef _WAVEFORMATEXTENSIBLE_
83 typedef struct {
84     WAVEFORMATEX    Format;
85     union {
86         WORD wValidBitsPerSample;       /* bits of precision  */
87         WORD wSamplesPerBlock;          /* valid if wBitsPerSample==0 */
88         WORD wReserved;                 /* If neither applies, set to zero. */
89     } Samples;
90     DWORD           dwChannelMask;      /* which channels are */
91                                         /* present in stream  */
92     GUID            SubFormat;
93 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
94 #endif
95
96 static const GUID __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = {WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
97 static const GUID __KSDATAFORMAT_SUBTYPE_PCM = {WAVE_FORMAT_PCM, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 static const GUID __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF = {WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
99
100 /*****************************************************************************
101  * Local prototypes
102  *****************************************************************************/
103 static int  Open         ( vlc_object_t * );
104 static void Close        ( vlc_object_t * );
105 static void Play         ( aout_instance_t * );
106
107 /*****************************************************************************
108  * notification_thread_t: waveOut event thread
109  *****************************************************************************/
110 typedef struct notification_thread_t
111 {
112     VLC_COMMON_MEMBERS
113     aout_instance_t *p_aout;
114
115 } notification_thread_t;
116
117 /* local functions */
118 static void Probe        ( aout_instance_t * );
119 static int OpenWaveOut   ( aout_instance_t *, int, int, int, int, vlc_bool_t );
120 static int OpenWaveOutPCM( aout_instance_t *, int*, int, int, int, vlc_bool_t );
121 static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
122                            aout_buffer_t * );
123
124 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
125 static void WaveOutThread( notification_thread_t * );
126
127 static int VolumeInfos( aout_instance_t *, audio_volume_t * );
128 static int VolumeGet( aout_instance_t *, audio_volume_t * );
129 static int VolumeSet( aout_instance_t *, audio_volume_t );
130
131 /*****************************************************************************
132  * Module descriptor
133  *****************************************************************************/
134 #define FLOAT_TEXT N_("Use float32 output")
135 #define FLOAT_LONGTEXT N_( \
136     "The option allows you to enable or disable the high-quality float32 " \
137     "audio output mode (which is not well supported by some soundcards)." )
138
139 vlc_module_begin();
140     set_shortname( "WaveOut" );
141     set_description( _("Win32 waveOut extension output") );
142     set_capability( "audio output", 50 );
143     set_category( CAT_AUDIO );
144     set_subcategory( SUBCAT_AUDIO_AOUT );
145     add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, VLC_TRUE );
146     set_callbacks( Open, Close );
147 vlc_module_end();
148
149 /*****************************************************************************
150  * aout_sys_t: waveOut audio output method descriptor
151  *****************************************************************************
152  * This structure is part of the audio output thread descriptor.
153  * It describes the waveOut specific properties of an audio device.
154  *****************************************************************************/
155 struct aout_sys_t
156 {
157     HWAVEOUT h_waveout;                        /* handle to waveout instance */
158
159     WAVEFORMATEXTENSIBLE waveformat;                         /* audio format */
160
161     WAVEHDR waveheader[FRAMES_NUM];
162
163     notification_thread_t *p_notif;                      /* WaveOutThread id */
164     HANDLE event;
165
166     int i_buffer_size;
167
168     byte_t *p_silence_buffer;               /* buffer we use to play silence */
169
170     vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
171     int pi_chan_table[AOUT_CHAN_MAX];
172 };
173
174 static const uint32_t pi_channels_src[] =
175     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
176       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
177       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
178       AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
179 static const uint32_t pi_channels_in[] =
180     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
181       SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
182       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
183       SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
184 static const uint32_t pi_channels_out[] =
185     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
186       SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
187       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
188       SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
189
190 /*****************************************************************************
191  * Open: open the audio device
192  *****************************************************************************
193  * This function opens and setups Win32 waveOut
194  *****************************************************************************/
195 static int Open( vlc_object_t *p_this )
196 {
197     aout_instance_t *p_aout = (aout_instance_t *)p_this;
198     vlc_value_t val;
199     int i;
200
201     /* Allocate structure */
202     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
203
204     if( p_aout->output.p_sys == NULL )
205     {
206         msg_Err( p_aout, "out of memory" );
207         return VLC_EGENERIC;
208     }
209
210     p_aout->output.pf_play = Play;
211     p_aout->b_die = VLC_FALSE;
212
213     if( var_Type( p_aout, "audio-device" ) == 0 )
214     {
215         Probe( p_aout );
216     }
217
218     if( var_Get( p_aout, "audio-device", &val ) < 0 )
219     {
220         /* Probe() has failed. */
221         free( p_aout->output.p_sys );
222         return VLC_EGENERIC;
223     }
224
225     var_Create( p_aout, "waveout-float32", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
226
227     /* Open the device */
228     if( val.i_int == AOUT_VAR_SPDIF )
229     {
230         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
231
232         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
233                          p_aout->output.output.i_physical_channels,
234                          aout_FormatNbChannels( &p_aout->output.output ),
235                          p_aout->output.output.i_rate, VLC_FALSE )
236             != VLC_SUCCESS )
237         {
238             msg_Err( p_aout, "cannot open waveout audio device" );
239             free( p_aout->output.p_sys );
240             return VLC_EGENERIC;
241         }
242
243         /* Calculate the frame size in bytes */
244         p_aout->output.i_nb_samples = A52_FRAME_NB;
245         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
246         p_aout->output.output.i_frame_length = A52_FRAME_NB;
247         p_aout->output.p_sys->i_buffer_size =
248             p_aout->output.output.i_bytes_per_frame;
249
250         aout_VolumeNoneInit( p_aout );
251     }
252     else
253     {
254         WAVEOUTCAPS wocaps;
255
256         if( val.i_int == AOUT_VAR_5_1 )
257         {
258             p_aout->output.output.i_physical_channels
259                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
260                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
261                    | AOUT_CHAN_LFE;
262         }
263         else if( val.i_int == AOUT_VAR_2F2R )
264         {
265             p_aout->output.output.i_physical_channels
266                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
267                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
268         }
269         else if( val.i_int == AOUT_VAR_MONO )
270         {
271             p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
272         }
273         else
274         {
275             p_aout->output.output.i_physical_channels
276                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
277         }
278
279         if( OpenWaveOutPCM( p_aout, &p_aout->output.output.i_format,
280                             p_aout->output.output.i_physical_channels,
281                             aout_FormatNbChannels( &p_aout->output.output ),
282                             p_aout->output.output.i_rate, VLC_FALSE )
283             != VLC_SUCCESS )
284         {
285             msg_Err( p_aout, "cannot open waveout audio device" );
286             free( p_aout->output.p_sys );
287             return VLC_EGENERIC;
288         }
289
290         /* Calculate the frame size in bytes */
291         p_aout->output.i_nb_samples = FRAME_SIZE;
292         aout_FormatPrepare( &p_aout->output.output );
293         p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
294             p_aout->output.output.i_bytes_per_frame;
295
296         aout_VolumeSoftInit( p_aout );
297
298         /* Check for hardware volume support */
299         if( waveOutGetDevCaps( (UINT_PTR)p_aout->output.p_sys->h_waveout,
300                                &wocaps, sizeof(wocaps) ) == MMSYSERR_NOERROR &&
301             wocaps.dwSupport & WAVECAPS_VOLUME )
302         {
303             DWORD i_dummy;
304             if( waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_dummy )
305                 == MMSYSERR_NOERROR )
306             {
307                 p_aout->output.pf_volume_infos = VolumeInfos;
308                 p_aout->output.pf_volume_get = VolumeGet;
309                 p_aout->output.pf_volume_set = VolumeSet;
310             }
311         }
312     }
313
314
315     waveOutReset( p_aout->output.p_sys->h_waveout );
316
317     /* Allocate silence buffer */
318     p_aout->output.p_sys->p_silence_buffer =
319         malloc( p_aout->output.p_sys->i_buffer_size );
320     if( p_aout->output.p_sys->p_silence_buffer == NULL )
321     {
322         free( p_aout->output.p_sys );
323         msg_Err( p_aout, "out of memory" );
324         return 1;
325     }
326
327     /* Zero the buffer. WinCE doesn't have calloc(). */
328     memset( p_aout->output.p_sys->p_silence_buffer, 0,
329             p_aout->output.p_sys->i_buffer_size );
330
331     /* Now we need to setup our waveOut play notification structure */
332     p_aout->output.p_sys->p_notif =
333         vlc_object_create( p_aout, sizeof(notification_thread_t) );
334     p_aout->output.p_sys->p_notif->p_aout = p_aout;
335     p_aout->output.p_sys->event = CreateEvent( NULL, FALSE, FALSE, NULL );
336
337     /* Then launch the notification thread */
338     if( vlc_thread_create( p_aout->output.p_sys->p_notif,
339                            "waveOut Notification Thread", WaveOutThread,
340                            VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
341     {
342         msg_Err( p_aout, "cannot create WaveOutThread" );
343     }
344
345     /* We need to kick off the playback in order to have the callback properly
346      * working */
347     for( i = 0; i < FRAMES_NUM; i++ )
348     {
349         p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
350         p_aout->output.p_sys->waveheader[i].dwUser = 0;
351     }
352     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
353                  &p_aout->output.p_sys->waveheader[0], NULL );
354
355     return 0;
356 }
357
358 /*****************************************************************************
359  * Probe: probe the audio device for available formats and channels
360  *****************************************************************************/
361 static void Probe( aout_instance_t * p_aout )
362 {
363     vlc_value_t val, text;
364     int i_format;
365     unsigned int i_physical_channels;
366
367     var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
368     text.psz_string = _("Audio Device");
369     var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
370
371     /* Test for 5.1 support */
372     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
373                           AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
374                           AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
375     if( p_aout->output.output.i_physical_channels == i_physical_channels )
376     {
377         if( OpenWaveOutPCM( p_aout, &i_format,
378                             i_physical_channels, 6,
379                             p_aout->output.output.i_rate, VLC_TRUE )
380             == VLC_SUCCESS )
381         {
382             val.i_int = AOUT_VAR_5_1;
383             text.psz_string = N_("5.1");
384             var_Change( p_aout, "audio-device",
385                         VLC_VAR_ADDCHOICE, &val, &text );
386             msg_Dbg( p_aout, "device supports 5.1 channels" );
387         }
388     }
389
390     /* Test for 2 Front 2 Rear support */
391     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
392                           AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
393     if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
394         == i_physical_channels )
395     {
396         if( OpenWaveOutPCM( p_aout, &i_format,
397                             i_physical_channels, 4,
398                             p_aout->output.output.i_rate, VLC_TRUE )
399             == VLC_SUCCESS )
400         {
401             val.i_int = AOUT_VAR_2F2R;
402             text.psz_string = N_("2 Front 2 Rear");
403             var_Change( p_aout, "audio-device",
404                         VLC_VAR_ADDCHOICE, &val, &text );
405             msg_Dbg( p_aout, "device supports 4 channels" );
406         }
407     }
408
409     /* Test for stereo support */
410     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
411     if( OpenWaveOutPCM( p_aout, &i_format,
412                         i_physical_channels, 2,
413                         p_aout->output.output.i_rate, VLC_TRUE )
414         == VLC_SUCCESS )
415     {
416         val.i_int = AOUT_VAR_STEREO;
417         text.psz_string = N_("Stereo");
418         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
419         msg_Dbg( p_aout, "device supports 2 channels" );
420     }
421
422     /* Test for mono support */
423     i_physical_channels = AOUT_CHAN_CENTER;
424     if( OpenWaveOutPCM( p_aout, &i_format,
425                         i_physical_channels, 1,
426                         p_aout->output.output.i_rate, VLC_TRUE )
427         == VLC_SUCCESS )
428     {
429         val.i_int = AOUT_VAR_MONO;
430         text.psz_string = N_("Mono");
431         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
432         msg_Dbg( p_aout, "device supports 1 channel" );
433     }
434
435     /* Test for SPDIF support */
436     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
437     {
438         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
439                          p_aout->output.output.i_physical_channels,
440                          aout_FormatNbChannels( &p_aout->output.output ),
441                          p_aout->output.output.i_rate, VLC_TRUE )
442             == VLC_SUCCESS )
443         {
444             msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
445             val.i_int = AOUT_VAR_SPDIF;
446             text.psz_string = N_("A/52 over S/PDIF");
447             var_Change( p_aout, "audio-device",
448                         VLC_VAR_ADDCHOICE, &val, &text );
449             if( config_GetInt( p_aout, "spdif" ) )
450                 var_Set( p_aout, "audio-device", val );
451         }
452     }
453
454     var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
455     if( val.i_int <= 0 )
456     {
457         /* Probe() has failed. */
458         var_Destroy( p_aout, "audio-device" );
459         return;
460     }
461
462     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
463
464     val.b_bool = VLC_TRUE;
465     var_Set( p_aout, "intf-change", val );
466 }
467
468 /*****************************************************************************
469  * Play: play a sound buffer
470  *****************************************************************************
471  * This doesn't actually play the buffer. This just stores the buffer so it
472  * can be played by the callback thread.
473  *****************************************************************************/
474 static void Play( aout_instance_t *_p_aout )
475 {
476 }
477
478 /*****************************************************************************
479  * Close: close the audio device
480  *****************************************************************************/
481 static void Close( vlc_object_t *p_this )
482 {
483     aout_instance_t *p_aout = (aout_instance_t *)p_this;
484     aout_sys_t *p_sys = p_aout->output.p_sys;
485
486     /* Before calling waveOutClose we must reset the device */
487     vlc_object_kill( p_aout );
488
489     waveOutReset( p_sys->h_waveout );
490
491     /* wake up the audio thread */
492     SetEvent( p_sys->event );
493     vlc_thread_join( p_sys->p_notif );
494     vlc_object_destroy( p_sys->p_notif );
495     CloseHandle( p_sys->event );
496
497     /* Close the device */
498     if( waveOutClose( p_sys->h_waveout ) != MMSYSERR_NOERROR )
499     {
500         msg_Err( p_aout, "waveOutClose failed" );
501     }
502
503     free( p_sys->p_silence_buffer );
504     free( p_sys );
505 }
506
507 /*****************************************************************************
508  * OpenWaveOut: open the waveout sound device
509  ****************************************************************************/
510 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
511                         int i_channels, int i_nb_channels, int i_rate,
512                         vlc_bool_t b_probe )
513 {
514     MMRESULT result;
515     unsigned int i;
516
517     /* Set sound format */
518
519 #define waveformat p_aout->output.p_sys->waveformat
520
521     waveformat.dwChannelMask = 0;
522     for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
523     {
524         if( i_channels & pi_channels_src[i] )
525             waveformat.dwChannelMask |= pi_channels_in[i];
526     }
527
528     switch( i_format )
529     {
530     case VLC_FOURCC('s','p','d','i'):
531         i_nb_channels = 2;
532         /* To prevent channel re-ordering */
533         waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
534         waveformat.Format.wBitsPerSample = 16;
535         waveformat.Samples.wValidBitsPerSample =
536             waveformat.Format.wBitsPerSample;
537         waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
538         waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
539         break;
540
541     case VLC_FOURCC('f','l','3','2'):
542         waveformat.Format.wBitsPerSample = sizeof(float) * 8;
543         waveformat.Samples.wValidBitsPerSample =
544             waveformat.Format.wBitsPerSample;
545         waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
546         waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
547         break;
548
549     case VLC_FOURCC('s','1','6','l'):
550         waveformat.Format.wBitsPerSample = 16;
551         waveformat.Samples.wValidBitsPerSample =
552             waveformat.Format.wBitsPerSample;
553         waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
554         waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_PCM;
555         break;
556     }
557
558     waveformat.Format.nChannels = i_nb_channels;
559     waveformat.Format.nSamplesPerSec = i_rate;
560     waveformat.Format.nBlockAlign =
561         waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
562     waveformat.Format.nAvgBytesPerSec =
563         waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
564
565     /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
566     if( i_nb_channels <= 2 )
567     {
568         waveformat.Format.cbSize = 0;
569     }
570     else
571     {
572         waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
573         waveformat.Format.cbSize =
574             sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
575     }
576
577     /* Open the device */
578     result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
579                           (WAVEFORMATEX *)&waveformat,
580                           (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
581                           CALLBACK_FUNCTION | (b_probe?WAVE_FORMAT_QUERY:0) );
582     if( result == WAVERR_BADFORMAT )
583     {
584         msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
585         return VLC_EGENERIC;
586     }
587     if( result == MMSYSERR_ALLOCATED )
588     {
589         msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
590         return VLC_EGENERIC;
591     }
592     if( result != MMSYSERR_NOERROR )
593     {
594         msg_Warn( p_aout, "waveOutOpen failed" );
595         return VLC_EGENERIC;
596     }
597
598     p_aout->output.p_sys->b_chan_reorder =
599         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
600                                   waveformat.dwChannelMask, i_nb_channels,
601                                   p_aout->output.p_sys->pi_chan_table );
602
603     if( p_aout->output.p_sys->b_chan_reorder )
604     {
605         msg_Dbg( p_aout, "channel reordering needed" );
606     }
607
608     return VLC_SUCCESS;
609
610 #undef waveformat
611
612 }
613
614 /*****************************************************************************
615  * OpenWaveOutPCM: open a PCM waveout sound device
616  ****************************************************************************/
617 static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
618                            int i_channels, int i_nb_channels, int i_rate,
619                            vlc_bool_t b_probe )
620 {
621     vlc_value_t val;
622
623     var_Get( p_aout, "waveout-float32", &val );
624
625     if( !val.b_bool || OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
626                                    i_channels, i_nb_channels, i_rate, b_probe )
627         != VLC_SUCCESS )
628     {
629         if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
630                           i_channels, i_nb_channels, i_rate, b_probe )
631              != VLC_SUCCESS )
632         {
633             return VLC_EGENERIC;
634         }
635         else
636         {
637             *i_format = VLC_FOURCC('s','1','6','l');
638             return VLC_SUCCESS;
639         }
640     }
641     else
642     {
643         *i_format = VLC_FOURCC('f','l','3','2');
644         return VLC_SUCCESS;
645     }
646 }
647
648 /*****************************************************************************
649  * PlayWaveOut: play a buffer through the WaveOut device
650  *****************************************************************************/
651 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
652                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
653 {
654     MMRESULT result;
655
656     /* Prepare the buffer */
657     if( p_buffer != NULL )
658         p_waveheader->lpData = p_buffer->p_buffer;
659     else
660         /* Use silence buffer instead */
661         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
662
663     p_waveheader->dwUser = p_buffer ? (DWORD_PTR)p_buffer : (DWORD_PTR)1;
664     p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
665     p_waveheader->dwFlags = 0;
666
667     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
668     if( result != MMSYSERR_NOERROR )
669     {
670         msg_Err( p_aout, "waveOutPrepareHeader failed" );
671         return VLC_EGENERIC;
672     }
673
674     /* Send the buffer to the waveOut queue */
675     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
676     if( result != MMSYSERR_NOERROR )
677     {
678         msg_Err( p_aout, "waveOutWrite failed" );
679         return VLC_EGENERIC;
680     }
681
682     return VLC_SUCCESS;
683 }
684
685 /*****************************************************************************
686  * WaveOutCallback: what to do once WaveOut has played its sound samples
687  *****************************************************************************/
688 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
689                                       DWORD _p_aout,
690                                       DWORD dwParam1, DWORD dwParam2 )
691 {
692     aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
693     int i, i_queued_frames = 0;
694
695     if( uMsg != WOM_DONE ) return;
696
697     if( p_aout->b_die ) return;
698
699     /* Find out the current latency */
700     for( i = 0; i < FRAMES_NUM; i++ )
701     {
702         /* Check if frame buf is available */
703         if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
704         {
705             i_queued_frames++;
706         }
707     }
708
709     /* Don't wake up the thread too much */
710     if( i_queued_frames < FRAMES_NUM / 2 )
711         SetEvent( p_aout->output.p_sys->event );
712 }
713
714 /*****************************************************************************
715  * WaveOutThread: this thread will capture play notification events.
716  *****************************************************************************
717  * We use this thread to feed new audio samples to the sound card because
718  * we are not authorized to use waveOutWrite() directly in the waveout
719  * callback.
720  *****************************************************************************/
721 static void WaveOutThread( notification_thread_t *p_notif )
722 {
723     aout_instance_t *p_aout = p_notif->p_aout;
724     aout_sys_t *p_sys = p_aout->output.p_sys;
725     aout_buffer_t *p_buffer = NULL;
726     WAVEHDR *p_waveheader = p_sys->waveheader;
727     int i, i_queued_frames;
728     vlc_bool_t b_sleek;
729
730     /* We don't want any resampling when using S/PDIF */
731     b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
732
733     while( 1 )
734     {
735         WaitForSingleObject( p_sys->event, INFINITE );
736
737         /* Cleanup and find out the current latency */
738         i_queued_frames = 0;
739         for( i = 0; i < FRAMES_NUM; i++ )
740         {
741             if( (p_waveheader[i].dwFlags & WHDR_DONE) &&
742                 p_waveheader[i].dwUser )
743             {
744                 aout_buffer_t *p_buffer =
745                         (aout_buffer_t *)(p_waveheader[i].dwUser);
746                 /* Unprepare and free the buffers which has just been played */
747                 waveOutUnprepareHeader( p_sys->h_waveout, &p_waveheader[i],
748                                         sizeof(WAVEHDR) );
749
750                 if( p_waveheader[i].dwUser != 1 )
751                     aout_BufferFree( p_buffer );
752
753                 p_waveheader[i].dwUser = 0;
754             }
755
756             /* Check if frame buf is available */
757             if( !(p_waveheader[i].dwFlags & WHDR_DONE) )
758             {
759                 i_queued_frames++;
760             }
761         }
762
763         if( p_aout->b_die ) return;
764
765         /* Try to fill in as many frame buffers as possible */
766         for( i = 0; i < FRAMES_NUM; i++ )
767         {
768             /* Check if frame buf is available */
769             if( p_waveheader[i].dwFlags & WHDR_DONE )
770             {
771                 /* Take into account the latency */
772                 p_buffer = aout_OutputNextBuffer( p_aout,
773                     mdate() + 1000000 * i_queued_frames /
774                     p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
775                     b_sleek );
776
777                 if( !p_buffer && i_queued_frames )
778                 {
779                     /* We aren't late so no need to play a blank sample */
780                     break;
781                 }
782
783                 /* Do the channel reordering */
784                 if( p_buffer && p_sys->b_chan_reorder )
785                 {
786                     aout_ChannelReorder( p_buffer->p_buffer,
787                         p_buffer->i_nb_bytes,
788                         p_sys->waveformat.Format.nChannels,
789                         p_sys->pi_chan_table,
790                         p_sys->waveformat.Format.wBitsPerSample );
791                 }
792
793                 PlayWaveOut( p_aout, p_sys->h_waveout,
794                              &p_waveheader[i], p_buffer );
795
796                 i_queued_frames++;
797             }
798         }
799     }
800 }
801
802 static int VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
803 {
804     *pi_soft = AOUT_VOLUME_MAX / 2;
805     return 0;
806 }
807
808 static int VolumeGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
809 {
810     DWORD i_waveout_vol;
811
812 #ifdef UNDER_CE
813     waveOutGetVolume( 0, &i_waveout_vol );
814 #else
815     waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_waveout_vol );
816 #endif
817
818     i_waveout_vol &= 0xFFFF;
819     *pi_volume = p_aout->output.i_volume =
820         (i_waveout_vol * AOUT_VOLUME_MAX + 0xFFFF /*rounding*/) / 2 / 0xFFFF;
821     return 0;
822 }
823
824 static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
825 {
826     unsigned long i_waveout_vol = i_volume * 0xFFFF * 2 / AOUT_VOLUME_MAX;
827     i_waveout_vol |= (i_waveout_vol << 16);
828
829 #ifdef UNDER_CE
830     waveOutSetVolume( 0, i_waveout_vol );
831 #else
832     waveOutSetVolume( p_aout->output.p_sys->h_waveout, i_waveout_vol );
833 #endif
834
835     p_aout->output.i_volume = i_volume;
836     return 0;
837 }