mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-25 20:27:34 +08:00
srtp自动初始化,支持多线程
This commit is contained in:
parent
bdf2783a6b
commit
0d4cc2fc65
@ -4,15 +4,14 @@
|
|||||||
#include "SrtpSession.hpp"
|
#include "SrtpSession.hpp"
|
||||||
#include <cstring> // std::memset(), std::memcpy()
|
#include <cstring> // std::memset(), std::memcpy()
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
#include "Util/util.h"
|
||||||
|
using namespace toolkit;
|
||||||
|
|
||||||
namespace RTC
|
namespace RTC
|
||||||
{
|
{
|
||||||
/* Static. */
|
/* Static. */
|
||||||
|
|
||||||
static constexpr size_t EncryptBufferSize{ 65536 };
|
static std::vector<const char *> errors = {
|
||||||
static uint8_t EncryptBuffer[EncryptBufferSize];
|
|
||||||
|
|
||||||
std::vector<const char *> DepLibSRTP::errors = {
|
|
||||||
// From 0 (srtp_err_status_ok) to 24 (srtp_err_status_pfkey_err).
|
// From 0 (srtp_err_status_ok) to 24 (srtp_err_status_pfkey_err).
|
||||||
"success (srtp_err_status_ok)",
|
"success (srtp_err_status_ok)",
|
||||||
"unspecified failure (srtp_err_status_fail)",
|
"unspecified failure (srtp_err_status_fail)",
|
||||||
@ -43,65 +42,69 @@ namespace RTC
|
|||||||
|
|
||||||
/* Static methods. */
|
/* Static methods. */
|
||||||
|
|
||||||
void DepLibSRTP::ClassInit() {
|
const char *DepLibSRTP::GetErrorString(srtp_err_status_t code) {
|
||||||
|
// This throws out_of_range if the given index is not in the vector.
|
||||||
|
return errors.at(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DepLibSRTP::IsError(srtp_err_status_t code) {
|
||||||
|
return (code != srtp_err_status_ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
INSTANCE_IMP(DepLibSRTP);
|
||||||
|
|
||||||
|
DepLibSRTP::DepLibSRTP(){
|
||||||
MS_TRACE();
|
MS_TRACE();
|
||||||
|
|
||||||
MS_DEBUG_TAG(info, "libsrtp version: \"%s\"", srtp_get_version_string());
|
MS_DEBUG_TAG(info, "libsrtp version: \"%s\"", srtp_get_version_string());
|
||||||
|
|
||||||
srtp_err_status_t err = srtp_init();
|
srtp_err_status_t err = srtp_init();
|
||||||
|
|
||||||
if (DepLibSRTP::IsError(err))
|
if (DepLibSRTP::IsError(err)) {
|
||||||
MS_THROW_ERROR("srtp_init() failed: %s", DepLibSRTP::GetErrorString(err));
|
MS_THROW_ERROR("srtp_init() failed: %s", DepLibSRTP::GetErrorString(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set libsrtp event handler.
|
||||||
|
err = srtp_install_event_handler([](srtp_event_data_t *data){
|
||||||
|
MS_TRACE();
|
||||||
|
switch (data->event)
|
||||||
|
{
|
||||||
|
case event_ssrc_collision:
|
||||||
|
MS_WARN_TAG(srtp, "SSRC collision occurred");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case event_key_soft_limit:
|
||||||
|
MS_WARN_TAG(srtp, "stream reached the soft key usage limit and will expire soon");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case event_key_hard_limit:
|
||||||
|
MS_WARN_TAG(srtp, "stream reached the hard key usage limit and has expired");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case event_packet_index_limit:
|
||||||
|
MS_WARN_TAG(srtp, "stream reached the hard packet limit (2^48 packets)");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (DepLibSRTP::IsError(err))
|
||||||
|
{
|
||||||
|
MS_THROW_ERROR("srtp_install_event_handler() failed: %s", DepLibSRTP::GetErrorString(err));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DepLibSRTP::ClassDestroy() {
|
DepLibSRTP::~DepLibSRTP(){
|
||||||
MS_TRACE();
|
MS_TRACE();
|
||||||
|
|
||||||
srtp_shutdown();
|
srtp_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Class methods. */
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void SrtpSession::ClassInit()
|
|
||||||
{
|
|
||||||
// Set libsrtp event handler.
|
|
||||||
srtp_err_status_t err =
|
|
||||||
srtp_install_event_handler(static_cast<srtp_event_handler_func_t*>(OnSrtpEvent));
|
|
||||||
|
|
||||||
if (DepLibSRTP::IsError(err))
|
|
||||||
{
|
|
||||||
MS_THROW_ERROR("srtp_install_event_handler() failed: %s", DepLibSRTP::GetErrorString(err));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SrtpSession::OnSrtpEvent(srtp_event_data_t* data)
|
|
||||||
{
|
|
||||||
MS_TRACE();
|
|
||||||
|
|
||||||
switch (data->event)
|
|
||||||
{
|
|
||||||
case event_ssrc_collision:
|
|
||||||
MS_WARN_TAG(srtp, "SSRC collision occurred");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case event_key_soft_limit:
|
|
||||||
MS_WARN_TAG(srtp, "stream reached the soft key usage limit and will expire soon");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case event_key_hard_limit:
|
|
||||||
MS_WARN_TAG(srtp, "stream reached the hard key usage limit and has expired");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case event_packet_index_limit:
|
|
||||||
MS_WARN_TAG(srtp, "stream reached the hard packet limit (2^48 packets)");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Instance methods. */
|
/* Instance methods. */
|
||||||
|
|
||||||
SrtpSession::SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t* key, size_t keyLen)
|
SrtpSession::SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t* key, size_t keyLen)
|
||||||
{
|
{
|
||||||
|
_env = DepLibSRTP::Instance().shared_from_this();
|
||||||
MS_TRACE();
|
MS_TRACE();
|
||||||
|
|
||||||
srtp_policy_t policy; // NOLINT(cppcoreguidelines-pro-type-member-init)
|
srtp_policy_t policy; // NOLINT(cppcoreguidelines-pro-type-member-init)
|
||||||
|
@ -4,21 +4,22 @@
|
|||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
#include <srtp2/srtp.h>
|
#include <srtp2/srtp.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace RTC
|
namespace RTC
|
||||||
{
|
{
|
||||||
class DepLibSRTP {
|
class DepLibSRTP : public std::enable_shared_from_this<DepLibSRTP>
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
static void ClassInit();
|
using Ptr = std::shared_ptr<DepLibSRTP>;
|
||||||
static void ClassDestroy();
|
~DepLibSRTP();
|
||||||
static bool IsError(srtp_err_status_t code) { return (code != srtp_err_status_ok); }
|
|
||||||
static const char *GetErrorString(srtp_err_status_t code) {
|
static bool IsError(srtp_err_status_t code);
|
||||||
// This throws out_of_range if the given index is not in the vector.
|
static const char *GetErrorString(srtp_err_status_t code);
|
||||||
return DepLibSRTP::errors.at(code);
|
static DepLibSRTP &Instance();
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::vector<const char *> errors;
|
DepLibSRTP();
|
||||||
};
|
};
|
||||||
|
|
||||||
class SrtpSession
|
class SrtpSession
|
||||||
@ -40,12 +41,6 @@ namespace RTC
|
|||||||
OUTBOUND
|
OUTBOUND
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
|
||||||
static void ClassInit();
|
|
||||||
|
|
||||||
private:
|
|
||||||
static void OnSrtpEvent(srtp_event_data_t* data);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t* key, size_t keyLen);
|
SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t* key, size_t keyLen);
|
||||||
~SrtpSession();
|
~SrtpSession();
|
||||||
@ -63,6 +58,10 @@ namespace RTC
|
|||||||
private:
|
private:
|
||||||
// Allocated by this.
|
// Allocated by this.
|
||||||
srtp_t session{ nullptr };
|
srtp_t session{ nullptr };
|
||||||
|
//rtp包最大1600
|
||||||
|
static constexpr size_t EncryptBufferSize{ 1600 };
|
||||||
|
uint8_t EncryptBuffer[EncryptBufferSize];
|
||||||
|
DepLibSRTP::Ptr _env;
|
||||||
};
|
};
|
||||||
} // namespace RTC
|
} // namespace RTC
|
||||||
|
|
||||||
|
@ -5,15 +5,16 @@
|
|||||||
WebRtcTransport::WebRtcTransport() {
|
WebRtcTransport::WebRtcTransport() {
|
||||||
static onceToken token([](){
|
static onceToken token([](){
|
||||||
RTC::DtlsTransport::ClassInit();
|
RTC::DtlsTransport::ClassInit();
|
||||||
RTC::DepLibSRTP::ClassInit();
|
|
||||||
RTC::SrtpSession::ClassInit();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
dtls_transport_ = std::make_shared<RTC::DtlsTransport>(EventPollerPool::Instance().getFirstPoller(), this);
|
dtls_transport_ = std::make_shared<RTC::DtlsTransport>(EventPollerPool::Instance().getFirstPoller(), this);
|
||||||
ice_server_ = std::make_shared<RTC::IceServer>(this, makeRandStr(4), makeRandStr(24));
|
ice_server_ = std::make_shared<RTC::IceServer>(this, makeRandStr(4), makeRandStr(24));
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtcTransport::~WebRtcTransport() {}
|
WebRtcTransport::~WebRtcTransport() {
|
||||||
|
dtls_transport_ = nullptr;
|
||||||
|
ice_server_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user