BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / video_pattern.rs

video_pattern.rs

video_pattern.rs 5.5 KiB Download Raw
1use std::sync::Arc;
2use std::time::Duration;
3
4use libwebrtc::video_frame::I420Buffer;
5use tokio::task::JoinHandle;
6use tokio::time::interval;
7
8use crate::blur::BlurSettings;
9use crate::effects::EffectsThread;
10use crate::video_send::LocalSink;
11
12pub const CAMERA_WIDTH: u32 = 640;
13pub const CAMERA_HEIGHT: u32 = 360;
14pub const SCREEN_WIDTH: u32 = 1280;
15pub const SCREEN_HEIGHT: u32 = 720;
16const SQUARE: u32 = 64;
17const BORDER: u32 = 2;
18const BLOCK_ROWS: u32 = 6;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum PatternKind {
22 Camera,
23 Screen,
24}
25
26impl PatternKind {
27 pub fn size(self) -> (u32, u32) {
28 match self {
29 PatternKind::Camera => (CAMERA_WIDTH, CAMERA_HEIGHT),
30 PatternKind::Screen => (SCREEN_WIDTH, SCREEN_HEIGHT),
31 }
32 }
33
34 pub fn fps(self) -> u32 {
35 match self {
36 PatternKind::Camera => 30,
37 PatternKind::Screen => 15,
38 }
39 }
40}
41
42/// A moving test picture: no real camera or screen is ever read.
43pub fn pattern_frame(kind: PatternKind, tick: u32) -> I420Buffer {
44 let (width, height) = kind.size();
45 let mut buffer = I420Buffer::new(width, height);
46 let (stride_y, stride_u, stride_v) = buffer.strides();
47 let (y_plane, u_plane, v_plane) = buffer.data_mut();
48 match kind {
49 PatternKind::Camera => paint_camera(y_plane, u_plane, v_plane, (width, height), (stride_y, stride_u, stride_v), tick),
50 PatternKind::Screen => paint_screen(y_plane, u_plane, v_plane, (width, height), (stride_y, stride_u, stride_v), tick),
51 }
52 buffer
53}
54
55fn paint_camera(
56 y_plane: &mut [u8],
57 u_plane: &mut [u8],
58 v_plane: &mut [u8],
59 (width, height): (u32, u32),
60 (stride_y, stride_u, stride_v): (u32, u32, u32),
61 tick: u32,
62) {
63 let square_x = (tick * 6) % (width - SQUARE);
64 let square_y = (tick * 4) % (height - SQUARE);
65 for row in 0..height {
66 for column in 0..width {
67 let inside = column >= square_x && column < square_x + SQUARE && row >= square_y && row < square_y + SQUARE;
68 let shade = ((column + tick * 3) * 255 / width) as u8;
69 let on_border = column < BORDER || row < BORDER || column >= width - BORDER || row >= height - BORDER;
70 let delta_x = column as i64 - width as i64 / 2;
71 let delta_y = row as i64 - height as i64 / 2;
72 let radius = height as i64 / 2 - 8;
73 let on_circle = (delta_x * delta_x + delta_y * delta_y - radius * radius).abs() < radius * 6;
74 y_plane[(row * stride_y + column) as usize] = if on_border || on_circle || inside { 235 } else { shade / 2 + 24 };
75 }
76 }
77 for row in 0..height / 2 {
78 for column in 0..width / 2 {
79 u_plane[(row * stride_u + column) as usize] = (64 + (column * 128 / (width / 2)) as u8).wrapping_add((tick * 2) as u8 / 4);
80 v_plane[(row * stride_v + column) as usize] = (192 - (row * 128 / (height / 2)) as u8).wrapping_add((tick % 64) as u8);
81 }
82 }
83}
84
85fn paint_screen(
86 y_plane: &mut [u8],
87 u_plane: &mut [u8],
88 v_plane: &mut [u8],
89 (width, height): (u32, u32),
90 (stride_y, stride_u, stride_v): (u32, u32, u32),
91 tick: u32,
92) {
93 let band = height / BLOCK_ROWS;
94 let cursor_x = (tick * 9) % (width - SQUARE / 2);
95 let cursor_y = (tick * 5) % (height - SQUARE / 2);
96 for row in 0..height {
97 for column in 0..width {
98 let on_cursor = column >= cursor_x && column < cursor_x + SQUARE / 2 && row >= cursor_y && row < cursor_y + SQUARE / 2;
99 let line = (row / 18) % 2 == 0 && column % 11 < 7 && column > 40 && column < width - 40;
100 let title_bar = row < 40;
101 y_plane[(row * stride_y + column) as usize] = match (on_cursor, title_bar, line) {
102 (true, _, _) => 235,
103 (_, true, _) => 60,
104 (_, _, true) => 40,
105 _ => 225 - (row / band * 6) as u8,
106 };
107 }
108 }
109 u_plane[..(stride_u * height / 2) as usize].fill(128);
110 v_plane[..(stride_v * height / 2) as usize].fill(128);
111}
112
113pub fn spawn_pattern(mut sink: LocalSink, kind: PatternKind, effects: Option<Arc<BlurSettings>>) -> JoinHandle<()> {
114 tokio::spawn(async move {
115 let mut ticker = interval(Duration::from_secs(1) / kind.fps());
116 let mut tick = 0u32;
117 let mut deliver: Box<dyn FnMut(I420Buffer) + Send> = match effects {
118 Some(settings) => match EffectsThread::spawn(settings, move |frame| sink.push(frame)) {
119 Ok(effects) => Box::new(move |frame| effects.submit(frame)),
120 Err(_) => return,
121 },
122 None => Box::new(move |frame| sink.push(frame)),
123 };
124 loop {
125 ticker.tick().await;
126 deliver(pattern_frame(kind, tick));
127 tick = tick.wrapping_add(1);
128 }
129 })
130}
131
132#[cfg(test)]
133mod tests {
134 use super::*;
135
136 fn luma_checksum(kind: PatternKind, tick: u32) -> u64 {
137 let buffer = pattern_frame(kind, tick);
138 buffer.data().0.iter().map(|&value| u64::from(value)).sum()
139 }
140
141 #[test]
142 fn the_camera_pattern_moves_between_frames() {
143 assert_ne!(luma_checksum(PatternKind::Camera, 0), luma_checksum(PatternKind::Camera, 9));
144 }
145
146 #[test]
147 fn the_screen_pattern_moves_between_frames() {
148 assert_ne!(luma_checksum(PatternKind::Screen, 0), luma_checksum(PatternKind::Screen, 4));
149 }
150
151 #[test]
152 fn patterns_have_the_documented_sizes() {
153 assert_eq!(pattern_frame(PatternKind::Camera, 1).strides().0, CAMERA_WIDTH);
154 assert_eq!(PatternKind::Screen.size(), (SCREEN_WIDTH, SCREEN_HEIGHT));
155 }
156}