|
| 1 | +//! Mascot-as-webcam pipeline. |
| 2 | +//! |
| 3 | +//! Once at app startup we rasterize the OpenHuman mascot SVG into a |
| 4 | +//! 640×480 RGBA bitmap, convert it to YUV420, and write a single-frame |
| 5 | +//! YUV4MPEG2 (Y4M) file to the per-user data directory. The file is |
| 6 | +//! cached across launches keyed by source-SVG hash so subsequent boots |
| 7 | +//! skip the rasterization. |
| 8 | +//! |
| 9 | +//! At browser launch, `lib.rs` passes the cached path to CEF via |
| 10 | +//! `--use-file-for-fake-video-capture=<path>`. CEF reads it on every |
| 11 | +//! `getUserMedia({video:true})` call and loops on EOF, so a single |
| 12 | +//! frame produces a steady-state still image as the agent's "webcam". |
| 13 | +//! |
| 14 | +//! No JS is injected anywhere — this is a process-level Chromium flag, |
| 15 | +//! not page-level instrumentation. |
| 16 | +
|
| 17 | +use std::fs; |
| 18 | +use std::path::{Path, PathBuf}; |
| 19 | + |
| 20 | +use resvg::usvg::{Options as UsvgOptions, Tree as UsvgTree}; |
| 21 | +use tiny_skia::{Pixmap, Transform}; |
| 22 | + |
| 23 | +/// Output webcam resolution. 640×480 is what every videoconferencing |
| 24 | +/// app expects to negotiate against; Meet downscales to whatever it |
| 25 | +/// wants from there. |
| 26 | +const WIDTH: u32 = 640; |
| 27 | +const HEIGHT: u32 = 480; |
| 28 | +const FRAMERATE: &str = "F30:1"; |
| 29 | + |
| 30 | +/// Mascot SVG embedded at build time. The remotion bundle owns the |
| 31 | +/// canonical asset; we vendor a copy of its content via `include_str!` |
| 32 | +/// so the shell builds without needing the remotion tree at runtime. |
| 33 | +const MASCOT_SVG: &str = include_str!("../../../../remotion/public/mascot.svg"); |
| 34 | + |
| 35 | +/// Top-level entrypoint. Returns the path to a Y4M file CEF can read, |
| 36 | +/// rasterizing the mascot if no cached version exists. |
| 37 | +/// |
| 38 | +/// Errors are logged + returned as `String` so the caller (lib.rs) |
| 39 | +/// can decide whether to skip the fake-camera flag and let the user |
| 40 | +/// see the default "no camera" path. We do **not** panic — a missing |
| 41 | +/// fake camera is degraded but not fatal. |
| 42 | +pub fn ensure_mascot_y4m(data_dir: &Path) -> Result<PathBuf, String> { |
| 43 | + let cache_dir = data_dir.join("cache").join("fake_camera"); |
| 44 | + fs::create_dir_all(&cache_dir).map_err(|e| format!("create cache dir: {e}"))?; |
| 45 | + |
| 46 | + let svg_hash = stable_hash(MASCOT_SVG); |
| 47 | + let y4m_path = cache_dir.join(format!("mascot-{WIDTH}x{HEIGHT}-{svg_hash:016x}.y4m")); |
| 48 | + |
| 49 | + if y4m_path.exists() { |
| 50 | + log::info!( |
| 51 | + "[fake-camera] reusing cached mascot Y4M path={}", |
| 52 | + y4m_path.display() |
| 53 | + ); |
| 54 | + return Ok(y4m_path); |
| 55 | + } |
| 56 | + |
| 57 | + log::info!( |
| 58 | + "[fake-camera] rasterizing mascot {}x{} -> {}", |
| 59 | + WIDTH, |
| 60 | + HEIGHT, |
| 61 | + y4m_path.display() |
| 62 | + ); |
| 63 | + let rgba = rasterize_svg(MASCOT_SVG)?; |
| 64 | + let y4m_bytes = encode_single_frame_y4m(&rgba); |
| 65 | + |
| 66 | + // Atomic-ish write: write to .partial then rename, so a crash |
| 67 | + // mid-write never leaves CEF reading a half-finished Y4M. |
| 68 | + let tmp_path = y4m_path.with_extension("y4m.partial"); |
| 69 | + fs::write(&tmp_path, &y4m_bytes).map_err(|e| format!("write y4m: {e}"))?; |
| 70 | + // Tolerate a concurrent writer landing first: if rename fails but the |
| 71 | + // target already exists, the other writer wrote the same SVG-hash-keyed |
| 72 | + // file and we can drop our temp copy. |
| 73 | + match fs::rename(&tmp_path, &y4m_path) { |
| 74 | + Ok(()) => Ok(y4m_path), |
| 75 | + Err(_) if y4m_path.exists() => { |
| 76 | + let _ = fs::remove_file(&tmp_path); |
| 77 | + Ok(y4m_path) |
| 78 | + } |
| 79 | + Err(e) => Err(format!("rename y4m: {e}")), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Render the SVG to a 640×480 RGBA8 bitmap, letterboxed onto a flat |
| 84 | +/// background so the mascot looks centered in the participant tile |
| 85 | +/// regardless of source aspect ratio. |
| 86 | +fn rasterize_svg(svg: &str) -> Result<Vec<u8>, String> { |
| 87 | + let tree = |
| 88 | + UsvgTree::from_str(svg, &UsvgOptions::default()).map_err(|e| format!("parse svg: {e}"))?; |
| 89 | + let svg_size = tree.size(); |
| 90 | + let svg_w = svg_size.width(); |
| 91 | + let svg_h = svg_size.height(); |
| 92 | + if svg_w <= 0.0 || svg_h <= 0.0 { |
| 93 | + return Err("mascot svg has zero size".into()); |
| 94 | + } |
| 95 | + |
| 96 | + let mut pixmap = Pixmap::new(WIDTH, HEIGHT).ok_or_else(|| "alloc pixmap".to_string())?; |
| 97 | + // Background fill — Meet's tile is rectangular and we want a clean |
| 98 | + // backdrop, not transparent (which the YUV conversion would |
| 99 | + // collapse to black anyway). |
| 100 | + pixmap.fill(tiny_skia::Color::from_rgba8(247, 244, 238, 255)); |
| 101 | + |
| 102 | + // Fit the mascot inside the frame with a 12% margin so it doesn't |
| 103 | + // get cropped at the corners by Meet's rounded mask. |
| 104 | + let margin = 0.12; |
| 105 | + let target_w = (WIDTH as f32) * (1.0 - 2.0 * margin); |
| 106 | + let target_h = (HEIGHT as f32) * (1.0 - 2.0 * margin); |
| 107 | + let scale = (target_w / svg_w).min(target_h / svg_h); |
| 108 | + let drawn_w = svg_w * scale; |
| 109 | + let drawn_h = svg_h * scale; |
| 110 | + let tx = ((WIDTH as f32) - drawn_w) / 2.0; |
| 111 | + let ty = ((HEIGHT as f32) - drawn_h) / 2.0; |
| 112 | + |
| 113 | + let transform = Transform::from_scale(scale, scale).post_translate(tx, ty); |
| 114 | + resvg::render(&tree, transform, &mut pixmap.as_mut()); |
| 115 | + |
| 116 | + Ok(pixmap.take()) |
| 117 | +} |
| 118 | + |
| 119 | +/// Convert an RGBA8 buffer (length WIDTH * HEIGHT * 4) to a Y4M file |
| 120 | +/// containing a single FRAME using BT.601 limited-range coefficients. |
| 121 | +/// Chromium's fake video capture re-reads the file in a loop, so one |
| 122 | +/// frame is enough for a steady image. |
| 123 | +fn encode_single_frame_y4m(rgba: &[u8]) -> Vec<u8> { |
| 124 | + let header = format!( |
| 125 | + "YUV4MPEG2 W{WIDTH} H{HEIGHT} {FRAMERATE} Ip A1:1 C420jpeg Xopenhuman-mascot\nFRAME\n" |
| 126 | + ); |
| 127 | + |
| 128 | + let pixel_count = (WIDTH * HEIGHT) as usize; |
| 129 | + let mut y_plane = Vec::with_capacity(pixel_count); |
| 130 | + let chroma_count = ((WIDTH / 2) * (HEIGHT / 2)) as usize; |
| 131 | + let mut u_plane = Vec::with_capacity(chroma_count); |
| 132 | + let mut v_plane = Vec::with_capacity(chroma_count); |
| 133 | + |
| 134 | + // Y plane: per-pixel luma. |
| 135 | + for chunk in rgba.chunks_exact(4) { |
| 136 | + let (r, g, b) = (chunk[0] as f32, chunk[1] as f32, chunk[2] as f32); |
| 137 | + let y = (0.299 * r + 0.587 * g + 0.114 * b).clamp(0.0, 255.0) as u8; |
| 138 | + y_plane.push(y); |
| 139 | + } |
| 140 | + |
| 141 | + // U/V planes: average each 2×2 block. |
| 142 | + for by in (0..HEIGHT).step_by(2) { |
| 143 | + for bx in (0..WIDTH).step_by(2) { |
| 144 | + let mut r_sum = 0.0; |
| 145 | + let mut g_sum = 0.0; |
| 146 | + let mut b_sum = 0.0; |
| 147 | + for dy in 0..2 { |
| 148 | + for dx in 0..2 { |
| 149 | + let x = bx + dx; |
| 150 | + let y = by + dy; |
| 151 | + let idx = ((y * WIDTH + x) * 4) as usize; |
| 152 | + r_sum += rgba[idx] as f32; |
| 153 | + g_sum += rgba[idx + 1] as f32; |
| 154 | + b_sum += rgba[idx + 2] as f32; |
| 155 | + } |
| 156 | + } |
| 157 | + let r = r_sum / 4.0; |
| 158 | + let g = g_sum / 4.0; |
| 159 | + let b = b_sum / 4.0; |
| 160 | + let u = (-0.169 * r - 0.331 * g + 0.5 * b + 128.0).clamp(0.0, 255.0) as u8; |
| 161 | + let v = (0.5 * r - 0.419 * g - 0.081 * b + 128.0).clamp(0.0, 255.0) as u8; |
| 162 | + u_plane.push(u); |
| 163 | + v_plane.push(v); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + let mut out = Vec::with_capacity(header.len() + y_plane.len() + u_plane.len() + v_plane.len()); |
| 168 | + out.extend_from_slice(header.as_bytes()); |
| 169 | + out.extend_from_slice(&y_plane); |
| 170 | + out.extend_from_slice(&u_plane); |
| 171 | + out.extend_from_slice(&v_plane); |
| 172 | + out |
| 173 | +} |
| 174 | + |
| 175 | +/// Stable, deterministic hash of a string — used to key the Y4M cache |
| 176 | +/// against the source SVG. We don't need cryptographic strength, just |
| 177 | +/// "did the SVG change?", so std's `DefaultHasher` is fine. |
| 178 | +fn stable_hash(s: &str) -> u64 { |
| 179 | + use std::hash::{Hash, Hasher}; |
| 180 | + let mut h = std::collections::hash_map::DefaultHasher::new(); |
| 181 | + s.hash(&mut h); |
| 182 | + h.finish() |
| 183 | +} |
| 184 | + |
| 185 | +#[cfg(test)] |
| 186 | +mod tests { |
| 187 | + use super::*; |
| 188 | + |
| 189 | + #[test] |
| 190 | + fn y4m_header_includes_dimensions_and_colorspace() { |
| 191 | + let dummy = vec![0u8; (WIDTH * HEIGHT * 4) as usize]; |
| 192 | + let bytes = encode_single_frame_y4m(&dummy); |
| 193 | + let header_end = bytes.iter().position(|&b| b == b'\n').unwrap(); |
| 194 | + let header = std::str::from_utf8(&bytes[..header_end]).unwrap(); |
| 195 | + assert!(header.contains(&format!("W{WIDTH}"))); |
| 196 | + assert!(header.contains(&format!("H{HEIGHT}"))); |
| 197 | + assert!(header.contains("C420jpeg")); |
| 198 | + } |
| 199 | + |
| 200 | + #[test] |
| 201 | + fn y4m_payload_size_matches_yuv420_layout() { |
| 202 | + let dummy = vec![0u8; (WIDTH * HEIGHT * 4) as usize]; |
| 203 | + let bytes = encode_single_frame_y4m(&dummy); |
| 204 | + // Header up to first newline, then "FRAME\n", then planes. |
| 205 | + let frame_marker = b"FRAME\n"; |
| 206 | + let frame_idx = bytes |
| 207 | + .windows(frame_marker.len()) |
| 208 | + .position(|w| w == frame_marker) |
| 209 | + .expect("FRAME marker present"); |
| 210 | + let payload_len = bytes.len() - frame_idx - frame_marker.len(); |
| 211 | + let expected = (WIDTH * HEIGHT) as usize + 2 * ((WIDTH / 2) * (HEIGHT / 2)) as usize; |
| 212 | + assert_eq!(payload_len, expected); |
| 213 | + } |
| 214 | + |
| 215 | + #[test] |
| 216 | + fn rasterize_svg_produces_correctly_sized_buffer() { |
| 217 | + let rgba = rasterize_svg(MASCOT_SVG).expect("rasterize"); |
| 218 | + assert_eq!(rgba.len(), (WIDTH * HEIGHT * 4) as usize); |
| 219 | + } |
| 220 | + |
| 221 | + #[test] |
| 222 | + fn stable_hash_is_deterministic() { |
| 223 | + assert_eq!(stable_hash("openhuman"), stable_hash("openhuman")); |
| 224 | + assert_ne!(stable_hash("a"), stable_hash("b")); |
| 225 | + } |
| 226 | +} |
0 commit comments