Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions platform/src/audio/AudioCaptureWasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <audioclient.h>
#include <endpointvolume.h>
#include <ks.h>
#include <ksmedia.h>
#include <mmdeviceapi.h>
#include <mmreg.h>
#include <objbase.h>
Expand All @@ -33,11 +35,34 @@ struct ComScope {
}
};

bool IsFloatFormat(const WAVEFORMATEX& wf) {
if (wf.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) return true;
if (wf.wFormatTag != WAVE_FORMAT_EXTENSIBLE) return false;
const auto& ext = reinterpret_cast<const WAVEFORMATEXTENSIBLE&>(wf);
return ext.SubFormat.Data1 == WAVE_FORMAT_IEEE_FLOAT;
enum class SampleType { Unsupported,
Float32,
Pcm16 };

struct CaptureFormat {
size_t channels;
SampleType sampleType;
};

SampleType GetSampleType(const WAVEFORMATEX& wf) {
if (wf.nChannels == 0 || wf.nBlockAlign * 8u != wf.nChannels * unsigned(wf.wBitsPerSample))
return SampleType::Unsupported;

WORD tag = wf.wFormatTag;
if (tag == WAVE_FORMAT_EXTENSIBLE) {
if (wf.cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
return SampleType::Unsupported;
const auto& ext = reinterpret_cast<const WAVEFORMATEXTENSIBLE&>(wf);
constexpr GUID floatSubtype{STATIC_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT};
constexpr GUID pcmSubtype{STATIC_KSDATAFORMAT_SUBTYPE_PCM};
if (IsEqualGUID(ext.SubFormat, floatSubtype))
tag = WAVE_FORMAT_IEEE_FLOAT;
else if (IsEqualGUID(ext.SubFormat, pcmSubtype))
tag = WAVE_FORMAT_PCM;
}
if (tag == WAVE_FORMAT_IEEE_FLOAT && wf.wBitsPerSample == 32) return SampleType::Float32;
if (tag == WAVE_FORMAT_PCM && wf.wBitsPerSample == 16) return SampleType::Pcm16;
return SampleType::Unsupported;
}

int16_t ClampToPcm(float sample) {
Expand Down Expand Up @@ -81,10 +106,10 @@ struct AudioCapture::Impl {
return nowUs - lastPacketUs >= frameUs && nowUs >= nextPadUs;
}

void FeedInterleaved(const void* data, size_t deviceFrames, const WAVEFORMATEX& wf,
void FeedInterleaved(const void* data, size_t deviceFrames, const CaptureFormat& wf,
bool silent) {
const size_t deviceChannels = wf.nChannels;
const bool isFloat = IsFloatFormat(wf);
const size_t deviceChannels = wf.channels;
const bool isFloat = wf.sampleType == SampleType::Float32;
const auto* asFloat = static_cast<const float*>(data);
const auto* asPcm = static_cast<const int16_t*>(data);

Expand All @@ -105,11 +130,11 @@ struct AudioCapture::Impl {
Emit();
}

bool DrainPackets(IAudioCaptureClient& capture, const WAVEFORMATEX& wf, bool muted);
bool DrainPackets(IAudioCaptureClient& capture, const CaptureFormat& wf, bool muted);
void Run();
};

bool AudioCapture::Impl::DrainPackets(IAudioCaptureClient& capture, const WAVEFORMATEX& wf,
bool AudioCapture::Impl::DrainPackets(IAudioCaptureClient& capture, const CaptureFormat& wf,
bool muted) {
bool drained = false;
UINT32 available = 0;
Expand Down Expand Up @@ -152,10 +177,18 @@ void AudioCapture::Impl::Run() {
return;
}

const WAVEFORMATEX wf = *mix;
if (wf.nSamplesPerSec != format.sampleRate) {
if (mix->nSamplesPerSec != format.sampleRate) {
LOGW("[audio] evt=capture_rate_mismatch device=%lu want=%u note=sharing without sound",
static_cast<unsigned long>(wf.nSamplesPerSec), format.sampleRate);
static_cast<unsigned long>(mix->nSamplesPerSec), format.sampleRate);
CoTaskMemFree(mix);
SetEvent(ready);
return;
}

const CaptureFormat wf{mix->nChannels, GetSampleType(*mix)};
if (wf.sampleType == SampleType::Unsupported) {
LOGW("[audio] evt=capture_format_unsupported backend=wasapi tag=%u bits=%u channels=%u",
unsigned(mix->wFormatTag), unsigned(mix->wBitsPerSample), unsigned(mix->nChannels));
CoTaskMemFree(mix);
SetEvent(ready);
return;
Expand Down
Loading