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"
13 using base::win::ScopedComPtr;
14 using base::win::ScopedCOMInitializer;
18 WASAPIAudioInputStream::WASAPIAudioInputStream(
19 AudioManagerWin* manager,
20 const AudioParameters& params,
21 const std::string& device_id)
23 capture_thread_(NULL),
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),
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";
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;
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_;
59 // All events are auto-reset events and non-signaled initially.
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());
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());
70 ms_to_frame_count_ = static_cast<double>(params.sample_rate()) / 1000.0;
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));
77 DLOG(ERROR) << "High-resolution performance counters are not supported.";
81 WASAPIAudioInputStream::~WASAPIAudioInputStream() {}
83 bool WASAPIAudioInputStream::Open() {
84 DCHECK(CalledOnValidThread());
85 // Verify that we are not already opened.
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();
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();
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.
106 hr = GetAudioEngineStreamFormat();
109 // Verify that the selected audio endpoint supports the specified format
110 // set during construction.
111 if (!DesiredFormatIsSupported())
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();
118 opened_ = SUCCEEDED(hr);
122 void WASAPIAudioInputStream::Start(AudioInputCallback* callback) {
123 DCHECK(CalledOnValidThread());
125 DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
135 // Starts periodic AGC microphone measurements if the AGC has been enabled
136 // using SetAutomaticGainControl().
139 // Create and start the thread that will drive the capturing by waiting for
142 new base::DelegateSimpleThread(this, "wasapi_capture_thread");
143 capture_thread_->Start();
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.";
149 if (SUCCEEDED(hr) && audio_render_client_for_loopback_)
150 hr = audio_render_client_for_loopback_->Start();
152 started_ = SUCCEEDED(hr);
155 void WASAPIAudioInputStream::Stop() {
156 DCHECK(CalledOnValidThread());
157 DVLOG(1) << "WASAPIAudioInputStream::Stop()";
161 // Stops periodic AGC microphone measurements.
164 // Shut down the capture thread.
165 if (stop_capture_event_.IsValid()) {
166 SetEvent(stop_capture_event_.Get());
169 // Stop the input audio streaming.
170 HRESULT hr = audio_client_->Stop();
172 LOG(ERROR) << "Failed to stop input streaming.";
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;
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.
192 // Inform the audio manager that we have been closed. This will cause our
194 manager_->ReleaseInputStream(this);
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";
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.
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);
215 DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
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),
223 DLOG_IF(WARNING, FAILED(hr)) << "Failed to set new input master volume.";
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.
233 double WASAPIAudioInputStream::GetVolume() {
234 DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
238 // Retrieve the current volume level. The value is in the range 0.0 to 1.0.
240 HRESULT hr = simple_audio_volume_->GetMasterVolume(&level);
241 DLOG_IF(WARNING, FAILED(hr)) << "Failed to get input master volume.";
243 return static_cast<double>(level);
247 AudioParameters WASAPIAudioInputStream::GetInputStreamParameters(
248 const std::string& device_id) {
249 int sample_rate = 48000;
250 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
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;
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;
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);
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);
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());
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());
300 ScopedComPtr<IAudioClient> audio_client;
301 hr = endpoint_device->Activate(__uuidof(IAudioClient),
302 CLSCTX_INPROC_SERVER,
304 audio_client.ReceiveVoid());
305 return SUCCEEDED(hr) ? audio_client->GetMixFormat(device_format) : hr;
308 void WASAPIAudioInputStream::Run() {
309 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA);
311 // Increase the thread priority.
312 capture_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio);
314 // Enable MMCSS to ensure that this thread receives prioritized access to
316 DWORD task_index = 0;
317 HANDLE mm_task = avrt::AvSetMmThreadCharacteristics(L"Pro Audio",
320 (mm_task && avrt::AvSetMmThreadPriority(mm_task, AVRT_PRIORITY_CRITICAL));
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 << ").";
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
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]);
339 LARGE_INTEGER now_count;
340 bool recording = true;
342 double volume = GetVolume();
343 HANDLE wait_array[2] = {stop_capture_event_, audio_samples_ready_event_};
345 while (recording && !error) {
346 HRESULT hr = S_FALSE;
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) {
354 case WAIT_OBJECT_0 + 0:
355 // |stop_capture_event_| has been set.
358 case WAIT_OBJECT_0 + 1:
360 // |audio_samples_ready_event_| has been set.
361 BYTE* data_ptr = NULL;
362 UINT32 num_frames_to_read = 0;
364 UINT64 device_position = 0;
365 UINT64 first_audio_frame_timestamp = 0;
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,
374 &first_audio_frame_timestamp);
376 DLOG(ERROR) << "Failed to get data from the capture buffer";
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);
385 if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
386 // Clear out the local buffer since silence is reported.
387 memset(&capture_buffer[pos], 0, num_bytes);
389 // Copy captured data from audio engine buffer to local buffer.
390 memcpy(&capture_buffer[pos], data_ptr, num_bytes);
393 buffer_frame_index += num_frames_to_read;
396 hr = audio_capture_client_->ReleaseBuffer(num_frames_to_read);
397 DLOG_IF(ERROR, FAILED(hr)) << "Failed to release capture buffer";
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;
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);
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_) {
419 reinterpret_cast<uint8*>(capture_buffer.get());
421 // Deliver data packet, delay estimation and volume level to
426 delay_frames * frame_size_,
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
433 memmove(&capture_buffer[0],
434 &capture_buffer[packet_size_bytes_],
435 (buffer_frame_index - packet_size_frames_) * frame_size_);
437 buffer_frame_index -= packet_size_frames_;
438 delay_frames -= packet_size_frames_;
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 "
456 if (mm_task && !avrt::AvRevertMmThreadCharacteristics(mm_task)) {
457 PLOG(WARNING) << "Failed to disable MMCSS";
461 void WASAPIAudioInputStream::HandleError(HRESULT err) {
462 NOTREACHED() << "Error code: " << err;
464 sink_->OnError(this);
467 HRESULT WASAPIAudioInputStream::SetCaptureDevice() {
468 ScopedComPtr<IMMDeviceEnumerator> enumerator;
469 HRESULT hr = enumerator.CreateInstance(__uuidof(MMDeviceEnumerator),
470 NULL, CLSCTX_INPROC_SERVER);
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.
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;
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());
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."
502 hr = enumerator->GetDevice(base::UTF8ToUTF16(device_id_).c_str(),
503 endpoint_device_.Receive());
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);
516 if (!(state & DEVICE_STATE_ACTIVE)) {
517 DLOG(ERROR) << "Selected capture device is not active.";
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,
530 audio_client_.ReceiveVoid());
534 HRESULT WASAPIAudioInputStream::GetAudioEngineStreamFormat() {
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));
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;
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";
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,
588 DLOG_IF(ERROR, hr == S_FALSE) << "Format is not supported "
589 << "but a closest match exists.";
593 HRESULT WASAPIAudioInputStream::InitializeAudioEngine() {
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;
602 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST;
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,
613 0, // hnsBufferDuration
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_);
629 DVLOG(1) << "endpoint buffer size: " << endpoint_buffer_size_frames_
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)
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)
657 // Set the event handle that the audio engine will signal each time a buffer
658 // becomes ready to be processed by the client.
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
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.
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());
679 hr = audio_render_client_for_loopback_->Initialize(
680 AUDCLNT_SHAREMODE_SHARED,
681 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
682 0, 0, &format_, NULL);
686 hr = audio_render_client_for_loopback_->SetEventHandle(
687 audio_samples_ready_event_.Get());
689 hr = audio_client_->SetEventHandle(audio_samples_ready_event_.Get());
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());
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());