Skip to main content

webrtc_sys/
video_track.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 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        pub user_data: Vec<u8>,
51    }
52
53    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
54    #[repr(i32)]
55    pub enum EncodedVideoCodec {
56        H264,
57        H265,
58        VP8,
59        VP9,
60        AV1,
61    }
62
63    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
64    #[repr(i32)]
65    pub enum EncodedFrameType {
66        Key,
67        Delta,
68    }
69
70    #[derive(Debug)]
71    pub struct EncodedVideoFrameData {
72        pub codec: EncodedVideoCodec,
73        pub frame_type: EncodedFrameType,
74        pub timestamp_us: i64,
75    }
76
77    #[derive(Debug)]
78    pub struct EncodedRateControlRequest {
79        pub has_request: bool,
80        pub target_bitrate_bps: u64,
81        pub framerate_fps: f64,
82    }
83
84    extern "C++" {
85        include!("livekit/video_frame.h");
86        include!("livekit/media_stream_track.h");
87
88        type VideoFrame = crate::video_frame::ffi::VideoFrame;
89        type MediaStreamTrack = crate::media_stream_track::ffi::MediaStreamTrack;
90    }
91
92    extern "C++" {
93        include!("livekit/packet_trailer.h");
94        include!("livekit/video_track.h");
95
96        type PacketTrailerHandler = crate::packet_trailer::ffi::PacketTrailerHandler;
97    }
98
99    unsafe extern "C++" {
100
101        type VideoTrack;
102        type NativeVideoSink;
103        type VideoTrackSource;
104
105        fn add_sink(self: &VideoTrack, sink: &SharedPtr<NativeVideoSink>);
106        fn remove_sink(self: &VideoTrack, sink: &SharedPtr<NativeVideoSink>);
107        fn set_should_receive(self: &VideoTrack, should_receive: bool);
108        fn should_receive(self: &VideoTrack) -> bool;
109        fn content_hint(self: &VideoTrack) -> ContentHint;
110        fn set_content_hint(self: &VideoTrack, hint: ContentHint);
111        fn new_native_video_sink(observer: Box<VideoSinkWrapper>) -> SharedPtr<NativeVideoSink>;
112
113        fn video_resolution(self: &VideoTrackSource) -> VideoResolution;
114        fn on_captured_frame(
115            self: &VideoTrackSource,
116            frame: &UniquePtr<VideoFrame>,
117            frame_metadata: &FrameMetadata,
118        ) -> bool;
119        fn capture_dmabuf_frame(
120            self: &VideoTrackSource,
121            dmabuf_fd: i32,
122            width: i32,
123            height: i32,
124            pixel_format: i32,
125            timestamp_us: i64,
126            frame_metadata: &FrameMetadata,
127        ) -> bool;
128        fn capture_encoded_frame(
129            self: &VideoTrackSource,
130            width: i32,
131            height: i32,
132            frame: &EncodedVideoFrameData,
133            payload: &[u8],
134            frame_metadata: &FrameMetadata,
135        ) -> bool;
136        fn take_keyframe_request(self: &VideoTrackSource) -> bool;
137        fn take_rate_control_request(self: &VideoTrackSource) -> EncodedRateControlRequest;
138        fn set_packet_trailer_handler(
139            self: &VideoTrackSource,
140            handler: SharedPtr<PacketTrailerHandler>,
141        );
142        fn new_video_track_source(
143            resolution: &VideoResolution,
144            is_screencast: bool,
145        ) -> SharedPtr<VideoTrackSource>;
146        fn video_to_media(track: SharedPtr<VideoTrack>) -> SharedPtr<MediaStreamTrack>;
147        unsafe fn media_to_video(track: SharedPtr<MediaStreamTrack>) -> SharedPtr<VideoTrack>;
148        fn _shared_video_track() -> SharedPtr<VideoTrack>;
149    }
150
151    extern "Rust" {
152        type VideoSinkWrapper;
153
154        fn on_frame(self: &VideoSinkWrapper, frame: UniquePtr<VideoFrame>);
155        fn on_discarded_frame(self: &VideoSinkWrapper);
156        fn on_constraints_changed(
157            self: &VideoSinkWrapper,
158            constraints: VideoTrackSourceConstraints,
159        );
160    }
161}
162
163impl_thread_safety!(ffi::VideoTrack, Send + Sync);
164impl_thread_safety!(ffi::NativeVideoSink, Send + Sync);
165impl_thread_safety!(ffi::VideoTrackSource, Send + Sync);
166
167pub trait VideoSink: Send {
168    fn on_frame(&self, frame: UniquePtr<VideoFrame>);
169    fn on_discarded_frame(&self);
170    fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints);
171}
172
173pub struct VideoSinkWrapper {
174    observer: Arc<dyn VideoSink>,
175}
176
177impl VideoSinkWrapper {
178    pub fn new(observer: Arc<dyn VideoSink>) -> Self {
179        Self { observer }
180    }
181
182    fn on_frame(&self, frame: UniquePtr<VideoFrame>) {
183        self.observer.on_frame(frame);
184    }
185
186    fn on_discarded_frame(&self) {
187        self.observer.on_discarded_frame();
188    }
189
190    fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints) {
191        self.observer.on_constraints_changed(constraints);
192    }
193}