BlueGit

ict rusty-teams

Files

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

video_pattern.rs

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