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