every.channel: sanitized baseline

This commit is contained in:
every.channel 2026-02-15 16:17:27 -05:00
commit 897e556bea
No known key found for this signature in database
258 changed files with 74298 additions and 0 deletions

View file

@ -0,0 +1,308 @@
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
fn ec_node_path() -> std::path::PathBuf {
if let Ok(value) = std::env::var("EC_NODE_BIN") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec_node") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec-node") {
return value.into();
}
let exe = std::env::current_exe().expect("current_exe");
let debug_dir = exe
.parent()
.and_then(|p| p.parent())
.expect("expected target/debug/deps");
debug_dir.join("ec-node")
}
fn wait_for_line_prefix(
lines: &mut dyn Iterator<Item = std::io::Result<String>>,
prefix: &str,
timeout: Duration,
) -> Option<String> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
match lines.next() {
Some(Ok(line)) => {
if let Some(rest) = line.strip_prefix(prefix) {
return Some(rest.trim().to_string());
}
}
Some(Err(_)) => continue,
None => break,
}
}
None
}
fn blake3_hex(path: &Path) -> anyhow::Result<String> {
let bytes = std::fs::read(path)?;
Ok(blake3::hash(&bytes).to_hex().to_string())
}
fn concat_init_and_segment(init: &Path, seg: &Path, out: &Path) -> anyhow::Result<()> {
let init_bytes = std::fs::read(init)?;
let seg_bytes = std::fs::read(seg)?;
let mut bytes = Vec::with_capacity(init_bytes.len() + seg_bytes.len());
bytes.extend_from_slice(&init_bytes);
bytes.extend_from_slice(&seg_bytes);
std::fs::write(out, bytes)?;
Ok(())
}
fn first_video_frame_keyframe_flag(mp4: &Path) -> anyhow::Result<u32> {
if Command::new("ffprobe")
.arg("-version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_err()
{
// Cross-OS environments might not have ffprobe installed; treat as skip.
return Ok(1);
}
// Read only the first decoded frame record. For fMP4 this works reliably if we concat init+seg.
let out = Command::new("ffprobe")
.arg("-v")
.arg("error")
.arg("-select_streams")
.arg("v:0")
.arg("-show_frames")
.arg("-read_intervals")
.arg("%+#1")
.arg("-show_entries")
.arg("frame=key_frame")
.arg("-of")
.arg("csv=p=0")
.arg(mp4)
.output()?;
if !out.status.success() {
anyhow::bail!("ffprobe failed: {}", String::from_utf8_lossy(&out.stderr));
}
let s = String::from_utf8_lossy(&out.stdout);
let first = s.lines().next().unwrap_or("").trim();
// Some ffprobe builds may append extra columns (e.g. side data) even with restricted
// `-show_entries`. We only care about the first token.
let token = first.split(',').next().unwrap_or("").trim();
let flag: u32 = token
.parse()
.map_err(|_| anyhow::anyhow!("unexpected ffprobe output: {first:?}"))?;
Ok(flag)
}
fn write_deterministic_ts(out_path: &Path) -> anyhow::Result<()> {
// Deterministic synthetic A/V source: 30fps CFR with a fixed sine audio tone.
// Output: MPEG-TS, constrained to a stable keyframe cadence (g=60 -> 2s GOP).
let status = Command::new("ffmpeg")
.arg("-hide_banner")
.arg("-loglevel")
.arg("error")
.arg("-nostdin")
.arg("-y")
.arg("-f")
.arg("lavfi")
.arg("-i")
.arg("testsrc2=size=1280x720:rate=30")
.arg("-f")
.arg("lavfi")
.arg("-i")
.arg("sine=frequency=1000:sample_rate=48000")
.arg("-t")
.arg("10")
.arg("-map")
.arg("0:v:0")
.arg("-map")
.arg("1:a:0")
.arg("-c:v")
.arg("libx264")
.arg("-pix_fmt")
.arg("yuv420p")
.arg("-g")
.arg("60")
.arg("-keyint_min")
.arg("60")
.arg("-sc_threshold")
.arg("0")
.arg("-bf")
.arg("0")
.arg("-threads")
.arg("1")
.arg("-fflags")
.arg("+bitexact")
.arg("-flags:v")
.arg("+bitexact")
.arg("-c:a")
.arg("aac")
.arg("-b:a")
.arg("128k")
.arg("-ac")
.arg("2")
.arg("-ar")
.arg("48000")
.arg("-flags:a")
.arg("+bitexact")
.arg("-f")
.arg("mpegts")
.arg(out_path)
.status()?;
if !status.success() {
anyhow::bail!("ffmpeg synthetic TS generation failed with {status}");
}
Ok(())
}
fn run_ladder(ec_node: &Path, input_ts: &Path, out_dir: &Path) -> anyhow::Result<()> {
let signing_key = "11".repeat(32);
let network_secret = "22".repeat(32);
let stream_id = "every.channel/determinism/cmaf-ladder";
let broadcast_name = "every.channel/determinism/cmaf-ladder";
let mut cmd = Command::new(ec_node);
cmd.env("EVERY_CHANNEL_MANIFEST_SIGNING_KEY", &signing_key)
.arg("moq-publish")
.arg("--publish-manifests")
.arg("--encode")
.arg("cmaf")
.arg("--cmaf-ladder")
.arg("hd3")
.arg("--epoch-chunks")
.arg("1")
.arg("--max-chunks")
.arg("3")
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(stream_id)
.arg("--broadcast-name")
.arg(broadcast_name)
.arg("--track-name")
.arg("chunks")
.arg("--init-track")
.arg("init")
.arg("--network-secret")
.arg(&network_secret)
.arg("--chunk-dir")
.arg(out_dir)
.arg("--startup-delay-ms")
.arg("0")
.arg("ts")
.arg(input_ts)
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
// This will run until --max-chunks is reached, then exit.
let mut child = cmd.spawn()?;
let stdout = child.stdout.take().expect("publisher stdout missing");
let mut lines = BufReader::new(stdout).lines();
let _remote = wait_for_line_prefix(&mut lines, "moq endpoint addr: ", Duration::from_secs(10))
.ok_or_else(|| anyhow::anyhow!("publisher did not print endpoint addr"))?;
let status = child.wait()?;
if !status.success() {
anyhow::bail!("publisher failed: {status}");
}
Ok(())
}
#[test]
#[ignore]
fn deterministic_cmaf_ladder_outputs_match_across_runs() {
let ec_node = ec_node_path();
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let tmp = std::env::temp_dir().join(format!("ec-determinism-cmaf-ladder-{ts}"));
let _ = std::fs::create_dir_all(&tmp);
let input_ts = tmp.join("input.ts");
write_deterministic_ts(&input_ts).expect("write deterministic TS");
let run1 = tmp.join("run1");
let run2 = tmp.join("run2");
let _ = std::fs::remove_dir_all(&run1);
let _ = std::fs::remove_dir_all(&run2);
std::fs::create_dir_all(&run1).unwrap();
std::fs::create_dir_all(&run2).unwrap();
run_ladder(&ec_node, &input_ts, &run1).expect("run ladder 1");
run_ladder(&ec_node, &input_ts, &run2).expect("run ladder 2");
for variant in ["1080p", "720p", "480p"] {
let v1 = run1.join("cmaf-ladder").join(variant);
let v2 = run2.join("cmaf-ladder").join(variant);
let init1 = v1.join("init.mp4");
let init2 = v2.join("init.mp4");
assert!(
init1.exists() && init2.exists(),
"missing init for {variant}"
);
assert_eq!(
blake3_hex(&init1).unwrap(),
blake3_hex(&init2).unwrap(),
"init differs for {variant}"
);
for idx in 0..3 {
let s1 = v1.join(format!("segment_{idx:06}.m4s"));
let s2 = v2.join(format!("segment_{idx:06}.m4s"));
assert!(
s1.exists() && s2.exists(),
"missing segment {idx} for {variant}"
);
assert_eq!(
blake3_hex(&s1).unwrap(),
blake3_hex(&s2).unwrap(),
"segment {idx} differs for {variant}"
);
}
}
}
#[test]
#[ignore]
fn cmaf_ladder_segments_start_with_keyframes() {
let ec_node = ec_node_path();
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let tmp = std::env::temp_dir().join(format!("ec-determinism-cmaf-ladder-kf-{ts}"));
let _ = std::fs::create_dir_all(&tmp);
let input_ts = tmp.join("input.ts");
write_deterministic_ts(&input_ts).expect("write deterministic TS");
let run = tmp.join("run");
let _ = std::fs::remove_dir_all(&run);
std::fs::create_dir_all(&run).unwrap();
run_ladder(&ec_node, &input_ts, &run).expect("run ladder");
for variant in ["1080p", "720p", "480p"] {
let v = run.join("cmaf-ladder").join(variant);
let init = v.join("init.mp4");
assert!(init.exists(), "missing init for {variant}");
for idx in 0..3 {
let seg = v.join(format!("segment_{idx:06}.m4s"));
assert!(seg.exists(), "missing segment {idx} for {variant}");
let stitched = tmp.join(format!("stitched-{variant}-{idx:06}.mp4"));
concat_init_and_segment(&init, &seg, &stitched).unwrap();
let keyflag = first_video_frame_keyframe_flag(&stitched).unwrap();
assert_eq!(
keyflag, 1,
"segment {idx} not keyframe-aligned for {variant}"
);
}
}
}

View file

@ -0,0 +1,231 @@
use std::io::{BufRead, BufReader, Read};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
const TS_PACKET_SIZE: usize = 188;
fn env_required(key: &str) -> Option<String> {
std::env::var(key)
.ok()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
}
fn ec_node_path() -> std::path::PathBuf {
if let Ok(value) = std::env::var("EC_NODE_BIN") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec_node") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec-node") {
return value.into();
}
let exe = std::env::current_exe().expect("current_exe");
let debug_dir = exe
.parent()
.and_then(|p| p.parent())
.expect("expected target/debug/deps");
debug_dir.join("ec-node")
}
fn wait_for_line_prefix(
lines: &mut dyn Iterator<Item = std::io::Result<String>>,
prefix: &str,
timeout: Duration,
) -> Option<String> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
match lines.next() {
Some(Ok(line)) => {
if let Some(rest) = line.strip_prefix(prefix) {
return Some(rest.trim().to_string());
}
}
Some(Err(_)) => continue,
None => break,
}
}
None
}
fn write_short_ts_recording(
host: &str,
channel: &str,
out_path: &std::path::Path,
) -> anyhow::Result<()> {
// Use lineup to resolve name -> number, but capture from the provided host.
// (OrbStack/Linux may not resolve the lineup URL's mDNS hostname.)
let device = ec_hdhomerun::discover_from_host(host)?;
let lineup = ec_hdhomerun::fetch_lineup(&device)?;
let entry = ec_hdhomerun::find_lineup_entry_by_number(&lineup, channel)
.or_else(|| ec_hdhomerun::find_lineup_entry_by_name(&lineup, channel))
.ok_or_else(|| anyhow::anyhow!("channel not found in lineup: {channel}"))?;
let guide_number = entry.channel.number.as_deref().unwrap_or(channel);
let capture_url = format!("http://{host}:5004/auto/v{guide_number}");
// Capture a short TS sample directly from the HDHR.
// Retry a few times to handle "no tuner available" 5xx responses.
let mut last_err: Option<anyhow::Error> = None;
for attempt in 0..10 {
match ec_hdhomerun::open_stream_url(&capture_url, Some(14)) {
Ok(mut stream) => {
let mut file = std::fs::File::create(out_path)?;
std::io::copy(&mut stream, &mut file)?;
last_err = None;
break;
}
Err(err) => {
last_err = Some(err);
std::thread::sleep(Duration::from_millis(400 * (attempt + 1) as u64));
continue;
}
}
}
if let Some(err) = last_err {
return Err(err);
}
let mut file = std::fs::File::open(out_path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
let mut len = bytes.len();
let rem = len % TS_PACKET_SIZE;
if rem != 0 {
len -= rem;
std::fs::write(out_path, &bytes[..len])?;
}
if len < 188 * 200 {
anyhow::bail!("recorded TS too small ({} bytes) from HDHR {}", len, host);
}
Ok(())
}
#[test]
#[ignore]
fn e2e_cmaf_ladder_one_publisher_three_subscribers_verify_manifests() {
let host = match env_required("EVERY_CHANNEL_E2E_HDHR_HOST") {
Some(v) => v,
None => return, // skip
};
let channel = match env_required("EVERY_CHANNEL_E2E_HDHR_CHANNEL") {
Some(v) => v,
None => return, // skip
};
let ec_node = ec_node_path();
// Keep secrets deterministic for reproducibility.
let signing_key = "11".repeat(32);
let network_secret = "22".repeat(32);
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let stream_id = format!("every.channel/e2e/cmaf-ladder/{ts}");
let broadcast_name = stream_id.clone();
let tmp = std::env::temp_dir().join(format!("ec-e2e-cmaf-ladder-{ts}"));
let _ = std::fs::create_dir_all(&tmp);
let input_ts = tmp.join("input.ts");
write_short_ts_recording(&host, &channel, &input_ts).expect("failed to record TS from HDHR");
let mut publisher = Command::new(&ec_node);
publisher
.env("EVERY_CHANNEL_MANIFEST_SIGNING_KEY", &signing_key)
.arg("moq-publish")
.arg("--publish-manifests")
.arg("--encode")
.arg("cmaf")
.arg("--cmaf-ladder")
.arg("hd3")
.arg("--epoch-chunks")
.arg("1")
.arg("--max-chunks")
.arg("3")
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(&stream_id)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg("chunks")
.arg("--init-track")
.arg("init")
.arg("--manifest-track")
.arg("manifests")
.arg("--network-secret")
.arg(&network_secret)
.arg("--chunk-dir")
.arg(tmp.join("pub-chunks"))
.arg("--startup-delay-ms")
.arg("4000")
.arg("ts")
.arg(input_ts.to_string_lossy().to_string())
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
let mut pub_child = publisher.spawn().expect("spawn publisher");
let pub_stdout = pub_child.stdout.take().expect("publisher stdout missing");
let mut pub_lines = BufReader::new(pub_stdout).lines();
let remote = wait_for_line_prefix(
&mut pub_lines,
"moq endpoint addr: ",
Duration::from_secs(10),
)
.expect("publisher did not print endpoint addr");
let variants = ["1080p", "720p", "480p"];
let mut subscribers = Vec::new();
for variant in variants {
let out_dir = tmp.join(format!("sub-{variant}"));
let mut sub = Command::new(&ec_node);
sub.arg("moq-subscribe")
.arg("--remote")
.arg(&remote)
.arg("--remote-manifests")
.arg(&remote)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg(format!("chunks/{variant}"))
.arg("--subscribe-manifests")
.arg("--require-manifest")
.arg("--manifest-track")
.arg("manifests")
.arg("--container")
.arg("cmaf")
.arg("--subscribe-init")
.arg("--init-track")
.arg(format!("init/{variant}"))
.arg("--raw-cmaf")
.arg("--stop-after")
.arg("2")
.arg("--network-secret")
.arg(&network_secret)
.arg("--output-dir")
.arg(&out_dir)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
subscribers.push((
variant.to_string(),
out_dir,
sub.spawn().expect("spawn subscriber"),
));
}
for (variant, out_dir, mut child) in subscribers {
let status = child.wait().expect("wait subscriber");
assert!(status.success(), "subscriber {variant} failed: {status}");
let init = out_dir.join("init.mp4");
assert!(init.exists(), "subscriber {variant} missing init.mp4");
let seg0 = out_dir.join("segment_000000.m4s");
assert!(seg0.exists(), "subscriber {variant} missing first segment");
}
let _ = pub_child.kill();
}

View file

@ -0,0 +1,211 @@
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
fn env_required(key: &str) -> Option<String> {
std::env::var(key)
.ok()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
}
fn looks_drm(value: &str) -> bool {
let value = value.to_lowercase();
value.contains("drm")
|| value.contains("encrypted")
|| value.contains("protected")
|| value.contains("copy")
|| value.contains("widevine")
}
fn autodiscover_hdhr_host_and_channel() -> Option<(String, String)> {
let devices = ec_hdhomerun::discover().ok()?;
let device = devices.into_iter().next()?;
let lineup = ec_hdhomerun::fetch_lineup(&device).ok()?;
let entry = lineup.iter().find(|e| {
let tag_drm = e.tags.iter().any(|t| looks_drm(t));
let raw_drm = e
.raw
.as_object()
.map(|obj| {
obj.iter()
.any(|(k, v)| looks_drm(k) || looks_drm(&v.to_string()))
})
.unwrap_or(false);
!tag_drm && !raw_drm && e.channel.number.as_deref().unwrap_or("").trim() != ""
})?;
let host = device.ip.clone();
let channel = entry
.channel
.number
.clone()
.or_else(|| Some(entry.channel.name.clone()))
.unwrap_or_else(|| "2.1".to_string());
Some((host, channel))
}
fn ec_node_path() -> std::path::PathBuf {
if let Ok(value) = std::env::var("EC_NODE_BIN") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec_node") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec-node") {
return value.into();
}
// Fallback: assume a standard cargo target layout.
let exe = std::env::current_exe().expect("current_exe");
let debug_dir = exe
.parent()
.and_then(|p| p.parent())
.expect("expected target/debug/deps");
let bin = debug_dir.join("ec-node");
bin
}
#[test]
#[ignore]
fn e2e_hdhr_publish_then_subscribe_with_manifest_and_encryption() {
let host = env_required("EVERY_CHANNEL_E2E_HDHR_HOST");
let channel = env_required("EVERY_CHANNEL_E2E_HDHR_CHANNEL");
let (host, channel) = match (host, channel) {
(Some(host), Some(channel)) => (host, channel),
_ => match autodiscover_hdhr_host_and_channel() {
Some(v) => v,
None => return, // skip
},
};
let ec_node = ec_node_path();
// Keep secrets deterministic for reproducibility.
let signing_key = "11".repeat(32);
let network_secret = "22".repeat(32);
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let broadcast_name = format!("every.channel/e2e/{ts}");
let tmp = std::env::temp_dir().join(format!("ec-e2e-hdhr-{ts}"));
let publish_chunks = tmp.join("publish-chunks");
let subscribe_out = tmp.join("subscribe-out");
let mut publisher = Command::new(&ec_node);
publisher
.env("EVERY_CHANNEL_MANIFEST_SIGNING_KEY", &signing_key)
.arg("moq-publish")
.arg("--publish-manifests")
.arg("--epoch-chunks")
.arg("1")
.arg("--max-chunks")
.arg("8")
.arg("--chunk-ms")
.arg("2000")
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--network-secret")
.arg(&network_secret)
.arg("--chunk-dir")
.arg(&publish_chunks)
.arg("hdhr")
.arg("--host")
.arg(&host)
.arg("--channel")
.arg(&channel)
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
let mut child = publisher.spawn().expect("failed to spawn publisher");
let stdout = child.stdout.take().expect("publisher stdout missing");
let mut lines = BufReader::new(stdout).lines();
let mut remote: Option<String> = None;
let mut track: Option<String> = None;
let deadline = Instant::now() + Duration::from_secs(10);
while Instant::now() < deadline {
let line = match lines.next() {
Some(Ok(line)) => line,
Some(Err(_)) => continue,
None => break,
};
if let Some(rest) = line.strip_prefix("moq endpoint addr: ") {
remote = Some(rest.trim().to_string());
} else if let Some(rest) = line.strip_prefix("moq track: ") {
track = Some(rest.trim().to_string());
}
if remote.is_some() && track.is_some() {
break;
}
}
let remote = remote.expect("publisher did not print endpoint addr in time");
let track = track.expect("publisher did not print track in time");
let mut subscriber = Command::new(&ec_node);
subscriber
.arg("moq-subscribe")
.arg("--remote")
.arg(&remote)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg(&track)
.arg("--subscribe-manifests")
.arg("--require-manifest")
.arg("--max-invalid-chunks")
.arg("0")
.arg("--stop-after")
.arg("3")
.arg("--output-dir")
.arg(&subscribe_out)
.arg("--chunk-ms")
.arg("2000")
.arg("--network-secret")
.arg(&network_secret)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
let mut sub_child = subscriber.spawn().expect("failed to spawn subscriber");
let start = Instant::now();
loop {
if let Ok(Some(status)) = sub_child.try_wait() {
assert!(status.success(), "subscriber exited with {status}");
break;
}
if start.elapsed() > Duration::from_secs(30) {
let _ = sub_child.kill();
panic!("subscriber timed out");
}
std::thread::sleep(Duration::from_millis(200));
}
// Publisher should exit after max chunks; don't hang forever.
let start = Instant::now();
loop {
if let Ok(Some(status)) = child.try_wait() {
assert!(status.success(), "publisher exited with {status}");
break;
}
if start.elapsed() > Duration::from_secs(30) {
let _ = child.kill();
panic!("publisher timed out");
}
std::thread::sleep(Duration::from_millis(200));
}
let playlist = subscribe_out.join("index.m3u8");
assert!(
playlist.exists(),
"missing playlist at {}",
playlist.display()
);
let segments = std::fs::read_dir(&subscribe_out)
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().starts_with("segment_"))
.count();
assert!(segments >= 1, "expected at least one segment");
}

