Fix hosted live playback
This commit is contained in:
parent
340e2346ba
commit
6739b424ab
4 changed files with 142 additions and 72 deletions
|
|
@ -16,33 +16,6 @@ fn chrome_path() -> Option<std::path::PathBuf> {
|
|||
.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 {
|
||||
|
|
@ -80,12 +53,16 @@ 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 currentTime = v ? v.currentTime : null;
|
||||
let muted = v ? v.muted : null;
|
||||
let readyState = v ? v.readyState : null;
|
||||
let buffered = v ? Array.from({ length: v.buffered.length }, (_, i) => [v.buffered.start(i), v.buffered.end(i)]) : [];
|
||||
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 });
|
||||
return JSON.stringify({ hasVideo: !!v, videoSrc: src, currentTime, muted, readyState, buffered, placeholderText, statusText, sources });
|
||||
})();"#;
|
||||
let v = tab.evaluate(js, false)?;
|
||||
Ok(v.value
|
||||
|
|
@ -93,20 +70,84 @@ fn debug_player_state(tab: &headless_chrome::Tab) -> anyhow::Result<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()
|
||||
);
|
||||
fn video_motion_sample(tab: &headless_chrome::Tab) -> anyhow::Result<Option<(f64, u32)>> {
|
||||
let js = r#"(function() {
|
||||
let v = document.querySelector('video');
|
||||
if (!v || !v.videoWidth || !v.videoHeight) return null;
|
||||
let canvas = window.__ec_motion_canvas || (window.__ec_motion_canvas = document.createElement('canvas'));
|
||||
canvas.width = 160;
|
||||
canvas.height = 90;
|
||||
let ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
ctx.drawImage(v, 0, 0, canvas.width, canvas.height);
|
||||
let data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
|
||||
let hash = 2166136261 >>> 0;
|
||||
for (let i = 0; i < data.length; i += 16) {
|
||||
hash ^= data[i]; hash = Math.imul(hash, 16777619);
|
||||
hash ^= data[i + 1]; hash = Math.imul(hash, 16777619);
|
||||
hash ^= data[i + 2]; hash = Math.imul(hash, 16777619);
|
||||
}
|
||||
return JSON.stringify({ currentTime: v.currentTime, hash: hash >>> 0 });
|
||||
})();"#;
|
||||
let v = tab.evaluate(&js, false)?;
|
||||
Ok(v.value.and_then(|v| v.as_bool()).unwrap_or(false))
|
||||
let Some(s) = v.value.and_then(|v| v.as_str().map(|s| s.to_string())) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let value: serde_json::Value = serde_json::from_str(&s)?;
|
||||
let current_time = value
|
||||
.get("currentTime")
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or_default();
|
||||
let hash = value
|
||||
.get("hash")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or_default() as u32;
|
||||
Ok(Some((current_time, hash)))
|
||||
}
|
||||
|
||||
fn wait_for_video_motion(tab: &headless_chrome::Tab, timeout: Duration) -> anyhow::Result<()> {
|
||||
let deadline = Instant::now() + timeout;
|
||||
let mut first: Option<(f64, u32)> = None;
|
||||
while Instant::now() < deadline {
|
||||
if let Some(sample) = video_motion_sample(tab)? {
|
||||
if let Some((first_time, first_hash)) = first {
|
||||
if sample.0 > first_time + 0.5 && sample.1 != first_hash {
|
||||
return Ok(());
|
||||
}
|
||||
} else {
|
||||
first = Some(sample);
|
||||
}
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
}
|
||||
let st = debug_player_state(tab).unwrap_or_default();
|
||||
anyhow::bail!("timed out waiting for changing video frames\nplayer_state={st}");
|
||||
}
|
||||
|
||||
fn wait_for_unmuted_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');
|
||||
return !!v && v.muted === false && v.volume > 0;
|
||||
})();"#;
|
||||
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));
|
||||
}
|
||||
let st = debug_player_state(tab).unwrap_or_default();
|
||||
anyhow::bail!("timed out waiting for unmuted video\nplayer_state={st}");
|
||||
}
|
||||
|
||||
fn watch_url(site_url: &str, relay_url: &str, stream_id: &str) -> anyhow::Result<String> {
|
||||
let mut url = url::Url::parse(site_url)?;
|
||||
url.set_path("/watch");
|
||||
url.query_pairs_mut()
|
||||
.clear()
|
||||
.append_pair("url", relay_url)
|
||||
.append_pair("name", stream_id);
|
||||
Ok(url.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -123,6 +164,8 @@ fn e2e_remote_website_watch_existing_stream_id() -> anyhow::Result<()> {
|
|||
|
||||
let site_url = std::env::var("EVERY_CHANNEL_SITE_URL")
|
||||
.unwrap_or_else(|_| "https://every.channel/".to_string());
|
||||
let relay_url = std::env::var("EVERY_CHANNEL_RELAY_URL")
|
||||
.unwrap_or_else(|_| "https://cdn.moq.dev/anon".to_string());
|
||||
let stream_id = match std::env::var("EVERY_CHANNEL_STREAM_ID") {
|
||||
Ok(v) if !v.trim().is_empty() => v,
|
||||
_ => return Ok(()), // skip
|
||||
|
|
@ -133,7 +176,6 @@ fn e2e_remote_website_watch_existing_stream_id() -> anyhow::Result<()> {
|
|||
.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"),
|
||||
|
|
@ -142,22 +184,9 @@ fn e2e_remote_website_watch_existing_stream_id() -> anyhow::Result<()> {
|
|||
.unwrap();
|
||||
let browser = headless_chrome::Browser::new(launch_options)?;
|
||||
let tab = browser.new_tab()?;
|
||||
tab.navigate_to(&site_url)?;
|
||||
tab.navigate_to(&watch_url(&site_url, &relay_url, &stream_id)?)?;
|
||||
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();
|
||||
|
|
@ -170,5 +199,9 @@ fn e2e_remote_website_watch_existing_stream_id() -> anyhow::Result<()> {
|
|||
anyhow::bail!("{err}\nplayer_state={st}");
|
||||
}
|
||||
|
||||
tab.wait_for_element("video")?.click()?;
|
||||
wait_for_unmuted_video(&tab, Duration::from_secs(10))?;
|
||||
wait_for_video_motion(&tab, Duration::from_secs(30))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue