1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #define FRAME_SIZE 4096 /* The size is in samples, not in bytes */
37 /*****************************************************************************
39 *****************************************************************************/
41 # define DWORD_PTR DWORD
42 # ifdef waveOutGetDevCaps
43 # undef waveOutGetDevCaps
44 MMRESULT WINAPI waveOutGetDevCaps(UINT, LPWAVEOUTCAPS, UINT);
48 #ifndef WAVE_FORMAT_IEEE_FLOAT
49 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
52 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
53 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
56 #ifndef WAVE_FORMAT_EXTENSIBLE
57 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
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
82 #ifndef _WAVEFORMATEXTENSIBLE_
86 WORD wValidBitsPerSample; /* bits of precision */
87 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
88 WORD wReserved; /* If neither applies, set to zero. */
90 DWORD dwChannelMask; /* which channels are */
91 /* present in stream */
93 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
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}};
100 /*****************************************************************************
102 *****************************************************************************/
103 static int Open ( vlc_object_t * );
104 static void Close ( vlc_object_t * );
105 static void Play ( aout_instance_t * );
107 /*****************************************************************************
108 * notification_thread_t: waveOut event thread
109 *****************************************************************************/
110 typedef struct notification_thread_t
113 aout_instance_t *p_aout;
115 } notification_thread_t;
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 *,
124 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
125 static void WaveOutThread( notification_thread_t * );
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 );
131 /*****************************************************************************
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)." )
140 set_shortname( "WaveOut" );
141 set_description( _("Win32 waveOut extension output") );
142 set_capability( "audio output", 50 );
143 set_category( CAT_AUDIO );
144 set_subcategory( SUBCAT_AUDIO_AOUT );
145 add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, VLC_TRUE );
146 set_callbacks( Open, Close );
149 /*****************************************************************************
150 * aout_sys_t: waveOut audio output method descriptor
151 *****************************************************************************
152 * This structure is part of the audio output thread descriptor.
153 * It describes the waveOut specific properties of an audio device.
154 *****************************************************************************/
157 HWAVEOUT h_waveout; /* handle to waveout instance */
159 WAVEFORMATEXTENSIBLE waveformat; /* audio format */
161 WAVEHDR waveheader[FRAMES_NUM];
163 notification_thread_t *p_notif; /* WaveOutThread id */
168 byte_t *p_silence_buffer; /* buffer we use to play silence */
170 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
171 int pi_chan_table[AOUT_CHAN_MAX];
174 static const uint32_t pi_channels_src[] =
175 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
176 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
177 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
178 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
179 static const uint32_t pi_channels_in[] =
180 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
181 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
182 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
183 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
184 static const uint32_t pi_channels_out[] =
185 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
186 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
187 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
188 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
190 /*****************************************************************************
191 * Open: open the audio device
192 *****************************************************************************
193 * This function opens and setups Win32 waveOut
194 *****************************************************************************/
195 static int Open( vlc_object_t *p_this )
197 aout_instance_t *p_aout = (aout_instance_t *)p_this;
201 /* Allocate structure */
202 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
204 if( p_aout->output.p_sys == NULL )
206 msg_Err( p_aout, "out of memory" );
210 p_aout->output.pf_play = Play;
211 p_aout->b_die = VLC_FALSE;
213 if( var_Type( p_aout, "audio-device" ) == 0 )
218 if( var_Get( p_aout, "audio-device", &val ) < 0 )
220 /* Probe() has failed. */
221 free( p_aout->output.p_sys );
225 var_Create( p_aout, "waveout-float32", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
227 /* Open the device */
228 if( val.i_int == AOUT_VAR_SPDIF )
230 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
232 if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
233 p_aout->output.output.i_physical_channels,
234 aout_FormatNbChannels( &p_aout->output.output ),
235 p_aout->output.output.i_rate, VLC_FALSE )
238 msg_Err( p_aout, "cannot open waveout audio device" );
239 free( p_aout->output.p_sys );
243 /* Calculate the frame size in bytes */
244 p_aout->output.i_nb_samples = A52_FRAME_NB;
245 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
246 p_aout->output.output.i_frame_length = A52_FRAME_NB;
247 p_aout->output.p_sys->i_buffer_size =
248 p_aout->output.output.i_bytes_per_frame;
250 aout_VolumeNoneInit( p_aout );
256 if( val.i_int == AOUT_VAR_5_1 )
258 p_aout->output.output.i_physical_channels
259 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
260 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
263 else if( val.i_int == AOUT_VAR_2F2R )
265 p_aout->output.output.i_physical_channels
266 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
267 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
269 else if( val.i_int == AOUT_VAR_MONO )
271 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
275 p_aout->output.output.i_physical_channels
276 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
279 if( OpenWaveOutPCM( p_aout, &p_aout->output.output.i_format,
280 p_aout->output.output.i_physical_channels,
281 aout_FormatNbChannels( &p_aout->output.output ),
282 p_aout->output.output.i_rate, VLC_FALSE )
285 msg_Err( p_aout, "cannot open waveout audio device" );
286 free( p_aout->output.p_sys );
290 /* Calculate the frame size in bytes */
291 p_aout->output.i_nb_samples = FRAME_SIZE;
292 aout_FormatPrepare( &p_aout->output.output );
293 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
294 p_aout->output.output.i_bytes_per_frame;
296 aout_VolumeSoftInit( p_aout );
298 /* Check for hardware volume support */
299 if( waveOutGetDevCaps( (UINT_PTR)p_aout->output.p_sys->h_waveout,
300 &wocaps, sizeof(wocaps) ) == MMSYSERR_NOERROR &&
301 wocaps.dwSupport & WAVECAPS_VOLUME )
304 if( waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_dummy )
305 == MMSYSERR_NOERROR )
307 p_aout->output.pf_volume_infos = VolumeInfos;
308 p_aout->output.pf_volume_get = VolumeGet;
309 p_aout->output.pf_volume_set = VolumeSet;
315 waveOutReset( p_aout->output.p_sys->h_waveout );
317 /* Allocate silence buffer */
318 p_aout->output.p_sys->p_silence_buffer =
319 malloc( p_aout->output.p_sys->i_buffer_size );
320 if( p_aout->output.p_sys->p_silence_buffer == NULL )
322 free( p_aout->output.p_sys );
323 msg_Err( p_aout, "out of memory" );
327 /* Zero the buffer. WinCE doesn't have calloc(). */
328 memset( p_aout->output.p_sys->p_silence_buffer, 0,
329 p_aout->output.p_sys->i_buffer_size );
331 /* Now we need to setup our waveOut play notification structure */
332 p_aout->output.p_sys->p_notif =
333 vlc_object_create( p_aout, sizeof(notification_thread_t) );
334 p_aout->output.p_sys->p_notif->p_aout = p_aout;
335 p_aout->output.p_sys->event = CreateEvent( NULL, FALSE, FALSE, NULL );
337 /* Then launch the notification thread */
338 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
339 "waveOut Notification Thread", WaveOutThread,
340 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
342 msg_Err( p_aout, "cannot create WaveOutThread" );
345 /* We need to kick off the playback in order to have the callback properly
347 for( i = 0; i < FRAMES_NUM; i++ )
349 p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
350 p_aout->output.p_sys->waveheader[i].dwUser = 0;
352 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
353 &p_aout->output.p_sys->waveheader[0], NULL );
358 /*****************************************************************************
359 * Probe: probe the audio device for available formats and channels
360 *****************************************************************************/
361 static void Probe( aout_instance_t * p_aout )
363 vlc_value_t val, text;
365 unsigned int i_physical_channels;
367 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
368 text.psz_string = _("Audio Device");
369 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
371 /* Test for 5.1 support */
372 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
373 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
374 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
375 if( p_aout->output.output.i_physical_channels == i_physical_channels )
377 if( OpenWaveOutPCM( p_aout, &i_format,
378 i_physical_channels, 6,
379 p_aout->output.output.i_rate, VLC_TRUE )
382 val.i_int = AOUT_VAR_5_1;
383 text.psz_string = N_("5.1");
384 var_Change( p_aout, "audio-device",
385 VLC_VAR_ADDCHOICE, &val, &text );
386 msg_Dbg( p_aout, "device supports 5.1 channels" );
390 /* Test for 2 Front 2 Rear support */
391 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
392 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
393 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
394 == i_physical_channels )
396 if( OpenWaveOutPCM( p_aout, &i_format,
397 i_physical_channels, 4,
398 p_aout->output.output.i_rate, VLC_TRUE )
401 val.i_int = AOUT_VAR_2F2R;
402 text.psz_string = N_("2 Front 2 Rear");
403 var_Change( p_aout, "audio-device",
404 VLC_VAR_ADDCHOICE, &val, &text );
405 msg_Dbg( p_aout, "device supports 4 channels" );
409 /* Test for stereo support */
410 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
411 if( OpenWaveOutPCM( p_aout, &i_format,
412 i_physical_channels, 2,
413 p_aout->output.output.i_rate, VLC_TRUE )
416 val.i_int = AOUT_VAR_STEREO;
417 text.psz_string = N_("Stereo");
418 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
419 msg_Dbg( p_aout, "device supports 2 channels" );
422 /* Test for mono support */
423 i_physical_channels = AOUT_CHAN_CENTER;
424 if( OpenWaveOutPCM( p_aout, &i_format,
425 i_physical_channels, 1,
426 p_aout->output.output.i_rate, VLC_TRUE )
429 val.i_int = AOUT_VAR_MONO;
430 text.psz_string = N_("Mono");
431 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
432 msg_Dbg( p_aout, "device supports 1 channel" );
435 /* Test for SPDIF support */
436 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
438 if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
439 p_aout->output.output.i_physical_channels,
440 aout_FormatNbChannels( &p_aout->output.output ),
441 p_aout->output.output.i_rate, VLC_TRUE )
444 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
445 val.i_int = AOUT_VAR_SPDIF;
446 text.psz_string = N_("A/52 over S/PDIF");
447 var_Change( p_aout, "audio-device",
448 VLC_VAR_ADDCHOICE, &val, &text );
449 if( config_GetInt( p_aout, "spdif" ) )
450 var_Set( p_aout, "audio-device", val );
454 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
457 /* Probe() has failed. */
458 var_Destroy( p_aout, "audio-device" );
462 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
464 val.b_bool = VLC_TRUE;
465 var_Set( p_aout, "intf-change", val );
468 /*****************************************************************************
469 * Play: play a sound buffer
470 *****************************************************************************
471 * This doesn't actually play the buffer. This just stores the buffer so it
472 * can be played by the callback thread.
473 *****************************************************************************/
474 static void Play( aout_instance_t *_p_aout )
478 /*****************************************************************************
479 * Close: close the audio device
480 *****************************************************************************/
481 static void Close( vlc_object_t *p_this )
483 aout_instance_t *p_aout = (aout_instance_t *)p_this;
484 aout_sys_t *p_sys = p_aout->output.p_sys;
486 /* Before calling waveOutClose we must reset the device */
487 vlc_object_kill( p_aout );
489 waveOutReset( p_sys->h_waveout );
491 /* wake up the audio thread */
492 SetEvent( p_sys->event );
493 vlc_thread_join( p_sys->p_notif );
494 vlc_object_destroy( p_sys->p_notif );
495 CloseHandle( p_sys->event );
497 /* Close the device */
498 if( waveOutClose( p_sys->h_waveout ) != MMSYSERR_NOERROR )
500 msg_Err( p_aout, "waveOutClose failed" );
503 free( p_sys->p_silence_buffer );
507 /*****************************************************************************
508 * OpenWaveOut: open the waveout sound device
509 ****************************************************************************/
510 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
511 int i_channels, int i_nb_channels, int i_rate,
517 /* Set sound format */
519 #define waveformat p_aout->output.p_sys->waveformat
521 waveformat.dwChannelMask = 0;
522 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
524 if( i_channels & pi_channels_src[i] )
525 waveformat.dwChannelMask |= pi_channels_in[i];
530 case VLC_FOURCC('s','p','d','i'):
532 /* To prevent channel re-ordering */
533 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
534 waveformat.Format.wBitsPerSample = 16;
535 waveformat.Samples.wValidBitsPerSample =
536 waveformat.Format.wBitsPerSample;
537 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
538 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
541 case VLC_FOURCC('f','l','3','2'):
542 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
543 waveformat.Samples.wValidBitsPerSample =
544 waveformat.Format.wBitsPerSample;
545 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
546 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
549 case VLC_FOURCC('s','1','6','l'):
550 waveformat.Format.wBitsPerSample = 16;
551 waveformat.Samples.wValidBitsPerSample =
552 waveformat.Format.wBitsPerSample;
553 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
554 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_PCM;
558 waveformat.Format.nChannels = i_nb_channels;
559 waveformat.Format.nSamplesPerSec = i_rate;
560 waveformat.Format.nBlockAlign =
561 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
562 waveformat.Format.nAvgBytesPerSec =
563 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
565 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
566 if( i_nb_channels <= 2 )
568 waveformat.Format.cbSize = 0;
572 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
573 waveformat.Format.cbSize =
574 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
577 /* Open the device */
578 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
579 (WAVEFORMATEX *)&waveformat,
580 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
581 CALLBACK_FUNCTION | (b_probe?WAVE_FORMAT_QUERY:0) );
582 if( result == WAVERR_BADFORMAT )
584 msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
587 if( result == MMSYSERR_ALLOCATED )
589 msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
592 if( result != MMSYSERR_NOERROR )
594 msg_Warn( p_aout, "waveOutOpen failed" );
598 p_aout->output.p_sys->b_chan_reorder =
599 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
600 waveformat.dwChannelMask, i_nb_channels,
601 p_aout->output.p_sys->pi_chan_table );
603 if( p_aout->output.p_sys->b_chan_reorder )
605 msg_Dbg( p_aout, "channel reordering needed" );
614 /*****************************************************************************
615 * OpenWaveOutPCM: open a PCM waveout sound device
616 ****************************************************************************/
617 static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
618 int i_channels, int i_nb_channels, int i_rate,
623 var_Get( p_aout, "waveout-float32", &val );
625 if( !val.b_bool || OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
626 i_channels, i_nb_channels, i_rate, b_probe )
629 if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
630 i_channels, i_nb_channels, i_rate, b_probe )
637 *i_format = VLC_FOURCC('s','1','6','l');
643 *i_format = VLC_FOURCC('f','l','3','2');
648 /*****************************************************************************
649 * PlayWaveOut: play a buffer through the WaveOut device
650 *****************************************************************************/
651 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
652 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
656 /* Prepare the buffer */
657 if( p_buffer != NULL )
658 p_waveheader->lpData = p_buffer->p_buffer;
660 /* Use silence buffer instead */
661 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
663 p_waveheader->dwUser = p_buffer ? (DWORD_PTR)p_buffer : (DWORD_PTR)1;
664 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
665 p_waveheader->dwFlags = 0;
667 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
668 if( result != MMSYSERR_NOERROR )
670 msg_Err( p_aout, "waveOutPrepareHeader failed" );
674 /* Send the buffer to the waveOut queue */
675 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
676 if( result != MMSYSERR_NOERROR )
678 msg_Err( p_aout, "waveOutWrite failed" );
685 /*****************************************************************************
686 * WaveOutCallback: what to do once WaveOut has played its sound samples
687 *****************************************************************************/
688 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
690 DWORD dwParam1, DWORD dwParam2 )
692 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
693 int i, i_queued_frames = 0;
695 if( uMsg != WOM_DONE ) return;
697 if( p_aout->b_die ) return;
699 /* Find out the current latency */
700 for( i = 0; i < FRAMES_NUM; i++ )
702 /* Check if frame buf is available */
703 if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
709 /* Don't wake up the thread too much */
710 if( i_queued_frames < FRAMES_NUM / 2 )
711 SetEvent( p_aout->output.p_sys->event );
714 /*****************************************************************************
715 * WaveOutThread: this thread will capture play notification events.
716 *****************************************************************************
717 * We use this thread to feed new audio samples to the sound card because
718 * we are not authorized to use waveOutWrite() directly in the waveout
720 *****************************************************************************/
721 static void WaveOutThread( notification_thread_t *p_notif )
723 aout_instance_t *p_aout = p_notif->p_aout;
724 aout_sys_t *p_sys = p_aout->output.p_sys;
725 aout_buffer_t *p_buffer = NULL;
726 WAVEHDR *p_waveheader = p_sys->waveheader;
727 int i, i_queued_frames;
730 /* We don't want any resampling when using S/PDIF */
731 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
735 WaitForSingleObject( p_sys->event, INFINITE );
737 /* Cleanup and find out the current latency */
739 for( i = 0; i < FRAMES_NUM; i++ )
741 if( (p_waveheader[i].dwFlags & WHDR_DONE) &&
742 p_waveheader[i].dwUser )
744 aout_buffer_t *p_buffer =
745 (aout_buffer_t *)(p_waveheader[i].dwUser);
746 /* Unprepare and free the buffers which has just been played */
747 waveOutUnprepareHeader( p_sys->h_waveout, &p_waveheader[i],
750 if( p_waveheader[i].dwUser != 1 )
751 aout_BufferFree( p_buffer );
753 p_waveheader[i].dwUser = 0;
756 /* Check if frame buf is available */
757 if( !(p_waveheader[i].dwFlags & WHDR_DONE) )
763 if( p_aout->b_die ) return;
765 /* Try to fill in as many frame buffers as possible */
766 for( i = 0; i < FRAMES_NUM; i++ )
768 /* Check if frame buf is available */
769 if( p_waveheader[i].dwFlags & WHDR_DONE )
771 /* Take into account the latency */
772 p_buffer = aout_OutputNextBuffer( p_aout,
773 mdate() + 1000000 * i_queued_frames /
774 p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
777 if( !p_buffer && i_queued_frames )
779 /* We aren't late so no need to play a blank sample */
783 /* Do the channel reordering */
784 if( p_buffer && p_sys->b_chan_reorder )
786 aout_ChannelReorder( p_buffer->p_buffer,
787 p_buffer->i_nb_bytes,
788 p_sys->waveformat.Format.nChannels,
789 p_sys->pi_chan_table,
790 p_sys->waveformat.Format.wBitsPerSample );
793 PlayWaveOut( p_aout, p_sys->h_waveout,
794 &p_waveheader[i], p_buffer );
802 static int VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
804 *pi_soft = AOUT_VOLUME_MAX / 2;
808 static int VolumeGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
813 waveOutGetVolume( 0, &i_waveout_vol );
815 waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_waveout_vol );
818 i_waveout_vol &= 0xFFFF;
819 *pi_volume = p_aout->output.i_volume =
820 (i_waveout_vol * AOUT_VOLUME_MAX + 0xFFFF /*rounding*/) / 2 / 0xFFFF;
824 static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
826 unsigned long i_waveout_vol = i_volume * 0xFFFF * 2 / AOUT_VOLUME_MAX;
827 i_waveout_vol |= (i_waveout_vol << 16);
830 waveOutSetVolume( 0, i_waveout_vol );
832 waveOutSetVolume( p_aout->output.p_sys->h_waveout, i_waveout_vol );
835 p_aout->output.i_volume = i_volume;