Skip to content

Commit b0800f5

Browse files
committed
Threads-SRTP: Support async decrypt RTCP
1. SrsAsyncSRTP support unprotect_rtcp packet. 2. Extract on_rtcp_plaintext from on_rtcp.
1 parent 949c80e commit b0800f5

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

trunk/src/app/srs_app_rtc_conn.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ srs_error_t SrsSecurityTransport::on_rtp_plaintext(char* plaintext, int size)
204204
return session_->on_rtp_plaintext(plaintext, size);
205205
}
206206

207+
srs_error_t SrsSecurityTransport::on_rtcp_plaintext(char* plaintext, int size)
208+
{
209+
return session_->on_rtcp_plaintext(plaintext, size);
210+
}
211+
207212
srs_error_t SrsSecurityTransport::protect_rtp(void* packet, int* nb_cipher)
208213
{
209214
return srtp_->protect_rtp(packet, nb_cipher);
@@ -2001,6 +2006,23 @@ srs_error_t SrsRtcConnection::on_rtcp(char* data, int nb_data)
20012006
return srs_error_wrap(err, "rtcp unprotect");
20022007
}
20032008

2009+
// For async SRTP, the nb_unprotected_buf might be zero, which means we do not got the plaintext
2010+
// right now, and it will callback if get one.
2011+
if (nb_unprotected_buf == 0) {
2012+
return err;
2013+
}
2014+
2015+
if ((err = on_rtcp_plaintext(data, nb_unprotected_buf)) != srs_success) {
2016+
return srs_error_wrap(err, "cipher=%d", nb_data);
2017+
}
2018+
2019+
return err;
2020+
}
2021+
2022+
srs_error_t SrsRtcConnection::on_rtcp_plaintext(char* data, int nb_unprotected_buf)
2023+
{
2024+
srs_error_t err = srs_success;
2025+
20042026
char* unprotected_buf = data;
20052027
if (_srs_blackhole->blackhole) {
20062028
_srs_blackhole->sendto(unprotected_buf, nb_unprotected_buf);
@@ -2022,7 +2044,7 @@ srs_error_t SrsRtcConnection::on_rtcp(char* data, int nb_data)
20222044
SrsAutoFree(SrsRtcpCommon, rtcp);
20232045

20242046
if(srs_success != err) {
2025-
return srs_error_wrap(err, "cipher=%u, plaintext=%u, bytes=[%s], rtcp=(%u,%u,%u,%u)", nb_data, nb_unprotected_buf,
2047+
return srs_error_wrap(err, "plaintext=%u, bytes=[%s], rtcp=(%u,%u,%u,%u)", nb_unprotected_buf,
20262048
srs_string_dumps_hex(rtcp->data(), rtcp->size(), rtcp->size()).c_str(),
20272049
rtcp->get_rc(), rtcp->type(), rtcp->get_ssrc(), rtcp->size());
20282050
}

trunk/src/app/srs_app_rtc_conn.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class SrsSecurityTransport : public ISrsRtcTransport
144144
srs_error_t srtp_initialize();
145145
public:
146146
srs_error_t on_rtp_plaintext(char* plaintext, int size);
147+
srs_error_t on_rtcp_plaintext(char* plaintext, int size);
147148
};
148149

149150
// Semi security transport, setup DTLS and SRTP, with SRTP decrypt, without SRTP encrypt.
@@ -493,6 +494,8 @@ class SrsRtcConnection : public ISrsResource
493494
srs_error_t find_publisher(char* buf, int size, SrsRtcPublishStream** ppublisher);
494495
public:
495496
srs_error_t on_rtcp(char* data, int nb_data);
497+
private:
498+
srs_error_t on_rtcp_plaintext(char* plaintext, int size);
496499
private:
497500
srs_error_t dispatch_rtcp(SrsRtcpCommon* rtcp);
498501
public:

trunk/src/app/srs_app_threads.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,9 @@ srs_error_t SrsAsyncSRTP::unprotect_rtp(void* packet, int* nb_plaintext)
814814
_srs_async_srtp->add_packet(pkt);
815815

816816
// Do the job asynchronously.
817-
*nb_plaintext = 0;
817+
if (nb_plaintext) {
818+
*nb_plaintext = 0;
819+
}
818820

819821
return srs_success;
820822
}
@@ -825,8 +827,22 @@ srs_error_t SrsAsyncSRTP::unprotect_rtcp(void* packet, int* nb_plaintext)
825827
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "not ready");
826828
}
827829

828-
// TODO: FIMXE: Remove it.
829-
return SrsSRTP::unprotect_rtcp(packet, nb_plaintext);
830+
int nb_cipher = *nb_plaintext;
831+
char* buf = new char[nb_cipher];
832+
memcpy(buf, packet, nb_cipher);
833+
834+
SrsAsyncSRTPPacket* pkt = new SrsAsyncSRTPPacket(task_);
835+
pkt->msg_->wrap(buf, nb_cipher);
836+
pkt->is_rtp_ = false;
837+
pkt->do_decrypt_ = true;
838+
_srs_async_srtp->add_packet(pkt);
839+
840+
// Do the job asynchronously.
841+
if (nb_plaintext) {
842+
*nb_plaintext = 0;
843+
}
844+
845+
return srs_success;
830846
}
831847

832848
SrsAsyncSRTPTask::SrsAsyncSRTPTask(SrsAsyncSRTP* codec)
@@ -871,10 +887,12 @@ srs_error_t SrsAsyncSRTPTask::cook(SrsAsyncSRTPPacket* pkt)
871887
return err;
872888
}
873889

890+
pkt->nb_consumed_ = pkt->msg_->size;
874891
if (pkt->do_decrypt_) {
875892
if (pkt->is_rtp_) {
876-
pkt->nb_consumed_ = pkt->msg_->size;
877893
err = impl_->unprotect_rtp(pkt->msg_->payload, &pkt->nb_consumed_);
894+
} else {
895+
err = impl_->unprotect_rtcp(pkt->msg_->payload, &pkt->nb_consumed_);
878896
}
879897
}
880898
if (err != srs_success) {
@@ -898,6 +916,8 @@ srs_error_t SrsAsyncSRTPTask::consume(SrsAsyncSRTPPacket* pkt)
898916
if (pkt->do_decrypt_) {
899917
if (pkt->is_rtp_) {
900918
err = codec_->transport_->on_rtp_plaintext(payload, pkt->nb_consumed_);
919+
} else {
920+
err = codec_->transport_->on_rtcp_plaintext(payload, pkt->nb_consumed_);
901921
}
902922
}
903923

0 commit comments

Comments
 (0)