Skip to content

Commit

Permalink
Threads-SRTP: Config and add files for the async-srtp
Browse files Browse the repository at this point in the history
1. If configed the async srtp, use a new object.
2. Allow sync and async srtp, by config.
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent 2367483 commit f068540
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
3 changes: 3 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

#############################################################################################
Expand Down
17 changes: 17 additions & 0 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SrsConfDirective*> SrsConfig::get_stream_casters()
{
srs_assert(root);
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion trunk/src/app/srs_app_rtc_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ using namespace std;
#include <srs_app_rtc_server.hpp>
#include <srs_app_rtc_source.hpp>
#include <srs_protocol_utility.hpp>
#include <srs_app_threads.hpp>

#include <srs_protocol_kbps.hpp>

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/app/srs_app_rtc_dtls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 31 additions & 1 deletion trunk/src/app/srs_app_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
14 changes: 14 additions & 0 deletions trunk/src/app/srs_app_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <srs_kernel_file.hpp>
#include <srs_kernel_flv.hpp>
#include <srs_app_rtc_dtls.hpp>

#include <pthread.h>

Expand Down Expand Up @@ -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

0 comments on commit f068540

Please sign in to comment.