Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1430782e2a | ||
|
|
2d6f5cdf78 | ||
|
|
a4ce0feb61 | ||
|
|
90bf6e030c |
@@ -12,4 +12,27 @@ public interface Microphone {
|
||||
CodecType getCodec();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -239,13 +239,13 @@ public class PacketBody1VoiceWhisper extends PacketBody {
|
||||
channelIds[n] = (int) (buffer.getLong());
|
||||
|
||||
clientIds = new int[clientIdCount];
|
||||
for (int n = 0; n < channelIdCount; n ++)
|
||||
for (int n = 0; n < clientIdCount; n ++)
|
||||
clientIds[n] = (buffer.getShort() & 0xFFFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return 2 + 2 +
|
||||
return 1 + 1 +
|
||||
(channelIds == null ? 0 : channelIds.length * 8) +
|
||||
(clientIds == null ? 0 : clientIds.length * 2);
|
||||
}
|
||||
|
||||
+11
-1
@@ -61,7 +61,7 @@ public class PacketStatistics {
|
||||
}
|
||||
|
||||
public int getSentPacketsLastMinute() {
|
||||
long now = System.nanoTime() - 1_000_000_000L;
|
||||
long now = System.nanoTime() - 60_000_000_000L;
|
||||
|
||||
synchronized (sentPacketList) {
|
||||
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() {
|
||||
long now = System.nanoTime() - 60_000_000_000L;
|
||||
|
||||
|
||||
+2
@@ -478,6 +478,8 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
// > 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.
|
||||
((PacketBody0Voice)packet.getBody()).setPacketId(header.getPacketId());
|
||||
} else if (header.getType() == PacketBodyType.VOICE_WHISPER) {
|
||||
((PacketBody1VoiceWhisper)packet.getBody()).setPacketId(header.getPacketId());
|
||||
}
|
||||
|
||||
// Flush to a buffer
|
||||
|
||||
+141
-43
@@ -16,8 +16,12 @@ import com.github.manevolent.ts3j.identity.Uid;
|
||||
import com.github.manevolent.ts3j.protocol.*;
|
||||
import com.github.manevolent.ts3j.protocol.client.ClientConnectionState;
|
||||
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.PacketBody1VoiceWhisper;
|
||||
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.client.LocalClientHandlerConnected;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting;
|
||||
@@ -53,7 +57,13 @@ public class LocalTeamspeakClientSocket
|
||||
private volatile InetSocketAddress remote;
|
||||
|
||||
private Microphone microphone;
|
||||
private volatile PacketBody1VoiceWhisper.WhisperTarget whisperTarget;
|
||||
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<>();
|
||||
|
||||
@@ -144,11 +154,11 @@ public class LocalTeamspeakClientSocket
|
||||
response.add(new CommandSingleParameter("connection_bandwidth_sent_last_second_" + name,
|
||||
Integer.toString(stats.getSentBytesLastSecond())));
|
||||
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,
|
||||
Integer.toString(stats.getReceivedBytesLastSecond())));
|
||||
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)));
|
||||
@@ -278,6 +288,40 @@ public class LocalTeamspeakClientSocket
|
||||
packet.getDatagram().setSocketAddress(remote);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/** 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) {
|
||||
if (this.microphone != microphone) {
|
||||
this.microphone = microphone;
|
||||
|
||||
if (microphone != null && microphoneThread == null) {
|
||||
microphoneThread = new HighPrecisionTimer(
|
||||
20,
|
||||
TICK_MS,
|
||||
5F, // up to 100ms of drift/catchup
|
||||
new MicrophoneTask()
|
||||
);
|
||||
microphoneThread.setDaemon(true);
|
||||
microphoneThread.setPriority(Thread.MAX_PRIORITY);
|
||||
|
||||
if (getState() != ClientConnectionState.DISCONNECTED)
|
||||
microphoneThread.start();
|
||||
@@ -620,14 +678,24 @@ public class LocalTeamspeakClientSocket
|
||||
}
|
||||
}
|
||||
|
||||
private static final int TICK_MS = 20;
|
||||
|
||||
private class MicrophoneTask implements Runnable {
|
||||
private boolean wasSendingAudio = false;
|
||||
/** Where the voice session under way goes; null for the channel. */
|
||||
private PacketBody1VoiceWhisper.WhisperTarget sendingTarget;
|
||||
private byte sessionId = 1;
|
||||
private int flaggedPackets = 5;
|
||||
|
||||
@Override
|
||||
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;
|
||||
|
||||
@@ -636,54 +704,84 @@ public class LocalTeamspeakClientSocket
|
||||
if ((microphone = LocalTeamspeakClientSocket.this.microphone) == null)
|
||||
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()) {
|
||||
// TODO
|
||||
// Set a state and send a muted event
|
||||
} else if (microphone.isReady()) {
|
||||
byte[] data = microphone.provide();
|
||||
if (data.length <= 0)
|
||||
return;
|
||||
boolean live = microphone.isLiveCapture();
|
||||
byte[] data;
|
||||
do {
|
||||
data = microphone.provide();
|
||||
if (data.length <= 0) break;
|
||||
|
||||
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut());
|
||||
try {
|
||||
writePacket(voicePacket(target, microphone.getCodec(), data,
|
||||
flaggedPackets > 0 && target == null ? sessionId : null));
|
||||
|
||||
voice.setCodecType(microphone.getCodec());
|
||||
voice.setCodecData(data);
|
||||
if (flaggedPackets > 0) voice.setServerFlag0(sessionId);
|
||||
|
||||
try {
|
||||
writePacket(voice);
|
||||
|
||||
flaggedPackets--;
|
||||
wasSendingAudio = true;
|
||||
} catch (Exception e) {
|
||||
// All we are really concerned about here is a disconnection event, in which case the loop
|
||||
// will exit.
|
||||
getExceptionHandler().accept(e);
|
||||
}
|
||||
flaggedPackets--;
|
||||
wasSendingAudio = true;
|
||||
sendingTarget = target;
|
||||
} catch (Exception e) {
|
||||
// All we are really concerned about here is a disconnection event, in which case the loop
|
||||
// will exit.
|
||||
getExceptionHandler().accept(e);
|
||||
return;
|
||||
}
|
||||
} while (live && microphone.isReady());
|
||||
} else if (wasSendingAudio) {
|
||||
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut());
|
||||
|
||||
voice.setCodecType(microphone.getCodec());
|
||||
voice.setCodecData(new byte[0]);
|
||||
|
||||
try {
|
||||
writePacket(voice);
|
||||
|
||||
// reset decoder flag
|
||||
flaggedPackets = 5;
|
||||
sessionId++;
|
||||
|
||||
if (sessionId >= 8)
|
||||
sessionId = 1;
|
||||
|
||||
wasSendingAudio = false;
|
||||
} catch (Exception e) {
|
||||
// All we are really concerned about here is a disconnection event, in which case the loop
|
||||
// will exit.
|
||||
getExceptionHandler().accept(e);
|
||||
}
|
||||
sendEnd(microphone);
|
||||
}
|
||||
}
|
||||
|
||||
/** Ends the voice session on the route it went, which an empty packet does; false once disconnected. */
|
||||
private boolean sendEnd(Microphone microphone) {
|
||||
try {
|
||||
writePacket(voicePacket(sendingTarget, microphone.getCodec(), new byte[0], null));
|
||||
|
||||
// reset decoder flag
|
||||
flaggedPackets = 5;
|
||||
sessionId++;
|
||||
|
||||
if (sessionId >= 8)
|
||||
sessionId = 1;
|
||||
|
||||
wasSendingAudio = false;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
// All we are really concerned about here is a disconnection event, in which case the loop
|
||||
// will exit.
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private class ClientCommandResponse<T>
|
||||
|
||||
@@ -64,8 +64,8 @@ public class HighPrecisionTimer extends Thread {
|
||||
|
||||
now = System.nanoTime();
|
||||
|
||||
// Clamp the next target to the maximum "drift" allowed
|
||||
if (nextTarget - now > maximumDriftInNanos)
|
||||
// After a stall, catch up at most the allowed number of ticks rather than all of them
|
||||
if (now - nextTarget > maximumDriftInNanos)
|
||||
nextTarget = now - maximumDriftInNanos;
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user