diff --git a/platform/src/audio/AudioCaptureWasapi.cpp b/platform/src/audio/AudioCaptureWasapi.cpp index 837c41ba..f384bd12 100644 --- a/platform/src/audio/AudioCaptureWasapi.cpp +++ b/platform/src/audio/AudioCaptureWasapi.cpp @@ -9,6 +9,8 @@ #include #include +#include +#include #include #include #include @@ -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(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(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) { @@ -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(data); const auto* asPcm = static_cast(data); @@ -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; @@ -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(wf.nSamplesPerSec), format.sampleRate); + static_cast(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;