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 set_packet_trailer_handler(
88 self: &VideoTrackSource,
89 handler: SharedPtr<PacketTrailerHandler>,
90 );
91 fn new_video_track_source(
92 resolution: &VideoResolution,
93 is_screencast: bool,
94 ) -> SharedPtr<VideoTrackSource>;
95 fn video_to_media(track: SharedPtr<VideoTrack>) -> SharedPtr<MediaStreamTrack>;
96 unsafe fn media_to_video(track: SharedPtr<MediaStreamTrack>) -> SharedPtr<VideoTrack>;
97 fn _shared_video_track() -> SharedPtr<VideoTrack>;
98 }
99
100 extern "Rust" {
101 type VideoSinkWrapper;
102
103 fn on_frame(self: &VideoSinkWrapper, frame: UniquePtr<VideoFrame>);
104 fn on_discarded_frame(self: &VideoSinkWrapper);
105 fn on_constraints_changed(
106 self: &VideoSinkWrapper,
107 constraints: VideoTrackSourceConstraints,
108 );
109 }
110}
111
112impl_thread_safety!(ffi::VideoTrack, Send + Sync);
113impl_thread_safety!(ffi::NativeVideoSink, Send + Sync);
114impl_thread_safety!(ffi::VideoTrackSource, Send + Sync);
115
116pub trait VideoSink: Send {
117 fn on_frame(&self, frame: UniquePtr<VideoFrame>);
118 fn on_discarded_frame(&self);
119 fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints);
120}
121
122pub struct VideoSinkWrapper {
123 observer: Arc<dyn VideoSink>,
124}
125
126impl VideoSinkWrapper {
127 pub fn new(observer: Arc<dyn VideoSink>) -> Self {
128 Self { observer }
129 }
130
131 fn on_frame(&self, frame: UniquePtr<VideoFrame>) {
132 self.observer.on_frame(frame);
133 }
134
135 fn on_discarded_frame(&self) {
136 self.observer.on_discarded_frame();
137 }
138
139 fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints) {
140 self.observer.on_constraints_changed(constraints);
141 }
142}