From f068540eedaea43b0f74930b5034e37a3df2f6b3 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 14 Mar 2021 21:39:36 +0800 Subject: [PATCH] Threads-SRTP: Config and add files for the async-srtp 1. If configed the async srtp, use a new object. 2. Allow sync and async srtp, by config. --- trunk/conf/full.conf | 3 +++ trunk/src/app/srs_app_config.cpp | 17 ++++++++++++++++ trunk/src/app/srs_app_config.hpp | 1 + trunk/src/app/srs_app_rtc_conn.cpp | 9 ++++++++- trunk/src/app/srs_app_rtc_dtls.hpp | 4 ++-- trunk/src/app/srs_app_threads.cpp | 32 +++++++++++++++++++++++++++++- trunk/src/app/srs_app_threads.hpp | 14 +++++++++++++ 7 files changed, 76 insertions(+), 4 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index d8f942757a..8e70de013d 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -121,6 +121,9 @@ threads { # The thread pool manager cycle interval, in seconds. # Default: 5 interval 5; + # Whether enable the ASYNC SRTP, codec in dedicate threads. + # Default: off + async_srtp off; } ############################################################################################# diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 8f6e36daa1..eeb959e34c 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -4127,6 +4127,23 @@ srs_utime_t SrsConfig::get_threads_interval() return v * SRS_UTIME_SECONDS; } +bool SrsConfig::get_threads_async_srtp() +{ + static bool DEFAULT = false; + + SrsConfDirective* conf = root->get("threads"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("async_srtp"); + if (!conf) { + return DEFAULT; + } + + return SRS_CONF_PERFER_FALSE(conf->arg0()); +} + vector SrsConfig::get_stream_casters() { srs_assert(root); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 741d0c556b..f05676a397 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -478,6 +478,7 @@ class SrsConfig // Thread pool section. public: virtual srs_utime_t get_threads_interval(); + virtual bool get_threads_async_srtp(); // stream_caster section public: // Get all stream_caster in config file. diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index 2aecf96e70..8da8801628 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -57,6 +57,7 @@ using namespace std; #include #include #include +#include #include @@ -93,7 +94,13 @@ SrsSecurityTransport::SrsSecurityTransport(SrsRtcConnection* s) session_ = s; dtls_ = new SrsDtls((ISrsDtlsCallback*)this); - srtp_ = new SrsSRTP(); + + bool async_srtp = _srs_config->get_threads_async_srtp(); + if (!async_srtp) { + srtp_ = new SrsSRTP(); + } else { + srtp_ = new SrsAsyncSRTP(); + } handshake_done = false; } diff --git a/trunk/src/app/srs_app_rtc_dtls.hpp b/trunk/src/app/srs_app_rtc_dtls.hpp index 0938f30c66..575261ac84 100644 --- a/trunk/src/app/srs_app_rtc_dtls.hpp +++ b/trunk/src/app/srs_app_rtc_dtls.hpp @@ -231,14 +231,14 @@ class SrsDtls class SrsSRTP { -private: +protected: srtp_t recv_ctx_; srtp_t send_ctx_; public: SrsSRTP(); virtual ~SrsSRTP(); public: - // Intialize srtp context with recv_key and send_key. + // Initialize srtp context with recv_key and send_key. srs_error_t initialize(std::string recv_key, std::string send_key); public: srs_error_t protect_rtp(void* packet, int* nb_cipher); diff --git a/trunk/src/app/srs_app_threads.cpp b/trunk/src/app/srs_app_threads.cpp index e8c17414d5..b1da2b7c5f 100644 --- a/trunk/src/app/srs_app_threads.cpp +++ b/trunk/src/app/srs_app_threads.cpp @@ -128,7 +128,9 @@ srs_error_t SrsThreadPool::initialize() } interval_ = _srs_config->get_threads_interval(); - srs_trace("Thread #%d(%s): init interval=%dms", entry_->num, entry_->label.c_str(), srsu2msi(interval_)); + bool async_srtp = _srs_config->get_threads_async_srtp(); + srs_trace("Thread #%d(%s): init interval=%dms, async_srtp=%d", + entry_->num, entry_->label.c_str(), srsu2msi(interval_), async_srtp); return err; } @@ -481,3 +483,31 @@ srs_error_t SrsAsyncLogManager::do_start() // TODO: FIXME: It should be thread-local or thread-safe. SrsAsyncLogManager* _srs_async_log = new SrsAsyncLogManager(); + +SrsAsyncSRTP::SrsAsyncSRTP() +{ +} + +SrsAsyncSRTP::~SrsAsyncSRTP() +{ +} + +srs_error_t SrsAsyncSRTP::protect_rtp(void* packet, int* nb_cipher) +{ + return SrsSRTP::protect_rtp(packet, nb_cipher); +} + +srs_error_t SrsAsyncSRTP::protect_rtcp(void* packet, int* nb_cipher) +{ + return SrsSRTP::protect_rtcp(packet, nb_cipher); +} + +srs_error_t SrsAsyncSRTP::unprotect_rtp(void* packet, int* nb_plaintext) +{ + return SrsSRTP::unprotect_rtp(packet, nb_plaintext); +} + +srs_error_t SrsAsyncSRTP::unprotect_rtcp(void* packet, int* nb_plaintext) +{ + return SrsSRTP::unprotect_rtcp(packet, nb_plaintext); +} diff --git a/trunk/src/app/srs_app_threads.hpp b/trunk/src/app/srs_app_threads.hpp index 7b44e6316f..9cbab29102 100644 --- a/trunk/src/app/srs_app_threads.hpp +++ b/trunk/src/app/srs_app_threads.hpp @@ -28,6 +28,7 @@ #include #include +#include #include @@ -254,4 +255,17 @@ class SrsAsyncLogManager // The global async log manager. extern SrsAsyncLogManager* _srs_async_log; +// The async SRTP codec. +class SrsAsyncSRTP : public SrsSRTP +{ +public: + SrsAsyncSRTP(); + virtual ~SrsAsyncSRTP(); +public: + srs_error_t protect_rtp(void* packet, int* nb_cipher); + srs_error_t protect_rtcp(void* packet, int* nb_cipher); + srs_error_t unprotect_rtp(void* packet, int* nb_plaintext); + srs_error_t unprotect_rtcp(void* packet, int* nb_plaintext); +}; + #endif