Compare commits

...
4 Commits
Author SHA1 Message Date
erikbrockoandClaude Opus 5.5 1430782e2a Send the microphone's voice as whispers to a target
setWhisperTarget routes voice into VOICE_WHISPER packets to channels and
clients until cleared; a voice session under way ends on its old route
first. Whisper packets carry their own packet id and get the redundant
copies voice does.

WhisperTargetMultiple sized its two counts as two bytes each, leaving
zero bytes after the voice data, and read the client ids by the channel
count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 22:18:07 +00:00
erikbrockoandClaude Opus 5.5 2d6f5cdf78 Send every ready live-capture frame per tick and cap the timer's catch-up burst
A mic running faster than the 20 ms timer filled the queue until frames were
dropped; live sources now drain each tick. The catch-up clamp was inverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 20:14:42 +00:00
erikbrockoandClaude Opus 5.5 a4ce0feb61 Report a minute's bandwidth as bytes per second
setconnectioninfo sent the bytes of the whole last minute where the
server expects the minute's average rate, so others saw our bandwidth
60 times too high. PacketStatistics gains the per-second averages, and
the packets-last-minute count looked at the last second only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 20:26:59 +00:00
erikbrockoandClaude Opus 5.5 90bf6e030c Resend voice packets for loss resilience
A microphone can ask for every voice packet to be sent again, a number
of times at a fixed spacing. The copies are the same encrypted packet,
so listeners keep whichever arrives first. A TS3 server forwards them,
and a jitter buffer that sees them arrive late grows to wait for them:
the sender buys its listeners a deeper buffer, trading latency for
riding out loss, with native clients as much as with others.

