Skip to main content

gosuto_libwebrtc/
video_source.rs

1// Copyright 2025 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use livekit_protocol::enum_dispatch;
16
17use crate::imp::video_source as vs_imp;
18
19#[derive(Debug, Clone)]
20pub struct VideoResolution {
21    pub width: u32,
22    pub height: u32,
23}
24
25impl Default for VideoResolution {
26    // Default to 720p
27    fn default() -> Self {
28        VideoResolution { width: 1280, height: 720 }
29    }
30}
31
32#[non_exhaustive]
33#[derive(Debug, Clone)]
34pub enum RtcVideoSource {
35    // TODO(theomonnom): Web video sources (eq. to tracks on browsers?)
36    #[cfg(not(target_arch = "wasm32"))]
37    Native(native::NativeVideoSource),
38}
39
40// TODO(theomonnom): Support enum dispatch with conditional compilation?
41impl RtcVideoSource {
42    enum_dispatch!(
43        [Native];
44        pub fn video_resolution(self: &Self) -> VideoResolution;
45    );
46}
47
48#[cfg(not(target_arch = "wasm32"))]
49pub mod native {
50    use std::fmt::{Debug, Formatter};
51
52    use super::*;
53    use crate::video_frame::{VideoBuffer, VideoFrame};
54
55    #[derive(Clone)]
56    pub struct NativeVideoSource {
57        pub(crate) handle: vs_imp::NativeVideoSource,
58    }
59
60    impl Debug for NativeVideoSource {
61        fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
62            f.debug_struct("NativeVideoSource").finish()
63        }
64    }
65
66    impl Default for NativeVideoSource {
67        fn default() -> Self {
68            Self::new(VideoResolution::default(), false)
69        }
70    }
71
72    impl NativeVideoSource {
73        pub fn new(resolution: VideoResolution, is_screencast: bool) -> Self {
74            Self { handle: vs_imp::NativeVideoSource::new(resolution, is_screencast) }
75        }
76
77        pub fn capture_frame<T: AsRef<dyn VideoBuffer>>(&self, frame: &VideoFrame<T>) {
78            self.handle.capture_frame(frame)
79        }
80
81        pub fn video_resolution(&self) -> VideoResolution {
82            self.handle.video_resolution()
83        }
84    }
85}
86
87#[cfg(target_arch = "wasm32")]
88pub mod web {}