Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion quest/m1/explicit-state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ which collapses that state and waits on [Drop set](/quest/m1/drop-set.md).

## Required

- [moq-boy pause state](/quest/m1/explicit-state/boy-pause.md) - a new viewer always wakes a paused emulator
- [Publish capture phases](/quest/m1/explicit-state/publish-capture.md) - dismissing the screen-share picker turns sharing off
- [js/qmux close](/quest/m1/explicit-state/qmux-close.md) - a graceful remote close ends qmux streams cleanly
- [FFI handle state](/quest/m1/explicit-state/ffi-state.md) - FFI handles hold one state each: video follow under one lock, requests answered once, drivers Cancelled after shutdown
Expand Down
34 changes: 0 additions & 34 deletions quest/m1/explicit-state/boy-pause.md

This file was deleted.

193 changes: 124 additions & 69 deletions rs/moq-boy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
//!
//! Pause/resume state machine:
//! ```text
//! video_active ─┐
//! ├─ both false → paused (emulation stops, condvar blocks)
//! audio_active ─┘
//! either true → resumed (condvar notified)
//! Wanted { video, audio, video_closed, audio_closed } under the condvar's mutex
//! neither wanted and some track still open → paused (emulation stops, condvar blocks)
//! anything wanted or both closed → resumed (condvar notified)
//!
//! On resume → force video keyframe, reanchor audio
//! ```
Expand All @@ -29,7 +28,6 @@ use bytes::Bytes;
use kio::clock::Instant;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

Expand Down Expand Up @@ -151,19 +149,66 @@ struct Session {
video_track: moq_net::track::Demand,
audio_track: moq_net::track::Demand,

/// Whether anyone is subscribed to the video/audio tracks.
video_active: AtomicBool,
audio_active: AtomicBool,

/// True when no viewers are watching (both tracks unused).
paused: AtomicBool,
/// Condvar to wake the emulator thread on resume.
resume: (Mutex<()>, Condvar),
/// Which tracks viewers want, guarded by the mutex the emulator waits on.
pause: Pause,

/// Location label for status reporting.
location: Option<String>,
}

/// Which tracks some viewer wants. Paused means neither is.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct Wanted {
video: bool,
audio: bool,
/// The video track's demand ended: it counts as unwanted from now on.
video_closed: bool,
/// The audio track's demand ended: it counts as unwanted from now on.
audio_closed: bool,
}

impl Wanted {
fn paused(&self) -> bool {
!(self.video || self.audio || (self.video_closed && self.audio_closed))
}
}

/// The demand, guarded by the mutex the emulator waits on.
#[derive(Default)]
struct Pause {
wanted: Mutex<Wanted>,
cvar: Condvar,
}

impl Pause {
/// Change the demand under the lock, then wake the emulator.
fn update(&self, f: impl FnOnce(&mut Wanted)) {
f(&mut self.wanted.lock().unwrap());
self.cvar.notify_all();
}

/// Current demand, read under the lock.
fn get(&self) -> Wanted {
*self.wanted.lock().unwrap()
}

/// Block until some track is wanted (or demand closed).
fn wait(&self) {
drop(
self.cvar
.wait_while(self.wanted.lock().unwrap(), |d| d.paused())
.unwrap(),
);
}
}

/// Which track a monitor updates.
#[derive(Clone, Copy, Debug)]
enum Kind {
Video,
Audio,
}

/// Wait until some viewer wants the track, or none does.
async fn until_wanted(demand: &moq_net::track::Demand, wanted: bool) -> moq_net::Result<()> {
let mut demand = demand.clone();
Expand All @@ -172,54 +217,36 @@ async fn until_wanted(demand: &moq_net::track::Demand, wanted: bool) -> moq_net:
}

