publish client-sdk-native (#12)

* Create README.md

* add example

* crates & examples

* fix syntax

* add LICENSE

* thirdparty LICENSE

* rearrange repo

* fix build

* egui versions

* prepare publish

* fix demo compilation

* add test ci

* Update rust.yml

* forgot runs-on

* install protoc before building

* avoid rate limit

* include submodules

* updates to readme

* cache rust builds

Co-authored-by: David Zhao <[email protected]>
Co-authored-by: David Zhao <[email protected]>
This commit is contained in:
Théo Monnom
2023-01-02 20:13:48 +01:00
committed by GitHub
co-authored by David Zhao David Zhao
parent a927baac94
commit a07b3451a3
102 changed files with 893 additions and 344 deletions
+10
View File
@@ -0,0 +1,10 @@
//
// Created by Théo Monnom on 01/09/2022.
//
#include "livekit/candidate.h"
namespace livekit {
Candidate::Candidate(const cricket::Candidate& candidate)
: candidate_(candidate) {}
} // namespace livekit
+10
View File
@@ -0,0 +1,10 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
include!("livekit/candidate.h");
type Candidate; // cricket::Candidate
fn _unique_candidate() -> UniquePtr<Candidate>; // Ignore
}
}
+90
View File
@@ -0,0 +1,90 @@
//
// Created by Théo Monnom on 01/09/2022.
//
#include "livekit/data_channel.h"
#include <utility>
#include "webrtc-sys/src/data_channel.rs.h"
namespace livekit {
DataChannel::DataChannel(
std::shared_ptr<RTCRuntime> rtc_runtime,
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
: rtc_runtime_(std::move(rtc_runtime)),
data_channel_(std::move(data_channel)) {}
void DataChannel::register_observer(NativeDataChannelObserver& observer) {
data_channel_->RegisterObserver(&observer);
}
void DataChannel::unregister_observer() {
data_channel_->UnregisterObserver();
}
bool DataChannel::send(const DataBuffer& buffer) const {
return data_channel_->Send(webrtc::DataBuffer{
rtc::CopyOnWriteBuffer(buffer.ptr, buffer.len), buffer.binary});
}
rust::String DataChannel::label() const {
return data_channel_->label();
}
DataState DataChannel::state() const {
return static_cast<DataState>(data_channel_->state());
}
void DataChannel::close() const {
return data_channel_->Close();
}
std::unique_ptr<NativeDataChannelInit> create_data_channel_init(
DataChannelInit init) {
auto rtc_init = std::make_unique<webrtc::DataChannelInit>();
rtc_init->id = init.id;
rtc_init->negotiated = init.negotiated;
rtc_init->ordered = init.ordered;
rtc_init->protocol = init.protocol.c_str();
rtc_init->reliable = init.reliable;
if (init.has_max_retransmit_time)
rtc_init->maxRetransmitTime = init.max_retransmit_time;
if (init.has_max_retransmits)
rtc_init->maxRetransmits = init.max_retransmits;
if (init.has_priority)
rtc_init->priority = static_cast<webrtc::Priority>(init.priority);
return rtc_init;
}
NativeDataChannelObserver::NativeDataChannelObserver(
rust::Box<DataChannelObserverWrapper> observer)
: observer_(std::move(observer)) {}
void NativeDataChannelObserver::OnStateChange() {
observer_->on_state_change();
}
void NativeDataChannelObserver::OnMessage(const webrtc::DataBuffer& buffer) {
DataBuffer data{};
data.ptr = buffer.data.data();
data.len = buffer.data.size();
data.binary = buffer.binary;
observer_->on_message(data);
}
void NativeDataChannelObserver::OnBufferedAmountChange(
uint64_t sent_data_size) {
observer_->on_buffered_amount_change(sent_data_size);
}
std::unique_ptr<NativeDataChannelObserver> create_native_data_channel_observer(
rust::Box<DataChannelObserverWrapper> observer) {
return std::make_unique<NativeDataChannelObserver>(std::move(observer));
}
} // namespace livekit
+126
View File
@@ -0,0 +1,126 @@
use std::fmt::Debug;
use std::slice;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(i32)]
pub enum Priority {
VeryLow,
Low,
Medium,
High,
}
#[derive(Debug)]
pub struct DataChannelInit {
#[allow(deprecated)]
#[deprecated]
pub reliable: bool,
pub ordered: bool,
pub has_max_retransmit_time: bool,
pub max_retransmit_time: i32,
pub has_max_retransmits: bool,
pub max_retransmits: i32,
pub protocol: String,
pub negotiated: bool,
pub id: i32,
pub has_priority: bool,
pub priority: Priority,
}
#[derive(Debug)]
pub struct DataBuffer {
pub ptr: *const u8,
pub len: usize,
pub binary: bool,
}
#[derive(Debug)]
#[repr(i32)]
pub enum DataState {
Connecting,
Open,
Closing,
Closed,
}
extern "Rust" {
type DataChannelObserverWrapper;
fn on_state_change(self: &DataChannelObserverWrapper);
fn on_message(self: &DataChannelObserverWrapper, buffer: DataBuffer);
fn on_buffered_amount_change(self: &DataChannelObserverWrapper, sent_data_size: u64);
}
unsafe extern "C++" {
include!("livekit/data_channel.h");
type DataChannel;
type NativeDataChannelInit;
type NativeDataChannelObserver;
/// SAFETY
/// The observer must live as the datachannel uses it
unsafe fn register_observer(
self: Pin<&mut DataChannel>,
observer: Pin<&mut NativeDataChannelObserver>,
);
fn unregister_observer(self: Pin<&mut DataChannel>);
fn send(self: &DataChannel, data: &DataBuffer) -> bool;
fn label(self: &DataChannel) -> String;
fn state(self: &DataChannel) -> DataState;
fn close(self: &DataChannel);
fn create_data_channel_init(init: DataChannelInit) -> UniquePtr<NativeDataChannelInit>;
fn create_native_data_channel_observer(
observer: Box<DataChannelObserverWrapper>,
) -> UniquePtr<NativeDataChannelObserver>;
fn _unique_data_channel() -> UniquePtr<DataChannel>; // Ignore
}
}
unsafe impl Send for ffi::DataChannel {}
unsafe impl Sync for ffi::DataChannel {}
unsafe impl Send for ffi::NativeDataChannelObserver {}
unsafe impl Sync for ffi::NativeDataChannelObserver {}
// DataChannelObserver
pub trait DataChannelObserver: Send {
fn on_state_change(&self);
fn on_message(&self, data: &[u8], is_binary: bool);
fn on_buffered_amount_change(&self, sent_data_size: u64);
}
pub struct DataChannelObserverWrapper {
observer: *mut dyn DataChannelObserver,
}
impl DataChannelObserverWrapper {
/// SAFETY
/// DataChannelObserver must lives as long as DataChannelObserverWrapper does
pub unsafe fn new(observer: *mut dyn DataChannelObserver) -> Self {
Self { observer }
}
fn on_state_change(&self) {
unsafe {
(*self.observer).on_state_change();
}
}
fn on_message(&self, buffer: ffi::DataBuffer) {
unsafe {
let data = slice::from_raw_parts(buffer.ptr, buffer.len);
(*self.observer).on_message(data, buffer.binary);
}
}
fn on_buffered_amount_change(&self, sent_data_size: u64) {
unsafe { (*self.observer).on_buffered_amount_change(sent_data_size) };
}
}
+161
View File
@@ -0,0 +1,161 @@
//
// Created by Théo Monnom on 01/09/2022.
//
#include "livekit/jsep.h"
#include <iomanip>
#include <memory>
#include "webrtc-sys/src/jsep.rs.h"
#include "livekit/rtc_error.h"
namespace livekit {
std::string serialize_sdp_error(webrtc::SdpParseError error) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
ss << std::setw(8) << (uint32_t)error.line.length();
ss << std::dec << std::setw(1) << error.line;
ss << std::dec << std::setw(1) << error.description;
return ss.str();
}
IceCandidate::IceCandidate(
std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate)
: ice_candidate_(std::move(ice_candidate)) {}
rust::String IceCandidate::sdp_mid() const {
return ice_candidate_->sdp_mid();
}
int IceCandidate::sdp_mline_index() const {
return ice_candidate_->sdp_mline_index();
}
rust::String IceCandidate::candidate() const {
return stringify();
}
rust::String IceCandidate::stringify() const {
std::string str;
ice_candidate_->ToString(&str);
return rust::String{str};
}
std::unique_ptr<webrtc::IceCandidateInterface> IceCandidate::release() {
return std::move(ice_candidate_);
}
std::unique_ptr<IceCandidate> create_ice_candidate(rust::String sdp_mid,
int sdp_mline_index,
rust::String sdp) {
webrtc::SdpParseError error;
auto ice_rtc = webrtc::CreateIceCandidate(sdp_mid.c_str(), sdp_mline_index,
sdp.c_str(), &error);
if (!ice_rtc) {
throw std::runtime_error(serialize_sdp_error(error));
}
return std::make_unique<IceCandidate>(
std::unique_ptr<webrtc::IceCandidateInterface>(ice_rtc));
}
SessionDescription::SessionDescription(
std::unique_ptr<webrtc::SessionDescriptionInterface> session_description)
: session_description_(std::move(session_description)) {}
rust::String SessionDescription::stringify() const {
std::string str;
session_description_->ToString(&str);
return rust::String{str};
}
std::unique_ptr<SessionDescription> SessionDescription::clone() const {
return std::make_unique<SessionDescription>(session_description_->Clone());
}
std::unique_ptr<webrtc::SessionDescriptionInterface>
SessionDescription::release() {
return std::move(session_description_);
}
std::unique_ptr<SessionDescription> create_session_description(
SdpType type,
rust::String sdp) {
webrtc::SdpParseError error;
auto rtc_sdp = webrtc::CreateSessionDescription(
static_cast<webrtc::SdpType>(type), sdp.c_str(), &error);
if (!rtc_sdp) {
throw std::runtime_error(serialize_sdp_error(error));
}
return std::make_unique<SessionDescription>(std::move(rtc_sdp));
}
// CreateSdpObserver
NativeCreateSdpObserver::NativeCreateSdpObserver(
rust::Box<CreateSdpObserverWrapper> observer)
: observer_(std::move(observer)) {}
void NativeCreateSdpObserver::OnSuccess(
webrtc::SessionDescriptionInterface* desc) {
// We have ownership of desc
observer_->on_success(std::make_unique<SessionDescription>(
std::unique_ptr<webrtc::SessionDescriptionInterface>(desc)));
}
void NativeCreateSdpObserver::OnFailure(webrtc::RTCError error) {
observer_->on_failure(to_error(error));
}
std::unique_ptr<NativeCreateSdpObserverHandle>
create_native_create_sdp_observer(
rust::Box<CreateSdpObserverWrapper> observer) {
return std::make_unique<NativeCreateSdpObserverHandle>(
NativeCreateSdpObserverHandle{
rtc::make_ref_counted<NativeCreateSdpObserver>(std::move(observer))});
}
// SetLocalSdpObserver
NativeSetLocalSdpObserver::NativeSetLocalSdpObserver(
rust::Box<SetLocalSdpObserverWrapper> observer)
: observer_(std::move(observer)) {}
void NativeSetLocalSdpObserver::OnSetLocalDescriptionComplete(
webrtc::RTCError error) {
observer_->on_set_local_description_complete(to_error(error));
}
std::unique_ptr<NativeSetLocalSdpObserverHandle>
create_native_set_local_sdp_observer(
rust::Box<SetLocalSdpObserverWrapper> observer) {
return std::make_unique<NativeSetLocalSdpObserverHandle>(
NativeSetLocalSdpObserverHandle{
rtc::make_ref_counted<NativeSetLocalSdpObserver>(
std::move(observer))});
}
// SetRemoteSdpObserver
NativeSetRemoteSdpObserver::NativeSetRemoteSdpObserver(
rust::Box<SetRemoteSdpObserverWrapper> observer)
: observer_(std::move(observer)) {}
void NativeSetRemoteSdpObserver::OnSetRemoteDescriptionComplete(
webrtc::RTCError error) {
observer_->on_set_remote_description_complete(to_error(error));
}
std::unique_ptr<NativeSetRemoteSdpObserverHandle>
create_native_set_remote_sdp_observer(
rust::Box<SetRemoteSdpObserverWrapper> observer) {
return std::make_unique<NativeSetRemoteSdpObserverHandle>(
NativeSetRemoteSdpObserverHandle{
rtc::make_ref_counted<NativeSetRemoteSdpObserver>(
std::move(observer))});
}
} // namespace livekit
+210
View File
@@ -0,0 +1,210 @@
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::mem::ManuallyDrop;
use std::str::FromStr;
use cxx::UniquePtr;
use crate::rtc_error::ffi::RTCError;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(i32)]
pub enum SdpType {
Offer,
PrAnswer,
Answer,
Rollback,
}
#[derive(Debug)]
pub struct SdpParseError {
pub line: String,
pub description: String,
}
extern "Rust" {
type CreateSdpObserverWrapper;
fn on_success(
self: &CreateSdpObserverWrapper,
session_description: UniquePtr<SessionDescription>,
);
fn on_failure(self: &CreateSdpObserverWrapper, error: RTCError);
type SetLocalSdpObserverWrapper;
fn on_set_local_description_complete(self: &SetLocalSdpObserverWrapper, error: RTCError);
type SetRemoteSdpObserverWrapper;
fn on_set_remote_description_complete(self: &SetRemoteSdpObserverWrapper, error: RTCError);
}
unsafe extern "C++" {
include!("webrtc-sys/src/rtc_error.rs.h");
include!("livekit/jsep.h");
type RTCError = crate::rtc_error::ffi::RTCError;
type IceCandidate;
type SessionDescription;
type NativeCreateSdpObserverHandle;
type NativeSetLocalSdpObserverHandle;
type NativeSetRemoteSdpObserverHandle;
fn sdp_mid(self: &IceCandidate) -> String;
fn sdp_mline_index(self: &IceCandidate) -> i32;
fn candidate(self: &IceCandidate) -> String;
fn stringify(self: &IceCandidate) -> String;
fn stringify(self: &SessionDescription) -> String;
fn clone(self: &SessionDescription) -> UniquePtr<SessionDescription>;
fn create_native_create_sdp_observer(
observer: Box<CreateSdpObserverWrapper>,
) -> UniquePtr<NativeCreateSdpObserverHandle>;
fn create_native_set_local_sdp_observer(
observer: Box<SetLocalSdpObserverWrapper>,
) -> UniquePtr<NativeSetLocalSdpObserverHandle>;
fn create_native_set_remote_sdp_observer(
observer: Box<SetRemoteSdpObserverWrapper>,
) -> UniquePtr<NativeSetRemoteSdpObserverHandle>;
fn create_ice_candidate(
sdp_mid: String,
sdp_mline_index: i32,
sdp: String,
) -> Result<UniquePtr<IceCandidate>>;
fn create_session_description(
sdp_type: SdpType,
sdp: String,
) -> Result<UniquePtr<SessionDescription>>;
fn _unique_ice_candidate() -> UniquePtr<IceCandidate>; // Ignore
fn _unique_session_description() -> UniquePtr<SessionDescription>; // Ignore
}
}
impl Error for ffi::SdpParseError {}
impl Display for ffi::SdpParseError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"SdpParseError occurred {}: {}",
self.line, self.description
)
}
}
unsafe impl Send for ffi::SessionDescription {}
unsafe impl Sync for ffi::SessionDescription {}
unsafe impl Send for ffi::IceCandidate {}
unsafe impl Sync for ffi::IceCandidate {}
impl ffi::SdpParseError {
/// # Safety
/// The value must be correctly encoded
pub unsafe fn from(value: &str) -> Self {
// Parse the hex encoded error from c++
let line_length = u32::from_str_radix(&value[0..8], 16).unwrap() as usize + 8;
let line = String::from(&value[8..line_length]);
let description = String::from(&value[line_length..]);
Self { line, description }
}
}
impl FromStr for ffi::SdpType {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"offer" => Ok(ffi::SdpType::Offer),
"pranswer" => Ok(ffi::SdpType::PrAnswer),
"answer" => Ok(ffi::SdpType::Answer),
"rollback" => Ok(ffi::SdpType::Rollback),
_ => Err(()),
}
}
}
// CreateSdpObserver
pub struct CreateSdpObserverWrapper {
pub on_success: ManuallyDrop<Box<dyn FnOnce(UniquePtr<ffi::SessionDescription>) + Send>>,
pub on_failure: ManuallyDrop<Box<dyn FnOnce(RTCError) + Send>>,
}
impl CreateSdpObserverWrapper {
fn on_success(&self, session_description: UniquePtr<ffi::SessionDescription>) {
unsafe {
std::ptr::read(&*self.on_success)(session_description);
}
}
fn on_failure(&self, error: RTCError) {
unsafe {
std::ptr::read(&*self.on_failure)(error);
}
}
}
// SetLocalSdpObserver
pub struct SetLocalSdpObserverWrapper(pub ManuallyDrop<Box<dyn FnOnce(RTCError) + Send>>);
impl SetLocalSdpObserverWrapper {
fn on_set_local_description_complete(&self, error: RTCError) {
unsafe {
std::ptr::read(&*self.0)(error);
}
}
}
// SetRemoteSdpObserver
pub struct SetRemoteSdpObserverWrapper(pub ManuallyDrop<Box<dyn FnOnce(RTCError) + Send>>);
impl SetRemoteSdpObserverWrapper {
fn on_set_remote_description_complete(&self, error: RTCError) {
unsafe {
std::ptr::read(&*self.0)(error);
}
}
}
#[cfg(test)]
mod tests {
use log::info;
use crate::jsep::ffi;
#[test]
fn throw_error() {
let sdp_string = "v=0
o=- 6549709950142776241 2 IN IP4 127.0.0.1
s=-
t=0 0
======================== ERROR HERE
a=group:BUNDLE 0
a=extmap-allow-mixed
a=msid-semantic: WMS
m=application 9 UDP/DTLS/SCTP webrtc-datachannel
c=IN IP4 0.0.0.0
a=ice-ufrag:Tw7h
a=ice-pwd:6XOVUD6HpcB4c1M8EB8jXJE9
a=ice-options:trickle
a=fingerprint:sha-256 4F:EC:23:59:5D:A5:E6:3E:3E:5D:8A:09:B6:FA:04:AA:19:99:49:67:BD:65:93:06:BB:EE:AC:D5:21:0F:57:D6
a=setup:actpass
a=mid:0
a=sctp-port:5000
a=max-message-size:262144
";
let sdp = ffi::create_session_description(ffi::SdpType::Offer, sdp_string.to_string());
let err = unsafe { ffi::SdpParseError::from(sdp.err().unwrap().what()) };
info!("parse err: {:?}", err)
}
}
+17
View File
@@ -0,0 +1,17 @@
pub mod candidate;
pub mod data_channel;
pub mod jsep;
pub mod media_stream;
pub mod peer_connection;
pub mod peer_connection_factory;
pub mod rtc_error;
pub mod rtp_receiver;
pub mod rtp_transceiver;
pub mod video_frame;
pub mod video_frame_buffer;
pub mod webrtc;
pub mod yuv_helper;
pub const MEDIA_TYPE_VIDEO: &str = "video";
pub const MEDIA_TYPE_AUDIO: &str = "audio";
pub const MEDIA_TYPE_DATA: &str = "data";
+113
View File
@@ -0,0 +1,113 @@
//
// Created by Théo Monnom on 31/08/2022.
//
#include "livekit/media_stream.h"
#include "api/media_stream_interface.h"
#include "webrtc-sys/src/media_stream.rs.h"
namespace livekit {
MediaStreamTrack::MediaStreamTrack(
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track)
: track_(std::move(track)) {}
std::unique_ptr<MediaStreamTrack> MediaStreamTrack::from(
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track) {
if (track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
return std::make_unique<VideoTrack>(
rtc::scoped_refptr<webrtc::VideoTrackInterface>(
static_cast<webrtc::VideoTrackInterface*>(track.get())));
} else {
return std::make_unique<AudioTrack>(
rtc::scoped_refptr<webrtc::AudioTrackInterface>(
static_cast<webrtc::AudioTrackInterface*>(track.get())));
}
}
rust::String MediaStreamTrack::kind() const {
return track_->kind();
}
rust::String MediaStreamTrack::id() const {
return track_->id();
}
bool MediaStreamTrack::enabled() const {
return track_->enabled();
}
bool MediaStreamTrack::set_enabled(bool enable) {
return track_->set_enabled(enable);
}
TrackState MediaStreamTrack::state() const {
return static_cast<TrackState>(track_->state());
}
MediaStream::MediaStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
: media_stream_(std::move(stream)) {}
rust::String MediaStream::id() const {
return media_stream_->id();
}
AudioTrack::AudioTrack(rtc::scoped_refptr<webrtc::AudioTrackInterface> track)
: MediaStreamTrack(std::move(track)) {}
VideoTrack::VideoTrack(rtc::scoped_refptr<webrtc::VideoTrackInterface> track)
: MediaStreamTrack(std::move(track)) {}
void VideoTrack::add_sink(NativeVideoFrameSink& sink) {
track()->AddOrUpdateSink(&sink, rtc::VideoSinkWants());
}
void VideoTrack::remove_sink(NativeVideoFrameSink& sink) {
track()->RemoveSink(&sink);
}
void VideoTrack::set_should_receive(bool should_receive) {
track()->set_should_receive(should_receive);
}
bool VideoTrack::should_receive() const {
return track()->should_receive();
}
ContentHint VideoTrack::content_hint() const {
return static_cast<ContentHint>(track()->content_hint());
}
void VideoTrack::set_content_hint(ContentHint hint) {
track()->set_content_hint(
static_cast<webrtc::VideoTrackInterface::ContentHint>(hint));
}
NativeVideoFrameSink::NativeVideoFrameSink(
rust::Box<VideoFrameSinkWrapper> observer)
: observer_(std::move(observer)) {}
void NativeVideoFrameSink::OnFrame(const webrtc::VideoFrame& frame) {
observer_->on_frame(std::make_unique<VideoFrame>(frame));
}
void NativeVideoFrameSink::OnDiscardedFrame() {
observer_->on_discarded_frame();
}
void NativeVideoFrameSink::OnConstraintsChanged(
const webrtc::VideoTrackSourceConstraints& constraints) {
VideoTrackSourceConstraints cst;
cst.min_fps = constraints.min_fps.value_or(-1);
cst.max_fps = constraints.max_fps.value_or(-1);
observer_->on_constraints_changed(cst);
}
std::unique_ptr<NativeVideoFrameSink> create_native_video_frame_sink(
rust::Box<VideoFrameSinkWrapper> observer) {
return std::make_unique<NativeVideoFrameSink>(std::move(observer));
}
} // namespace livekit
+129
View File
@@ -0,0 +1,129 @@
use cxx::UniquePtr;
use crate::video_frame::ffi::VideoFrame;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(i32)]
pub enum TrackState {
Live,
Ended,
}
#[derive(Debug)]
#[repr(i32)]
pub enum ContentHint {
None,
Fluid,
Detailed,
Text,
}
// -1 = optional
pub struct VideoTrackSourceConstraints {
pub min_fps: f64,
pub max_fps: f64,
}
unsafe extern "C++" {
include!("livekit/media_stream.h");
include!("livekit/video_frame.h");
type NativeVideoFrameSink;
type MediaStreamTrack;
type MediaStream;
type AudioTrack;
type VideoTrack;
type VideoFrame = crate::video_frame::ffi::VideoFrame;
fn id(self: &MediaStream) -> String;
fn kind(self: &MediaStreamTrack) -> String;
fn id(self: &MediaStreamTrack) -> String;
fn enabled(self: &MediaStreamTrack) -> bool;
fn set_enabled(self: Pin<&mut MediaStreamTrack>, enable: bool) -> bool;
fn state(self: &MediaStreamTrack) -> TrackState;
unsafe fn add_sink(self: Pin<&mut VideoTrack>, sink: Pin<&mut NativeVideoFrameSink>);
unsafe fn remove_sink(self: Pin<&mut VideoTrack>, sink: Pin<&mut NativeVideoFrameSink>);
fn set_should_receive(self: Pin<&mut VideoTrack>, should_receive: bool);
fn should_receive(self: &VideoTrack) -> bool;
fn content_hint(self: &VideoTrack) -> ContentHint;
fn set_content_hint(self: Pin<&mut VideoTrack>, hint: ContentHint);
fn create_native_video_frame_sink(
observer: Box<VideoFrameSinkWrapper>,
) -> UniquePtr<NativeVideoFrameSink>;
unsafe fn video_to_media(track: *const VideoTrack) -> *const MediaStreamTrack;
unsafe fn audio_to_media(track: *const AudioTrack) -> *const MediaStreamTrack;
unsafe fn media_to_video(track: *const MediaStreamTrack) -> *const VideoTrack;
unsafe fn media_to_audio(track: *const MediaStreamTrack) -> *const AudioTrack;
fn _unique_media_stream_track() -> UniquePtr<MediaStreamTrack>; // Ignore
fn _unique_media_stream() -> UniquePtr<MediaStream>; // Ignore
fn _unique_audio_track() -> UniquePtr<AudioTrack>; // Ignore
fn _unique_video_track() -> UniquePtr<VideoTrack>; // Ignore
}
extern "Rust" {
type VideoFrameSinkWrapper;
fn on_frame(self: &VideoFrameSinkWrapper, frame: UniquePtr<VideoFrame>);
fn on_discarded_frame(self: &VideoFrameSinkWrapper);
fn on_constraints_changed(
self: &VideoFrameSinkWrapper,
constraints: VideoTrackSourceConstraints,
);
}
}
unsafe impl Sync for ffi::MediaStreamTrack {}
unsafe impl Send for ffi::MediaStreamTrack {}
unsafe impl Sync for ffi::MediaStream {}
unsafe impl Send for ffi::MediaStream {}
unsafe impl Send for ffi::AudioTrack {}
unsafe impl Sync for ffi::AudioTrack {}
unsafe impl Send for ffi::VideoTrack {}
unsafe impl Sync for ffi::VideoTrack {}
unsafe impl Send for ffi::NativeVideoFrameSink {}
unsafe impl Sync for ffi::NativeVideoFrameSink {}
pub trait VideoFrameSink: Send + Sync {
fn on_frame(&self, frame: UniquePtr<VideoFrame>);
fn on_discarded_frame(&self);
fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints);
}
pub struct VideoFrameSinkWrapper {
observer: *mut dyn VideoFrameSink,
}
impl VideoFrameSinkWrapper {
/// # Safety
/// VideoFrameSink must lives as long as VideoSinkInterfaceWrapper does
pub unsafe fn new(observer: *mut dyn VideoFrameSink) -> Self {
Self { observer }
}
fn on_frame(&self, frame: UniquePtr<VideoFrame>) {
unsafe {
(*self.observer).on_frame(frame);
}
}
fn on_discarded_frame(&self) {
unsafe {
(*self.observer).on_discarded_frame();
}
}
fn on_constraints_changed(&self, constraints: ffi::VideoTrackSourceConstraints) {
unsafe {
(*self.observer).on_constraints_changed(constraints);
}
}
}
+273
View File
@@ -0,0 +1,273 @@
//
// Created by Théo Monnom on 30/08/2022.
//
#include "livekit/peer_connection.h"
#include "livekit/media_stream.h"
#include "webrtc-sys/src/peer_connection.rs.h"
#include "livekit/rtc_error.h"
namespace livekit {
inline webrtc::PeerConnectionInterface::RTCOfferAnswerOptions
toNativeOfferAnswerOptions(const RTCOfferAnswerOptions& options) {
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtc_options;
rtc_options.offer_to_receive_video = options.offer_to_receive_video;
rtc_options.offer_to_receive_audio = options.offer_to_receive_audio;
rtc_options.voice_activity_detection = options.voice_activity_detection;
rtc_options.ice_restart = options.ice_restart;
rtc_options.use_rtp_mux = options.use_rtp_mux;
rtc_options.raw_packetization_for_video = options.raw_packetization_for_video;
rtc_options.num_simulcast_layers = options.num_simulcast_layers;
rtc_options.use_obsolete_sctp_sdp = options.use_obsolete_sctp_sdp;
return rtc_options;
}
PeerConnection::PeerConnection(
std::shared_ptr<RTCRuntime> rtc_runtime,
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection)
: rtc_runtime_(std::move(rtc_runtime)),
peer_connection_(std::move(peer_connection)) {}
void PeerConnection::create_offer(
NativeCreateSdpObserverHandle& observer_handle,
RTCOfferAnswerOptions options) {
peer_connection_->CreateOffer(observer_handle.observer.get(),
toNativeOfferAnswerOptions(options));
}
void PeerConnection::create_answer(
NativeCreateSdpObserverHandle& observer_handle,
RTCOfferAnswerOptions options) {
peer_connection_->CreateAnswer(observer_handle.observer.get(),
toNativeOfferAnswerOptions(options));
}
void PeerConnection::set_local_description(
std::unique_ptr<SessionDescription> desc,
NativeSetLocalSdpObserverHandle& observer) {
peer_connection_->SetLocalDescription(desc->clone()->release(),
observer.observer);
}
void PeerConnection::set_remote_description(
std::unique_ptr<SessionDescription> desc,
NativeSetRemoteSdpObserverHandle& observer) {
peer_connection_->SetRemoteDescription(desc->clone()->release(),
observer.observer);
}
std::unique_ptr<DataChannel> PeerConnection::create_data_channel(
rust::String label,
std::unique_ptr<NativeDataChannelInit> init) {
auto result =
peer_connection_->CreateDataChannelOrError(label.c_str(), init.get());
if (!result.ok()) {
throw std::runtime_error(serialize_error(to_error(result.error())));
}
return std::make_unique<DataChannel>(rtc_runtime_, result.value());
}
void PeerConnection::add_ice_candidate(
std::unique_ptr<IceCandidate> candidate,
NativeAddIceCandidateObserver& observer) {
peer_connection_->AddIceCandidate(
candidate->release(),
[&](const webrtc::RTCError& err) { observer.OnComplete(to_error(err)); });
}
std::unique_ptr<SessionDescription> PeerConnection::local_description() const {
auto local_description = peer_connection_->local_description();
if (local_description)
return std::make_unique<SessionDescription>(local_description->Clone());
return std::unique_ptr<SessionDescription>();
}
std::unique_ptr<SessionDescription> PeerConnection::remote_description() const {
auto remote_description = peer_connection_->remote_description();
if (remote_description)
return std::make_unique<SessionDescription>(remote_description->Clone());
return std::unique_ptr<SessionDescription>();
}
SignalingState PeerConnection::signaling_state() const {
return static_cast<SignalingState>(peer_connection_->signaling_state());
}
IceGatheringState PeerConnection::ice_gathering_state() const {
return static_cast<IceGatheringState>(
peer_connection_->ice_gathering_state());
}
IceConnectionState PeerConnection::ice_connection_state() const {
return static_cast<IceConnectionState>(
peer_connection_->ice_connection_state());
}
void PeerConnection::close() {
peer_connection_->Close();
}
// AddIceCandidateObserver
NativeAddIceCandidateObserver::NativeAddIceCandidateObserver(
rust::Box<AddIceCandidateObserverWrapper> observer)
: observer_(std::move(observer)) {}
void NativeAddIceCandidateObserver::OnComplete(const RTCError& error) {
observer_->on_complete(error);
}
std::unique_ptr<NativeAddIceCandidateObserver>
create_native_add_ice_candidate_observer(
rust::Box<AddIceCandidateObserverWrapper> observer) {
return std::make_unique<NativeAddIceCandidateObserver>(std::move(observer));
}
// PeerConnectionObserver
NativePeerConnectionObserver::NativePeerConnectionObserver(
std::shared_ptr<RTCRuntime> rtc_runtime,
rust::Box<PeerConnectionObserverWrapper> observer)
: rtc_runtime_(std::move(rtc_runtime)), observer_(std::move(observer)) {}
void NativePeerConnectionObserver::OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) {
observer_->on_signaling_change(static_cast<SignalingState>(new_state));
}
void NativePeerConnectionObserver::OnAddStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
observer_->on_add_stream(std::make_unique<MediaStream>(stream));
}
void NativePeerConnectionObserver::OnRemoveStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
observer_->on_remove_stream(std::make_unique<MediaStream>(stream));
}
void NativePeerConnectionObserver::OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
observer_->on_data_channel(
std::make_unique<DataChannel>(rtc_runtime_, data_channel));
}
void NativePeerConnectionObserver::OnRenegotiationNeeded() {
observer_->on_renegotiation_needed();
}
void NativePeerConnectionObserver::OnNegotiationNeededEvent(uint32_t event_id) {
observer_->on_negotiation_needed_event(event_id);
}
void NativePeerConnectionObserver::OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
observer_->on_ice_connection_change(
static_cast<IceConnectionState>(new_state));
}
void NativePeerConnectionObserver::OnStandardizedIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
observer_->on_standardized_ice_connection_change(
static_cast<IceConnectionState>(new_state));
}
void NativePeerConnectionObserver::OnConnectionChange(
webrtc::PeerConnectionInterface::PeerConnectionState new_state) {
observer_->on_connection_change(static_cast<PeerConnectionState>(new_state));
}
void NativePeerConnectionObserver::OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state) {
observer_->on_ice_gathering_change(static_cast<IceGatheringState>(new_state));
}
void NativePeerConnectionObserver::OnIceCandidate(
const webrtc::IceCandidateInterface* candidate) {
auto new_candidate = webrtc::CreateIceCandidate(candidate->sdp_mid(),
candidate->sdp_mline_index(),
candidate->candidate());
observer_->on_ice_candidate(
std::make_unique<IceCandidate>(std::move(new_candidate)));
}
void NativePeerConnectionObserver::OnIceCandidateError(
const std::string& address,
int port,
const std::string& url,
int error_code,
const std::string& error_text) {
observer_->on_ice_candidate_error(address, port, url, error_code, error_text);
}
void NativePeerConnectionObserver::OnIceCandidatesRemoved(
const std::vector<cricket::Candidate>& candidates) {
rust::Vec<CandidatePtr> vec;
for (const auto& item : candidates) {
vec.push_back(CandidatePtr{std::make_unique<Candidate>(item)});
}
observer_->on_ice_candidates_removed(std::move(vec));
}
void NativePeerConnectionObserver::OnIceConnectionReceivingChange(
bool receiving) {
observer_->on_ice_connection_receiving_change(receiving);
}
void NativePeerConnectionObserver::OnIceSelectedCandidatePairChanged(
const cricket::CandidatePairChangeEvent& event) {
CandidatePairChangeEvent e;
e.selected_candidate_pair.local =
std::make_unique<Candidate>(event.selected_candidate_pair.local);
e.selected_candidate_pair.remote =
std::make_unique<Candidate>(event.selected_candidate_pair.remote);
e.last_data_received_ms = event.last_data_received_ms;
e.reason = event.reason;
e.estimated_disconnected_time_ms = event.estimated_disconnected_time_ms;
observer_->on_ice_selected_candidate_pair_changed(std::move(e));
}
void NativePeerConnectionObserver::OnAddTrack(
rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
streams) {
rust::Vec<MediaStreamPtr> vec;
for (const auto& item : streams) {
vec.push_back(MediaStreamPtr{std::make_unique<MediaStream>(item)});
}
observer_->on_add_track(std::make_unique<RtpReceiver>(receiver),
std::move(vec));
}
void NativePeerConnectionObserver::OnTrack(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) {
observer_->on_track(std::make_unique<RtpTransceiver>(transceiver));
}
void NativePeerConnectionObserver::OnRemoveTrack(
rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) {
observer_->on_remove_track(std::make_unique<RtpReceiver>(receiver));
}
void NativePeerConnectionObserver::OnInterestingUsage(int usage_pattern) {
observer_->on_interesting_usage(usage_pattern);
}
std::unique_ptr<NativePeerConnectionObserver>
create_native_peer_connection_observer(
std::shared_ptr<RTCRuntime> rtc_runtime,
rust::Box<PeerConnectionObserverWrapper> observer) {
return std::make_unique<NativePeerConnectionObserver>(rtc_runtime,
std::move(observer));
}
} // namespace livekit
+485
View File
@@ -0,0 +1,485 @@
use std::fmt::Debug;
use std::mem::ManuallyDrop;
use cxx::UniquePtr;
use crate::candidate::ffi::Candidate;
use crate::data_channel::ffi::DataChannel;
use crate::jsep::ffi::IceCandidate;
use crate::media_stream::ffi::MediaStream;
use crate::rtc_error::ffi::RTCError;
use crate::rtp_receiver::ffi::RtpReceiver;
use crate::rtp_transceiver::ffi::RtpTransceiver;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
struct CandidatePair {
local: UniquePtr<Candidate>,
remote: UniquePtr<Candidate>,
}
struct CandidatePairChangeEvent {
selected_candidate_pair: CandidatePair,
last_data_received_ms: i64,
reason: String,
estimated_disconnected_time_ms: i64,
}
#[derive(Debug)]
#[repr(i32)]
pub enum PeerConnectionState {
New,
Connecting,
Connected,
Disconnected,
Failed,
Closed,
}
#[derive(Debug)]
#[repr(i32)]
pub enum SignalingState {
Stable,
HaveLocalOffer,
HaveLocalPrAnswer,
HaveRemoteOffer,
HaveRemotePrAnswer,
Closed,
}
#[derive(Debug)]
#[repr(i32)]
pub enum IceConnectionState {
IceConnectionNew,
IceConnectionChecking,
IceConnectionConnected,
IceConnectionCompleted,
IceConnectionFailed,
IceConnectionDisconnected,
IceConnectionClosed,
IceConnectionMax,
}
#[derive(Debug)]
#[repr(i32)]
pub enum IceGatheringState {
IceGatheringNew,
IceGatheringGathering,
IceGatheringComplete,
}
#[derive(Debug)]
pub struct RTCOfferAnswerOptions {
offer_to_receive_video: i32,
offer_to_receive_audio: i32,
voice_activity_detection: bool,
ice_restart: bool,
use_rtp_mux: bool,
raw_packetization_for_video: bool,
num_simulcast_layers: i32,
use_obsolete_sctp_sdp: bool,
}
// Wrapper to opaque C++ objects
// https://github.com/dtolnay/cxx/issues/741
struct MediaStreamPtr {
pub ptr: UniquePtr<MediaStream>,
}
struct CandidatePtr {
pub ptr: UniquePtr<Candidate>,
}
unsafe extern "C++" {
include!("livekit/peer_connection.h");
include!("livekit/jsep.h");
include!("livekit/data_channel.h");
include!("livekit/rtp_receiver.h");
include!("livekit/rtp_transceiver.h");
include!("livekit/media_stream.h");
include!("livekit/candidate.h");
include!("webrtc-sys/src/rtc_error.rs.h");
type RTCError = crate::rtc_error::ffi::RTCError;
type Candidate = crate::candidate::ffi::Candidate;
type IceCandidate = crate::jsep::ffi::IceCandidate;
type DataChannel = crate::data_channel::ffi::DataChannel;
type RtpReceiver = crate::rtp_receiver::ffi::RtpReceiver;
type RtpTransceiver = crate::rtp_transceiver::ffi::RtpTransceiver;
type MediaStream = crate::media_stream::ffi::MediaStream;
type NativeCreateSdpObserverHandle = crate::jsep::ffi::NativeCreateSdpObserverHandle;
type NativeSetLocalSdpObserverHandle = crate::jsep::ffi::NativeSetLocalSdpObserverHandle;
type NativeSetRemoteSdpObserverHandle = crate::jsep::ffi::NativeSetRemoteSdpObserverHandle;
type NativeDataChannelInit = crate::data_channel::ffi::NativeDataChannelInit;
type SessionDescription = crate::jsep::ffi::SessionDescription;
type RTCRuntime = crate::webrtc::ffi::RTCRuntime;
type NativeAddIceCandidateObserver;
type NativePeerConnectionObserver;
type PeerConnection;
/// SAFETY
/// The observer must live as long as the operation ends
unsafe fn create_offer(
self: Pin<&mut PeerConnection>,
observer: Pin<&mut NativeCreateSdpObserverHandle>,
options: RTCOfferAnswerOptions,
);
/// SAFETY
/// The observer must live as long as the operation ends
unsafe fn create_answer(
self: Pin<&mut PeerConnection>,
observer: Pin<&mut NativeCreateSdpObserverHandle>,
options: RTCOfferAnswerOptions,
);
/// SAFETY
/// The observer must live as long as the operation ends
unsafe fn set_local_description(
self: Pin<&mut PeerConnection>,
desc: UniquePtr<SessionDescription>,
observer: Pin<&mut NativeSetLocalSdpObserverHandle>,
);
/// SAFETY
/// The observer must live as long as the operation ends
unsafe fn set_remote_description(
self: Pin<&mut PeerConnection>,
desc: UniquePtr<SessionDescription>,
observer: Pin<&mut NativeSetRemoteSdpObserverHandle>,
);
fn create_data_channel(
self: Pin<&mut PeerConnection>,
label: String,
init: UniquePtr<NativeDataChannelInit>,
) -> Result<UniquePtr<DataChannel>>;
fn add_ice_candidate(
self: Pin<&mut PeerConnection>,
candidate: UniquePtr<IceCandidate>,
observer: Pin<&mut NativeAddIceCandidateObserver>,
);
fn local_description(self: &PeerConnection) -> UniquePtr<SessionDescription>;
fn remote_description(self: &PeerConnection) -> UniquePtr<SessionDescription>;
fn signaling_state(self: &PeerConnection) -> SignalingState;
fn ice_gathering_state(self: &PeerConnection) -> IceGatheringState;
fn ice_connection_state(self: &PeerConnection) -> IceConnectionState;
fn close(self: Pin<&mut PeerConnection>);
fn create_native_peer_connection_observer(
rtc_runtime: SharedPtr<RTCRuntime>,
observer: Box<PeerConnectionObserverWrapper>,
) -> UniquePtr<NativePeerConnectionObserver>;
fn create_native_add_ice_candidate_observer(
observer: Box<AddIceCandidateObserverWrapper>,
) -> UniquePtr<NativeAddIceCandidateObserver>;
fn _unique_peer_connection() -> UniquePtr<PeerConnection>; // Ignore
}
extern "Rust" {
type AddIceCandidateObserverWrapper;
fn on_complete(self: &AddIceCandidateObserverWrapper, error: RTCError);
type PeerConnectionObserverWrapper;
fn on_signaling_change(self: &PeerConnectionObserverWrapper, new_state: SignalingState);
fn on_add_stream(self: &PeerConnectionObserverWrapper, stream: UniquePtr<MediaStream>);
fn on_remove_stream(self: &PeerConnectionObserverWrapper, stream: UniquePtr<MediaStream>);
fn on_data_channel(
self: &PeerConnectionObserverWrapper,
data_channel: UniquePtr<DataChannel>,
);
fn on_renegotiation_needed(self: &PeerConnectionObserverWrapper);
fn on_negotiation_needed_event(self: &PeerConnectionObserverWrapper, event: u32);
fn on_ice_connection_change(
self: &PeerConnectionObserverWrapper,
new_state: IceConnectionState,
);
fn on_standardized_ice_connection_change(
self: &PeerConnectionObserverWrapper,
new_state: IceConnectionState,
);
fn on_connection_change(
self: &PeerConnectionObserverWrapper,
new_state: PeerConnectionState,
);
fn on_ice_gathering_change(
self: &PeerConnectionObserverWrapper,
new_state: IceGatheringState,
);
fn on_ice_candidate(
self: &PeerConnectionObserverWrapper,
candidate: UniquePtr<IceCandidate>,
);
fn on_ice_candidate_error(
self: &PeerConnectionObserverWrapper,
address: String,
port: i32,
url: String,
error_code: i32,
error_text: String,
);
fn on_ice_candidates_removed(
self: &PeerConnectionObserverWrapper,
removed: Vec<CandidatePtr>,
);
fn on_ice_connection_receiving_change(
self: &PeerConnectionObserverWrapper,
receiving: bool,
);
fn on_ice_selected_candidate_pair_changed(
self: &PeerConnectionObserverWrapper,
event: CandidatePairChangeEvent,
);
fn on_add_track(
self: &PeerConnectionObserverWrapper,
receiver: UniquePtr<RtpReceiver>,
streams: Vec<MediaStreamPtr>,
);
fn on_track(self: &PeerConnectionObserverWrapper, transceiver: UniquePtr<RtpTransceiver>);
fn on_remove_track(self: &PeerConnectionObserverWrapper, receiver: UniquePtr<RtpReceiver>);
fn on_interesting_usage(self: &PeerConnectionObserverWrapper, usage_pattern: i32);
}
}
// https://webrtc.github.io/webrtc-org/native-code/native-apis/
unsafe impl Send for ffi::PeerConnection {}
unsafe impl Sync for ffi::PeerConnection {}
unsafe impl Send for ffi::NativePeerConnectionObserver {}
unsafe impl Sync for ffi::NativePeerConnectionObserver {}
unsafe impl Sync for ffi::NativeAddIceCandidateObserver {}
unsafe impl Send for ffi::NativeAddIceCandidateObserver {}
unsafe impl Sync for ffi::NativeSetRemoteSdpObserverHandle {}
unsafe impl Send for ffi::NativeSetRemoteSdpObserverHandle {}
unsafe impl Sync for ffi::NativeSetLocalSdpObserverHandle {}
unsafe impl Send for ffi::NativeSetLocalSdpObserverHandle {}
unsafe impl Sync for ffi::NativeCreateSdpObserverHandle {}
unsafe impl Send for ffi::NativeCreateSdpObserverHandle {}
impl Default for ffi::RTCOfferAnswerOptions {
/*
static const int kUndefined = -1;
static const int kMaxOfferToReceiveMedia = 1;
static const int kOfferToReceiveMediaTrue = 1;
*/
fn default() -> Self {
Self {
offer_to_receive_video: -1,
offer_to_receive_audio: -1,
voice_activity_detection: true,
ice_restart: false,
use_rtp_mux: true,
raw_packetization_for_video: false,
num_simulcast_layers: 1,
use_obsolete_sctp_sdp: false,
}
}
}
pub struct AddIceCandidateObserverWrapper(pub ManuallyDrop<Box<dyn FnOnce(RTCError) + Send>>);
impl AddIceCandidateObserverWrapper {
fn on_complete(&self, error: RTCError) {
unsafe {
std::ptr::read(&*self.0)(error);
}
}
}
pub trait PeerConnectionObserver: Send + Sync {
fn on_signaling_change(&self, new_state: ffi::SignalingState);
fn on_add_stream(&self, stream: UniquePtr<MediaStream>);
fn on_remove_stream(&self, stream: UniquePtr<MediaStream>);
fn on_data_channel(&self, data_channel: UniquePtr<DataChannel>);
fn on_renegotiation_needed(&self);
fn on_negotiation_needed_event(&self, event: u32);
fn on_ice_connection_change(&self, new_state: ffi::IceConnectionState);
fn on_standardized_ice_connection_change(&self, new_state: ffi::IceConnectionState);
fn on_connection_change(&self, new_state: ffi::PeerConnectionState);
fn on_ice_gathering_change(&self, new_state: ffi::IceGatheringState);
fn on_ice_candidate(&self, candidate: UniquePtr<IceCandidate>);
fn on_ice_candidate_error(
&self,
address: String,
port: i32,
url: String,
error_code: i32,
error_text: String,
);
fn on_ice_candidates_removed(&self, removed: Vec<UniquePtr<Candidate>>);
fn on_ice_connection_receiving_change(&self, receiving: bool);
fn on_ice_selected_candidate_pair_changed(&self, event: ffi::CandidatePairChangeEvent);
fn on_add_track(&self, receiver: UniquePtr<RtpReceiver>, streams: Vec<UniquePtr<MediaStream>>);
fn on_track(&self, transceiver: UniquePtr<RtpTransceiver>);
fn on_remove_track(&self, receiver: UniquePtr<RtpReceiver>);
fn on_interesting_usage(&self, usage_pattern: i32);
}
// Thread safety is handled inside PeerConnectionObserver
pub struct PeerConnectionObserverWrapper {
observer: *mut dyn PeerConnectionObserver,
}
impl PeerConnectionObserverWrapper {
/// # Safety
/// PeerConnectionObserver must lives as long as PeerConnectionObserverWrapper does
pub unsafe fn new(observer: *mut dyn PeerConnectionObserver) -> Self {
Self { observer }
}
fn on_signaling_change(&self, new_state: ffi::SignalingState) {
unsafe {
(*self.observer).on_signaling_change(new_state);
}
}
fn on_add_stream(&self, stream: UniquePtr<MediaStream>) {
unsafe {
(*self.observer).on_add_stream(stream);
}
}
fn on_remove_stream(&self, stream: UniquePtr<MediaStream>) {
unsafe {
(*self.observer).on_remove_stream(stream);
}
}
fn on_data_channel(&self, data_channel: UniquePtr<DataChannel>) {
unsafe {
(*self.observer).on_data_channel(data_channel);
}
}
fn on_renegotiation_needed(&self) {
unsafe {
(*self.observer).on_renegotiation_needed();
}
}
fn on_negotiation_needed_event(&self, event: u32) {
unsafe {
(*self.observer).on_negotiation_needed_event(event);
}
}
fn on_ice_connection_change(&self, new_state: ffi::IceConnectionState) {
unsafe {
(*self.observer).on_ice_connection_change(new_state);
}
}
fn on_standardized_ice_connection_change(&self, new_state: ffi::IceConnectionState) {
unsafe {
(*self.observer).on_standardized_ice_connection_change(new_state);
}
}
fn on_connection_change(&self, new_state: ffi::PeerConnectionState) {
unsafe {
(*self.observer).on_connection_change(new_state);
}
}
fn on_ice_gathering_change(&self, new_state: ffi::IceGatheringState) {
unsafe {
(*self.observer).on_ice_gathering_change(new_state);
}
}
fn on_ice_candidate(&self, candidate: UniquePtr<IceCandidate>) {
unsafe {
(*self.observer).on_ice_candidate(candidate);
}
}
fn on_ice_candidate_error(
&self,
address: String,
port: i32,
url: String,
error_code: i32,
error_text: String,
) {
unsafe {
(*self.observer).on_ice_candidate_error(address, port, url, error_code, error_text);
}
}
fn on_ice_candidates_removed(&self, removed: Vec<ffi::CandidatePtr>) {
let mut vec = Vec::new();
for v in removed {
vec.push(v.ptr);
}
unsafe {
(*self.observer).on_ice_candidates_removed(vec);
}
}
fn on_ice_connection_receiving_change(&self, receiving: bool) {
unsafe {
(*self.observer).on_ice_connection_receiving_change(receiving);
}
}
fn on_ice_selected_candidate_pair_changed(&self, event: ffi::CandidatePairChangeEvent) {
unsafe {
(*self.observer).on_ice_selected_candidate_pair_changed(event);
}
}
fn on_add_track(&self, receiver: UniquePtr<RtpReceiver>, streams: Vec<ffi::MediaStreamPtr>) {
let mut vec = Vec::new();
for v in streams {
vec.push(v.ptr);
}
unsafe {
(*self.observer).on_add_track(receiver, vec);
}
}
fn on_track(&self, transceiver: UniquePtr<RtpTransceiver>) {
unsafe {
(*self.observer).on_track(transceiver);
}
}
fn on_remove_track(&self, receiver: UniquePtr<RtpReceiver>) {
unsafe {
(*self.observer).on_remove_track(receiver);
}
}
fn on_interesting_usage(&self, usage_pattern: i32) {
unsafe {
(*self.observer).on_interesting_usage(usage_pattern);
}
}
}
+103
View File
@@ -0,0 +1,103 @@
//
// Created by Théo Monnom on 03/08/2022.
//
#include "livekit/peer_connection_factory.h"
#include <utility>
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "api/rtc_event_log/rtc_event_log_factory.h"
#include "api/task_queue/default_task_queue_factory.h"
#include "api/video_codecs/builtin_video_decoder_factory.h"
#include "api/video_codecs/builtin_video_encoder_factory.h"
#include "webrtc-sys/src/peer_connection_factory.rs.h"
#include "livekit/rtc_error.h"
#include "media/engine/webrtc_media_engine.h"
namespace livekit {
PeerConnectionFactory::PeerConnectionFactory(
std::shared_ptr<RTCRuntime> rtc_runtime)
: rtc_runtime_(std::move(rtc_runtime)) {
RTC_LOG(LS_INFO) << "PeerConnectionFactory::PeerConnectionFactory()";
webrtc::PeerConnectionFactoryDependencies dependencies;
dependencies.network_thread = rtc_runtime_->network_thread();
dependencies.worker_thread = rtc_runtime_->worker_thread();
dependencies.signaling_thread = rtc_runtime_->signaling_thread();
dependencies.socket_factory = rtc_runtime_->network_thread()->socketserver();
dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
dependencies.event_log_factory = std::make_unique<webrtc::RtcEventLogFactory>(
dependencies.task_queue_factory.get());
dependencies.call_factory = webrtc::CreateCallFactory();
dependencies.trials = std::make_unique<webrtc::FieldTrialBasedConfig>();
cricket::MediaEngineDependencies media_deps;
media_deps.task_queue_factory = dependencies.task_queue_factory.get();
media_deps.video_encoder_factory = webrtc::CreateBuiltinVideoEncoderFactory();
media_deps.video_decoder_factory = webrtc::CreateBuiltinVideoDecoderFactory();
media_deps.audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
media_deps.audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
media_deps.audio_processing = webrtc::AudioProcessingBuilder().Create();
media_deps.trials = dependencies.trials.get();
dependencies.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
peer_factory_ =
webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
if (peer_factory_.get() == nullptr) {
RTC_LOG_ERR(LS_ERROR) << "Failed to create PeerConnectionFactory";
return;
}
}
PeerConnectionFactory::~PeerConnectionFactory() {
RTC_LOG(LS_INFO) << "PeerConnectionFactory::~PeerConnectionFactory()";
}
std::unique_ptr<PeerConnection> PeerConnectionFactory::create_peer_connection(
std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config,
NativePeerConnectionObserver& observer) const {
webrtc::PeerConnectionDependencies deps{&observer};
auto result =
peer_factory_->CreatePeerConnectionOrError(*config, std::move(deps));
if (!result.ok()) {
throw std::runtime_error(serialize_error(to_error(result.error())));
}
return std::make_unique<PeerConnection>(rtc_runtime_, result.value());
}
std::unique_ptr<PeerConnectionFactory> create_peer_connection_factory(
std::shared_ptr<RTCRuntime> rtc_runtime) {
return std::make_unique<PeerConnectionFactory>(std::move(rtc_runtime));
}
std::unique_ptr<NativeRTCConfiguration> create_rtc_configuration(
RTCConfiguration conf) {
auto rtc =
std::make_unique<webrtc::PeerConnectionInterface::RTCConfiguration>();
for (auto& item : conf.ice_servers) {
webrtc::PeerConnectionInterface::IceServer ice_server;
ice_server.username = item.username.c_str();
ice_server.password = item.password.c_str();
for (auto& url : item.urls) {
ice_server.urls.emplace_back(url.c_str());
}
rtc->servers.push_back(ice_server);
rtc->continual_gathering_policy =
static_cast<webrtc::PeerConnectionInterface::ContinualGatheringPolicy>(
conf.continual_gathering_policy);
rtc->type = static_cast<webrtc::PeerConnectionInterface::IceTransportsType>(
conf.ice_transport_type);
}
return rtc;
}
} // namespace livekit
+60
View File
@@ -0,0 +1,60 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug, Clone)]
pub struct ICEServer {
pub urls: Vec<String>,
pub username: String,
pub password: String,
}
#[derive(Debug)]
#[repr(i32)]
pub enum ContinualGatheringPolicy {
GatherOnce,
GatherContinually,
}
#[derive(Debug)]
#[repr(i32)]
pub enum IceTransportsType {
None,
Relay,
NoHost,
All,
}
#[derive(Debug, Clone)]
pub struct RTCConfiguration {
pub ice_servers: Vec<ICEServer>,
pub continual_gathering_policy: ContinualGatheringPolicy,
pub ice_transport_type: IceTransportsType,
}
unsafe extern "C++" {
include!("livekit/peer_connection_factory.h");
type PeerConnection = crate::peer_connection::ffi::PeerConnection;
type NativePeerConnectionObserver =
crate::peer_connection::ffi::NativePeerConnectionObserver;
type PeerConnectionFactory;
type NativeRTCConfiguration;
type RTCRuntime = crate::webrtc::ffi::RTCRuntime;
fn create_peer_connection_factory(
runtime: SharedPtr<RTCRuntime>,
) -> UniquePtr<PeerConnectionFactory>;
fn create_rtc_configuration(conf: RTCConfiguration) -> UniquePtr<NativeRTCConfiguration>;
/// # Safety
/// The observer must live as long as the PeerConnection
unsafe fn create_peer_connection(
self: &PeerConnectionFactory,
config: UniquePtr<NativeRTCConfiguration>,
observer: Pin<&mut NativePeerConnectionObserver>,
) -> Result<UniquePtr<PeerConnection>>;
}
}
unsafe impl Send for ffi::PeerConnectionFactory {}
unsafe impl Sync for ffi::PeerConnectionFactory {}
+56
View File
@@ -0,0 +1,56 @@
//
// Created by theom on 04/09/2022.
//
#include "livekit/rtc_error.h"
#include <iomanip>
#include <sstream>
#include <string>
namespace livekit {
RTCError to_error(const webrtc::RTCError& error) {
RTCError lk_error;
lk_error.error_detail = static_cast<RTCErrorDetailType>(error.error_detail());
lk_error.error_type = static_cast<RTCErrorType>(error.type());
lk_error.has_sctp_cause_code = error.sctp_cause_code().has_value();
lk_error.sctp_cause_code = error.sctp_cause_code().value_or(0);
lk_error.message = error.message();
return lk_error;
}
std::string serialize_error(const RTCError& error) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
ss << std::setw(8) << (uint32_t)error.error_type;
ss << std::setw(8) << (uint32_t)error.error_detail;
ss << std::setw(2) << (uint16_t)error.has_sctp_cause_code;
ss << std::setw(4) << (uint16_t)error.sctp_cause_code;
ss << std::dec << std::setw(1) << std::string(error.message);
return ss.str();
}
#ifdef LIVEKIT_TEST
rust::String serialize_deserialize() {
RTCError lk_error;
lk_error.error_type = RTCErrorType::InternalError;
lk_error.error_detail = RTCErrorDetailType::DataChannelFailure;
lk_error.has_sctp_cause_code = true;
lk_error.sctp_cause_code = 24;
lk_error.message = "this is not a test, I repeat, this is not a test";
return serialize_error(lk_error);
}
void throw_error() {
RTCError lk_error;
lk_error.error_type = RTCErrorType::InvalidModification;
lk_error.error_detail = RTCErrorDetailType::None;
lk_error.has_sctp_cause_code = false;
lk_error.sctp_cause_code = 0;
lk_error.message = "exception is thrown!";
throw std::runtime_error(serialize_error(lk_error));
}
#endif
} // namespace livekit
+128
View File
@@ -0,0 +1,128 @@
use std::error::Error;
use std::fmt::{Display, Formatter};
use crate::rtc_error::ffi::RTCErrorType;
// cxx doesn't support custom Exception type, so we serialize RTCError inside the cxx::Exception "what" string
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(i32)]
pub enum RTCErrorType {
None,
UnsupportedOperation,
UnsupportedParameter,
InvalidParameter,
InvalidRange,
SyntaxError,
InvalidState,
InvalidModification,
NetworkError,
ResourceExhausted,
InternalError,
OperationErrorWithData,
}
#[derive(Debug)]
#[repr(i32)]
pub enum RTCErrorDetailType {
None,
DataChannelFailure,
DtlsFailure,
FingerprintFailure,
SctpFailure,
SdpSyntaxError,
HardwareEncoderNotAvailable,
HardwareEncoderError,
}
#[derive(Debug)]
pub struct RTCError {
pub error_type: RTCErrorType,
pub message: String,
pub error_detail: RTCErrorDetailType,
pub has_sctp_cause_code: bool,
// cxx doesn't support the Option trait
pub sctp_cause_code: u16,
}
}
impl ffi::RTCError {
/// # Safety
/// The value must be correctly encoded
pub unsafe fn from(value: &str) -> Self {
// Parse the hex encoded error from c++
let error_type = u32::from_str_radix(&value[0..8], 16).unwrap();
let error_detail = u32::from_str_radix(&value[8..16], 16).unwrap();
let has_scp_cause_code = u8::from_str_radix(&value[16..18], 16).unwrap();
let sctp_cause_code = u16::from_str_radix(&value[18..22], 16).unwrap();
let message = String::from(&value[22..]); // msg isn't encoded
Self {
error_type: std::mem::transmute(error_type),
error_detail: std::mem::transmute(error_detail),
sctp_cause_code,
has_sctp_cause_code: has_scp_cause_code == 1,
message,
}
}
pub fn ok(&self) -> bool {
self.error_type == RTCErrorType::None
}
}
impl Error for ffi::RTCError {}
impl Display for ffi::RTCError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"RtcError occurred {:?}: {}",
self.error_type, self.message
)
}
}
#[cfg(test)]
mod tests {
use crate::rtc_error::ffi::{RTCError, RTCErrorDetailType, RTCErrorType};
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
include!("livekit/rtc_error.h");
fn serialize_deserialize() -> String;
fn throw_error() -> Result<()>;
}
}
#[test]
fn serialize_deserialize() {
let str = ffi::serialize_deserialize();
let error = unsafe { RTCError::from(&str) };
assert_eq!(error.error_type, RTCErrorType::InternalError);
assert_eq!(error.error_detail, RTCErrorDetailType::DataChannelFailure);
assert_eq!(error.has_sctp_cause_code, true);
assert_eq!(error.sctp_cause_code, 24);
assert_eq!(
error.message,
"this is not a test, I repeat, this is not a test"
);
}
#[test]
fn throw_error() {
let exc: cxx::Exception = ffi::throw_error().err().unwrap();
let error = unsafe { RTCError::from(exc.what()) };
assert_eq!(error.error_type, RTCErrorType::InvalidModification);
assert_eq!(error.error_detail, RTCErrorDetailType::None);
assert_eq!(error.has_sctp_cause_code, false);
assert_eq!(error.sctp_cause_code, 0);
assert_eq!(error.message, "exception is thrown!");
}
}
+16
View File
@@ -0,0 +1,16 @@
//
// Created by Théo Monnom on 01/09/2022.
//
#include "livekit/rtp_receiver.h"
namespace livekit {
RtpReceiver::RtpReceiver(
rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver)
: receiver_(std::move(receiver)) {}
std::unique_ptr<MediaStreamTrack> RtpReceiver::track() const {
return MediaStreamTrack::from(receiver_->track());
}
} // namespace livekit
+18
View File
@@ -0,0 +1,18 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
include!("livekit/rtp_receiver.h");
include!("livekit/media_stream.h");
type MediaStreamTrack = crate::media_stream::ffi::MediaStreamTrack;
type RtpReceiver;
fn track(self: &RtpReceiver) -> UniquePtr<MediaStreamTrack>;
fn _unique_rtp_receiver() -> UniquePtr<RtpReceiver>; // Ignore
}
}
unsafe impl Sync for ffi::RtpReceiver {}
unsafe impl Send for ffi::RtpReceiver {}
+11
View File
@@ -0,0 +1,11 @@
//
// Created by Théo Monnom on 02/09/2022.
//
#include "livekit/rtp_transceiver.h"
namespace livekit {
RtpTransceiver::RtpTransceiver(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver)
: transceiver_(std::move(transceiver)) {}
} // namespace livekit
+10
View File
@@ -0,0 +1,10 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
include!("livekit/rtp_transceiver.h");
type RtpTransceiver;
fn _unique_rtp_transceiver() -> UniquePtr<RtpTransceiver>; // Ignore
}
}
+32
View File
@@ -0,0 +1,32 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(i32)]
pub enum VideoRotation {
VideoRotation0 = 0,
VideoRotation90 = 90,
VideoRotation180 = 180,
VideoRotation270 = 270,
}
unsafe extern "C++" {
include!("livekit/video_frame.h");
include!("livekit/video_frame_buffer.h");
type VideoFrame;
type VideoFrameBuffer = crate::video_frame_buffer::ffi::VideoFrameBuffer;
fn width(self: &VideoFrame) -> i32;
fn height(self: &VideoFrame) -> i32;
fn size(self: &VideoFrame) -> u32;
fn id(self: &VideoFrame) -> u16;
fn timestamp_us(self: &VideoFrame) -> i64;
fn ntp_time_ms(self: &VideoFrame) -> i64;
fn transport_frame_id(self: &VideoFrame) -> u32;
fn timestamp(self: &VideoFrame) -> u32;
fn rotation(self: &VideoFrame) -> VideoRotation;
fn video_frame_buffer(self: &VideoFrame) -> UniquePtr<VideoFrameBuffer>;
fn _unique_video_frame() -> UniquePtr<VideoFrame>; // Ignore
}
}
+48
View File
@@ -0,0 +1,48 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(i32)]
pub enum VideoFrameBufferType {
Native,
I420,
I420A,
I422,
I444,
I010,
NV12,
}
unsafe extern "C++" {
include!("livekit/video_frame_buffer.h");
type VideoFrameBuffer;
type PlanarYuvBuffer;
type PlanarYuv8Buffer;
type I420Buffer;
fn buffer_type(self: &VideoFrameBuffer) -> VideoFrameBufferType;
fn width(self: &VideoFrameBuffer) -> i32;
fn height(self: &VideoFrameBuffer) -> i32;
// Require ownership
unsafe fn to_i420(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<I420Buffer>;
unsafe fn get_i420(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<I420Buffer>;
// TODO(theomonnom): Bridge other get_*
fn chroma_width(self: &PlanarYuvBuffer) -> i32;
fn chroma_height(self: &PlanarYuvBuffer) -> i32;
fn stride_y(self: &PlanarYuvBuffer) -> i32;
fn stride_u(self: &PlanarYuvBuffer) -> i32;
fn stride_v(self: &PlanarYuvBuffer) -> i32;
fn data_y(self: &PlanarYuv8Buffer) -> *const u8;
fn data_u(self: &PlanarYuv8Buffer) -> *const u8;
fn data_v(self: &PlanarYuv8Buffer) -> *const u8;
unsafe fn yuv_to_vfb(yuv: *const PlanarYuvBuffer) -> *const VideoFrameBuffer;
unsafe fn yuv8_to_yuv(yuv8: *const PlanarYuv8Buffer) -> *const PlanarYuvBuffer;
unsafe fn i420_to_yuv8(i420: *const I420Buffer) -> *const PlanarYuv8Buffer;
fn _unique_video_frame_buffer() -> UniquePtr<VideoFrameBuffer>;
}
}
+46
View File
@@ -0,0 +1,46 @@
//
// Created by theom on 18/09/2022.
//
#include "livekit/webrtc.h"
#include "rtc_base/logging.h"
namespace livekit {
RTCRuntime::RTCRuntime() {
// rtc::LogMessage::LogToDebug(rtc::LS_INFO);
RTC_LOG(LS_INFO) << "RTCRuntime()";
RTC_CHECK(rtc::InitializeSSL()) << "Failed to InitializeSSL()";
network_thread_ = rtc::Thread::CreateWithSocketServer();
network_thread_->SetName("network_thread", &network_thread_);
network_thread_->Start();
worker_thread_ = rtc::Thread::Create();
worker_thread_->SetName("worker_thread", &worker_thread_);
worker_thread_->Start();
signaling_thread_ = rtc::Thread::Create();
signaling_thread_->SetName("signaling_thread", &signaling_thread_);
signaling_thread_->Start();
}
RTCRuntime::~RTCRuntime() {
RTC_LOG(LS_INFO) << "~RTCRuntime()";
RTC_CHECK(rtc::CleanupSSL()) << "Failed to CleanupSSL()";
}
rtc::Thread* RTCRuntime::network_thread() const {
return network_thread_.get();
}
rtc::Thread* RTCRuntime::worker_thread() const {
return worker_thread_.get();
}
rtc::Thread* RTCRuntime::signaling_thread() const {
return signaling_thread_.get();
}
std::shared_ptr<RTCRuntime> create_rtc_runtime() {
return std::make_shared<RTCRuntime>();
}
} // namespace livekit
+14
View File
@@ -0,0 +1,14 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
include!("livekit/webrtc.h");
type RTCRuntime;
fn create_rtc_runtime() -> SharedPtr<RTCRuntime>;
}
}
unsafe impl Send for ffi::RTCRuntime {}
unsafe impl Sync for ffi::RTCRuntime {}
+19
View File
@@ -0,0 +1,19 @@
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
include!("livekit/yuv_helper.h");
unsafe fn i420_to_abgr(
src_y: *const u8,
src_stride_y: i32,
src_u: *const u8,
src_stride_u: i32,
src_v: *const u8,
src_stride_v: i32,
dst_abgr: *mut u8,
dst_stride_abgr: i32,
width: i32,
height: i32,
);
}
}