Skip to content

armgit / raea

Public Imported from GitHub
0

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.

Page media in its own crate, raea-runtime-dom-media (runtime/dom/media), so raea-runtime-dom stays under the crate limit (L1 size: 2049 lines); its copies folded

shinyobjectzd3e292205812
diff --git a/Cargo.lock b/Cargo.lock
index a9936764..82f40edc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14698,14 +14698,27 @@ dependencies = [
"keyboard-types",
"raea-core-wire",
"raea-io-net",
+ "raea-runtime-dom-media",
"raea-runtime-js",
"raea-runtime-twins",
"raea-sys-canvas",
- "raea-sys-image",
"raea-sys-page",
"serde_json",
"stylo",
"url",
+]
+
+[[package]]
+name = "raea-runtime-dom-media"
+version = "0.1.0"
+dependencies = [
+ "bevy",
+ "blitz-dom",
+ "raea-io-net",
+ "raea-runtime-js",
+ "raea-sys-image",
+ "raea-sys-page",
+ "serde_json",
"vello",
]
diff --git a/Cargo.toml b/Cargo.toml
index 7ff80e7d..153e237c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -118,6 +118,7 @@ members = [
"systems/image",
"runtime/twins",
"runtime/dom",
+ "runtime/dom/media",
"runtime/edit",
"runtime/view",
"runtime/encode",
diff --git a/runtime/dom/Cargo.toml b/runtime/dom/Cargo.toml
index bd2abf49..3b002c61 100644
--- a/runtime/dom/Cargo.toml
+++ b/runtime/dom/Cargo.toml
@@ -9,7 +9,6 @@ description = "TSX roots on a Blitz page: their scripts, ops, events, timers, if
[dependencies]
# the layers under it
raea-sys-page = { path = "../../systems/page" }
-raea-sys-image = { path = "../../systems/image" }
raea-runtime-twins = { path = "../../runtime/twins" }
bevy = { version = "=0.20.0", default-features = false, features = ["std", "default_app", "bevy_render", "bevy_asset", "bevy_image", "bevy_log", "bevy_window", "ui_bevy_render", "bevy_picking", "ui_picking", "bevy_input_focus"] }
# the document: stylo's styles, Taffy's and Blitz's layout (floats on, as Chrome lays them out)
@@ -17,9 +16,10 @@ blitz-dom = { version = "=0.3.0-beta.4", default-features = false, features = ["
blitz-traits = "=0.3.0-beta.4"
# keys as Blitz takes them (host.rs WindowInput: a window's KeyboardInput as DOM keys and codes), blitz-traits' own
keyboard-types = "0.8"
-vello = { version = "=0.11.0", default-features = false, features = ["wgpu"] }
# a page's script host: JS engines, native calls, media, canvas, accessibility records
raea-runtime-js = { path = "../js" }
+# the page's video and audio elements (runtime/dom/media)
+raea-runtime-dom-media = { path = "media" }
# a canvas element's bitmap, drawn from its display list
raea-sys-canvas = { path = "../../systems/canvas" }
# the op wire, and the element tree raea's page diffs into ops (motion.rs)
diff --git a/runtime/dom/media/Cargo.toml b/runtime/dom/media/Cargo.toml
new file mode 100644
index 00000000..3082863b
--- /dev/null
+++ b/runtime/dom/media/Cargo.toml
@@ -0,0 +1,17 @@
+[package]
+name = "raea-runtime-dom-media"
+version = "0.1.0"
+edition = "2024"
+license = "FSL-1.1-ALv2"
+publish = false
+description = "The page's video and audio elements: each source decoded and played on its own thread (raea-runtime-js's stream), its news as the element's media events, a video's frame as its picture"
+
+[dependencies]
+bevy = { version = "=0.20.0", default-features = false, features = ["std", "bevy_log"] }
+blitz-dom = { version = "=0.3.0-beta.4", default-features = false, features = ["svg", "woff", "system-fonts", "floats", "text-transform-icu", "complex-scripts"] }
+raea-io-net = { path = "../../../io/net" }
+raea-runtime-js = { path = "../../js" }
+raea-sys-image = { path = "../../../systems/image" }
+raea-sys-page = { path = "../../../systems/page" }
+serde_json = "1"
+vello = { version = "=0.11.0", default-features = false, features = ["wgpu"] }
diff --git a/runtime/dom/src/media.rs b/runtime/dom/media/src/lib.rs
similarity index 92%
rename from runtime/dom/src/media.rs
rename to runtime/dom/media/src/lib.rs
index 69ba64b8..8434b59d 100644
--- a/runtime/dom/src/media.rs
+++ b/runtime/dom/media/src/lib.rs
@@ -5,14 +5,15 @@
//! A seek is the prop `__seek` ([time, n], set by the dom layer's `currentTime`); playing is `__play` ([on, time, n,
//! loop], set by `play()` and `pause()`), and `__muted` the element's muted. Playing, the decoder's frames come on the
//! source's clock (its sound's: raea-runtime-js media_play.rs), each a `time` news for the element's currentTime, with
-//! `timeupdate` four times a second as a browser's, `playing` once it's going, and `ended` at its end.
+//! `timeupdate` four times a second as a browser's, `playing` once it's going, and `ended` at its end. raea-runtime-dom's
+//! page host keeps one `Medias` and syncs it after each commit.
use std::collections::HashMap;
use std::sync::Arc;
use blitz_dom::BaseDocument;
use blitz_dom::node::{ImageData, RasterImageData, SpecialElementData};
-use raea_runtime_js::media_html::stream::{Msg, Stream};
+use raea_runtime_js::media_html::stream::{Cmd, Msg, Stream};
use serde_json::{Value, json};
use raea_sys_page::ops::Nodes;
@@ -42,6 +43,13 @@ pub struct Media {
told: Option<std::time::Instant>,
}
+impl Media {
+ /// Whether `t` is where the seek asked for landed (it's then asked no more).
+ fn landed(&mut self, t: f64) -> bool {
+ self.seeking.take_if(|s| (*s - t).abs() < 1e-9).is_some()
+ }
+}
+
/// Each media element by its wire id.
pub type Medias = HashMap<u64, Media>;
@@ -74,7 +82,7 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mu
let muted = props.and_then(|p| p.get("__muted")).and_then(Value::as_bool);
if m.muted != muted {
if let (Some(v), Some(st)) = (muted, m.stream.as_mut()) {
- st.muted(v);
+ st.ask(Cmd::Muted(v));
}
m.muted = muted;
}
@@ -83,7 +91,7 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mu
&& let Some(st) = m.stream.as_mut()
{
m.seeking = Some(t);
- st.seek(t);
+ st.ask(Cmd::Seek(t));
}
m.seek = seek;
}
@@ -92,9 +100,9 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mu
if let (Some(p), Some(st)) = (play.as_ref(), m.stream.as_mut()) {
m.playing = p.get(0).and_then(Value::as_bool).unwrap_or(false);
if m.playing {
- st.play(p.get(1).and_then(Value::as_f64).unwrap_or(0.0), p.get(3).and_then(Value::as_bool).unwrap_or(false));
+ st.ask(Cmd::Play { at: p.get(1).and_then(Value::as_f64).unwrap_or(0.0), looping: p.get(3).and_then(Value::as_bool).unwrap_or(false) });
} else {
- st.pause();
+ st.ask(Cmd::Pause);
}
}
m.play = play;
@@ -117,7 +125,7 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mu
m.loaded = true;
out.push(json!({"id": id, "type": "loadeddata", "w": w, "h": h, "duration": m.duration}));
}
- if m.seeking.take_if(|s| (*s - t).abs() < 1e-9).is_some() {
+ if m.landed(t) {
m.seeked = true;
out.push(json!({"id": id, "type": "seeked", "time": t}));
}
@@ -127,7 +135,7 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mu
}
}
Msg::Time(t) => {
- if m.seeking.take_if(|s| (*s - t).abs() < 1e-9).is_some() {
+ if m.landed(t) {
out.push(json!({"id": id, "type": "seeked", "time": t}));
}
if m.playing {
diff --git a/runtime/dom/src/lib.rs b/runtime/dom/src/lib.rs
index c056ce04..efead279 100644
--- a/runtime/dom/src/lib.rs
+++ b/runtime/dom/src/lib.rs
@@ -1,6 +1,6 @@
//! raea-runtime-dom: TSX roots on a Blitz page (host.rs: each root's JS, its ops applied, its events, timers, iframes
-//! and host calls), getComputedStyle (computed.rs) and a page's video and audio elements (media.rs).
+//! and host calls), getComputedStyle (computed.rs) and a page's video and audio elements (raea-runtime-dom-media).
pub mod computed;
pub mod host;
-pub mod media;
+pub use raea_runtime_dom_media as media;
diff --git a/runtime/js/src/media_html.rs b/runtime/js/src/media_html.rs
index 8cadd95f..ebe443d1 100644
--- a/runtime/js/src/media_html.rs
+++ b/runtime/js/src/media_html.rs
@@ -22,8 +22,9 @@ pub mod stream {
Fail(String),
}
- /// What the element asks of its decoder.
- pub(crate) enum Cmd {
+ /// What the element asks of its decoder. Play plays from `at` on the source's clock (its sound's, when it has
+ /// sound and a device): frames as they fall due, each `Frame` news, and `Ended` at its end unless `looping`.
+ pub enum Cmd {
Seek(f64),
Play { at: f64, looping: bool },
Pause,
@@ -36,22 +37,9 @@ pub mod stream {
}
impl Stream {
- pub fn seek(&mut self, t: f64) {
- let _ = self.tx.send(Cmd::Seek(t));
- }
-
- /// Plays from `at` on the source's clock (its sound's, when it has sound and a device): frames as they fall due,
- /// each `Frame` news, and `Ended` at its end unless `looping`.
- pub fn play(&mut self, at: f64, looping: bool) {
- let _ = self.tx.send(Cmd::Play { at, looping });
- }
-
- pub fn pause(&mut self) {
- let _ = self.tx.send(Cmd::Pause);
- }
-
- pub fn muted(&mut self, muted: bool) {
- let _ = self.tx.send(Cmd::Muted(muted));
+ /// Asks the decoder to seek, play, pause or mute; a decoder that has stopped hears nothing.
+ pub fn ask(&mut self, c: Cmd) {
+ let _ = self.tx.send(c);
}
/// The decoder's next news, if it has any now.
diff --git a/runtime/js/src/media_play.rs b/runtime/js/src/media_play.rs
index ba5846e3..c1bdffaa 100644
--- a/runtime/js/src/media_play.rs
+++ b/runtime/js/src/media_play.rs
@@ -192,7 +192,7 @@ impl<'a> Player<'a> {
#[cfg(test)]
mod tests {
- use crate::media_html::stream::{self, Msg, Stream};
+ use crate::media_html::stream::{self, Cmd, Msg, Stream};
use std::time::{Duration, Instant};
/// The next news within `within`, polled as the page host polls it (each frame).
@@ -230,7 +230,7 @@ mod tests {
assert!(matches!(next(&st, Duration::from_secs(5)), Some(Msg::Frame(t, _)) if t == 0.0));
let began = Instant::now();
- st.play(0.0, false);
+ st.ask(Cmd::Play { at: 0.0, looping: false });
assert!(matches!(next(&st, Duration::from_secs(1)), Some(Msg::Playing(t)) if t == 0.0));
let (mut frames, mut last, mut ended) = (0, -1.0, None);
while let Some(m) = next(&st, Duration::from_secs(3)) {
@@ -252,10 +252,10 @@ mod tests {
assert!((0.85..1.4).contains(&took), "a second's clip took {took} s");
assert!(frames >= 20, "{frames} frames of 30");
- st.seek(0.5);
+ st.ask(Cmd::Seek(0.5));
assert!(matches!(next(&st, Duration::from_secs(2)), Some(Msg::Frame(t, _)) if t == 0.5));
- st.play(0.5, true);
+ st.ask(Cmd::Play { at: 0.5, looping: true });
let (mut wrapped, mut last, until) = (false, 0.5, Instant::now() + Duration::from_millis(1200));
while Instant::now() < until {
match next(&st, Duration::from_millis(200)) {
@@ -269,7 +269,7 @@ mod tests {
}
assert!(wrapped, "looping went round");
- st.pause();
+ st.ask(Cmd::Pause);
let mut after = 0;
while let Some(m) = next(&st, Duration::from_millis(300)) {
if matches!(m, Msg::Frame(..)) {
diff --git a/tools/layers.toml b/tools/layers.toml
index 8274b5fa..df93ed20 100644
--- a/tools/layers.toml
+++ b/tools/layers.toml
@@ -116,6 +116,7 @@ raea-sys-fonts = "systems"
raea-sys-image = "systems"
raea-runtime-twins = "runtime"
raea-runtime-dom = "runtime"
+raea-runtime-dom-media = "runtime"
raea-runtime-edit = "runtime"
raea-runtime-input = "runtime"
raea-runtime-bake = "runtime"