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    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}