cleanup: webrtc-sys & fix RtcRuntime disposing crashes (#81)

This commit is contained in:
Théo Monnom
2023-06-04 01:02:00 +02:00
committed by GitHub
parent 05ad1c95af
commit 55bda13069
80 changed files with 2209 additions and 1954 deletions
+50 -42
View File
@@ -16,6 +16,7 @@
#include "livekit/peer_connection_factory.h"
#include <memory>
#include <utility>
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
@@ -26,19 +27,49 @@
#include "api/video_codecs/builtin_video_decoder_factory.h"
#include "api/video_codecs/builtin_video_encoder_factory.h"
#include "livekit/audio_device.h"
#include "livekit/peer_connection.h"
#include "livekit/rtc_error.h"
#include "livekit/rtp_parameters.h"
#include "livekit/video_decoder_factory.h"
#include "livekit/video_encoder_factory.h"
#include "livekit/webrtc.h"
#include "media/engine/webrtc_media_engine.h"
#include "rtc_base/location.h"
#include "rtc_base/thread.h"
#include "webrtc-sys/src/peer_connection.rs.h"
#include "webrtc-sys/src/peer_connection_factory.rs.h"
namespace livekit {
webrtc::PeerConnectionInterface::RTCConfiguration to_native_rtc_configuration(
RtcConfiguration config) {
webrtc::PeerConnectionInterface::RTCConfiguration rtc_config{};
for (auto item : config.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_config.servers.push_back(ice_server);
}
rtc_config.continual_gathering_policy =
static_cast<webrtc::PeerConnectionInterface::ContinualGatheringPolicy>(
config.continual_gathering_policy);
rtc_config.type =
static_cast<webrtc::PeerConnectionInterface::IceTransportsType>(
config.ice_transport_type);
return rtc_config;
}
PeerConnectionFactory::PeerConnectionFactory(
std::shared_ptr<RTCRuntime> rtc_runtime)
: rtc_runtime_(std::move(rtc_runtime)) {
std::shared_ptr<RtcRuntime> rtc_runtime)
: rtc_runtime_(rtc_runtime) {
RTC_LOG(LS_INFO) << "PeerConnectionFactory::PeerConnectionFactory()";
webrtc::PeerConnectionFactoryDependencies dependencies;
@@ -87,31 +118,35 @@ PeerConnectionFactory::~PeerConnectionFactory() {
}
std::shared_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));
RtcConfiguration config,
std::unique_ptr<NativePeerConnectionObserver> observer) const {
observer->rtc_runtime_ = rtc_runtime_; // See peer_connection.h
webrtc::PeerConnectionDependencies deps{observer.get()};
auto result = peer_factory_->CreatePeerConnectionOrError(
to_native_rtc_configuration(config), std::move(deps));
if (!result.ok()) {
throw std::runtime_error(serialize_error(to_error(result.error())));
}
return std::make_shared<PeerConnection>(rtc_runtime_, result.value());
return std::make_shared<PeerConnection>(rtc_runtime_, std::move(observer),
result.value());
}
std::shared_ptr<VideoTrack> PeerConnectionFactory::create_video_track(
rust::String label,
std::shared_ptr<AdaptedVideoTrackSource> source) const {
return std::make_shared<VideoTrack>(
peer_factory_->CreateVideoTrack(label.c_str(), source->get().get()));
std::shared_ptr<VideoTrackSource> source) const {
return std::static_pointer_cast<VideoTrack>(
rtc_runtime_->get_or_create_media_stream_track(
peer_factory_->CreateVideoTrack(label.c_str(), source->get().get())));
}
std::shared_ptr<AudioTrack> PeerConnectionFactory::create_audio_track(
rust::String label,
std::shared_ptr<AudioTrackSource> source) const {
return std::make_shared<AudioTrack>(
peer_factory_->CreateAudioTrack(label.c_str(), source->get().get()));
return std::static_pointer_cast<AudioTrack>(
rtc_runtime_->get_or_create_media_stream_track(
peer_factory_->CreateAudioTrack(label.c_str(), source->get().get())));
}
RtpCapabilities PeerConnectionFactory::get_rtp_sender_capabilities(
@@ -126,35 +161,8 @@ RtpCapabilities PeerConnectionFactory::get_rtp_receiver_capabilities(
static_cast<cricket::MediaType>(type)));
}
std::shared_ptr<PeerConnectionFactory> create_peer_connection_factory(
std::shared_ptr<RTCRuntime> rtc_runtime) {
return std::make_shared<PeerConnectionFactory>(std::move(rtc_runtime));
std::shared_ptr<PeerConnectionFactory> create_peer_connection_factory() {
return std::make_shared<PeerConnectionFactory>(RtcRuntime::create());
}
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