forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

share.rs288 lines, 7.8 KiBHistoryRawDownload
1use bitcode::{Decode, Encode};
2
3use crate::{constant::MAXIMUM_SHARE_LAYERS, model::Id};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)]
6pub enum Codec {
7 H264,
8 H265,
9 Av1,
10 Vp8,
11 Vp9,
12}
13
14impl Codec {
15 pub const ALL: [Self; 5] = [Self::H264, Self::H265, Self::Av1, Self::Vp8, Self::Vp9];
16 pub const PREFERENCE: [Self; 3] = [Self::Av1, Self::H265, Self::H264];
17
18 pub fn name(self) -> &'static str {
19 match self {
20 Self::H264 => "H264",
21 Self::H265 => "H265",
22 Self::Av1 => "AV1",
23 Self::Vp8 => "VP8",
24 Self::Vp9 => "VP9",
25 }
26 }
27
28 pub fn parse(name: &str) -> Option<Self> {
29 Self::ALL.into_iter().find(|codec| codec.name() == name)
30 }
31
32 pub fn bits_per_pixel(self) -> f32 {
33 match self {
34 Self::Vp8 => 0.18,
35 Self::H264 => 0.16,
36 Self::Vp9 => 0.14,
37 Self::H265 | Self::Av1 => 0.12,
38 }
39 }
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)]
43pub enum Resolution {
44 P360,
45 P480,
46 P720,
47 P1080,
48 P1440,
49 P2160,
50}
51
52impl Resolution {
53 pub const ALL: [Self; 6] = [
54 Self::P360,
55 Self::P480,
56 Self::P720,
57 Self::P1080,
58 Self::P1440,
59 Self::P2160,
60 ];
61
62 pub fn height(self) -> u32 {
63 match self {
64 Self::P360 => 360,
65 Self::P480 => 480,
66 Self::P720 => 720,
67 Self::P1080 => 1080,
68 Self::P1440 => 1440,
69 Self::P2160 => 2160,
70 }
71 }
72
73 pub fn size(self, source_width: u32, source_height: u32) -> (u32, u32) {
74 let height = self.height().min(source_height);
75 let width = (source_width as u64 * height as u64 / source_height.max(1) as u64) as u32;
76 (width & !7, height & !7)
77 }
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)]
81pub enum Fps {
82 F15,
83 F30,
84 F60,
85}
86
87impl Fps {
88 pub const ALL: [Self; 3] = [Self::F15, Self::F30, Self::F60];
89
90 pub fn value(self) -> u32 {
91 match self {
92 Self::F15 => 15,
93 Self::F30 => 30,
94 Self::F60 => 60,
95 }
96 }
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)]
100pub struct Quality {
101 pub resolution: Resolution,
102 pub fps: Fps,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)]
106pub struct Layer {
107 pub quality: Quality,
108 pub codec: Codec,
109}
110
111impl Layer {
112 pub fn to_byte(self) -> u8 {
113 (self.quality.resolution as u8) << 5 | (self.quality.fps as u8) << 3 | self.codec as u8
114 }
115
116 pub fn from_byte(byte: u8) -> Option<Self> {
117 Some(Self {
118 quality: Quality {
119 resolution: *Resolution::ALL.get((byte >> 5) as usize)?,
120 fps: *Fps::ALL.get((byte >> 3 & 0b11) as usize)?,
121 },
122 codec: *Codec::ALL.get((byte & 0b111) as usize)?,
123 })
124 }
125
126 pub fn bitrate(self, source_width: u32, source_height: u32) -> u32 {
127 let (width, height) = self.quality.resolution.size(source_width, source_height);
128 (width as f32
129 * height as f32
130 * self.quality.fps.value() as f32
131 * self.codec.bits_per_pixel()) as u32
132 }
133}
134
135#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
136pub struct Source {
137 pub width: u32,
138 pub height: u32,
139 pub top: Quality,
140 pub encode: Vec<Codec>,
141}
142
143#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)]
144pub enum Kind {
145 Screen,
146 Camera,
147}
148
149#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
150pub struct Share {
151 pub user_id: Id,
152 pub kind: Kind,
153 pub source: Source,
154 pub started_ms: u64,
155}
156
157pub type Mode = (u32, u32);
158
159pub fn options(
160 modes: &[Mode],
161 resolution: Resolution,
162 max_height: u32,
163) -> (Vec<Resolution>, Vec<Fps>) {
164 let reachable = |height: u32| modes.iter().filter(move |(mode, _)| *mode >= height);
165 let resolutions: Vec<Resolution> = Resolution::ALL
166 .into_iter()
167 .filter(|option| {
168 option.height() <= max_height && reachable(option.height()).next().is_some()
169 })
170 .collect();
171 let height = resolutions
172 .iter()
173 .copied()
174 .filter(|option| *option <= resolution)
175 .max()
176 .or(resolutions.first().copied())
177 .map_or(0, Resolution::height);
178 let fps = Fps::ALL
179 .into_iter()
180 .filter(|fps| reachable(height).any(|(_, rate)| rate.saturating_add(1) >= fps.value()))
181 .collect();
182 (resolutions, fps)
183}
184
185pub fn clamp(top: Quality, modes: &[Mode], max_height: u32) -> Quality {
186 let (resolutions, _) = options(modes, top.resolution, max_height);
187 let resolution = resolutions
188 .into_iter()
189 .filter(|resolution| *resolution <= top.resolution)
190 .max()
191 .unwrap_or(Resolution::P360);
192 let (_, frame_rates) = options(modes, resolution, max_height);
193 Quality {
194 resolution,
195 fps: frame_rates
196 .into_iter()
197 .filter(|fps| *fps <= top.fps)
198 .max()
199 .unwrap_or(Fps::F15),
200 }
201}
202
203pub fn best_codec(encode: &[Codec], decode: &[Codec]) -> Option<Codec> {
204 Codec::PREFERENCE
205 .into_iter()
206 .find(|codec| encode.contains(codec) && decode.contains(codec))
207}
208
209pub fn choose(source: &Source, wanted: Quality, decode: &[Codec], used: &[Layer]) -> Option<Layer> {
210 let codec = best_codec(&source.encode, decode)?;
211 let quality = Quality {
212 resolution: wanted.resolution.min(source.top.resolution),
213 fps: wanted.fps.min(source.top.fps),
214 };
215 let layer = Layer { quality, codec };
216 if used.len() < MAXIMUM_SHARE_LAYERS || used.contains(&layer) {
217 return Some(layer);
218 }
219 used.iter()
220 .copied()
221 .filter(|used| used.quality <= quality && decode.contains(&used.codec))
222 .max()
223 .or_else(|| {
224 used.iter()
225 .copied()
226 .filter(|used| decode.contains(&used.codec))
227 .min()
228 })
229}
230
231#[cfg(test)]
232mod tests {
233 use super::*;
234
235 #[test]
236 fn fps_follows_resolution() {
237 let modes = [(720, 60), (1080, 30)];
238 let (resolutions, fps) = options(&modes, Resolution::P1080, u32::MAX);
239 assert!(resolutions.contains(&Resolution::P1080));
240 assert_eq!(fps, vec![Fps::F15, Fps::F30]);
241 assert!(
242 options(&modes, Resolution::P720, u32::MAX)
243 .1
244 .contains(&Fps::F60)
245 );
246 let top = Quality {
247 resolution: Resolution::P1080,
248 fps: Fps::F60,
249 };
250 assert_eq!(clamp(top, &modes, u32::MAX).fps, Fps::F30);
251 assert_eq!(
252 options(&[(1440, 59)], Resolution::P1440, u32::MAX).1.last(),
253 Some(&Fps::F60)
254 );
255 }
256
257 #[test]
258 fn layer_byte_round_trip_and_choice() {
259 for resolution in Resolution::ALL {
260 for fps in Fps::ALL {
261 for codec in Codec::ALL {
262 let layer = Layer {
263 quality: Quality { resolution, fps },
264 codec,
265 };
266 assert_eq!(Layer::from_byte(layer.to_byte()), Some(layer));
267 }
268 }
269 }
270
271 let source = Source {
272 width: 2560,
273 height: 1440,
274 top: Quality {
275 resolution: Resolution::P1440,
276 fps: Fps::F60,
277 },
278 encode: vec![Codec::H264, Codec::Av1],
279 };
280 let wanted = Quality {
281 resolution: Resolution::P1080,
282 fps: Fps::F60,
283 };
284 let layer = choose(&source, wanted, &[Codec::H264, Codec::H265], &[]).unwrap();
285 assert_eq!(layer.quality.resolution, Resolution::P1080);
286 assert_eq!(layer.codec, Codec::H264);
287 }
288}