// Copyright 2025 LiveKit, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::sync::Arc; use cxx::UniquePtr; use crate::{impl_thread_safety, video_frame::ffi::VideoFrame}; #[cxx::bridge(namespace = "livekit_ffi")] pub mod ffi { #[repr(i32)] pub enum ContentHint { None, Fluid, Detailed, Text, } #[derive(Debug)] pub struct VideoTrackSourceConstraints { pub has_min_fps: bool, pub min_fps: f64, pub has_max_fps: bool, pub max_fps: f64, } #[derive(Debug)] pub struct VideoResolution { pub width: u32, pub height: u32, } #[derive(Debug)] pub struct FrameMetadata { pub has_packet_trailer: bool, pub user_timestamp: u64, pub frame_id: u32, pub user_data: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(i32)] pub enum EncodedVideoCodec { H264, H265, VP8, VP9, AV1, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(i32)] pub enum EncodedFrameType { Key, Delta, } #[derive(Debug)] pub struct EncodedVideoFrameData { pub codec: EncodedVideoCodec, pub frame_type: EncodedFrameType, pub timestamp_us: i64, } #[derive(Debug)] pub struct EncodedRateControlRequest { pub has_request: bool, pub target_bitrate_bps: u64, pub framerate_fps: f64, } extern "C++" { include!("livekit/video_frame.h"); include!("livekit/media_stream_track.h"); type VideoFrame = crate::video_frame::ffi::VideoFrame; type MediaStreamTrack = crate::media_stream_track::ffi::MediaStreamTrack; } extern "C++" { include!("livekit/packet_trailer.h"); include!("livekit/video_track.h"); type PacketTrailerHandler = crate::packet_trailer::ffi::PacketTrailerHandler; } unsafe extern "C++" { type VideoTrack; type NativeVideoSink; type VideoTrackSource; fn add_sink(self: &VideoTrack, sink: &SharedPtr); fn remove_sink(self: &VideoTrack, sink: &SharedPtr); fn set_should_receive(self: &VideoTrack, should_receive: bool); fn should_receive(self: &VideoTrack) -> bool; fn content_hint(self: &VideoTrack) -> ContentHint; fn set_content_hint(self: &VideoTrack, hint: ContentHint); fn new_native_video_sink(observer: Box) -> SharedPtr; fn video_resolution(self: &VideoTrackSource) -> VideoResolution; fn on_captured_frame( self: &VideoTrackSource, frame: &UniquePtr, frame_metadata: &FrameMetadata, ) -> bool; fn capture_encoded_frame( self: &VideoTrackSource, width: i32, height: i32, frame: &EncodedVideoFrameData, payload: &[u8], frame_metadata: &FrameMetadata, ) -> bool; fn take_keyframe_request(self: &VideoTrackSource) -> bool; fn take_rate_control_request(self: &VideoTrackSource) -> EncodedRateControlRequest; fn set_packet_trailer_handler( self: &VideoTrackSource, handler: SharedPtr, ); fn new_video_track_source( resolution: &VideoResolution, is_screencast: bool, ) -> SharedPtr; fn video_to_media(track: SharedPtr) -> SharedPtr; unsafe fn media_to_video(track: SharedPtr) -> SharedPtr; fn _shared_video_track() -> SharedPtr; } extern "Rust" { type VideoSinkWrapper; fn on_frame(self: &VideoSinkWrapper, frame: UniquePtr); fn on_discarded_frame(self: &VideoSinkWrapper); fn on_constraints_changed( self: &VideoSinkWrapper, constraints: VideoTrackSourceConstraints, ); } } impl_thread_safety!(ffi::VideoTrack, Send + Sync); impl_thread_safety!(ffi::NativeVideoSink, Send + Sync); impl_thread_safety!(ffi::VideoTrackSource, Send + Sync); pub trait VideoSink: Send { fn on_frame(&self, frame: UniquePtr); fn on_discarded_frame(&self); fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints); } pub struct VideoSinkWrapper { observer: Arc, } impl VideoSinkWrapper { pub fn new(observer: Arc) -> Self { Self { observer } } fn on_frame(&self, frame: UniquePtr) { self.observer.on_frame(frame); } fn on_discarded_frame(&self) { self.observer.on_discarded_frame(); } fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints) { self.observer.on_constraints_changed(constraints); } }