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