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