1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/win/audio_low_latency_output_win.h"
7 #include <Functiondiscoverykeys_devpkey.h>
9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/histogram.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/scoped_propvariant.h"
16 #include "media/audio/win/audio_manager_win.h"
17 #include "media/audio/win/avrt_wrapper_win.h"
18 #include "media/audio/win/core_audio_util_win.h"
19 #include "media/base/limits.h"
20 #include "media/base/media_switches.h"
22 using base::win::ScopedComPtr
;
23 using base::win::ScopedCOMInitializer
;
24 using base::win::ScopedCoMem
;
28 typedef uint32 ChannelConfig
;
30 // Retrieves an integer mask which corresponds to the channel layout the
31 // audio engine uses for its internal processing/mixing of shared-mode
32 // streams. This mask indicates which channels are present in the multi-
33 // channel stream. The least significant bit corresponds with the Front Left
34 // speaker, the next least significant bit corresponds to the Front Right
35 // speaker, and so on, continuing in the order defined in KsMedia.h.
36 // See http://msdn.microsoft.com/en-us/library/windows/hardware/ff537083(v=vs.85).aspx
38 static ChannelConfig
GetChannelConfig() {
39 WAVEFORMATPCMEX format
;
40 return SUCCEEDED(CoreAudioUtil::GetDefaultSharedModeMixFormat(
41 eRender
, eConsole
, &format
)) ?
42 static_cast<int>(format
.dwChannelMask
) : 0;
45 // Compare two sets of audio parameters and return true if they are equal.
46 // Note that bits_per_sample() is excluded from this comparison since Core
47 // Audio can deal with most bit depths. As an example, if the native/mixing
48 // bit depth is 32 bits (default), opening at 16 or 24 still works fine and
49 // the audio engine will do the required conversion for us. Channel count is
50 // excluded since Open() will fail anyways and it doesn't impact buffering.
51 static bool CompareAudioParametersNoBitDepthOrChannels(
52 const media::AudioParameters
& a
, const media::AudioParameters
& b
) {
53 return (a
.format() == b
.format() &&
54 a
.sample_rate() == b
.sample_rate() &&
55 a
.frames_per_buffer() == b
.frames_per_buffer());
58 // Converts Microsoft's channel configuration to ChannelLayout.
59 // This mapping is not perfect but the best we can do given the current
60 // ChannelLayout enumerator and the Windows-specific speaker configurations
61 // defined in ksmedia.h. Don't assume that the channel ordering in
62 // ChannelLayout is exactly the same as the Windows specific configuration.
63 // As an example: KSAUDIO_SPEAKER_7POINT1_SURROUND is mapped to
64 // CHANNEL_LAYOUT_7_1 but the positions of Back L, Back R and Side L, Side R
65 // speakers are different in these two definitions.
66 static ChannelLayout
ChannelConfigToChannelLayout(ChannelConfig config
) {
68 case KSAUDIO_SPEAKER_DIRECTOUT
:
69 return CHANNEL_LAYOUT_NONE
;
70 case KSAUDIO_SPEAKER_MONO
:
71 return CHANNEL_LAYOUT_MONO
;
72 case KSAUDIO_SPEAKER_STEREO
:
73 return CHANNEL_LAYOUT_STEREO
;
74 case KSAUDIO_SPEAKER_QUAD
:
75 return CHANNEL_LAYOUT_QUAD
;
76 case KSAUDIO_SPEAKER_SURROUND
:
77 return CHANNEL_LAYOUT_4_0
;
78 case KSAUDIO_SPEAKER_5POINT1
:
79 return CHANNEL_LAYOUT_5_1_BACK
;
80 case KSAUDIO_SPEAKER_5POINT1_SURROUND
:
81 return CHANNEL_LAYOUT_5_1
;
82 case KSAUDIO_SPEAKER_7POINT1
:
83 return CHANNEL_LAYOUT_7_1_WIDE
;
84 case KSAUDIO_SPEAKER_7POINT1_SURROUND
:
85 return CHANNEL_LAYOUT_7_1
;
87 VLOG(1) << "Unsupported channel layout: " << config
;
88 return CHANNEL_LAYOUT_UNSUPPORTED
;
93 AUDCLNT_SHAREMODE
WASAPIAudioOutputStream::GetShareMode() {
94 const CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
95 if (cmd_line
->HasSwitch(switches::kEnableExclusiveAudio
))
96 return AUDCLNT_SHAREMODE_EXCLUSIVE
;
97 return AUDCLNT_SHAREMODE_SHARED
;
101 int WASAPIAudioOutputStream::HardwareChannelCount() {
102 WAVEFORMATPCMEX format
;
103 return SUCCEEDED(CoreAudioUtil::GetDefaultSharedModeMixFormat(
104 eRender
, eConsole
, &format
)) ?
105 static_cast<int>(format
.Format
.nChannels
) : 0;
109 ChannelLayout
WASAPIAudioOutputStream::HardwareChannelLayout() {
110 return ChannelConfigToChannelLayout(GetChannelConfig());
114 int WASAPIAudioOutputStream::HardwareSampleRate() {
115 WAVEFORMATPCMEX format
;
116 return SUCCEEDED(CoreAudioUtil::GetDefaultSharedModeMixFormat(
117 eRender
, eConsole
, &format
)) ?
118 static_cast<int>(format
.Format
.nSamplesPerSec
) : 0;
121 WASAPIAudioOutputStream::WASAPIAudioOutputStream(AudioManagerWin
* manager
,
122 const AudioParameters
& params
,
124 : creating_thread_id_(base::PlatformThread::CurrentId()),
127 audio_parameters_are_valid_(false),
129 endpoint_buffer_size_frames_(0),
130 device_role_(device_role
),
131 share_mode_(GetShareMode()),
132 num_written_frames_(0),
134 audio_bus_(AudioBus::Create(params
)) {
136 VLOG(1) << "WASAPIAudioOutputStream::WASAPIAudioOutputStream()";
137 VLOG_IF(1, share_mode_
== AUDCLNT_SHAREMODE_EXCLUSIVE
)
138 << "Core Audio (WASAPI) EXCLUSIVE MODE is enabled.";
140 if (share_mode_
== AUDCLNT_SHAREMODE_SHARED
) {
141 // Verify that the input audio parameters are identical (bit depth and
142 // channel count are excluded) to the preferred (native) audio parameters.
143 // Open() will fail if this is not the case.
144 AudioParameters preferred_params
;
145 HRESULT hr
= CoreAudioUtil::GetPreferredAudioParameters(
146 eRender
, device_role
, &preferred_params
);
147 audio_parameters_are_valid_
= SUCCEEDED(hr
) &&
148 CompareAudioParametersNoBitDepthOrChannels(params
, preferred_params
);
149 LOG_IF(WARNING
, !audio_parameters_are_valid_
)
150 << "Input and preferred parameters are not identical.";
153 // Load the Avrt DLL if not already loaded. Required to support MMCSS.
154 bool avrt_init
= avrt::Initialize();
155 DCHECK(avrt_init
) << "Failed to load the avrt.dll";
157 // Set up the desired render format specified by the client. We use the
158 // WAVE_FORMAT_EXTENSIBLE structure to ensure that multiple channel ordering
159 // and high precision data can be supported.
161 // Begin with the WAVEFORMATEX structure that specifies the basic format.
162 WAVEFORMATEX
* format
= &format_
.Format
;
163 format
->wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
164 format
->nChannels
= params
.channels();
165 format
->nSamplesPerSec
= params
.sample_rate();
166 format
->wBitsPerSample
= params
.bits_per_sample();
167 format
->nBlockAlign
= (format
->wBitsPerSample
/ 8) * format
->nChannels
;
168 format
->nAvgBytesPerSec
= format
->nSamplesPerSec
* format
->nBlockAlign
;
169 format
->cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
171 // Add the parts which are unique to WAVE_FORMAT_EXTENSIBLE.
172 format_
.Samples
.wValidBitsPerSample
= params
.bits_per_sample();
173 format_
.dwChannelMask
= GetChannelConfig();
174 format_
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
176 // Store size (in different units) of audio packets which we expect to
177 // get from the audio endpoint device in each render event.
178 packet_size_frames_
= params
.frames_per_buffer();
179 packet_size_bytes_
= params
.GetBytesPerBuffer();
180 packet_size_ms_
= (1000.0 * packet_size_frames_
) / params
.sample_rate();
181 VLOG(1) << "Number of bytes per audio frame : " << format
->nBlockAlign
;
182 VLOG(1) << "Number of audio frames per packet: " << packet_size_frames_
;
183 VLOG(1) << "Number of bytes per packet : " << packet_size_bytes_
;
184 VLOG(1) << "Number of milliseconds per packet: " << packet_size_ms_
;
186 // All events are auto-reset events and non-signaled initially.
188 // Create the event which the audio engine will signal each time
189 // a buffer becomes ready to be processed by the client.
190 audio_samples_render_event_
.Set(CreateEvent(NULL
, FALSE
, FALSE
, NULL
));
191 DCHECK(audio_samples_render_event_
.IsValid());
193 // Create the event which will be set in Stop() when capturing shall stop.
194 stop_render_event_
.Set(CreateEvent(NULL
, FALSE
, FALSE
, NULL
));
195 DCHECK(stop_render_event_
.IsValid());
198 WASAPIAudioOutputStream::~WASAPIAudioOutputStream() {}
200 bool WASAPIAudioOutputStream::Open() {
201 VLOG(1) << "WASAPIAudioOutputStream::Open()";
202 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_
);
207 // Audio parameters must be identical to the preferred set of parameters
208 // if shared mode (default) is utilized.
209 if (share_mode_
== AUDCLNT_SHAREMODE_SHARED
) {
210 if (!audio_parameters_are_valid_
) {
211 LOG(ERROR
) << "Audio parameters are not valid.";
216 // Create an IAudioClient interface for the default rendering IMMDevice.
217 ScopedComPtr
<IAudioClient
> audio_client
=
218 CoreAudioUtil::CreateDefaultClient(eRender
, device_role_
);
222 // Extra sanity to ensure that the provided device format is still valid.
223 if (!CoreAudioUtil::IsFormatSupported(audio_client
,
229 HRESULT hr
= S_FALSE
;
230 if (share_mode_
== AUDCLNT_SHAREMODE_SHARED
) {
231 // Initialize the audio stream between the client and the device in shared
232 // mode and using event-driven buffer handling.
233 hr
= CoreAudioUtil::SharedModeInitialize(
234 audio_client
, &format_
, audio_samples_render_event_
.Get(),
235 &endpoint_buffer_size_frames_
);
239 // We know from experience that the best possible callback sequence is
240 // achieved when the packet size (given by the native device period)
241 // is an even multiple of the endpoint buffer size.
242 // Examples: 48kHz => 960 % 480, 44.1kHz => 896 % 448 or 882 % 441.
243 if (endpoint_buffer_size_frames_
% packet_size_frames_
!= 0) {
244 LOG(ERROR
) << "Bailing out due to non-perfect timing.";
248 // TODO(henrika): break out to CoreAudioUtil::ExclusiveModeInitialize()
249 // when removing the enable-exclusive-audio flag.
250 hr
= ExclusiveModeInitialization(audio_client
,
251 audio_samples_render_event_
.Get(),
252 &endpoint_buffer_size_frames_
);
256 // The buffer scheme for exclusive mode streams is not designed for max
257 // flexibility. We only allow a "perfect match" between the packet size set
258 // by the user and the actual endpoint buffer size.
259 if (endpoint_buffer_size_frames_
!= packet_size_frames_
) {
260 LOG(ERROR
) << "Bailing out due to non-perfect timing.";
265 // Create an IAudioRenderClient client for an initialized IAudioClient.
266 // The IAudioRenderClient interface enables us to write output data to
267 // a rendering endpoint buffer.
268 ScopedComPtr
<IAudioRenderClient
> audio_render_client
=
269 CoreAudioUtil::CreateRenderClient(audio_client
);
270 if (!audio_render_client
)
273 // Store valid COM interfaces.
274 audio_client_
= audio_client
;
275 audio_render_client_
= audio_render_client
;
281 void WASAPIAudioOutputStream::Start(AudioSourceCallback
* callback
) {
282 VLOG(1) << "WASAPIAudioOutputStream::Start()";
283 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_
);
287 if (render_thread_
) {
288 CHECK_EQ(callback
, source_
);
294 // Create and start the thread that will drive the rendering by waiting for
296 render_thread_
.reset(
297 new base::DelegateSimpleThread(this, "wasapi_render_thread"));
298 render_thread_
->Start();
299 if (!render_thread_
->HasBeenStarted()) {
300 LOG(ERROR
) << "Failed to start WASAPI render thread.";
304 // Ensure that the endpoint buffer is prepared with silence.
305 if (share_mode_
== AUDCLNT_SHAREMODE_SHARED
) {
306 if (!CoreAudioUtil::FillRenderEndpointBufferWithSilence(
307 audio_client_
, audio_render_client_
)) {
308 LOG(WARNING
) << "Failed to prepare endpoint buffers with silence.";
312 num_written_frames_
= endpoint_buffer_size_frames_
;
314 // Start streaming data between the endpoint buffer and the audio engine.
315 HRESULT hr
= audio_client_
->Start();
317 SetEvent(stop_render_event_
.Get());
318 render_thread_
->Join();
319 render_thread_
.reset();
324 void WASAPIAudioOutputStream::Stop() {
325 VLOG(1) << "WASAPIAudioOutputStream::Stop()";
326 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_
);
330 // Stop output audio streaming.
331 HRESULT hr
= audio_client_
->Stop();
333 LOG_IF(ERROR
, hr
!= AUDCLNT_E_NOT_INITIALIZED
)
334 << "Failed to stop output streaming: " << std::hex
<< hr
;
337 // Wait until the thread completes and perform cleanup.
338 SetEvent(stop_render_event_
.Get());
339 render_thread_
->Join();
340 render_thread_
.reset();
342 // Ensure that we don't quit the main thread loop immediately next
343 // time Start() is called.
344 ResetEvent(stop_render_event_
.Get());
346 // Clear source callback, it'll be set again on the next Start() call.
349 // Flush all pending data and reset the audio clock stream position to 0.
350 hr
= audio_client_
->Reset();
352 LOG_IF(ERROR
, hr
!= AUDCLNT_E_NOT_INITIALIZED
)
353 << "Failed to reset streaming: " << std::hex
<< hr
;
356 // Extra safety check to ensure that the buffers are cleared.
357 // If the buffers are not cleared correctly, the next call to Start()
358 // would fail with AUDCLNT_E_BUFFER_ERROR at IAudioRenderClient::GetBuffer().
359 // This check is is only needed for shared-mode streams.
360 if (share_mode_
== AUDCLNT_SHAREMODE_SHARED
) {
361 UINT32 num_queued_frames
= 0;
362 audio_client_
->GetCurrentPadding(&num_queued_frames
);
363 DCHECK_EQ(0u, num_queued_frames
);
367 void WASAPIAudioOutputStream::Close() {
368 VLOG(1) << "WASAPIAudioOutputStream::Close()";
369 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_
);
371 // It is valid to call Close() before calling open or Start().
372 // It is also valid to call Close() after Start() has been called.
375 // Inform the audio manager that we have been closed. This will cause our
377 manager_
->ReleaseOutputStream(this);
380 void WASAPIAudioOutputStream::SetVolume(double volume
) {
381 VLOG(1) << "SetVolume(volume=" << volume
<< ")";
382 float volume_float
= static_cast<float>(volume
);
383 if (volume_float
< 0.0f
|| volume_float
> 1.0f
) {
386 volume_
= volume_float
;
389 void WASAPIAudioOutputStream::GetVolume(double* volume
) {
390 VLOG(1) << "GetVolume()";
391 *volume
= static_cast<double>(volume_
);
394 void WASAPIAudioOutputStream::Run() {
395 ScopedCOMInitializer
com_init(ScopedCOMInitializer::kMTA
);
397 // Increase the thread priority.
398 render_thread_
->SetThreadPriority(base::kThreadPriority_RealtimeAudio
);
400 // Enable MMCSS to ensure that this thread receives prioritized access to
402 DWORD task_index
= 0;
403 HANDLE mm_task
= avrt::AvSetMmThreadCharacteristics(L
"Pro Audio",
406 (mm_task
&& avrt::AvSetMmThreadPriority(mm_task
, AVRT_PRIORITY_CRITICAL
));
408 // Failed to enable MMCSS on this thread. It is not fatal but can lead
409 // to reduced QoS at high load.
410 DWORD err
= GetLastError();
411 LOG(WARNING
) << "Failed to enable MMCSS (error code=" << err
<< ").";
414 HRESULT hr
= S_FALSE
;
418 HANDLE wait_array
[] = { stop_render_event_
,
419 audio_samples_render_event_
};
420 UINT64 device_frequency
= 0;
422 // The IAudioClock interface enables us to monitor a stream's data
423 // rate and the current position in the stream. Allocate it before we
425 ScopedComPtr
<IAudioClock
> audio_clock
;
426 hr
= audio_client_
->GetService(__uuidof(IAudioClock
),
427 audio_clock
.ReceiveVoid());
429 // The device frequency is the frequency generated by the hardware clock in
430 // the audio device. The GetFrequency() method reports a constant frequency.
431 hr
= audio_clock
->GetFrequency(&device_frequency
);
434 PLOG_IF(ERROR
, error
) << "Failed to acquire IAudioClock interface: "
437 // Keep rendering audio until the stop event or the stream-switch event
438 // is signaled. An error event can also break the main thread loop.
439 while (playing
&& !error
) {
440 // Wait for a close-down event, stream-switch event or a new render event.
441 DWORD wait_result
= WaitForMultipleObjects(arraysize(wait_array
),
446 switch (wait_result
) {
447 case WAIT_OBJECT_0
+ 0:
448 // |stop_render_event_| has been set.
451 case WAIT_OBJECT_0
+ 1:
452 // |audio_samples_render_event_| has been set.
453 RenderAudioFromSource(audio_clock
, device_frequency
);
461 if (playing
&& error
) {
462 // Stop audio rendering since something has gone wrong in our main thread
463 // loop. Note that, we are still in a "started" state, hence a Stop() call
464 // is required to join the thread properly.
465 audio_client_
->Stop();
466 PLOG(ERROR
) << "WASAPI rendering failed.";
470 if (mm_task
&& !avrt::AvRevertMmThreadCharacteristics(mm_task
)) {
471 PLOG(WARNING
) << "Failed to disable MMCSS";
475 void WASAPIAudioOutputStream::RenderAudioFromSource(
476 IAudioClock
* audio_clock
, UINT64 device_frequency
) {
477 TRACE_EVENT0("audio", "RenderAudioFromSource");
479 HRESULT hr
= S_FALSE
;
480 UINT32 num_queued_frames
= 0;
481 uint8
* audio_data
= NULL
;
483 // Contains how much new data we can write to the buffer without
484 // the risk of overwriting previously written data that the audio
485 // engine has not yet read from the buffer.
486 size_t num_available_frames
= 0;
488 if (share_mode_
== AUDCLNT_SHAREMODE_SHARED
) {
489 // Get the padding value which represents the amount of rendering
490 // data that is queued up to play in the endpoint buffer.
491 hr
= audio_client_
->GetCurrentPadding(&num_queued_frames
);
492 num_available_frames
=
493 endpoint_buffer_size_frames_
- num_queued_frames
;
495 DLOG(ERROR
) << "Failed to retrieve amount of available space: "
500 // While the stream is running, the system alternately sends one
501 // buffer or the other to the client. This form of double buffering
502 // is referred to as "ping-ponging". Each time the client receives
503 // a buffer from the system (triggers this event) the client must
504 // process the entire buffer. Calls to the GetCurrentPadding method
505 // are unnecessary because the packet size must always equal the
506 // buffer size. In contrast to the shared mode buffering scheme,
507 // the latency for an event-driven, exclusive-mode stream depends
508 // directly on the buffer size.
509 num_available_frames
= endpoint_buffer_size_frames_
;
512 // Check if there is enough available space to fit the packet size
513 // specified by the client.
514 if (num_available_frames
< packet_size_frames_
)
517 DLOG_IF(ERROR
, num_available_frames
% packet_size_frames_
!= 0)
518 << "Non-perfect timing detected (num_available_frames="
519 << num_available_frames
<< ", packet_size_frames="
520 << packet_size_frames_
<< ")";
522 // Derive the number of packets we need to get from the client to
523 // fill up the available area in the endpoint buffer.
524 // |num_packets| will always be one for exclusive-mode streams and
525 // will be one in most cases for shared mode streams as well.
526 // However, we have found that two packets can sometimes be
528 size_t num_packets
= (num_available_frames
/ packet_size_frames_
);
530 for (size_t n
= 0; n
< num_packets
; ++n
) {
531 // Grab all available space in the rendering endpoint buffer
532 // into which the client can write a data packet.
533 hr
= audio_render_client_
->GetBuffer(packet_size_frames_
,
536 DLOG(ERROR
) << "Failed to use rendering audio buffer: "
541 // Derive the audio delay which corresponds to the delay between
542 // a render event and the time when the first audio sample in a
543 // packet is played out through the speaker. This delay value
544 // can typically be utilized by an acoustic echo-control (AEC)
545 // unit at the render side.
547 int audio_delay_bytes
= 0;
548 hr
= audio_clock
->GetPosition(&position
, NULL
);
550 // Stream position of the sample that is currently playing
551 // through the speaker.
552 double pos_sample_playing_frames
= format_
.Format
.nSamplesPerSec
*
553 (static_cast<double>(position
) / device_frequency
);
555 // Stream position of the last sample written to the endpoint
556 // buffer. Note that, the packet we are about to receive in
557 // the upcoming callback is also included.
558 size_t pos_last_sample_written_frames
=
559 num_written_frames_
+ packet_size_frames_
;
561 // Derive the actual delay value which will be fed to the
562 // render client using the OnMoreData() callback.
563 audio_delay_bytes
= (pos_last_sample_written_frames
-
564 pos_sample_playing_frames
) * format_
.Format
.nBlockAlign
;
567 // Read a data packet from the registered client source and
568 // deliver a delay estimate in the same callback to the client.
569 // A time stamp is also stored in the AudioBuffersState. This
570 // time stamp can be used at the client side to compensate for
571 // the delay between the usage of the delay value and the time
574 int frames_filled
= source_
->OnMoreData(
575 audio_bus_
.get(), AudioBuffersState(0, audio_delay_bytes
));
576 uint32 num_filled_bytes
= frames_filled
* format_
.Format
.nBlockAlign
;
577 DCHECK_LE(num_filled_bytes
, packet_size_bytes_
);
579 // Note: If this ever changes to output raw float the data must be
580 // clipped and sanitized since it may come from an untrusted
581 // source such as NaCl.
582 const int bytes_per_sample
= format_
.Format
.wBitsPerSample
>> 3;
583 audio_bus_
->Scale(volume_
);
584 audio_bus_
->ToInterleaved(
585 frames_filled
, bytes_per_sample
, audio_data
);
588 // Release the buffer space acquired in the GetBuffer() call.
589 // Render silence if we were not able to fill up the buffer totally.
590 DWORD flags
= (num_filled_bytes
< packet_size_bytes_
) ?
591 AUDCLNT_BUFFERFLAGS_SILENT
: 0;
592 audio_render_client_
->ReleaseBuffer(packet_size_frames_
, flags
);
594 num_written_frames_
+= packet_size_frames_
;
598 void WASAPIAudioOutputStream::HandleError(HRESULT err
) {
599 CHECK((started() && GetCurrentThreadId() == render_thread_
->tid()) ||
600 (!started() && GetCurrentThreadId() == creating_thread_id_
));
601 NOTREACHED() << "Error code: " << std::hex
<< err
;
603 source_
->OnError(this);
606 HRESULT
WASAPIAudioOutputStream::ExclusiveModeInitialization(
607 IAudioClient
* client
, HANDLE event_handle
, uint32
* endpoint_buffer_size
) {
608 DCHECK_EQ(share_mode_
, AUDCLNT_SHAREMODE_EXCLUSIVE
);
610 float f
= (1000.0 * packet_size_frames_
) / format_
.Format
.nSamplesPerSec
;
611 REFERENCE_TIME requested_buffer_duration
=
612 static_cast<REFERENCE_TIME
>(f
* 10000.0 + 0.5);
614 DWORD stream_flags
= AUDCLNT_STREAMFLAGS_NOPERSIST
;
615 bool use_event
= (event_handle
!= NULL
&&
616 event_handle
!= INVALID_HANDLE_VALUE
);
618 stream_flags
|= AUDCLNT_STREAMFLAGS_EVENTCALLBACK
;
619 VLOG(2) << "stream_flags: 0x" << std::hex
<< stream_flags
;
621 // Initialize the audio stream between the client and the device.
622 // For an exclusive-mode stream that uses event-driven buffering, the
623 // caller must specify nonzero values for hnsPeriodicity and
624 // hnsBufferDuration, and the values of these two parameters must be equal.
625 // The Initialize method allocates two buffers for the stream. Each buffer
626 // is equal in duration to the value of the hnsBufferDuration parameter.
627 // Following the Initialize call for a rendering stream, the caller should
628 // fill the first of the two buffers before starting the stream.
629 HRESULT hr
= S_FALSE
;
630 hr
= client
->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE
,
632 requested_buffer_duration
,
633 requested_buffer_duration
,
634 reinterpret_cast<WAVEFORMATEX
*>(&format_
),
637 if (hr
== AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED
) {
638 LOG(ERROR
) << "AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED";
640 UINT32 aligned_buffer_size
= 0;
641 client
->GetBufferSize(&aligned_buffer_size
);
642 VLOG(1) << "Use aligned buffer size instead: " << aligned_buffer_size
;
644 // Calculate new aligned periodicity. Each unit of reference time
645 // is 100 nanoseconds.
646 REFERENCE_TIME aligned_buffer_duration
= static_cast<REFERENCE_TIME
>(
647 (10000000.0 * aligned_buffer_size
/ format_
.Format
.nSamplesPerSec
)
650 // It is possible to re-activate and re-initialize the audio client
651 // at this stage but we bail out with an error code instead and
652 // combine it with a log message which informs about the suggested
653 // aligned buffer size which should be used instead.
654 VLOG(1) << "aligned_buffer_duration: "
655 << static_cast<double>(aligned_buffer_duration
/ 10000.0)
657 } else if (hr
== AUDCLNT_E_INVALID_DEVICE_PERIOD
) {
658 // We will get this error if we try to use a smaller buffer size than
659 // the minimum supported size (usually ~3ms on Windows 7).
660 LOG(ERROR
) << "AUDCLNT_E_INVALID_DEVICE_PERIOD";
666 hr
= client
->SetEventHandle(event_handle
);
668 VLOG(1) << "IAudioClient::SetEventHandle: " << std::hex
<< hr
;
673 UINT32 buffer_size_in_frames
= 0;
674 hr
= client
->GetBufferSize(&buffer_size_in_frames
);
676 VLOG(1) << "IAudioClient::GetBufferSize: " << std::hex
<< hr
;
680 *endpoint_buffer_size
= buffer_size_in_frames
;
681 VLOG(2) << "endpoint buffer size: " << buffer_size_in_frames
;