A stream task per voice connection runs tsc-stream's Streams: the voice
task forwards stream notifications and command failures to it and sends
its requests on the connection. It starts only when the server flavor has
the streams capability; stream commands elsewhere give an Error event.
Commands: StartStream{setup, auto_accept}, StopStream, AcceptViewer,
KickViewer, SendStreamFrame, WatchStream, LeaveStream,
RequestStreamKeyframe.
Events: StreamsChanged, StreamState (Starting / Live{id, sink} /
Ended), StreamViewerRequest, StreamViewers, StreamKeyframeRequest,
WatchState (Requested / Connecting / Connected / Ended).
Encoded frames of our stream go in through the StreamSink handed out
with StreamState::Live (or SendStreamFrame); received frames of watched
streams come on their own bus, Engine::subscribe_frames, to keep the
event bus light. VoiceOptions::stream_peer sets the peer network config.
Tests: two stream tasks through a fake relay server (unit), and a
TSC_LIVE test with two engine sessions streaming synthetic frames on the
TS6 dev server plus the error on TS3.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
324 lines
8.0 KiB
Rust
324 lines
8.0 KiB
Rust
//! The client engine.
|
|
//!
|
|
//! The UI talks to an [`Engine`] with [`Command`]s and receives [`Event`]s.
|
|
//! Each server the user works with is a session with up to three sources:
|
|
//!
|
|
//! - **voice**: a normal client connection (visible, can talk)
|
|
//! - **gateway**: a `tsgw` gateway (invisible presence, relayed chat)
|
|
//! - **query**: the user's own ServerQuery credentials (same, without a gateway)
|
|
//!
|
|
//! Presence comes from the most authoritative connected source
|
|
//! (voice > gateway > query). Channel chat goes through the voice connection
|
|
//! when the user is in that channel, otherwise through a relay.
|
|
//!
|
|
//! On TeamSpeak 6 servers the voice connection also carries streams (screen
|
|
//! sharing): see the `*Stream*` commands and events, and
|
|
//! [`Engine::subscribe_frames`] for the frames of watched streams.
|
|
|
|
mod audio;
|
|
mod gateway;
|
|
mod query;
|
|
mod route;
|
|
mod session;
|
|
pub mod stream;
|
|
mod voice;
|
|
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::{broadcast, mpsc};
|
|
use tsc_model::{Capabilities, ChannelId, ChatMessage, ChatTarget, Presence, ServerFlavor};
|
|
|
|
pub use route::{ChatRoute, Dedup, route_chat};
|
|
pub use stream::{StreamFrame, StreamSink, StreamState, WatchState};
|
|
use tsc_stream::{EncodedFrame, StreamInfo, StreamSetup, ViewerInfo};
|
|
pub use voice::VoiceOptions;
|
|
|
|
pub type SessionId = u64;
|
|
|
|
/// What the UI asks for.
|
|
#[derive(Clone, Debug)]
|
|
pub enum Command {
|
|
ConnectVoice {
|
|
session: SessionId,
|
|
options: Box<VoiceOptions>,
|
|
},
|
|
DisconnectVoice {
|
|
session: SessionId,
|
|
},
|
|
/// Invisible presence and relay chat through a `tsgw` gateway.
|
|
ObserveGateway {
|
|
session: SessionId,
|
|
url: String,
|
|
identity: Box<tsclientlib::Identity>,
|
|
},
|
|
/// Invisible presence and relay chat with own ServerQuery credentials.
|
|
ObserveQuery {
|
|
session: SessionId,
|
|
connect: Box<tsc_query::Connect>,
|
|
},
|
|
StopObserving {
|
|
session: SessionId,
|
|
},
|
|
OpenChat {
|
|
session: SessionId,
|
|
target: ChatTarget,
|
|
},
|
|
CloseChat {
|
|
session: SessionId,
|
|
target: ChatTarget,
|
|
},
|
|
SendChat {
|
|
session: SessionId,
|
|
target: ChatTarget,
|
|
text: String,
|
|
},
|
|
MoveToChannel {
|
|
session: SessionId,
|
|
channel: ChannelId,
|
|
password: Option<String>,
|
|
},
|
|
SetInputMuted {
|
|
session: SessionId,
|
|
muted: bool,
|
|
},
|
|
SetOutputMuted {
|
|
session: SessionId,
|
|
muted: bool,
|
|
},
|
|
/// Push-to-talk state (or "always transmit" when voice activation is off).
|
|
SetTransmitting {
|
|
session: SessionId,
|
|
on: bool,
|
|
},
|
|
/// Start streaming in our channel (TeamSpeak 6 only). Progress comes as
|
|
/// [`Event::StreamState`]; frames go in through the sink of
|
|
/// [`StreamState::Live`] or [`Command::SendStreamFrame`].
|
|
StartStream {
|
|
session: SessionId,
|
|
setup: StreamSetup,
|
|
/// Accept every viewer instead of asking with [`Event::StreamViewerRequest`].
|
|
auto_accept: bool,
|
|
},
|
|
StopStream {
|
|
session: SessionId,
|
|
},
|
|
/// Answer an [`Event::StreamViewerRequest`].
|
|
AcceptViewer {
|
|
session: SessionId,
|
|
viewer: u16,
|
|
accept: bool,
|
|
},
|
|
/// Remove a viewer from our stream.
|
|
KickViewer {
|
|
session: SessionId,
|
|
viewer: u16,
|
|
},
|
|
/// An encoded frame for our stream.
|
|
SendStreamFrame {
|
|
session: SessionId,
|
|
frame: EncodedFrame,
|
|
},
|
|
/// Watch a stream from [`Event::StreamsChanged`]. Frames arrive through
|
|
/// [`Engine::subscribe_frames`].
|
|
WatchStream {
|
|
session: SessionId,
|
|
stream_id: String,
|
|
},
|
|
LeaveStream {
|
|
session: SessionId,
|
|
stream_id: String,
|
|
},
|
|
/// Ask the streamer of a watched stream for a keyframe.
|
|
RequestStreamKeyframe {
|
|
session: SessionId,
|
|
stream_id: String,
|
|
},
|
|
/// Close everything of a session.
|
|
CloseSession {
|
|
session: SessionId,
|
|
},
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
pub enum VoiceState {
|
|
#[default]
|
|
Disconnected,
|
|
Connecting,
|
|
Connected,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
pub enum ObserveState {
|
|
#[default]
|
|
Off,
|
|
Connecting,
|
|
/// Invisible presence is live.
|
|
Observing,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Source {
|
|
Voice,
|
|
Gateway,
|
|
Query,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct SessionState {
|
|
pub voice: VoiceState,
|
|
pub observe: ObserveState,
|
|
/// Where presence currently comes from.
|
|
pub presence_source: Option<Source>,
|
|
/// Our own channel while voice-connected.
|
|
pub own_channel: Option<ChannelId>,
|
|
pub input_muted: bool,
|
|
pub output_muted: bool,
|
|
pub transmitting: bool,
|
|
}
|
|
|
|
/// What the engine reports.
|
|
#[derive(Clone, Debug)]
|
|
pub enum Event {
|
|
State {
|
|
session: SessionId,
|
|
state: SessionState,
|
|
},
|
|
ServerInfo {
|
|
session: SessionId,
|
|
name: String,
|
|
flavor: ServerFlavor,
|
|
capabilities: Capabilities,
|
|
},
|
|
/// Full presence of the session (the UI rebuilds its view from it).
|
|
Presence {
|
|
session: SessionId,
|
|
presence: Arc<Presence>,
|
|
},
|
|
Chat {
|
|
session: SessionId,
|
|
message: ChatMessage,
|
|
},
|
|
/// A client in the session started or stopped talking (voice only).
|
|
Talking {
|
|
session: SessionId,
|
|
client: u16,
|
|
talking: bool,
|
|
},
|
|
Error {
|
|
session: SessionId,
|
|
message: String,
|
|
},
|
|
/// The streams in our channel (TeamSpeak 6), full list.
|
|
StreamsChanged {
|
|
session: SessionId,
|
|
streams: Vec<StreamInfo>,
|
|
},
|
|
/// Our own stream.
|
|
StreamState {
|
|
session: SessionId,
|
|
state: StreamState,
|
|
},
|
|
/// Someone asks to watch our stream; answer with [`Command::AcceptViewer`].
|
|
StreamViewerRequest {
|
|
session: SessionId,
|
|
viewer: u16,
|
|
message: String,
|
|
},
|
|
/// The viewers of our stream and their state, full list.
|
|
StreamViewers {
|
|
session: SessionId,
|
|
viewers: Vec<ViewerInfo>,
|
|
},
|
|
/// A viewer needs a keyframe (also flagged on the [`StreamSink`]).
|
|
StreamKeyframeRequest {
|
|
session: SessionId,
|
|
},
|
|
/// A stream we watch.
|
|
WatchState {
|
|
session: SessionId,
|
|
stream_id: String,
|
|
state: WatchState,
|
|
},
|
|
}
|
|
|
|
/// Handle to the engine. Cheap to clone; all methods are non-blocking.
|
|
#[derive(Clone)]
|
|
pub struct Engine {
|
|
commands: mpsc::UnboundedSender<Command>,
|
|
events: broadcast::Sender<Event>,
|
|
frames: broadcast::Sender<StreamFrame>,
|
|
}
|
|
|
|
impl Engine {
|
|
/// Start the engine on the current tokio runtime.
|
|
pub fn start() -> Self {
|
|
let (commands, rx) = mpsc::unbounded_channel();
|
|
let (events, _) = broadcast::channel(4096);
|
|
// About a minute of one watched stream.
|
|
let (frames, _) = broadcast::channel(4096);
|
|
tokio::spawn(run(rx, events.clone(), frames.clone()));
|
|
Self { commands, events, frames }
|
|
}
|
|
|
|
pub fn send(&self, command: Command) {
|
|
let _ = self.commands.send(command);
|
|
}
|
|
|
|
pub fn subscribe(&self) -> broadcast::Receiver<Event> {
|
|
self.events.subscribe()
|
|
}
|
|
|
|
/// Encoded frames of the streams we watch (kept off the event bus).
|
|
pub fn subscribe_frames(&self) -> broadcast::Receiver<StreamFrame> {
|
|
self.frames.subscribe()
|
|
}
|
|
}
|
|
|
|
async fn run(
|
|
mut commands: mpsc::UnboundedReceiver<Command>,
|
|
events: broadcast::Sender<Event>,
|
|
frames: broadcast::Sender<StreamFrame>,
|
|
) {
|
|
let mut sessions: HashMap<SessionId, session::SessionHandle> = HashMap::new();
|
|
while let Some(command) = commands.recv().await {
|
|
let id = command_session(&command);
|
|
if matches!(command, Command::CloseSession { .. }) {
|
|
if let Some(s) = sessions.remove(&id) {
|
|
s.send(command);
|
|
}
|
|
continue;
|
|
}
|
|
sessions
|
|
.entry(id)
|
|
.or_insert_with(|| session::SessionHandle::spawn(id, events.clone(), frames.clone()))
|
|
.send(command);
|
|
}
|
|
}
|
|
|
|
fn command_session(command: &Command) -> SessionId {
|
|
match command {
|
|
Command::ConnectVoice { session, .. }
|
|
| Command::DisconnectVoice { session }
|
|
| Command::ObserveGateway { session, .. }
|
|
| Command::ObserveQuery { session, .. }
|
|
| Command::StopObserving { session }
|
|
| Command::OpenChat { session, .. }
|
|
| Command::CloseChat { session, .. }
|
|
| Command::SendChat { session, .. }
|
|
| Command::MoveToChannel { session, .. }
|
|
| Command::SetInputMuted { session, .. }
|
|
| Command::SetOutputMuted { session, .. }
|
|
| Command::SetTransmitting { session, .. }
|
|
| Command::StartStream { session, .. }
|
|
| Command::StopStream { session }
|
|
| Command::AcceptViewer { session, .. }
|
|
| Command::KickViewer { session, .. }
|
|
| Command::SendStreamFrame { session, .. }
|
|
| Command::WatchStream { session, .. }
|
|
| Command::LeaveStream { session, .. }
|
|
| Command::RequestStreamKeyframe { session, .. }
|
|
| Command::CloseSession { session } => *session,
|
|
}
|
|
}
|