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