From 225c006140ae33d4991a2e0f6b351af97a28dcec Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Mon, 16 Nov 2020 14:50:54 +0100 Subject: [PATCH] Added possibilities to configure the ICE agent --- server/src/client/SpeakingClient.cpp | 9 ++++- server/src/rtc/imports.h | 18 ++++++++- server/src/rtc/lib.cpp | 56 ++++++++++++++++++++++++++-- server/src/rtc/lib.h | 2 +- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/server/src/client/SpeakingClient.cpp b/server/src/client/SpeakingClient.cpp index 9ed46a7..d454c4e 100644 --- a/server/src/client/SpeakingClient.cpp +++ b/server/src/client/SpeakingClient.cpp @@ -714,7 +714,14 @@ void SpeakingClient::processJoin() { /* TODO: Will be a RTP client later on, just without audio */ this->rtc_client_id = this->server->rtc_server().create_native_client(dynamic_pointer_cast(this->ref())); } else if(this->getType() == ClientType::CLIENT_WEB) { - this->rtc_client_id = this->server->rtc_server().create_rtp_client(dynamic_pointer_cast(this->ref())); + std::string error; + auto result = this->server->rtc_server().create_rtp_client(dynamic_pointer_cast(this->ref()), error); + if(result > 0) { + this->rtc_client_id = result; + } else { + this->rtc_client_id = 0; + logCritical(this->getServerId(), "{} Failed to configure RTC session: {}", CLIENT_STR_LOG_PREFIX, error); + } } this->rtc_session_pending_describe = true; diff --git a/server/src/rtc/imports.h b/server/src/rtc/imports.h index d8abee0..89c3da5 100644 --- a/server/src/rtc/imports.h +++ b/server/src/rtc/imports.h @@ -12,6 +12,8 @@ struct NativeCallbacks { void(*log)(uint8_t /* level */, const void* /* callback data */, const char* /* message */, uint32_t /* length */); void(*free_client_data)(const void*); + uint32_t(*rtc_configure)(const void* /* callback data */, void* /* configure callback data */); + void(*client_stream_assignment)(const void* /* callback data */, uint32_t /* stream id */, const void* /* source callback data */); void(*client_offer_generated)(const void* /* callback data */, const char* /* offer */, size_t /* offer length */); @@ -21,6 +23,18 @@ struct NativeCallbacks { void(*client_audio_sender_data)(const void* /* callback data */, const void* /* source callback data */, uint8_t /* mode */, uint16_t /* seq. no. */, uint8_t /* codec */, const void* /* data */, uint32_t /* length */); }; +struct RtpClientConfigureOptions { + uint16_t min_port; + uint16_t max_port; + + bool ice_tcp; + bool ice_udp; + bool ice_upnp; + + const char* stun_host; + uint16_t stun_port; +}; + extern const char* librtc_version(); extern void librtc_free_str(const char* /* ptr */); @@ -29,10 +43,12 @@ extern const char* librtc_init(const NativeCallbacks* /* */, size_t /* size of t extern void* librtc_create_server(); extern void librtc_destroy_server(void* /* server */); -extern uint32_t librtc_create_rtp_client(void* /* server */, void* /* callback data */); +extern uint32_t librtc_create_rtp_client(void* /* server */, void* /* callback data */, const char** /* error ptr */); extern uint32_t librtc_create_native_client(void* /* server */, void* /* callback data */); extern void librtc_destroy_client(void* /* server */, uint32_t /* client id */); +extern const char* librtc_rtc_configure(void* /* callback data */, const RtpClientConfigureOptions* /* config */, size_t /* config size */); + extern const char* librtc_reset_rtp_session(void* /* server */, uint32_t /* client id */); extern const char* librtc_apply_remote_description(void* /* server */, uint32_t /* client id */, uint32_t /* mode */, const char* /* description */); extern const char* librtc_generate_local_description(void* /* server */, uint32_t /* client id */, char** /* description */); diff --git a/server/src/rtc/lib.cpp b/server/src/rtc/lib.cpp index 025deca..c907a96 100644 --- a/server/src/rtc/lib.cpp +++ b/server/src/rtc/lib.cpp @@ -62,6 +62,38 @@ void librtc_callback_free_client_data(const void* data) { delete (LibCallbackData*) data; } +uint32_t librtc_callback_rtc_configure(const void* callback_data_ptr, void* configure_ptr) { + auto callback_data = (LibCallbackData*) callback_data_ptr; + + auto target_client = callback_data->weak_ref.lock(); + if(!target_client) { return 2; } + + RtpClientConfigureOptions options{}; + memset(&options, 0, sizeof(options)); + + options.ice_tcp = config::web::tcp_enabled; + options.ice_udp = config::web::udp_enabled; + options.ice_upnp = config::web::enable_upnp; + options.max_port = config::web::webrtc_port_min; + options.max_port = config::web::webrtc_port_max; + + std::string stun_host{config::web::stun_host}; + options.stun_host = stun_host.c_str(); + options.stun_port = config::web::stun_port; + if(!config::web::stun_enabled) { + options.stun_port = 0; + options.stun_host = nullptr; + } + + auto result = librtc_rtc_configure(configure_ptr, &options, sizeof(options)); + if(result) { + logError(target_client->getServerId(), "{} Failed to configure RTC connection: {}", CLIENT_STR_LOG_PREFIX_(target_client), result); + librtc_free_str(result); + return 1; + } + return 0; +} + void librtc_callback_client_stream_assignment(const void* callback_data_ptr, uint32_t stream_id, const void* source_data_ptr) { auto callback_data = (LibCallbackData*) callback_data_ptr; auto source_data = (LibCallbackData*) source_data_ptr; @@ -178,11 +210,13 @@ void librtc_callback_client_audio_sender_data(const void* callback_data_ptr, con } static NativeCallbacks native_callbacks{ - .version = 1, + .version = 2, .log = librtc_callback_log, .free_client_data = librtc_callback_free_client_data, + .rtc_configure = librtc_callback_rtc_configure, + .client_stream_assignment = librtc_callback_client_stream_assignment, .client_offer_generated = librtc_callback_client_offer_generated, @@ -213,12 +247,28 @@ Server::~Server() { librtc_destroy_server(this->server_ptr); } -RTCClientId Server::create_rtp_client(const std::shared_ptr &client) { +RTCClientId Server::create_rtp_client(const std::shared_ptr &client, std::string& error) { auto data = new LibCallbackData{ .client_id = client->getClientId(), .weak_ref = client }; - return librtc_create_rtp_client(this->server_ptr, data); + + const char* error_ptr{nullptr}; + auto client_id = librtc_create_rtp_client(this->server_ptr, data, &error_ptr); + if(client_id == 0) { + /* LibCallbackData will be automatically freed */ + + if(error_ptr) { + error.clear(); + error.append(error_ptr); + librtc_free_str(error_ptr); + } else { + error = "unknown error"; + } + + return 0; + } + return client_id; } RTCClientId Server::create_native_client(const std::shared_ptr &client) { diff --git a/server/src/rtc/lib.h b/server/src/rtc/lib.h index 1e46844..db0c5b4 100644 --- a/server/src/rtc/lib.h +++ b/server/src/rtc/lib.h @@ -38,7 +38,7 @@ namespace ts::rtc { Server(); ~Server(); - RTCClientId create_rtp_client(const std::shared_ptr& /* client */); + RTCClientId create_rtp_client(const std::shared_ptr& /* client */, std::string& /* error */); RTCClientId create_native_client(const std::shared_ptr& /* client */); void destroy_client(RTCClientId /* client id */);