BlueGit

ict rusty-teams

Files

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

video_pattern.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
video_pattern.rs 6.0 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(
50 y_plane,
51 u_plane,
52 v_plane,
53 (width, height),
54 (stride_y, stride_u, stride_v),
55 tick,
56 ),
57 PatternKind::Screen => paint_screen(
58 y_plane,
59 u_plane,
60 v_plane,
61 (width, height),
62 (stride_y, stride_u, stride_v),
63 tick,
64 ),
65 }
66 buffer
67}
68
69fn paint_camera(
70 y_plane: &mut [u8],
71 u_plane: &mut [u8],
72 v_plane: &mut [u8],
73 (width, height): (u32, u32),
74 (stride_y, stride_u, stride_v): (u32, u32, u32),
75 tick: u32,
76) {
77 let square_x = (tick * 6) % (width - SQUARE);
78 let square_y = (tick * 4) % (height - SQUARE);
79 for row in 0..height {
80 for column in 0..width {
81 let inside = column >= square_x
82 && column < square_x + SQUARE
83 && row >= square_y
84 && row < square_y + SQUARE;
85 let shade = ((column + tick * 3) * 255 / width) as u8;
86 let on_border = column < BORDER
87 || row < BORDER
88 || column >= width - BORDER
89 || row >= height - BORDER;
90 let delta_x = column as i64 - width as i64 / 2;
91 let delta_y = row as i64 - height as i64 / 2;
92 let radius = height as i64 / 2 - 8;
93 let on_circle =
94 (delta_x * delta_x + delta_y * delta_y - radius * radius).abs() < radius * 6;
95 y_plane[(row * stride_y + column) as usize] = if on_border || on_circle || inside {
96 235
97 } else {
98 shade / 2 + 24
99 };
100 }
101 }
102 for row in 0..height / 2 {
103 for column in 0..width / 2 {
104 u_plane[(row * stride_u + column) as usize] =
105 (64 + (column * 128 / (width / 2)) as u8).wrapping_add((tick * 2) as u8 / 4);
106 v_plane[(row * stride_v + column) as usize] =
107 (192 - (row * 128 / (height / 2)) as u8).wrapping_add((tick % 64) as u8);
108 }
109 }
110}
111
112fn paint_screen(
113 y_plane: &mut [u8],
114 u_plane: &mut [u8],
115 v_plane: &mut [u8],
116 (width, height): (u32, u32),
117 (stride_y, stride_u, stride_v): (u32, u32, u32),
118 tick: u32,
119) {
120 let band = height / BLOCK_ROWS;
121 let cursor_x = (tick * 9) % (width - SQUARE / 2);
122 let cursor_y = (tick * 5) % (height - SQUARE / 2);
123 for row in 0..height {
124 for column in 0..width {
125 let on_cursor = column >= cursor_x
126 && column < cursor_x + SQUARE / 2
127 && row >= cursor_y
128 && row < cursor_y + SQUARE / 2;
129 let line = (row / 18) % 2 == 0 && column % 11 < 7 && column > 40 && column < width - 40;
130 let title_bar = row < 40;
131 y_plane[(row * stride_y + column) as usize] = match (on_cursor, title_bar, line) {
132 (true, _, _) => 235,
133 (_, true, _) => 60,
134 (_, _, true) => 40,
135 _ => 225 - (row / band * 6) as u8,
136 };
137 }
138 }
139 u_plane[..(stride_u * height / 2) as usize].fill(128);
140 v_plane[..(stride_v * height / 2) as usize].fill(128);
141}
142
143pub fn spawn_pattern(
144 mut sink: LocalSink,
145 kind: PatternKind,
146 effects: Option<Arc<BlurSettings>>,
147) -> JoinHandle<()> {
148 tokio::spawn(async move {
149 let mut ticker = interval(Duration::from_secs(1) / kind.fps());
150 let mut tick = 0u32;
151 let mut deliver: Box<dyn FnMut(I420Buffer) + Send> = match effects {
152 Some(settings) => match EffectsThread::spawn(settings, move |frame| sink.push(frame)) {
153 Ok(effects) => Box::new(move |frame| effects.submit(frame)),
154 Err(_) => return,
155 },
156 None => Box::new(move |frame| sink.push(frame)),
157 };
158 loop {
159 ticker.tick().await;
160 deliver(pattern_frame(kind, tick));
161 tick = tick.wrapping_add(1);
162 }
163 })
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169
170 fn luma_checksum(kind: PatternKind, tick: u32) -> u64 {
171 let buffer = pattern_frame(kind, tick);
172 buffer.data().0.iter().map(|&value| u64::from(value)).sum()
173 }
174
175 #[test]
176 fn the_camera_pattern_moves_between_frames() {
177 assert_ne!(
178 luma_checksum(PatternKind::Camera, 0),
179 luma_checksum(PatternKind::Camera, 9)
180 );
181 }
182
183 #[test]
184 fn the_screen_pattern_moves_between_frames() {
185 assert_ne!(
186 luma_checksum(PatternKind::Screen, 0),
187 luma_checksum(PatternKind::Screen, 4)
188 );
189 }
190
191 #[test]
192 fn patterns_have_the_documented_sizes() {
193 assert_eq!(
194 pattern_frame(PatternKind::Camera, 1).strides().0,
195 CAMERA_WIDTH
196 );
197 assert_eq!(PatternKind::Screen.size(), (SCREEN_WIDTH, SCREEN_HEIGHT));
198 }
199}