Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / audio / win / audio_low_latency_output_win.cc
blob83bac1625f80a99db61fb912782382cb953ad1b6
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/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/trace_event/trace_event.h"
14 #include "base/win/scoped_propvariant.h"
15 #include "media/audio/win/audio_manager_win.h"
16 #include "media/audio/win/avrt_wrapper_win.h"
17 #include "media/audio/win/core_audio_util_win.h"
18 #include "media/base/limits.h"
19 #include "media/base/media_switches.h"
21 using base::win::ScopedComPtr;
22 using base::win::ScopedCOMInitializer;
23 using base::win::ScopedCoMem;
25 namespace media {
27 // static
28 AUDCLNT_SHAREMODE WASAPIAudioOutputStream::GetShareMode() {
29 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
30 if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio))
31 return AUDCLNT_SHAREMODE_EXCLUSIVE;
32 return AUDCLNT_SHAREMODE_SHARED;
35 // static
36 int WASAPIAudioOutputStream::HardwareSampleRate(const std::string& device_id) {
37 WAVEFORMATPCMEX format;
38 ScopedComPtr<IAudioClient> client;
39 if (device_id.empty()) {
40 client = CoreAudioUtil::CreateDefaultClient(eRender, eConsole);
41 } else {
42 ScopedComPtr<IMMDevice> device(CoreAudioUtil::CreateDevice(device_id));
43 if (!device.get())
44 return 0;
45 client = CoreAudioUtil::CreateClient(device.get());
48 if (!client.get() ||
49 FAILED(CoreAudioUtil::GetSharedModeMixFormat(client.get(), &format)))
50 return 0;
52 return static_cast<int>(format.Format.nSamplesPerSec);
55 WASAPIAudioOutputStream::WASAPIAudioOutputStream(AudioManagerWin* manager,
56 const std::string& device_id,
57 const AudioParameters& params,
58 ERole device_role)
59 : creating_thread_id_(base::PlatformThread::CurrentId()),
60 manager_(manager),
61 format_(),
62 opened_(false),
63 volume_(1.0),
64 packet_size_frames_(0),
65 packet_size_bytes_(0),
66 endpoint_buffer_size_frames_(0),
67 device_id_(device_id),
68 device_role_(device_role),
69 share_mode_(GetShareMode()),
70 num_written_frames_(0),
71 source_(NULL),
72 audio_bus_(AudioBus::Create(params)) {
73 DCHECK(manager_);
75 DVLOG(1) << "WASAPIAudioOutputStream::WASAPIAudioOutputStream()";
76 DVLOG_IF(1, share_mode_ == AUDCLNT_SHAREMODE_EXCLUSIVE)
77 << "Core Audio (WASAPI) EXCLUSIVE MODE is enabled.";
79 // Load the Avrt DLL if not already loaded. Required to support MMCSS.
80 bool avrt_init = avrt::Initialize();
81 DCHECK(avrt_init) << "Failed to load the avrt.dll";
83 // Set up the desired render format specified by the client. We use the
84 // WAVE_FORMAT_EXTENSIBLE structure to ensure that multiple channel ordering
85 // and high precision data can be supported.
87 // Begin with the WAVEFORMATEX structure that specifies the basic format.
88 WAVEFORMATEX* format = &format_.Format;
89 format->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
90 format->nChannels = params.channels();
91 format->nSamplesPerSec = params.sample_rate();
92 format->wBitsPerSample = params.bits_per_sample();
93 format->nBlockAlign = (format->wBitsPerSample / 8) * format->nChannels;
94 format->nAvgBytesPerSec = format->nSamplesPerSec * format->nBlockAlign;
95 format->cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
97 // Add the parts which are unique to WAVE_FORMAT_EXTENSIBLE.
98 format_.Samples.wValidBitsPerSample = params.bits_per_sample();
99 format_.dwChannelMask = CoreAudioUtil::GetChannelConfig(device_id, eRender);
100 format_.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
102 // Store size (in different units) of audio packets which we expect to
103 // get from the audio endpoint device in each render event.
104 packet_size_frames_ = params.frames_per_buffer();
105 packet_size_bytes_ = params.GetBytesPerBuffer();
106 DVLOG(1) << "Number of bytes per audio frame : " << format->nBlockAlign;
107 DVLOG(1) << "Number of audio frames per packet: " << packet_size_frames_;
108 DVLOG(1) << "Number of bytes per packet : " << packet_size_bytes_;
109 DVLOG(1) << "Number of milliseconds per packet: "
110 << params.GetBufferDuration().InMillisecondsF();
112 // All events are auto-reset events and non-signaled initially.
114 // Create the event which the audio engine will signal each time
115 // a buffer becomes ready to be processed by the client.
116 audio_samples_render_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
117 DCHECK(audio_samples_render_event_.IsValid());
119 // Create the event which will be set in Stop() when capturing shall stop.
120 stop_render_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
121 DCHECK(stop_render_event_.IsValid());
124 WASAPIAudioOutputStream::~WASAPIAudioOutputStream() {
125 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_);
128 bool WASAPIAudioOutputStream::Open() {
129 DVLOG(1) << "WASAPIAudioOutputStream::Open()";
130 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_);
131 if (opened_)
132 return true;
134 DCHECK(!audio_client_.get());
135 DCHECK(!audio_render_client_.get());
137 // Will be set to true if we ended up opening the default communications
138 // device.
139 bool communications_device = false;
141 // Create an IAudioClient interface for the default rendering IMMDevice.
142 ScopedComPtr<IAudioClient> audio_client;
143 if (device_id_.empty() ||
144 CoreAudioUtil::DeviceIsDefault(eRender, device_role_, device_id_)) {
145 audio_client = CoreAudioUtil::CreateDefaultClient(eRender, device_role_);
146 communications_device = (device_role_ == eCommunications);
147 } else {
148 ScopedComPtr<IMMDevice> device(CoreAudioUtil::CreateDevice(device_id_));
149 DLOG_IF(ERROR, !device.get()) << "Failed to open device: " << device_id_;
150 if (device.get())
151 audio_client = CoreAudioUtil::CreateClient(device.get());
154 if (!audio_client.get())
155 return false;
157 // Extra sanity to ensure that the provided device format is still valid.
158 if (!CoreAudioUtil::IsFormatSupported(audio_client.get(), share_mode_,
159 &format_)) {
160 LOG(ERROR) << "Audio parameters are not supported.";
161 return false;
164 HRESULT hr = S_FALSE;
165 if (share_mode_ == AUDCLNT_SHAREMODE_SHARED) {
166 // Initialize the audio stream between the client and the device in shared
167 // mode and using event-driven buffer handling.
168 hr = CoreAudioUtil::SharedModeInitialize(
169 audio_client.get(), &format_, audio_samples_render_event_.Get(),
170 &endpoint_buffer_size_frames_,
171 communications_device ? &kCommunicationsSessionId : NULL);
172 if (FAILED(hr))
173 return false;
175 // We know from experience that the best possible callback sequence is
176 // achieved when the packet size (given by the native device period)
177 // is an even divisor of the endpoint buffer size.
178 // Examples: 48kHz => 960 % 480, 44.1kHz => 896 % 448 or 882 % 441.
179 if (endpoint_buffer_size_frames_ % packet_size_frames_ != 0) {
180 LOG(ERROR)
181 << "Bailing out due to non-perfect timing. Buffer size of "
182 << packet_size_frames_ << " is not an even divisor of "
183 << endpoint_buffer_size_frames_;
184 return false;
186 } else {
187 // TODO(henrika): break out to CoreAudioUtil::ExclusiveModeInitialize()
188 // when removing the enable-exclusive-audio flag.
189 hr = ExclusiveModeInitialization(audio_client.get(),
190 audio_samples_render_event_.Get(),
191 &endpoint_buffer_size_frames_);
192 if (FAILED(hr))
193 return false;
195 // The buffer scheme for exclusive mode streams is not designed for max
196 // flexibility. We only allow a "perfect match" between the packet size set
197 // by the user and the actual endpoint buffer size.
198 if (endpoint_buffer_size_frames_ != packet_size_frames_) {
199 LOG(ERROR) << "Bailing out due to non-perfect timing.";
200 return false;
204 // Create an IAudioRenderClient client for an initialized IAudioClient.
205 // The IAudioRenderClient interface enables us to write output data to
206 // a rendering endpoint buffer.
207 ScopedComPtr<IAudioRenderClient> audio_render_client =
208 CoreAudioUtil::CreateRenderClient(audio_client.get());
209 if (!audio_render_client.get())
210 return false;
212 // Store valid COM interfaces.
213 audio_client_ = audio_client;
214 audio_render_client_ = audio_render_client;
216 hr = audio_client_->GetService(__uuidof(IAudioClock),
217 audio_clock_.ReceiveVoid());
218 if (FAILED(hr)) {
219 LOG(ERROR) << "Failed to get IAudioClock service.";
220 return false;
223 opened_ = true;
224 return true;
227 void WASAPIAudioOutputStream::Start(AudioSourceCallback* callback) {
228 DVLOG(1) << "WASAPIAudioOutputStream::Start()";
229 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_);
230 CHECK(callback);
231 CHECK(opened_);
233 if (render_thread_) {
234 CHECK_EQ(callback, source_);
235 return;
238 source_ = callback;
240 // Ensure that the endpoint buffer is prepared with silence.
241 if (share_mode_ == AUDCLNT_SHAREMODE_SHARED) {
242 if (!CoreAudioUtil::FillRenderEndpointBufferWithSilence(
243 audio_client_.get(), audio_render_client_.get())) {
244 LOG(ERROR) << "Failed to prepare endpoint buffers with silence.";
245 callback->OnError(this);
246 return;
249 num_written_frames_ = endpoint_buffer_size_frames_;
251 // Create and start the thread that will drive the rendering by waiting for
252 // render events.
253 render_thread_.reset(new base::DelegateSimpleThread(
254 this, "wasapi_render_thread",
255 base::SimpleThread::Options(base::ThreadPriority::REALTIME_AUDIO)));
256 render_thread_->Start();
257 if (!render_thread_->HasBeenStarted()) {
258 LOG(ERROR) << "Failed to start WASAPI render thread.";
259 StopThread();
260 callback->OnError(this);
261 return;
264 // Start streaming data between the endpoint buffer and the audio engine.
265 HRESULT hr = audio_client_->Start();
266 if (FAILED(hr)) {
267 PLOG(ERROR) << "Failed to start output streaming: " << std::hex << hr;
268 StopThread();
269 callback->OnError(this);
273 void WASAPIAudioOutputStream::Stop() {
274 DVLOG(1) << "WASAPIAudioOutputStream::Stop()";
275 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_);
276 if (!render_thread_)
277 return;
279 // Stop output audio streaming.
280 HRESULT hr = audio_client_->Stop();
281 if (FAILED(hr)) {
282 PLOG(ERROR) << "Failed to stop output streaming: " << std::hex << hr;
283 source_->OnError(this);
286 // Make a local copy of |source_| since StopThread() will clear it.
287 AudioSourceCallback* callback = source_;
288 StopThread();
290 // Flush all pending data and reset the audio clock stream position to 0.
291 hr = audio_client_->Reset();
292 if (FAILED(hr)) {
293 PLOG(ERROR) << "Failed to reset streaming: " << std::hex << hr;
294 callback->OnError(this);
297 // Extra safety check to ensure that the buffers are cleared.
298 // If the buffers are not cleared correctly, the next call to Start()
299 // would fail with AUDCLNT_E_BUFFER_ERROR at IAudioRenderClient::GetBuffer().
300 // This check is is only needed for shared-mode streams.
301 if (share_mode_ == AUDCLNT_SHAREMODE_SHARED) {
302 UINT32 num_queued_frames = 0;
303 audio_client_->GetCurrentPadding(&num_queued_frames);
304 DCHECK_EQ(0u, num_queued_frames);
308 void WASAPIAudioOutputStream::Close() {
309 DVLOG(1) << "WASAPIAudioOutputStream::Close()";
310 DCHECK_EQ(GetCurrentThreadId(), creating_thread_id_);
312 // It is valid to call Close() before calling open or Start().
313 // It is also valid to call Close() after Start() has been called.
314 Stop();
316 // Inform the audio manager that we have been closed. This will cause our
317 // destruction.
318 manager_->ReleaseOutputStream(this);
321 void WASAPIAudioOutputStream::SetVolume(double volume) {
322 DVLOG(1) << "SetVolume(volume=" << volume << ")";
323 float volume_float = static_cast<float>(volume);
324 if (volume_float < 0.0f || volume_float > 1.0f) {
325 return;
327 volume_ = volume_float;
330 void WASAPIAudioOutputStream::GetVolume(double* volume) {
331 DVLOG(1) << "GetVolume()";
332 *volume = static_cast<double>(volume_);
335 void WASAPIAudioOutputStream::Run() {
336 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA);
338 // Enable MMCSS to ensure that this thread receives prioritized access to
339 // CPU resources.
340 DWORD task_index = 0;
341 HANDLE mm_task = avrt::AvSetMmThreadCharacteristics(L"Pro Audio",
342 &task_index);
343 bool mmcss_is_ok =
344 (mm_task && avrt::AvSetMmThreadPriority(mm_task, AVRT_PRIORITY_CRITICAL));
345 if (!mmcss_is_ok) {
346 // Failed to enable MMCSS on this thread. It is not fatal but can lead
347 // to reduced QoS at high load.
348 DWORD err = GetLastError();
349 LOG(WARNING) << "Failed to enable MMCSS (error code=" << err << ").";
352 HRESULT hr = S_FALSE;
354 bool playing = true;
355 bool error = false;
356 HANDLE wait_array[] = { stop_render_event_.Get(),
357 audio_samples_render_event_.Get() };
358 UINT64 device_frequency = 0;
360 // The device frequency is the frequency generated by the hardware clock in
361 // the audio device. The GetFrequency() method reports a constant frequency.
362 hr = audio_clock_->GetFrequency(&device_frequency);
363 error = FAILED(hr);
364 PLOG_IF(ERROR, error) << "Failed to acquire IAudioClock interface: "
365 << std::hex << hr;
367 // Keep rendering audio until the stop event or the stream-switch event
368 // is signaled. An error event can also break the main thread loop.
369 while (playing && !error) {
370 // Wait for a close-down event, stream-switch event or a new render event.
371 DWORD wait_result = WaitForMultipleObjects(arraysize(wait_array),
372 wait_array,
373 FALSE,
374 INFINITE);
376 switch (wait_result) {
377 case WAIT_OBJECT_0 + 0:
378 // |stop_render_event_| has been set.
379 playing = false;
380 break;
381 case WAIT_OBJECT_0 + 1:
382 // |audio_samples_render_event_| has been set.
383 error = !RenderAudioFromSource(device_frequency);
384 break;
385 default:
386 error = true;
387 break;
391 if (playing && error) {
392 // Stop audio rendering since something has gone wrong in our main thread
393 // loop. Note that, we are still in a "started" state, hence a Stop() call
394 // is required to join the thread properly.
395 audio_client_->Stop();
396 PLOG(ERROR) << "WASAPI rendering failed.";
399 // Disable MMCSS.
400 if (mm_task && !avrt::AvRevertMmThreadCharacteristics(mm_task)) {
401 PLOG(WARNING) << "Failed to disable MMCSS";
405 bool WASAPIAudioOutputStream::RenderAudioFromSource(UINT64 device_frequency) {
406 TRACE_EVENT0("audio", "RenderAudioFromSource");
408 HRESULT hr = S_FALSE;
409 UINT32 num_queued_frames = 0;
410 uint8* audio_data = NULL;
412 // Contains how much new data we can write to the buffer without
413 // the risk of overwriting previously written data that the audio
414 // engine has not yet read from the buffer.
415 size_t num_available_frames = 0;
417 if (share_mode_ == AUDCLNT_SHAREMODE_SHARED) {
418 // Get the padding value which represents the amount of rendering
419 // data that is queued up to play in the endpoint buffer.
420 hr = audio_client_->GetCurrentPadding(&num_queued_frames);
421 num_available_frames =
422 endpoint_buffer_size_frames_ - num_queued_frames;
423 if (FAILED(hr)) {
424 DLOG(ERROR) << "Failed to retrieve amount of available space: "
425 << std::hex << hr;
426 return false;
428 } else {
429 // While the stream is running, the system alternately sends one
430 // buffer or the other to the client. This form of double buffering
431 // is referred to as "ping-ponging". Each time the client receives
432 // a buffer from the system (triggers this event) the client must
433 // process the entire buffer. Calls to the GetCurrentPadding method
434 // are unnecessary because the packet size must always equal the
435 // buffer size. In contrast to the shared mode buffering scheme,
436 // the latency for an event-driven, exclusive-mode stream depends
437 // directly on the buffer size.
438 num_available_frames = endpoint_buffer_size_frames_;
441 // Check if there is enough available space to fit the packet size
442 // specified by the client.
443 if (num_available_frames < packet_size_frames_)
444 return true;
446 DLOG_IF(ERROR, num_available_frames % packet_size_frames_ != 0)
447 << "Non-perfect timing detected (num_available_frames="
448 << num_available_frames << ", packet_size_frames="
449 << packet_size_frames_ << ")";
451 // Derive the number of packets we need to get from the client to
452 // fill up the available area in the endpoint buffer.
453 // |num_packets| will always be one for exclusive-mode streams and
454 // will be one in most cases for shared mode streams as well.
455 // However, we have found that two packets can sometimes be
456 // required.
457 size_t num_packets = (num_available_frames / packet_size_frames_);
459 for (size_t n = 0; n < num_packets; ++n) {
460 // Grab all available space in the rendering endpoint buffer
461 // into which the client can write a data packet.
462 hr = audio_render_client_->GetBuffer(packet_size_frames_,
463 &audio_data);
464 if (FAILED(hr)) {
465 DLOG(ERROR) << "Failed to use rendering audio buffer: "
466 << std::hex << hr;
467 return false;
470 // Derive the audio delay which corresponds to the delay between
471 // a render event and the time when the first audio sample in a
472 // packet is played out through the speaker. This delay value
473 // can typically be utilized by an acoustic echo-control (AEC)
474 // unit at the render side.
475 UINT64 position = 0;
476 uint32 audio_delay_bytes = 0;
477 hr = audio_clock_->GetPosition(&position, NULL);
478 if (SUCCEEDED(hr)) {
479 // Stream position of the sample that is currently playing
480 // through the speaker.
481 double pos_sample_playing_frames = format_.Format.nSamplesPerSec *
482 (static_cast<double>(position) / device_frequency);
484 // Stream position of the last sample written to the endpoint
485 // buffer. Note that, the packet we are about to receive in
486 // the upcoming callback is also included.
487 size_t pos_last_sample_written_frames =
488 num_written_frames_ + packet_size_frames_;
490 // Derive the actual delay value which will be fed to the
491 // render client using the OnMoreData() callback.
492 audio_delay_bytes = (pos_last_sample_written_frames -
493 pos_sample_playing_frames) * format_.Format.nBlockAlign;
496 // Read a data packet from the registered client source and
497 // deliver a delay estimate in the same callback to the client.
499 int frames_filled = source_->OnMoreData(
500 audio_bus_.get(), audio_delay_bytes);
501 uint32 num_filled_bytes = frames_filled * format_.Format.nBlockAlign;
502 DCHECK_LE(num_filled_bytes, packet_size_bytes_);
504 // Note: If this ever changes to output raw float the data must be
505 // clipped and sanitized since it may come from an untrusted
506 // source such as NaCl.
507 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3;
508 audio_bus_->Scale(volume_);
509 audio_bus_->ToInterleaved(
510 frames_filled, bytes_per_sample, audio_data);
513 // Release the buffer space acquired in the GetBuffer() call.
514 // Render silence if we were not able to fill up the buffer totally.
515 DWORD flags = (num_filled_bytes < packet_size_bytes_) ?
516 AUDCLNT_BUFFERFLAGS_SILENT : 0;
517 audio_render_client_->ReleaseBuffer(packet_size_frames_, flags);
519 num_written_frames_ += packet_size_frames_;
522 return true;
525 HRESULT WASAPIAudioOutputStream::ExclusiveModeInitialization(
526 IAudioClient* client, HANDLE event_handle, uint32* endpoint_buffer_size) {
527 DCHECK_EQ(share_mode_, AUDCLNT_SHAREMODE_EXCLUSIVE);
529 float f = (1000.0 * packet_size_frames_) / format_.Format.nSamplesPerSec;
530 REFERENCE_TIME requested_buffer_duration =
531 static_cast<REFERENCE_TIME>(f * 10000.0 + 0.5);
533 DWORD stream_flags = AUDCLNT_STREAMFLAGS_NOPERSIST;
534 bool use_event = (event_handle != NULL &&
535 event_handle != INVALID_HANDLE_VALUE);
536 if (use_event)
537 stream_flags |= AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
538 DVLOG(2) << "stream_flags: 0x" << std::hex << stream_flags;
540 // Initialize the audio stream between the client and the device.
541 // For an exclusive-mode stream that uses event-driven buffering, the
542 // caller must specify nonzero values for hnsPeriodicity and
543 // hnsBufferDuration, and the values of these two parameters must be equal.
544 // The Initialize method allocates two buffers for the stream. Each buffer
545 // is equal in duration to the value of the hnsBufferDuration parameter.
546 // Following the Initialize call for a rendering stream, the caller should
547 // fill the first of the two buffers before starting the stream.
548 HRESULT hr = S_FALSE;
549 hr = client->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE,
550 stream_flags,
551 requested_buffer_duration,
552 requested_buffer_duration,
553 reinterpret_cast<WAVEFORMATEX*>(&format_),
554 NULL);
555 if (FAILED(hr)) {
556 if (hr == AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED) {
557 LOG(ERROR) << "AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED";
559 UINT32 aligned_buffer_size = 0;
560 client->GetBufferSize(&aligned_buffer_size);
561 DVLOG(1) << "Use aligned buffer size instead: " << aligned_buffer_size;
563 // Calculate new aligned periodicity. Each unit of reference time
564 // is 100 nanoseconds.
565 REFERENCE_TIME aligned_buffer_duration = static_cast<REFERENCE_TIME>(
566 (10000000.0 * aligned_buffer_size / format_.Format.nSamplesPerSec)
567 + 0.5);
569 // It is possible to re-activate and re-initialize the audio client
570 // at this stage but we bail out with an error code instead and
571 // combine it with a log message which informs about the suggested
572 // aligned buffer size which should be used instead.
573 DVLOG(1) << "aligned_buffer_duration: "
574 << static_cast<double>(aligned_buffer_duration / 10000.0)
575 << " [ms]";
576 } else if (hr == AUDCLNT_E_INVALID_DEVICE_PERIOD) {
577 // We will get this error if we try to use a smaller buffer size than
578 // the minimum supported size (usually ~3ms on Windows 7).
579 LOG(ERROR) << "AUDCLNT_E_INVALID_DEVICE_PERIOD";
581 return hr;
584 if (use_event) {
585 hr = client->SetEventHandle(event_handle);
586 if (FAILED(hr)) {
587 DVLOG(1) << "IAudioClient::SetEventHandle: " << std::hex << hr;
588 return hr;
592 UINT32 buffer_size_in_frames = 0;
593 hr = client->GetBufferSize(&buffer_size_in_frames);
594 if (FAILED(hr)) {
595 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr;
596 return hr;
599 *endpoint_buffer_size = buffer_size_in_frames;
600 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames;
601 return hr;
604 void WASAPIAudioOutputStream::StopThread() {
605 if (render_thread_ ) {
606 if (render_thread_->HasBeenStarted()) {
607 // Wait until the thread completes and perform cleanup.
608 SetEvent(stop_render_event_.Get());
609 render_thread_->Join();
612 render_thread_.reset();
614 // Ensure that we don't quit the main thread loop immediately next
615 // time Start() is called.
616 ResetEvent(stop_render_event_.Get());
619 source_ = NULL;
622 } // namespace media