View file

@ -0,0 +1,305 @@
use std::io::{BufRead, BufReader, Read, Write};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
const TS_PACKET_SIZE: usize = 188;
fn env_required(key: &str) -> Option<String> {
std::env::var(key)
.ok()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
}
fn ec_node_path() -> std::path::PathBuf {
if let Ok(value) = std::env::var("EC_NODE_BIN") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec_node") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec-node") {
return value.into();
}
let exe = std::env::current_exe().expect("current_exe");
let debug_dir = exe
.parent()
.and_then(|p| p.parent())
.expect("expected target/debug/deps");
debug_dir.join("ec-node")
}
fn wait_for_line_prefix(
lines: &mut dyn Iterator<Item = std::io::Result<String>>,
prefix: &str,
timeout: Duration,
) -> Option<String> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
match lines.next() {
Some(Ok(line)) => {
if let Some(rest) = line.strip_prefix(prefix) {
return Some(rest.trim().to_string());
}
}
Some(Err(_)) => continue,
None => break,
}
}
None
}
fn write_short_ts_recording(
host: &str,
channel: &str,
out_path: &std::path::Path,
) -> anyhow::Result<()> {
// Use the lineup's stream URL so we get the correct host/port (often :5004).
// HDHomeRun supports `duration=...` on the stream URL on many models.
// We also cap by time/bytes to avoid hanging if duration is ignored.
let device = ec_hdhomerun::discover_from_host(host)?;
let lineup = ec_hdhomerun::fetch_lineup(&device)?;
let entry = ec_hdhomerun::find_lineup_entry_by_number(&lineup, channel)
.or_else(|| ec_hdhomerun::find_lineup_entry_by_name(&lineup, channel))
.ok_or_else(|| anyhow::anyhow!("channel not found in lineup: {channel}"))?;
// Tuner allocation can transiently fail (503) if another client is using all tuners.
// Retry briefly; we only need a short capture.
let mut last_err: Option<anyhow::Error> = None;
let mut stream = loop {
match ec_hdhomerun::open_stream_entry(entry, Some(8)) {
Ok(stream) => break stream,
Err(err) => {
let msg = format!("{err:#}");
last_err = Some(err);
if msg.contains("503") {
std::thread::sleep(Duration::from_millis(500));
continue;
}
return Err(last_err.unwrap());
}
}
};
let mut file = std::fs::File::create(out_path)?;
let start = Instant::now();
let mut bytes = 0usize;
let mut buf = [0u8; 64 * 1024];
loop {
let n = stream.read(&mut buf)?;
if n == 0 {
break;
}
file.write_all(&buf[..n])?;
bytes += n;
if bytes >= 8 * 1024 * 1024 {
break;
}
if start.elapsed() > Duration::from_secs(6) {
break;
}
}
file.flush()?;
// Ensure the TS file ends on a packet boundary.
let len = file.metadata()?.len();
let rem = (len as usize) % TS_PACKET_SIZE;
if rem != 0 {
file.set_len(len - rem as u64)?;
bytes = (len as usize) - rem;
}
if bytes < 188 * 20 {
anyhow::bail!("recorded TS too small ({} bytes) from HDHR {}", bytes, host);
}
Ok(())
}
#[test]
#[ignore]
fn e2e_split_sources_manifests_from_one_peer_objects_from_another() {
let host = match env_required("EVERY_CHANNEL_E2E_HDHR_HOST") {
Some(v) => v,
None => return, // skip
};
let channel = match env_required("EVERY_CHANNEL_E2E_HDHR_CHANNEL") {
Some(v) => v,
None => return, // skip
};
let ec_node = ec_node_path();
// Keep secrets deterministic for reproducibility.
let signing_key = "11".repeat(32);
let network_secret = "22".repeat(32);
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let stream_id = format!("every.channel/e2e/mesh/{ts}");
let broadcast_name = stream_id.clone();
let tmp = std::env::temp_dir().join(format!("ec-e2e-mesh-split-{ts}"));
let _ = std::fs::create_dir_all(&tmp);
let input_ts = tmp.join("input.ts");
let manifest_chunks = tmp.join("chunks-manifests");
let object_chunks = tmp.join("chunks-objects");
let subscribe_out = tmp.join("subscribe-out");
write_short_ts_recording(&host, &channel, &input_ts).expect("failed to record TS from HDHR");
// Publisher A: leader/signer, publishes manifests only.
// Give subscribers time to connect before ingest starts.
let mut pub_manifests = Command::new(&ec_node);
pub_manifests
.env("EVERY_CHANNEL_MANIFEST_SIGNING_KEY", &signing_key)
.arg("moq-publish")
.arg("--publish-manifests")
.arg("--publish-chunks")
.arg("false")
.arg("--epoch-chunks")
.arg("1")
.arg("--max-chunks")
.arg("6")
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(&stream_id)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg("noop")
.arg("--manifest-track")
.arg("manifests")
.arg("--network-secret")
.arg(&network_secret)
.arg("--chunk-dir")
.arg(&manifest_chunks)
.arg("--startup-delay-ms")
.arg("5000")
.arg("ts")
.arg(input_ts.to_string_lossy().to_string())
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
let mut pub_a = pub_manifests.spawn().expect("spawn manifest publisher");
let a_stdout = pub_a
.stdout
.take()
.expect("manifest publisher stdout missing");
let mut a_lines = BufReader::new(a_stdout).lines();
let remote_manifests =
wait_for_line_prefix(&mut a_lines, "moq endpoint addr: ", Duration::from_secs(10))
.expect("manifest publisher did not print endpoint addr");
// Publisher B: relay/data, publishes chunk objects only.
// Delay longer than the manifest publisher so the subscriber can receive manifests first.
let mut pub_objects = Command::new(&ec_node);
pub_objects
.arg("moq-publish")
.arg("--publish-chunks")
.arg("true")
.arg("--max-chunks")
.arg("6")
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(&stream_id)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg("objects")
.arg("--network-secret")
.arg(&network_secret)
.arg("--chunk-dir")
.arg(&object_chunks)
.arg("--startup-delay-ms")
.arg("9000")
.arg("ts")
.arg(input_ts.to_string_lossy().to_string())
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
let mut pub_b = pub_objects.spawn().expect("spawn object publisher");
let b_stdout = pub_b
.stdout
.take()
.expect("object publisher stdout missing");
let mut b_lines = BufReader::new(b_stdout).lines();
let remote_objects =
wait_for_line_prefix(&mut b_lines, "moq endpoint addr: ", Duration::from_secs(10))
.expect("object publisher did not print endpoint addr");
// Subscriber: stitch objects from B with manifests from A.
let mut subscriber = Command::new(&ec_node);
subscriber
.arg("moq-subscribe")
.arg("--remote")
.arg(&remote_objects)
.arg("--remote-manifests")
.arg(&remote_manifests)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg("objects")
.arg("--manifest-track")
.arg("manifests")
.arg("--subscribe-manifests")
.arg("--require-manifest")
.arg("--max-invalid-chunks")
.arg("0")
.arg("--stop-after")
.arg("2")
.arg("--output-dir")
.arg(&subscribe_out)
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(&stream_id)
.arg("--network-secret")
.arg(&network_secret)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
let mut sub_child = subscriber.spawn().expect("failed to spawn subscriber");
let start = Instant::now();
loop {
if let Ok(Some(status)) = sub_child.try_wait() {
assert!(status.success(), "subscriber exited with {status}");
break;
}
if start.elapsed() > Duration::from_secs(30) {
let _ = sub_child.kill();
panic!("subscriber timed out");
}
std::thread::sleep(Duration::from_millis(200));
}
// Ensure publishers exit after max chunks.
for child in [&mut pub_a, &mut pub_b] {
let start = Instant::now();
loop {
if let Ok(Some(status)) = child.try_wait() {
assert!(status.success(), "publisher exited with {status}");
break;
}
if start.elapsed() > Duration::from_secs(30) {
let _ = child.kill();
panic!("publisher timed out");
}
std::thread::sleep(Duration::from_millis(200));
}
}
let playlist = subscribe_out.join("index.m3u8");
assert!(
playlist.exists(),
"missing playlist at {}",
playlist.display()
);
let segments = std::fs::read_dir(&subscribe_out)
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().starts_with("segment_"))
.count();
assert!(segments >= 1, "expected at least one segment");
}

