Added .clang-format + reformatted code

This commit is contained in:
Théo Monnom
2022-09-20 22:47:47 +02:00
parent baf1db9a04
commit e74aac70bb
35 changed files with 863 additions and 698 deletions
@@ -5,7 +5,6 @@
#include "livekit/candidate.h"
namespace livekit {
Candidate::Candidate(const cricket::Candidate &candidate) : candidate_(candidate) {
}
} // livekit
Candidate::Candidate(const cricket::Candidate& candidate)
: candidate_(candidate) {}
} // namespace livekit
@@ -1,5 +1,3 @@
use cxx::UniquePtr;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
@@ -2,78 +2,83 @@
// Created by Théo Monnom on 01/09/2022.
//
#include "livekit/data_channel.h"
#include <utility>
#include "livekit/data_channel.h"
#include "libwebrtc-sys/src/data_channel.rs.h"
namespace livekit {
DataChannel::DataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) : data_channel_(std::move(data_channel)) {
DataChannel::DataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
: data_channel_(std::move(data_channel)) {}
}
void DataChannel::register_observer(NativeDataChannelObserver& observer) {
data_channel_->RegisterObserver(&observer);
}
void DataChannel::register_observer(NativeDataChannelObserver &observer) {
data_channel_->RegisterObserver(&observer);
}
void DataChannel::unregister_observer() {
data_channel_->UnregisterObserver();
}
void DataChannel::unregister_observer() {
data_channel_->UnregisterObserver();
}
bool DataChannel::send(const DataBuffer& buffer) {
return data_channel_->Send(webrtc::DataBuffer{
rtc::CopyOnWriteBuffer(buffer.ptr, buffer.len), buffer.binary});
}
bool DataChannel::send(const DataBuffer &buffer) {
return data_channel_->Send(webrtc::DataBuffer{rtc::CopyOnWriteBuffer(buffer.ptr, buffer.len), buffer.binary });
}
rust::String DataChannel::label() const {
return data_channel_->label();
}
rust::String DataChannel::label() const{
return data_channel_->label();
}
void DataChannel::close() {
return data_channel_->Close();
}
void DataChannel::close() {
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;
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_retransmit_time)
rtc_init->maxRetransmitTime = init.max_retransmit_time;
if (init.has_max_retransmits)
rtc_init->maxRetransmits = init.max_retransmits;
if(init.has_max_retransmits)
rtc_init->maxRetransmits = init.max_retransmits;
if (init.has_priority)
rtc_init->priority = static_cast<webrtc::Priority>(init.priority);
if(init.has_priority)
rtc_init->priority = static_cast<webrtc::Priority>(init.priority);
return rtc_init;
}
return rtc_init;
}
NativeDataChannelObserver::NativeDataChannelObserver(
rust::Box<DataChannelObserverWrapper> observer)
: observer_(std::move(observer)) {}
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::OnStateChange() {
observer_->on_state_change();
}
void NativeDataChannelObserver::OnBufferedAmountChange(
uint64_t sent_data_size) {
observer_->on_buffered_amount_change(sent_data_size);
}
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));
}
} // livekit
std::unique_ptr<NativeDataChannelObserver> create_native_data_channel_observer(
rust::Box<DataChannelObserverWrapper> observer) {
return std::make_unique<NativeDataChannelObserver>(std::move(observer));
}
} // namespace livekit
@@ -1,9 +1,7 @@
use cxx::UniquePtr;
use std::slice;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(u32)]
pub enum Priority {
@@ -83,6 +81,7 @@ pub mod ffi {
}
unsafe impl Send for ffi::DataChannel {}
unsafe impl Send for ffi::NativeDataChannelObserver {}
// DataChannelObserver
@@ -2,93 +2,106 @@
// Created by Théo Monnom on 01/09/2022.
//
#include "livekit/jsep.h"
#include <memory>
#include "livekit/rtc_error.h"
#include "livekit/jsep.h"
#include "libwebrtc-sys/src/jsep.rs.h"
#include "livekit/rtc_error.h"
#include "rtc_base/ref_counted_object.h"
namespace livekit {
IceCandidate::IceCandidate(std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate) : ice_candidate_(std::move(ice_candidate)){
IceCandidate::IceCandidate(
std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate)
: ice_candidate_(std::move(ice_candidate)) {}
}
std::unique_ptr<webrtc::IceCandidateInterface> IceCandidate::release() {
return std::move(ice_candidate_);
}
std::unique_ptr<webrtc::IceCandidateInterface> IceCandidate::release() {
return std::move(ice_candidate_);
}
SessionDescription::SessionDescription(
std::unique_ptr<webrtc::SessionDescriptionInterface> session_description)
: session_description_(std::move(session_description)) {}
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());
}
rust::String SessionDescription::stringify() const {
std::string str;
session_description_->ToString(&str);
return rust::String{str};
}
std::unique_ptr<webrtc::SessionDescriptionInterface>
SessionDescription::release() {
return std::move(session_description_);
}
std::unique_ptr<SessionDescription> SessionDescription::clone() const {
return std::make_unique<SessionDescription>(session_description_->Clone());
}
// CreateSdpObserver
std::unique_ptr<webrtc::SessionDescriptionInterface> SessionDescription::release() {
return std::move(session_description_);
}
NativeCreateSdpObserver::NativeCreateSdpObserver(
rust::Box<CreateSdpObserverWrapper> observer)
: observer_(std::move(observer)) {}
// CreateSdpObserver
void NativeCreateSdpObserver::OnSuccess(
webrtc::SessionDescriptionInterface* desc) {
// We have ownership of desc
observer_->on_success(std::make_unique<SessionDescription>(
std::unique_ptr<webrtc::SessionDescriptionInterface>(desc)));
}
NativeCreateSdpObserver::NativeCreateSdpObserver(
rust::Box<CreateSdpObserverWrapper> observer) : observer_(std::move(observer)) {
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))});
}
void NativeCreateSdpObserver::OnSuccess(webrtc::SessionDescriptionInterface *desc) {
// We have ownership of desc
observer_->on_success(std::make_unique<SessionDescription>(std::unique_ptr<webrtc::SessionDescriptionInterface>(desc)));
}
// SetLocalSdpObserver
void NativeCreateSdpObserver::OnFailure(webrtc::RTCError error) {
observer_->on_failure(to_error(error));
}
NativeSetLocalSdpObserver::NativeSetLocalSdpObserver(
rust::Box<SetLocalSdpObserverWrapper> observer)
: observer_(std::move(observer)) {}
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))
});
}
void NativeSetLocalSdpObserver::OnSetLocalDescriptionComplete(
webrtc::RTCError error) {
observer_->on_set_local_description_complete(to_error(error));
}
// SetLocalSdpObserver
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))});
}
NativeSetLocalSdpObserver::NativeSetLocalSdpObserver(rust::Box<SetLocalSdpObserverWrapper> observer) : observer_(std::move(observer)) {
// SetRemoteSdpObserver
}
NativeSetRemoteSdpObserver::NativeSetRemoteSdpObserver(
rust::Box<SetRemoteSdpObserverWrapper> observer)
: observer_(std::move(observer)) {}
void NativeSetLocalSdpObserver::OnSetLocalDescriptionComplete(webrtc::RTCError error) {
observer_->on_set_local_description_complete(to_error(error));
}
void NativeSetRemoteSdpObserver::OnSetRemoteDescriptionComplete(
webrtc::RTCError error) {
observer_->on_set_remote_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))
});
}
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))});
}
// 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))
});
}
} // livekit
} // namespace livekit
@@ -1,11 +1,11 @@
use cxx::UniquePtr;
use std::fmt::{Debug, Formatter};
use cxx::UniquePtr;
use crate::rtc_error::ffi::RTCError;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
extern "Rust" {
type CreateSdpObserverWrapper;
fn on_success(
@@ -46,7 +46,7 @@ pub mod ffi {
) -> UniquePtr<NativeSetRemoteSdpObserverHandle>;
fn _unique_ice_candidate() -> UniquePtr<IceCandidate>; // Ignore
fn _unique_session_description() -> UniquePtr<SessionDescription>; // Ignore
fn _unique_session_description() -> UniquePtr<SessionDescription>; // Ignore
}
}
@@ -6,7 +6,7 @@
namespace livekit {
MediaStreamInterface::MediaStreamInterface(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) : media_stream_(std::move(stream)) {
}
} // livekit
MediaStreamInterface::MediaStreamInterface(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
: media_stream_(std::move(stream)) {}
} // namespace livekit
@@ -1,5 +1,3 @@
use cxx::UniquePtr;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
unsafe extern "C++" {
@@ -3,193 +3,234 @@
//
#include "livekit/peer_connection.h"
#include "libwebrtc-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;
}
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(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection) : peer_connection_(std::move(peer_connection)) {
PeerConnection::PeerConnection(
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection)
: 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_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::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_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);
}
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());
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())));
}
if(!result.ok()) {
throw std::runtime_error(serialize_error(to_error(result.error())));
}
return std::make_unique<DataChannel>(result.value());
}
return std::make_unique<DataChannel>(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)); });
}
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));
});
}
void PeerConnection::close() {
peer_connection_->Close();
}
void PeerConnection::close() {
peer_connection_->Close();
}
// AddIceCandidateObserver
// AddIceCandidateObserver
NativeAddIceCandidateObserver::NativeAddIceCandidateObserver(
rust::Box<AddIceCandidateObserverWrapper> observer)
: observer_(std::move(observer)) {}
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));
}
void NativeAddIceCandidateObserver::OnComplete(const RTCError &error) {
observer_->on_complete(error);
}
// PeerConnectionObserver
std::unique_ptr<NativeAddIceCandidateObserver> create_native_add_ice_candidate_observer(rust::Box<AddIceCandidateObserverWrapper> observer) {
return std::make_unique<NativeAddIceCandidateObserver>(std::move(observer));
}
NativePeerConnectionObserver::NativePeerConnectionObserver(
rust::Box<PeerConnectionObserverWrapper> observer)
: observer_(std::move(observer)) {}
// PeerConnectionObserver
void NativePeerConnectionObserver::OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) {
observer_->on_signaling_change(static_cast<SignalingState>(new_state));
}
NativePeerConnectionObserver::NativePeerConnectionObserver(rust::Box<PeerConnectionObserverWrapper> observer) : observer_(std::move(observer)) {
void NativePeerConnectionObserver::OnAddStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
observer_->on_add_stream(std::make_unique<MediaStreamInterface>(stream));
}
}
void NativePeerConnectionObserver::OnRemoveStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
observer_->on_remove_stream(std::make_unique<MediaStreamInterface>(stream));
}
void NativePeerConnectionObserver::OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) {
observer_->on_signaling_change(static_cast<SignalingState>(new_state));
}
void NativePeerConnectionObserver::OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
observer_->on_data_channel(std::make_unique<DataChannel>(data_channel));
}
void NativePeerConnectionObserver::OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
observer_->on_add_stream(std::make_unique<MediaStreamInterface>(stream));
}
void NativePeerConnectionObserver::OnRenegotiationNeeded() {
observer_->on_renegotiation_needed();
}
void NativePeerConnectionObserver::OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
observer_->on_remove_stream(std::make_unique<MediaStreamInterface>(stream));
}
void NativePeerConnectionObserver::OnNegotiationNeededEvent(uint32_t event_id) {
observer_->on_negotiation_needed_event(event_id);
}
void NativePeerConnectionObserver::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
observer_->on_data_channel(std::make_unique<DataChannel>(data_channel));
}
void NativePeerConnectionObserver::OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
observer_->on_ice_connection_change(
static_cast<IceConnectionState>(new_state));
}
void NativePeerConnectionObserver::OnRenegotiationNeeded() {
observer_->on_renegotiation_needed();
}
void NativePeerConnectionObserver::OnStandardizedIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
observer_->on_standardized_ice_connection_change(
static_cast<IceConnectionState>(new_state));
}
void NativePeerConnectionObserver::OnNegotiationNeededEvent(uint32_t event_id) {
observer_->on_negotiation_needed_event(event_id);
}
void NativePeerConnectionObserver::OnConnectionChange(
webrtc::PeerConnectionInterface::PeerConnectionState new_state) {
observer_->on_connection_change(static_cast<PeerConnectionState>(new_state));
}
void
NativePeerConnectionObserver::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) {
observer_->on_ice_connection_change(static_cast<IceConnectionState>(new_state));
}
void NativePeerConnectionObserver::OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state) {
observer_->on_ice_gathering_change(static_cast<IceGatheringState>(new_state));
}
void NativePeerConnectionObserver::OnStandardizedIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
observer_->on_standardized_ice_connection_change(static_cast<IceConnectionState>(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::OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state) {
observer_->on_connection_change(static_cast<PeerConnectionState>(new_state));
}
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::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) {
observer_->on_ice_gathering_change(static_cast<IceGatheringState>(new_state));
}
void NativePeerConnectionObserver::OnIceCandidatesRemoved(
const std::vector<cricket::Candidate>& candidates) {
rust::Vec<CandidatePtr> vec;
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)));
}
for (const auto& item : candidates) {
vec.push_back(CandidatePtr{std::make_unique<Candidate>(item)});
}
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);
}
observer_->on_ice_candidates_removed(std::move(vec));
}
void NativePeerConnectionObserver::OnIceCandidatesRemoved(const std::vector<cricket::Candidate> &candidates) {
rust::Vec<CandidatePtr> vec;
void NativePeerConnectionObserver::OnIceConnectionReceivingChange(
bool receiving) {
observer_->on_ice_connection_receiving_change(receiving);
}
for (const auto &item: candidates) {
vec.push_back(CandidatePtr{
std::make_unique<Candidate>(item)
});
}
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_candidates_removed(std::move(vec));
}
observer_->on_ice_selected_candidate_pair_changed(std::move(e));
}
void NativePeerConnectionObserver::OnIceConnectionReceivingChange(bool receiving) {
observer_->on_ice_connection_receiving_change(receiving);
}
void NativePeerConnectionObserver::OnAddTrack(
rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
streams) {
rust::Vec<MediaStreamPtr> vec;
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;
for (const auto& item : streams) {
vec.push_back(MediaStreamPtr{std::make_unique<MediaStreamInterface>(item)});
}
observer_->on_ice_selected_candidate_pair_changed(std::move(e));
}
observer_->on_add_track(std::make_unique<RtpReceiver>(receiver),
std::move(vec));
}
void NativePeerConnectionObserver::OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>> &streams) {
rust::Vec<MediaStreamPtr> vec;
void NativePeerConnectionObserver::OnTrack(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) {
observer_->on_track(std::make_unique<RtpTransceiver>(transceiver));
}
for (const auto &item: streams) {
vec.push_back(MediaStreamPtr{
std::make_unique<MediaStreamInterface>(item)
});
}
void NativePeerConnectionObserver::OnRemoveTrack(
rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) {
observer_->on_remove_track(std::make_unique<RtpReceiver>(receiver));
}
observer_->on_add_track(std::make_unique<RtpReceiver>(receiver), std::move(vec));
}
void NativePeerConnectionObserver::OnInterestingUsage(int usage_pattern) {
observer_->on_interesting_usage(usage_pattern);
}
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(rust::Box<PeerConnectionObserverWrapper> observer) {
return std::make_unique<NativePeerConnectionObserver>(std::move(observer));
}
} // livekit
std::unique_ptr<NativePeerConnectionObserver>
create_native_peer_connection_observer(
rust::Box<PeerConnectionObserverWrapper> observer) {
return std::make_unique<NativePeerConnectionObserver>(std::move(observer));
}
} // namespace livekit
@@ -1,3 +1,5 @@
use cxx::UniquePtr;
use crate::candidate::ffi::Candidate;
use crate::data_channel::ffi::DataChannel;
use crate::jsep::ffi::IceCandidate;
@@ -5,11 +7,9 @@ use crate::media_stream_interface::ffi::MediaStreamInterface;
use crate::rtc_error::ffi::RTCError;
use crate::rtp_receiver::ffi::RtpReceiver;
use crate::rtp_transceiver::ffi::RtpTransceiver;
use cxx::UniquePtr;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
struct CandidatePair {
local: UniquePtr<Candidate>,
remote: UniquePtr<Candidate>,
@@ -246,6 +246,7 @@ pub mod ffi {
// https://webrtc.github.io/webrtc-org/native-code/native-apis/
unsafe impl Sync for ffi::PeerConnection {}
unsafe impl Send for ffi::PeerConnection {}
unsafe impl Send for ffi::NativePeerConnectionObserver {}
@@ -3,85 +3,93 @@
//
#include "livekit/peer_connection_factory.h"
#include "api/video_codecs/builtin_video_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "api/video_codecs/builtin_video_encoder_factory.h"
#include "media/engine/webrtc_media_engine.h"
#include "api/task_queue/default_task_queue_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 "libwebrtc-sys/src/peer_connection_factory.rs.h"
#include "livekit/rtc_error.h"
#include "media/engine/webrtc_media_engine.h"
namespace livekit{
namespace livekit {
PeerConnectionFactory::PeerConnectionFactory(){
rtc::LogMessage::LogToDebug(rtc::LS_INFO);
RTC_LOG(LS_INFO) << "PeerConnectionFactory::PeerConnectionFactory()";
PeerConnectionFactory::PeerConnectionFactory() {
rtc::LogMessage::LogToDebug(rtc::LS_INFO);
RTC_LOG(LS_INFO) << "PeerConnectionFactory::PeerConnectionFactory()";
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();
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();
webrtc::PeerConnectionFactoryDependencies dependencies;
dependencies.network_thread = network_thread_.get();
dependencies.worker_thread = worker_thread_.get();
dependencies.signaling_thread = signaling_thread_.get();
dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
dependencies.event_log_factory = std::make_unique<webrtc::RtcEventLogFactory>(dependencies.task_queue_factory.get());
webrtc::PeerConnectionFactoryDependencies dependencies;
dependencies.network_thread = network_thread_.get();
dependencies.worker_thread = worker_thread_.get();
dependencies.signaling_thread = signaling_thread_.get();
dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
dependencies.event_log_factory = std::make_unique<webrtc::RtcEventLogFactory>(
dependencies.task_queue_factory.get());
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();
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();
dependencies.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
dependencies.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
peer_factory_ = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
peer_factory_ =
webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
if (peer_factory_.get() == nullptr) {
RTC_LOG_ERR(LS_ERROR) << "Failed to create PeerConnectionFactory";
return;
}
if (peer_factory_.get() == nullptr) {
RTC_LOG_ERR(LS_ERROR) << "Failed to create PeerConnectionFactory";
return;
}
}
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>(result.value());
}
std::unique_ptr<PeerConnectionFactory> create_peer_connection_factory() {
return std::make_unique<PeerConnectionFactory>();
}
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());
}
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));
rtc->servers.push_back(ice_server);
}
if(!result.ok()) {
throw std::runtime_error(serialize_error(to_error(result.error())));
}
return std::make_unique<PeerConnection>(result.value());
}
std::unique_ptr<PeerConnectionFactory> create_peer_connection_factory() {
return std::make_unique<PeerConnectionFactory>();
}
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);
}
return rtc;
}
} // namespace lk
return rtc;
}
} // namespace livekit
@@ -1,25 +1,10 @@
use crate::candidate::ffi::Candidate;
use crate::data_channel::ffi::DataChannel;
use crate::jsep::ffi::IceCandidate;
use crate::jsep::CreateSdpObserver;
use crate::media_stream_interface::ffi::MediaStreamInterface;
use crate::peer_connection::ffi::{
CandidatePairChangeEvent, IceConnectionState, IceGatheringState, PeerConnectionState,
SignalingState,
};
use crate::peer_connection::PeerConnectionObserver;
use crate::rtp_receiver::ffi::RtpReceiver;
use crate::rtp_transceiver::ffi::RtpTransceiver;
use crate::{jsep, peer_connection};
use cxx::UniquePtr;
use log::info;
use std::any::Any;
use std::thread::sleep;
use std::time::Duration;
use crate::jsep::CreateSdpObserver;
use crate::peer_connection::PeerConnectionObserver;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug, Clone)]
pub struct ICEServer {
pub urls: Vec<String>,
@@ -37,7 +22,7 @@ pub mod ffi {
type PeerConnection = crate::peer_connection::ffi::PeerConnection;
type NativePeerConnectionObserver =
crate::peer_connection::ffi::NativePeerConnectionObserver;
crate::peer_connection::ffi::NativePeerConnectionObserver;
type PeerConnectionFactory;
type NativeRTCConfiguration;
@@ -3,53 +3,54 @@
//
#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;
}
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();
}
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);
}
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));
}
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
} // livekit
} // namespace livekit
@@ -6,7 +6,6 @@ use std::fmt::{Display, Formatter};
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(u32)]
pub enum RTCErrorType {
@@ -42,7 +41,8 @@ pub mod ffi {
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 has_sctp_cause_code: bool,
// cxx doesn't support the Option trait
pub sctp_cause_code: u16,
}
}
@@ -5,7 +5,7 @@
#include "livekit/rtp_receiver.h"
namespace livekit {
RtpReceiver::RtpReceiver(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) : receiver_(std::move(receiver)) {
}
} // livekit
RtpReceiver::RtpReceiver(
rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver)
: receiver_(std::move(receiver)) {}
} // namespace livekit
@@ -5,7 +5,7 @@
#include "livekit/rtp_transceiver.h"
namespace livekit {
RtpTransceiver::RtpTransceiver(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) : transceiver_(std::move(transceiver)) {
}
} // livekit
RtpTransceiver::RtpTransceiver(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver)
: transceiver_(std::move(transceiver)) {}
} // namespace livekit
@@ -3,20 +3,21 @@
//
#include "livekit/webrtc.h"
#include "rtc_base/logging.h"
namespace livekit {
RTCRuntime::RTCRuntime() {
RTC_LOG(LS_INFO) << "RTCRuntime()";
RTC_CHECK(rtc::InitializeSSL()) << "Failed to InitializeSSL()";
}
RTCRuntime::RTCRuntime() {
RTC_LOG(LS_INFO) << "RTCRuntime()";
RTC_CHECK(rtc::InitializeSSL()) << "Failed to InitializeSSL()";
}
RTCRuntime::~RTCRuntime() {
RTC_LOG(LS_INFO) << "~RTCRuntime()";
RTC_CHECK(rtc::CleanupSSL()) << "Failed to CleanupSSL()";
}
RTCRuntime::~RTCRuntime() {
RTC_LOG(LS_INFO) << "~RTCRuntime()";
RTC_CHECK(rtc::CleanupSSL()) << "Failed to CleanupSSL()";
}
std::unique_ptr<RTCRuntime> create_rtc_runtime(){
return std::make_unique<RTCRuntime>();
}
} // livekit
std::unique_ptr<RTCRuntime> create_rtc_runtime() {
return std::make_unique<RTCRuntime>();
}
} // namespace livekit