impl Session {
/// Monitor a single track's subscription state.
/// Sets the flag when a viewer subscribes, clears it when all unsubscribe.
async fn run_track_monitor(&self, name: &str, track: &moq_net::track::Demand, flag: &AtomicBool) {
/// Mirror one track's subscription state into the shared demand.
async fn run_track_monitor(&self, kind: Kind) {
let track = match kind {
Kind::Video => &self.video_track,
Kind::Audio => &self.audio_track,
};
let set = |wanted: bool| {
self.pause.update(|d| match kind {
Kind::Video => d.video = wanted,
Kind::Audio => d.audio = wanted,
})
};
loop {
if until_wanted(track, true).await.is_err() {
break;
}
tracing::info!("resuming {name}: viewer subscribed");
flag.store(true, Ordering::Release);
self.paused.store(false, Ordering::Release);
self.resume.1.notify_all();
tracing::info!(?kind, "viewer subscribed");
set(true);

if until_wanted(track, false).await.is_err() {
break;
}
tracing::info!("pausing {name}: no viewers");
flag.store(false, Ordering::Release);
}
}

/// Monitor overall pause state.
/// Pauses when BOTH tracks are unused, resumes when EITHER becomes used.
async fn run_pause_monitor(&self) {
loop {
// Wait for BOTH tracks to become unused.
let (v, a) = tokio::join!(
until_wanted(&self.video_track, false),
until_wanted(&self.audio_track, false)
);
if v.is_err() || a.is_err() {
break;
}
tracing::info!("pausing emulation: no viewers");
self.paused.store(true, Ordering::Release);

// Wait for EITHER track to become used.
tokio::select! {
Err(_) = until_wanted(&self.video_track, true) => break,
Err(_) = until_wanted(&self.audio_track, true) => break,
else => {},
}
tracing::info!("resuming emulation: viewer connected");
self.paused.store(false, Ordering::Release);
self.resume.1.notify_all();
tracing::info!(?kind, "no viewers");
set(false);
}
// Ensure emulator thread isn't stuck waiting on resume.
self.paused.store(false, Ordering::Release);
self.resume.1.notify_all();
// A closed track is unwanted; once both close, the emulator never blocks again.
self.pause.update(|d| match kind {
Kind::Video => (d.video, d.video_closed) = (false, true),
Kind::Audio => (d.audio, d.audio_closed) = (false, true),
});
}

/// Block the emulator thread until viewers connect.
Expand All @@ -228,11 +255,7 @@ impl Session {
/// joins mid-playthrough rather than a fresh boot.
fn wait_for_resume(&self) {
tracing::info!("pausing encoding");
let (lock, cvar) = &self.resume;
let mut guard = lock.lock().unwrap();
while self.paused.load(Ordering::Acquire) {
guard = cvar.wait(guard).unwrap();
}
self.pause.wait();
tracing::info!("resuming encoding");
}

Expand Down Expand Up @@ -310,22 +333,16 @@ async fn run(config: &Config) -> Result<()> {
video_encoder,
video_track,
audio_track,
video_active: AtomicBool::new(false),
audio_active: AtomicBool::new(false),
paused: AtomicBool::new(true), // Start paused until first viewer.
resume: (Mutex::new(()), Condvar::new()),
pause: Pause::default(), // Paused until the first viewer.
location: config.location.clone(),
});

// Monitor track subscriptions.
let s = session.clone();
tokio::spawn(async move { s.run_track_monitor("video", &s.video_track, &s.video_active).await });
tokio::spawn(async move { s.run_track_monitor(Kind::Video).await });

let s = session.clone();
tokio::spawn(async move { s.run_track_monitor("audio", &s.audio_track, &s.audio_active).await });

let s = session.clone();
tokio::spawn(async move { s.run_pause_monitor().await });
tokio::spawn(async move { s.run_track_monitor(Kind::Audio).await });

// Run the emulator on a blocking thread.
let emulator_handle = tokio::task::spawn_blocking({
Expand Down Expand Up @@ -375,7 +392,7 @@ fn run_emulator(

loop {
// Block when no viewers are watching. See state diagram in module docs.
if session.paused.load(Ordering::Acquire) {
if session.pause.get().paused() {
// Mark the break BEFORE parking, not after resuming. Our PTS is the emulator
// clock, which keeps running while we sleep, so the next frame lands however
// long the pause was into the future. Published as a marker group that gap
Expand Down Expand Up @@ -472,8 +489,11 @@ fn run_emulator(
let elapsed = start.elapsed();

// Accumulate stats for this frame.
let is_video = session.video_active.load(Ordering::Relaxed);
let is_audio = session.audio_active.load(Ordering::Relaxed);
let Wanted {
video: is_video,
audio: is_audio,
..
} = session.pause.get();
game_stats.tick(is_video, is_audio);

// Tick the emulator.
Expand Down Expand Up @@ -545,6 +565,41 @@ async fn main() -> Result<()> {
mod tests {
use super::*;

/// A viewer subscribing while the emulator parks always wakes it: the demand
/// changes under the lock the emulator waits on, so no notify is lost between
/// its check and its wait.
#[test]
fn subscribe_always_wakes() {
for _ in 0..2_000 {
let pause = Arc::new(Pause::default());
let waiter = std::thread::spawn({
let pause = pause.clone();
move || pause.wait()
});
pause.update(|d| d.video = true);
waiter.join().unwrap();
}
}

/// Paused only while neither track is wanted and some track's demand is open.
#[test]
fn paused_is_derived() {
let mut d = Wanted::default();
assert!(d.paused());
d.audio = true;
assert!(!d.paused());

// Video's encoder exits while audio plays: audio leaving must still pause.
d.video_closed = true;
assert!(!d.paused());
d.audio = false;
assert!(d.paused());

// Both closed: never block again.
d.audio_closed = true;
assert!(!d.paused());
}

/// `--connect` is the only way to reach the relay: it must parse without
/// a connect flag of our own alongside it, and the URL must land where `run`
/// reads it. A second required flag would leave `--connect` inert.
Expand Down
4 changes: 3 additions & 1 deletion rs/uniffi-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ description = "Generate the Kotlin, Swift, and Python bindings from the moq-ffi
name = "uniffi-bindgen"
path = "src/main.rs"
# Off by default so a workspace check skips the generator fork. Maturin passes
# required features along when it runs this bin for the Python wheel.
# required features along when it runs this bin for the Python wheel. It has no
# tests, so the test build leaves it out rather than compile the fork.
required-features = ["cli"]
test = false

[features]
cli = ["dep:uniffi"]
Expand Down
13 changes: 7 additions & 6 deletions sh/rs/select.sh
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,12 @@ case "$action" in
# The relay's io_uring listener, Linux-only. A moq-uring change selects
# the relay too, as its dependent.
if [[ "$(uname -s)" == Linux ]] && wants moq-relay; then features+=(moq-relay/io-uring); fi
# Cargo skips a bench or example whose `required-features` are off without
# a word, and a bench-only change selects its crate alone, so no dependent
# turns them on. Turn on every one the built crates' targets name. All of
# them are additive and build wherever the gate runs; moq-uring's targets
# compile to nothing off Linux.
# Cargo skips a target whose `required-features` are off without a word,
# and a crate alone in the selection (a bench-only change, or moq-boy,
# whose one target is its binary) has no dependent to turn them on. Turn
# on every one named by a target this build holds: benches, examples, and
# every target with tests. All of them are additive and build wherever the
# gate runs; moq-uring's targets compile to nothing off Linux.
required=$(
set -e
cargo metadata --locked --format-version 1 --no-deps | jq -r --arg names "$building" --arg all "$packages" '
Expand All @@ -221,7 +222,7 @@ case "$action" in
| select(if $all == "ALL" then (.name | endswith("-fuzz") | not) else (.name as $n | $want | index($n) != null) end)
| .name as $name
| .targets[]
| select(.kind // [] | any(. == "bench" or . == "example"))
| select((.kind // [] | any(. == "bench" or . == "example")) or .test)
| (.["required-features"] // [])[]
| "\($name)/\(.)"' | sort -u | tr '\n' ' '
)
Expand Down
11 changes: 9 additions & 2 deletions sh/rs/select.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def target_features():
"""Every bench and example `required-features`, as `crate/feature`, for the workspace."""
"""Every `required-features` of a bench, example, or tested target, as `crate/feature`, for the workspace."""
metadata = json.loads(
subprocess.run(
["cargo", "metadata", "--locked", "--format-version", "1", "--no-deps"],
Expand All @@ -32,7 +32,7 @@ def target_features():
for package in metadata["packages"]
if not package["name"].endswith("-fuzz")
for target in package["targets"]
if {"bench", "example"} & set(target["kind"])
if {"bench", "example"} & set(target["kind"]) or target["test"]
for feature in target.get("required-features", [])
}
)
Expand Down Expand Up @@ -265,6 +265,13 @@ def test_a_bench_alone_enables_its_required_features(self):
self.assertIn(" --benches --examples ", call)
self.assertIn(feature, call.split(" --features ", 1)[1].split())

def test_a_binary_alone_enables_its_required_features(self):
# moq-boy's only target is its binary, so without openh264 the build
# holds nothing and its tests never run.
calls = self.selected("rs/moq-boy/src/main.rs", action="test")
(call,) = [call for call in calls if call.startswith("rs test ")]
self.assertIn("moq-boy/openh264", call.split(" --features ", 1)[1].split())


class MetadataTest(unittest.TestCase):
def setUp(self):
Expand Down
Loading