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