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")]
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(resolution: &VideoResolution) -> SharedPtr<VideoTrackSource>;
71        fn video_to_media(track: SharedPtr<VideoTrack>) -> SharedPtr<MediaStreamTrack>;
72        unsafe fn media_to_video(track: SharedPtr<MediaStreamTrack>) -> SharedPtr<VideoTrack>;
73        fn _shared_video_track() -> SharedPtr<VideoTrack>;
74    }
75
76    extern "Rust" {
77        type VideoSinkWrapper;
78
79        fn on_frame(self: &VideoSinkWrapper, frame: UniquePtr<VideoFrame>);
80        fn on_discarded_frame(self: &VideoSinkWrapper);
81        fn on_constraints_changed(
82            self: &VideoSinkWrapper,
83            constraints: VideoTrackSourceConstraints,
84        );
85    }
86}
87
88impl_thread_safety!(ffi::VideoTrack, Send + Sync);
89impl_thread_safety!(ffi::NativeVideoSink, Send + Sync);
90impl_thread_safety!(ffi::VideoTrackSource, Send + Sync);
91
92pub trait VideoSink: Send {
93    fn on_frame(&self, frame: UniquePtr<VideoFrame>);
94    fn on_discarded_frame(&self);
95    fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints);
96}
97
98pub struct VideoSinkWrapper {
99    observer: Arc<dyn VideoSink>,
100}
101
102impl VideoSinkWrapper {
103    pub fn new(observer: Arc<dyn VideoSink>) -> Self {
104        Self { observer }
105    }
106
107    fn on_frame(&self, frame: UniquePtr<VideoFrame>) {
108        self.observer.on_frame(frame);
109    }
110
111    fn on_discarded_frame(&self) {
112        self.observer.on_discarded_frame();
113    }
114
115    fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints) {
116        self.observer.on_constraints_changed(constraints);
117    }
118}