This source file includes following definitions.
- IsDefaultCommunicationDevice
- sink_
- Open
- Start
- Stop
- Close
- GetMaxVolume
- SetVolume
- GetVolume
- GetInputStreamParameters
- GetMixFormat
- Run
- HandleError
- SetCaptureDevice
- ActivateCaptureDevice
- GetAudioEngineStreamFormat
- DesiredFormatIsSupported
- InitializeAudioEngine
#include "media/audio/win/audio_low_latency_input_win.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "media/audio/win/audio_manager_win.h"
#include "media/audio/win/avrt_wrapper_win.h"
using base::win::ScopedComPtr;
using base::win::ScopedCOMInitializer;
namespace media {
namespace {
bool IsDefaultCommunicationDevice(IMMDeviceEnumerator* enumerator,
IMMDevice* device) {
ScopedComPtr<IMMDevice> communications;
if (FAILED(enumerator->GetDefaultAudioEndpoint(eCapture, eCommunications,
communications.Receive()))) {
return false;
}
base::win::ScopedCoMem<WCHAR> communications_id, device_id;
device->GetId(&device_id);
communications->GetId(&communications_id);
return lstrcmpW(communications_id, device_id) == 0;
}
}
WASAPIAudioInputStream::WASAPIAudioInputStream(
AudioManagerWin* manager,
const AudioParameters& params,
const std::string& device_id)
: manager_(manager),
capture_thread_(NULL),
opened_(false),
started_(false),
frame_size_(0),
packet_size_frames_(0),
packet_size_bytes_(0),
endpoint_buffer_size_frames_(0),
effects_(params.effects()),
device_id_(device_id),
perf_count_to_100ns_units_(0.0),
ms_to_frame_count_(0.0),
sink_(NULL) {
DCHECK(manager_);
bool avrt_init = avrt::Initialize();
DCHECK(avrt_init) << "Failed to load the Avrt.dll";
format_.nSamplesPerSec = params.sample_rate();
format_.wFormatTag = WAVE_FORMAT_PCM;
format_.wBitsPerSample = params.bits_per_sample();
format_.nChannels = params.channels();
format_.nBlockAlign = (format_.wBitsPerSample / 8) * format_.nChannels;
format_.nAvgBytesPerSec = format_.nSamplesPerSec * format_.nBlockAlign;
format_.cbSize = 0;
frame_size_ = format_.nBlockAlign;
packet_size_frames_ = params.GetBytesPerBuffer() / format_.nBlockAlign;
packet_size_bytes_ = params.GetBytesPerBuffer();
DVLOG(1) << "Number of bytes per audio frame : " << frame_size_;
DVLOG(1) << "Number of audio frames per packet: " << packet_size_frames_;
audio_samples_ready_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
DCHECK(audio_samples_ready_event_.IsValid());
stop_capture_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
DCHECK(stop_capture_event_.IsValid());
ms_to_frame_count_ = static_cast<double>(params.sample_rate()) / 1000.0;
LARGE_INTEGER performance_frequency;
if (QueryPerformanceFrequency(&performance_frequency)) {
perf_count_to_100ns_units_ =
(10000000.0 / static_cast<double>(performance_frequency.QuadPart));
} else {
DLOG(ERROR) << "High-resolution performance counters are not supported.";
}
}
WASAPIAudioInputStream::~WASAPIAudioInputStream() {}
bool WASAPIAudioInputStream::Open() {
DCHECK(CalledOnValidThread());
if (opened_)
return false;
HRESULT hr = SetCaptureDevice();
if (FAILED(hr))
return false;
hr = ActivateCaptureDevice();
if (FAILED(hr))
return false;
#ifndef NDEBUG
hr = GetAudioEngineStreamFormat();
#endif
if (!DesiredFormatIsSupported())
return false;
hr = InitializeAudioEngine();
opened_ = SUCCEEDED(hr);
return opened_;
}
void WASAPIAudioInputStream::Start(AudioInputCallback* callback) {
DCHECK(CalledOnValidThread());
DCHECK(callback);
DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
if (!opened_)
return;
if (started_)
return;
DCHECK(!sink_);
sink_ = callback;
StartAgc();
capture_thread_ =
new base::DelegateSimpleThread(this, "wasapi_capture_thread");
capture_thread_->Start();
HRESULT hr = audio_client_->Start();
DLOG_IF(ERROR, FAILED(hr)) << "Failed to start input streaming.";
if (SUCCEEDED(hr) && audio_render_client_for_loopback_)
hr = audio_render_client_for_loopback_->Start();
started_ = SUCCEEDED(hr);
}
void WASAPIAudioInputStream::Stop() {
DCHECK(CalledOnValidThread());
DVLOG(1) << "WASAPIAudioInputStream::Stop()";
if (!started_)
return;
StopAgc();
if (stop_capture_event_.IsValid()) {
SetEvent(stop_capture_event_.Get());
}
HRESULT hr = audio_client_->Stop();
if (FAILED(hr)) {
LOG(ERROR) << "Failed to stop input streaming.";
}
if (capture_thread_) {
SetEvent(stop_capture_event_.Get());
capture_thread_->Join();
capture_thread_ = NULL;
}
started_ = false;
sink_ = NULL;
}
void WASAPIAudioInputStream::Close() {
DVLOG(1) << "WASAPIAudioInputStream::Close()";
Stop();
manager_->ReleaseInputStream(this);
}
double WASAPIAudioInputStream::GetMaxVolume() {
DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
if (!opened_)
return 0.0;
return 1.0;
}
void WASAPIAudioInputStream::SetVolume(double volume) {
DVLOG(1) << "SetVolume(volume=" << volume << ")";
DCHECK(CalledOnValidThread());
DCHECK_GE(volume, 0.0);
DCHECK_LE(volume, 1.0);
DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
if (!opened_)
return;
HRESULT hr = simple_audio_volume_->SetMasterVolume(static_cast<float>(volume),
NULL);
DLOG_IF(WARNING, FAILED(hr)) << "Failed to set new input master volume.";
UpdateAgcVolume();
}
double WASAPIAudioInputStream::GetVolume() {
DLOG_IF(ERROR, !opened_) << "Open() has not been called successfully";
if (!opened_)
return 0.0;
float level = 0.0f;
HRESULT hr = simple_audio_volume_->GetMasterVolume(&level);
DLOG_IF(WARNING, FAILED(hr)) << "Failed to get input master volume.";
return static_cast<double>(level);
}
AudioParameters WASAPIAudioInputStream::GetInputStreamParameters(
const std::string& device_id) {
int sample_rate = 48000;
ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
base::win::ScopedCoMem<WAVEFORMATEX> audio_engine_mix_format;
int effects = AudioParameters::NO_EFFECTS;
if (SUCCEEDED(GetMixFormat(device_id, &audio_engine_mix_format, &effects))) {
sample_rate = static_cast<int>(audio_engine_mix_format->nSamplesPerSec);
channel_layout = audio_engine_mix_format->nChannels == 1 ?
CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
}
int frames_per_buffer = sample_rate / 100;
return AudioParameters(
AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, 0, sample_rate,
16, frames_per_buffer, effects);
}
HRESULT WASAPIAudioInputStream::GetMixFormat(const std::string& device_id,
WAVEFORMATEX** device_format,
int* effects) {
DCHECK(effects);
ScopedComPtr<IMMDeviceEnumerator> enumerator;
HRESULT hr = enumerator.CreateInstance(__uuidof(MMDeviceEnumerator), NULL,
CLSCTX_INPROC_SERVER);
if (FAILED(hr))
return hr;
ScopedComPtr<IMMDevice> endpoint_device;
if (device_id == AudioManagerBase::kDefaultDeviceId) {
hr = enumerator->GetDefaultAudioEndpoint(eCapture, eConsole,
endpoint_device.Receive());
} else if (device_id == AudioManagerBase::kLoopbackInputDeviceId) {
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole,
endpoint_device.Receive());
} else {
hr = enumerator->GetDevice(base::UTF8ToUTF16(device_id).c_str(),
endpoint_device.Receive());
}
if (FAILED(hr))
return hr;
*effects = IsDefaultCommunicationDevice(enumerator, endpoint_device) ?
AudioParameters::DUCKING : AudioParameters::NO_EFFECTS;
ScopedComPtr<IAudioClient> audio_client;
hr = endpoint_device->Activate(__uuidof(IAudioClient),
CLSCTX_INPROC_SERVER,
NULL,
audio_client.ReceiveVoid());
return SUCCEEDED(hr) ? audio_client->GetMixFormat(device_format) : hr;
}
void WASAPIAudioInputStream::Run() {
ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA);
capture_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio);
DWORD task_index = 0;
HANDLE mm_task = avrt::AvSetMmThreadCharacteristics(L"Pro Audio",
&task_index);
bool mmcss_is_ok =
(mm_task && avrt::AvSetMmThreadPriority(mm_task, AVRT_PRIORITY_CRITICAL));
if (!mmcss_is_ok) {
DWORD err = GetLastError();
LOG(WARNING) << "Failed to enable MMCSS (error code=" << err << ").";
}
size_t buffer_frame_index = 0;
size_t capture_buffer_size = std::max(
2 * endpoint_buffer_size_frames_ * frame_size_,
2 * packet_size_frames_ * frame_size_);
scoped_ptr<uint8[]> capture_buffer(new uint8[capture_buffer_size]);
LARGE_INTEGER now_count;
bool recording = true;
bool error = false;
double volume = GetVolume();
HANDLE wait_array[2] = {stop_capture_event_, audio_samples_ready_event_};
while (recording && !error) {
HRESULT hr = S_FALSE;
DWORD wait_result = WaitForMultipleObjects(2, wait_array, FALSE, INFINITE);
switch (wait_result) {
case WAIT_FAILED:
error = true;
break;
case WAIT_OBJECT_0 + 0:
recording = false;
break;
case WAIT_OBJECT_0 + 1:
{
BYTE* data_ptr = NULL;
UINT32 num_frames_to_read = 0;
DWORD flags = 0;
UINT64 device_position = 0;
UINT64 first_audio_frame_timestamp = 0;
hr = audio_capture_client_->GetBuffer(&data_ptr,
&num_frames_to_read,
&flags,
&device_position,
&first_audio_frame_timestamp);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to get data from the capture buffer";
continue;
}
if (num_frames_to_read != 0) {
size_t pos = buffer_frame_index * frame_size_;
size_t num_bytes = num_frames_to_read * frame_size_;
DCHECK_GE(capture_buffer_size, pos + num_bytes);
if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
memset(&capture_buffer[pos], 0, num_bytes);
} else {
memcpy(&capture_buffer[pos], data_ptr, num_bytes);
}
buffer_frame_index += num_frames_to_read;
}
hr = audio_capture_client_->ReleaseBuffer(num_frames_to_read);
DLOG_IF(ERROR, FAILED(hr)) << "Failed to release capture buffer";
QueryPerformanceCounter(&now_count);
double audio_delay_frames =
((perf_count_to_100ns_units_ * now_count.QuadPart -
first_audio_frame_timestamp) / 10000.0) * ms_to_frame_count_ +
buffer_frame_index - num_frames_to_read;
GetAgcVolume(&volume);
uint32 delay_frames = static_cast<uint32>(audio_delay_frames + 0.5);
while (buffer_frame_index >= packet_size_frames_) {
uint8* audio_data =
reinterpret_cast<uint8*>(capture_buffer.get());
sink_->OnData(this,
audio_data,
packet_size_bytes_,
delay_frames * frame_size_,
volume);
memmove(&capture_buffer[0],
&capture_buffer[packet_size_bytes_],
(buffer_frame_index - packet_size_frames_) * frame_size_);
buffer_frame_index -= packet_size_frames_;
delay_frames -= packet_size_frames_;
}
}
break;
default:
error = true;
break;
}
}
if (recording && error) {
NOTREACHED() << "WASAPI capturing failed with error code "
<< GetLastError();
}
if (mm_task && !avrt::AvRevertMmThreadCharacteristics(mm_task)) {
PLOG(WARNING) << "Failed to disable MMCSS";
}
}
void WASAPIAudioInputStream::HandleError(HRESULT err) {
NOTREACHED() << "Error code: " << err;
if (sink_)
sink_->OnError(this);
}
HRESULT WASAPIAudioInputStream::SetCaptureDevice() {
DCHECK(!endpoint_device_);
ScopedComPtr<IMMDeviceEnumerator> enumerator;
HRESULT hr = enumerator.CreateInstance(__uuidof(MMDeviceEnumerator),
NULL, CLSCTX_INPROC_SERVER);
if (FAILED(hr))
return hr;
if (effects_ & AudioParameters::DUCKING) {
hr = enumerator->GetDefaultAudioEndpoint(eCapture, eCommunications,
endpoint_device_.Receive());
if (endpoint_device_ && device_id_ != AudioManagerBase::kDefaultDeviceId) {
base::win::ScopedCoMem<WCHAR> communications_id;
endpoint_device_->GetId(&communications_id);
if (device_id_ !=
base::WideToUTF8(static_cast<WCHAR*>(communications_id))) {
DLOG(WARNING) << "Ducking has been requested for a non-default device."
"Not supported.";
endpoint_device_.Release();
}
}
}
if (!endpoint_device_) {
if (device_id_ == AudioManagerBase::kDefaultDeviceId) {
hr = enumerator->GetDefaultAudioEndpoint(eCapture, eConsole,
endpoint_device_.Receive());
} else if (device_id_ == AudioManagerBase::kLoopbackInputDeviceId) {
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole,
endpoint_device_.Receive());
} else {
hr = enumerator->GetDevice(base::UTF8ToUTF16(device_id_).c_str(),
endpoint_device_.Receive());
}
}
if (FAILED(hr))
return hr;
DWORD state = DEVICE_STATE_DISABLED;
hr = endpoint_device_->GetState(&state);
if (FAILED(hr))
return hr;
if (!(state & DEVICE_STATE_ACTIVE)) {
DLOG(ERROR) << "Selected capture device is not active.";
hr = E_ACCESSDENIED;
}
return hr;
}
HRESULT WASAPIAudioInputStream::ActivateCaptureDevice() {
HRESULT hr = endpoint_device_->Activate(__uuidof(IAudioClient),
CLSCTX_INPROC_SERVER,
NULL,
audio_client_.ReceiveVoid());
return hr;
}
HRESULT WASAPIAudioInputStream::GetAudioEngineStreamFormat() {
HRESULT hr = S_OK;
#ifndef NDEBUG
base::win::ScopedCoMem<WAVEFORMATEXTENSIBLE> format_ex;
hr = audio_client_->GetMixFormat(
reinterpret_cast<WAVEFORMATEX**>(&format_ex));
WAVEFORMATEX format = format_ex->Format;
DVLOG(2) << "WAVEFORMATEX:";
DVLOG(2) << " wFormatTags : 0x" << std::hex << format.wFormatTag;
DVLOG(2) << " nChannels : " << format.nChannels;
DVLOG(2) << " nSamplesPerSec : " << format.nSamplesPerSec;
DVLOG(2) << " nAvgBytesPerSec: " << format.nAvgBytesPerSec;
DVLOG(2) << " nBlockAlign : " << format.nBlockAlign;
DVLOG(2) << " wBitsPerSample : " << format.wBitsPerSample;
DVLOG(2) << " cbSize : " << format.cbSize;
DVLOG(2) << "WAVEFORMATEXTENSIBLE:";
DVLOG(2) << " wValidBitsPerSample: " <<
format_ex->Samples.wValidBitsPerSample;
DVLOG(2) << " dwChannelMask : 0x" << std::hex <<
format_ex->dwChannelMask;
if (format_ex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM)
DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_PCM";
else if (format_ex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT";
else if (format_ex->SubFormat == KSDATAFORMAT_SUBTYPE_WAVEFORMATEX)
DVLOG(2) << " SubFormat : KSDATAFORMAT_SUBTYPE_WAVEFORMATEX";
#endif
return hr;
}
bool WASAPIAudioInputStream::DesiredFormatIsSupported() {
base::win::ScopedCoMem<WAVEFORMATEX> closest_match;
HRESULT hr = audio_client_->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED,
&format_,
&closest_match);
DLOG_IF(ERROR, hr == S_FALSE) << "Format is not supported "
<< "but a closest match exists.";
return (hr == S_OK);
}
HRESULT WASAPIAudioInputStream::InitializeAudioEngine() {
DWORD flags;
if (device_id_ == AudioManagerBase::kLoopbackInputDeviceId) {
flags = AUDCLNT_STREAMFLAGS_LOOPBACK | AUDCLNT_STREAMFLAGS_NOPERSIST;
} else {
flags =
AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST;
}
HRESULT hr = audio_client_->Initialize(AUDCLNT_SHAREMODE_SHARED,
flags,
0,
0,
&format_,
NULL);
if (FAILED(hr))
return hr;
hr = audio_client_->GetBufferSize(&endpoint_buffer_size_frames_);
if (FAILED(hr))
return hr;
DVLOG(1) << "endpoint buffer size: " << endpoint_buffer_size_frames_
<< " [frames]";
#ifndef NDEBUG
REFERENCE_TIME device_period_shared_mode = 0;
REFERENCE_TIME device_period_exclusive_mode = 0;
HRESULT hr_dbg = audio_client_->GetDevicePeriod(
&device_period_shared_mode, &device_period_exclusive_mode);
if (SUCCEEDED(hr_dbg)) {
DVLOG(1) << "device period: "
<< static_cast<double>(device_period_shared_mode / 10000.0)
<< " [ms]";
}
REFERENCE_TIME latency = 0;
hr_dbg = audio_client_->GetStreamLatency(&latency);
if (SUCCEEDED(hr_dbg)) {
DVLOG(1) << "stream latency: " << static_cast<double>(latency / 10000.0)
<< " [ms]";
}
#endif
if (device_id_ == AudioManagerBase::kLoopbackInputDeviceId) {
hr = endpoint_device_->Activate(
__uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL,
audio_render_client_for_loopback_.ReceiveVoid());
if (FAILED(hr))
return hr;
hr = audio_render_client_for_loopback_->Initialize(
AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
0, 0, &format_, NULL);
if (FAILED(hr))
return hr;
hr = audio_render_client_for_loopback_->SetEventHandle(
audio_samples_ready_event_.Get());
} else {
hr = audio_client_->SetEventHandle(audio_samples_ready_event_.Get());
}
if (FAILED(hr))
return hr;
hr = audio_client_->GetService(__uuidof(IAudioCaptureClient),
audio_capture_client_.ReceiveVoid());
if (FAILED(hr))
return hr;
hr = audio_client_->GetService(__uuidof(ISimpleAudioVolume),
simple_audio_volume_.ReceiveVoid());
return hr;
}
}