obs_wrapper/source/
video.rs

1use std::mem;
2
3use obs_sys::{
4    obs_source_frame, video_output_get_format, video_output_get_frame_rate,
5    video_output_get_height, video_output_get_width, video_t,
6};
7
8#[derive(Debug)]
9pub enum VideoFormat {
10    Unknown,
11    None,
12    I420,
13    NV12,
14    YVYU,
15    YUY2,
16    UYVY,
17    RGBA,
18    BGRA,
19    BGRX,
20    Y800,
21    I444,
22    BGR3,
23    I422,
24    I40A,
25    I42A,
26    YUVA,
27    AYUV,
28}
29
30impl PartialEq for VideoFormat {
31    fn eq(&self, other: &Self) -> bool {
32        if matches!(self, VideoFormat::Unknown) || matches!(other, VideoFormat::Unknown) {
33            false
34        } else {
35            mem::discriminant(self) == mem::discriminant(other)
36        }
37    }
38}
39
40impl From<u32> for VideoFormat {
41    fn from(raw: u32) -> Self {
42        match raw {
43            0 => VideoFormat::None,
44            1 => VideoFormat::I420,
45            2 => VideoFormat::NV12,
46            3 => VideoFormat::YVYU,
47            4 => VideoFormat::YUY2,
48            5 => VideoFormat::UYVY,
49            6 => VideoFormat::RGBA,
50            7 => VideoFormat::BGRA,
51            8 => VideoFormat::BGRX,
52            9 => VideoFormat::Y800,
53            10 => VideoFormat::I444,
54            11 => VideoFormat::BGR3,
55            12 => VideoFormat::I422,
56            13 => VideoFormat::I40A,
57            14 => VideoFormat::I42A,
58            15 => VideoFormat::YUVA,
59            16 => VideoFormat::AYUV,
60            _ => VideoFormat::Unknown,
61        }
62    }
63}
64
65pub struct VideoDataContext {
66    pointer: *mut obs_source_frame,
67}
68
69impl VideoDataContext {
70    pub(crate) unsafe fn from_raw(pointer: *mut obs_source_frame) -> Self {
71        Self { pointer }
72    }
73
74    pub fn get_format(&self) -> VideoFormat {
75        let raw = unsafe { (*self.pointer).format };
76
77        VideoFormat::from(raw as u32)
78    }
79
80    pub fn get_width(&self) -> u32 {
81        unsafe { (*self.pointer).width }
82    }
83
84    pub fn get_height(&self) -> u32 {
85        unsafe { (*self.pointer).height }
86    }
87
88    pub fn get_data_buffer(&self, idx: usize) -> *mut u8 {
89        unsafe { (*self.pointer).data[idx] }
90    }
91
92    pub fn get_linesize(&self, idx: usize) -> u32 {
93        unsafe { (*self.pointer).linesize[idx] }
94    }
95}
96
97#[allow(unused)]
98pub struct VideoRef {
99    pointer: *mut video_t,
100}
101
102#[allow(unused)]
103impl VideoRef {
104    pub(crate) unsafe fn from_raw(pointer: *mut video_t) -> Self {
105        Self { pointer }
106    }
107
108    pub(crate) fn get_width(&self) -> u32 {
109        unsafe { video_output_get_width(self.pointer) }
110    }
111
112    pub(crate) fn get_height(&self) -> u32 {
113        unsafe { video_output_get_height(self.pointer) }
114    }
115
116    pub(crate) fn get_frame_rate(&self) -> f64 {
117        unsafe { video_output_get_frame_rate(self.pointer) }
118    }
119
120    pub(crate) fn get_format(&self) -> VideoFormat {
121        let raw = unsafe { video_output_get_format(self.pointer) };
122
123        VideoFormat::from(raw as u32)
124    }
125}