Merge late-join stream discovery and the settings service
Conflicts with the SRTP/BWE/simulcast work were in the engine's command loop, session and stream tasks (both sides kept: SRTP order and layers next to settings and discovery) and in the voelin-stream exports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
This commit is contained in:
commit
6538734a6e
30 files changed
+3419
-123
No files matched your search
@@ -0,0 +1,338 @@
|
||||
//! Finding streams the server did not announce to us.
|
||||
//!
|
||||
//! The server sends `notifystreamstarted` only to the clients that are in the
|
||||
//! streamer's channel when the stream starts. A client that connects later, or
|
||||
//! enters the channel later, only sees the streamer's `client_is_streaming=1`.
|
||||
//! `requeststreaminfo clid=<streamer>` answers with a `notifystreaminfo` part
|
||||
//! per stream of that client, for any client on the server (see
|
||||
//! `docs/research/ts6-late-join.md`).
|
||||
//!
|
||||
//! [`Discovery`] follows the clients on the server (their channel and
|
||||
//! streaming flag, fed by [`Streams::update_clients`](crate::Streams::update_clients)),
|
||||
//! keeps the [`StreamDirectory`] to the streams in our channel, and looks up
|
||||
//! the streams of clients that stream in our channel but whose stream we were
|
||||
//! not told: after connecting, after we or they change channels, and when a
|
||||
//! streamer's announcement did not come. Where it looks them up is a
|
||||
//! [`StreamLookup`]: the server ([`ServerLookup`]) now; another directory
|
||||
//! (e.g. a gateway's) can be added with
|
||||
//! [`Streams::add_lookup`](crate::Streams::add_lookup) and hands what it finds
|
||||
//! to [`Streams::discovered`](crate::Streams::discovered).
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use tracing::debug;
|
||||
use tsclientlib::ClientId;
|
||||
|
||||
use crate::session::{Outbox, Request, StreamDirectory};
|
||||
|
||||
/// What discovery needs to know about a client on the server.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ClientState {
|
||||
pub channel: u64,
|
||||
/// `client_is_streaming`; `None` if the server did not say (TeamSpeak 3).
|
||||
pub streaming: Option<bool>,
|
||||
}
|
||||
|
||||
/// A place to look up the streams of a client.
|
||||
pub trait StreamLookup: Send {
|
||||
/// Start looking up the streams of `streamer`; `false` if this source
|
||||
/// cannot (then the next one is asked). The server's answer arrives as
|
||||
/// `notifystreaminfo`; other sources report through
|
||||
/// [`Streams::discovered`](crate::Streams::discovered).
|
||||
fn lookup(&mut self, streamer: ClientId, out: &mut Outbox) -> bool;
|
||||
|
||||
/// A request of this source failed on the server.
|
||||
fn failed(&mut self, _request: &Request, _error: &str) {}
|
||||
}
|
||||
|
||||
/// Asks the server: `requeststreaminfo clid=<streamer>`.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ServerLookup {
|
||||
/// The server does not know the command (older TeamSpeak 6 versions).
|
||||
unsupported: bool,
|
||||
}
|
||||
|
||||
impl StreamLookup for ServerLookup {
|
||||
fn lookup(&mut self, streamer: ClientId, out: &mut Outbox) -> bool {
|
||||
if self.unsupported {
|
||||
return false;
|
||||
}
|
||||
out.request(Request::StreamInfo { streamer });
|
||||
true
|
||||
}
|
||||
|
||||
fn failed(&mut self, request: &Request, error: &str) {
|
||||
// "command not found" (error 256): never ask again on this connection.
|
||||
if error.contains("CommandNotFound") || error.contains("command not found") {
|
||||
debug!(?request, "the server does not know requeststreaminfo");
|
||||
self.unsupported = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See the [module docs](self).
|
||||
pub struct Discovery {
|
||||
own: ClientId,
|
||||
/// The clients as of the last update; empty before the first.
|
||||
clients: BTreeMap<u16, ClientState>,
|
||||
/// Streamers looked up since they (or we) last changed channel or
|
||||
/// started streaming; asked again after such a change.
|
||||
asked: BTreeSet<u16>,
|
||||
lookups: Vec<Box<dyn StreamLookup>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Discovery {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Discovery")
|
||||
.field("own", &self.own)
|
||||
.field("clients", &self.clients.len())
|
||||
.field("asked", &self.asked)
|
||||
.field("lookups", &self.lookups.len())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Discovery {
|
||||
/// Discovery through the server.
|
||||
pub fn new(own: ClientId) -> Self {
|
||||
Self {
|
||||
own,
|
||||
clients: BTreeMap::new(),
|
||||
asked: BTreeSet::new(),
|
||||
lookups: vec![Box::new(ServerLookup::default())],
|
||||
}
|
||||
}
|
||||
|
||||
/// Another place to look streams up, asked after the ones before.
|
||||
pub fn add_lookup(&mut self, lookup: Box<dyn StreamLookup>) {
|
||||
self.lookups.push(lookup);
|
||||
}
|
||||
|
||||
/// Our channel, once the clients are known.
|
||||
pub fn own_channel(&self) -> Option<u64> {
|
||||
self.clients.get(&self.own.0).map(|c| c.channel)
|
||||
}
|
||||
|
||||
/// The channel of a client, as of the last update.
|
||||
pub fn channel_of(&self, client: ClientId) -> Option<u64> {
|
||||
self.clients.get(&client.0).map(|c| c.channel)
|
||||
}
|
||||
|
||||
/// Whether a stream of `streamer` belongs in the directory: it is in our
|
||||
/// channel (or the clients are not known yet).
|
||||
pub fn in_our_channel(&self, streamer: ClientId) -> bool {
|
||||
match self.own_channel() {
|
||||
None => true,
|
||||
Some(own) => self.clients.get(&streamer.0).is_some_and(|c| c.channel == own),
|
||||
}
|
||||
}
|
||||
|
||||
/// The clients on the server changed (full list). Updates the streaming
|
||||
/// flags in `directory`, drops the streams of clients that left or are
|
||||
/// not in our channel, and looks up the streams we were not told.
|
||||
/// Returns whether the directory's streams changed.
|
||||
pub fn update(
|
||||
&mut self,
|
||||
clients: BTreeMap<u16, ClientState>,
|
||||
directory: &mut StreamDirectory,
|
||||
out: &mut Outbox,
|
||||
) -> bool {
|
||||
let old_channel = self.own_channel();
|
||||
let new_channel = clients.get(&self.own.0).map(|c| c.channel);
|
||||
let mut changed = false;
|
||||
// Streams announced to us but whose streamer went away or out of our channel.
|
||||
let first = self.clients.is_empty();
|
||||
for (id, now) in &clients {
|
||||
let was = self.clients.get(id);
|
||||
match (was.and_then(|c| c.streaming), now.streaming) {
|
||||
// Only changes count: a list may predate the streaming flag of
|
||||
// a stream that was just announced.
|
||||
(Some(was), Some(now)) if was != now => {
|
||||
changed |= directory.set_streaming(ClientId(*id), now);
|
||||
}
|
||||
(None, Some(true)) => {
|
||||
directory.set_streaming(ClientId(*id), true);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if was.is_none_or(|was| was.channel != now.channel || was.streaming != now.streaming) {
|
||||
self.asked.remove(id);
|
||||
}
|
||||
}
|
||||
if old_channel != new_channel {
|
||||
self.asked.clear();
|
||||
}
|
||||
self.asked.retain(|id| clients.contains_key(id));
|
||||
changed |= directory.retain_streamers(|c| {
|
||||
clients.get(&c.0).is_some_and(|s| new_channel.is_none_or(|own| s.channel == own))
|
||||
});
|
||||
|
||||
// A client in our channel that just started streaming is announced
|
||||
// by the server itself; look it up only if the announcement has not
|
||||
// come by the next update.
|
||||
let just_started = |id: &u16, now: &ClientState| {
|
||||
let was = self.clients.get(id);
|
||||
!first
|
||||
&& was.is_some_and(|was| {
|
||||
was.streaming != Some(true) && Some(was.channel) == old_channel
|
||||
}) && Some(now.channel) == new_channel
|
||||
};
|
||||
let candidates: Vec<u16> = clients
|
||||
.iter()
|
||||
.filter(|(id, c)| {
|
||||
**id != self.own.0
|
||||
&& c.streaming == Some(true)
|
||||
&& new_channel.is_some_and(|own| c.channel == own)
|
||||
&& directory.by_streamer(ClientId(**id)).is_none()
|
||||
&& !self.asked.contains(id)
|
||||
&& !just_started(id, c)
|
||||
})
|
||||
.map(|(id, _)| *id)
|
||||
.collect();
|
||||
self.clients = clients;
|
||||
for id in candidates {
|
||||
let streamer = ClientId(id);
|
||||
if self.lookups.iter_mut().any(|l| l.lookup(streamer, out)) {
|
||||
debug!(streamer = id, "looking up an unannounced stream");
|
||||
self.asked.insert(id);
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
/// A request of a lookup failed on the server.
|
||||
pub fn failed(&mut self, request: &Request, error: &str) {
|
||||
for lookup in &mut self.lookups {
|
||||
lookup.failed(request, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::session::Output;
|
||||
use crate::{StreamInfo, StreamKind, StreamNotification};
|
||||
|
||||
const OWN: u16 = 1;
|
||||
|
||||
fn clients(list: &[(u16, u64, bool)]) -> BTreeMap<u16, ClientState> {
|
||||
list.iter()
|
||||
.map(|&(id, channel, streaming)| {
|
||||
(id, ClientState { channel, streaming: Some(streaming) })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn looked_up(out: &mut Outbox) -> Vec<u16> {
|
||||
let mut ids = Vec::new();
|
||||
while let Some(o) = out.pop() {
|
||||
match o {
|
||||
Output::Request(Request::StreamInfo { streamer }) => ids.push(streamer.0),
|
||||
other => panic!("unexpected {other:?}"),
|
||||
}
|
||||
}
|
||||
ids
|
||||
}
|
||||
|
||||
fn started(id: &str, streamer: u16) -> StreamNotification {
|
||||
StreamNotification::Started {
|
||||
info: StreamInfo {
|
||||
id: id.into(),
|
||||
streamer: ClientId(streamer),
|
||||
name: String::new(),
|
||||
kind: StreamKind::Screen,
|
||||
bitrate: 4608,
|
||||
viewer_limit: 0,
|
||||
audio: true,
|
||||
},
|
||||
return_code: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn looks_up_streams_that_were_not_announced() {
|
||||
let mut d = Discovery::new(ClientId(OWN));
|
||||
let mut dir = StreamDirectory::default();
|
||||
let mut out = Outbox::default();
|
||||
|
||||
// Connected: 2 streams in our channel, 3 in another one.
|
||||
d.update(clients(&[(OWN, 1, false), (2, 1, true), (3, 5, true)]), &mut dir, &mut out);
|
||||
assert_eq!(looked_up(&mut out), [2]);
|
||||
// Asked once, not again on the next update.
|
||||
d.update(
|
||||
clients(&[(OWN, 1, false), (2, 1, true), (3, 5, true), (4, 1, false)]),
|
||||
&mut dir,
|
||||
&mut out,
|
||||
);
|
||||
assert_eq!(looked_up(&mut out), [] as [u16; 0]);
|
||||
// The answer fills the directory.
|
||||
assert!(dir.apply(&started("s-2", 2)));
|
||||
|
||||
// We move to 3's channel: 3 is looked up, 2's stream leaves the list.
|
||||
let moved = clients(&[(OWN, 5, false), (2, 1, true), (3, 5, true), (4, 1, false)]);
|
||||
assert!(d.update(moved.clone(), &mut dir, &mut out));
|
||||
assert_eq!(looked_up(&mut out), [3]);
|
||||
assert!(dir.get("s-2").is_none());
|
||||
assert!(d.in_our_channel(ClientId(3)) && !d.in_our_channel(ClientId(2)));
|
||||
|
||||
// 4 comes to our channel and starts streaming there: announced by
|
||||
// the server, so not looked up at once...
|
||||
let mut now = moved.clone();
|
||||
now.insert(4, ClientState { channel: 5, streaming: Some(false) });
|
||||
d.update(now.clone(), &mut dir, &mut out);
|
||||
now.insert(4, ClientState { channel: 5, streaming: Some(true) });
|
||||
d.update(now.clone(), &mut dir, &mut out);
|
||||
assert_eq!(looked_up(&mut out), [] as [u16; 0]);
|
||||
// ...only if the announcement has not come by the next update.
|
||||
now.insert(9, ClientState { channel: 1, streaming: Some(false) });
|
||||
d.update(now.clone(), &mut dir, &mut out);
|
||||
assert_eq!(looked_up(&mut out), [4]);
|
||||
|
||||
// A streamer that stops loses its stream; one that left too.
|
||||
assert!(dir.apply(&started("s-4", 4)));
|
||||
now.insert(4, ClientState { channel: 5, streaming: Some(false) });
|
||||
assert!(d.update(now.clone(), &mut dir, &mut out));
|
||||
assert!(dir.get("s-4").is_none());
|
||||
assert!(dir.apply(&started("s-3", 3)));
|
||||
now.remove(&3);
|
||||
assert!(d.update(now, &mut dir, &mut out));
|
||||
assert_eq!(dir.iter().count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_server_without_the_command_is_not_asked_again() {
|
||||
let mut d = Discovery::new(ClientId(OWN));
|
||||
let mut dir = StreamDirectory::default();
|
||||
let mut out = Outbox::default();
|
||||
d.update(clients(&[(OWN, 1, false), (2, 1, true)]), &mut dir, &mut out);
|
||||
let request = Request::StreamInfo { streamer: ClientId(2) };
|
||||
assert_eq!(looked_up(&mut out), [2]);
|
||||
d.failed(&request, "command failed: CommandNotFound");
|
||||
d.update(clients(&[(OWN, 1, false), (3, 1, true)]), &mut dir, &mut out);
|
||||
assert_eq!(looked_up(&mut out), [] as [u16; 0]);
|
||||
}
|
||||
|
||||
/// A second source, e.g. a gateway's directory.
|
||||
struct Recorder(std::sync::Arc<std::sync::Mutex<Vec<u16>>>);
|
||||
|
||||
impl StreamLookup for Recorder {
|
||||
fn lookup(&mut self, streamer: ClientId, _out: &mut Outbox) -> bool {
|
||||
self.0.lock().unwrap().push(streamer.0);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_lookups_follow_the_server() {
|
||||
let mut d = Discovery::new(ClientId(OWN));
|
||||
let asked = std::sync::Arc::default();
|
||||
d.add_lookup(Box::new(Recorder(std::sync::Arc::clone(&asked))));
|
||||
let mut dir = StreamDirectory::default();
|
||||
let mut out = Outbox::default();
|
||||
d.failed(&Request::StreamInfo { streamer: ClientId(2) }, "command not found");
|
||||
d.update(clients(&[(OWN, 1, false), (2, 1, true)]), &mut dir, &mut out);
|
||||
assert_eq!(looked_up(&mut out), [] as [u16; 0]);
|
||||
assert_eq!(*asked.lock().unwrap(), [2]);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
//! streams in our channel, our own stream with a peer per viewer, and the
|
||||
//! streams we watch. [`FrameSource`] is where a streamer's encoded frames come from.
|
||||
|
||||
pub mod discovery;
|
||||
pub mod dtls;
|
||||
pub mod feedback;
|
||||
pub mod layer;
|
||||
@@ -19,6 +20,7 @@ pub mod signal;
|
||||
pub mod source;
|
||||
pub mod stun;
|
||||
|
||||
pub use discovery::{ClientState, StreamLookup};
|
||||
pub use dtls::SrtpProfile;
|
||||
pub use feedback::LayerFeedback;
|
||||
pub use layer::{LayerId, LayerSet, LayerSpec};
|
||||
|
||||
@@ -126,7 +126,18 @@ pub enum StreamNotification {
|
||||
info: StreamInfo,
|
||||
return_code: Option<String>,
|
||||
},
|
||||
/// A stream in our channel, or the answer to [`stream_info`].
|
||||
Info(StreamInfo),
|
||||
/// The streamer changed its stream (`updatestream`); only the changed fields.
|
||||
Updated {
|
||||
id: String,
|
||||
streamer: ClientId,
|
||||
name: Option<String>,
|
||||
kind: Option<StreamKind>,
|
||||
bitrate: Option<u32>,
|
||||
viewer_limit: Option<u32>,
|
||||
audio: Option<bool>,
|
||||
},
|
||||
Stopped {
|
||||
id: String,
|
||||
streamer: Option<ClientId>,
|
||||
@@ -183,11 +194,13 @@ impl StreamNotification {
|
||||
return_code: p.return_code.clone(),
|
||||
})
|
||||
.collect(),
|
||||
// The answer for a client without streams has no parts but
|
||||
// `return_code`.
|
||||
InMessage::StreamInfo(m) => m
|
||||
.iter()
|
||||
.filter_map(|p| {
|
||||
Some(Self::Info(StreamInfo {
|
||||
id: p.stream_id.clone(),
|
||||
id: p.stream_id.clone().filter(|id| !id.is_empty())?,
|
||||
streamer: p.client_id?,
|
||||
name: p.stream_name.clone().unwrap_or_default(),
|
||||
kind: StreamKind::from_u8(p.stream_type.unwrap_or(3)),
|
||||
@@ -197,6 +210,18 @@ impl StreamNotification {
|
||||
}))
|
||||
})
|
||||
.collect(),
|
||||
InMessage::StreamUpdated(m) => m
|
||||
.iter()
|
||||
.map(|p| Self::Updated {
|
||||
id: p.stream_id.clone(),
|
||||
streamer: p.client_id,
|
||||
name: p.stream_name.clone(),
|
||||
kind: p.stream_type.map(StreamKind::from_u8),
|
||||
bitrate: p.bitrate,
|
||||
viewer_limit: p.viewer_limit,
|
||||
audio: p.audio,
|
||||
})
|
||||
.collect(),
|
||||
InMessage::StreamStopped(m) => m
|
||||
.iter()
|
||||
.map(|p| Self::Stopped {
|
||||
@@ -252,7 +277,8 @@ impl StreamNotification {
|
||||
pub fn stream_id(&self) -> &str {
|
||||
match self {
|
||||
Self::Started { info, .. } | Self::Info(info) => &info.id,
|
||||
Self::Stopped { id, .. }
|
||||
Self::Updated { id, .. }
|
||||
| Self::Stopped { id, .. }
|
||||
| Self::JoinRequest { id, .. }
|
||||
| Self::JoinResponse { id, .. }
|
||||
| Self::Signaling { id, .. }
|
||||
@@ -320,6 +346,17 @@ pub fn signaling(id: &str, peer: ClientId, json: &str) -> OutCommand {
|
||||
))
|
||||
}
|
||||
|
||||
/// `requeststreaminfo`: ask for the streams of `streamer`, also one in
|
||||
/// another channel. The server answers with `notifystreaminfo` (one part per
|
||||
/// stream; none if the client does not stream). This is how a client learns
|
||||
/// about streams that started before it joined the server or the channel:
|
||||
/// the server announces streams only to those in the channel when they start.
|
||||
pub fn stream_info(streamer: ClientId) -> OutCommand {
|
||||
c2s::OutRequestStreamInfoMessage::new(&mut iter::once(c2s::OutRequestStreamInfoPart {
|
||||
client_id: streamer,
|
||||
}))
|
||||
}
|
||||
|
||||
/// `removeclientfromstream`: end a viewer's session (`reason` is usually
|
||||
/// [`LeaveReason::Kicked`]; the viewer gets `notifystreamclientleft` with it).
|
||||
pub fn remove_viewer(id: &str, viewer: ClientId, reason: LeaveReason) -> OutCommand {
|
||||
@@ -340,6 +377,56 @@ mod tests {
|
||||
String::from_utf8(cmd.0.content().to_vec()).unwrap()
|
||||
}
|
||||
|
||||
/// The stream notifications in a command as the server sends it.
|
||||
fn parse(command: &str) -> Vec<StreamNotification> {
|
||||
use tsproto_packets::packets::{Direction, Flags, InPacket, PacketType};
|
||||
let out = OutCommand::new(Direction::S2C, Flags::empty(), PacketType::Command, command);
|
||||
let packet = InPacket::new(Direction::S2C, out.0.data());
|
||||
let msg = InMessage::new(packet.header(), packet.content()).unwrap();
|
||||
StreamNotification::from_message(&msg)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stream_info_answers() {
|
||||
// As the server answered `requeststreaminfo` (6.0.0-beta13.1).
|
||||
let answer = parse(
|
||||
"notifystreaminfo return_code=0 clid=6 id=6b5158dd name=renamed\\sagain type=3 \
|
||||
accessibility=1 mode=1 viewer=0 bitrate=4000 viewer_limit=0 audio=1|clid=6 \
|
||||
id=069ba23c name=second type=3 accessibility=1 mode=1 viewer=2 bitrate=4608 \
|
||||
viewer_limit=0 audio=0",
|
||||
);
|
||||
let infos: Vec<_> = answer
|
||||
.iter()
|
||||
.map(|n| match n {
|
||||
StreamNotification::Info(i) => {
|
||||
(i.id.as_str(), i.streamer, i.name.as_str(), i.audio)
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
infos,
|
||||
[
|
||||
("6b5158dd", ClientId(6), "renamed again", true),
|
||||
("069ba23c", ClientId(6), "second", false)
|
||||
]
|
||||
);
|
||||
// A client without streams.
|
||||
assert!(parse("notifystreaminfo return_code=1").is_empty());
|
||||
assert_eq!(
|
||||
parse("notifystreamupdated clid=6 id=6b5158dd name=renamed bitrate=4000"),
|
||||
[StreamNotification::Updated {
|
||||
id: "6b5158dd".into(),
|
||||
streamer: ClientId(6),
|
||||
name: Some("renamed".into()),
|
||||
kind: None,
|
||||
bitrate: Some(4000),
|
||||
viewer_limit: None,
|
||||
audio: None,
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commands() {
|
||||
let s = StreamSetup { name: "my screen".into(), ..Default::default() };
|
||||
@@ -361,6 +448,7 @@ mod tests {
|
||||
text(remove_viewer("u-1", ClientId(7), LeaveReason::Kicked)),
|
||||
"removeclientfromstream id=u-1 clid=7 reason=5"
|
||||
);
|
||||
assert_eq!(text(stream_info(ClientId(6))), "requeststreaminfo clid=6");
|
||||
let r = text(respond("u-1", ClientId(7), Some("v=0"), true));
|
||||
assert!(r.starts_with("respondjoinstreamrequest id=u-1 clid=7"), "{r}");
|
||||
assert!(r.contains("offer=v=0") && r.contains("decision=1"), "{r}");
|
||||
|
||||
@@ -36,6 +36,7 @@ use tracing::{debug, warn};
|
||||
use tsclientlib::ClientId;
|
||||
use tsproto_packets::packets::OutCommand;
|
||||
|
||||
use crate::discovery::{ClientState, Discovery, StreamLookup};
|
||||
use crate::dtls::SrtpProfile;
|
||||
use crate::feedback::LayerFeedback;
|
||||
use crate::layer::{LayerId, LayerSet, LayerSpec};
|
||||
@@ -83,6 +84,8 @@ pub enum Request {
|
||||
Signal { id: String, peer: ClientId, signal: Signal },
|
||||
/// `removeclientfromstream`
|
||||
RemoveViewer { id: String, viewer: ClientId, reason: LeaveReason },
|
||||
/// `requeststreaminfo`: the streams of a client (see [`crate::discovery`]).
|
||||
StreamInfo { streamer: ClientId },
|
||||
}
|
||||
|
||||
impl Request {
|
||||
@@ -99,6 +102,7 @@ impl Request {
|
||||
}
|
||||
Self::Signal { id, peer, signal } => proto::signaling(id, *peer, &signal.to_json()),
|
||||
Self::RemoveViewer { id, viewer, reason } => proto::remove_viewer(id, *viewer, *reason),
|
||||
Self::StreamInfo { streamer } => proto::stream_info(*streamer),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -264,6 +268,18 @@ impl StreamDirectory {
|
||||
self.streaming.insert(info.streamer.0);
|
||||
self.streams.insert(info.id.clone(), info.clone()).as_ref() != Some(info)
|
||||
}
|
||||
StreamNotification::Updated {
|
||||
id, name, kind, bitrate, viewer_limit, audio, ..
|
||||
} => {
|
||||
let Some(info) = self.streams.get_mut(id) else { return false };
|
||||
let before = info.clone();
|
||||
info.name = name.clone().unwrap_or(before.name.clone());
|
||||
info.kind = kind.unwrap_or(before.kind);
|
||||
info.bitrate = bitrate.unwrap_or(before.bitrate);
|
||||
info.viewer_limit = viewer_limit.unwrap_or(before.viewer_limit);
|
||||
info.audio = audio.unwrap_or(before.audio);
|
||||
*info != before
|
||||
}
|
||||
StreamNotification::Stopped { id, .. } => self.streams.remove(id).is_some(),
|
||||
_ => false,
|
||||
}
|
||||
@@ -1421,6 +1437,7 @@ pub struct Streams {
|
||||
own: ClientId,
|
||||
config: PeerConfig,
|
||||
directory: StreamDirectory,
|
||||
discovery: Discovery,
|
||||
streamer: Option<StreamerSession>,
|
||||
viewers: BTreeMap<String, ViewerSession>,
|
||||
out: Outbox,
|
||||
@@ -1433,6 +1450,7 @@ impl Streams {
|
||||
own,
|
||||
config,
|
||||
directory: StreamDirectory::default(),
|
||||
discovery: Discovery::new(own),
|
||||
streamer: None,
|
||||
viewers: BTreeMap::new(),
|
||||
out: Outbox::default(),
|
||||
@@ -1447,6 +1465,11 @@ impl Streams {
|
||||
&self.directory
|
||||
}
|
||||
|
||||
/// The clients' channels as of [`Self::update_clients`].
|
||||
pub fn discovery(&self) -> &Discovery {
|
||||
&self.discovery
|
||||
}
|
||||
|
||||
/// Our stream, while it has not ended.
|
||||
pub fn streamer(&self) -> Option<&StreamerSession> {
|
||||
self.streamer.as_ref().filter(|s| !s.is_ended())
|
||||
@@ -1607,7 +1630,10 @@ impl Streams {
|
||||
|
||||
/// Feed a stream notification from the connection.
|
||||
pub async fn handle_notification(&mut self, n: StreamNotification) {
|
||||
if self.directory.apply(&n) {
|
||||
// Looked up streams of clients that have left our channel since.
|
||||
let elsewhere = matches!(&n, StreamNotification::Info(info)
|
||||
if !self.discovery.in_our_channel(info.streamer));
|
||||
if !elsewhere && self.directory.apply(&n) {
|
||||
self.emit_streams();
|
||||
}
|
||||
if let Some(s) = &mut self.streamer
|
||||
@@ -1637,12 +1663,40 @@ impl Streams {
|
||||
}
|
||||
}
|
||||
|
||||
/// The clients on the server changed (full list, with ourselves): keeps
|
||||
/// the directory to the streams in our channel and looks up the streams
|
||||
/// the server did not announce to us (see [`crate::discovery`]). Covers
|
||||
/// [`Self::set_client_streaming`] and [`Self::retain_streamers`].
|
||||
pub fn update_clients(&mut self, clients: BTreeMap<u16, ClientState>) {
|
||||
if self.discovery.update(clients, &mut self.directory, &mut self.out) {
|
||||
self.emit_streams();
|
||||
}
|
||||
}
|
||||
|
||||
/// A stream found by another [`StreamLookup`] (e.g. a gateway's directory).
|
||||
pub fn discovered(&mut self, info: StreamInfo) {
|
||||
if self.discovery.in_our_channel(info.streamer)
|
||||
&& self.directory.apply(&StreamNotification::Info(info))
|
||||
{
|
||||
self.emit_streams();
|
||||
}
|
||||
}
|
||||
|
||||
/// Another place to look up unannounced streams, after the server.
|
||||
pub fn add_lookup(&mut self, lookup: Box<dyn StreamLookup>) {
|
||||
self.discovery.add_lookup(lookup);
|
||||
}
|
||||
|
||||
/// A request from [`Output::Request`] failed on the server.
|
||||
pub fn request_failed(&mut self, request: &Request, error: &str) {
|
||||
let watched = match request {
|
||||
Request::Join { id, .. } | Request::Leave { id, .. } | Request::Signal { id, .. } => {
|
||||
self.viewers.get_mut(id)
|
||||
}
|
||||
Request::StreamInfo { .. } => {
|
||||
self.discovery.failed(request, error);
|
||||
return;
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if let Some(v) = watched {
|
||||
|
||||
Reference in new issue
Block a user