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