- improve SRTP decoder API

This commit is contained in:
Dmytro Bogovych 2025-08-24 14:01:43 +03:00
parent 6b7b086bf7
commit e6cb2a22f7
4 changed files with 18 additions and 17 deletions

View File

@ -306,18 +306,22 @@ void AudioStream::dataArrived(PDatagramSocket s, const void* buffer, int length,
int receiveLength = length; int receiveLength = length;
memcpy(mReceiveBuffer, buffer, length); memcpy(mReceiveBuffer, buffer, length);
bool srtpResult;
if (mSrtpSession.active()) if (mSrtpSession.active())
{ {
bool srtpResult;
size_t srcLength = length; size_t dstLength = sizeof mSrtpDecodeBuffer;
if (RtpHelper::isRtp(mReceiveBuffer, receiveLength)) if (RtpHelper::isRtp(mReceiveBuffer, receiveLength))
srtpResult = mSrtpSession.unprotectRtp(mReceiveBuffer, &receiveLength); srtpResult = mSrtpSession.unprotectRtp(mReceiveBuffer, srcLength, mSrtpDecodeBuffer, &dstLength);
else else
srtpResult = mSrtpSession.unprotectRtcp(mReceiveBuffer, &receiveLength); srtpResult = mSrtpSession.unprotectRtcp(mReceiveBuffer, srcLength, mSrtpDecodeBuffer, &dstLength);
if (!srtpResult) if (!srtpResult)
{ {
ICELogError(<<"Cannot decrypt SRTP packet."); ICELogError(<<"Cannot decrypt SRTP packet.");
return; return;
} }
memcpy(mReceiveBuffer, mSrtpDecodeBuffer, dstLength);
receiveLength = dstLength;
} }
switch (source.family()) switch (source.family())

View File

@ -87,7 +87,8 @@ protected:
mCaptureResampler32, mCaptureResampler32,
mCaptureResampler48; mCaptureResampler48;
DtmfContext mDtmfContext; DtmfContext mDtmfContext;
char mReceiveBuffer[MAX_VALID_UDPPACKET_SIZE]; char mReceiveBuffer[MAX_VALID_UDPPACKET_SIZE],
mSrtpDecodeBuffer[MAX_VALID_UDPPACKET_SIZE];
struct struct
{ {

View File

@ -209,36 +209,32 @@ bool SrtpSession::protectRtcp(void* buffer, int* length)
return false; return false;
} }
bool SrtpSession::unprotectRtp(void* buffer, int* length) bool SrtpSession::unprotectRtp(const void* src, size_t srcLength, void* dst, size_t* dstLength)
{ {
//addSsrc(RtpHelper::findSsrc(buffer, *length), sdIncoming); //addSsrc(RtpHelper::findSsrc(buffer, *length), sdIncoming);
Lock l(mGuard); Lock l(mGuard);
if (mInboundSession) if (mInboundSession)
{ {
size_t rtp_len = MAX_VALID_UDPPACKET_SIZE;
auto code = srtp_unprotect(mInboundSession, auto code = srtp_unprotect(mInboundSession,
(const uint8_t*)buffer, (size_t)*length, (const uint8_t*)src, srcLength,
(uint8_t*)buffer, &rtp_len); (uint8_t*)dst, dstLength);
*length = rtp_len;
return code == 0; return code == 0;
} }
else else
return false; return false;
} }
bool SrtpSession::unprotectRtcp(void* buffer, int* length) bool SrtpSession::unprotectRtcp(const void* src, size_t srcLength, void* dst, size_t* dstLength)
{ {
//addSsrc(RtpHelper::findSsrc(buffer, *length), sdIncoming); //addSsrc(RtpHelper::findSsrc(buffer, *length), sdIncoming);
Lock l(mGuard); Lock l(mGuard);
if (mInboundSession) if (mInboundSession)
{ {
size_t rtp_len = MAX_VALID_UDPPACKET_SIZE;
auto code = srtp_unprotect_rtcp(mInboundSession, auto code = srtp_unprotect_rtcp(mInboundSession,
(const uint8_t*)buffer, (size_t)*length, (const uint8_t*)src, srcLength,
(uint8_t*)buffer, (size_t*)rtp_len); (uint8_t*)dst, dstLength);
*length = rtp_len;
return code == 0; return code == 0;
} }
else else

View File

@ -48,8 +48,8 @@ public:
/* bufferPtr is RTP packet data i.e. header + payload. Buffer must be big enough to hold encrypted data. */ /* bufferPtr is RTP packet data i.e. header + payload. Buffer must be big enough to hold encrypted data. */
bool protectRtp(void* buffer, int* length); bool protectRtp(void* buffer, int* length);
bool protectRtcp(void* buffer, int* length); bool protectRtcp(void* buffer, int* length);
bool unprotectRtp(void* buffer, int* length); bool unprotectRtp(const void* src, size_t srcLength, void* dst, size_t* dstLength);
bool unprotectRtcp(void* buffer, int* length); bool unprotectRtcp(const void* src, size_t srcLength, void* dst, size_t* dstLength);
static void initSrtp(); static void initSrtp();