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