Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / audio / win / audio_low_latency_input_win.cc
blob2939522df500ac0e6105f6219b35bd57ecced65b
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_input_win.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/trace_event/trace_event.h"
11 #include "media/audio/win/audio_manager_win.h"
12 #include "media/audio/win/avrt_wrapper_win.h"
13 #include "media/audio/win/core_audio_util_win.h"
14 #include "media/base/audio_bus.h"
16 using base::win::ScopedComPtr;
17 using base::win::ScopedCOMInitializer;
19 namespace media {
21 WASAPIAudioInputStream::WASAPIAudioInputStream(AudioManagerWin* manager,
22 const AudioParameters& params,
23 const std::string& device_id)
24 : manager_(manager),
25 capture_thread_(NULL),
26 opened_(false),
27 started_(false),
28 frame_size_(0),
29 packet_size_frames_(0),
30 packet_size_bytes_(0),
31 endpoint_buffer_size_frames_(0),
32 device_id_(device_id),
33 perf_count_to_100ns_units_(0.0),
34 ms_to_frame_count_(0.0),
35 sink_(NULL),
36 audio_bus_(media::AudioBus::Create(params)) {
37 DCHECK(manager_);
39 // Load the Avrt DLL if not already loaded. Required to support MMCSS.
40 bool avrt_init = avrt::Initialize();
41 DCHECK(avrt_init) << "Failed to load the Avrt.dll";
43 // Set up the desired capture format specified by the client.
44 format_.nSamplesPerSec = params.sample_rate();
45 format_.wFormatTag = WAVE_FORMAT_PCM;
46 format_.wBitsPerSample = params.bits_per_sample();
47 format_.nChannels = params.channels();
48 format_.nBlockAlign = (format_.wBitsPerSample / 8) * format_.nChannels;
49 format_.nAvgBytesPerSec = format_.nSamplesPerSec * format_.nBlockAlign;
50 format_.cbSize = 0;
52 // Size in bytes of each audio frame.
53 frame_size_ = format_.nBlockAlign;
54 // Store size of audio packets which we expect to get from the audio
55 // endpoint device in each capture event.
56 packet_size_frames_ = params.GetBytesPerBuffer() / format_.nBlockAlign;
57 packet_size_bytes_ = params.GetBytesPerBuffer();
58 DVLOG(1) << "Number of bytes per audio frame : " << frame_size_;
59 DVLOG(1) << "Number of audio frames per packet: " << packet_size_frames_;
61 // All events are auto-reset events and non-signaled initially.
63 // Create the event which the audio engine will signal each time
64 // a buffer becomes ready to be processed by the client.
65 audio_samples_ready_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
66 DCHECK(audio_samples_ready_event_.IsValid());
68 // Create the event which will be set in Stop() when capturing shall stop.
69 stop_capture_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
70 DCHECK(stop_capture_event_.IsValid());
72 ms_to_frame_count_ = static_cast<double>(params.sample_rate()) / 1000.0;
74 LARGE_INTEGER performance_frequency;
75 if (QueryPerformanceFrequency(&performance_frequency)) {
76 perf_count_to_100ns_units_ =
77 (10000000.0 / static_cast<double>(performance_frequency.QuadPart));
78 } else {
79 DLOG(ERROR) << "High-resolution performance counters are not supported.";
83 WASAPIAudioInputStream::~WASAPIAudioInputStream() {
84 DCHECK(CalledOnValidThread());
87 bool WASAPIAudioInputStream::Open() {
88 DCHECK(CalledOnValidThread());
89 // Verify that we are not already opened.
90 if (opened_)
91 return false;
93 // Obtain a reference to the IMMDevice interface of the capturing
94 // device with the specified unique identifier or role which was
95 // set at construction.
96 HRESULT hr = SetCaptureDevice();
97 if (FAILED(hr))
98 return false;
100 // Obtain an IAudioClient interface which enables us to create and initialize
101 // an audio stream between an audio application and the audio engine.
102 hr = ActivateCaptureDevice();
103 if (FAILED(hr))
104 return false;
106 // Retrieve the stream format which the audio engine uses for its internal
107 // processing/mixing of shared-mode streams. This function call is for
108 // diagnostic purposes only and only in debug mode.
109 #ifndef NDEBUG
110 hr = GetAudioEngineStreamFormat();
111 #endif
113 // Verify that the selected audio endpoint supports the specified format
114 // set during construction.
115 if (!DesiredFormatIsSupported())
116 return false;
118 // Initialize the audio stream between the client and the device using
119 // shared mode and a lowest possible glitch-free latency.
120 hr = InitializeAudioEngine();
122 opened_ = SUCCEEDED(hr);
123 return opened_;
126 void WASAPIAudioInputStream::Start(AudioInputCallback* callback) {
127 DCHECK(CalledOnValidThread());
128 DCHECK(callback);
129 DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
130 if (!opened_)
131 return;
133 if (started_)
134 return;
136 DCHECK(!sink_);
137 sink_ = callback;
139 // Starts periodic AGC microphone measurements if the AGC has been enabled
140 // using SetAutomaticGainControl().
141 StartAgc();
143 // Create and start the thread that will drive the capturing by waiting for
144 // capture events.
145 capture_thread_ = new base::DelegateSimpleThread(
146 this, "wasapi_capture_thread",
147 base::SimpleThread::Options(base::ThreadPriority::REALTIME_AUDIO));
148 capture_thread_->Start();
150 // Start streaming data between the endpoint buffer and the audio engine.
151 HRESULT hr = audio_client_->Start();
152 DLOG_IF(ERROR, FAILED(hr)) << "Failed to start input streaming.";
154 if (SUCCEEDED(hr) && audio_render_client_for_loopback_.get())
155 hr = audio_render_client_for_loopback_->Start();
157 started_ = SUCCEEDED(hr);
160 void WASAPIAudioInputStream::Stop() {
161 DCHECK(CalledOnValidThread());
162 DVLOG(1) << "WASAPIAudioInputStream::Stop()";
163 if (!started_)
164 return;
166 // Stops periodic AGC microphone measurements.
167 StopAgc();
169 // Shut down the capture thread.
170 if (stop_capture_event_.IsValid()) {
171 SetEvent(stop_capture_event_.Get());
174 // Stop the input audio streaming.
175 HRESULT hr = audio_client_->Stop();
176 if (FAILED(hr)) {
177 LOG(ERROR) << "Failed to stop input streaming.";
180 // Wait until the thread completes and perform cleanup.
181 if (capture_thread_) {
182 SetEvent(stop_capture_event_.Get());
183 capture_thread_->Join();
184 capture_thread_ = NULL;
187 started_ = false;
188 sink_ = NULL;
191 void WASAPIAudioInputStream::Close() {
192 DVLOG(1) << "WASAPIAudioInputStream::Close()";
193 // It is valid to call Close() before calling open or Start().
194 // It is also valid to call Close() after Start() has been called.
195 Stop();
197 // Inform the audio manager that we have been closed. This will cause our
198 // destruction.
199 manager_->ReleaseInputStream(this);
202 double WASAPIAudioInputStream::GetMaxVolume() {
203 // Verify that Open() has been called succesfully, to ensure that an audio
204 // session exists and that an ISimpleAudioVolume interface has been created.
205 DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
206 if (!opened_)
207 return 0.0;
209 // The effective volume value is always in the range 0.0 to 1.0, hence
210 // we can return a fixed value (=1.0) here.
211 return 1.0;
214 void WASAPIAudioInputStream::SetVolume(double volume) {
215 DVLOG(1) << "SetVolume(volume=" << volume << ")";
216 DCHECK(CalledOnValidThread());
217 DCHECK_GE(volume, 0.0);
218 DCHECK_LE(volume, 1.0);
220 DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
221 if (!opened_)
222 return;
224 // Set a new master volume level. Valid volume levels are in the range
225 // 0.0 to 1.0. Ignore volume-change events.
226 HRESULT hr =
227 simple_audio_volume_->SetMasterVolume(static_cast<float>(volume), NULL);
228 if (FAILED(hr))
229 DLOG(WARNING) << "Failed to set new input master volume.";
231 // Update the AGC volume level based on the last setting above. Note that,
232 // the volume-level resolution is not infinite and it is therefore not
233 // possible to assume that the volume provided as input parameter can be
234 // used directly. Instead, a new query to the audio hardware is required.
235 // This method does nothing if AGC is disabled.
236 UpdateAgcVolume();
239 double WASAPIAudioInputStream::GetVolume() {
240 DCHECK(opened_) << "Open() has not been called successfully";
241 if (!opened_)
242 return 0.0;
244 // Retrieve the current volume level. The value is in the range 0.0 to 1.0.
245 float level = 0.0f;
246 HRESULT hr = simple_audio_volume_->GetMasterVolume(&level);
247 if (FAILED(hr))
248 DLOG(WARNING) << "Failed to get input master volume.";
250 return static_cast<double>(level);
253 bool WASAPIAudioInputStream::IsMuted() {
254 DCHECK(opened_) << "Open() has not been called successfully";
255 DCHECK(CalledOnValidThread());
256 if (!opened_)
257 return false;
259 // Retrieves the current muting state for the audio session.
260 BOOL is_muted = FALSE;
261 HRESULT hr = simple_audio_volume_->GetMute(&is_muted);
262 if (FAILED(hr))
263 DLOG(WARNING) << "Failed to get input master volume.";
265 return is_muted != FALSE;
268 void WASAPIAudioInputStream::Run() {
269 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA);
271 // Enable MMCSS to ensure that this thread receives prioritized access to
272 // CPU resources.
273 DWORD task_index = 0;
274 HANDLE mm_task = avrt::AvSetMmThreadCharacteristics(L"Pro Audio",
275 &task_index);
276 bool mmcss_is_ok =
277 (mm_task && avrt::AvSetMmThreadPriority(mm_task, AVRT_PRIORITY_CRITICAL));
278 if (!mmcss_is_ok) {
279 // Failed to enable MMCSS on this thread. It is not fatal but can lead
280 // to reduced QoS at high load.
281 DWORD err = GetLastError();
282 LOG(WARNING) << "Failed to enable MMCSS (error code=" << err << ").";
285 // Allocate a buffer with a size that enables us to take care of cases like:
286 // 1) The recorded buffer size is smaller, or does not match exactly with,
287 // the selected packet size used in each callback.
288 // 2) The selected buffer size is larger than the recorded buffer size in
289 // each event.
290 size_t buffer_frame_index = 0;
291 size_t capture_buffer_size = std::max(
292 2 * endpoint_buffer_size_frames_ * frame_size_,
293 2 * packet_size_frames_ * frame_size_);
294 scoped_ptr<uint8[]> capture_buffer(new uint8[capture_buffer_size]);
296 LARGE_INTEGER now_count = {};
297 bool recording = true;
298 bool error = false;
299 double volume = GetVolume();
300 HANDLE wait_array[2] =
301 { stop_capture_event_.Get(), audio_samples_ready_event_.Get() };
303 base::win::ScopedComPtr<IAudioClock> audio_clock;
304 audio_client_->GetService(__uuidof(IAudioClock), audio_clock.ReceiveVoid());
306 while (recording && !error) {
307 HRESULT hr = S_FALSE;
309 // Wait for a close-down event or a new capture event.
310 DWORD wait_result = WaitForMultipleObjects(2, wait_array, FALSE, INFINITE);
311 switch (wait_result) {
312 case WAIT_FAILED:
313 error = true;
314 break;
315 case WAIT_OBJECT_0 + 0:
316 // |stop_capture_event_| has been set.
317 recording = false;
318 break;
319 case WAIT_OBJECT_0 + 1:
321 TRACE_EVENT0("audio", "WASAPIAudioInputStream::Run_0");
322 // |audio_samples_ready_event_| has been set.
323 BYTE* data_ptr = NULL;
324 UINT32 num_frames_to_read = 0;
325 DWORD flags = 0;
326 UINT64 device_position = 0;
327 UINT64 first_audio_frame_timestamp = 0;
329 // Retrieve the amount of data in the capture endpoint buffer,
330 // replace it with silence if required, create callbacks for each
331 // packet and store non-delivered data for the next event.
332 hr = audio_capture_client_->GetBuffer(&data_ptr,
333 &num_frames_to_read,
334 &flags,
335 &device_position,
336 &first_audio_frame_timestamp);
337 if (FAILED(hr)) {
338 DLOG(ERROR) << "Failed to get data from the capture buffer";
339 continue;
342 if (audio_clock) {
343 // The reported timestamp from GetBuffer is not as reliable as the
344 // clock from the client. We've seen timestamps reported for
345 // USB audio devices, be off by several days. Furthermore we've
346 // seen them jump back in time every 2 seconds or so.
347 audio_clock->GetPosition(
348 &device_position, &first_audio_frame_timestamp);
352 if (num_frames_to_read != 0) {
353 size_t pos = buffer_frame_index * frame_size_;
354 size_t num_bytes = num_frames_to_read * frame_size_;
355 DCHECK_GE(capture_buffer_size, pos + num_bytes);
357 if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
358 // Clear out the local buffer since silence is reported.
359 memset(&capture_buffer[pos], 0, num_bytes);
360 } else {
361 // Copy captured data from audio engine buffer to local buffer.
362 memcpy(&capture_buffer[pos], data_ptr, num_bytes);
365 buffer_frame_index += num_frames_to_read;
368 hr = audio_capture_client_->ReleaseBuffer(num_frames_to_read);
369 DLOG_IF(ERROR, FAILED(hr)) << "Failed to release capture buffer";
371 // Derive a delay estimate for the captured audio packet.
372 // The value contains two parts (A+B), where A is the delay of the
373 // first audio frame in the packet and B is the extra delay
374 // contained in any stored data. Unit is in audio frames.
375 QueryPerformanceCounter(&now_count);
376 // first_audio_frame_timestamp will be 0 if we didn't get a timestamp.
377 double audio_delay_frames = first_audio_frame_timestamp == 0 ?
378 num_frames_to_read :
379 ((perf_count_to_100ns_units_ * now_count.QuadPart -
380 first_audio_frame_timestamp) / 10000.0) * ms_to_frame_count_ +
381 buffer_frame_index - num_frames_to_read;
383 // Get a cached AGC volume level which is updated once every second
384 // on the audio manager thread. Note that, |volume| is also updated
385 // each time SetVolume() is called through IPC by the render-side AGC.
386 GetAgcVolume(&volume);
388 // Deliver captured data to the registered consumer using a packet
389 // size which was specified at construction.
390 uint32 delay_frames = static_cast<uint32>(audio_delay_frames + 0.5);
391 while (buffer_frame_index >= packet_size_frames_) {
392 // Copy data to audio bus to match the OnData interface.
393 uint8* audio_data = reinterpret_cast<uint8*>(capture_buffer.get());
394 audio_bus_->FromInterleaved(
395 audio_data, audio_bus_->frames(), format_.wBitsPerSample / 8);
397 // Deliver data packet, delay estimation and volume level to
398 // the user.
399 sink_->OnData(
400 this, audio_bus_.get(), delay_frames * frame_size_, volume);
402 // Store parts of the recorded data which can't be delivered
403 // using the current packet size. The stored section will be used
404 // either in the next while-loop iteration or in the next
405 // capture event.
406 // TODO(tommi): If this data will be used in the next capture
407 // event, we will report incorrect delay estimates because
408 // we'll use the one for the captured data that time around
409 // (i.e. in the future).
410 memmove(&capture_buffer[0],
411 &capture_buffer[packet_size_bytes_],
412 (buffer_frame_index - packet_size_frames_) * frame_size_);
414 DCHECK_GE(buffer_frame_index, packet_size_frames_);
415 buffer_frame_index -= packet_size_frames_;
416 if (delay_frames > packet_size_frames_) {
417 delay_frames -= packet_size_frames_;
418 } else {
419 delay_frames = 0;
423 break;
424 default:
425 error = true;
426 break;
430 if (recording && error) {
431 // TODO(henrika): perhaps it worth improving the cleanup here by e.g.
432 // stopping the audio client, joining the thread etc.?
433 NOTREACHED() << "WASAPI capturing failed with error code "
434 << GetLastError();
437 // Disable MMCSS.
438 if (mm_task && !avrt::AvRevertMmThreadCharacteristics(mm_task)) {
439 PLOG(WARNING) << "Failed to disable MMCSS";
443 void WASAPIAudioInputStream::HandleError(HRESULT err) {
444 NOTREACHED() << "Error code: " << err;
445 if (sink_)
446 sink_->OnError(this);
449 HRESULT WASAPIAudioInputStream::SetCaptureDevice() {
450 DCHECK(!endpoint_device_.get());
452 ScopedComPtr<IMMDeviceEnumerator> enumerator;
453 HRESULT hr = enumerator.CreateInstance(__uuidof(MMDeviceEnumerator),
454 NULL, CLSCTX_INPROC_SERVER);
455 if (FAILED(hr))
456 return hr;
458 // Retrieve the IMMDevice by using the specified role or the specified
459 // unique endpoint device-identification string.
461 if (device_id_ == AudioManagerBase::kDefaultDeviceId) {
462 // Retrieve the default capture audio endpoint for the specified role.
463 // Note that, in Windows Vista, the MMDevice API supports device roles
464 // but the system-supplied user interface programs do not.
465 hr = enumerator->GetDefaultAudioEndpoint(eCapture, eConsole,
466 endpoint_device_.Receive());
467 } else if (device_id_ == AudioManagerBase::kCommunicationsDeviceId) {
468 hr = enumerator->GetDefaultAudioEndpoint(eCapture, eCommunications,
469 endpoint_device_.Receive());
470 } else if (device_id_ == AudioManagerBase::kLoopbackInputDeviceId) {
471 // Capture the default playback stream.
472 hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole,
473 endpoint_device_.Receive());
474 } else {
475 hr = enumerator->GetDevice(base::UTF8ToUTF16(device_id_).c_str(),
476 endpoint_device_.Receive());
479 if (FAILED(hr))
480 return hr;
482 // Verify that the audio endpoint device is active, i.e., the audio
483 // adapter that connects to the endpoint device is present and enabled.
484 DWORD state = DEVICE_STATE_DISABLED;
485 hr = endpoint_device_->GetState(&state);
486 if (FAILED(hr))
487 return hr;
489 if (!(state & DEVICE_STATE_ACTIVE)) {
490 DLOG(ERROR) << "Selected capture device is not active.";
491 hr = E_ACCESSDENIED;
494 return hr;
497 HRESULT WASAPIAudioInputStream::ActivateCaptureDevice() {
498 // Creates and activates an IAudioClient COM object given the selected
499 // capture endpoint device.
500 HRESULT hr = endpoint_device_->Activate(__uuidof(IAudioClient),
501 CLSCTX_INPROC_SERVER,
502 NULL,
503 audio_client_.ReceiveVoid());
504 return hr;
507 HRESULT WASAPIAudioInputStream::GetAudioEngineStreamFormat() {
508 HRESULT hr = S_OK;
509 #ifndef NDEBUG
510 // The GetMixFormat() method retrieves the stream format that the
511 // audio engine uses for its internal processing of shared-mode streams.
512 // The method always uses a WAVEFORMATEXTENSIBLE structure, instead
513 // of a stand-alone WAVEFORMATEX structure, to specify the format.
514 // An WAVEFORMATEXTENSIBLE structure can specify both the mapping of
515 // channels to speakers and the number of bits of precision in each sample.
516 base::win::ScopedCoMem<WAVEFORMATEXTENSIBLE> format_ex;
517 hr = audio_client_->GetMixFormat(
518 reinterpret_cast<WAVEFORMATEX**>(&format_ex));
520 // See http://msdn.microsoft.com/en-us/windows/hardware/gg463006#EFH
521 // for details on the WAVE file format.
522 WAVEFORMATEX format = format_ex->Format;
523 DVLOG(2) << "WAVEFORMATEX:";
524 DVLOG(2) << " wFormatTags : 0x" << std::hex << format.wFormatTag;
525 DVLOG(2) << " nChannels : " << format.nChannels;
526 DVLOG(2) << " nSamplesPerSec : " << format.nSamplesPerSec;
527 DVLOG(2) << " nAvgBytesPerSec: " << format.nAvgBytesPerSec;
528 DVLOG(2) << " nBlockAlign : " << format.nBlockAlign;
529 DVLOG(2) << " wBitsPerSample : " << format.wBitsPerSample;
530 DVLOG(2) << " cbSize : " << format.cbSize;
532 DVLOG(2) << "WAVEFORMATEXTENSIBLE:";
533 DVLOG(2) << " wValidBitsPerSample: " <<
534 format_ex->Samples.wValidBitsPerSample;
535 DVLOG(2) << " dwChannelMask : 0x" << std::hex <<
536 format_ex->dwChannelMask;
537 if (format_ex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM)
538 DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_PCM";
539 else if (format_ex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
540 DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT";
541 else if (format_ex->SubFormat == KSDATAFORMAT_SUBTYPE_WAVEFORMATEX)
542 DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_WAVEFORMATEX";
543 #endif
544 return hr;
547 bool WASAPIAudioInputStream::DesiredFormatIsSupported() {
548 // An application that uses WASAPI to manage shared-mode streams can rely
549 // on the audio engine to perform only limited format conversions. The audio
550 // engine can convert between a standard PCM sample size used by the
551 // application and the floating-point samples that the engine uses for its
552 // internal processing. However, the format for an application stream
553 // typically must have the same number of channels and the same sample
554 // rate as the stream format used by the device.
555 // Many audio devices support both PCM and non-PCM stream formats. However,
556 // the audio engine can mix only PCM streams.
557 base::win::ScopedCoMem<WAVEFORMATEX> closest_match;
558 HRESULT hr = audio_client_->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED,
559 &format_,
560 &closest_match);
561 DLOG_IF(ERROR, hr == S_FALSE) << "Format is not supported "
562 << "but a closest match exists.";
563 return (hr == S_OK);
566 HRESULT WASAPIAudioInputStream::InitializeAudioEngine() {
567 DWORD flags;
568 // Use event-driven mode only fo regular input devices. For loopback the
569 // EVENTCALLBACK flag is specified when intializing
570 // |audio_render_client_for_loopback_|.
571 if (device_id_ == AudioManagerBase::kLoopbackInputDeviceId) {
572 flags = AUDCLNT_STREAMFLAGS_LOOPBACK | AUDCLNT_STREAMFLAGS_NOPERSIST;
573 } else {
574 flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST;
577 // Initialize the audio stream between the client and the device.
578 // We connect indirectly through the audio engine by using shared mode.
579 // Note that, |hnsBufferDuration| is set of 0, which ensures that the
580 // buffer is never smaller than the minimum buffer size needed to ensure
581 // that glitches do not occur between the periodic processing passes.
582 // This setting should lead to lowest possible latency.
583 HRESULT hr = audio_client_->Initialize(
584 AUDCLNT_SHAREMODE_SHARED,
585 flags,
586 0, // hnsBufferDuration
588 &format_,
589 device_id_ == AudioManagerBase::kCommunicationsDeviceId ?
590 &kCommunicationsSessionId : nullptr);
592 if (FAILED(hr))
593 return hr;
595 // Retrieve the length of the endpoint buffer shared between the client
596 // and the audio engine. The buffer length determines the maximum amount
597 // of capture data that the audio engine can read from the endpoint buffer
598 // during a single processing pass.
599 // A typical value is 960 audio frames <=> 20ms @ 48kHz sample rate.
600 hr = audio_client_->GetBufferSize(&endpoint_buffer_size_frames_);
601 if (FAILED(hr))
602 return hr;
604 DVLOG(1) << "endpoint buffer size: " << endpoint_buffer_size_frames_
605 << " [frames]";
607 #ifndef NDEBUG
608 // The period between processing passes by the audio engine is fixed for a
609 // particular audio endpoint device and represents the smallest processing
610 // quantum for the audio engine. This period plus the stream latency between
611 // the buffer and endpoint device represents the minimum possible latency
612 // that an audio application can achieve.
613 // TODO(henrika): possibly remove this section when all parts are ready.
614 REFERENCE_TIME device_period_shared_mode = 0;
615 REFERENCE_TIME device_period_exclusive_mode = 0;
616 HRESULT hr_dbg = audio_client_->GetDevicePeriod(
617 &device_period_shared_mode, &device_period_exclusive_mode);
618 if (SUCCEEDED(hr_dbg)) {
619 DVLOG(1) << "device period: "
620 << static_cast<double>(device_period_shared_mode / 10000.0)
621 << " [ms]";
624 REFERENCE_TIME latency = 0;
625 hr_dbg = audio_client_->GetStreamLatency(&latency);
626 if (SUCCEEDED(hr_dbg)) {
627 DVLOG(1) << "stream latency: " << static_cast<double>(latency / 10000.0)
628 << " [ms]";
630 #endif
632 // Set the event handle that the audio engine will signal each time a buffer
633 // becomes ready to be processed by the client.
635 // In loopback case the capture device doesn't receive any events, so we
636 // need to create a separate playback client to get notifications. According
637 // to MSDN:
639 // A pull-mode capture client does not receive any events when a stream is
640 // initialized with event-driven buffering and is loopback-enabled. To
641 // work around this, initialize a render stream in event-driven mode. Each
642 // time the client receives an event for the render stream, it must signal
643 // the capture client to run the capture thread that reads the next set of
644 // samples from the capture endpoint buffer.
646 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd316551(v=vs.85).aspx
647 if (device_id_ == AudioManagerBase::kLoopbackInputDeviceId) {
648 hr = endpoint_device_->Activate(
649 __uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL,
650 audio_render_client_for_loopback_.ReceiveVoid());
651 if (FAILED(hr))
652 return hr;
654 hr = audio_render_client_for_loopback_->Initialize(
655 AUDCLNT_SHAREMODE_SHARED,
656 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
657 0, 0, &format_, NULL);
658 if (FAILED(hr))
659 return hr;
661 hr = audio_render_client_for_loopback_->SetEventHandle(
662 audio_samples_ready_event_.Get());
663 } else {
664 hr = audio_client_->SetEventHandle(audio_samples_ready_event_.Get());
667 if (FAILED(hr))
668 return hr;
670 // Get access to the IAudioCaptureClient interface. This interface
671 // enables us to read input data from the capture endpoint buffer.
672 hr = audio_client_->GetService(__uuidof(IAudioCaptureClient),
673 audio_capture_client_.ReceiveVoid());
674 if (FAILED(hr))
675 return hr;
677 // Obtain a reference to the ISimpleAudioVolume interface which enables
678 // us to control the master volume level of an audio session.
679 hr = audio_client_->GetService(__uuidof(ISimpleAudioVolume),
680 simple_audio_volume_.ReceiveVoid());
681 return hr;
684 } // namespace media