Skip to content
Closed
Show file tree
Hide file tree
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
72 changes: 72 additions & 0 deletions webrtc-jni/src/main/cpp/src/media/audio/VoiceActivityListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<jobject> & listener,
float threshold, int hangoverMs) :
listener(listener),
Expand Down Expand Up @@ -52,6 +121,9 @@ namespace jni
return;
}

// Attaching the thread runs Java as well.
ScopedDenormals denormals;

JNIEnv * env = AttachCurrentThread();

if (env == nullptr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Float> 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.
Expand Down
Loading