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