mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 06:53:19 -10:00
A <Video> whose src is an http(s) URL plays it, fetched once into raea's URL cache (closes #57)
core/scene's media_path joined an https src to the composition's folder, so a remote clip was refused as "<dir>/https:/…: no video track" (seven snapcn components in #13: hero-launch, laptop-frame, phone-frame, phone-stack, reel-collage, reel-wall, screen-recording). raea_io_net::cache::remote(src) gives an http(s) src's file through cache::fetch (keyed by the URL, which is in the composition's source, so a render stays a function of its code; under $RAEA_CACHE beside the render cache) and None for a file. The page's video elements, the export's sounds and live play's video list ask it first. A src that can't be fetched refuses the frame saying why: "video <url>: HTTP 404 Not Found", or "(not in raea's cache): not sent, because RAEA_OFFLINE is set". The cache's status errors no longer name the URL twice. bench/remote-media/check.sh: a local server's red clip plays (centre 252,0,0), plays again offline from the cache, an uncached URL offline refuses, a 404 refuses with its status. Before: all four read the URL as a path. laptop-frame (snapcn) renders its remote clip, 240 frames at 7.0 ms a frame.
This commit is contained in:
1 parent
a3db7b6dba
commit
ffd2738dcc
9 files changed
+99
-6
No files matched your search
Generated
+3
@@ -14743,6 +14743,7 @@ dependencies = [
|
||||
"raea-gpu-device",
|
||||
"raea-io-assets",
|
||||
"raea-io-media",
|
||||
"raea-io-net",
|
||||
"raea-page",
|
||||
"raea-render",
|
||||
"raea-runtime-bake",
|
||||
@@ -14829,6 +14830,7 @@ dependencies = [
|
||||
"image",
|
||||
"raea-core-program",
|
||||
"raea-gpu-device",
|
||||
"raea-io-net",
|
||||
"raea-io-trace",
|
||||
"raea-render",
|
||||
"raea-runtime-bake",
|
||||
@@ -14865,6 +14867,7 @@ dependencies = [
|
||||
"raea-io-assets",
|
||||
"raea-io-home",
|
||||
"raea-io-media",
|
||||
"raea-io-net",
|
||||
"raea-page",
|
||||
"raea-render",
|
||||
"raea-runtime-input",
|
||||
|
||||
Executable
+60
@@ -0,0 +1,60 @@
|
||||
#!/bin/bash
|
||||
# #57: a <Video> whose src is an http(s) URL plays that clip, fetched once into raea's URL cache (raea-io-net's
|
||||
# cache.rs, keyed by the URL), never read as a path beside the composition ("<dir>/https:/…: no video track"). A
|
||||
# local server serves a red clip; the render's centre must be red, a second render (offline, RAEA_OFFLINE=1) must play it
|
||||
# from the cache, an uncached URL offline must refuse saying so, and a 404 must refuse with its status. Exits 1 on a
|
||||
# failure.
|
||||
# bench/remote-media/check.sh (RAEA_BIN: the raea to run, default target/release/raea)
|
||||
cd "$(dirname "$0")/../.." || exit 2
|
||||
BIN=${RAEA_BIN:-$PWD/target/release/raea}
|
||||
export RAEA_SIGN=0 RAEA_MARK=0 RAEA_EXPORT_CACHE=0
|
||||
OUT=$(mktemp -d "${TMPDIR:-/tmp}/raea-remote-media.XXXXXX")
|
||||
export RAEA_CACHE=$OUT/cache
|
||||
mkdir -p "$OUT/www" "$OUT/comp"
|
||||
ffmpeg -loglevel error -f lavfi -i color=c=red:s=64x64:r=30 -t 1 -pix_fmt yuv420p "$OUT/www/red.mp4" || { echo "FAIL: ffmpeg"; exit 2; }
|
||||
PORT=$(python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')
|
||||
python3 -m http.server "$PORT" --bind 127.0.0.1 --directory "$OUT/www" > "$OUT/http.log" 2>&1 &
|
||||
SRV=$!
|
||||
trap 'kill $SRV 2>/dev/null; wait $SRV 2>/dev/null; rm -rf "$OUT"' EXIT
|
||||
for _ in $(seq 1 100); do curl -sf -o /dev/null "http://127.0.0.1:$PORT/red.mp4" && break; perl -e 'select(undef,undef,undef,0.05)'; done
|
||||
comp() { # NAME URL
|
||||
cat > "$OUT/comp/$1.tsx" <<TSX
|
||||
import { AbsoluteFill, Composition, Video, registerRoot } from "raea";
|
||||
const Shot = () => (
|
||||
<AbsoluteFill style={{ backgroundColor: "#000" }}>
|
||||
<Video src="$2" style={{ position: "absolute", left: 0, top: 0, width: 160, height: 160, objectFit: "cover" }} />
|
||||
</AbsoluteFill>
|
||||
);
|
||||
registerRoot(() => <Composition id="remote" component={Shot} width={160} height={160} fps={30} durationInFrames={2} />);
|
||||
TSX
|
||||
}
|
||||
status=0
|
||||
red() { # PNG: the centre's red and green
|
||||
magick "$1" -format "%[fx:int(255*p{80,80}.r+0.5)] %[fx:int(255*p{80,80}.g+0.5)]" info:
|
||||
}
|
||||
plays() { # NAME WHAT
|
||||
rm -f "$OUT/$1.mp4" "$OUT/$1.png"
|
||||
"$BIN" render "$OUT/comp/$1.tsx" -o "$OUT/$1.mp4" > "$OUT/$1.log" 2>&1 || { echo "FAIL $2: the render failed"; sed 's/^/ /' "$OUT/$1.log" | tail -8; status=1; return; }
|
||||
local png=$OUT/$1.png
|
||||
ffmpeg -loglevel error -i "$OUT/$1.mp4" -frames:v 1 -y "$png" && [ -f "$png" ] || { echo "FAIL $2: no frame"; status=1; return; }
|
||||
read -r r g <<< "$(red "$png")"
|
||||
(( r > 200 && g < 60 )) && echo "ok $2 (centre $r,$g)" || { echo "FAIL $2: centre $r,$g, wanted red"; status=1; }
|
||||
}
|
||||
refuses() { # NAME WANT WHAT
|
||||
if "$BIN" render "$OUT/comp/$1.tsx" -o "$OUT/$1.mp4" > "$OUT/$1.log" 2>&1; then echo "FAIL $3: rendered"; status=1; return; fi
|
||||
if grep -q "no video track" "$OUT/$1.log"; then echo "FAIL $3: read the URL as a path"; sed 's/^/ /' "$OUT/$1.log" | tail -4; status=1; return; fi
|
||||
grep -q "$2" "$OUT/$1.log" && echo "ok $3" || { echo "FAIL $3: no \"$2\" in its refusal"; sed 's/^/ /' "$OUT/$1.log" | tail -4; status=1; }
|
||||
}
|
||||
comp live "http://127.0.0.1:$PORT/red.mp4"
|
||||
plays live "an http src plays"
|
||||
kill $SRV; wait $SRV 2>/dev/null
|
||||
RAEA_OFFLINE=1 plays live "the same src, offline, from the cache"
|
||||
comp uncached "http://127.0.0.1:$PORT/uncached.mp4"
|
||||
RAEA_OFFLINE=1 refuses uncached "not in raea's cache" "an uncached src offline refuses"
|
||||
python3 -m http.server "$PORT" --bind 127.0.0.1 --directory "$OUT/www" > "$OUT/http.log" 2>&1 &
|
||||
SRV=$!
|
||||
for _ in $(seq 1 100); do curl -sf -o /dev/null "http://127.0.0.1:$PORT/red.mp4" && break; perl -e 'select(undef,undef,undef,0.05)'; done
|
||||
comp gone "http://127.0.0.1:$PORT/gone.mp4"
|
||||
refuses gone "404" "a 404 refuses with its status"
|
||||
[ $status = 0 ] && echo "PASS remote media srcs"
|
||||
exit $status
|
||||
+12
-1
@@ -19,6 +19,13 @@ fn fnv(s: &str) -> String {
|
||||
format!("{h:016x}")
|
||||
}
|
||||
|
||||
/// A media src (a <Video>'s, a sound's) that names an http(s) URL: Some(its file, fetched once into the cache, or why
|
||||
/// not: offline and not cached, an HTTP status), None when it names a file. The URL is the cache key, and it is in the
|
||||
/// composition's source, so a render that names it stays a function of its code.
|
||||
pub fn remote(src: &str) -> Option<Result<PathBuf, String>> {
|
||||
(src.starts_with("http://") || src.starts_with("https://")).then(|| fetch(src))
|
||||
}
|
||||
|
||||
/// An http(s) URL's bytes, from the cache or fetched into it; the cached file's path.
|
||||
pub fn fetch(url: &str) -> Result<PathBuf, String> {
|
||||
let google = url.starts_with("https://fonts.googleapis.com/");
|
||||
@@ -43,7 +50,11 @@ pub fn fetch(url: &str) -> Result<PathBuf, String> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let agent = ureq::AgentBuilder::new().redirects(10).timeout(Duration::from_secs(300)).build();
|
||||
let r = agent.get(url).set("User-Agent", if google { CHROME_UA } else { PLAIN_UA }).call().map_err(|e| format!("{url}: {e}"))?;
|
||||
let r = agent.get(url).set("User-Agent", if google { CHROME_UA } else { PLAIN_UA }).call().map_err(|e| match e {
|
||||
// ureq's message names the URL again: the status alone
|
||||
ureq::Error::Status(code, r) => format!("{url}: HTTP {code} {}", r.status_text()),
|
||||
e => format!("{url}: {e}"),
|
||||
})?;
|
||||
let mut bytes = Vec::new();
|
||||
std::io::Read::read_to_end(&mut r.into_reader(), &mut bytes).map_err(|e| format!("{url}: {e}"))?;
|
||||
if bytes.is_empty() {
|
||||
|
||||
@@ -19,6 +19,7 @@ raea-runtime-encode = { path = "../encode" }
|
||||
raea-page = { path = "../../engine/page" }
|
||||
raea-sys-page = { path = "../../systems/page" }
|
||||
raea-gpu-device = { path = "../../gpu/device" }
|
||||
raea-io-net = { path = "../../io/net" }
|
||||
raea-io-media = { path = "../../io/media", default-features = false, features = ["soft-audio", "apple"] }
|
||||
raea-core-program = { path = "../../core/program" }
|
||||
raea-core-scene = { path = "../../core/scene" }
|
||||
|
||||
@@ -182,7 +182,7 @@ fn push_sound(cx: &mut Ctx, props: &Map<String, Value>, skey: &str) {
|
||||
// linear gain, any non-negative (above 1 boosts); data-gain-db, when the element carries one, wins
|
||||
let volume = num(props, "data-gain-db").map(|db| 10f64.powf(db / 20.0)).or_else(|| num(props, "data-volume")).unwrap_or(1.0);
|
||||
if let (Some(src), Some(at)) = (props.get("src").and_then(|v| v.as_str()), num(props, "data-source-time")) {
|
||||
let file = media_path(cx.dir, src);
|
||||
let Some(file) = raea_io_net::cache::remote(src).map_or_else(|| Some(media_path(cx.dir, src)), Result::ok) else { return }; // http(s): raea's URL cache (#57)
|
||||
// a sound track with samples: a short window says so (decoding the whole take for it held a long take whole)
|
||||
let audible = *cx.audible.entry(file.clone()).or_insert_with(|| raea_io_media::decode_audio_window(&file, 0.0, 0.05).is_ok_and(|a| a.len() > 0));
|
||||
if !muted && volume > 0.0 && audible {
|
||||
|
||||
@@ -14,6 +14,7 @@ raea-runtime-export = { path = "../export" }
|
||||
raea-runtime-bake = { path = "../bake" }
|
||||
raea-render = { path = "../../engine/render", default-features = false, features = ["native"] }
|
||||
raea-gpu-device = { path = "../../gpu/device" }
|
||||
raea-io-net = { path = "../../io/net" }
|
||||
raea-io-trace = { path = "../../io/trace" }
|
||||
raea-core-program = { path = "../../core/program" }
|
||||
raea-sys-mix = { path = "../../systems/mix" }
|
||||
|
||||
@@ -195,7 +195,13 @@ impl Live {
|
||||
}
|
||||
let Some(src) = p.0.get("src").and_then(Value::as_str) else { continue };
|
||||
let at = p.0.get("data-source-time").and_then(|v| v.as_f64().or_else(|| v.as_str().and_then(|s| s.parse().ok()))).unwrap_or(0.0);
|
||||
out.push((raea_runtime_react::scene::media_path(&self.dir, src), at));
|
||||
// an http(s) src from raea's URL cache (#57)
|
||||
let file = match raea_io_net::cache::remote(src) {
|
||||
None => raea_runtime_react::scene::media_path(&self.dir, src),
|
||||
Some(Ok(f)) => f,
|
||||
Some(Err(_)) => continue,
|
||||
};
|
||||
out.push((file, at));
|
||||
}
|
||||
}
|
||||
out
|
||||
|
||||
@@ -10,6 +10,7 @@ description = "A composition's page in Bevy: React's elements laid out and paint
|
||||
raea-runtime-view = { path = "../view" }
|
||||
raea-sys-frame = { path = "../../systems/frame" }
|
||||
raea-io-assets = { path = "../../io/assets" }
|
||||
raea-io-net = { path = "../../io/net" }
|
||||
raea-io-home = { path = "../../io/home" }
|
||||
raea-runtime-react = { path = "../../runtime/react", default-features = false }
|
||||
raea-render = { path = "../../engine/render", default-features = false, features = ["native"] }
|
||||
|
||||
@@ -128,9 +128,19 @@ pub(crate) fn walk(react: &World, e: Entity, parent: u64, at: At, dir: &Path, ho
|
||||
let src = (kind == "video").then(|| props.and_then(|p| p.get("src")).and_then(Value::as_str)).flatten();
|
||||
let me = if let Some(src) = src {
|
||||
let at_s = props.and_then(|p| p.get("data-source-time")).and_then(|v| v.as_f64().or_else(|| v.as_str().and_then(|s| s.parse().ok()))).unwrap_or(0.0);
|
||||
let file = raea_runtime_react::scene::media_path(dir, src);
|
||||
let fit = video_fit(props, &file, at_s);
|
||||
videos.push(VideoAt { id, file, at: at_s, fit });
|
||||
// an http(s) src is fetched once into raea's URL cache (#57), never read as a path beside the composition
|
||||
let file = match raea_io_net::cache::remote(src) {
|
||||
None => Some(raea_runtime_react::scene::media_path(dir, src)),
|
||||
Some(Ok(f)) => Some(f),
|
||||
Some(Err(e)) => {
|
||||
REFUSED.with(|c| c.borrow_mut().push(format!("video {e}")));
|
||||
None
|
||||
}
|
||||
};
|
||||
let fit = file.as_ref().and_then(|f| video_fit(props, f, at_s));
|
||||
if let Some(file) = file {
|
||||
videos.push(VideoAt { id, file, at: at_s, fit });
|
||||
}
|
||||
let mut page = page_props(kind, props, dir);
|
||||
// a framed video shows its crop, whose aspect is its box's, stretched to the box
|
||||
if fit.is_some() {
|
||||
|
||||
Reference in new issue
Block a user