webrtc_sys/
video_track.rs1use std::sync::Arc;
16
17use cxx::UniquePtr;
18
19use crate::{impl_thread_safety, video_frame::ffi::VideoFrame};
20
21#[cxx::bridge(namespace = "livekit_ffi")]
22pub mod ffi {
23 #[repr(i32)]
24 pub enum ContentHint {
25 None,
26 Fluid,
27 Detailed,
28 Text,
29 }
30
31 #[derive(Debug)]
32 pub struct VideoTrackSourceConstraints {
33 pub has_min_fps: bool,
34 pub min_fps: f64,
35 pub has_max_fps: bool,
36 pub max_fps: f64,
37 }
38
39 #[derive(Debug)]
40 pub struct VideoResolution {
41 pub width: u32,
42 pub height: u32,
43 }
44
45 extern "C++" {
46 include!("livekit/video_frame.h");
47 include!("livekit/media_stream_track.h");
48
49 type VideoFrame = crate::video_frame::ffi::VideoFrame;
50 type MediaStreamTrack = crate::media_stream_track::ffi::MediaStreamTrack;
51 }
52
53 unsafe extern "C++" {
54 include!("livekit/video_track.h");
55
56 type VideoTrack;
57 type NativeVideoSink;
58 type VideoTrackSource;
59
60 fn add_sink(self: &VideoTrack, sink: &SharedPtr<NativeVideoSink>);
61 fn remove_sink(self: &VideoTrack, sink: &SharedPtr<NativeVideoSink>);
62 fn set_should_receive(self: &VideoTrack, should_receive: bool);
63 fn should_receive(self: &VideoTrack) -> bool;
64 fn content_hint(self: &VideoTrack) -> ContentHint;
65 fn set_content_hint(self: &VideoTrack, hint: ContentHint);
66 fn new_native_video_sink(observer: Box<VideoSinkWrapper>) -> SharedPtr<NativeVideoSink>;
67
68 fn video_resolution(self: &VideoTrackSource) -> VideoResolution;
69 fn on_captured_frame(self: &VideoTrackSource, frame: &UniquePtr<VideoFrame>) -> bool;
70 fn new_video_track_source(
71 resolution: &VideoResolution,
72 is_screencast: bool,
73 ) -> SharedPtr<VideoTrackSource>;
74 fn video_to_media(track: SharedPtr<VideoTrack>) -> SharedPtr<MediaStreamTrack>;
75 unsafe fn media_to_video(track: SharedPtr<MediaStreamTrack>) -> SharedPtr<VideoTrack>;
76 fn _shared_video_track() -> SharedPtr<VideoTrack>;
77 }
78
79 extern "Rust" {
80 type VideoSinkWrapper;
81
82 fn on_frame(self: &VideoSinkWrapper, frame: UniquePtr<VideoFrame>);
83 fn on_discarded_frame(self: &VideoSinkWrapper);
84 fn on_constraints_changed(
85 self: &VideoSinkWrapper,
86 constraints: VideoTrackSourceConstraints,
87 );
88 }
89}
90
91impl_thread_safety!(ffi::VideoTrack, Send + Sync);
92impl_thread_safety!(ffi::NativeVideoSink, Send + Sync);
93impl_thread_safety!(ffi::VideoTrackSource, Send + Sync);
94
95pub trait VideoSink: Send {
96 fn on_frame(&self, frame: UniquePtr<VideoFrame>);
97 fn on_discarded_frame(&self);
98 fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints);
99}
100
101pub struct VideoSinkWrapper {
102 observer: Arc<dyn VideoSink>,
103}
104
105impl VideoSinkWrapper {
106 pub fn new(observer: Arc<dyn VideoSink>) -> Self {
107 Self { observer }
108 }
109
110 fn on_frame(&self, frame: UniquePtr<VideoFrame>) {
111 self.observer.on_frame(frame);
112 }
113
114 fn on_discarded_frame(&self) {
115 self.observer.on_discarded_frame();
116 }
117
118 fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints) {
119 self.observer.on_constraints_changed(constraints);
120 }
121}