Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/android-volume-byte-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/react-native': patch
---

android: Fix useTrackVolume reading audio samples in the wrong byte order
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule
import com.livekit.reactnative.audio.events.Events
import livekit.org.webrtc.AudioTrackSink
import java.nio.ByteBuffer
import java.nio.ByteOrder
import kotlin.math.round
import kotlin.math.sqrt

Expand Down Expand Up @@ -34,17 +35,20 @@ abstract class BaseVolumeProcessor : AudioTrackSink {
numberOfFrames: Int,
absoluteCaptureTimestampMs: Long
) {
audioData.mark()
audioData.position(0)
// WebRTC hands us a JNI direct buffer, which Java defaults to big-endian,
// while the PCM samples are native (little-endian). Read through a view
// in native order so the sink's buffer is left untouched.
val samples = audioData.duplicate().order(ByteOrder.nativeOrder())
samples.position(0)
var average = 0L
val bytesPerSample = bitsPerSample / 8

// RMS average calculation
for (i in 0 until numberOfFrames) {
val value = when (bytesPerSample) {
1 -> audioData.get().toLong()
2 -> audioData.getShort().toLong()
4 -> audioData.getInt().toLong()
1 -> samples.get().toLong()
2 -> samples.getShort().toLong()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Quiet audio leaves track volume stale

When corrected samples produce zero, onVolumeCalculated emits a reading that useTrackVolume ignores. The track keeps its previous nonzero volume during near-silent frames.

Learn more

The processor emits normalized volume values for each audio buffer. The useTrackVolume listener updates state only when event.volume is truthy, so it discards zero. Correct native-order decoding can turn a low-level frame that previously decoded as nonzero into a zero RMS reading. The listener then preserves an earlier, louder reading.

Example: A 480-frame buffer containing one PCM16 sample of value 1 and otherwise zeros produces a zero RMS after native-order decoding. Before this change, swapping that sample to 256 produced a nonzero reading. If the last displayed volume was 0.5, the new zero event leaves it at 0.5.

Recommended fix: In useTrackVolume, check the event ID and tag independently of the numeric volume; accept zero as a valid reading. Add a transition test from nonzero to zero.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

4 -> samples.getInt().toLong()
else -> throw IllegalArgumentException()
}

Expand All @@ -60,7 +64,6 @@ abstract class BaseVolumeProcessor : AudioTrackSink {
4 -> volume / Int.MAX_VALUE
else -> throw IllegalArgumentException()
}
audioData.reset()

onVolumeCalculated(volumeNormalized)
}
Expand Down
Loading