servo_media_player/
video.rs

1use std::sync::Arc;
2
3#[derive(Clone)]
4pub enum VideoFrameData {
5    Raw(Arc<Vec<u8>>),
6    Texture(u32),
7    OESTexture(u32),
8}
9
10pub trait Buffer: Send + Sync {
11    fn to_vec(&self) -> Result<VideoFrameData, ()>;
12}
13
14#[derive(Clone)]
15pub struct VideoFrame {
16    width: i32,
17    height: i32,
18    data: VideoFrameData,
19    _buffer: Arc<dyn Buffer>,
20}
21
22impl VideoFrame {
23    pub fn new(width: i32, height: i32, buffer: Arc<dyn Buffer>) -> Result<Self, ()> {
24        let data = buffer.to_vec()?;
25
26        Ok(VideoFrame {
27            width,
28            height,
29            data,
30            _buffer: buffer,
31        })
32    }
33
34    pub fn get_width(&self) -> i32 {
35        self.width
36    }
37
38    pub fn get_height(&self) -> i32 {
39        self.height
40    }
41
42    pub fn get_data(&self) -> Arc<Vec<u8>> {
43        match self.data {
44            VideoFrameData::Raw(ref data) => data.clone(),
45            _ => unreachable!("invalid raw data request for texture frame"),
46        }
47    }
48
49    pub fn get_texture_id(&self) -> u32 {
50        match self.data {
51            VideoFrameData::Texture(data) | VideoFrameData::OESTexture(data) => data,
52            _ => unreachable!("invalid texture id request for raw data frame"),
53        }
54    }
55
56    pub fn is_gl_texture(&self) -> bool {
57        match self.data {
58            VideoFrameData::Texture(_) | VideoFrameData::OESTexture(_) => true,
59            _ => false,
60        }
61    }
62
63    pub fn is_external_oes(&self) -> bool {
64        match self.data {
65            VideoFrameData::OESTexture(_) => true,
66            _ => false,
67        }
68    }
69}
70
71pub trait VideoFrameRenderer: Send + 'static {
72    fn render(&mut self, frame: VideoFrame);
73}