BlueGit

ict rusty-teams

Files

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

video_pattern.rs

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