Skip to main content

servo_media_player/
video.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::sync::Arc;
6
7use malloc_size_of::MallocSizeOf;
8use malloc_size_of_derive::MallocSizeOf;
9
10#[derive(Clone, MallocSizeOf)]
11pub enum VideoFrameData {
12    Raw(#[conditional_malloc_size_of] Arc<Vec<u8>>),
13    Texture(u32),
14    OESTexture(u32),
15}
16
17pub trait Buffer: Send + Sync + MallocSizeOf {
18    fn to_vec(&self) -> Option<VideoFrameData>;
19}
20
21#[derive(Clone, MallocSizeOf)]
22pub struct VideoFrame {
23    width: i32,
24    height: i32,
25    data: VideoFrameData,
26    #[ignore_malloc_size_of = "Difficult"]
27    _buffer: Arc<dyn Buffer>,
28}
29
30impl VideoFrame {
31    pub fn new(width: i32, height: i32, buffer: Arc<dyn Buffer>) -> Option<Self> {
32        let data = buffer.to_vec()?;
33        Some(VideoFrame {
34            width,
35            height,
36            data,
37            _buffer: buffer,
38        })
39    }
40
41    pub fn get_width(&self) -> i32 {
42        self.width
43    }
44
45    pub fn get_height(&self) -> i32 {
46        self.height
47    }
48
49    pub fn get_data(&self) -> Arc<Vec<u8>> {
50        match self.data {
51            VideoFrameData::Raw(ref data) => data.clone(),
52            _ => unreachable!("invalid raw data request for texture frame"),
53        }
54    }
55
56    pub fn get_texture_id(&self) -> u32 {
57        match self.data {
58            VideoFrameData::Texture(data) | VideoFrameData::OESTexture(data) => data,
59            _ => unreachable!("invalid texture id request for raw data frame"),
60        }
61    }
62
63    pub fn is_gl_texture(&self) -> bool {
64        matches!(
65            self.data,
66            VideoFrameData::Texture(_) | VideoFrameData::OESTexture(_)
67        )
68    }
69
70    pub fn is_external_oes(&self) -> bool {
71        matches!(self.data, VideoFrameData::OESTexture(_))
72    }
73}
74
75pub trait VideoFrameRenderer: Send + MallocSizeOf + 'static {
76    fn render(&mut self, frame: VideoFrame);
77}