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 );
147 set_callbacks( Open, Close );
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 *****************************************************************************/
158 HWAVEOUT h_waveout; /* handle to waveout instance */
160 WAVEFORMATEXTENSIBLE waveformat; /* audio format */
162 WAVEHDR waveheader[FRAMES_NUM];
164 notification_thread_t *p_notif; /* WaveOutThread id */
169 byte_t *p_silence_buffer; /* buffer we use to play silence */
171 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
172 int pi_chan_table[AOUT_CHAN_MAX];
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 };
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 )
198 aout_instance_t *p_aout = (aout_instance_t *)p_this;
202 /* Allocate structure */
203 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
205 if( p_aout->output.p_sys == NULL )
207 msg_Err( p_aout, "out of memory" );
211 p_aout->output.pf_play = Play;
212 p_aout->b_die = VLC_FALSE;
214 if( var_Type( p_aout, "audio-device" ) == 0 )
219 if( var_Get( p_aout, "audio-device", &val ) < 0 )
221 /* Probe() has failed. */
222 free( p_aout->output.p_sys );
226 var_Create( p_aout, "waveout-float32", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
228 /* Open the device */
229 if( val.i_int == AOUT_VAR_SPDIF )
231 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
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 )
239 msg_Err( p_aout, "cannot open waveout audio device" );
240 free( p_aout->output.p_sys );
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;
251 aout_VolumeNoneInit( p_aout );
257 if( val.i_int == AOUT_VAR_5_1 )
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
264 else if( val.i_int == AOUT_VAR_2F2R )
266 p_aout->output.output.i_physical_channels
267 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
268 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
270 else if( val.i_int == AOUT_VAR_MONO )
272 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
276 p_aout->output.output.i_physical_channels
277 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
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 )
286 msg_Err( p_aout, "cannot open waveout audio device" );
287 free( p_aout->output.p_sys );
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;
297 aout_VolumeSoftInit( p_aout );
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 )
305 if( waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_dummy )
306 == MMSYSERR_NOERROR )
308 p_aout->output.pf_volume_infos = VolumeInfos;
309 p_aout->output.pf_volume_get = VolumeGet;
310 p_aout->output.pf_volume_set = VolumeSet;
316 waveOutReset( p_aout->output.p_sys->h_waveout );
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 )
323 free( p_aout->output.p_sys );
324 msg_Err( p_aout, "out of memory" );
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 );
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 );
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 ) )
343 msg_Err( p_aout, "cannot create WaveOutThread" );
346 /* We need to kick off the playback in order to have the callback properly
348 for( i = 0; i < FRAMES_NUM; i++ )
350 p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
351 p_aout->output.p_sys->waveheader[i].dwUser = 0;
353 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
354 &p_aout->output.p_sys->waveheader[0], NULL );
359 /*****************************************************************************
360 * Probe: probe the audio device for available formats and channels
361 *****************************************************************************/
362 static void Probe( aout_instance_t * p_aout )
364 vlc_value_t val, text;
366 unsigned int i_physical_channels;
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 );
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 )
378 if( OpenWaveOutPCM( p_aout, &i_format,
379 i_physical_channels, 6,
380 p_aout->output.output.i_rate, VLC_TRUE )
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" );
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 )
397 if( OpenWaveOutPCM( p_aout, &i_format,
398 i_physical_channels, 4,
399 p_aout->output.output.i_rate, VLC_TRUE )
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" );
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 )
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" );
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 )
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" );
436 /* Test for SPDIF support */
437 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
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 )
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 );
455 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
458 /* Probe() has failed. */
459 var_Destroy( p_aout, "audio-device" );
463 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
465 val.b_bool = VLC_TRUE;
466 var_Set( p_aout, "intf-change", val );
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 )
479 /*****************************************************************************
480 * Close: close the audio device
481 *****************************************************************************/
482 static void Close( vlc_object_t *p_this )
484 aout_instance_t *p_aout = (aout_instance_t *)p_this;
485 aout_sys_t *p_sys = p_aout->output.p_sys;
487 /* Before calling waveOutClose we must reset the device */
488 vlc_object_kill( p_aout );
490 waveOutReset( p_sys->h_waveout );
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 );
498 /* Close the device */
499 if( waveOutClose( p_sys->h_waveout ) != MMSYSERR_NOERROR )
501 msg_Err( p_aout, "waveOutClose failed" );
504 free( p_sys->p_silence_buffer );
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,
518 /* Set sound format */
520 #define waveformat p_aout->output.p_sys->waveformat
522 waveformat.dwChannelMask = 0;
523 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
525 if( i_channels & pi_channels_src[i] )
526 waveformat.dwChannelMask |= pi_channels_in[i];
531 case VLC_FOURCC('s','p','d','i'):
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;
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;
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;
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;
566 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
567 if( i_nb_channels <= 2 )
569 waveformat.Format.cbSize = 0;
573 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
574 waveformat.Format.cbSize =
575 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
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 )
585 msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
588 if( result == MMSYSERR_ALLOCATED )
590 msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
593 if( result != MMSYSERR_NOERROR )
595 msg_Warn( p_aout, "waveOutOpen failed" );
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 );
604 if( p_aout->output.p_sys->b_chan_reorder )
606 msg_Dbg( p_aout, "channel reordering needed" );
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,
624 var_Get( p_aout, "waveout-float32", &val );
626 if( !val.b_bool || OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
627 i_channels, i_nb_channels, i_rate, b_probe )
630 if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
631 i_channels, i_nb_channels, i_rate, b_probe )
638 *i_format = VLC_FOURCC('s','1','6','l');
644 *i_format = VLC_FOURCC('f','l','3','2');
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 )
657 /* Prepare the buffer */
658 if( p_buffer != NULL )
659 p_waveheader->lpData = p_buffer->p_buffer;
661 /* Use silence buffer instead */
662 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
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;
668 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
669 if( result != MMSYSERR_NOERROR )
671 msg_Err( p_aout, "waveOutPrepareHeader failed" );
675 /* Send the buffer to the waveOut queue */
676 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
677 if( result != MMSYSERR_NOERROR )
679 msg_Err( p_aout, "waveOutWrite failed" );
686 /*****************************************************************************
687 * WaveOutCallback: what to do once WaveOut has played its sound samples
688 *****************************************************************************/
689 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
691 DWORD dwParam1, DWORD dwParam2 )
693 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
694 int i, i_queued_frames = 0;
696 if( uMsg != WOM_DONE ) return;
698 if( p_aout->b_die ) return;
700 /* Find out the current latency */
701 for( i = 0; i < FRAMES_NUM; i++ )
703 /* Check if frame buf is available */
704 if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
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 );
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
721 *****************************************************************************/
722 static void WaveOutThread( notification_thread_t *p_notif )
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;
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');
736 WaitForSingleObject( p_sys->event, INFINITE );
738 /* Cleanup and find out the current latency */
740 for( i = 0; i < FRAMES_NUM; i++ )
742 if( (p_waveheader[i].dwFlags & WHDR_DONE) &&
743 p_waveheader[i].dwUser )
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],
751 if( p_waveheader[i].dwUser != 1 )
752 aout_BufferFree( p_buffer );
754 p_waveheader[i].dwUser = 0;
757 /* Check if frame buf is available */
758 if( !(p_waveheader[i].dwFlags & WHDR_DONE) )
764 if( p_aout->b_die ) return;
766 /* Try to fill in as many frame buffers as possible */
767 for( i = 0; i < FRAMES_NUM; i++ )
769 /* Check if frame buf is available */
770 if( p_waveheader[i].dwFlags & WHDR_DONE )
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,
778 if( !p_buffer && i_queued_frames )
780 /* We aren't late so no need to play a blank sample */
784 /* Do the channel reordering */
785 if( p_buffer && p_sys->b_chan_reorder )
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 );
794 PlayWaveOut( p_aout, p_sys->h_waveout,
795 &p_waveheader[i], p_buffer );
803 static int VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
805 *pi_soft = AOUT_VOLUME_MAX / 2;
809 static int VolumeGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
814 waveOutGetVolume( 0, &i_waveout_vol );
816 waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_waveout_vol );
819 i_waveout_vol &= 0xFFFF;
820 *pi_volume = p_aout->output.i_volume =
821 (i_waveout_vol * AOUT_VOLUME_MAX + 0xFFFF /*rounding*/) / 2 / 0xFFFF;
825 static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
827 unsigned long i_waveout_vol = i_volume * 0xFFFF * 2 / AOUT_VOLUME_MAX;
828 i_waveout_vol |= (i_waveout_vol << 16);
831 waveOutSetVolume( 0, i_waveout_vol );
833 waveOutSetVolume( p_aout->output.p_sys->h_waveout, i_waveout_vol );
836 p_aout->output.i_volume = i_volume;