Skip to content

Commit 24f8eca

Browse files
committed
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.
1 parent 05dc8a7 commit 24f8eca

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎webrtc-jni/src/main/cpp/src/media/audio/VoiceActivityListener.cpp‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,77 @@
2020
#include "JavaUtils.h"
2121
#include "JNI_WebRTC.h"
2222

23+
#include "rtc_base/system/arch.h"
24+
2325
namespace jni
2426
{
27+
namespace
28+
{
29+
#if defined(WEBRTC_ARCH_X86_FAMILY)
30+
// Flush-to-zero and denormals-are-zero, in MXCSR.
31+
constexpr int kDenormalBits = 0x8040;
32+
#elif defined(WEBRTC_ARCH_ARM_FAMILY)
33+
// Flush-to-zero, in FPCR or FPSCR.
34+
constexpr int kDenormalBits = 1 << 24;
35+
#else
36+
constexpr int kDenormalBits = 0;
37+
#endif
38+
39+
int ReadControlWord()
40+
{
41+
int word = 0;
42+
#if defined(WEBRTC_ARCH_X86_FAMILY)
43+
asm volatile("stmxcsr %0" : "=m"(word));
44+
#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_32_BITS)
45+
asm volatile("vmrs %[word], FPSCR" : [word] "=r"(word));
46+
#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_64_BITS)
47+
asm volatile("mrs %x[word], FPCR" : [word] "=r"(word));
48+
#endif
49+
return word;
50+
}
51+
52+
void WriteControlWord(int word)
53+
{
54+
#if defined(WEBRTC_ARCH_X86_FAMILY)
55+
asm volatile("ldmxcsr %0" : : "m"(word));
56+
#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_32_BITS)
57+
asm volatile("vmsr FPSCR, %[word]" : : [word] "r"(word));
58+
#elif defined(WEBRTC_ARCH_ARM_FAMILY) && defined(WEBRTC_ARCH_64_BITS)
59+
asm volatile("msr FPCR, %x[word]" : : [word] "r"(word));
60+
#endif
61+
}
62+
63+
// Undoes webrtc::DenormalDisabler for a call into Java. AudioProcessing
64+
// holds one while it hands its audio to the capture tap, and the JVM
65+
// needs its default floating point state: Java's arithmetic depends on
66+
// it, and on Windows the JVM does not restore it by itself, where
67+
// -Xcheck:jni ends the process on the changed state.
68+
class ScopedDenormals
69+
{
70+
public:
71+
ScopedDenormals() :
72+
saved(ReadControlWord())
73+
{
74+
if (saved & kDenormalBits) {
75+
WriteControlWord(saved & ~kDenormalBits);
76+
}
77+
}
78+
79+
~ScopedDenormals()
80+
{
81+
if (saved & kDenormalBits) {
82+
WriteControlWord(saved);
83+
}
84+
}
85+
86+
ScopedDenormals(const ScopedDenormals &) = delete;
87+
ScopedDenormals & operator=(const ScopedDenormals &) = delete;
88+
89+
private:
90+
const int saved;
91+
};
92+
}
93+
2594
VoiceActivityListener::VoiceActivityListener(JNIEnv * env, const JavaGlobalRef<jobject> & listener,
2695
float threshold, int hangoverMs) :
2796
listener(listener),
@@ -52,6 +121,9 @@ namespace jni
52121
return;
53122
}
54123

124+
// Attaching the thread runs Java as well.
125+
ScopedDenormals denormals;
126+
55127
JNIEnv * env = AttachCurrentThread();
56128

57129
if (env == nullptr) {

‎webrtc/src/test/java/dev/onvoid/webrtc/media/audio/VoiceActivityListenerTest.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class VoiceActivityListenerTest extends TestBase {
5050

5151
private final VoiceActivityListener listener = changes::add;
5252

53+
/** Not a constant, so that halving it happens at run time. */
54+
private volatile float smallestNormal = Float.MIN_NORMAL;
55+
5356
private CustomAudioSource source;
5457

5558
private AudioTrack track;
@@ -279,6 +282,34 @@ void factoryHearsItsProcessing() {
279282
}
280283
}
281284

285+
@Test
286+
void listenerKeepsDenormals() {
287+
// WebRTC flushes denormals to zero while it processes audio, and the
288+
// listener is called from within that. Java must not run that way:
289+
// the JVM needs its default floating point state, and on Windows
290+
// -Xcheck:jni ends the process on anything else.
291+
AudioProcessing processing = new AudioProcessing();
292+
HeadlessAudioDeviceModule module = new HeadlessAudioDeviceModule();
293+
PeerConnectionFactory processed = new PeerConnectionFactory(module, processing);
294+
List<Float> halved = new CopyOnWriteArrayList<>();
295+
VoiceActivityListener halving = speaking -> halved.add(smallestNormal / 2);
296+
297+
try {
298+
processed.addVoiceActivityListener(ONE_SEGMENT, halving);
299+
300+
process(processing, speech(48000, 1), 48000);
301+
302+
assertEquals(List.of(Float.MIN_NORMAL / 2), halved);
303+
304+
processed.removeVoiceActivityListener(halving);
305+
}
306+
finally {
307+
processed.dispose();
308+
module.dispose();
309+
processing.dispose();
310+
}
311+
}
312+
282313
@Test
283314
void factoryDisposeRemovesListeners() {
284315
// The application's audio processing outlives the factory.

0 commit comments

Comments
 (0)