View file

@ -0,0 +1,345 @@
use std::io::{BufRead, BufReader, Read};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
const TS_PACKET_SIZE: usize = 188;
fn env_required(key: &str) -> Option<String> {
std::env::var(key)
.ok()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
}
fn looks_drm(value: &str) -> bool {
let value = value.to_lowercase();
value.contains("drm")
|| value.contains("encrypted")
|| value.contains("protected")
|| value.contains("copy")
|| value.contains("widevine")
}
fn autodiscover_hdhr_host_and_channel() -> Option<(String, String)> {
let devices = ec_hdhomerun::discover().ok()?;
let device = devices.into_iter().next()?;
let lineup = ec_hdhomerun::fetch_lineup(&device).ok()?;
let entry = lineup.iter().find(|e| {
// Prefer a likely-clear channel to avoid false negatives in E2E.
let tag_drm = e.tags.iter().any(|t| looks_drm(t));
let raw_drm = e
.raw
.as_object()
.map(|obj| {
obj.iter()
.any(|(k, v)| looks_drm(k) || looks_drm(&v.to_string()))
})
.unwrap_or(false);
!tag_drm && !raw_drm && e.channel.number.as_deref().unwrap_or("").trim() != ""
})?;
let host = device.ip.clone();
let channel = entry
.channel
.number
.clone()
.or_else(|| Some(entry.channel.name.clone()))
.unwrap_or_else(|| "2.1".to_string());
Some((host, channel))
}
fn ec_node_path() -> std::path::PathBuf {
if let Ok(value) = std::env::var("EC_NODE_BIN") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec_node") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec-node") {
return value.into();
}
let exe = std::env::current_exe().expect("current_exe");
let debug_dir = exe
.parent()
.and_then(|p| p.parent())
.expect("expected target/debug/deps");
debug_dir.join("ec-node")
}
fn wait_for_line_prefix(
lines: &mut dyn Iterator<Item = std::io::Result<String>>,
prefix: &str,
timeout: Duration,
) -> Option<String> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
match lines.next() {
Some(Ok(line)) => {
if let Some(rest) = line.strip_prefix(prefix) {
return Some(rest.trim().to_string());
}
}
Some(Err(_)) => continue,
None => break,
}
}
None
}
fn write_short_ts_recording(
host: &str,
channel: &str,
out_path: &std::path::Path,
) -> anyhow::Result<()> {
// Use lineup to resolve name -> number, but capture from the provided host.
// (OrbStack/Linux may not resolve the lineup URL's mDNS hostname.)
let device = ec_hdhomerun::discover_from_host(host)?;
let lineup = ec_hdhomerun::fetch_lineup(&device)?;
let entry = ec_hdhomerun::find_lineup_entry_by_number(&lineup, channel)
.or_else(|| ec_hdhomerun::find_lineup_entry_by_name(&lineup, channel))
.ok_or_else(|| anyhow::anyhow!("channel not found in lineup: {channel}"))?;
let guide_number = entry.channel.number.as_deref().unwrap_or(channel);
let capture_url = format!("http://{host}:5004/auto/v{guide_number}");
// Capture a short TS sample directly from the HDHR.
// Retry a few times to handle "no tuner available" 5xx responses.
let mut last_err: Option<anyhow::Error> = None;
for attempt in 0..10 {
match ec_hdhomerun::open_stream_url(&capture_url, Some(12)) {
Ok(mut stream) => {
let mut file = std::fs::File::create(out_path)?;
std::io::copy(&mut stream, &mut file)?;
last_err = None;
break;
}
Err(err) => {
last_err = Some(err);
std::thread::sleep(Duration::from_millis(400 * (attempt + 1) as u64));
continue;
}
}
}
if let Some(err) = last_err {
return Err(err);
}
let mut file = std::fs::File::open(out_path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
let mut len = bytes.len();
// Ensure the TS file ends on a packet boundary.
let rem = len % TS_PACKET_SIZE;
if rem != 0 {
len -= rem;
std::fs::write(out_path, &bytes[..len])?;
}
if len < 188 * 200 {
anyhow::bail!("recorded TS too small ({} bytes) from HDHR {}", len, host);
}
Ok(())
}
#[test]
#[ignore]
fn e2e_split_sources_cmaf_init_from_objects_peer_segments_verified_by_manifests_peer() {
let host = env_required("EVERY_CHANNEL_E2E_HDHR_HOST");
let channel = env_required("EVERY_CHANNEL_E2E_HDHR_CHANNEL");
let (host, channel) = match (host, channel) {
(Some(host), Some(channel)) => (host, channel),
_ => match autodiscover_hdhr_host_and_channel() {
Some(v) => v,
None => return, // skip
},
};
let ec_node = ec_node_path();
// Keep secrets deterministic for reproducibility.
let signing_key = "11".repeat(32);
let network_secret = "22".repeat(32);
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let stream_id = format!("every.channel/e2e/mesh-cmaf/{ts}");
let broadcast_name = stream_id.clone();
let tmp = std::env::temp_dir().join(format!("ec-e2e-mesh-split-cmaf-{ts}"));
let _ = std::fs::create_dir_all(&tmp);
let input_ts = tmp.join("input.ts");
let manifest_chunks = tmp.join("chunks-manifests");
let object_chunks = tmp.join("chunks-objects");
let subscribe_out = tmp.join("subscribe-out");
write_short_ts_recording(&host, &channel, &input_ts).expect("failed to record TS from HDHR");
// Publisher A: leader/signer, publishes manifests only (for CMAF segments).
let mut pub_manifests = Command::new(&ec_node);
pub_manifests
.env("EVERY_CHANNEL_MANIFEST_SIGNING_KEY", &signing_key)
.arg("moq-publish")
.arg("--publish-manifests")
.arg("--publish-chunks")
.arg("false")
.arg("--encode")
.arg("cmaf")
.arg("--epoch-chunks")
.arg("1")
.arg("--max-chunks")
.arg("4")
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(&stream_id)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg("noop")
.arg("--manifest-track")
.arg("manifests")
.arg("--network-secret")
.arg(&network_secret)
.arg("--chunk-dir")
.arg(&manifest_chunks)
.arg("--startup-delay-ms")
.arg("6000")
.arg("ts")
.arg(input_ts.to_string_lossy().to_string())
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
let mut pub_a = pub_manifests.spawn().expect("spawn manifest publisher");
let a_stdout = pub_a
.stdout
.take()
.expect("manifest publisher stdout missing");
let mut a_lines = BufReader::new(a_stdout).lines();
let remote_manifests =
wait_for_line_prefix(&mut a_lines, "moq endpoint addr: ", Duration::from_secs(10))
.expect("manifest publisher did not print endpoint addr");
// Publisher B: publishes init + segments as objects only.
let mut pub_objects = Command::new(&ec_node);
pub_objects
.arg("moq-publish")
.arg("--publish-chunks")
.arg("true")
.arg("--encode")
.arg("cmaf")
.arg("--init-track")
.arg("init")
.arg("--max-chunks")
.arg("4")
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(&stream_id)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg("objects")
.arg("--network-secret")
.arg(&network_secret)
.arg("--chunk-dir")
.arg(&object_chunks)
.arg("--startup-delay-ms")
.arg("10000")
.arg("ts")
.arg(input_ts.to_string_lossy().to_string())
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
let mut pub_b = pub_objects.spawn().expect("spawn object publisher");
let b_stdout = pub_b
.stdout
.take()
.expect("object publisher stdout missing");
let mut b_lines = BufReader::new(b_stdout).lines();
let remote_objects =
wait_for_line_prefix(&mut b_lines, "moq endpoint addr: ", Duration::from_secs(10))
.expect("object publisher did not print endpoint addr");
// Subscriber: init+segments from B, manifests from A.
let mut subscriber = Command::new(&ec_node);
subscriber
.arg("moq-subscribe")
.arg("--remote")
.arg(&remote_objects)
.arg("--remote-manifests")
.arg(&remote_manifests)
.arg("--broadcast-name")
.arg(&broadcast_name)
.arg("--track-name")
.arg("objects")
.arg("--manifest-track")
.arg("manifests")
.arg("--subscribe-manifests")
.arg("--require-manifest")
.arg("--max-invalid-chunks")
.arg("0")
.arg("--container")
.arg("cmaf")
.arg("--subscribe-init")
.arg("--init-track")
.arg("init")
.arg("--stop-after")
.arg("2")
.arg("--output-dir")
.arg(&subscribe_out)
.arg("--chunk-ms")
.arg("2000")
.arg("--stream-id")
.arg(&stream_id)
.arg("--network-secret")
.arg(&network_secret)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
let mut sub_child = subscriber.spawn().expect("failed to spawn subscriber");
let start = Instant::now();
loop {
if let Ok(Some(status)) = sub_child.try_wait() {
assert!(status.success(), "subscriber exited with {status}");
break;
}
if start.elapsed() > Duration::from_secs(60) {
let _ = sub_child.kill();
panic!("subscriber timed out");
}
std::thread::sleep(Duration::from_millis(200));
}
// Ensure publishers exit after max chunks.
for child in [&mut pub_a, &mut pub_b] {
let start = Instant::now();
loop {
if let Ok(Some(status)) = child.try_wait() {
assert!(status.success(), "publisher exited with {status}");
break;
}
if start.elapsed() > Duration::from_secs(90) {
let _ = child.kill();
panic!("publisher timed out");
}
std::thread::sleep(Duration::from_millis(200));
}
}
let playlist = subscribe_out.join("index.m3u8");
assert!(
playlist.exists(),
"missing playlist at {}",
playlist.display()
);
let init = subscribe_out.join("init.mp4");
assert!(init.exists(), "missing init segment at {}", init.display());
let segments = std::fs::read_dir(&subscribe_out)
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().ends_with(".m4s"))
.count();
assert!(segments >= 1, "expected at least one .m4s segment");
}

