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