From 24f8eca48afd4edfd4e46c2fb9dff7095be56f35 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Wed, 4 Nov 2026 19:00:11 +0100 Subject: [PATCH] fix: call voice activity listeners with the JVM's floating point state AudioProcessing flushes denormals to zero while it hands its audio to the capture tap, so Java ran with FTZ and DAZ set. -Xcheck:jni ended the process on Windows; now the listener call undoes it. --- .../src/media/audio/VoiceActivityListener.cpp | 72 +++++++++++++++++++ .../audio/VoiceActivityListenerTest.java | 31 ++++++++ 2 files changed, 103 insertions(+) diff --git a/webrtc-jni/src/main/cpp/src/media/audio/VoiceActivityListener.cpp b/webrtc-jni/src/main/cpp/src/media/audio/VoiceActivityListener.cpp index 11bf13fbf..1488e6064 100644 --- a/webrtc-jni/src/main/cpp/src/media/audio/VoiceActivityListener.cpp +++ b/webrtc-jni/src/main/cpp/src/media/audio/VoiceActivityListener.cpp @@ -20,8 +20,77 @@ #include "JavaUtils.h" #include "JNI_WebRTC.h" +#include "rtc_base/system/arch.h" + namespace jni { + namespace + { +#if defined(WEBRTC_ARCH_X86_FAMILY) + // Flush-to-zero and denormals-are-zero, in MXCSR. + constexpr int kDenormalBits = 0x8040; +#elif defined(WEBRTC_ARCH_ARM_FAMILY) + // Flush-to-zero, in FPCR or FPSCR. + constexpr int kDenormalBits = 1 << 24; +#else + constexpr int kDenormalBits = 0; +#endif + + int ReadControlWord() + { + int word = 0; +#if defined(WEBRTC_ARCH_X86_FAMILY) + asm volatile("stmxcsr %0" : "=m"(word)); +#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_32_BITS) + asm volatile("vmrs %[word], FPSCR" : [word] "=r"(word)); +#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_64_BITS) + asm volatile("mrs %x[word], FPCR" : [word] "=r"(word)); +#endif + return word; + } + + void WriteControlWord(int word) + { +#if defined(WEBRTC_ARCH_X86_FAMILY) + asm volatile("ldmxcsr %0" : : "m"(word)); +#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_32_BITS) + asm volatile("vmsr FPSCR, %[word]" : : [word] "r"(word)); +#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_64_BITS) + asm volatile("msr FPCR, %x[word]" : : [word] "r"(word)); +#endif + } + + // Undoes webrtc::DenormalDisabler for a call into Java. AudioProcessing + // holds one while it hands its audio to the capture tap, and the JVM + // needs its default floating point state: Java's arithmetic depends on + // it, and on Windows the JVM does not restore it by itself, where + // -Xcheck:jni ends the process on the changed state. + class ScopedDenormals + { + public: + ScopedDenormals() : + saved(ReadControlWord()) + { + if (saved & kDenormalBits) { + WriteControlWord(saved & ~kDenormalBits); + } + } + + ~ScopedDenormals() + { + if (saved & kDenormalBits) { + WriteControlWord(saved); + } + } + + ScopedDenormals(const ScopedDenormals &) = delete; + ScopedDenormals & operator=(const ScopedDenormals &) = delete; + + private: + const int saved; + }; + } + VoiceActivityListener::VoiceActivityListener(JNIEnv * env, const JavaGlobalRef & listener, float threshold, int hangoverMs) : listener(listener), @@ -52,6 +121,9 @@ namespace jni return; } + // Attaching the thread runs Java as well. + ScopedDenormals denormals; + JNIEnv * env = AttachCurrentThread(); if (env == nullptr) { diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/VoiceActivityListenerTest.java b/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/VoiceActivityListenerTest.java index 1d3c63aff..8e70fdc8b 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/VoiceActivityListenerTest.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/VoiceActivityListenerTest.java @@ -50,6 +50,9 @@ class VoiceActivityListenerTest extends TestBase { private final VoiceActivityListener listener = changes::add; + /** Not a constant, so that halving it happens at run time. */ + private volatile float smallestNormal = Float.MIN_NORMAL; + private CustomAudioSource source; private AudioTrack track; @@ -279,6 +282,34 @@ void factoryHearsItsProcessing() { } } + @Test + void listenerKeepsDenormals() { + // WebRTC flushes denormals to zero while it processes audio, and the + // listener is called from within that. Java must not run that way: + // the JVM needs its default floating point state, and on Windows + // -Xcheck:jni ends the process on anything else. + AudioProcessing processing = new AudioProcessing(); + HeadlessAudioDeviceModule module = new HeadlessAudioDeviceModule(); + PeerConnectionFactory processed = new PeerConnectionFactory(module, processing); + List halved = new CopyOnWriteArrayList<>(); + VoiceActivityListener halving = speaking -> halved.add(smallestNormal / 2); + + try { + processed.addVoiceActivityListener(ONE_SEGMENT, halving); + + process(processing, speech(48000, 1), 48000); + + assertEquals(List.of(Float.MIN_NORMAL / 2), halved); + + processed.removeVoiceActivityListener(halving); + } + finally { + processed.dispose(); + module.dispose(); + processing.dispose(); + } + } + @Test void factoryDisposeRemovesListeners() { // The application's audio processing outlives the factory.