View file

@ -0,0 +1,314 @@
use std::ffi::OsStr;
use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
fn which(cmd: &str) -> Option<std::path::PathBuf> {
if let Ok(path) = which::which(cmd) {
return Some(path);
}
None
}
fn chrome_path() -> Option<std::path::PathBuf> {
// Prefer the standard macOS Chrome app bundle.
let mac =
std::path::PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
if mac.exists() {
return Some(mac);
}
which("google-chrome")
.or_else(|| which("google-chrome-stable"))
.or_else(|| which("chromium"))
}
fn ec_node_path() -> std::path::PathBuf {
if let Ok(value) = std::env::var("EC_NODE_BIN") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec_node") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec-node") {
return value.into();
}
let exe = std::env::current_exe().expect("current_exe");
let debug_dir = exe
.parent()
.and_then(|p| p.parent())
.expect("expected target/debug/deps");
debug_dir.join("ec-node")
}
fn read_line_with_timeout(
lines: &mut dyn Iterator<Item = std::io::Result<String>>,
timeout: Duration,
) -> Option<String> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
match lines.next() {
Some(Ok(line)) => {
let line = line.trim().to_string();
if !line.is_empty() {
return Some(line);
}
}
Some(Err(_)) => continue,
None => break,
}
}
None
}
fn generate_ts_fixture(out: &std::path::Path) -> anyhow::Result<()> {
// Deterministic-ish fixture: single-threaded x264, fixed GOP, sine audio.
let status = Command::new("ffmpeg")
.arg("-hide_banner")
.arg("-loglevel")
.arg("error")
.arg("-nostdin")
.arg("-y")
.arg("-f")
.arg("lavfi")
.arg("-i")
.arg("testsrc2=size=1280x720:rate=30")
.arg("-f")
.arg("lavfi")
.arg("-i")
.arg("sine=frequency=1000:sample_rate=48000")
.arg("-t")
.arg("12")
.arg("-map")
.arg("0:v:0")
.arg("-map")
.arg("1:a:0")
.arg("-c:v")
.arg("libx264")
.arg("-pix_fmt")
.arg("yuv420p")
.arg("-g")
.arg("60")
.arg("-keyint_min")
.arg("60")
.arg("-sc_threshold")
.arg("0")
.arg("-bf")
.arg("0")
.arg("-threads")
.arg("1")
.arg("-c:a")
.arg("aac")
.arg("-b:a")
.arg("128k")
.arg("-ac")
.arg("2")
.arg("-ar")
.arg("48000")
.arg("-f")
.arg("mpegts")
.arg(out)
.status()?;
if !status.success() {
anyhow::bail!("ffmpeg fixture generation failed with {status}");
}
Ok(())
}
fn click_button_by_text(tab: &headless_chrome::Tab, text: &str) -> anyhow::Result<()> {
let js = format!(
r#"(function() {{
let btns = Array.from(document.querySelectorAll('button'));
let btn = btns.find(b => (b.innerText || '').trim() === {t});
if (!btn) return false;
btn.click();
return true;
}})();"#,
t = serde_json::to_string(text).unwrap()
);
let v = tab.evaluate(&js, false)?;
let ok = v.value.and_then(|v| v.as_bool()).unwrap_or(false);
if !ok {
anyhow::bail!("button not found: {text}");
}
Ok(())
}
fn fill_input_by_placeholder(
tab: &headless_chrome::Tab,
placeholder: &str,
value: &str,
) -> anyhow::Result<()> {
let js = format!(
r#"(function() {{
let input = document.querySelector('input[placeholder={p}]');
if (!input) return false;
input.focus();
input.value = {v};
input.dispatchEvent(new Event('input', {{ bubbles: true }}));
input.dispatchEvent(new Event('change', {{ bubbles: true }}));
return true;
}})();"#,
p = serde_json::to_string(placeholder).unwrap(),
v = serde_json::to_string(value).unwrap()
);
let v = tab.evaluate(&js, false)?;
let ok = v.value.and_then(|v| v.as_bool()).unwrap_or(false);
if !ok {
anyhow::bail!("input not found for placeholder: {placeholder}");
}
Ok(())
}
fn get_reply_link(tab: &headless_chrome::Tab) -> anyhow::Result<Option<String>> {
// Read the last readonly input inside the add menu; this is where we render the reply code.
let js = r#"(function() {
let menu = document.querySelector('.source-menu');
if (!menu) return null;
let inputs = Array.from(menu.querySelectorAll('input.source-menu-input[readonly]'));
if (!inputs.length) return null;
return inputs[inputs.length - 1].value || null;
})();"#;
let v = tab.evaluate(js, false)?;
Ok(v.value.and_then(|v| v.as_str().map(|s| s.to_string())))
}
fn wait_for_text(
tab: &headless_chrome::Tab,
needle: &str,
timeout: Duration,
) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
let js = format!(
r#"(function() {{
return document.body && (document.body.innerText || '').includes({n});
}})();"#,
n = serde_json::to_string(needle).unwrap()
);
let v = tab.evaluate(&js, false)?;
if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(200));
}
anyhow::bail!("timed out waiting for text: {needle}");
}
fn wait_for_blob_video(tab: &headless_chrome::Tab, timeout: Duration) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
let js = r#"(function() {
let v = document.querySelector('video');
if (!v) return false;
if (typeof v.src !== 'string') return false;
return v.src.startsWith('blob:');
})();"#;
let v = tab.evaluate(js, false)?;
if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(200));
}
anyhow::bail!("timed out waiting for video blob src");
}
#[test]
#[ignore]
fn e2e_remote_website_connects_to_local_direct_publisher() -> anyhow::Result<()> {
if which("ffmpeg").is_none() {
return Ok(()); // skip
}
let chrome = match chrome_path() {
Some(p) => p,
None => return Ok(()), // skip
};
let site_url = std::env::var("EVERY_CHANNEL_SITE_URL")
.unwrap_or_else(|_| "https://every.channel/".to_string());
let ec_node = ec_node_path();
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let tmp = std::env::temp_dir().join(format!("ec-e2e-remote-website-direct-{ts}"));
let _ = std::fs::create_dir_all(&tmp);
let input_ts = tmp.join("input.ts");
let chunk_dir = tmp.join("chunks");
generate_ts_fixture(&input_ts)?;
let mut pub_child = Command::new(&ec_node)
.arg("direct-publish")
.arg("--chunk-dir")
.arg(&chunk_dir)
.arg("--chunk-ms")
.arg("2000")
.arg("--max-segments")
.arg("6")
.arg("ts")
.arg(&input_ts)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()?;
let stdout = pub_child.stdout.take().expect("publisher stdout missing");
let mut lines = BufReader::new(stdout).lines();
let offer = read_line_with_timeout(&mut lines, Duration::from_secs(60))
.ok_or_else(|| anyhow::anyhow!("publisher did not print offer link in time"))?;
if !offer.starts_with("every.channel://direct?c=") {
anyhow::bail!("unexpected offer link: {offer}");
}
let launch_options = headless_chrome::LaunchOptionsBuilder::default()
.path(Some(chrome))
.headless(true)
.args(vec![
OsStr::new("--autoplay-policy=no-user-gesture-required"),
OsStr::new("--mute-audio"),
])
.build()
.unwrap();
let browser = headless_chrome::Browser::new(launch_options)?;
let tab = browser.new_tab()?;
tab.navigate_to(&site_url)?;
tab.wait_until_navigated()?;
// Open the add menu via class selector (stable).
tab.wait_for_element("button.add-source")?.click()?;
tab.wait_for_element(".source-menu")?;
// Use Watch a link flow.
fill_input_by_placeholder(&tab, "every.channel://watch?...", &offer)?;
click_button_by_text(&tab, "Parse link")?;
click_button_by_text(&tab, "Tune in")?;
// Poll for reply link.
let deadline = Instant::now() + Duration::from_secs(60);
let reply = loop {
if let Some(v) = get_reply_link(&tab)? {
if v.starts_with("every.channel://direct?c=") {
break v;
}
}
if Instant::now() > deadline {
anyhow::bail!("timed out waiting for reply link in UI");
}
std::thread::sleep(Duration::from_millis(200));
};
// Feed reply back to publisher.
let stdin = pub_child.stdin.as_mut().expect("publisher stdin missing");
writeln!(stdin, "{reply}")?;
stdin.flush()?;
// Website should go Live and show a blob video source.
wait_for_text(&tab, "Live", Duration::from_secs(60))?;
wait_for_blob_video(&tab, Duration::from_secs(60))?;
// Cleanup.
let _ = pub_child.kill();
let _ = pub_child.wait();
let _ = std::fs::remove_dir_all(&tmp);
Ok(())
}

