diff --git a/crates/voelin-core/src/settings.rs b/crates/voelin-core/src/settings.rs index c246dbd..eb2b7ba 100644 --- a/crates/voelin-core/src/settings.rs +++ b/crates/voelin-core/src/settings.rs @@ -1048,7 +1048,8 @@ pub enum StreamPermissions { /// Friends (contacts marked friend) are accepted; others are shown to /// the user ([`crate::Event::StreamViewerRequest`]). Friends, - /// Clients in our channel are accepted, others denied. + /// Clients in our channel are accepted; others (a stream can be watched + /// from any channel) are shown to the user. #[default] Channel, /// Every join request is denied. diff --git a/crates/voelin-core/src/stream.rs b/crates/voelin-core/src/stream.rs index 6060d99..018bda3 100644 --- a/crates/voelin-core/src/stream.rs +++ b/crates/voelin-core/src/stream.rs @@ -4,13 +4,16 @@ //! The voice task forwards stream notifications here and sends the resulting //! commands. Only started for TeamSpeak 6 servers. //! -//! Streams that started before we joined are looked up on the server -//! (`requeststreaminfo`) and in the session's gateway directory: its -//! registered entries (stream id and streamer) are handed to the stream -//! sessions ([`voelin_stream::Streams::discovered`]) for streamers in our -//! channel, and a [`StreamLookup`] answers for the gateway when the server -//! cannot. Our own stream's life (live, viewers, ended) goes back to the -//! session ([`OwnStreamEvent`]), which keeps its directory entry. +//! The streams of the whole server are listed, in every channel: a stream can +//! be watched from any channel, as with the official client. Streams that +//! started before we joined, and streams in other channels (the server +//! announces a stream only in its channel), are looked up on the server +//! (`requeststreaminfo`), so no gateway is needed. A gateway's directory is a +//! second source: its registered entries (stream id and streamer) are handed +//! to the stream sessions ([`voelin_stream::Streams::discovered`]), and a +//! [`StreamLookup`] answers for the gateway when the server cannot. Our own +//! stream's life (live, viewers, ended) goes back to the session +//! ([`OwnStreamEvent`]), which keeps its directory entry. use std::collections::{BTreeMap, BTreeSet}; use std::fmt; @@ -554,10 +557,11 @@ impl StreamTask { StreamPermissions::Nobody => Some(false), // Friends are let in; others: ask. StreamPermissions::Friends => self.friends.contains(&viewer.0).then_some(true), + // Our channel is let in; viewers from other channels: ask. StreamPermissions::Channel => { let discovery = self.streams.discovery(); let own = discovery.own_channel(); - Some(own.is_some() && discovery.channel_of(viewer) == own) + (own.is_some() && discovery.channel_of(viewer) == own).then_some(true) } } } @@ -1016,7 +1020,8 @@ mod tests { assert_eq!(parse_kind("game"), StreamKind::Screen); } - /// `stream.permissions` answers join requests (`friends`: friends only, others are asked). + /// `stream.permissions` answers join requests (`friends` and `channel`: + /// those are let in, others are asked). #[tokio::test(flavor = "multi_thread")] async fn permissions_answer_join_requests() { let in_channels = |streamer: u64, viewer: u64| { @@ -1028,7 +1033,7 @@ mod tests { for (permissions, viewer_channel, friend, expected) in [ (StreamPermissions::Everyone, 2, false, Some(true)), (StreamPermissions::Channel, 1, false, Some(true)), - (StreamPermissions::Channel, 2, false, Some(false)), + (StreamPermissions::Channel, 2, false, None), (StreamPermissions::Nobody, 1, false, Some(false)), (StreamPermissions::Friends, 1, false, None), (StreamPermissions::Friends, 2, true, Some(true)), diff --git a/crates/voelin-stream/src/discovery.rs b/crates/voelin-stream/src/discovery.rs index beacbfe..75d3329 100644 --- a/crates/voelin-stream/src/discovery.rs +++ b/crates/voelin-stream/src/discovery.rs @@ -7,12 +7,17 @@ //! per stream of that client, for any client on the server (see //! `docs/research/ts6-late-join.md`). //! +//! The answer works from any channel, and so does `joinstreamrequest`: a +//! client can watch a stream in another channel, as the official client does. +//! //! [`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 +//! keeps the [`StreamDirectory`] to the streams of clients that are on the +//! server and stream, in any channel, and looks up the streams of clients +//! that stream but whose stream we were not told: after connecting, when a +//! client in another channel starts streaming (the server announces it only +//! in that channel), after we or they change channels, and when a streamer's +//! announcement in our channel 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 @@ -118,19 +123,16 @@ impl Discovery { 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), - } + /// Whether a stream of `streamer` belongs in the directory: the client is + /// on the server (or the clients are not known yet). + pub fn knows(&self, streamer: ClientId) -> bool { + self.clients.is_empty() || self.clients.contains_key(&streamer.0) } /// 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. + /// flags in `directory`, drops the streams of clients that left, and + /// looks up the streams we were not told, in every channel. Returns + /// whether the directory's streams changed. pub fn update( &mut self, clients: BTreeMap, @@ -140,7 +142,7 @@ impl Discovery { 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. + // Streams announced to us but whose streamer stopped or went away. let first = self.clients.is_empty(); for (id, now) in &clients { let was = self.clients.get(id); @@ -163,9 +165,7 @@ impl Discovery { 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)) - }); + changed |= directory.retain_streamers(|c| clients.contains_key(&c.0)); // A client in our channel that just started streaming is announced // by the server itself; look it up only if the announcement has not @@ -177,12 +177,13 @@ impl Discovery { was.streaming != Some(true) && Some(was.channel) == old_channel }) && Some(now.channel) == new_channel }; + // Once we are on the server ourselves: every channel. let candidates: Vec = clients .iter() .filter(|(id, c)| { **id != self.own.0 && c.streaming == Some(true) - && new_channel.is_some_and(|own| c.channel == own) + && new_channel.is_some() && directory.by_streamer(ClientId(**id)).is_none() && !self.asked.contains(id) && !just_started(id, c) @@ -256,9 +257,10 @@ mod tests { let mut dir = StreamDirectory::default(); let mut out = Outbox::default(); - // Connected: 2 streams in our channel, 3 in another one. + // Connected: 2 streams in our channel, 3 in another one; both are + // looked up (a stream can be watched from any channel). d.update(clients(&[(OWN, 1, false), (2, 1, true), (3, 5, true)]), &mut dir, &mut out); - assert_eq!(looked_up(&mut out), [2]); + assert_eq!(looked_up(&mut out), [2, 3]); // Asked once, not again on the next update. d.update( clients(&[(OWN, 1, false), (2, 1, true), (3, 5, true), (4, 1, false)]), @@ -266,15 +268,28 @@ mod tests { &mut out, ); assert_eq!(looked_up(&mut out), [] as [u16; 0]); - // The answer fills the directory. + // The answers fill the directory. assert!(dir.apply(&started("s-2", 2))); + assert!(dir.apply(&started("s-3", 3))); - // We move to 3's channel: 3 is looked up, 2's stream leaves the list. + // We move to 3's channel: both streams stay, nothing is asked again. 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))); + assert!(!d.update(moved.clone(), &mut dir, &mut out)); + assert_eq!(looked_up(&mut out), [] as [u16; 0]); + assert!(dir.get("s-2").is_some() && dir.get("s-3").is_some()); + assert!(d.knows(ClientId(2)) && !d.knows(ClientId(7))); + assert_eq!((d.channel_of(ClientId(2)), d.own_channel()), (Some(1), Some(5))); + + // A client in another channel starts streaming: the server does not + // announce it to us, so it is looked up at once. + let mut now = moved.clone(); + now.insert(6, ClientState { channel: 1, streaming: Some(false) }); + d.update(now.clone(), &mut dir, &mut out); + now.insert(6, ClientState { channel: 1, streaming: Some(true) }); + d.update(now.clone(), &mut dir, &mut out); + assert_eq!(looked_up(&mut out), [6]); + now.remove(&6); + d.update(now, &mut dir, &mut out); // 4 comes to our channel and starts streaming there: announced by // the server, so not looked up at once... @@ -294,8 +309,10 @@ mod tests { 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.clone(), &mut dir, &mut out)); + assert!(dir.get("s-3").is_none()); + now.insert(2, ClientState { channel: 1, streaming: Some(false) }); assert!(d.update(now, &mut dir, &mut out)); assert_eq!(dir.iter().count(), 0); } diff --git a/crates/voelin-stream/src/session.rs b/crates/voelin-stream/src/session.rs index c4b03af..5a58e47 100644 --- a/crates/voelin-stream/src/session.rs +++ b/crates/voelin-stream/src/session.rs @@ -1672,10 +1672,10 @@ impl Streams { /// Feed a stream notification from the connection. pub async fn handle_notification(&mut self, n: StreamNotification) { - // 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) { + // Looked up streams of clients that have left the server since. + let gone = matches!(&n, StreamNotification::Info(info) + if !self.discovery.knows(info.streamer)); + if !gone && self.directory.apply(&n) { self.emit_streams(); } if let Some(s) = &mut self.streamer @@ -1706,8 +1706,9 @@ 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 + /// the directory to the streams of clients that are on the server and + /// stream, in any 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) { if self.discovery.update(clients, &mut self.directory, &mut self.out) { @@ -1717,7 +1718,7 @@ impl 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) + if self.discovery.knows(info.streamer) && self.directory.apply(&StreamNotification::Info(info)) { self.emit_streams(); diff --git a/crates/voelin-ui/src/app.rs b/crates/voelin-ui/src/app.rs index fc42ae1..113f858 100644 --- a/crates/voelin-ui/src/app.rs +++ b/crates/voelin-ui/src/app.rs @@ -248,6 +248,19 @@ impl SessionView { self.capabilities.streams && self.state.voice == VoiceState::Connected } + /// The channel a client is in. + pub fn channel_of(&self, client: u16) -> Option { + self.presence.clients.get(&client).map(|c| c.channel) + } + + /// The name of a client's channel when it is not ours; empty in ours. + pub fn other_channel_name(&self, client: u16) -> String { + self.channel_of(client) + .filter(|c| Some(*c) != self.state.own_channel) + .and_then(|c| self.presence.channels.get(&c)) + .map_or_else(String::new, |c| c.name.clone()) + } + /// A client's nickname, or its id. pub fn nickname(&self, client: u16) -> String { self.presence diff --git a/crates/voelin-ui/src/streams.rs b/crates/voelin-ui/src/streams.rs index 84640a5..7cecbf2 100644 --- a/crates/voelin-ui/src/streams.rs +++ b/crates/voelin-ui/src/streams.rs @@ -215,7 +215,12 @@ impl App { let current = self.current; let mut items: Vec = Vec::new(); if let Some(view) = view.filter(|v| v.streams_available()) { - for s in &view.streams { + // Every channel's streams (they can be watched from anywhere), + // ours first. + let own_channel = view.state.own_channel; + let mut streams: Vec<_> = view.streams.iter().collect(); + streams.sort_by_key(|s| view.channel_of(s.streamer.0) != own_channel); + for s in streams { let watching = self .watch .as_ref() @@ -224,6 +229,7 @@ impl App { id: s.id.clone().into(), name: s.name.clone().into(), streamer: view.nickname(s.streamer.0).into(), + channel: view.other_channel_name(s.streamer.0).into(), streamer_id: i32::from(s.streamer.0), viewers: view.stream_viewers.get(&s.id).map_or(0, |v| *v as i32), audio: s.audio, @@ -239,20 +245,17 @@ impl App { bitrate: bitrate_text(s.bitrate).into(), }); } - // Streams that started before we joined: the server did not - // announce them, and the engine is looking them up (they arrive - // in `StreamsChanged` like the others). Until then only the - // streaming flag is known. - let channel = view.state.own_channel; + // Streams that started before we joined, or in another channel: + // the server did not announce them, and the engine is looking + // them up (they arrive in `StreamsChanged` like the others). + // Until then only the streaming flag is known. for c in view.presence.clients.values() { let announced = view.streams.iter().any(|s| s.streamer.0 == c.id); - if c.streaming == Some(true) - && Some(c.channel) == channel - && !announced && view.state.own_client != Some(c.id) - { + if c.streaming == Some(true) && !announced && view.state.own_client != Some(c.id) { items.push(StreamItem { name: c.nickname.clone().into(), streamer: c.nickname.clone().into(), + channel: view.other_channel_name(c.id).into(), streamer_id: i32::from(c.id), ..StreamItem::default() }); @@ -346,6 +349,11 @@ impl App { .and_then(|v| v.streams.iter().find(|s| s.id == watch.stream_id)) .map_or(-1, |s| i32::from(s.streamer.0)); bridge.set_viewer_streamer_id(streamer); + let channel = u16::try_from(streamer).ok().and_then(|id| { + let view = self.view()?; + view.presence.channels.get(&view.channel_of(id)?).map(|c| c.name.clone()) + }); + bridge.set_viewer_channel(channel.unwrap_or_default().into()); bridge.set_viewer_elapsed(watch.elapsed().into()); let viewers = self.view().and_then(|v| v.stream_viewers.get(&watch.stream_id)).copied(); bridge.set_viewer_count(viewers.unwrap_or(0) as i32); diff --git a/crates/voelin-ui/ui/bridge.slint b/crates/voelin-ui/ui/bridge.slint index 08db46d..df004e5 100644 --- a/crates/voelin-ui/ui/bridge.slint +++ b/crates/voelin-ui/ui/bridge.slint @@ -217,12 +217,15 @@ export struct BookmarkForm { query-password: string, } -// A stream in our channel. An empty id: a client that streams, but whose -// stream is still being looked up (it started before we joined). +// A stream on the server, ours first: a stream can be watched from any +// channel. An empty id: a client that streams, but whose stream is still +// being looked up (it started before we joined, or in another channel). export struct StreamItem { id: string, name: string, streamer: string, + // The streamer's channel when it is not ours; empty in ours. + channel: string, // The streamer's client id (their avatar in the members). streamer-id: int, // How many watch it (the gateway's directory; 0 unknown). @@ -384,6 +387,8 @@ export global Bridge { in property viewer-title; // Who streams it (their client id: -1 unknown). in property viewer-streamer; + // The voice channel the watched stream is in. + in property viewer-channel; in property viewer-streamer-id: -1; // Shown instead of (or over) the picture: waiting, ended, errors. in property viewer-status; diff --git a/crates/voelin-ui/ui/screens/streams.slint b/crates/voelin-ui/ui/screens/streams.slint index 799274c..c1adc42 100644 --- a/crates/voelin-ui/ui/screens/streams.slint +++ b/crates/voelin-ui/ui/screens/streams.slint @@ -50,7 +50,10 @@ component StreamCard inherits Rectangle { overflow: elide; } Text { - text: stream.id == "" ? @tr("Started before you joined; looking it up…") : stream.own ? @tr("your stream") : stream.audio ? @tr("by {} · with sound", stream.streamer) : @tr("by {}", stream.streamer); + text: stream.id == "" ? (stream.channel == "" ? @tr("Started before you joined; looking it up…") : @tr("In {}; looking it up…", stream.channel)) + : stream.own ? @tr("your stream") + : stream.channel != "" ? @tr("by {} · in {}", stream.streamer, stream.channel) + : stream.audio ? @tr("by {} · with sound", stream.streamer) : @tr("by {}", stream.streamer); color: Theme.text-muted; font-size: Theme.font-sm; overflow: elide; @@ -150,7 +153,7 @@ export component StreamsPanel inherits VerticalLayout { } } if Bridge.streams.length == 0: Text { - text: @tr("No streams in your channel."); + text: @tr("No streams on this server."); color: Theme.text-muted; font-size: Theme.font-sm; } @@ -425,7 +428,7 @@ component StreamInfo inherits VerticalLayout { } // The stream we watch. With the shell: the channel's header, the player, -// a note that the stream belongs to the channel, and its chat below. In +// a note on the channel the stream belongs to, and its chat below. In // full screen and on a phone: the player alone. export component StreamViewer inherits Rectangle { // Reading our own width here would loop through the layout, so the @@ -465,8 +468,9 @@ export component StreamViewer inherits Rectangle { padding-bottom: Theme.space-3; StreamPlayer { } } - // Streams on TeamSpeak belong to the channel. - if Bridge.own-channel != "": HorizontalLayout { + // Streams on TeamSpeak belong to a channel (and can be watched from + // any channel; the streamer decides who may). + if Bridge.viewer-channel != "": HorizontalLayout { padding-left: Theme.space-4; padding-right: Theme.space-4; Rectangle { @@ -486,7 +490,7 @@ export component StreamViewer inherits Rectangle { y: (parent.height - self.height) / 2; } Text { - text: @tr("This stream is only available while you're in the {} voice channel.", Bridge.own-channel); + text: Bridge.viewer-channel == Bridge.own-channel ? @tr("Streamed in your voice channel, {}. The streamer decides who may watch.", Bridge.viewer-channel) : @tr("Streamed in the {} voice channel. The streamer decides who may watch.", Bridge.viewer-channel); color: Theme.text-secondary; font-size: Theme.font-sm; vertical-alignment: center; diff --git a/docs/research/ts6-late-join.md b/docs/research/ts6-late-join.md index 483d62e..15588fc 100644 --- a/docs/research/ts6-late-join.md +++ b/docs/research/ts6-late-join.md @@ -174,9 +174,12 @@ for A. So metadata would work as a fallback between Voelin clients, but optional `id` for the empty answer) and `notifystreamupdated`. - `voelin_stream::discovery::Discovery` follows the clients on the server: their channel and `client_is_streaming`. It keeps the stream directory to - the streams in our own channel. It looks up a client that streams in our - channel and whose stream we were not told: + the streams of every client on the server that streams, in any channel + (a stream can be watched from another channel, see below). It looks up a + client that streams and whose stream we were not told: - after connecting; + - when a client in another channel starts streaming (the server announces + a stream only in its channel); - after we or the streamer change channels; - when a streamer in our channel starts and its `notifystreamstarted` has not arrived by the next update of the client list. @@ -194,6 +197,17 @@ for A. So metadata would work as a fallback between Voelin clients, but - The engine (`voelin-core`) and `voelinctl stream list/watch` feed the client list into `Streams::update_clients`. +Watching from another channel (confirmed, 6.0.0-beta13.1, 2026-10-03): with +B in the default channel and A's stream in another one, `requeststreaminfo +clid=` answers as above and `joinstreamrequest` reaches A +(`notifyjoinstreamrequest`), the probe above shows it. The whole join with +media works too: `scripts/it-smoke.sh`, step "late stream", has a viewer in +the default channel watch a stream in `smoke-elsewhere`, and the app (no +gateway) listed and played a stream in another channel. Voelin lists the +streams of every channel, ours first; as a streamer with `stream.permissions += channel` (the default) it lets its channel in and asks the user about +viewers from other channels. + Fallbacks (i) and (ii) from the plan were not built, because the native command works. (i) would have been a re-announcement by the streamer; (ii) would have put the stream id into `client_meta_data`. The probe shows what diff --git a/scripts/it-smoke.sh b/scripts/it-smoke.sh index 2fde60b..2bd82af 100755 --- a/scripts/it-smoke.sh +++ b/scripts/it-smoke.sh @@ -22,7 +22,8 @@ # later finds it there and watches it # 11. stream (TeamSpeak 6 only): a viewer watches a synthetic VP8 + Opus stream # 12. late stream (TeamSpeak 6 only): a viewer that connects after the stream -# started finds it (requeststreaminfo) and watches it +# started finds it (requeststreaminfo) and watches it; a viewer in the +# default channel finds and watches a stream in another channel # 13. the engine's voice features (voelinctl engine): upload a file and # post its link in chat, another client lists the channel's files and # downloads the linked file (compared byte for byte), the file is @@ -171,6 +172,31 @@ stream_late_check() { grep -E "watching|received" "$out" } +# TeamSpeak 6: the stream runs in another channel. The server announces a +# stream only in its channel; a viewer elsewhere looks it up and watches it +# from where it is, as the official client can. +stream_elsewhere_check() { + local addr=$1 admin=$2 out="$STATE_DIR/stream-elsewhere-watch.log" + local streamer_out="$STATE_DIR/stream-elsewhere-start.log" streamer="far-$$-$RANDOM" + # Semi-permanent, so it outlives this command; it already exists after the first run. + # shellcheck disable=SC2086 + "$VOELINCTL" connect "$addr" $admin --nick admin raw \ + "channelcreate channel_name=smoke-elsewhere channel_flag_semi_permanent=1" >/dev/null 2>&1 || true + "$VOELINCTL" connect "$addr" --nick "$streamer" --channel smoke-elsewhere stream --loopback start \ + --synthetic --auto-accept --seconds 30 >"$streamer_out" 2>&1 & + local streamer_pid=$! + for _ in $(seq 1 60); do grep -q "is live" "$streamer_out" 2>/dev/null && break; sleep 0.25; done + grep -q "is live" "$streamer_out" || { cat "$streamer_out"; fail "streamer in another channel did not go live"; } + if ! "$VOELINCTL" connect "$addr" --nick "elsewhere-$$" stream --loopback watch --streamer-nick "$streamer" \ + --expect-frames 60 --timeout 25 >"$out" 2>&1; then + cat "$out" "$streamer_out" + fail "viewer in the default channel did not receive the stream in another channel" + fi + kill -INT "$streamer_pid" 2>/dev/null || true + wait "$streamer_pid" 2>/dev/null || true + grep -E "watching|received" "$out" +} + # An observer over ServerQuery must see a voice client join. presence_check() { local addr=$1 query=$2 out="$STATE_DIR/observe.log" @@ -488,6 +514,7 @@ for svc in "${SERVERS[@]}"; do if [[ $svc == ts6 ]]; then stream_check "$addr" stream_late_check "$addr" + stream_elsewhere_check "$addr" "$admin" fi done