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 #[derive(Debug)]
46 pub struct FrameMetadata {
47 pub has_packet_trailer: bool,
48 pub user_timestamp: u64,
49 pub frame_id: u32,
50 }
51
52 extern "C++" {
53 include!("livekit/video_frame.h");
54 include!("livekit/media_stream_track.h");
55
56 type VideoFrame = crate::video_frame::ffi::VideoFrame;
57 type MediaStreamTrack = crate::media_stream_track::ffi::MediaStreamTrack;
58 }
59
60 extern "C++" {
61 include!("livekit/packet_trailer.h");
62 include!("livekit/video_track.h");
63
64 type PacketTrailerHandler = crate::packet_trailer::ffi::PacketTrailerHandler;
65 }
66
67 unsafe extern "C++" {
68
69 type VideoTrack;
70 type NativeVideoSink;
71 type VideoTrackSource;
72
73 fn add_sink(self: &VideoTrack, sink: &SharedPtr<NativeVideoSink>);
74 fn remove_sink(self: &VideoTrack, sink: &SharedPtr<NativeVideoSink>);
75 fn set_should_receive(self: &VideoTrack, should_receive: bool);
76 fn should_receive(self: &VideoTrack) -> bool;
77 fn content_hint(self: &VideoTrack) -> ContentHint;
78 fn set_content_hint(self: &VideoTrack, hint: ContentHint);
79 fn new_native_video_sink(observer: Box<VideoSinkWrapper>) -> SharedPtr<NativeVideoSink>;
80
81 fn video_resolution(self: &VideoTrackSource) -> VideoResolution;
82 fn on_captured_frame(
83 self: &VideoTrackSource,
84 frame: &UniquePtr<VideoFrame>,
85 frame_metadata: &FrameMetadata,
86 ) -> bool;
87 fn capture_dmabuf_frame(
88 self: &VideoTrackSource,
89 dmabuf_fd: i32,
90 width: i32,
91 height: i32,
92 pixel_format: i32,
93 timestamp_us: i64,
94 frame_metadata: &FrameMetadata,
95 ) -> bool;
96 fn set_packet_trailer_handler(
97 self: &VideoTrackSource,
98 handler: SharedPtr<PacketTrailerHandler>,
99 );
100 fn new_video_track_source(
101 resolution: &VideoResolution,
102 is_screencast: bool,
103 ) -> SharedPtr<VideoTrackSource>;
104 fn video_to_media(track: SharedPtr<VideoTrack>) -> SharedPtr<MediaStreamTrack>;
105 unsafe fn media_to_video(track: SharedPtr<MediaStreamTrack>) -> SharedPtr<VideoTrack>;
106 fn _shared_video_track() -> SharedPtr<VideoTrack>;
107 }
108
109 extern "Rust" {
110 type VideoSinkWrapper;
111
112 fn on_frame(self: &VideoSinkWrapper, frame: UniquePtr<VideoFrame>);
113 fn on_discarded_frame(self: &VideoSinkWrapper);
114 fn on_constraints_changed(
115 self: &VideoSinkWrapper,
116 constraints: VideoTrackSourceConstraints,
117 );
118 }
119}
120
121impl_thread_safety!(ffi::VideoTrack, Send + Sync);
122impl_thread_safety!(ffi::NativeVideoSink, Send + Sync);
123impl_thread_safety!(ffi::VideoTrackSource, Send + Sync);
124
125pub trait VideoSink: Send {
126 fn on_frame(&self, frame: UniquePtr<VideoFrame>);
127 fn on_discarded_frame(&self);
128 fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints);
129}
130
131pub struct VideoSinkWrapper {
132 observer: Arc<dyn VideoSink>,
133}
134
135impl VideoSinkWrapper {
136 pub fn new(observer: Arc<dyn VideoSink>) -> Self {
137 Self { observer }
138 }
139
140 fn on_frame(&self, frame: UniquePtr<VideoFrame>) {
141 self.observer.on_frame(frame);
142 }
143
144 fn on_discarded_frame(&self) {
145 self.observer.on_discarded_frame();
146 }
147
148 fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints) {
149 self.observer.on_constraints_changed(constraints);
150 }
151}