Add Connection Info on Android and count received voice

Connection Info gains a "Voice received" section on both frontends,
counting what our own playback made of a speaker's voice (everyone's,
for ourselves): packets received, frames played and missed, packets
out of order, dropped as too late, and discarded as copies. Android
gets the window itself as a page from the client sheet.

The rows and their formatting live in core, shared by both frontends;
the Swing dialog keeps its tabs on one row. A minute's bandwidth now
shows as the average rate it is labelled as, not the minute's bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 20:26:59 +00:00
co-authored by Claude Opus 5.5
parent c8c54bd381
commit 9458ff3400
14 changed files with 437 additions and 198 deletions
@@ -42,6 +42,8 @@ public final class StreamingVoiceOutput implements VoiceOutput {
private final Set<Integer> mutedClients = ConcurrentHashMap.newKeySet();
/** Per-client gain on top of the master volume; absent means 1.0. */
private final Map<Integer, Double> clientVolumes = new ConcurrentHashMap<>();
/** What came in from speakers who have left. */
private volatile VoiceReceiveStats departed = VoiceReceiveStats.NONE;
private volatile String outputDevice;
private volatile double masterVolume = 1.0;
@@ -176,6 +178,9 @@ public final class StreamingVoiceOutput implements VoiceOutput {
public void removeClient(int clientId) {
VoiceStream s = streams.remove(clientId);
if (s != null) {
synchronized (this) {
departed = departed.plus(s.stats());
}
playout.removed(s);
s.close();
}
@@ -183,6 +188,19 @@ public final class StreamingVoiceOutput implements VoiceOutput {
clientVolumes.remove(clientId);
}
@Override
public VoiceReceiveStats receiveStats(int clientId) {
VoiceStream s = streams.get(clientId);
return s == null ? null : s.stats();
}
@Override
public VoiceReceiveStats receiveStats() {
VoiceReceiveStats total = departed;
for (VoiceStream s : streams.values()) total = total.plus(s.stats());
return total;
}
@Override
public void shutdown() {
clock.shutdownNow();
@@ -43,4 +43,10 @@ public interface VoiceOutput {
/** Receives every speaker's voice as it is played, e.g. for recording; null detaches it. */
void setVoiceTap(VoiceTap tap);
/** How one speaker's voice has been coming in, or {@code null} if none came yet. */
VoiceReceiveStats receiveStats(int clientId);
/** How everyone's voice has been coming in, those who left included. */
VoiceReceiveStats receiveStats();
}
@@ -0,0 +1,29 @@
package com.ts3client.audio;
/**
* How one speaker's voice, or everyone's together, has been coming in: what arrived, and
* what playback had to do without.
*
* @param received voice packets that arrived, copies included
* @param played frames played from a packet
* @param missed frames concealed because their packet was not there when due
* @param outOfOrder packets that arrived after a later one
* @param late packets that arrived after their frame was concealed, so were dropped;
* each one is also among {@code missed}
* @param duplicates copies of a packet already received, discarded
*/
public record VoiceReceiveStats(long received, long played, long missed, long outOfOrder, long late,
long duplicates) {
public static final VoiceReceiveStats NONE = new VoiceReceiveStats(0, 0, 0, 0, 0, 0);
public VoiceReceiveStats plus(VoiceReceiveStats o) {
return new VoiceReceiveStats(received + o.received, played + o.played, missed + o.missed,
outOfOrder + o.outOfOrder, late + o.late, duplicates + o.duplicates);
}
/** Frames that were due, whether played or concealed. */
public long due() {
return played + missed;
}
}
@@ -42,6 +42,8 @@ final class VoiceStream {
/** Silence after which the timeline and the delay estimate are dropped, as in the official client. */
private static final long IDLE_RESET_NANOS = 5_000_000_000L;
private static final int EPOCH_UNSET = Integer.MIN_VALUE;
/** How many recent sequence numbers are remembered to recognise a copy; a power of two. */
private static final int SEEN_WINDOW = 1024;
/** What a pull plays, decided under the lock and produced outside it. */
private enum Source { PACKET, CONCEAL, SILENCE, SURPLUS }
@@ -67,6 +69,15 @@ final class VoiceStream {
private int concealOwed;
/** Set when the timeline was dropped; the decoder follows on the next pull. */
private boolean resetDecoder;
/** Recently received sequence numbers, each at {@code seq % SEEN_WINDOW}. */
private final int[] seen = new int[SEEN_WINDOW];
private int highestSeq;
private long received;
private long played;
private long missed;
private long outOfOrder;
private long late;
private long duplicates;
/** Guards the decoder: a close from another thread may reach it mid-pull. */
private final Object decoderLock = new Object();
@@ -89,6 +100,7 @@ final class VoiceStream {
this.clientId = clientId;
this.opus = opus;
this.talkListener = talkListener;
forgetSeen();
}
void setTap(VoiceTap tap) {
@@ -118,16 +130,20 @@ final class VoiceStream {
epoch = EPOCH_UNSET;
stopMarkCount = 0;
resetDecoder = true;
forgetSeen();
}
boolean fresh = epoch == EPOCH_UNSET;
int ticks = unwrap(packetId & 0xFFFF) * FRAME_TICKS;
int seq = unwrap(packetId & 0xFFFF);
int ticks = seq * FRAME_TICKS;
if (end) {
markStop(ticks);
return false;
}
boolean behind = !fresh && ticks - jitter.position() < 0;
count(seq, fresh, behind);
// Playback already went past it, but the buffer still learns how late it ran, as in
// the official client; that is what grows the delay to wait for redundant copies.
if (!fresh && ticks - jitter.position() < 0) {
if (behind) {
jitter.put(data, ticks, FRAME_TICKS);
return false;
}
@@ -187,8 +203,16 @@ final class VoiceStream {
case OK -> {
source = Source.PACKET;
packet = jitter.payload();
played++;
}
case MISSING -> {
if (isStopMark(jitter.timestamp())) {
source = Source.SILENCE;
} else {
source = Source.CONCEAL;
missed++;
}
}
case MISSING -> source = isStopMark(jitter.timestamp()) ? Source.SILENCE : Source.CONCEAL;
default -> {
// The buffer grows its delay by inserting this many frames.
source = Source.CONCEAL;
@@ -327,6 +351,31 @@ final class VoiceStream {
return (epoch << 16) | seq;
}
/** Files an arrived packet under the counters. Call under the lock. */
private void count(int seq, boolean fresh, boolean behind) {
received++;
int slot = seq & (SEEN_WINDOW - 1);
if (!fresh && seen[slot] == seq) {
duplicates++;
return;
}
seen[slot] = seq;
if (behind) late++;
if (fresh || seq - highestSeq > 0) highestSeq = seq;
else outOfOrder++;
}
private void forgetSeen() {
java.util.Arrays.fill(seen, Integer.MIN_VALUE);
}
/** How this speaker's voice has been coming in since the stream was created. */
VoiceReceiveStats stats() {
synchronized (lock) {
return new VoiceReceiveStats(received, played, missed, outOfOrder, late, duplicates);
}
}
private void markStop(int ticks) {
int position = jitter.position();
int kept = 0;
@@ -1,5 +1,7 @@
package com.ts3client.net;
import com.ts3client.audio.VoiceReceiveStats;
import java.util.ArrayList;
import java.util.List;
@@ -50,6 +52,12 @@ public final class ConnectionStats {
public long bandwidthSentLastMinute = -1;
public long bandwidthReceivedLastMinute = -1;
/**
* How voice has been coming in here, measured by this client's own playback: from this
* speaker, or for ourselves from everyone. {@code null} when nothing was heard.
*/
public VoiceReceiveStats voiceReceived;
/** Per-category breakdown (one row per {@link Kind}); empty when unavailable. */
public final List<KindStats> perKind = new ArrayList<>();
@@ -8,6 +8,7 @@ import com.github.manevolent.ts3j.protocol.ProtocolRole;
import com.github.manevolent.ts3j.protocol.packet.statistics.PacketStatistics;
import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket;
import com.github.manevolent.ts3j.util.Pair;
import com.ts3client.audio.VoiceOutput;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -42,12 +43,22 @@ final class ConnectionStatsCollector {
*/
void request(int clientId, Consumer<ConnectionStats> callback) {
if (clientId == conn.getSelfClientId()) {
new Thread(() -> callback.accept(buildLocalStats()), "ts3j-conninfo-self").start();
new Thread(() -> callback.accept(withVoice(buildLocalStats(), clientId)), "ts3j-conninfo-self").start();
} else {
new Thread(() -> requestRemote(clientId, callback), "ts3j-conninfo").start();
new Thread(() -> requestRemote(clientId, s -> callback.accept(withVoice(s, clientId))),
"ts3j-conninfo").start();
}
}
/** Adds what our playback saw of the client's voice, or everyone's for ourselves. */
private ConnectionStats withVoice(ConnectionStats s, int clientId) {
VoiceOutput playback = conn.getPlayback();
if (playback != null) {
s.voiceReceived = s.self ? playback.receiveStats() : playback.receiveStats(clientId);
}
return s;
}
/** Matches an asynchronously arriving {@code notifyconnectioninfo} report to its request. */
void onReport(UnknownTeamspeakEvent e) {
int clid = safeInt(e.get("clid"));
@@ -98,8 +109,8 @@ final class ConnectionStatsCollector {
ks.bytesReceived = st.getReceivedBytes();
ks.bandwidthSentLastSecond = st.getSentBytesLastSecond();
ks.bandwidthReceivedLastSecond = st.getReceivedBytesLastSecond();
ks.bandwidthSentLastMinute = st.getSentBytesLastMinute();
ks.bandwidthReceivedLastMinute = st.getReceivedBytesLastMinute();
ks.bandwidthSentLastMinute = st.getSentBandwidthLastMinute();
ks.bandwidthReceivedLastMinute = st.getReceivedBandwidthLastMinute();
s.perKind.add(ks);
pSent += ks.packetsSent;
@@ -0,0 +1,143 @@
package com.ts3client.net;
import com.ts3client.audio.VoiceReceiveStats;
import java.text.DecimalFormat;
import java.util.List;
/** Renders {@link ConnectionStats} figures for display; an unknown value (negative) shows as a dash. */
public final class ConnectionStatsFormat {
public static final String UNKNOWN = "—";
private ConnectionStatsFormat() {
}
public static String orDash(String s) {
return (s == null || s.isEmpty()) ? UNKNOWN : s;
}
public static String ping(double ms, double deviationMs) {
if (ms < 0) return UNKNOWN;
String base = Math.round(ms) + " ms";
if (deviationMs > 0) base += " (± " + Math.round(deviationMs) + " ms)";
return base;
}
/** A fraction 0..1 as a percentage. */
public static String loss(double fraction) {
if (fraction < 0) return UNKNOWN;
return new DecimalFormat("0.00").format(fraction * 100.0) + " %";
}
public static String count(long n) {
return n < 0 ? UNKNOWN : new DecimalFormat("#,##0").format(n);
}
/** A count with its share of {@code of}, e.g. {@code 12 (0.4 %)}. */
public static String countOf(long n, long of) {
if (n < 0) return UNKNOWN;
if (of <= 0) return count(n);
return count(n) + " (" + new DecimalFormat("0.0").format(100.0 * n / of) + " %)";
}
public static String bytes(long bytes) {
if (bytes < 0) return UNKNOWN;
if (bytes < 1024) return bytes + " B";
double kib = bytes / 1024.0;
if (kib < 1024) return new DecimalFormat("0.0").format(kib) + " KiB";
double mib = kib / 1024.0;
if (mib < 1024) return new DecimalFormat("0.00").format(mib) + " MiB";
return new DecimalFormat("0.00").format(mib / 1024.0) + " GiB";
}
public static String rate(long bytesPerSecond) {
return bytesPerSecond < 0 ? UNKNOWN : bytes(bytesPerSecond) + "/s";
}
/** Sent and received side by side. */
public static String upDown(String up, String down) {
return up + " / " + down;
}
/** A labelled figure. */
public record Row(String label, String value) {
}
/** Who the client is and how the link to them is doing; {@code s} may be null while loading. */
public static List<Row> summaryRows(ConnectionStats s, String selfAddress) {
ConnectionStats k = s == null ? new ConnectionStats() : s;
return List.of(
new Row("Address", k.self ? selfAddress : orDash(k.ip)),
new Row("Client version", orDash(k.version)),
new Row("Platform", orDash(k.platform)),
new Row("Ping", ping(k.pingMs, k.pingDeviationMs)),
new Row("Idle time", duration(k.idleTimeMs)),
new Row("Connected", duration(k.connectedTimeMs)),
new Row("Filetransfer (↑/↓)",
upDown(rate(k.filetransferBandwidthSent), rate(k.filetransferBandwidthReceived))));
}
/** One traffic category's figures, or the totals for a null {@code kind}. */
public static List<Row> trafficRows(ConnectionStats s, ConnectionStats.Kind kind) {
double loss = -1;
long packetsSent = -1, packetsReceived = -1, bytesSent = -1, bytesReceived = -1;
long secondSent = -1, secondReceived = -1, minuteSent = -1, minuteReceived = -1;
if (s != null && kind == null) {
loss = s.packetLoss;
packetsSent = s.packetsSentTotal;
packetsReceived = s.packetsReceivedTotal;
bytesSent = s.bytesSentTotal;
bytesReceived = s.bytesReceivedTotal;
secondSent = s.bandwidthSentLastSecond;
secondReceived = s.bandwidthReceivedLastSecond;
minuteSent = s.bandwidthSentLastMinute;
minuteReceived = s.bandwidthReceivedLastMinute;
} else if (s != null && s.kind(kind) != null) {
ConnectionStats.KindStats k = s.kind(kind);
loss = k.packetLoss;
packetsSent = k.packetsSent;
packetsReceived = k.packetsReceived;
bytesSent = k.bytesSent;
bytesReceived = k.bytesReceived;
secondSent = k.bandwidthSentLastSecond;
secondReceived = k.bandwidthReceivedLastSecond;
minuteSent = k.bandwidthSentLastMinute;
minuteReceived = k.bandwidthReceivedLastMinute;
}
return List.of(
new Row("Packet loss", loss(loss)),
new Row("Packets (↑/↓)", upDown(count(packetsSent), count(packetsReceived))),
new Row("Transferred (↑/↓)", upDown(bytes(bytesSent), bytes(bytesReceived))),
new Row("Bandwidth · last second (↑/↓)", upDown(rate(secondSent), rate(secondReceived))),
new Row("Bandwidth · last minute (↑/↓)", upDown(rate(minuteSent), rate(minuteReceived))));
}
/** The voice our playback received; every value is a dash when nothing was heard. */
public static List<Row> voiceRows(VoiceReceiveStats v) {
VoiceReceiveStats s = v == null ? VoiceReceiveStats.NONE : v;
boolean known = v != null;
return List.of(
new Row("Received", known ? count(s.received()) : UNKNOWN),
new Row("Played", known ? countOf(s.played(), s.due()) : UNKNOWN),
new Row("Missed", known ? countOf(s.missed(), s.due()) : UNKNOWN),
new Row("Out of order", known ? countOf(s.outOfOrder(), s.received()) : UNKNOWN),
new Row("Dropped (too late)", known ? countOf(s.late(), s.received()) : UNKNOWN),
new Row("Discarded (copies)", known ? countOf(s.duplicates(), s.received()) : UNKNOWN));
}
public static String duration(long ms) {
if (ms < 0) return UNKNOWN;
long totalSeconds = ms / 1000;
long days = totalSeconds / 86400;
long hours = (totalSeconds % 86400) / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
StringBuilder sb = new StringBuilder();
if (days > 0) sb.append(days).append("d ");
if (days > 0 || hours > 0) sb.append(hours).append("h ");
if (days > 0 || hours > 0 || minutes > 0) sb.append(minutes).append("m ");
sb.append(seconds).append("s");
return sb.toString();
}
}
@@ -88,6 +88,21 @@ class VoiceStreamTest {
assertEquals(List.of(true, false), talk);
}
@Test
void countsWhatCameInAndWhatWasMissing() {
offer(0, 1);
offer(2, 3);
offer(1, 2); // out of order, still in time
offer(2, 3); // a copy
pull();
pull();
pull();
pull(); // 3 never arrives: concealed
offer(3, 4); // too late, dropped
offer(3, 4); // a copy of that
assertEquals(new VoiceReceiveStats(6, 3, 1, 1, 1, 2), stream.stats());
}
@Test
void theTalkersStopEndsTheBurst() {
offer(0, 1);