ZLMediaKit/webrtc/SrtpSession.hpp

69 lines
1.5 KiB
C++
Raw Normal View History

2021-03-24 16:52:41 +08:00
#ifndef MS_RTC_SRTP_SESSION_HPP
#define MS_RTC_SRTP_SESSION_HPP
2021-03-27 09:13:10 +08:00
#include "Utils.hpp"
2021-03-24 16:52:41 +08:00
#include <srtp2/srtp.h>
#include <vector>
2021-03-27 09:38:13 +08:00
#include <memory>
2021-03-24 16:52:41 +08:00
2021-03-26 11:07:03 +08:00
namespace RTC
{
2021-03-27 09:38:13 +08:00
class DepLibSRTP : public std::enable_shared_from_this<DepLibSRTP>
{
2021-03-26 11:07:03 +08:00
public:
2021-03-27 09:38:13 +08:00
using Ptr = std::shared_ptr<DepLibSRTP>;
~DepLibSRTP();
static bool IsError(srtp_err_status_t code);
static const char *GetErrorString(srtp_err_status_t code);
static DepLibSRTP &Instance();
2021-03-26 11:07:03 +08:00
private:
2021-03-27 09:38:13 +08:00
DepLibSRTP();
2021-03-26 11:07:03 +08:00
};
class SrtpSession
{
public:
enum class CryptoSuite
{
NONE = 0,
AES_CM_128_HMAC_SHA1_80 = 1,
AES_CM_128_HMAC_SHA1_32,
AEAD_AES_256_GCM,
AEAD_AES_128_GCM
};
public:
enum class Type
{
INBOUND = 1,
OUTBOUND
};
public:
SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t* key, size_t keyLen);
~SrtpSession();
public:
bool EncryptRtp(const uint8_t** data, size_t* len, uint8_t pt);
2021-03-26 11:07:03 +08:00
bool DecryptSrtp(uint8_t* data, size_t* len);
bool EncryptRtcp(const uint8_t** data, size_t* len);
bool DecryptSrtcp(uint8_t* data, size_t* len);
void RemoveStream(uint32_t ssrc)
{
srtp_remove_stream(this->session, uint32_t{ htonl(ssrc) });
}
private:
// Allocated by this.
srtp_t session{ nullptr };
2021-03-27 09:38:13 +08:00
//rtp包最大1600
static constexpr size_t EncryptBufferSize{ 2000 };
2021-03-27 09:38:13 +08:00
uint8_t EncryptBuffer[EncryptBufferSize];
DepLibSRTP::Ptr _env;
2021-03-26 11:07:03 +08:00
};
} // namespace RTC
2021-03-24 16:52:41 +08:00
#endif