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