mirror of
https://github.com/nestriness/nestri.git
synced 2026-11-29 18:46:13 +02:00
feat(neshub): per-window pause and resume of capture and encode (#448)
Implements the box side of decision 0087 (items 1 and 2): a per-window
Pause / Resume.
## Plan
A correction to the brief: `nesprotocol` lives in this repo (nestri),
not in nescore. nescore and nesktop consume it by git rev. So the
protocol and the box side land together here, then nescore gets the
client call and a pin move, then nesktop gets the probe scenario and its
pin move. Also, the video encoder is `nescapture` (a Vulkan layer in the
game's process), not `neswire` (audio), and the compositor `nescope`
needs no change: capture happens in the game's present hook.
**Messages** (control stream, client to hub, empty payload, like
`MSG_IDR_REQUEST`):
- `MSG_WINDOW_PAUSE = 0x18`
- `MSG_WINDOW_RESUME = 0x19`
**Window id flow.** No id in the message. The hub already knows which
window a client is subscribed to (`MSG_WINDOW_SUBSCRIBE`) and the pid of
that window's encoder (`subscribe.2`, the same one keyframe requests
use). So:
1. client sends pause/resume on the control stream;
2. `neshub` `run_control_reader` turns it into `MSG_WINDOW_PAUSE/RESUME`
targeted at that encoder's own command socket
(`capture_cmd::window_pause/resume`, via `to_encoder`). Targeted
commands are not recorded in the replay history, so a later encoder is
not born paused;
3. `nescapture`'s command listener sets a `paused` flag; `try_capture`
in the present hook returns early while it is set (before the frame
gate, before any blit, slot or encode). The game's process is untouched;
4. resume clears the flag and sets `idr_requested`, so the first frame
after it is a keyframe. The hub also sets `awaiting_keyframe` for that
client, as for an IDR request.
A pause from a client whose encoder the hub does not know yet is
ignored, because "all encoders" is the wrong target for a pause.
Old hub, new client: logs an unknown control message. Old nescapture
ignores the unknown byte.
## Tested
- `cargo test -p nesprotocol -p neshub`: 122 + 79 pass (new: type bytes
distinct, including 0x17 subscribe; pause/resume target one encoder and
are not replayed).
- `cargo test -p nescapture`: 162 pass (new: pause stops, resume
requests a keyframe). Built on nestripc-1 with `SLANGC` pointed at a
copy in a container layer, because the box has no slangc.
## Not verified
- No real game or GPU run. That the present hook really stops blitting
while paused, and that a keyframe lands within 1 s of resume, is proven
only by unit tests here; the nesprobe `--pause` scenario in the nesktop
PR is what checks it end to end, and it has not been run against a box.
- A game that presents nothing after resume (a fully still scene with no
presents) would delay the keyframe until its next present, same as any
existing IDR request.
## Update after review (78a8bc8)
- A pause is per session. `neshub/src/pauses.rs` keeps the set of
sessions pausing each encoder; the encoder is paused while the set is
non-empty. Resume goes only to the pid that session paused and never
broadcasts (a resume from a session that paused nothing starts nothing).
- A session releases its pause on drop (`ClientSession::drop`), on
resubscribe, and when the primary picture moves (`restart_picture`).
Keyed by the never-reused gamepad session number, so a reconnecting
client cannot release or inherit another session's pause.
- Tests (neshub 86 pass): two sessions on one encoder; session that
never paused resumes nothing; drop while paused (unit and through
`remove_session` on the e2e rig); resubscribe while paused releases the
old encoder only; pausing another encoder moves the pause.
This commit is contained in:
@@ -55,7 +55,8 @@ use std::time::Instant;
|
||||
|
||||
use nesprotocol::{
|
||||
CODEC_AV1, CODEC_H264, CODEC_H265, CODEC_KEEP, CODEC_PYROWAVE, ClientCaps, FLAG_KEYFRAME,
|
||||
FLAG_RECONFIG, MSG_CLIENT_CAPS, MSG_ENCODE_SETTINGS, MSG_IDR_REQUEST, STREAM_VIDEO,
|
||||
FLAG_RECONFIG, MSG_CLIENT_CAPS, MSG_ENCODE_SETTINGS, MSG_IDR_REQUEST, MSG_WINDOW_PAUSE,
|
||||
MSG_WINDOW_RESUME, STREAM_VIDEO,
|
||||
decode_client_caps, decode_encode_settings_ext, encode_ipc_frame_from,
|
||||
};
|
||||
use pixelforge::{
|
||||
@@ -998,6 +999,10 @@ pub struct PipelineHandle {
|
||||
stall_probe: StallProbe,
|
||||
progress_epoch: Instant,
|
||||
idr_requested: Arc<AtomicBool>,
|
||||
/// Set by a pause from the hub (ref d-0087): the present hook captures
|
||||
/// nothing while it is, so nothing is blitted, encoded or sent. The game
|
||||
/// is not touched.
|
||||
paused: Arc<AtomicBool>,
|
||||
shutdown: Arc<AtomicBool>,
|
||||
pub capture_fps: Arc<AtomicU32>,
|
||||
pub encode_avg_ms: Arc<AtomicU32>,
|
||||
@@ -1089,6 +1094,8 @@ impl PipelineHandle {
|
||||
shutdown.clone(),
|
||||
);
|
||||
let idr_requested = Arc::new(AtomicBool::new(false));
|
||||
let paused = Arc::new(AtomicBool::new(false));
|
||||
let paused_listener = paused.clone();
|
||||
let capture_fps = Arc::new(AtomicU32::new(0));
|
||||
let encode_avg_ms = Arc::new(AtomicU32::new(0));
|
||||
let capture_ms = Arc::new(AtomicU32::new(0));
|
||||
@@ -1229,6 +1236,9 @@ impl PipelineHandle {
|
||||
let mut buf = [0u8; 128];
|
||||
loop {
|
||||
match sock.recv(&mut buf) {
|
||||
Ok(1) if buf[0] == MSG_WINDOW_PAUSE || buf[0] == MSG_WINDOW_RESUME => {
|
||||
apply_pause_message(buf[0], &paused_listener, &idr_thread);
|
||||
}
|
||||
Ok(1) if buf[0] == MSG_IDR_REQUEST => {
|
||||
log::info!("IDR requested by client");
|
||||
idr_thread.store(true, Ordering::Relaxed);
|
||||
@@ -1394,6 +1404,7 @@ impl PipelineHandle {
|
||||
stall_probe,
|
||||
progress_epoch,
|
||||
idr_requested,
|
||||
paused,
|
||||
shutdown,
|
||||
capture_fps,
|
||||
encode_avg_ms,
|
||||
@@ -1468,6 +1479,17 @@ impl PipelineHandle {
|
||||
true
|
||||
}
|
||||
|
||||
/// Whether the window is paused: the hook captures nothing.
|
||||
pub fn is_paused(&self) -> bool {
|
||||
self.paused.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Say the capture thread is alive while paused, so the stall watchdog
|
||||
/// does not report a stopped capture that was stopped on purpose.
|
||||
pub fn note_paused(&self) {
|
||||
self.capture_progress.note(self.progress_epoch, 7);
|
||||
}
|
||||
|
||||
pub fn request_idr(&self) {
|
||||
self.idr_requested.store(true, Ordering::Relaxed);
|
||||
}
|
||||
@@ -1524,6 +1546,45 @@ pub struct EncodeSettingsChange {
|
||||
pub pyro: Option<PyroSettings>,
|
||||
}
|
||||
|
||||
/// Apply a pause or resume from the hub. Resume clears the flag and asks for a
|
||||
/// keyframe in the same step, so the first frame after it is one the client
|
||||
/// can decode: everything it saw before the pause is stale.
|
||||
fn apply_pause_message(msg: u8, paused: &AtomicBool, idr_requested: &AtomicBool) {
|
||||
if msg == MSG_WINDOW_PAUSE {
|
||||
if !paused.swap(true, Ordering::Relaxed) {
|
||||
log::info!("paused by the client: capture and encode stopped");
|
||||
}
|
||||
} else if msg == MSG_WINDOW_RESUME {
|
||||
// The keyframe first: the flag lets the next present through, and the
|
||||
// encoder must already know that frame is to be an IDR.
|
||||
idr_requested.store(true, Ordering::Relaxed);
|
||||
if paused.swap(false, Ordering::Relaxed) {
|
||||
log::info!("resumed by the client: keyframe requested");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod pause_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn pause_stops_and_resume_starts_with_a_keyframe() {
|
||||
let paused = AtomicBool::new(false);
|
||||
let idr = AtomicBool::new(false);
|
||||
apply_pause_message(MSG_WINDOW_PAUSE, &paused, &idr);
|
||||
assert!(paused.load(Ordering::Relaxed));
|
||||
assert!(!idr.load(Ordering::Relaxed), "a pause asks for nothing");
|
||||
apply_pause_message(MSG_WINDOW_RESUME, &paused, &idr);
|
||||
assert!(!paused.load(Ordering::Relaxed));
|
||||
assert!(idr.load(Ordering::Relaxed));
|
||||
// A resume that was never paused is still a keyframe request, harmless.
|
||||
idr.store(false, Ordering::Relaxed);
|
||||
apply_pause_message(MSG_WINDOW_RESUME, &paused, &idr);
|
||||
assert!(idr.load(Ordering::Relaxed));
|
||||
}
|
||||
}
|
||||
|
||||
fn encoder_thread(
|
||||
mut cfg: EncoderConfig,
|
||||
frame_rx: mpsc::Receiver<CapturedFrame>,
|
||||
|
||||
@@ -213,6 +213,17 @@ unsafe fn try_capture(
|
||||
(images[image_index], fmt, ext, images.len())
|
||||
};
|
||||
|
||||
// A paused window captures nothing (ref d-0087): no blit, no slot, no
|
||||
// encode. Before the gate, so the gate's own bookkeeping does not run
|
||||
// either, and the frame the client resumes on is a fresh one.
|
||||
if let Ok(enc) = ds.encoder.lock()
|
||||
&& let Some(ref h) = *enc
|
||||
&& h.is_paused()
|
||||
{
|
||||
h.note_paused();
|
||||
return None;
|
||||
}
|
||||
|
||||
// Gate before any GPU work is queued. A game presenting faster than the
|
||||
// target would otherwise pay a full blit for frames the
|
||||
// encoder throws away moments later.
|
||||
|
||||
@@ -44,6 +44,17 @@ pub fn keyframe_request(pid: Option<u32>) -> Vec<u8> {
|
||||
to_encoder(pid, &[MSG_IDR_REQUEST])
|
||||
}
|
||||
|
||||
/// Stop `pid`'s encoder capturing and encoding; it keeps its game running.
|
||||
/// Never replayed to a later encoder: it is about one window, not a setting.
|
||||
pub fn window_pause(pid: Option<u32>) -> Vec<u8> {
|
||||
to_encoder(pid, &[nesprotocol::MSG_WINDOW_PAUSE])
|
||||
}
|
||||
|
||||
/// Start `pid`'s encoder again; it answers with a keyframe.
|
||||
pub fn window_resume(pid: Option<u32>) -> Vec<u8> {
|
||||
to_encoder(pid, &[nesprotocol::MSG_WINDOW_RESUME])
|
||||
}
|
||||
|
||||
/// Split a queued command into its target and the command itself.
|
||||
pub fn target_of(cmd: Vec<u8>) -> (Option<u32>, Vec<u8>) {
|
||||
if cmd.len() >= 5 && cmd[0] == TO_ENCODER {
|
||||
@@ -221,6 +232,25 @@ mod tests {
|
||||
assert_eq!(f.encoder(11), Some(vec![vec![MSG_IDR_REQUEST]]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pause_and_resume_go_to_one_encoder_and_are_not_replayed() {
|
||||
let mut f = Fanout::default();
|
||||
f.encoder(10);
|
||||
f.encoder(20);
|
||||
let (target, cmd) = target_of(window_pause(Some(20)));
|
||||
assert_eq!(cmd, vec![nesprotocol::MSG_WINDOW_PAUSE]);
|
||||
assert_eq!(
|
||||
f.destinations_for(target),
|
||||
vec![PathBuf::from("/tmp/nescapture-cmd-20.sock")]
|
||||
);
|
||||
let (target, cmd) = target_of(window_resume(Some(20)));
|
||||
assert_eq!(cmd, vec![nesprotocol::MSG_WINDOW_RESUME]);
|
||||
assert_eq!(target, Some(20));
|
||||
// Targeted commands are not recorded, so a later encoder is not
|
||||
// born paused.
|
||||
assert_eq!(f.encoder(30).map(|r| r.len()), Some(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pid_from_path() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -577,4 +577,39 @@ mod windows {
|
||||
assert_eq!(got.len(), 3);
|
||||
assert!(got.iter().all(|g| (g.0, g.1) == (0, 0)));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_session_that_goes_while_paused_releases_its_pause() {
|
||||
let rig = rig().await;
|
||||
tell(&rig, |m| encode_event_message(m, &opened(0, "launch_a"))).await;
|
||||
tell(&rig, |m| encode_window_pid(m, 0, GAME_A)).await;
|
||||
draw(&rig, GAME_A, 0, true).await;
|
||||
let _ = received(&rig.client).await;
|
||||
let me = rig._client_ep.addr().id;
|
||||
let handle = rig.mgr.pause_handle_of(&me).await.expect("a session");
|
||||
handle.pause(GAME_A);
|
||||
assert!(rig.mgr.encoder_paused(GAME_A));
|
||||
let controller = tokio::sync::Mutex::new(Controller::new(Limits::new(4_000)));
|
||||
let (cmd, _rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
rig.mgr.remove_session(&me, &controller, &cmd).await;
|
||||
assert!(!rig.mgr.encoder_paused(GAME_A), "the pause went with the session");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_resubscribe_while_paused_releases_the_old_encoder() {
|
||||
let rig = rig().await;
|
||||
tell(&rig, |m| encode_event_message(m, &opened(0, "launch_a"))).await;
|
||||
tell(&rig, |m| encode_event_message(m, &opened(1, "launch_b"))).await;
|
||||
tell(&rig, |m| encode_window_pid(m, 0, GAME_A)).await;
|
||||
tell(&rig, |m| encode_window_pid(m, 1, GAME_B)).await;
|
||||
draw(&rig, GAME_A, 0, true).await;
|
||||
let _ = received(&rig.client).await;
|
||||
let me = rig._client_ep.addr().id;
|
||||
let handle = rig.mgr.pause_handle_of(&me).await.expect("a session");
|
||||
handle.pause(GAME_A);
|
||||
assert!(rig.mgr.encoder_paused(GAME_A));
|
||||
let (idr, _rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
rig.mgr.subscribe(me, 1, &idr).await;
|
||||
assert!(!rig.mgr.encoder_paused(GAME_A), "no longer shown, no longer held");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ mod session;
|
||||
mod ticket;
|
||||
mod windows;
|
||||
mod capture_cmd;
|
||||
mod pauses;
|
||||
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
//! Who has paused which encoder, ref(d-0087).
|
||||
//!
|
||||
//! A pause is a client's, not an encoder's: two clients can be shown the same
|
||||
//! window, and one pausing it must not freeze the other's picture. So the hub
|
||||
//! keeps, for each encoder, the set of sessions pausing it, and the encoder is
|
||||
//! stopped only while that set is not empty. A session leaving (its
|
||||
//! connection ending, a resubscribe, the primary picture moving) releases its
|
||||
//! pause, so a pause can never outlive the client that asked for it.
|
||||
//!
|
||||
//! Keyed by the session's gamepad number, which is never reused within a hub's
|
||||
//! lifetime, so a client that reconnects is a new session and cannot release
|
||||
//! the pause of the one it replaced, or the other way round.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
|
||||
use crate::capture_cmd::{window_pause, window_resume};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PauseBook {
|
||||
/// The encoder each pausing session paused. One at a time per session.
|
||||
paused_by: BTreeMap<u32, u32>,
|
||||
/// Sessions that have gone. Set under the same lock that records pauses,
|
||||
/// so a pause that a still-running reader makes after its session was
|
||||
/// dropped finds it shut and does nothing, rather than holding an encoder
|
||||
/// that nobody is left to resume.
|
||||
closed: BTreeSet<u32>,
|
||||
}
|
||||
|
||||
impl PauseBook {
|
||||
fn users(&self, pid: u32) -> BTreeSet<u32> {
|
||||
self.paused_by
|
||||
.iter()
|
||||
.filter(|(_, p)| **p == pid)
|
||||
.map(|(s, _)| *s)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `session` pauses `pid`. The commands to send, in order.
|
||||
fn pause(&mut self, session: u32, pid: u32) -> Vec<Vec<u8>> {
|
||||
let mut out = Vec::new();
|
||||
if self.closed.contains(&session) {
|
||||
return out;
|
||||
}
|
||||
if self.paused_by.get(&session) == Some(&pid) {
|
||||
return out;
|
||||
}
|
||||
// Moving from one encoder to another releases the first.
|
||||
out.extend(self.release(session));
|
||||
let first = self.users(pid).is_empty();
|
||||
self.paused_by.insert(session, pid);
|
||||
if first {
|
||||
out.push(window_pause(Some(pid)));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `session` stops pausing. Resumes the encoder it paused, and only that
|
||||
/// one, and only when no other session still pauses it.
|
||||
fn release(&mut self, session: u32) -> Vec<Vec<u8>> {
|
||||
let Some(pid) = self.paused_by.remove(&session) else {
|
||||
return Vec::new();
|
||||
};
|
||||
if self.users(pid).is_empty() {
|
||||
vec![window_resume(Some(pid))]
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// The session is gone: release what it paused and refuse any more.
|
||||
fn close(&mut self, session: u32) -> Vec<Vec<u8>> {
|
||||
self.closed.insert(session);
|
||||
self.release(session)
|
||||
}
|
||||
|
||||
pub fn paused(&self, pid: u32) -> bool {
|
||||
!self.users(pid).is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// One session's way of pausing and resuming, shared with its control reader.
|
||||
#[derive(Clone)]
|
||||
pub struct PauseHandle {
|
||||
book: Arc<Mutex<PauseBook>>,
|
||||
session: u32,
|
||||
cmd_tx: UnboundedSender<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl PauseHandle {
|
||||
pub fn new(book: Arc<Mutex<PauseBook>>, session: u32, cmd_tx: UnboundedSender<Vec<u8>>) -> Self {
|
||||
Self { book, session, cmd_tx }
|
||||
}
|
||||
|
||||
fn send(&self, cmds: Vec<Vec<u8>>) {
|
||||
for c in cmds {
|
||||
let _ = self.cmd_tx.send(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// Pause `pid` for this session. `pid` is the encoder it is shown, which
|
||||
/// is never 0 here: the caller does not pause what it cannot name.
|
||||
pub fn pause(&self, pid: u32) {
|
||||
let cmds = self.book.lock().unwrap_or_else(|e| e.into_inner()).pause(self.session, pid);
|
||||
self.send(cmds);
|
||||
}
|
||||
|
||||
/// Resume what this session paused. Never anything else, and nothing at
|
||||
/// all when it paused nothing.
|
||||
pub fn resume(&self) {
|
||||
self.release();
|
||||
}
|
||||
|
||||
/// The session is being dropped: release its pause and refuse any pause a
|
||||
/// reader that is still running makes after this.
|
||||
pub fn close(&self) {
|
||||
let cmds = self.book.lock().unwrap_or_else(|e| e.into_inner()).close(self.session);
|
||||
self.send(cmds);
|
||||
}
|
||||
|
||||
/// Let go of whatever this session paused: it is leaving, or looking at
|
||||
/// another picture.
|
||||
pub fn release(&self) {
|
||||
let cmds = self.book.lock().unwrap_or_else(|e| e.into_inner()).release(self.session);
|
||||
self.send(cmds);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn rig() -> (Arc<Mutex<PauseBook>>, UnboundedSender<Vec<u8>>, tokio::sync::mpsc::UnboundedReceiver<Vec<u8>>) {
|
||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
(Arc::new(Mutex::new(PauseBook::default())), tx, rx)
|
||||
}
|
||||
|
||||
fn sent(rx: &mut tokio::sync::mpsc::UnboundedReceiver<Vec<u8>>) -> Vec<Vec<u8>> {
|
||||
std::iter::from_fn(|| rx.try_recv().ok()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_of_two_sessions_pausing_a_shared_encoder_pauses_it_until_both_let_go() {
|
||||
let (book, tx, mut rx) = rig();
|
||||
let a = PauseHandle::new(book.clone(), 1, tx.clone());
|
||||
let b = PauseHandle::new(book.clone(), 2, tx);
|
||||
a.pause(10);
|
||||
assert_eq!(sent(&mut rx), vec![window_pause(Some(10))]);
|
||||
// B pauses too: the encoder is already stopped, nothing to send.
|
||||
b.pause(10);
|
||||
assert!(sent(&mut rx).is_empty());
|
||||
// A resumes, B still pauses: the encoder stays stopped.
|
||||
a.resume();
|
||||
assert!(sent(&mut rx).is_empty());
|
||||
assert!(book.lock().unwrap().paused(10));
|
||||
b.resume();
|
||||
assert_eq!(sent(&mut rx), vec![window_resume(Some(10))]);
|
||||
assert!(!book.lock().unwrap().paused(10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_session_that_never_paused_resumes_nothing() {
|
||||
let (book, tx, mut rx) = rig();
|
||||
let a = PauseHandle::new(book.clone(), 1, tx.clone());
|
||||
let b = PauseHandle::new(book, 2, tx);
|
||||
a.pause(10);
|
||||
sent(&mut rx);
|
||||
// B, shown the same window, did not pause it: its resume must not
|
||||
// start the encoder A stopped.
|
||||
b.resume();
|
||||
b.release();
|
||||
assert!(sent(&mut rx).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_session_that_goes_while_paused_resumes_the_encoder() {
|
||||
let (book, tx, mut rx) = rig();
|
||||
let a = PauseHandle::new(book.clone(), 1, tx);
|
||||
a.pause(10);
|
||||
sent(&mut rx);
|
||||
a.release(); // what its drop does
|
||||
assert_eq!(sent(&mut rx), vec![window_resume(Some(10))]);
|
||||
// Releasing twice is not resuming twice.
|
||||
a.release();
|
||||
assert!(sent(&mut rx).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_resubscribe_while_paused_releases_the_old_encoder_only() {
|
||||
let (book, tx, mut rx) = rig();
|
||||
let a = PauseHandle::new(book.clone(), 1, tx.clone());
|
||||
let other = PauseHandle::new(book.clone(), 2, tx);
|
||||
a.pause(10);
|
||||
other.pause(20);
|
||||
sent(&mut rx);
|
||||
a.release(); // restart_picture on moving to another window
|
||||
assert_eq!(sent(&mut rx), vec![window_resume(Some(10))]);
|
||||
assert!(book.lock().unwrap().paused(20), "someone else's pause is untouched");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pause_after_the_session_is_closed_is_a_no_op() {
|
||||
let (book, tx, mut rx) = rig();
|
||||
let a = PauseHandle::new(book.clone(), 1, tx);
|
||||
a.pause(10);
|
||||
sent(&mut rx);
|
||||
a.close();
|
||||
assert_eq!(sent(&mut rx), vec![window_resume(Some(10))]);
|
||||
// The control reader, still running on another thread, pauses again.
|
||||
a.pause(10);
|
||||
assert!(sent(&mut rx).is_empty());
|
||||
assert!(!book.lock().unwrap().paused(10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn releasing_without_closing_still_allows_a_later_pause() {
|
||||
let (book, tx, mut rx) = rig();
|
||||
let a = PauseHandle::new(book, 1, tx);
|
||||
a.pause(10);
|
||||
a.release();
|
||||
sent(&mut rx);
|
||||
a.pause(20);
|
||||
assert_eq!(sent(&mut rx), vec![window_pause(Some(20))]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pausing_another_encoder_moves_the_pause() {
|
||||
let (book, tx, mut rx) = rig();
|
||||
let a = PauseHandle::new(book, 1, tx);
|
||||
a.pause(10);
|
||||
a.pause(20);
|
||||
assert_eq!(
|
||||
sent(&mut rx),
|
||||
vec![window_pause(Some(10)), window_resume(Some(10)), window_pause(Some(20))]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use nesprotocol::{ControlMode, ReceiverReport, decode_control_mode, decode_recei
|
||||
use nesprotocol::{FRAME_HDR_LEN, STREAM_VERSION, encode_frame};
|
||||
use nesprotocol::{
|
||||
MSG_CLIENT_CAPS, MSG_CONTROL_MODE, MSG_ENCODE_SETTINGS, MSG_GAMEPAD, MSG_GAMEPAD_FEEDBACK,
|
||||
MSG_IDR_REQUEST, MSG_INPUT_BATCH, MSG_RECEIVER_REPORT,
|
||||
MSG_IDR_REQUEST, MSG_INPUT_BATCH, MSG_RECEIVER_REPORT, MSG_WINDOW_PAUSE, MSG_WINDOW_RESUME,
|
||||
};
|
||||
|
||||
use crate::control::{Controller, PathView};
|
||||
@@ -106,6 +106,9 @@ pub struct ClientSession {
|
||||
/// The encoder whose frames this client was last sent (0: none yet), so
|
||||
/// its keyframe requests go to that encoder alone.
|
||||
encoder_pid: Arc<AtomicU32>,
|
||||
/// This session's pause of an encoder, if any (d-0087); released when it
|
||||
/// goes or looks at another picture.
|
||||
pause: crate::pauses::PauseHandle,
|
||||
primary_window: Arc<AtomicU32>,
|
||||
/// Where a subscribe request goes: the manager, which holds the windows.
|
||||
subscribe_tx: tokio::sync::mpsc::UnboundedSender<(iroh::EndpointId, WindowId)>,
|
||||
@@ -150,6 +153,8 @@ impl Drop for ClientSession {
|
||||
for task in &self.tasks {
|
||||
task.abort();
|
||||
}
|
||||
// A pause is this client's, and does not outlive it.
|
||||
self.pause.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,6 +174,7 @@ impl ClientSession {
|
||||
pyrowave_kbps: u32,
|
||||
client: iroh::EndpointId,
|
||||
negotiator: Arc<Negotiator>,
|
||||
pauses: Arc<std::sync::Mutex<crate::pauses::PauseBook>>,
|
||||
) -> Self {
|
||||
let (gamepad_feedback_tx, gamepad_feedback_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let (video_tx, video_rx) = tokio::sync::mpsc::unbounded_channel::<MediaItem>();
|
||||
@@ -176,7 +182,9 @@ impl ClientSession {
|
||||
let (cursor_tx, cursor_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let (stats_tx, stats_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let (windows_tx, windows_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let pause = crate::pauses::PauseHandle::new(pauses, gamepad_session, idr_cmd_tx.clone());
|
||||
Self {
|
||||
pause,
|
||||
subscription: Arc::new(std::sync::Mutex::new(Subscription::Legacy)),
|
||||
encoder_pid: Arc::new(AtomicU32::new(encoder_pid)),
|
||||
primary_window,
|
||||
@@ -349,6 +357,7 @@ impl ClientSession {
|
||||
client: self.client,
|
||||
negotiator: self.negotiator.clone(),
|
||||
};
|
||||
let pause = self.pause.clone();
|
||||
let subscribe = (
|
||||
self.client,
|
||||
self.subscribe_tx.clone(),
|
||||
@@ -356,7 +365,7 @@ impl ClientSession {
|
||||
);
|
||||
self.tasks.push(tokio::spawn(async move {
|
||||
run_control_reader(
|
||||
conn, idr, reports, controller, awaiting, pyrowave, caps, subscribe,
|
||||
conn, idr, reports, controller, awaiting, pyrowave, caps, subscribe, pause,
|
||||
)
|
||||
.await
|
||||
}));
|
||||
@@ -513,6 +522,9 @@ impl ClientSession {
|
||||
/// zero, hold deltas until a keyframe, and forget what it reported about
|
||||
/// the old one. The caller asks for the keyframe.
|
||||
pub fn restart_picture(&self) {
|
||||
// Another picture: whatever this client paused is no longer the one
|
||||
// it is shown, so it stops holding it.
|
||||
self.pause.release();
|
||||
self.awaiting_keyframe.store(true, Ordering::Relaxed);
|
||||
if self.pending_video.is_none() {
|
||||
let _ = self.send_video.send(MediaItem::Resubscribe);
|
||||
@@ -812,6 +824,7 @@ async fn run_control_reader(
|
||||
tokio::sync::mpsc::UnboundedSender<(iroh::EndpointId, WindowId)>,
|
||||
Arc<AtomicU32>,
|
||||
),
|
||||
pause: crate::pauses::PauseHandle,
|
||||
) {
|
||||
run_framed_reader(conn, BIDI_CONTROL, "control", None, |msg_type, payload| {
|
||||
let idr_cmd_tx = idr_cmd_tx.clone();
|
||||
@@ -821,6 +834,7 @@ async fn run_control_reader(
|
||||
let pyrowave = pyrowave.clone();
|
||||
let caps_sink = caps_sink.clone();
|
||||
let subscribe = subscribe.clone();
|
||||
let pause = pause.clone();
|
||||
async move {
|
||||
match msg_type {
|
||||
MSG_WINDOW_SUBSCRIBE => match nesprotocol::window::decode_subscribe(&payload) {
|
||||
@@ -843,6 +857,28 @@ async fn run_control_reader(
|
||||
// ref(d-0085): the other windows are fine.
|
||||
let _ = idr_cmd_tx.send(keyframe_request(pid_or_all(&subscribe.2)));
|
||||
}
|
||||
MSG_WINDOW_PAUSE => {
|
||||
// This session's pause of the encoder it is shown, and of
|
||||
// nothing else. Until it has been sent a frame the hub
|
||||
// does not know that encoder, and there is nothing to pause.
|
||||
match pid_or_all(&subscribe.2) {
|
||||
Some(pid) => {
|
||||
info!("client pauses encoder {pid}");
|
||||
pause.pause(pid);
|
||||
}
|
||||
None => debug!("pause from a client with no encoder yet; ignored"),
|
||||
}
|
||||
}
|
||||
MSG_WINDOW_RESUME => {
|
||||
// Only what this session paused. A resume from one that
|
||||
// paused nothing starts nothing; the keyframe is asked for
|
||||
// either way, since the client restarted its picture.
|
||||
awaiting_keyframe.store(true, Ordering::Relaxed);
|
||||
pause.resume();
|
||||
if let Some(pid) = pid_or_all(&subscribe.2) {
|
||||
let _ = idr_cmd_tx.send(keyframe_request(Some(pid)));
|
||||
}
|
||||
}
|
||||
MSG_CLIENT_CAPS => {
|
||||
// Passed through untouched, and in particular *without*
|
||||
// taking the controller off automatic the way encode
|
||||
@@ -1208,6 +1244,8 @@ pub struct SessionManager {
|
||||
std::sync::Mutex<Option<tokio::sync::mpsc::UnboundedReceiver<(iroh::EndpointId, WindowId)>>>,
|
||||
/// The encoder of the primary picture (0: untagged or none yet).
|
||||
primary_pid: Arc<AtomicU32>,
|
||||
/// Which sessions have paused which encoders, ref(d-0087).
|
||||
pauses: Arc<std::sync::Mutex<crate::pauses::PauseBook>>,
|
||||
/// Where commands for the encoders go; set by `serve_subscriptions`.
|
||||
cmd_tx: std::sync::OnceLock<tokio::sync::mpsc::UnboundedSender<Vec<u8>>>,
|
||||
}
|
||||
@@ -1239,9 +1277,24 @@ impl SessionManager {
|
||||
relay_ms: Arc::new(AtomicU32::new(0)),
|
||||
pyrowave_kbps,
|
||||
negotiator: Arc::new(Negotiator::new()),
|
||||
pauses: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A session's pause handle, for tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn pause_handle_of(
|
||||
&self,
|
||||
id: &iroh::EndpointId,
|
||||
) -> Option<crate::pauses::PauseHandle> {
|
||||
self.sessions.lock().await.get(id).map(|s| s.pause.clone())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn encoder_paused(&self, pid: u32) -> bool {
|
||||
self.pauses.lock().unwrap().paused(pid)
|
||||
}
|
||||
|
||||
/// Take on one of a client's connections, creating the session if this is
|
||||
/// the first to arrive.
|
||||
///
|
||||
@@ -1273,6 +1326,7 @@ impl SessionManager {
|
||||
self.pyrowave_kbps,
|
||||
id,
|
||||
self.negotiator.clone(),
|
||||
self.pauses.clone(),
|
||||
)
|
||||
});
|
||||
session.attach(carrier, conn);
|
||||
|
||||
@@ -103,6 +103,15 @@ pub const MSG_IDR_REQUEST: u8 = 0x10; // request a keyframe (desktop → hub →
|
||||
pub const MSG_ENCODE_SETTINGS: u8 = 0x12; // change encoder settings (desktop → hub → nescapture)
|
||||
pub const MSG_CLIENT_CAPS: u8 = 0x15; // what the client can decode (desktop → hub → nescapture)
|
||||
pub const MSG_SURFACE_COLOR: u8 = 0x16; // what the compositor was told a surface is (nescope → nescapture)
|
||||
/// Stop capturing and encoding the window this client is shown (desktop → hub
|
||||
/// → that window's nescapture), ref(d-0087). The game keeps running; nothing is
|
||||
/// sent for the window until [`MSG_WINDOW_RESUME`]. No payload: the hub already
|
||||
/// knows which window the client is subscribed to, as it does for
|
||||
/// [`MSG_IDR_REQUEST`]. An old hub logs it as an unknown control message.
|
||||
pub const MSG_WINDOW_PAUSE: u8 = 0x18;
|
||||
/// Start again, with a keyframe within a second (desktop → hub → nescapture).
|
||||
/// Resume is also what forces the keyframe, so the client need not ask for one.
|
||||
pub const MSG_WINDOW_RESUME: u8 = 0x19;
|
||||
/// What the receiver actually got, once a second (desktop → hub).
|
||||
///
|
||||
/// The hub cannot see this. Its own view of the path -- RTT, congestion window,
|
||||
@@ -1009,6 +1018,9 @@ mod media_control_tests {
|
||||
("MSG_ENCODE_SETTINGS", MSG_ENCODE_SETTINGS),
|
||||
("MSG_CLIENT_CAPS", MSG_CLIENT_CAPS),
|
||||
("MSG_SURFACE_COLOR", MSG_SURFACE_COLOR),
|
||||
("MSG_WINDOW_PAUSE", MSG_WINDOW_PAUSE),
|
||||
("MSG_WINDOW_RESUME", MSG_WINDOW_RESUME),
|
||||
("MSG_WINDOW_SUBSCRIBE", window::MSG_WINDOW_SUBSCRIBE),
|
||||
("MSG_RECEIVER_REPORT", MSG_RECEIVER_REPORT),
|
||||
("MSG_CONTROL_MODE", MSG_CONTROL_MODE),
|
||||
("MSG_INPUT_BATCH", MSG_INPUT_BATCH),
|
||||
|
||||
Reference in New Issue
Block a user