View file

@ -0,0 +1,243 @@
use std::ffi::OsStr;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
fn which(cmd: &str) -> Option<std::path::PathBuf> {
which::which(cmd).ok()
}
fn chrome_path() -> Option<std::path::PathBuf> {
// Prefer the standard macOS Chrome app bundle.
let mac =
std::path::PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
if mac.exists() {
return Some(mac);
}
which("google-chrome")
.or_else(|| which("google-chrome-stable"))
.or_else(|| which("chromium"))
}
fn ec_node_path() -> std::path::PathBuf {
if let Ok(value) = std::env::var("EC_NODE_BIN") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec_node") {
return value.into();
}
if let Ok(value) = std::env::var("CARGO_BIN_EXE_ec-node") {
return value.into();
}
let exe = std::env::current_exe().expect("current_exe");
let debug_dir = exe
.parent()
.and_then(|p| p.parent())
.expect("expected target/debug/deps");
debug_dir.join("ec-node")
}
fn generate_ts_fixture(out: &std::path::Path) -> anyhow::Result<()> {
// Deterministic-ish fixture: single-threaded x264, fixed GOP, sine audio.
let status = Command::new("ffmpeg")
.arg("-hide_banner")
.arg("-loglevel")
.arg("error")
.arg("-nostdin")
.arg("-y")
.arg("-f")
.arg("lavfi")
.arg("-i")
.arg("testsrc2=size=1280x720:rate=30")
.arg("-f")
.arg("lavfi")
.arg("-i")
.arg("sine=frequency=1000:sample_rate=48000")
.arg("-t")
.arg("12")
.arg("-map")
.arg("0:v:0")
.arg("-map")
.arg("1:a:0")
.arg("-c:v")
.arg("libx264")
.arg("-pix_fmt")
.arg("yuv420p")
.arg("-g")
.arg("60")
.arg("-keyint_min")
.arg("60")
.arg("-sc_threshold")
.arg("0")
.arg("-bf")
.arg("0")
.arg("-threads")
.arg("1")
.arg("-c:a")
.arg("aac")
.arg("-b:a")
.arg("128k")
.arg("-ac")
.arg("2")
.arg("-ar")
.arg("48000")
.arg("-f")
.arg("mpegts")
.arg(out)
.status()?;
if !status.success() {
anyhow::bail!("ffmpeg fixture generation failed with {status}");
}
Ok(())
}
fn click_css(tab: &headless_chrome::Tab, css: &str) -> anyhow::Result<()> {
tab.wait_for_element(css)?.click()?;
Ok(())
}
fn wait_for_text(
tab: &headless_chrome::Tab,
needle: &str,
timeout: Duration,
) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
let js = format!(
r#"(function() {{
return document.body && (document.body.innerText || '').includes({n});
}})();"#,
n = serde_json::to_string(needle).unwrap()
);
let v = tab.evaluate(&js, false)?;
if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(200));
}
anyhow::bail!("timed out waiting for text: {needle}");
}
fn wait_for_blob_video(tab: &headless_chrome::Tab, timeout: Duration) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
let js = r#"(function() {
let v = document.querySelector('video');
if (!v) return false;
if (typeof v.src !== 'string') return false;
return v.src.startsWith('blob:');
})();"#;
let v = tab.evaluate(js, false)?;
if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(200));
}
anyhow::bail!("timed out waiting for video blob src");
}
fn click_global_watch(tab: &headless_chrome::Tab, stream_id: &str) -> anyhow::Result<bool> {
let js = format!(
r#"(function() {{
let target = {sid};
let btn = document.querySelector(`button[data-stream-id="${{target}}"]`)
|| document.querySelector(`button[data_stream_id="${{target}}"]`);
if (!btn) return false;
btn.click();
return true;
}})();"#,
sid = serde_json::to_string(stream_id).unwrap()
);
let v = tab.evaluate(&js, false)?;
Ok(v.value.and_then(|v| v.as_bool()).unwrap_or(false))
}
#[test]
#[ignore]
fn e2e_remote_website_directory_connects_to_local_direct_publisher() -> anyhow::Result<()> {
if which("ffmpeg").is_none() {
return Ok(()); // skip
}
let chrome = match chrome_path() {
Some(p) => p,
None => return Ok(()), // skip
};
let site_url = std::env::var("EVERY_CHANNEL_SITE_URL")
.unwrap_or_else(|_| "https://every.channel/".to_string());
let directory_url = std::env::var("EVERY_CHANNEL_DIRECTORY_URL")
.unwrap_or_else(|_| "https://every.channel".to_string());
let ec_node = ec_node_path();
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let stream_id = format!("every.channel/e2e/{ts}");
let title = format!("E2E {ts}");
let tmp = std::env::temp_dir().join(format!("ec-e2e-remote-website-directory-{ts}"));
let _ = std::fs::create_dir_all(&tmp);
let input_ts = tmp.join("input.ts");
let chunk_dir = tmp.join("chunks");
generate_ts_fixture(&input_ts)?;
let mut pub_child = Command::new(&ec_node)
.arg("direct-publish")
.arg("--directory-url")
.arg(&directory_url)
.arg("--stream-id")
.arg(&stream_id)
.arg("--title")
.arg(&title)
.arg("--chunk-dir")
.arg(&chunk_dir)
.arg("--chunk-ms")
.arg("2000")
.arg("--max-segments")
.arg("6")
.arg("ts")
.arg(&input_ts)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::inherit())
.spawn()?;
let launch_options = headless_chrome::LaunchOptionsBuilder::default()
.path(Some(chrome))
.headless(true)
.args(vec![
OsStr::new("--autoplay-policy=no-user-gesture-required"),
OsStr::new("--mute-audio"),
])
.build()
.unwrap();
let browser = headless_chrome::Browser::new(launch_options)?;
let tab = browser.new_tab()?;
tab.navigate_to(&site_url)?;
tab.wait_until_navigated()?;
// Refresh public list and watch our stream_id.
click_css(&tab, "button[data-testid='global-refresh']")?;
let deadline = Instant::now() + Duration::from_secs(60);
loop {
if click_global_watch(&tab, &stream_id)? {
break;
}
if Instant::now() > deadline {
anyhow::bail!("timed out waiting for stream_id to appear in global list");
}
std::thread::sleep(Duration::from_millis(250));
let _ = click_global_watch(&tab, &stream_id)?;
}
// Website should go Live and show a blob video source.
wait_for_text(&tab, "Live", Duration::from_secs(60))?;
wait_for_blob_video(&tab, Duration::from_secs(60))?;
// Cleanup.
let _ = pub_child.kill();
let _ = pub_child.wait();
let _ = std::fs::remove_dir_all(&tmp);
Ok(())
}

