BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / pictures.rs

pictures.rs

8629da58 History
pictures.rs 1.8 KiB Download Raw
1use std::collections::HashMap;
2use std::sync::Arc;
3use std::time::{Duration, Instant};
4
5use calling::{VideoHub, VideoKey, VideoPicture};
6use gpui_kit::*;
7use image::{Frame, RgbaImage};
8
9const STALE_AFTER: Duration = Duration::from_millis(2500);
10
11struct Shown {
12 image: Arc<RenderImage>,
13 updated: Instant,
14}
15
16#[derive(Default)]
17pub struct CallPictures {
18 shown: HashMap<VideoKey, Shown>,
19}
20
21fn render_image(picture: VideoPicture) -> Option<Arc<RenderImage>> {
22 let bytes = RgbaImage::from_raw(picture.width, picture.height, picture.bgra)?;
23 Some(Arc::new(RenderImage::new(vec![Frame::new(bytes)])))
24}
25
26impl CallPictures {
27 pub fn apply(&mut self, hub: &VideoHub, cx: &mut App) {
28 let now = Instant::now();
29 for (key, picture) in hub.drain() {
30 let picture = Arc::try_unwrap(picture).unwrap_or_else(|shared| (*shared).clone());
31 let Some(image) = render_image(picture) else {
32 continue;
33 };
34 if let Some(previous) = self.shown.insert(
35 key,
36 Shown {
37 image,
38 updated: now,
39 },
40 ) {
41 cx.drop_image(previous.image, None);
42 }
43 }
44 }
45
46 pub fn live(&self, key: &VideoKey, now: Instant) -> Option<Arc<RenderImage>> {
47 self.shown
48 .get(key)
49 .filter(|shown| *key == VideoKey::Screen || now.saturating_duration_since(shown.updated) < STALE_AFTER)
50 .map(|shown| shown.image.clone())
51 }
52
53 pub fn forget(&mut self, key: &VideoKey, cx: &mut App) {
54 if let Some(previous) = self.shown.remove(key) {
55 cx.drop_image(previous.image, None);
56 }
57 }
58
59 pub fn clear(&mut self, cx: &mut App) {
60 for (_, previous) in self.shown.drain() {
61 cx.drop_image(previous.image, None);
62 }
63 }
64}