1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/win/audio_low_latency_input_win.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "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"
15 using base::win::ScopedComPtr
;
16 using base::win::ScopedCOMInitializer
;
21 // Returns true if |device| represents the default communication capture device.
22 bool IsDefaultCommunicationDevice(IMMDeviceEnumerator
* enumerator
,
24 ScopedComPtr
<IMMDevice
> communications
;
25 if (FAILED(enumerator
->GetDefaultAudioEndpoint(eCapture
, eCommunications
,
26 communications
.Receive()))) {
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;
38 WASAPIAudioInputStream::WASAPIAudioInputStream(AudioManagerWin
* manager
,
39 const AudioParameters
& params
,
40 const std::string
& device_id
)
42 capture_thread_(NULL
),
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),
54 audio_bus_(media::AudioBus::Create(params
)) {
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";
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
;
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_
;
79 // All events are auto-reset events and non-signaled initially.
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());
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());
90 ms_to_frame_count_
= static_cast<double>(params
.sample_rate()) / 1000.0;
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
));
97 DLOG(ERROR
) << "High-resolution performance counters are not supported.";
101 WASAPIAudioInputStream::~WASAPIAudioInputStream() {
102 DCHECK(CalledOnValidThread());
105 bool WASAPIAudioInputStream::Open() {
106 DCHECK(CalledOnValidThread());
107 // Verify that we are not already opened.
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();
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();
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.
128 hr
= GetAudioEngineStreamFormat();
131 // Verify that the selected audio endpoint supports the specified format
132 // set during construction.
133 if (!DesiredFormatIsSupported())
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();
140 opened_
= SUCCEEDED(hr
);
144 void WASAPIAudioInputStream::Start(AudioInputCallback
* callback
) {
145 DCHECK(CalledOnValidThread());
147 DLOG_IF(ERROR
, !opened_
) << "Open() has not been called successfully";
157 // Starts periodic AGC microphone measurements if the AGC has been enabled
158 // using SetAutomaticGainControl().
161 // Create and start the thread that will drive the capturing by waiting for
164 new base::DelegateSimpleThread(this, "wasapi_capture_thread");
165 capture_thread_
->Start();
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.";
171 if (SUCCEEDED(hr
) && audio_render_client_for_loopback_
)
172 hr
= audio_render_client_for_loopback_
->Start();
174 started_
= SUCCEEDED(hr
);
177 void WASAPIAudioInputStream::Stop() {
178 DCHECK(CalledOnValidThread());
179 DVLOG(1) << "WASAPIAudioInputStream::Stop()";
183 // Stops periodic AGC microphone measurements.
186 // Shut down the capture thread.
187 if (stop_capture_event_
.IsValid()) {
188 SetEvent(stop_capture_event_
.Get());
191 // Stop the input audio streaming.
192 HRESULT hr
= audio_client_
->Stop();
194 LOG(ERROR
) << "Failed to stop input streaming.";
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
;
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.
214 // Inform the audio manager that we have been closed. This will cause our
216 manager_
->ReleaseInputStream(this);
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";
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.
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);
237 DLOG_IF(ERROR
, !opened_
) << "Open() has not been called successfully";
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
),
245 DLOG_IF(WARNING
, FAILED(hr
)) << "Failed to set new input master volume.";
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.
255 double WASAPIAudioInputStream::GetVolume() {
256 DLOG_IF(ERROR
, !opened_
) << "Open() has not been called successfully";
260 // Retrieve the current volume level. The value is in the range 0.0 to 1.0.
262 HRESULT hr
= simple_audio_volume_
->GetMasterVolume(&level
);
263 DLOG_IF(WARNING
, FAILED(hr
)) << "Failed to get input master volume.";
265 return static_cast<double>(level
);
269 AudioParameters
WASAPIAudioInputStream::GetInputStreamParameters(
270 const std::string
& device_id
) {
271 int sample_rate
= 48000;
272 ChannelLayout channel_layout
= CHANNEL_LAYOUT_STEREO
;
274 base::win::ScopedCoMem
<WAVEFORMATEX
> audio_engine_mix_format
;
275 int effects
= AudioParameters::NO_EFFECTS
;
276 if (SUCCEEDED(GetMixFormat(device_id
, &audio_engine_mix_format
, &effects
))) {
277 sample_rate
= static_cast<int>(audio_engine_mix_format
->nSamplesPerSec
);
278 channel_layout
= audio_engine_mix_format
->nChannels
== 1 ?
279 CHANNEL_LAYOUT_MONO
: CHANNEL_LAYOUT_STEREO
;
282 // Use 10ms frame size as default.
283 int frames_per_buffer
= sample_rate
/ 100;
284 return AudioParameters(
285 AudioParameters::AUDIO_PCM_LOW_LATENCY
, channel_layout
, 0, sample_rate
,
286 16, frames_per_buffer
, effects
);
290 HRESULT
WASAPIAudioInputStream::GetMixFormat(const std::string
& device_id
,
291 WAVEFORMATEX
** device_format
,
295 // It is assumed that this static method is called from a COM thread, i.e.,
296 // CoInitializeEx() is not called here to avoid STA/MTA conflicts.
297 ScopedComPtr
<IMMDeviceEnumerator
> enumerator
;
298 HRESULT hr
= enumerator
.CreateInstance(__uuidof(MMDeviceEnumerator
), NULL
,
299 CLSCTX_INPROC_SERVER
);
303 ScopedComPtr
<IMMDevice
> endpoint_device
;
304 if (device_id
== AudioManagerBase::kDefaultDeviceId
) {
305 // Retrieve the default capture audio endpoint.
306 hr
= enumerator
->GetDefaultAudioEndpoint(eCapture
, eConsole
,
307 endpoint_device
.Receive());
308 } else if (device_id
== AudioManagerBase::kLoopbackInputDeviceId
) {
309 // Get the mix format of the default playback stream.
310 hr
= enumerator
->GetDefaultAudioEndpoint(eRender
, eConsole
,
311 endpoint_device
.Receive());
313 // Retrieve a capture endpoint device that is specified by an endpoint
314 // device-identification string.
315 hr
= enumerator
->GetDevice(base::UTF8ToUTF16(device_id
).c_str(),
316 endpoint_device
.Receive());
322 *effects
= IsDefaultCommunicationDevice(enumerator
, endpoint_device
) ?
323 AudioParameters::DUCKING
: AudioParameters::NO_EFFECTS
;
325 ScopedComPtr
<IAudioClient
> audio_client
;
326 hr
= endpoint_device
->Activate(__uuidof(IAudioClient
),
327 CLSCTX_INPROC_SERVER
,
329 audio_client
.ReceiveVoid());
330 return SUCCEEDED(hr
) ? audio_client
->GetMixFormat(device_format
) : hr
;
333 void WASAPIAudioInputStream::Run() {
334 ScopedCOMInitializer
com_init(ScopedCOMInitializer::kMTA
);
336 // Increase the thread priority.
337 capture_thread_
->SetThreadPriority(base::kThreadPriority_RealtimeAudio
);
339 // Enable MMCSS to ensure that this thread receives prioritized access to
341 DWORD task_index
= 0;
342 HANDLE mm_task
= avrt::AvSetMmThreadCharacteristics(L
"Pro Audio",
345 (mm_task
&& avrt::AvSetMmThreadPriority(mm_task
, AVRT_PRIORITY_CRITICAL
));
347 // Failed to enable MMCSS on this thread. It is not fatal but can lead
348 // to reduced QoS at high load.
349 DWORD err
= GetLastError();
350 LOG(WARNING
) << "Failed to enable MMCSS (error code=" << err
<< ").";
353 // Allocate a buffer with a size that enables us to take care of cases like:
354 // 1) The recorded buffer size is smaller, or does not match exactly with,
355 // the selected packet size used in each callback.
356 // 2) The selected buffer size is larger than the recorded buffer size in
358 size_t buffer_frame_index
= 0;
359 size_t capture_buffer_size
= std::max(
360 2 * endpoint_buffer_size_frames_
* frame_size_
,
361 2 * packet_size_frames_
* frame_size_
);
362 scoped_ptr
<uint8
[]> capture_buffer(new uint8
[capture_buffer_size
]);
364 LARGE_INTEGER now_count
;
365 bool recording
= true;
367 double volume
= GetVolume();
368 HANDLE wait_array
[2] = {stop_capture_event_
, audio_samples_ready_event_
};
370 while (recording
&& !error
) {
371 HRESULT hr
= S_FALSE
;
373 // Wait for a close-down event or a new capture event.
374 DWORD wait_result
= WaitForMultipleObjects(2, wait_array
, FALSE
, INFINITE
);
375 switch (wait_result
) {
379 case WAIT_OBJECT_0
+ 0:
380 // |stop_capture_event_| has been set.
383 case WAIT_OBJECT_0
+ 1:
385 // |audio_samples_ready_event_| has been set.
386 BYTE
* data_ptr
= NULL
;
387 UINT32 num_frames_to_read
= 0;
389 UINT64 device_position
= 0;
390 UINT64 first_audio_frame_timestamp
= 0;
392 // Retrieve the amount of data in the capture endpoint buffer,
393 // replace it with silence if required, create callbacks for each
394 // packet and store non-delivered data for the next event.
395 hr
= audio_capture_client_
->GetBuffer(&data_ptr
,
399 &first_audio_frame_timestamp
);
401 DLOG(ERROR
) << "Failed to get data from the capture buffer";
405 if (num_frames_to_read
!= 0) {
406 size_t pos
= buffer_frame_index
* frame_size_
;
407 size_t num_bytes
= num_frames_to_read
* frame_size_
;
408 DCHECK_GE(capture_buffer_size
, pos
+ num_bytes
);
410 if (flags
& AUDCLNT_BUFFERFLAGS_SILENT
) {
411 // Clear out the local buffer since silence is reported.
412 memset(&capture_buffer
[pos
], 0, num_bytes
);
414 // Copy captured data from audio engine buffer to local buffer.
415 memcpy(&capture_buffer
[pos
], data_ptr
, num_bytes
);
418 buffer_frame_index
+= num_frames_to_read
;
421 hr
= audio_capture_client_
->ReleaseBuffer(num_frames_to_read
);
422 DLOG_IF(ERROR
, FAILED(hr
)) << "Failed to release capture buffer";
424 // Derive a delay estimate for the captured audio packet.
425 // The value contains two parts (A+B), where A is the delay of the
426 // first audio frame in the packet and B is the extra delay
427 // contained in any stored data. Unit is in audio frames.
428 QueryPerformanceCounter(&now_count
);
429 double audio_delay_frames
=
430 ((perf_count_to_100ns_units_
* now_count
.QuadPart
-
431 first_audio_frame_timestamp
) / 10000.0) * ms_to_frame_count_
+
432 buffer_frame_index
- num_frames_to_read
;
434 // Get a cached AGC volume level which is updated once every second
435 // on the audio manager thread. Note that, |volume| is also updated
436 // each time SetVolume() is called through IPC by the render-side AGC.
437 GetAgcVolume(&volume
);
439 // Deliver captured data to the registered consumer using a packet
440 // size which was specified at construction.
441 uint32 delay_frames
= static_cast<uint32
>(audio_delay_frames
+ 0.5);
442 while (buffer_frame_index
>= packet_size_frames_
) {
443 // Copy data to audio bus to match the OnData interface.
444 uint8
* audio_data
= reinterpret_cast<uint8
*>(capture_buffer
.get());
445 audio_bus_
->FromInterleaved(
446 audio_data
, audio_bus_
->frames(), format_
.wBitsPerSample
/ 8);
448 // Deliver data packet, delay estimation and volume level to
451 this, audio_bus_
.get(), delay_frames
* frame_size_
, volume
);
453 // Store parts of the recorded data which can't be delivered
454 // using the current packet size. The stored section will be used
455 // either in the next while-loop iteration or in the next
457 memmove(&capture_buffer
[0],
458 &capture_buffer
[packet_size_bytes_
],
459 (buffer_frame_index
- packet_size_frames_
) * frame_size_
);
461 buffer_frame_index
-= packet_size_frames_
;
462 delay_frames
-= packet_size_frames_
;
472 if (recording
&& error
) {
473 // TODO(henrika): perhaps it worth improving the cleanup here by e.g.
474 // stopping the audio client, joining the thread etc.?
475 NOTREACHED() << "WASAPI capturing failed with error code "
480 if (mm_task
&& !avrt::AvRevertMmThreadCharacteristics(mm_task
)) {
481 PLOG(WARNING
) << "Failed to disable MMCSS";
485 void WASAPIAudioInputStream::HandleError(HRESULT err
) {
486 NOTREACHED() << "Error code: " << err
;
488 sink_
->OnError(this);
491 HRESULT
WASAPIAudioInputStream::SetCaptureDevice() {
492 DCHECK(!endpoint_device_
);
494 ScopedComPtr
<IMMDeviceEnumerator
> enumerator
;
495 HRESULT hr
= enumerator
.CreateInstance(__uuidof(MMDeviceEnumerator
),
496 NULL
, CLSCTX_INPROC_SERVER
);
500 // Retrieve the IMMDevice by using the specified role or the specified
501 // unique endpoint device-identification string.
503 if (effects_
& AudioParameters::DUCKING
) {
504 // Ducking has been requested and it is only supported for the default
505 // communication device. So, let's open up the communication device and
506 // see if the ID of that device matches the requested ID.
507 // We consider a kDefaultDeviceId as well as an explicit device id match,
508 // to be valid matches.
509 hr
= enumerator
->GetDefaultAudioEndpoint(eCapture
, eCommunications
,
510 endpoint_device_
.Receive());
511 if (endpoint_device_
&& device_id_
!= AudioManagerBase::kDefaultDeviceId
) {
512 base::win::ScopedCoMem
<WCHAR
> communications_id
;
513 endpoint_device_
->GetId(&communications_id
);
515 base::WideToUTF8(static_cast<WCHAR
*>(communications_id
))) {
516 DLOG(WARNING
) << "Ducking has been requested for a non-default device."
518 // We can't honor the requested effect flag, so turn it off and
519 // continue. We'll check this flag later to see if we've actually
520 // opened up the communications device, so it's important that it
521 // reflects the active state.
522 effects_
&= ~AudioParameters::DUCKING
;
523 endpoint_device_
.Release(); // Fall back on code below.
528 if (!endpoint_device_
) {
529 if (device_id_
== AudioManagerBase::kDefaultDeviceId
) {
530 // Retrieve the default capture audio endpoint for the specified role.
531 // Note that, in Windows Vista, the MMDevice API supports device roles
532 // but the system-supplied user interface programs do not.
533 hr
= enumerator
->GetDefaultAudioEndpoint(eCapture
, eConsole
,
534 endpoint_device_
.Receive());
535 } else if (device_id_
== AudioManagerBase::kLoopbackInputDeviceId
) {
536 // Capture the default playback stream.
537 hr
= enumerator
->GetDefaultAudioEndpoint(eRender
, eConsole
,
538 endpoint_device_
.Receive());
540 hr
= enumerator
->GetDevice(base::UTF8ToUTF16(device_id_
).c_str(),
541 endpoint_device_
.Receive());
548 // Verify that the audio endpoint device is active, i.e., the audio
549 // adapter that connects to the endpoint device is present and enabled.
550 DWORD state
= DEVICE_STATE_DISABLED
;
551 hr
= endpoint_device_
->GetState(&state
);
555 if (!(state
& DEVICE_STATE_ACTIVE
)) {
556 DLOG(ERROR
) << "Selected capture device is not active.";
563 HRESULT
WASAPIAudioInputStream::ActivateCaptureDevice() {
564 // Creates and activates an IAudioClient COM object given the selected
565 // capture endpoint device.
566 HRESULT hr
= endpoint_device_
->Activate(__uuidof(IAudioClient
),
567 CLSCTX_INPROC_SERVER
,
569 audio_client_
.ReceiveVoid());
573 HRESULT
WASAPIAudioInputStream::GetAudioEngineStreamFormat() {
576 // The GetMixFormat() method retrieves the stream format that the
577 // audio engine uses for its internal processing of shared-mode streams.
578 // The method always uses a WAVEFORMATEXTENSIBLE structure, instead
579 // of a stand-alone WAVEFORMATEX structure, to specify the format.
580 // An WAVEFORMATEXTENSIBLE structure can specify both the mapping of
581 // channels to speakers and the number of bits of precision in each sample.
582 base::win::ScopedCoMem
<WAVEFORMATEXTENSIBLE
> format_ex
;
583 hr
= audio_client_
->GetMixFormat(
584 reinterpret_cast<WAVEFORMATEX
**>(&format_ex
));
586 // See http://msdn.microsoft.com/en-us/windows/hardware/gg463006#EFH
587 // for details on the WAVE file format.
588 WAVEFORMATEX format
= format_ex
->Format
;
589 DVLOG(2) << "WAVEFORMATEX:";
590 DVLOG(2) << " wFormatTags : 0x" << std::hex
<< format
.wFormatTag
;
591 DVLOG(2) << " nChannels : " << format
.nChannels
;
592 DVLOG(2) << " nSamplesPerSec : " << format
.nSamplesPerSec
;
593 DVLOG(2) << " nAvgBytesPerSec: " << format
.nAvgBytesPerSec
;
594 DVLOG(2) << " nBlockAlign : " << format
.nBlockAlign
;
595 DVLOG(2) << " wBitsPerSample : " << format
.wBitsPerSample
;
596 DVLOG(2) << " cbSize : " << format
.cbSize
;
598 DVLOG(2) << "WAVEFORMATEXTENSIBLE:";
599 DVLOG(2) << " wValidBitsPerSample: " <<
600 format_ex
->Samples
.wValidBitsPerSample
;
601 DVLOG(2) << " dwChannelMask : 0x" << std::hex
<<
602 format_ex
->dwChannelMask
;
603 if (format_ex
->SubFormat
== KSDATAFORMAT_SUBTYPE_PCM
)
604 DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_PCM";
605 else if (format_ex
->SubFormat
== KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)
606 DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT";
607 else if (format_ex
->SubFormat
== KSDATAFORMAT_SUBTYPE_WAVEFORMATEX
)
608 DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_WAVEFORMATEX";
613 bool WASAPIAudioInputStream::DesiredFormatIsSupported() {
614 // An application that uses WASAPI to manage shared-mode streams can rely
615 // on the audio engine to perform only limited format conversions. The audio
616 // engine can convert between a standard PCM sample size used by the
617 // application and the floating-point samples that the engine uses for its
618 // internal processing. However, the format for an application stream
619 // typically must have the same number of channels and the same sample
620 // rate as the stream format used by the device.
621 // Many audio devices support both PCM and non-PCM stream formats. However,
622 // the audio engine can mix only PCM streams.
623 base::win::ScopedCoMem
<WAVEFORMATEX
> closest_match
;
624 HRESULT hr
= audio_client_
->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED
,
627 DLOG_IF(ERROR
, hr
== S_FALSE
) << "Format is not supported "
628 << "but a closest match exists.";
632 HRESULT
WASAPIAudioInputStream::InitializeAudioEngine() {
634 // Use event-driven mode only fo regular input devices. For loopback the
635 // EVENTCALLBACK flag is specified when intializing
636 // |audio_render_client_for_loopback_|.
637 if (device_id_
== AudioManagerBase::kLoopbackInputDeviceId
) {
638 flags
= AUDCLNT_STREAMFLAGS_LOOPBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
;
641 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
;
644 // Initialize the audio stream between the client and the device.
645 // We connect indirectly through the audio engine by using shared mode.
646 // Note that, |hnsBufferDuration| is set of 0, which ensures that the
647 // buffer is never smaller than the minimum buffer size needed to ensure
648 // that glitches do not occur between the periodic processing passes.
649 // This setting should lead to lowest possible latency.
650 HRESULT hr
= audio_client_
->Initialize(
651 AUDCLNT_SHAREMODE_SHARED
,
653 0, // hnsBufferDuration
656 (effects_
& AudioParameters::DUCKING
) ? &kCommunicationsSessionId
: NULL
);
661 // Retrieve the length of the endpoint buffer shared between the client
662 // and the audio engine. The buffer length determines the maximum amount
663 // of capture data that the audio engine can read from the endpoint buffer
664 // during a single processing pass.
665 // A typical value is 960 audio frames <=> 20ms @ 48kHz sample rate.
666 hr
= audio_client_
->GetBufferSize(&endpoint_buffer_size_frames_
);
670 DVLOG(1) << "endpoint buffer size: " << endpoint_buffer_size_frames_
674 // The period between processing passes by the audio engine is fixed for a
675 // particular audio endpoint device and represents the smallest processing
676 // quantum for the audio engine. This period plus the stream latency between
677 // the buffer and endpoint device represents the minimum possible latency
678 // that an audio application can achieve.
679 // TODO(henrika): possibly remove this section when all parts are ready.
680 REFERENCE_TIME device_period_shared_mode
= 0;
681 REFERENCE_TIME device_period_exclusive_mode
= 0;
682 HRESULT hr_dbg
= audio_client_
->GetDevicePeriod(
683 &device_period_shared_mode
, &device_period_exclusive_mode
);
684 if (SUCCEEDED(hr_dbg
)) {
685 DVLOG(1) << "device period: "
686 << static_cast<double>(device_period_shared_mode
/ 10000.0)
690 REFERENCE_TIME latency
= 0;
691 hr_dbg
= audio_client_
->GetStreamLatency(&latency
);
692 if (SUCCEEDED(hr_dbg
)) {
693 DVLOG(1) << "stream latency: " << static_cast<double>(latency
/ 10000.0)
698 // Set the event handle that the audio engine will signal each time a buffer
699 // becomes ready to be processed by the client.
701 // In loopback case the capture device doesn't receive any events, so we
702 // need to create a separate playback client to get notifications. According
705 // A pull-mode capture client does not receive any events when a stream is
706 // initialized with event-driven buffering and is loopback-enabled. To
707 // work around this, initialize a render stream in event-driven mode. Each
708 // time the client receives an event for the render stream, it must signal
709 // the capture client to run the capture thread that reads the next set of
710 // samples from the capture endpoint buffer.
712 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd316551(v=vs.85).aspx
713 if (device_id_
== AudioManagerBase::kLoopbackInputDeviceId
) {
714 hr
= endpoint_device_
->Activate(
715 __uuidof(IAudioClient
), CLSCTX_INPROC_SERVER
, NULL
,
716 audio_render_client_for_loopback_
.ReceiveVoid());
720 hr
= audio_render_client_for_loopback_
->Initialize(
721 AUDCLNT_SHAREMODE_SHARED
,
722 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
723 0, 0, &format_
, NULL
);
727 hr
= audio_render_client_for_loopback_
->SetEventHandle(
728 audio_samples_ready_event_
.Get());
730 hr
= audio_client_
->SetEventHandle(audio_samples_ready_event_
.Get());
736 // Get access to the IAudioCaptureClient interface. This interface
737 // enables us to read input data from the capture endpoint buffer.
738 hr
= audio_client_
->GetService(__uuidof(IAudioCaptureClient
),
739 audio_capture_client_
.ReceiveVoid());
743 // Obtain a reference to the ISimpleAudioVolume interface which enables
744 // us to control the master volume level of an audio session.
745 hr
= audio_client_
->GetService(__uuidof(ISimpleAudioVolume
),
746 simple_audio_volume_
.ReceiveVoid());