Move our own state out of TeamspeakConnection: OwnClient, Recording

OwnClient owns the microphone and speakers and our flags (mute, deafen,
away, commander, talk request, phonetic nickname, input hardware) and
publishes them; Recording owns the recorder. ServerSession reads the
flags from OwnClient instead of keeping its own copy, and they are now
cleared when a connect starts rather than once it is done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-11 19:04:01 +00:00
co-authored by Claude Opus 5.5
parent c2090d8122
commit 9107bdd02e
11 changed files with 529 additions and 413 deletions
@@ -105,8 +105,8 @@ class ServerController internal constructor(
val isConnected: Boolean get() = session.isConnected val isConnected: Boolean get() = session.isConnected
internal val isBusy: Boolean get() = session.isBusy || network.isRecovering internal val isBusy: Boolean get() = session.isBusy || network.isRecovering
internal val microphone get() = if (session.isConnected) conn.microphone else null internal val microphone get() = if (session.isConnected) conn.ownClient().microphone() else null
internal val playback get() = if (session.isConnected) conn.playback else null internal val playback get() = if (session.isConnected) conn.ownClient().playback() else null
/** Not on the server and not trying to get back on it: done with. */ /** Not on the server and not trying to get back on it: done with. */
internal val ended: Boolean get() = started && !isBusy internal val ended: Boolean get() = started && !isBusy
+2 -2
View File
@@ -14,8 +14,8 @@ Items are worked through in the order below. Status: open, done (commit), or clo
| 4 | `TeamspeakConnection` (2,157 lines) still does lifecycle, mic/speaker, recording, chat logs, log/sound text, contacts, avatars, icons, file transfers, myTeamSpeak and job plumbing. The `*Admin` helpers take the whole connection, which forwards ~90 methods to them | in progress, in sub-steps: | | 4 | `TeamspeakConnection` (2,157 lines) still does lifecycle, mic/speaker, recording, chat logs, log/sound text, contacts, avatars, icons, file transfers, myTeamSpeak and job plumbing. The `*Admin` helpers take the whole connection, which forwards ~90 methods to them | in progress, in sub-steps: |
| 4a | … chat log and what we tell the user (log lines, sounds, links and sound variables) | done (`1bc7eb5`): `ChatLog`, `Announcer` | | 4a | … chat log and what we tell the user (log lines, sounds, links and sound variables) | done (`1bc7eb5`): `ChatLog`, `Announcer` |
| 4b | … request plumbing into its own class; `*Admin` helpers on narrow dependencies instead of the whole connection | done (`da908f7`; renamed `Uplink` in 4c, as `session.ServerLink` is the `ts3server://` link): `ServerLink` owns the socket, event and request threads and file transfers, and runs requests; the helpers take it (plus the model, permissions or avatars where they need them) | | 4b | … request plumbing into its own class; `*Admin` helpers on narrow dependencies instead of the whole connection | done (`da908f7`; renamed `Uplink` in 4c, as `session.ServerLink` is the `ts3server://` link): `ServerLink` owns the socket, event and request threads and file transfers, and runs requests; the helpers take it (plus the model, permissions or avatars where they need them) |
| 4c | … frontends reach the admin helpers directly instead of ~40 one-line forwarders | done (uncommitted): the helpers are public with non-blocking methods, reached through `ServerSession`/`TeamspeakConnection` getters; 30 forwarders gone, and what they added (stop-at-first-refusal loops, log lines, description refresh, own avatar flag) moved into the helpers. Swing dialogs still take the whole connection for the model and our ids (item 5) | | 4c | … frontends reach the admin helpers directly instead of ~40 one-line forwarders | done (`959f7ed`): the helpers are public with non-blocking methods, reached through `ServerSession`/`TeamspeakConnection` getters; 30 forwarders gone, and what they added (stop-at-first-refusal loops, log lines, description refresh, own avatar flag) moved into the helpers. Swing dialogs still take the whole connection for the model and our ids (item 5) |
| 4d | … our own state (mic, speakers, away, talk request, recording) out of the connection | open | | 4d | … our own state (mic, speakers, away, talk request, recording) out of the connection | done (uncommitted): `OwnClient` owns the devices and our flags, and publishes them; `Recording` the recorder. `ServerSession` no longer keeps its own copy of the flags, and they are cleared when a connect starts rather than once it is done |
| 5 | Two front doors: frontends use both `ServerSession` and `session.connection()` with no rule for which | open | | 5 | Two front doors: frontends use both `ServerSession` and `session.connection()` with no rule for which | open |
| 6 | Menu bar, toolbar and hotkeys each implement the same commands: `MainMenuBar.Listener` (~50 methods, ~210 lines of forwarding in `MainFrame`), `MainToolbar.Listener`, `HotkeyActions` | open | | 6 | Menu bar, toolbar and hotkeys each implement the same commands: `MainMenuBar.Listener` (~50 methods, ~210 lines of forwarding in `MainFrame`), `MainToolbar.Listener`, `HotkeyActions` | open |
| 7 | The admin list dialogs (bans, complaints, offline messages, privilege keys, server log) repeat the load/status/table-model scaffolding | open | | 7 | The admin list dialogs (bans, complaints, offline messages, privilege keys, server log) repeat the load/status/table-model scaffolding | open |
@@ -2,6 +2,7 @@ package eu.lixko.ts3j.client.net;
import com.github.manevolent.ts3j.event.*; import com.github.manevolent.ts3j.event.*;
import com.github.manevolent.ts3j.api.TextMessageTargetMode; import com.github.manevolent.ts3j.api.TextMessageTargetMode;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.contacts.Contact; import eu.lixko.ts3j.client.contacts.Contact;
import eu.lixko.ts3j.client.sound.SoundEvent; import eu.lixko.ts3j.client.sound.SoundEvent;
import eu.lixko.ts3j.client.sound.SoundNotifier; import eu.lixko.ts3j.client.sound.SoundNotifier;
@@ -59,7 +60,8 @@ final class ConnectionEventHandler implements TS3Listener {
logClientLeft(e, name); logClientLeft(e, name);
} }
conn.getModel().removeClient(e.getClientId()); conn.getModel().removeClient(e.getClientId());
if (conn.getPlayback() != null) conn.getPlayback().removeClient(e.getClientId()); VoiceOutput playback = conn.own.playback();
if (playback != null) playback.removeClient(e.getClientId());
conn.ui.onModelChanged(); conn.ui.onModelChanged();
} }
@@ -182,7 +184,7 @@ final class ConnectionEventHandler implements TS3Listener {
private void announceClientEntered(ClientJoinEvent e) { private void announceClientEntered(ClientJoinEvent e) {
if (!conn.isConnected()) return; if (!conn.isConnected()) return;
int clientId = e.getClientId(); int clientId = e.getClientId();
boolean current = conn.inOwnChannel(e.getClientTargetId()); boolean current = conn.own.inOwnChannel(e.getClientTargetId());
Map<String, String> vars = conn.announce.clientVars(clientId, e.getClientNickname()); Map<String, String> vars = conn.announce.clientVars(clientId, e.getClientNickname());
switch (safeInt(e, "reasonid")) { switch (safeInt(e, "reasonid")) {
case REASON_MOVED: case REASON_MOVED:
@@ -207,7 +209,7 @@ final class ConnectionEventHandler implements TS3Listener {
private void announceClientLeft(ClientLeaveEvent e) { private void announceClientLeft(ClientLeaveEvent e) {
if (!conn.isConnected()) return; if (!conn.isConnected()) return;
int clientId = e.getClientId(); int clientId = e.getClientId();
boolean current = conn.inOwnChannel(e.getClientFromId()); boolean current = conn.own.inOwnChannel(e.getClientFromId());
Map<String, String> vars = conn.announce.clientVars(clientId, null); Map<String, String> vars = conn.announce.clientVars(clientId, null);
switch (safeInt(e, "reasonid")) { switch (safeInt(e, "reasonid")) {
case REASON_TIMEOUT: case REASON_TIMEOUT:
@@ -243,8 +245,8 @@ final class ConnectionEventHandler implements TS3Listener {
/** A client we can see moved between two channels we can see ("stays"). */ /** A client we can see moved between two channels we can see ("stays"). */
private void announceClientMoved(int reason, int clientId, int fromChannel, int toChannel) { private void announceClientMoved(int reason, int clientId, int fromChannel, int toChannel) {
if (!conn.isConnected()) return; if (!conn.isConnected()) return;
boolean toCurrent = conn.inOwnChannel(toChannel); boolean toCurrent = conn.own.inOwnChannel(toChannel);
boolean fromCurrent = conn.inOwnChannel(fromChannel); boolean fromCurrent = conn.own.inOwnChannel(fromChannel);
Map<String, String> vars = conn.announce.clientVars(clientId, null); Map<String, String> vars = conn.announce.clientVars(clientId, null);
switch (reason) { switch (reason) {
case REASON_MOVED: case REASON_MOVED:
@@ -385,7 +387,7 @@ final class ConnectionEventHandler implements TS3Listener {
if (!recording) { if (!recording) {
conn.announce.sound(SoundEvent.CLIENT_RECORDING_STOP, vars); conn.announce.sound(SoundEvent.CLIENT_RECORDING_STOP, vars);
} else { } else {
conn.announce.sound(conn.inOwnChannel(c.channelId) conn.announce.sound(conn.own.inOwnChannel(c.channelId)
? SoundEvent.CLIENT_RECORDING_IN_CHANNEL : SoundEvent.CLIENT_RECORDING_START, vars); ? SoundEvent.CLIENT_RECORDING_IN_CHANNEL : SoundEvent.CLIENT_RECORDING_START, vars);
} }
} }
@@ -436,7 +438,7 @@ final class ConnectionEventHandler implements TS3Listener {
// Sorting among the same siblings arrives here too, and TS3 announces it as an edit. // Sorting among the same siblings arrives here too, and TS3 announces it as an edit.
if (has(e, "channel_order")) conn.getModel().relinkChannel(ch.id, ch.parentId, e.getInt("channel_order")); if (has(e, "channel_order")) conn.getModel().relinkChannel(ch.id, ch.parentId, e.getInt("channel_order"));
if (conn.isConnected()) { if (conn.isConnected()) {
boolean current = conn.inOwnChannel(ch.id); boolean current = conn.own.inOwnChannel(ch.id);
conn.announce.sound(current conn.announce.sound(current
? byInvoker(e, SoundEvent.CHANNEL_EDITED_CURRENT_BY_YOU, ? byInvoker(e, SoundEvent.CHANNEL_EDITED_CURRENT_BY_YOU,
SoundEvent.CHANNEL_EDITED_CURRENT_BY_OTHER, SoundEvent.CHANNEL_EDITED_CURRENT_BY_OTHER) SoundEvent.CHANNEL_EDITED_CURRENT_BY_OTHER, SoundEvent.CHANNEL_EDITED_CURRENT_BY_OTHER)
@@ -93,7 +93,7 @@ final class ConnectionStatsCollector {
s.downloadQuotaMb = permissions.downloadQuotaMb(); s.downloadQuotaMb = permissions.downloadQuotaMb();
s.uploadQuotaMb = permissions.uploadQuotaMb(); s.uploadQuotaMb = permissions.uploadQuotaMb();
} }
VoiceOutput playback = conn.getPlayback(); VoiceOutput playback = conn.own.playback();
if (playback != null) { if (playback != null) {
s.voiceReceived = s.self ? playback.receiveStats() : playback.receiveStats(clientId); s.voiceReceived = s.self ? playback.receiveStats() : playback.receiveStats(clientId);
} }
@@ -0,0 +1,331 @@
package eu.lixko.ts3j.client.net;
import com.github.manevolent.ts3j.command.SingleCommand;
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
import com.github.manevolent.ts3j.protocol.ProtocolRole;
import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.audio.VoiceInput;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.sound.SoundEvent;
import java.util.function.Consumer;
import java.util.function.IntSupplier;
/**
* Ourselves on the server: the microphone and speakers of this connection and what we tell
* the server about us (muted, away, channel commander, talk request, phonetic nickname). A
* change is applied to our row in the model right away, as the server only tells the other
* clients, and published with {@code clientupdate}.
*
* <p>Every connect starts us out clean: the server knows nothing of the last one's flags.
*/
public final class OwnClient {
/** Shortest gap between two "you are talking while muted" reminders. */
private static final long MUTED_TALK_COOLDOWN_NANOS = 5_000_000_000L;
private final Settings settings;
private final AudioBackend audio;
private final Uplink uplink;
private final ServerModel model;
private final ConnectionListener ui;
private final Announcer announce;
private final IntSupplier selfId;
/** Set up by the connect thread and released by whichever thread notices the end; copied before use. */
private volatile VoiceInput microphone;
private volatile VoiceOutput playback;
/**
* Whether this connection owns the capture device. Only one connection may capture at a
* time, so the frontend hands the microphone to a single connection and the others keep
* their (idle) input pipeline.
*/
private volatile boolean microphoneActive;
private volatile boolean micMuted;
private volatile boolean micLocalMuted;
/** Speakers muted: only actions marked important are still heard. */
private volatile boolean deafened;
private volatile boolean away;
/** The away message last set; it outlives coming back, so going away again restores it. */
private volatile String awayMessage = "";
private volatile boolean commander;
private volatile long lastMutedTalkNanos;
OwnClient(Settings settings, AudioBackend audio, Uplink uplink, ServerModel model,
ConnectionListener ui, Announcer announce, IntSupplier selfId) {
this.settings = settings;
this.audio = audio;
this.uplink = uplink;
this.model = model;
this.ui = ui;
this.announce = announce;
this.selfId = selfId;
}
/** Our row in the model, or {@code null} while not on a server. */
public ClientEntry entry() {
return model.getClient(selfId.getAsInt());
}
/** Whether we are in that channel ourselves. */
boolean inOwnChannel(int channelId) {
ClientEntry self = entry();
return self != null && self.channelId == channelId;
}
public VoiceInput microphone() {
return microphone;
}
public VoiceOutput playback() {
return playback;
}
public boolean isMicMuted() {
return micMuted;
}
public boolean isMicLocalMuted() {
return micLocalMuted;
}
public boolean isDeafened() {
return deafened;
}
public boolean isAway() {
return away;
}
public String awayMessage() {
return awayMessage;
}
public boolean isCommander() {
return commander;
}
public boolean isTalkRequested() {
ClientEntry self = entry();
return self != null && self.talkRequest;
}
public String phoneticNickname() {
ClientEntry self = entry();
return self == null ? "" : self.phoneticNickname;
}
// ---- devices ----
/** Creates this connection's microphone and speakers, with every flag cleared, for a connect. */
void openDevices() {
micMuted = false;
micLocalMuted = false;
deafened = false;
away = false;
awayMessage = "";
commander = false;
VoiceOutput out = audio.createOutput(settings);
out.setMasterVolume(settings.effectiveOutputVolume());
out.setReceiveBuffer(settings.receiveBuffer);
out.setTalkListener((clientId, talking) -> {
ClientEntry c = model.getClient(clientId);
if (c != null) c.talking = talking;
if (c != null && inOwnChannel(c.channelId)) {
announce.sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_OTHER_ON
: SoundEvent.SPECIAL_MIC_CLICK_OTHER_OFF, announce.clientVars(clientId, null));
}
ui.onTalkStateChanged(clientId, talking);
});
playback = out;
VoiceInput mic = audio.createInput(settings);
mic.setTalkListener(talking -> {
int id = selfId.getAsInt();
if (id < 0) return;
ClientEntry c = model.getClient(id);
if (c != null) c.talking = talking;
announce.sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_SELF_ON : SoundEvent.SPECIAL_MIC_CLICK_SELF_OFF);
ui.onTalkStateChanged(id, talking);
});
mic.setMutedTalkListener(this::onTalkingWhileMuted);
microphone = mic;
}
/** Stops and lets go of the devices; there is no server left to tell. */
synchronized void releaseDevices() {
VoiceInput mic = microphone;
VoiceOutput out = playback;
microphone = null;
playback = null;
if (mic != null) {
try {
mic.stop();
} catch (Exception ignored) {
}
}
if (out != null) {
try {
out.shutdown();
} catch (Exception ignored) {
}
}
}
/**
* Gives this connection the capture device, or takes it away. Capturing only starts once
* connected; calling this before or during a connect is fine, the requested state is
* applied as soon as the microphone exists.
*/
public synchronized void setMicrophoneActive(boolean active) {
microphoneActive = active;
applyMicrophoneState();
}
/** Starts or stops capture to match {@link #microphoneActive}. */
synchronized void applyMicrophoneState() {
VoiceInput mic = microphone;
if (mic == null || !uplink.isUp()) return;
try {
if (microphoneActive) mic.start();
else mic.stop();
} catch (Exception e) {
ui.onError("Microphone unavailable: " + RequestException.rootMessage(e));
}
pushInputHardware();
}
/**
* Reports whether the capture device is actually available on this connection, so other
* clients see "Microphone Disabled" rather than plain silence while another tab holds it.
* {@code clientinit} always claims hardware input, so this is what corrects that once
* tabs start sharing one microphone.
*/
private void pushInputHardware() {
boolean hardware = microphoneActive;
updateEntry(self -> self.inputHardware = hardware);
publish("Microphone hardware status update failed", cmd -> flag(cmd, "client_input_hardware", hardware));
}
/** Reminds the user, at most every few seconds, that the microphone is muted. */
private void onTalkingWhileMuted() {
if (!settings.mutedTalkWarning) return;
long now = System.nanoTime();
if (now - lastMutedTalkNanos < MUTED_TALK_COOLDOWN_NANOS) return;
lastMutedTalkNanos = now;
announce.sound(SoundEvent.SPECIAL_TALKING_WHILE_MUTED);
}
// ---- what we tell the server ----
public void setMicMuted(boolean muted) {
micMuted = muted;
VoiceInput mic = microphone;
if (mic != null) mic.setMuted(muted);
announce.sound(muted ? SoundEvent.SOUND_CAPTURE_MUTED : SoundEvent.SOUND_CAPTURE_UNMUTED);
pushMuteFlags();
}
/**
* TS3's "Local Mic Mute": silences capture without touching the {@code client_input_muted}
* status the server (and other clients) see, and without a mute/unmute sound.
*/
public void setMicLocalMuted(boolean muted) {
micLocalMuted = muted;
VoiceInput mic = microphone;
if (mic != null) mic.setLocalMuted(muted);
}
public void setDeafened(boolean deaf) {
// Announce muting while we can still be heard, and unmuting once we can again.
if (deaf) announce.sound(SoundEvent.SOUND_PLAYBACK_MUTED);
deafened = deaf;
VoiceOutput out = playback;
if (out != null) out.setDeafened(deaf);
if (!deaf) announce.sound(SoundEvent.SOUND_PLAYBACK_UNMUTED);
pushMuteFlags();
}
/**
* The input/output muted flags are runtime status, not editable client-database
* properties, so they go out through clientupdate (like nickname/away), not clientedit.
*/
private void pushMuteFlags() {
boolean mic = micMuted;
boolean deaf = deafened;
updateEntry(self -> {
self.inputMuted = mic;
self.outputMuted = deaf;
});
publish("Mute status update failed", cmd -> {
flag(cmd, "client_input_muted", mic);
flag(cmd, "client_output_muted", deaf);
});
}
/** @param message the away message, or null to keep the one already set */
public void setAway(boolean away, String message) {
this.away = away;
if (message != null) awayMessage = message;
String shown = away ? awayMessage : "";
announce.sound(away ? SoundEvent.STATUS_SET_AWAY : SoundEvent.STATUS_SET_PRESENT);
updateEntry(self -> {
self.away = away;
self.awayMessage = shown;
});
publish("Away update failed", cmd -> {
flag(cmd, "client_away", away);
cmd.add(new CommandSingleParameter("client_away_message", shown));
});
}
public void setCommander(boolean commander) {
this.commander = commander;
updateEntry(self -> self.channelCommander = commander);
publish("Channel commander update failed", cmd -> flag(cmd, "client_is_channel_commander", commander));
}
/** @param message the reason shown to moderators; ignored when withdrawing the request */
public void setTalkRequest(boolean request, String message) {
updateEntry(self -> {
self.talkRequest = request;
self.talkRequestMessage = request ? message : "";
if (!request) self.talkRequestTime = 0;
});
publish("Talk power request failed", cmd -> {
flag(cmd, "client_talk_request", request);
if (request) cmd.add(new CommandSingleParameter("client_talk_request_msg", message));
});
}
/** Applies to this server only; the identity keeps its own for new connections. */
public void setPhoneticNickname(String phoneticNickname) {
updateEntry(self -> self.phoneticNickname = phoneticNickname);
publish("Setting phonetic nickname failed",
cmd -> cmd.add(new CommandSingleParameter("client_nickname_phonetic", phoneticNickname)));
}
/** Applies a change of ours to our row, which the server does not echo back to us. */
void updateEntry(Consumer<ClientEntry> change) {
ClientEntry self = entry();
if (self == null) return;
change.accept(self);
ui.onModelChanged();
}
/** Sends a {@code clientupdate} about us in the background. */
void publish(String errorLabel, Consumer<SingleCommand> fill) {
uplink.submit(sock -> {
SingleCommand cmd = new SingleCommand("clientupdate", ProtocolRole.CLIENT);
fill.accept(cmd);
sock.executeCommand(cmd).complete();
}, e -> announce.error(errorLabel + ": ", e));
}
private static void flag(SingleCommand cmd, String name, boolean value) {
cmd.add(new CommandSingleParameter(name, value ? "1" : "0"));
}
}
@@ -0,0 +1,102 @@
package eu.lixko.ts3j.client.net;
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.audio.VoiceInput;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.recording.VoiceRecorder;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
/**
* Recording what we hear on this server, our own voice included. The server is told, so
* other clients see that they are being recorded.
*/
public final class Recording {
private final AudioBackend audio;
private final Uplink uplink;
private final ServerModel model;
private final OwnClient own;
private final Supplier<String> selfUniqueId;
/** The recording in progress; guarded by this. */
private VoiceRecorder recorder;
Recording(AudioBackend audio, Uplink uplink, ServerModel model, OwnClient own, Supplier<String> selfUniqueId) {
this.audio = audio;
this.uplink = uplink;
this.model = model;
this.own = own;
this.selfUniqueId = selfUniqueId;
}
/** The recording in progress, or null. */
public synchronized VoiceRecorder current() {
return recorder;
}
public synchronized VoiceRecorder start(VoiceRecorder.Mode mode, Path file) throws IOException {
VoiceOutput out = own.playback();
VoiceInput mic = own.microphone();
if (!uplink.isUp() || out == null || mic == null) throw new IllegalStateException("Not connected");
if (recorder != null) throw new IllegalStateException("Already recording");
Map<String, String> names = new HashMap<>();
for (ClientEntry c : model.allClients()) {
if (!c.uniqueId.isEmpty()) names.putIfAbsent(c.uniqueId, c.nickname);
}
VoiceRecorder rec = VoiceRecorder.start(mode, file, audio.opus(), names);
recorder = rec;
out.setVoiceTap((clientId, pcm, frames) -> {
ClientEntry c = model.getClient(clientId);
if (c != null) rec.offer(c.uniqueId, c.nickname, pcm, 1, frames);
});
mic.setMonitorListener((pcm, channels) -> {
ClientEntry self = own.entry();
if (self != null) rec.offer(selfUniqueId.get(), self.nickname, pcm, channels, pcm.length / channels);
});
// A recording that fails to write ends by itself.
rec.whenDone().whenComplete((files, error) -> {
if (error != null) end(rec, true);
});
publish(true);
return rec;
}
/**
* Stops the recording, if any; its files are finished in the background.
*
* @param announce whether to tell the server; pointless when about to leave it
* @return the recording's files once written, or null if nothing was recording
*/
public CompletableFuture<List<Path>> stop(boolean announce) {
VoiceRecorder rec = current();
return rec != null && end(rec, announce) ? rec.stop() : null;
}
/** Ends {@code rec} if it is still the one recording. */
private boolean end(VoiceRecorder rec, boolean announce) {
synchronized (this) {
if (recorder != rec) return false;
recorder = null;
VoiceOutput out = own.playback();
VoiceInput mic = own.microphone();
if (out != null) out.setVoiceTap(null);
if (mic != null) mic.setMonitorListener(null);
}
if (announce && uplink.isUp()) publish(false);
return true;
}
private void publish(boolean recording) {
own.publish("Recording status update failed",
cmd -> cmd.add(new CommandSingleParameter("client_is_recording", recording ? "1" : "0")));
}
}
@@ -13,7 +13,6 @@ import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket; import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket;
import com.github.manevolent.ts3j.util.Ts3Crypt; import com.github.manevolent.ts3j.util.Ts3Crypt;
import eu.lixko.ts3j.client.audio.AudioBackend; import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.audio.VoiceInput;
import eu.lixko.ts3j.client.audio.VoiceOutput; import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.config.Settings; import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.contacts.Contact; import eu.lixko.ts3j.client.contacts.Contact;
@@ -28,15 +27,12 @@ import eu.lixko.ts3j.client.net.filetransfer.FileTransferManager;
import eu.lixko.ts3j.client.net.ban.BanEntry; import eu.lixko.ts3j.client.net.ban.BanEntry;
import eu.lixko.ts3j.client.net.ban.BanRule; import eu.lixko.ts3j.client.net.ban.BanRule;
import eu.lixko.ts3j.client.net.filetransfer.RemoteFile; import eu.lixko.ts3j.client.net.filetransfer.RemoteFile;
import eu.lixko.ts3j.client.recording.VoiceRecorder;
import eu.lixko.ts3j.client.sound.SoundEvent; import eu.lixko.ts3j.client.sound.SoundEvent;
import eu.lixko.ts3j.client.sound.SoundNotifier; import eu.lixko.ts3j.client.sound.SoundNotifier;
import eu.lixko.ts3j.client.text.TsLink; import eu.lixko.ts3j.client.text.TsLink;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@@ -67,9 +63,6 @@ public final class TeamspeakConnection implements TS3Listener {
private static final int REASON_SERVER_SHUTDOWN = 11; private static final int REASON_SERVER_SHUTDOWN = 11;
/** Shortest gap between two "you are talking while muted" reminders. */
private static final long MUTED_TALK_COOLDOWN_NANOS = 5_000_000_000L;
/** /**
* How long the handshake and initial download may take. That download includes the whole * How long the handshake and initial download may take. That download includes the whole
* channel list, which on a big server over a lossy link (mobile data) takes a while. * channel list, which on a big server over a lossy link (mobile data) takes a while.
@@ -77,7 +70,6 @@ public final class TeamspeakConnection implements TS3Listener {
private static final long CONNECT_TIMEOUT_MS = 30_000L; private static final long CONNECT_TIMEOUT_MS = 30_000L;
private final Settings settings; private final Settings settings;
private final AudioBackend audio;
private final ServerModel model = new ServerModel(); private final ServerModel model = new ServerModel();
/** The socket and the threads requests run on; package-private for {@link ConnectionStatsCollector}. */ /** The socket and the threads requests run on; package-private for {@link ConnectionStatsCollector}. */
final Uplink uplink = new Uplink(); final Uplink uplink = new Uplink();
@@ -103,6 +95,9 @@ public final class TeamspeakConnection implements TS3Listener {
final ServerLogAdmin serverLog = new ServerLogAdmin(uplink); final ServerLogAdmin serverLog = new ServerLogAdmin(uplink);
final OfflineMessageAdmin offlineMessages; final OfflineMessageAdmin offlineMessages;
private final AvatarAdmin avatarAdmin; private final AvatarAdmin avatarAdmin;
/** Ourselves: devices and status flags; package-private for {@link ConnectionEventHandler}. */
final OwnClient own;
private final Recording recording;
/* /*
* Set up by the connect thread and torn down by whichever thread notices the end; read from * Set up by the connect thread and torn down by whichever thread notices the end; read from
@@ -110,11 +105,7 @@ public final class TeamspeakConnection implements TS3Listener {
*/ */
/** The client whose whisper we heard last, to reply to; 0 for none yet. */ /** The client whose whisper we heard last, to reply to; 0 for none yet. */
private volatile int lastWhisperer; private volatile int lastWhisperer;
private volatile VoiceInput microphone;
private volatile VoiceOutput playback;
private volatile LocalIdentity identity; private volatile LocalIdentity identity;
/** The recording in progress; guarded by this connection. */
private VoiceRecorder recorder;
/** /**
* The connect in progress. A disconnect clears it and a newer connect replaces it, which * The connect in progress. A disconnect clears it and a newer connect replaces it, which
* tells a superseded attempt that is still failing not to clean up after itself. * tells a superseded attempt that is still failing not to clean up after itself.
@@ -126,20 +117,10 @@ public final class TeamspeakConnection implements TS3Listener {
private volatile String serverHost; private volatile String serverHost;
private volatile int serverPort; private volatile int serverPort;
/**
* Whether this connection owns the capture device. Only one connection may
* capture at a time, so the frontend hands the microphone to a single
* connection and the others keep their (idle) input pipeline.
*/
private volatile boolean microphoneActive;
/** Speakers muted: only actions marked important are still heard. */
private volatile boolean deafened;
/** Set while we are leaving on purpose, to tell a clean disconnect from a drop. */ /** Set while we are leaving on purpose, to tell a clean disconnect from a drop. */
private volatile boolean leaving; private volatile boolean leaving;
/** Guards against announcing the same disconnect from both the event and the teardown. */ /** Guards against announcing the same disconnect from both the event and the teardown. */
private volatile boolean disconnectAnnounced = true; private volatile boolean disconnectAnnounced = true;
private volatile long lastMutedTalkNanos;
/** The server's error id for why the last connect was refused; 0 when it was not. */ /** The server's error id for why the last connect was refused; 0 when it was not. */
private volatile int connectErrorId; private volatile int connectErrorId;
/** The server's {@code reasonid} for how the last connection ended; -1 when we left or never got on. */ /** The server's {@code reasonid} for how the last connection ended; -1 when we left or never got on. */
@@ -182,11 +163,10 @@ public final class TeamspeakConnection implements TS3Listener {
public TeamspeakConnection(Settings settings, AudioBackend audio, ConnectionListener ui, public TeamspeakConnection(Settings settings, AudioBackend audio, ConnectionListener ui,
SoundNotifier sounds, ContactStore contacts) { SoundNotifier sounds, ContactStore contacts) {
this.settings = settings; this.settings = settings;
this.audio = audio;
this.ui = ui; this.ui = ui;
this.contacts = contacts; this.contacts = contacts;
this.chatLog = new ChatLog(settings, ui); this.chatLog = new ChatLog(settings, ui);
this.announce = new Announcer(model, sounds, ui, chatLog, () -> deafened, uplink::isUp); this.announce = new Announcer(model, sounds, ui, chatLog, () -> ownClient().isDeafened(), uplink::isUp);
this.events = new ConnectionEventHandler(this); this.events = new ConnectionEventHandler(this);
this.icons = new IconRepository(this::fetchIcon, ui::onIconsUpdated); this.icons = new IconRepository(this::fetchIcon, ui::onIconsUpdated);
this.avatars = new AvatarRepository(this::fetchAvatar, ui::onInfoUpdated); this.avatars = new AvatarRepository(this::fetchAvatar, ui::onInfoUpdated);
@@ -194,7 +174,9 @@ public final class TeamspeakConnection implements TS3Listener {
this.complaints = new ComplaintAdmin(uplink, announce); this.complaints = new ComplaintAdmin(uplink, announce);
this.offlineMessages = new OfflineMessageAdmin(uplink, announce); this.offlineMessages = new OfflineMessageAdmin(uplink, announce);
this.avatarAdmin = new AvatarAdmin(uplink, model.permissions(), avatars, this::getSelfUniqueId, this.avatarAdmin = new AvatarAdmin(uplink, model.permissions(), avatars, this::getSelfUniqueId,
flag -> updateSelf(self -> self.avatarFlag = flag)); flag -> ownClient().updateEntry(self -> self.avatarFlag = flag));
this.own = new OwnClient(settings, audio, uplink, model, ui, announce, () -> selfClientId);
this.recording = new Recording(audio, uplink, model, own, this::getSelfUniqueId);
} }
public ServerModel getModel() { public ServerModel getModel() {
@@ -209,14 +191,6 @@ public final class TeamspeakConnection implements TS3Listener {
return selfClientId; return selfClientId;
} }
public VoiceInput getMicrophone() {
return microphone;
}
public VoiceOutput getPlayback() {
return playback;
}
/** /**
* The error id the server refused the last connect with ({@code 3329} for a ban, * The error id the server refused the last connect with ({@code 3329} for a ban,
* for one), or 0 when it was not refused or did not get that far. * for one), or 0 when it was not refused or did not get that far.
@@ -248,45 +222,13 @@ public final class TeamspeakConnection implements TS3Listener {
return serverPort; return serverPort;
} }
/** /** Ourselves: the microphone and speakers, and what we tell the server about us. */
* Gives this connection the capture device, or takes it away. Capturing only public OwnClient ownClient() {
* starts once connected; calling this before or during a connect is fine, the return own;
* requested state is applied as soon as the microphone exists.
*/
public synchronized void setMicrophoneActive(boolean active) {
microphoneActive = active;
applyMicrophoneState();
} }
public boolean isMicrophoneActive() { public Recording recording() {
return microphoneActive; return recording;
}
/** Starts or stops capture to match {@link #microphoneActive}. */
private synchronized void applyMicrophoneState() {
VoiceInput mic = microphone;
if (mic == null || !uplink.isUp()) return;
try {
if (microphoneActive) mic.start();
else mic.stop();
} catch (Exception e) {
ui.onError("Microphone unavailable: " + RequestException.rootMessage(e));
}
pushInputHardware();
}
/**
* Reports whether the capture device is actually available on this connection, so
* other clients see "Microphone Disabled" rather than plain silence while another
* tab holds it. {@code clientinit} always claims hardware input, so this is what
* corrects that once tabs start sharing one microphone.
*/
private void pushInputHardware() {
boolean hardware = microphoneActive;
updateSelf(self -> self.inputHardware = hardware);
selfUpdate(cmd ->
cmd.add(new CommandSingleParameter("client_input_hardware", hardware ? "1" : "0")),
"Microphone hardware status update failed");
} }
// ---- connection lifecycle ---- // ---- connection lifecycle ----
@@ -331,30 +273,8 @@ public final class TeamspeakConnection implements TS3Listener {
model.clear(); model.clear();
lastWhisperer = 0; lastWhisperer = 0;
playback = audio.createOutput(settings); own.openDevices();
playback.setMasterVolume(settings.effectiveOutputVolume()); VoiceOutput playback = own.playback();
playback.setReceiveBuffer(settings.receiveBuffer);
playback.setTalkListener((clientId, talking) -> {
ClientEntry c = model.getClient(clientId);
if (c != null) c.talking = talking;
if (c != null && inOwnChannel(c.channelId)) {
announce.sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_OTHER_ON
: SoundEvent.SPECIAL_MIC_CLICK_OTHER_OFF, announce.clientVars(clientId, null));
}
ui.onTalkStateChanged(clientId, talking);
});
microphone = audio.createInput(settings);
microphone.setTalkListener(talking -> {
if (selfClientId >= 0) {
ClientEntry c = model.getClient(selfClientId);
if (c != null) c.talking = talking;
announce.sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_SELF_ON
: SoundEvent.SPECIAL_MIC_CLICK_SELF_OFF);
ui.onTalkStateChanged(selfClientId, talking);
}
});
microphone.setMutedTalkListener(this::onTalkingWhileMuted);
LocalTeamspeakClientSocket client = uplink.open(); LocalTeamspeakClientSocket client = uplink.open();
client.setMyTeamSpeakAuthenticator(account == null ? null : account::authenticate); client.setMyTeamSpeakAuthenticator(account == null ? null : account::authenticate);
@@ -390,12 +310,12 @@ public final class TeamspeakConnection implements TS3Listener {
// Our own row usually arrives among the enter-view events the connect // Our own row usually arrives among the enter-view events the connect
// handshake carries, i.e. before our id was known here, so it has to be // handshake carries, i.e. before our id was known here, so it has to be
// marked once it is. // marked once it is.
ClientEntry ourselves = model.getClient(selfClientId); ClientEntry ourselves = own.entry();
if (ourselves != null) { if (ourselves != null) {
ourselves.self = true; ourselves.self = true;
model.enteredChannel(ourselves.channelId); model.enteredChannel(ourselves.channelId);
} }
client.setMicrophone(microphone); client.setMicrophone(own.microphone());
uplink.established(new FileTransferManager(client, () -> serverHost)); uplink.established(new FileTransferManager(client, () -> serverHost));
icons.retryFailed(); icons.retryFailed();
avatars.retryFailed(); avatars.retryFailed();
@@ -403,7 +323,6 @@ public final class TeamspeakConnection implements TS3Listener {
connectedAtMs = System.currentTimeMillis(); connectedAtMs = System.currentTimeMillis();
leaving = false; leaving = false;
disconnectAnnounced = false; disconnectAnnounced = false;
deafened = false;
offlineMessages.reset(); offlineMessages.reset();
chatLog.open(serverUniqueId()); chatLog.open(serverUniqueId());
ui.onConnected(); ui.onConnected();
@@ -419,7 +338,7 @@ public final class TeamspeakConnection implements TS3Listener {
unreadMessagesChanged(0); unreadMessagesChanged(0);
// Only captures if this connection holds the microphone. // Only captures if this connection holds the microphone.
applyMicrophoneState(); own.applyMicrophoneState();
} catch (Exception e) { } catch (Exception e) {
// Whoever took over reports on their own; cleaning up here would tear down what a newer // Whoever took over reports on their own; cleaning up here would tear down what a newer
// connect has set up in the meantime. // connect has set up in the meantime.
@@ -521,7 +440,7 @@ public final class TeamspeakConnection implements TS3Listener {
* @return the channel id, or -1 when unknown * @return the channel id, or -1 when unknown
*/ */
private int currentChannelId() { private int currentChannelId() {
ClientEntry self = model.getClient(selfClientId); ClientEntry self = own.entry();
if (self != null) return self.channelId; if (self != null) return self.channelId;
try { try {
return uplink.socket().getClientInfo(selfClientId).getChannelId(); return uplink.socket().getClientInfo(selfClientId).getChannelId();
@@ -600,93 +519,18 @@ public final class TeamspeakConnection implements TS3Listener {
: SoundEvent.CONNECTION_LOST_CONNECTION, announce.serverVars()); : SoundEvent.CONNECTION_LOST_CONNECTION, announce.serverVars());
} }
/** Reminds the user, at most every few seconds, that the microphone is muted. */
private void onTalkingWhileMuted() {
if (!settings.mutedTalkWarning) return;
long now = System.nanoTime();
if (now - lastMutedTalkNanos < MUTED_TALK_COOLDOWN_NANOS) return;
lastMutedTalkNanos = now;
announce.sound(SoundEvent.SPECIAL_TALKING_WHILE_MUTED);
}
private synchronized void safeCleanup() { private synchronized void safeCleanup() {
// Finishes the files; there is no server left to tell. // Finishes the files; there is no server left to tell.
VoiceRecorder rec = recorder; recording.stop(false);
if (rec != null) { own.releaseDevices();
detachRecorder();
rec.stop();
}
VoiceInput mic = microphone;
VoiceOutput out = playback;
microphone = null;
playback = null;
chatLog.close(); chatLog.close();
selfClientId = -1; selfClientId = -1;
stats.clear(); stats.clear();
if (mic != null) {
try {
mic.stop();
} catch (Exception ignored) {
}
}
if (out != null) {
try {
out.shutdown();
} catch (Exception ignored) {
}
}
uplink.close(); uplink.close();
} }
// ---- self actions ---- // ---- self actions ----
public void setMicMuted(boolean muted) {
VoiceInput mic = microphone;
if (mic != null) mic.setMuted(muted);
announce.sound(muted ? SoundEvent.SOUND_CAPTURE_MUTED : SoundEvent.SOUND_CAPTURE_UNMUTED);
pushSelfFlags();
}
/**
* TS3's "Local Mic Mute": silences capture without touching the {@code client_input_muted}
* status the server (and other clients) see, and without a mute/unmute sound.
*/
public void setMicLocalMuted(boolean muted) {
VoiceInput mic = microphone;
if (mic != null) mic.setLocalMuted(muted);
}
public void setDeafened(boolean deaf) {
// Announce muting while we can still be heard, and unmuting once we can again.
if (deaf) announce.sound(SoundEvent.SOUND_PLAYBACK_MUTED);
deafened = deaf;
VoiceOutput out = playback;
if (out != null) out.setDeafened(deaf);
if (!deaf) announce.sound(SoundEvent.SOUND_PLAYBACK_UNMUTED);
pushSelfFlags();
}
private void pushSelfFlags() {
VoiceInput mic = microphone;
VoiceOutput out = playback;
if (mic == null || out == null) return;
boolean micMuted = mic.isMuted();
boolean deaf = out.isDeafened();
updateSelf(self -> {
self.inputMuted = micMuted;
self.outputMuted = deaf;
});
// Publish the input/output muted flags to the server so others see them. These
// are runtime status, not editable client-database properties, so they go out
// through clientupdate (like nickname/away), not clientedit.
selfUpdate(cmd -> {
cmd.add(new CommandSingleParameter("client_input_muted", micMuted ? "1" : "0"));
cmd.add(new CommandSingleParameter("client_output_muted", deaf ? "1" : "0"));
}, "Mute status update failed");
}
public void joinChannel(int channelId, String password) { public void joinChannel(int channelId, String password) {
act("Could not join channel: ", sock -> act("Could not join channel: ", sock ->
sock.joinChannel(channelId, (password == null || password.isEmpty()) ? null : password)); sock.joinChannel(channelId, (password == null || password.isEmpty()) ? null : password));
@@ -878,7 +722,7 @@ public final class TeamspeakConnection implements TS3Listener {
public void sendChannelMessage(String text) { public void sendChannelMessage(String text) {
act("Message failed: ", sock -> { act("Message failed: ", sock -> {
ClientEntry self = model.getClient(selfClientId); ClientEntry self = own.entry();
int cid = self != null ? self.channelId : 0; int cid = self != null ? self.channelId : 0;
sock.sendChannelMessage(cid, text); sock.sendChannelMessage(cid, text);
keepSentChat(ConnectionListener.ChatScope.CHANNEL, "", text); keepSentChat(ConnectionListener.ChatScope.CHANNEL, "", text);
@@ -918,38 +762,6 @@ public final class TeamspeakConnection implements TS3Listener {
}); });
} }
public void setAway(boolean away, String message) {
announce.sound(away ? SoundEvent.STATUS_SET_AWAY : SoundEvent.STATUS_SET_PRESENT);
updateSelf(self -> {
self.away = away;
self.awayMessage = away && message != null ? message : "";
});
selfUpdate(cmd -> {
cmd.add(new CommandSingleParameter("client_away", away ? "1" : "0"));
cmd.add(new CommandSingleParameter("client_away_message", away && message != null ? message : ""));
}, "Away update failed");
}
public void setChannelCommander(boolean commander) {
updateSelf(self -> self.channelCommander = commander);
selfUpdate(cmd ->
cmd.add(new CommandSingleParameter("client_is_channel_commander", commander ? "1" : "0")),
"Channel commander update failed");
}
/** @param message the reason shown to moderators; ignored when withdrawing the request */
public void setTalkRequest(boolean request, String message) {
updateSelf(self -> {
self.talkRequest = request;
self.talkRequestMessage = request ? message : "";
if (!request) self.talkRequestTime = 0;
});
selfUpdate(cmd -> {
cmd.add(new CommandSingleParameter("client_talk_request", request ? "1" : "0"));
if (request) cmd.add(new CommandSingleParameter("client_talk_request_msg", message));
}, "Talk power request failed");
}
/** Grants or revokes clients' talk power, which lets them talk whatever their channel needs. */ /** Grants or revokes clients' talk power, which lets them talk whatever their channel needs. */
public void setTalker(Collection<Integer> clientIds, boolean talker) { public void setTalker(Collection<Integer> clientIds, boolean talker) {
if (clientIds.isEmpty()) return; if (clientIds.isEmpty()) return;
@@ -963,7 +775,7 @@ public final class TeamspeakConnection implements TS3Listener {
* it from everyone who has it there when {@code revokeOthers}. * it from everyone who has it there when {@code revokeOthers}.
*/ */
public void grantNextTalkPower(boolean revokeOthers) { public void grantNextTalkPower(boolean revokeOthers) {
ClientEntry self = model.getClient(selfClientId); ClientEntry self = own.entry();
if (self == null) return; if (self == null) return;
ClientEntry next = model.nextTalkRequest(self.channelId); ClientEntry next = model.nextTalkRequest(self.channelId);
List<ClientEntry> talkers = revokeOthers ? model.talkersIn(self.channelId) : List.of(); List<ClientEntry> talkers = revokeOthers ? model.talkersIn(self.channelId) : List.of();
@@ -982,109 +794,6 @@ public final class TeamspeakConnection implements TS3Listener {
new CommandSingleParameter("client_is_talker", talker ? "1" : "0"))).complete(); new CommandSingleParameter("client_is_talker", talker ? "1" : "0"))).complete();
} }
public void setPhoneticNickname(String phoneticNickname) {
updateSelf(self -> self.phoneticNickname = phoneticNickname);
selfUpdate(cmd -> cmd.add(new CommandSingleParameter("client_nickname_phonetic", phoneticNickname)),
"Setting phonetic nickname failed");
}
// ---- recording ----
/**
* Starts recording what we hear on this server, our own voice included, and tells the
* server so: other clients see that they are being recorded.
*/
public synchronized VoiceRecorder startRecording(VoiceRecorder.Mode mode, Path file) throws IOException {
VoiceOutput out = playback;
VoiceInput mic = microphone;
if (!uplink.isUp() || out == null || mic == null) throw new IllegalStateException("Not connected");
if (recorder != null) throw new IllegalStateException("Already recording");
Map<String, String> names = new HashMap<>();
for (ClientEntry c : model.allClients()) {
if (!c.uniqueId.isEmpty()) names.putIfAbsent(c.uniqueId, c.nickname);
}
VoiceRecorder rec = VoiceRecorder.start(mode, file, audio.opus(), names);
recorder = rec;
out.setVoiceTap((clientId, pcm, frames) -> {
ClientEntry c = model.getClient(clientId);
if (c != null) rec.offer(c.uniqueId, c.nickname, pcm, 1, frames);
});
mic.setMonitorListener((pcm, channels) -> {
ClientEntry self = model.getClient(selfClientId);
if (self != null) rec.offer(getSelfUniqueId(), self.nickname, pcm, channels, pcm.length / channels);
});
// A recording that fails to write ends by itself.
rec.whenDone().whenComplete((files, error) -> {
if (error != null) end(rec, true);
});
publishRecording(true);
return rec;
}
/**
* Stops the recording, if any; its files are finished in the background.
*
* @return the recording's files once written, or null if nothing was recording
*/
public CompletableFuture<List<Path>> stopRecording() {
return stopRecording(true);
}
/** @param announce whether to tell the server; pointless when about to leave it */
public CompletableFuture<List<Path>> stopRecording(boolean announce) {
VoiceRecorder rec = getRecorder();
return rec != null && end(rec, announce) ? rec.stop() : null;
}
/** Ends {@code rec} if it is still the one recording. */
private boolean end(VoiceRecorder rec, boolean announce) {
synchronized (this) {
if (recorder != rec) return false;
detachRecorder();
}
if (announce && uplink.isUp()) publishRecording(false);
return true;
}
public synchronized VoiceRecorder getRecorder() {
return recorder;
}
private synchronized void detachRecorder() {
recorder = null;
VoiceOutput out = playback;
VoiceInput mic = microphone;
if (out != null) out.setVoiceTap(null);
if (mic != null) mic.setMonitorListener(null);
}
private void publishRecording(boolean recording) {
selfUpdate(cmd -> cmd.add(new CommandSingleParameter("client_is_recording", recording ? "1" : "0")),
"Recording status update failed");
}
/**
* Applies a change of our own state to the local model. The server sends
* {@code notifyclientupdated} to the <em>other</em> clients only, so without this
* our own row would keep its old icons until something else refreshed it.
*/
private void updateSelf(java.util.function.Consumer<ClientEntry> change) {
ClientEntry self = model.getClient(selfClientId);
if (self == null) return;
change.accept(self);
ui.onModelChanged();
}
/** Sends a {@code clientupdate} for the local client in the background. */
private void selfUpdate(java.util.function.Consumer<SingleCommand> fill, String errorLabel) {
act(errorLabel + ": ", sock -> {
SingleCommand cmd = new SingleCommand("clientupdate", ProtocolRole.CLIENT);
fill.accept(cmd);
sock.executeCommand(cmd).complete();
});
}
public double getPingMillis() { public double getPingMillis() {
try { try {
LocalTeamspeakClientSocket sock = uplink.currentSocket(); LocalTeamspeakClientSocket sock = uplink.currentSocket();
@@ -1199,7 +908,7 @@ public final class TeamspeakConnection implements TS3Listener {
/** Unread messages in our offline message inbox on this server. */ /** Unread messages in our offline message inbox on this server. */
public int unreadOfflineMessages() { public int unreadOfflineMessages() {
ClientEntry self = model.getClient(selfClientId); ClientEntry self = own.entry();
return self == null ? 0 : self.unreadMessages; return self == null ? 0 : self.unreadMessages;
} }
@@ -1251,7 +960,7 @@ public final class TeamspeakConnection implements TS3Listener {
private Contact applyContactPlayback(ClientEntry client) { private Contact applyContactPlayback(ClientEntry client) {
if (client == null || client.self) return null; if (client == null || client.self) return null;
Contact contact = contacts.find(client.uniqueId); Contact contact = contacts.find(client.uniqueId);
VoiceOutput out = playback; VoiceOutput out = own.playback();
if (contact != null && out != null) { if (contact != null && out != null) {
if (contact.automute) out.setClientMuted(client.id, true); if (contact.automute) out.setClientMuted(client.id, true);
out.setClientVolume(client.id, contact.volumeGain()); out.setClientVolume(client.id, contact.volumeGain());
@@ -1277,7 +986,7 @@ public final class TeamspeakConnection implements TS3Listener {
/** Remembers a per-client volume, in the contact entry when the client has one. */ /** Remembers a per-client volume, in the contact entry when the client has one. */
public void setClientVolumeModifier(ClientEntry client, double decibels) { public void setClientVolumeModifier(ClientEntry client, double decibels) {
VoiceOutput out = playback; VoiceOutput out = own.playback();
if (out != null) out.setClientVolume(client.id, Math.pow(10.0, decibels / 20.0)); if (out != null) out.setClientVolume(client.id, Math.pow(10.0, decibels / 20.0));
Contact contact = contacts.find(client.uniqueId); Contact contact = contacts.find(client.uniqueId);
if (contact == null) return; if (contact == null) return;
@@ -1287,14 +996,14 @@ public final class TeamspeakConnection implements TS3Listener {
} }
public double getClientVolumeModifier(ClientEntry client) { public double getClientVolumeModifier(ClientEntry client) {
VoiceOutput out = playback; VoiceOutput out = own.playback();
if (out == null) return 0; if (out == null) return 0;
double gain = out.getClientVolume(client.id); double gain = out.getClientVolume(client.id);
return gain <= 0 ? -60 : 20.0 * Math.log10(gain); return gain <= 0 ? -60 : 20.0 * Math.log10(gain);
} }
public String getSelfUniqueId() { public String getSelfUniqueId() {
ClientEntry self = model.getClient(selfClientId); ClientEntry self = own.entry();
if (self != null && !self.uniqueId.isEmpty()) return self.uniqueId; if (self != null && !self.uniqueId.isEmpty()) return self.uniqueId;
LocalIdentity id = identity; LocalIdentity id = identity;
return id == null ? "" : id.getUid().toBase64(); return id == null ? "" : id.getUid().toBase64();
@@ -1479,18 +1188,11 @@ public final class TeamspeakConnection implements TS3Listener {
} }
private void keepSentChat(ConnectionListener.ChatScope scope, String peerUniqueId, String message) { private void keepSentChat(ConnectionListener.ChatScope scope, String peerUniqueId, String message) {
ClientEntry self = model.getClient(selfClientId); ClientEntry self = own.entry();
String uid = identity != null ? identity.getUid().toBase64() : ""; String uid = identity != null ? identity.getUid().toBase64() : "";
chatLog.sent(scope, selfClientId, uid, self != null ? self.nickname : settings.nickname, peerUniqueId, message); chatLog.sent(scope, selfClientId, uid, self != null ? self.nickname : settings.nickname, peerUniqueId, message);
} }
/** Whether the client is in the channel we are in ourselves. Package-private: also
* used by {@link ConnectionEventHandler}. */
boolean inOwnChannel(int channelId) {
ClientEntry self = model.getClient(selfClientId);
return self != null && self.channelId == channelId;
}
// ---- helpers ---- // ---- helpers ----
/** Package-private: also used by {@link ConnectionEventHandler}. */ /** Package-private: also used by {@link ConnectionEventHandler}. */
@@ -17,8 +17,10 @@ import eu.lixko.ts3j.client.net.ClientEntry;
import eu.lixko.ts3j.client.net.ComplaintAdmin; import eu.lixko.ts3j.client.net.ComplaintAdmin;
import eu.lixko.ts3j.client.net.ConnectionListener; import eu.lixko.ts3j.client.net.ConnectionListener;
import eu.lixko.ts3j.client.net.OfflineMessageAdmin; import eu.lixko.ts3j.client.net.OfflineMessageAdmin;
import eu.lixko.ts3j.client.net.OwnClient;
import eu.lixko.ts3j.client.net.Permissions; import eu.lixko.ts3j.client.net.Permissions;
import eu.lixko.ts3j.client.net.PrivilegeKeyAdmin; import eu.lixko.ts3j.client.net.PrivilegeKeyAdmin;
import eu.lixko.ts3j.client.net.Recording;
import eu.lixko.ts3j.client.net.Replies; import eu.lixko.ts3j.client.net.Replies;
import eu.lixko.ts3j.client.net.ServerLogAdmin; import eu.lixko.ts3j.client.net.ServerLogAdmin;
import eu.lixko.ts3j.client.net.TeamspeakConnection; import eu.lixko.ts3j.client.net.TeamspeakConnection;
@@ -79,6 +81,8 @@ public final class ServerSession {
private final IdentityStore identities; private final IdentityStore identities;
private final Listener listener; private final Listener listener;
private final TeamspeakConnection conn; private final TeamspeakConnection conn;
private final OwnClient own;
private final Recording recording;
private final AutoReconnect reconnects = new AutoReconnect(this); private final AutoReconnect reconnects = new AutoReconnect(this);
private volatile String title = "New connection"; private volatile String title = "New connection";
@@ -98,13 +102,6 @@ public final class ServerSession {
private record Target(String address, int port, String nickname, String password, String channelPassword) { private record Target(String address, int port, String nickname, String password, String channelPassword) {
} }
private volatile boolean micMuted;
private volatile boolean micLocalMuted;
private volatile boolean deafened;
private volatile boolean away;
/** Away message currently published, empty when away carries no message. */
private volatile String awayMessage = "";
private volatile boolean commander;
/** Where the microphone whispers to; null when it talks in our channel. */ /** Where the microphone whispers to; null when it talks in our channel. */
private volatile Whisper whisper; private volatile Whisper whisper;
/** {@link #whisper} as last resolved on the server, to resend only when it changes. */ /** {@link #whisper} as last resolved on the server, to resend only when it changes. */
@@ -116,6 +113,8 @@ public final class ServerSession {
this.identities = identities; this.identities = identities;
this.listener = listener; this.listener = listener;
this.conn = new TeamspeakConnection(settings, audio, new Events(), sounds, contacts); this.conn = new TeamspeakConnection(settings, audio, new Events(), sounds, contacts);
this.own = conn.ownClient();
this.recording = conn.recording();
} }
public TeamspeakConnection connection() { public TeamspeakConnection connection() {
@@ -322,11 +321,11 @@ public final class ServerSession {
/** Synchronous teardown for shutdown paths, so the server sees us leave and recordings are written. */ /** Synchronous teardown for shutdown paths, so the server sees us leave and recordings are written. */
public void shutdown() { public void shutdown() {
reconnects.cancel(); reconnects.cancel();
CompletableFuture<List<Path>> recording = conn.stopRecording(false); CompletableFuture<List<Path>> files = recording.stop(false);
if (isBusy()) conn.disconnectBlocking("Leaving"); if (isBusy()) conn.disconnectBlocking("Leaving");
if (recording != null) { if (files != null) {
try { try {
recording.join(); files.join();
} catch (RuntimeException ignored) { } catch (RuntimeException ignored) {
// Already reported. // Already reported.
} }
@@ -363,45 +362,42 @@ public final class ServerSession {
// ---- self state ---- // ---- self state ----
public boolean isMicMuted() { public boolean isMicMuted() {
return micMuted; return own.isMicMuted();
} }
public boolean isMicLocalMuted() { public boolean isMicLocalMuted() {
return micLocalMuted; return own.isMicLocalMuted();
} }
public boolean isDeafened() { public boolean isDeafened() {
return deafened; return own.isDeafened();
} }
public boolean isAway() { public boolean isAway() {
return away; return own.isAway();
} }
public String awayMessage() { public String awayMessage() {
return awayMessage; return own.awayMessage();
} }
public boolean isCommander() { public boolean isCommander() {
return commander; return own.isCommander();
} }
public void setMicMuted(boolean muted) { public void setMicMuted(boolean muted) {
micMuted = muted; own.setMicMuted(muted);
conn.setMicMuted(muted);
listener.onSessionChanged(); listener.onSessionChanged();
} }
/** TS3's "Local Mic Mute": silences capture without publishing a status change. */ /** TS3's "Local Mic Mute": silences capture without publishing a status change. */
public void setMicLocalMuted(boolean muted) { public void setMicLocalMuted(boolean muted) {
micLocalMuted = muted; own.setMicLocalMuted(muted);
conn.setMicLocalMuted(muted);
listener.onSessionChanged(); listener.onSessionChanged();
} }
public void setDeafened(boolean deaf) { public void setDeafened(boolean deaf) {
deafened = deaf; own.setDeafened(deaf);
conn.setDeafened(deaf);
listener.onSessionChanged(); listener.onSessionChanged();
} }
@@ -410,21 +406,18 @@ public final class ServerSession {
* message outlives coming back, so toggling away again restores it * message outlives coming back, so toggling away again restores it
*/ */
public void setAway(boolean away, String message) { public void setAway(boolean away, String message) {
this.away = away; own.setAway(away, message);
if (message != null) this.awayMessage = message;
conn.setAway(away, awayMessage);
listener.onSessionChanged(); listener.onSessionChanged();
} }
public void setCommander(boolean commander) { public void setCommander(boolean commander) {
this.commander = commander; own.setCommander(commander);
conn.setChannelCommander(commander);
listener.onSessionChanged(); listener.onSessionChanged();
} }
/** Hands the capture device to (or takes it from) this connection. */ /** Hands the capture device to (or takes it from) this connection. */
public void setMicrophoneActive(boolean active) { public void setMicrophoneActive(boolean active) {
conn.setMicrophoneActive(active); own.setMicrophoneActive(active);
} }
public void setNickname(String nickname) { public void setNickname(String nickname) {
@@ -432,17 +425,16 @@ public final class ServerSession {
} }
public boolean isTalkRequested() { public boolean isTalkRequested() {
ClientEntry self = self(); return conn.isConnected() && own.isTalkRequested();
return conn.isConnected() && self != null && self.talkRequest;
} }
/** @param message the reason shown to moderators, may be empty */ /** @param message the reason shown to moderators, may be empty */
public void requestTalkPower(String message) { public void requestTalkPower(String message) {
if (conn.isConnected() && canRequestTalkPower()) conn.setTalkRequest(true, message); if (conn.isConnected() && canRequestTalkPower()) own.setTalkRequest(true, message);
} }
public void cancelTalkRequest() { public void cancelTalkRequest() {
if (conn.isConnected()) conn.setTalkRequest(false, ""); if (conn.isConnected()) own.setTalkRequest(false, "");
} }
/** Grants talk power to whoever in our channel asked first, after revoking everyone else's when {@code revokeOthers}. */ /** Grants talk power to whoever in our channel asked first, after revoking everyone else's when {@code revokeOthers}. */
@@ -451,13 +443,12 @@ public final class ServerSession {
} }
public String phoneticNickname() { public String phoneticNickname() {
ClientEntry self = self(); return own.phoneticNickname();
return self == null ? "" : self.phoneticNickname;
} }
/** Applies to this server only; the identity keeps its own phonetic nickname for new connections. */ /** Applies to this server only; the identity keeps its own phonetic nickname for new connections. */
public void setPhoneticNickname(String phoneticNickname) { public void setPhoneticNickname(String phoneticNickname) {
if (conn.isConnected()) conn.setPhoneticNickname(phoneticNickname); if (conn.isConnected()) own.setPhoneticNickname(phoneticNickname);
} }
// ---- whisper ---- // ---- whisper ----
@@ -501,39 +492,25 @@ public final class ServerSession {
/** What the local client looks like on this server. */ /** What the local client looks like on this server. */
public SelfState selfState() { public SelfState selfState() {
if (!conn.isConnected()) return SelfState.DISCONNECTED; if (!conn.isConnected()) return SelfState.DISCONNECTED;
if (deafened) return SelfState.DEAFENED; if (own.isDeafened()) return SelfState.DEAFENED;
if (micMuted) return SelfState.MIC_MUTED; if (own.isMicMuted()) return SelfState.MIC_MUTED;
if (micLocalMuted) return SelfState.MIC_LOCAL_MUTED; if (own.isMicLocalMuted()) return SelfState.MIC_LOCAL_MUTED;
if (away) return SelfState.AWAY; if (own.isAway()) return SelfState.AWAY;
ClientEntry self = self(); ClientEntry self = own.entry();
boolean talking = self != null && self.talking; boolean talking = self != null && self.talking;
if (commander) return talking ? SelfState.COMMANDER_TALKING : SelfState.COMMANDER; if (own.isCommander()) return talking ? SelfState.COMMANDER_TALKING : SelfState.COMMANDER;
return talking ? SelfState.TALKING : SelfState.IDLE; return talking ? SelfState.TALKING : SelfState.IDLE;
} }
/** Clears all flags on a fresh connection; the server starts us out clean, so nothing to publish. */
private void resetSelfState() {
micMuted = false;
micLocalMuted = false;
deafened = false;
away = false;
awayMessage = "";
commander = false;
}
private ClientEntry self() {
return conn.getModel().getClient(conn.getSelfClientId());
}
// ---- recording ---- // ---- recording ----
public boolean isRecording() { public boolean isRecording() {
return conn.getRecorder() != null; return recording.current() != null;
} }
/** The recording in progress, or null. */ /** The recording in progress, or null. */
public VoiceRecorder.Mode recordingMode() { public VoiceRecorder.Mode recordingMode() {
VoiceRecorder rec = conn.getRecorder(); VoiceRecorder rec = recording.current();
return rec == null ? null : rec.mode(); return rec == null ? null : rec.mode();
} }
@@ -553,7 +530,7 @@ public final class ServerSession {
try { try {
Path dir = file.toAbsolutePath().getParent(); Path dir = file.toAbsolutePath().getParent();
if (dir != null) Files.createDirectories(dir); if (dir != null) Files.createDirectories(dir);
rec = conn.startRecording(mode, file); rec = recording.start(mode, file);
} catch (IOException | RuntimeException e) { } catch (IOException | RuntimeException e) {
listener.onSystemMessage("Failed to start recording: " + e.getMessage()); listener.onSystemMessage("Failed to start recording: " + e.getMessage());
return; return;
@@ -575,7 +552,7 @@ public final class ServerSession {
/** Stops the recording; its files are finished in the background, then reported. */ /** Stops the recording; its files are finished in the background, then reported. */
public void stopRecording() { public void stopRecording() {
if (conn.stopRecording() != null) listener.onSessionChanged(); if (recording.stop(true) != null) listener.onSessionChanged();
} }
// ---- chat ---- // ---- chat ----
@@ -650,14 +627,14 @@ public final class ServerSession {
/** The channel we are in, or null when not connected. */ /** The channel we are in, or null when not connected. */
public ChannelNode currentChannel() { public ChannelNode currentChannel() {
if (!conn.isConnected()) return null; if (!conn.isConnected()) return null;
ClientEntry self = self(); ClientEntry self = own.entry();
return self == null ? null : conn.getModel().getChannel(self.channelId); return self == null ? null : conn.getModel().getChannel(self.channelId);
} }
/** Path of the channel we are in, or empty when not connected. */ /** Path of the channel we are in, or empty when not connected. */
public String currentChannelPath() { public String currentChannelPath() {
if (!conn.isConnected()) return ""; if (!conn.isConnected()) return "";
ClientEntry self = self(); ClientEntry self = own.entry();
return self == null ? "" : conn.getModel().channelPath(self.channelId); return self == null ? "" : conn.getModel().channelPath(self.channelId);
} }
@@ -691,13 +668,13 @@ public final class ServerSession {
/** Whether we publish an avatar on this server. */ /** Whether we publish an avatar on this server. */
public boolean hasAvatar() { public boolean hasAvatar() {
if (!conn.isConnected()) return false; if (!conn.isConnected()) return false;
ClientEntry self = conn.getModel().getClient(conn.getSelfClientId()); ClientEntry self = own.entry();
return self != null && !self.avatarFlag.isEmpty(); return self != null && !self.avatarFlag.isEmpty();
} }
/** Whether we may ask for talk power: the server lets us, and we lack it in our channel, as it refuses otherwise. */ /** Whether we may ask for talk power: the server lets us, and we lack it in our channel, as it refuses otherwise. */
public boolean canRequestTalkPower() { public boolean canRequestTalkPower() {
ClientEntry self = self(); ClientEntry self = own.entry();
return permissions().canRequestTalkPower() && self != null && conn.getModel().lacksTalkPower(self); return permissions().canRequestTalkPower() && self != null && conn.getModel().lacksTalkPower(self);
} }
@@ -776,7 +753,6 @@ public final class ServerSession {
public void onConnected() { public void onConnected() {
connecting = false; connecting = false;
reconnects.connected(); reconnects.connected();
resetSelfState();
listener.onConnected(); listener.onConnected();
listener.onSystemMessage("Connected."); listener.onSystemMessage("Connected.");
listener.onSessionChanged(); listener.onSessionChanged();
@@ -131,7 +131,7 @@ public final class Sessions {
} }
private static VoiceInput microphoneOf(ServerSession session) { private static VoiceInput microphoneOf(ServerSession session) {
return session == null || !session.isConnected() ? null : session.connection().getMicrophone(); return session == null || !session.isConnected() ? null : session.connection().ownClient().microphone();
} }
// ---- the user's own state ---- // ---- the user's own state ----
@@ -1,6 +1,7 @@
package eu.lixko.ts3j.client.ui; package eu.lixko.ts3j.client.ui;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.audio.AudioBackend; import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.audio.desktop.DesktopAudioBackend; import eu.lixko.ts3j.client.audio.desktop.DesktopAudioBackend;
import eu.lixko.ts3j.client.config.AppDirs; import eu.lixko.ts3j.client.config.AppDirs;
@@ -1033,8 +1034,8 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
// Device and voice-gating changes apply to the capturing connection and the // Device and voice-gating changes apply to the capturing connection and the
// playback of the visible one; the rest pick the new settings up on connect. // playback of the visible one; the rest pick the new settings up on connect.
SettingsDialog dlg = new SettingsDialog(this, settings, audio, SettingsDialog dlg = new SettingsDialog(this, settings, audio,
sessions.microphoneOwner() == null ? null : sessions.microphoneOwner().connection().getMicrophone(), sessions.microphoneOwner() == null ? null : sessions.microphoneOwner().connection().ownClient().microphone(),
selected == null ? null : selected.connection().getPlayback(), selected == null ? null : selected.connection().ownClient().playback(),
sounds, hotkeys, sounds, hotkeys,
new MyTeamSpeakPanel(myTeamSpeak, new TeamSpeakImporter(identities, bookmarks, settings, contacts), new MyTeamSpeakPanel(myTeamSpeak, new TeamSpeakImporter(identities, bookmarks, settings, contacts),
menuBar::rebuildBookmarks, this::myTeamSpeakChanged), menuBar::rebuildBookmarks, this::myTeamSpeakChanged),
@@ -1076,10 +1077,11 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
private void applyOutputSettingsToAllTabs() { private void applyOutputSettingsToAllTabs() {
soundPlayer.setOutputDevice(settings.outputDevice); soundPlayer.setOutputDevice(settings.outputDevice);
for (ServerTab tab : tabs) { for (ServerTab tab : tabs) {
if (tab.connection().getPlayback() == null) continue; VoiceOutput playback = tab.connection().ownClient().playback();
tab.connection().getPlayback().setMasterVolume(settings.effectiveOutputVolume()); if (playback == null) continue;
tab.connection().getPlayback().setOutputDevice(settings.outputDevice); playback.setMasterVolume(settings.effectiveOutputVolume());
tab.connection().getPlayback().setReceiveBuffer(settings.receiveBuffer); playback.setOutputDevice(settings.outputDevice);
playback.setReceiveBuffer(settings.receiveBuffer);
} }
} }
@@ -1,5 +1,6 @@
package eu.lixko.ts3j.client.ui; package eu.lixko.ts3j.client.ui;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.contacts.ContactType; import eu.lixko.ts3j.client.contacts.ContactType;
import eu.lixko.ts3j.client.net.ChannelNode; import eu.lixko.ts3j.client.net.ChannelNode;
import eu.lixko.ts3j.client.net.Spacers; import eu.lixko.ts3j.client.net.Spacers;
@@ -182,9 +183,8 @@ final class ServerTabTreeActions implements ServerTreePanel.Actions {
@Override @Override
public void toggleClientMute(ClientEntry client) { public void toggleClientMute(ClientEntry client) {
if (conn.getPlayback() == null) return; VoiceOutput playback = conn.ownClient().playback();
boolean now = !conn.getPlayback().isClientMuted(client.id); if (playback != null) playback.setClientMuted(client.id, !playback.isClientMuted(client.id));
conn.getPlayback().setClientMuted(client.id, now);
} }
@Override @Override
@@ -358,7 +358,8 @@ final class ServerTabTreeActions implements ServerTreePanel.Actions {
@Override @Override
public boolean isClientLocallyMuted(int clientId) { public boolean isClientLocallyMuted(int clientId) {
return conn.getPlayback() != null && conn.getPlayback().isClientMuted(clientId); VoiceOutput playback = conn.ownClient().playback();
return playback != null && playback.isClientMuted(clientId);
} }
@Override @Override