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