View file

@ -0,0 +1,174 @@
use std::ffi::OsStr;
use std::time::{Duration, Instant};
fn which(cmd: &str) -> Option<std::path::PathBuf> {
which::which(cmd).ok()
}
fn chrome_path() -> Option<std::path::PathBuf> {
let mac =
std::path::PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
if mac.exists() {
return Some(mac);
}
which("google-chrome")
.or_else(|| which("google-chrome-stable"))
.or_else(|| which("chromium"))
}
fn click_css(tab: &headless_chrome::Tab, css: &str) -> anyhow::Result<()> {
tab.wait_for_element(css)?.click()?;
Ok(())
}
fn wait_for_text(
tab: &headless_chrome::Tab,
needle: &str,
timeout: Duration,
) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
let js = format!(
r#"(function() {{
return document.body && (document.body.innerText || '').includes({n});
}})();"#,
n = serde_json::to_string(needle).unwrap()
);
let v = tab.evaluate(&js, false)?;
if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(200));
}
anyhow::bail!("timed out waiting for text: {needle}");
}
fn wait_for_blob_video(tab: &headless_chrome::Tab, timeout: Duration) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
let js = r#"(function() {
let v = document.querySelector('video');
if (!v) return false;
if (typeof v.src !== 'string') return false;
return v.src.startsWith('blob:');
})();"#;
let v = tab.evaluate(js, false)?;
if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(200));
}
anyhow::bail!("timed out waiting for video blob src");
}
fn wait_for_video_element(tab: &headless_chrome::Tab, timeout: Duration) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
let js = r#"(function() {
return !!document.querySelector('video');
})();"#;
let v = tab.evaluate(js, false)?;
if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(200));
}
anyhow::bail!("timed out waiting for <video> element");
}
fn debug_player_state(tab: &headless_chrome::Tab) -> anyhow::Result<String> {
let js = r#"(function() {
let v = document.querySelector('video');
let src = v ? (v.src || '') : null;
let placeholder = document.querySelector('.placeholder');
let placeholderText = placeholder ? (placeholder.innerText || '') : null;
let status = document.querySelector('.source-status');
let statusText = status ? (status.innerText || '') : null;
let sources = Array.from(document.querySelectorAll('button[data-testid="global-watch"]')).length;
return JSON.stringify({ hasVideo: !!v, videoSrc: src, placeholderText, statusText, sources });
})();"#;
let v = tab.evaluate(js, false)?;
Ok(v.value
.and_then(|v| v.as_str().map(|s| s.to_string()))
.unwrap_or_default())
}
fn click_global_watch(tab: &headless_chrome::Tab, stream_id: &str) -> anyhow::Result<bool> {
let js = format!(
r#"(function() {{
let target = {sid};
let btn = document.querySelector(`button[data-stream-id="${{target}}"]`);
if (!btn) return false;
// Some SPA frameworks attach delegated listeners; dispatch a real click event.
btn.dispatchEvent(new MouseEvent('click', {{ bubbles: true, cancelable: true, view: window }}));
return true;
}})();"#,
sid = serde_json::to_string(stream_id).unwrap()
);
let v = tab.evaluate(&js, false)?;
Ok(v.value.and_then(|v| v.as_bool()).unwrap_or(false))
}
#[test]
#[ignore]
fn e2e_remote_website_watch_existing_stream_id() -> anyhow::Result<()> {
let chrome = match chrome_path() {
Some(p) => p,
None => return Ok(()), // skip
};
// We still want ffmpeg around for parity with other E2Es (and to discourage "works only without media tools").
if which("ffmpeg").is_none() {
return Ok(()); // skip
}
let site_url = std::env::var("EVERY_CHANNEL_SITE_URL")
.unwrap_or_else(|_| "https://every.channel/".to_string());
let stream_id = match std::env::var("EVERY_CHANNEL_STREAM_ID") {
Ok(v) if !v.trim().is_empty() => v,
_ => return Ok(()), // skip
};
let launch_options = headless_chrome::LaunchOptionsBuilder::default()
.path(Some(chrome))
.headless(true)
.args(vec![
OsStr::new("--autoplay-policy=no-user-gesture-required"),
OsStr::new("--mute-audio"),
OsStr::new("--disable-application-cache"),
OsStr::new("--disable-service-worker"),
OsStr::new("--disk-cache-size=0"),
])
.build()
.unwrap();
let browser = headless_chrome::Browser::new(launch_options)?;
let tab = browser.new_tab()?;
tab.navigate_to(&site_url)?;
tab.wait_until_navigated()?;
click_css(&tab, "button[data-testid='global-refresh']")?;
let deadline = Instant::now() + Duration::from_secs(60);
loop {
if click_global_watch(&tab, &stream_id)? {
break;
}
if Instant::now() > deadline {
anyhow::bail!("timed out waiting for stream_id to appear in global list");
}
std::thread::sleep(Duration::from_millis(250));
}
// Ensure the player is instantiated.
if let Err(err) = wait_for_video_element(&tab, Duration::from_secs(90)) {
let st = debug_player_state(&tab).unwrap_or_default();
anyhow::bail!("{err}\nplayer_state={st}");
}
// We consider playback "started" when the video uses a blob: URL (MSE).
if let Err(err) = wait_for_blob_video(&tab, Duration::from_secs(90)) {
let st = debug_player_state(&tab).unwrap_or_default();
anyhow::bail!("{err}\nplayer_state={st}");
}
Ok(())
}