Copies go out from the 20 ms microphone timer, so the spacing is best a
whole number of ticks. Copies of a connection that is gone are dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 20:17:13 +00:00
7 changed files with 222 additions and 48 deletions
@@ -12,4 +12,27 @@ public interface Microphone {
CodecType getCodec(); CodecType getCodec();
byte[] provide(); byte[] provide();
/**
* Milliseconds between a voice packet and each copy of it sent afterwards, or 0 to send it
* once. A copy is the same packet, so receivers keep whichever arrives first; a jitter buffer
* that sees copies arrive late grows to wait for them, trading latency for riding out loss.
*/
default int getRedundancyDelay() {
return 0;
}
/**
* Whether {@link #provide} hands out frames captured on a clock of their own, such as a sound
* card's. That clock drifts against the send timer, so each tick then sends every frame that is
* ready instead of one, keeping the drift from piling up as latency or overflowing a queue.
*/
default boolean isLiveCapture() {
return false;
}
/** How many copies follow each voice packet, {@link #getRedundancyDelay} apart. */
default int getRedundancyCopies() {
return 1;
}
} }
@@ -239,13 +239,13 @@ public class PacketBody1VoiceWhisper extends PacketBody {
channelIds[n] = (int) (buffer.getLong()); channelIds[n] = (int) (buffer.getLong());
clientIds = new int[clientIdCount]; clientIds = new int[clientIdCount];
for (int n = 0; n < channelIdCount; n ++) for (int n = 0; n < clientIdCount; n ++)
clientIds[n] = (buffer.getShort() & 0xFFFF); clientIds[n] = (buffer.getShort() & 0xFFFF);
} }
@Override @Override
public int getSize() { public int getSize() {
return 2 + 2 + return 1 + 1 +
(channelIds == null ? 0 : channelIds.length * 8) + (channelIds == null ? 0 : channelIds.length * 8) +
(clientIds == null ? 0 : clientIds.length * 2); (clientIds == null ? 0 : clientIds.length * 2);
} }
@@ -61,7 +61,7 @@ public class PacketStatistics {
} }
public int getSentPacketsLastMinute() { public int getSentPacketsLastMinute() {
long now = System.nanoTime() - 1_000_000_000L; long now = System.nanoTime() - 60_000_000_000L;
synchronized (sentPacketList) { synchronized (sentPacketList) {
return (int) sentPacketList.stream() return (int) sentPacketList.stream()
@@ -124,6 +124,16 @@ public class PacketStatistics {
} }
} }
/** Average bytes per second sent over the last minute, as TeamSpeak reports a minute's bandwidth. */
public int getSentBandwidthLastMinute() {
return getSentBytesLastMinute() / 60;
}
/** Average bytes per second received over the last minute. */
public int getReceivedBandwidthLastMinute() {
return getReceivedBytesLastMinute() / 60;
}
public int getReceivedBytesLastMinute() { public int getReceivedBytesLastMinute() {
long now = System.nanoTime() - 60_000_000_000L; long now = System.nanoTime() - 60_000_000_000L;
@@ -478,6 +478,8 @@ public abstract class AbstractTeamspeakClientSocket
// > X is a ushort in H2N order of an own audio packet counter // > X is a ushort in H2N order of an own audio packet counter
// it seems it can be the same as the packet counter so we will let the packethandler do it. // it seems it can be the same as the packet counter so we will let the packethandler do it.
((PacketBody0Voice)packet.getBody()).setPacketId(header.getPacketId()); ((PacketBody0Voice)packet.getBody()).setPacketId(header.getPacketId());
} else if (header.getType() == PacketBodyType.VOICE_WHISPER) {
((PacketBody1VoiceWhisper)packet.getBody()).setPacketId(header.getPacketId());
} }
// Flush to a buffer // Flush to a buffer
@@ -16,8 +16,12 @@ import com.github.manevolent.ts3j.identity.Uid;
import com.github.manevolent.ts3j.protocol.*; import com.github.manevolent.ts3j.protocol.*;
import com.github.manevolent.ts3j.protocol.client.ClientConnectionState; import com.github.manevolent.ts3j.protocol.client.ClientConnectionState;
import com.github.manevolent.ts3j.protocol.header.PacketHeader; import com.github.manevolent.ts3j.protocol.header.PacketHeader;
import com.github.manevolent.ts3j.enums.CodecType;
import com.github.manevolent.ts3j.protocol.packet.PacketBody;
import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice; import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice;
import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command; import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command;
import com.github.manevolent.ts3j.protocol.packet.PacketBodyType;
import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler; import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler;
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnected; import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnected;
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting; import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting;
@@ -53,7 +57,13 @@ public class LocalTeamspeakClientSocket
private volatile InetSocketAddress remote; private volatile InetSocketAddress remote;
private Microphone microphone; private Microphone microphone;
private volatile PacketBody1VoiceWhisper.WhisperTarget whisperTarget;
private HighPrecisionTimer microphoneThread; private HighPrecisionTimer microphoneThread;
/** Voice packets still to be sent again, soonest first; see {@link Microphone#getRedundancyDelay}. */
private final Queue<VoiceCopy> voiceCopies =
new PriorityBlockingQueue<>(64, Comparator.comparingLong((VoiceCopy copy) -> copy.due));
private volatile long voiceCopyDelayNanos;
private volatile int voiceCopyCount;
private List<TS3Listener> listeners = new LinkedList<>(); private List<TS3Listener> listeners = new LinkedList<>();
@@ -144,11 +154,11 @@ public class LocalTeamspeakClientSocket
response.add(new CommandSingleParameter("connection_bandwidth_sent_last_second_" + name, response.add(new CommandSingleParameter("connection_bandwidth_sent_last_second_" + name,
Integer.toString(stats.getSentBytesLastSecond()))); Integer.toString(stats.getSentBytesLastSecond())));
response.add(new CommandSingleParameter("connection_bandwidth_sent_last_minute_" + name, response.add(new CommandSingleParameter("connection_bandwidth_sent_last_minute_" + name,
Integer.toString(stats.getSentBytesLastMinute()))); Integer.toString(stats.getSentBandwidthLastMinute())));
response.add(new CommandSingleParameter("connection_bandwidth_received_last_second_" + name, response.add(new CommandSingleParameter("connection_bandwidth_received_last_second_" + name,
Integer.toString(stats.getReceivedBytesLastSecond()))); Integer.toString(stats.getReceivedBytesLastSecond())));
response.add(new CommandSingleParameter("connection_bandwidth_received_last_minute_" + name, response.add(new CommandSingleParameter("connection_bandwidth_received_last_minute_" + name,
Integer.toString(stats.getReceivedBytesLastMinute()))); Integer.toString(stats.getReceivedBandwidthLastMinute())));
} }
response.add(new CommandSingleParameter("connection_server2client_packetloss_total", Integer.toString(0))); response.add(new CommandSingleParameter("connection_server2client_packetloss_total", Integer.toString(0)));
@@ -278,6 +288,40 @@ public class LocalTeamspeakClientSocket
packet.getDatagram().setSocketAddress(remote); packet.getDatagram().setSocketAddress(remote);
socket.send(packet.getDatagram()); socket.send(packet.getDatagram());
long copyDelay = voiceCopyDelayNanos;
PacketBodyType type = packet.getHeader().getType();
if (copyDelay > 0 && (type == PacketBodyType.VOICE || type == PacketBodyType.VOICE_WHISPER)) {
// Aim half a tick early, so each copy goes out on the tick nearest its time.
long sent = System.nanoTime() - TICK_MS * 1_000_000L / 2;
for (int i = 1, n = voiceCopyCount; i <= n; i++)
voiceCopies.add(new VoiceCopy(sent + i * copyDelay, packet.getDatagram()));
}
}
/** Sends the voice copies that are due; runs on the microphone timer. */
private void sendDueVoiceCopies() {
long now = System.nanoTime();
VoiceCopy copy;
while ((copy = voiceCopies.peek()) != null && now - copy.due >= 0) {
// Whatever poll() hands out is due no later than the one peeked at.
copy = voiceCopies.poll();
try {
socket.send(copy.datagram);
} catch (IOException e) {
getExceptionHandler().accept(e);
}
}
}
private static final class VoiceCopy {
final long due;
final DatagramPacket datagram;
VoiceCopy(long due, DatagramPacket datagram) {
this.due = due;
this.datagram = datagram;
}
} }
@@ -546,17 +590,31 @@ public class LocalTeamspeakClientSocket
return microphone; return microphone;
} }
/** Where the microphone's voice goes: null for our channel, else whisper packets to the target. */
public PacketBody1VoiceWhisper.WhisperTarget getWhisperTarget() {
return whisperTarget;
}
/**
* Sends the microphone's voice to {@code target} as whispers, or to our channel again with null.
* A voice session under way ends on its old route first.
*/
public void setWhisperTarget(PacketBody1VoiceWhisper.WhisperTarget target) {
this.whisperTarget = target;
}
public void setMicrophone(Microphone microphone) { public void setMicrophone(Microphone microphone) {
if (this.microphone != microphone) { if (this.microphone != microphone) {
this.microphone = microphone; this.microphone = microphone;
if (microphone != null && microphoneThread == null) { if (microphone != null && microphoneThread == null) {
microphoneThread = new HighPrecisionTimer( microphoneThread = new HighPrecisionTimer(
20, TICK_MS,
5F, // up to 100ms of drift/catchup 5F, // up to 100ms of drift/catchup
new MicrophoneTask() new MicrophoneTask()
); );
microphoneThread.setDaemon(true); microphoneThread.setDaemon(true);
microphoneThread.setPriority(Thread.MAX_PRIORITY);
if (getState() != ClientConnectionState.DISCONNECTED) if (getState() != ClientConnectionState.DISCONNECTED)
microphoneThread.start(); microphoneThread.start();
@@ -620,14 +678,24 @@ public class LocalTeamspeakClientSocket
} }
} }
private static final int TICK_MS = 20;
private class MicrophoneTask implements Runnable { private class MicrophoneTask implements Runnable {
private boolean wasSendingAudio = false; private boolean wasSendingAudio = false;
/** Where the voice session under way goes; null for the channel. */
private PacketBody1VoiceWhisper.WhisperTarget sendingTarget;
private byte sessionId = 1; private byte sessionId = 1;
private int flaggedPackets = 5; private int flaggedPackets = 5;
@Override @Override
public void run() { public void run() {
if (!isConnected()) return; if (!isConnected()) {
// Copies from a connection that is gone must not reach the next one.
voiceCopies.clear();
return;
}
sendDueVoiceCopies();
Microphone microphone; Microphone microphone;
@@ -636,38 +704,51 @@ public class LocalTeamspeakClientSocket
if ((microphone = LocalTeamspeakClientSocket.this.microphone) == null) if ((microphone = LocalTeamspeakClientSocket.this.microphone) == null)
return; return;
// Copies are sent from this tick, so the delay is best a whole number of ticks.
int copyDelay = microphone.getRedundancyDelay();
int copies = microphone.getRedundancyCopies();
voiceCopyCount = copies;
voiceCopyDelayNanos = copyDelay > 0 && copies > 0 ? copyDelay * 1_000_000L : 0;
PacketBody1VoiceWhisper.WhisperTarget target = whisperTarget;
if (wasSendingAudio && target != sendingTarget) {
// The listeners of the old route hear the end of what they were hearing.
if (!sendEnd(microphone)) return;
}
if (microphone.isMuted()) { if (microphone.isMuted()) {
// TODO // TODO
// Set a state and send a muted event // Set a state and send a muted event
} else if (microphone.isReady()) { } else if (microphone.isReady()) {
byte[] data = microphone.provide(); boolean live = microphone.isLiveCapture();
if (data.length <= 0) byte[] data;
return; do {
data = microphone.provide();
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut()); if (data.length <= 0) break;
voice.setCodecType(microphone.getCodec());
voice.setCodecData(data);
if (flaggedPackets > 0) voice.setServerFlag0(sessionId);
try { try {
writePacket(voice); writePacket(voicePacket(target, microphone.getCodec(), data,
flaggedPackets > 0 && target == null ? sessionId : null));
flaggedPackets--; flaggedPackets--;
wasSendingAudio = true; wasSendingAudio = true;
sendingTarget = target;
} catch (Exception e) { } catch (Exception e) {
// All we are really concerned about here is a disconnection event, in which case the loop // All we are really concerned about here is a disconnection event, in which case the loop
// will exit. // will exit.
getExceptionHandler().accept(e); getExceptionHandler().accept(e);
return;
} }
} while (live && microphone.isReady());
} else if (wasSendingAudio) { } else if (wasSendingAudio) {
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut()); sendEnd(microphone);
}
voice.setCodecType(microphone.getCodec()); }
voice.setCodecData(new byte[0]);
/** Ends the voice session on the route it went, which an empty packet does; false once disconnected. */
private boolean sendEnd(Microphone microphone) {
try { try {
writePacket(voice); writePacket(voicePacket(sendingTarget, microphone.getCodec(), new byte[0], null));
// reset decoder flag // reset decoder flag
flaggedPackets = 5; flaggedPackets = 5;
@@ -677,12 +758,29 @@ public class LocalTeamspeakClientSocket
sessionId = 1; sessionId = 1;
wasSendingAudio = false; wasSendingAudio = false;
return true;
} catch (Exception e) { } catch (Exception e) {
// All we are really concerned about here is a disconnection event, in which case the loop // All we are really concerned about here is a disconnection event, in which case the loop
// will exit. // will exit.
getExceptionHandler().accept(e); getExceptionHandler().accept(e);
return false;
} }
} }
private PacketBody voicePacket(PacketBody1VoiceWhisper.WhisperTarget target, CodecType codec, byte[] data,
Byte flag) {
if (target == null) {
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut());
voice.setCodecType(codec);
voice.setCodecData(data);
if (flag != null) voice.setServerFlag0(flag);
return voice;
}
PacketBody1VoiceWhisper whisper = new PacketBody1VoiceWhisper(getRole().getOut());
whisper.setCodecType(codec);
whisper.setTarget(target);
whisper.setCodecData(data);
return whisper;
} }
} }
@@ -64,8 +64,8 @@ public class HighPrecisionTimer extends Thread {
now = System.nanoTime(); now = System.nanoTime();
// Clamp the next target to the maximum "drift" allowed // After a stall, catch up at most the allowed number of ticks rather than all of them
if (nextTarget - now > maximumDriftInNanos) if (now - nextTarget > maximumDriftInNanos)
nextTarget = now - maximumDriftInNanos; nextTarget = now - maximumDriftInNanos;
if (sleepFor(nextTarget - now)) { if (sleepFor(nextTarget - now)) {
@@ -0,0 +1,41 @@
package com.github.manevolent.ts3j;
import com.github.manevolent.ts3j.enums.CodecType;
import com.github.manevolent.ts3j.protocol.ProtocolRole;
import com.github.manevolent.ts3j.protocol.header.ClientPacketHeader;
import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
import junit.framework.TestCase;
import java.nio.ByteBuffer;
import java.util.Arrays;
/** A whisper a client sends: codec, then the channel and client counts and ids, then the voice. */
public class WhisperPacketTest extends TestCase {
public void testClientWhisperRoundTrip() {
PacketBody1VoiceWhisper.WhisperTargetMultiple target = new PacketBody1VoiceWhisper.WhisperTargetMultiple();
target.setChannelIds(new int[]{7, 9});
target.setClientIds(new int[]{3});
PacketBody1VoiceWhisper sent = new PacketBody1VoiceWhisper(ProtocolRole.CLIENT);
sent.setPacketId(42);
sent.setCodecType(CodecType.OPUS_VOICE);
sent.setTarget(target);
sent.setCodecData(new byte[]{1, 2, 3});
ByteBuffer buffer = ByteBuffer.allocate(sent.getSize());
sent.write(buffer);
assertEquals("the size covers exactly what is written", buffer.capacity(), buffer.position());
assertEquals(2 + 1 + 1 + 1 + 2 * 8 + 2 + 3, buffer.capacity());
buffer.flip();
PacketBody1VoiceWhisper read = new PacketBody1VoiceWhisper(ProtocolRole.CLIENT);
read.read(new ClientPacketHeader(), buffer);
PacketBody1VoiceWhisper.WhisperTargetMultiple readTarget =
(PacketBody1VoiceWhisper.WhisperTargetMultiple) read.getTarget();
assertEquals(42, read.getPacketId());
assertEquals(CodecType.OPUS_VOICE, read.getCodecType());
assertTrue(Arrays.equals(new int[]{7, 9}, readTarget.getChannelIds()));
assertTrue(Arrays.equals(new int[]{3}, readTarget.getClientIds()));
assertTrue(Arrays.equals(new byte[]{1, 2, 3}, read.getCodecData()));
}
}