Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
A <Video> whose src is an http(s) URL plays it, fetched once into raea's URL cache (closes #57)
shinyobjectzffd2738dccdb
diff --git a/Cargo.lock b/Cargo.lockindex 5a743c9c..e5204efb 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -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",diff --git a/bench/remote-media/check.sh b/bench/remote-media/check.shnew file mode 100755index 00000000..8779a797--- /dev/null+++ b/bench/remote-media/check.sh@@ -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 $statusdiff --git a/io/net/src/cache.rs b/io/net/src/cache.rsindex cae4d934..cff6e81f 100644--- a/io/net/src/cache.rs+++ b/io/net/src/cache.rs@@ -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() {diff --git a/runtime/export/Cargo.toml b/runtime/export/Cargo.tomlindex 57c91912..35d2a02d 100644--- a/runtime/export/Cargo.toml+++ b/runtime/export/Cargo.toml@@ -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" }diff --git a/runtime/export/src/lib.rs b/runtime/export/src/lib.rsindex 53126cb8..df40af48 100644--- a/runtime/export/src/lib.rs+++ b/runtime/export/src/lib.rs@@ -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, winslet 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 {diff --git a/runtime/live/Cargo.toml b/runtime/live/Cargo.tomlindex b8bc7272..3acb718b 100644--- a/runtime/live/Cargo.toml+++ b/runtime/live/Cargo.toml@@ -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" }diff --git a/runtime/live/src/open.rs b/runtime/live/src/open.rsindex 364f3da8..9243636a 100644--- a/runtime/live/src/open.rs+++ b/runtime/live/src/open.rs@@ -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));}}outdiff --git a/runtime/page/Cargo.toml b/runtime/page/Cargo.tomlindex 63f8f1b9..238e4542 100644--- a/runtime/page/Cargo.toml+++ b/runtime/page/Cargo.toml@@ -10,6 +10,7 @@ description = "A composition's page in Bevy: React's elements laid out and paintraea-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"] }diff --git a/runtime/page/src/elements.rs b/runtime/page/src/elements.rsindex 092447e2..75b0a97b 100644--- a/runtime/page/src/elements.rs+++ b/runtime/page/src/elements.rs@@ -128,9 +128,19 @@ pub(crate) fn walk(react: &World, e: Entity, parent: u64, at: At, dir: &Path, holet 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 boxif fit.is_some() {