Skip to content

Commit a26b926

Browse files
committed
Threads-SRTP: Use async encrypt SRTP packet
1. Async SRTP support protect RTP. 2. Send packet ignore when encrypt size is 0. 3. Callback to send packet if encrypt done.
1 parent 56ffc28 commit a26b926

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

trunk/src/app/srs_app_rtc_conn.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ srs_error_t SrsSecurityTransport::on_rtcp_plaintext(char* plaintext, int size)
209209
return session_->on_rtcp_plaintext(plaintext, size);
210210
}
211211

212+
srs_error_t SrsSecurityTransport::on_rtp_cipher(char* cipher, int size)
213+
{
214+
return session_->on_rtp_cipher(cipher, size);
215+
}
216+
212217
srs_error_t SrsSecurityTransport::protect_rtp(void* packet, int* nb_cipher)
213218
{
214219
return srtp_->protect_rtp(packet, nb_cipher);
@@ -2053,6 +2058,17 @@ srs_error_t SrsRtcConnection::on_rtcp_plaintext(char* data, int nb_unprotected_b
20532058
return err;
20542059
}
20552060

2061+
srs_error_t SrsRtcConnection::on_rtp_cipher(char* cipher, int size)
2062+
{
2063+
srs_error_t err = srs_success;
2064+
2065+
if ((err = sendonly_skt->sendto(cipher, size, 0)) != srs_success) {
2066+
srs_error_reset(err); // Ignore any error.
2067+
}
2068+
2069+
return err;
2070+
}
2071+
20562072
srs_error_t SrsRtcConnection::dispatch_rtcp(SrsRtcpCommon* rtcp)
20572073
{
20582074
srs_error_t err = srs_success;
@@ -2592,6 +2608,11 @@ srs_error_t SrsRtcConnection::do_send_packet(SrsRtpPacket2* pkt)
25922608
iov->iov_len = (size_t)nn_encrypt;
25932609
}
25942610

2611+
// Async SRTP encrypt.
2612+
if (iov->iov_len <= 0) {
2613+
return err;
2614+
}
2615+
25952616
// For NACK simulator, drop packet.
25962617
if (nn_simulate_player_nack_drop) {
25972618
simulate_player_drop_packet(&pkt->header, (int)iov->iov_len);
@@ -2601,8 +2622,9 @@ srs_error_t SrsRtcConnection::do_send_packet(SrsRtpPacket2* pkt)
26012622

26022623
++_srs_pps_srtps->sugar;
26032624

2604-
// TODO: FIXME: Handle error.
2605-
sendonly_skt->sendto(iov->iov_base, iov->iov_len, 0);
2625+
if ((err = sendonly_skt->sendto(iov->iov_base, iov->iov_len, 0)) != srs_success) {
2626+
srs_error_reset(err); // Ignore any error.
2627+
}
26062628

26072629
// Detail log, should disable it in release version.
26082630
srs_info("RTC: SEND PT=%u, SSRC=%#x, SEQ=%u, Time=%u, %u/%u bytes", pkt->header.get_payload_type(), pkt->header.get_ssrc(),

trunk/src/app/srs_app_rtc_conn.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class SrsSecurityTransport : public ISrsRtcTransport
145145
public:
146146
srs_error_t on_rtp_plaintext(char* plaintext, int size);
147147
srs_error_t on_rtcp_plaintext(char* plaintext, int size);
148+
srs_error_t on_rtp_cipher(char* cipher, int size);
148149
};
149150

150151
// Semi security transport, setup DTLS and SRTP, with SRTP decrypt, without SRTP encrypt.
@@ -496,6 +497,8 @@ class SrsRtcConnection : public ISrsResource
496497
srs_error_t on_rtcp(char* data, int nb_data);
497498
private:
498499
srs_error_t on_rtcp_plaintext(char* plaintext, int size);
500+
private:
501+
srs_error_t on_rtp_cipher(char* cipher, int size);
499502
private:
500503
srs_error_t dispatch_rtcp(SrsRtcpCommon* rtcp);
501504
public:

trunk/src/app/srs_app_threads.cpp

+27-8
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,23 @@ srs_error_t SrsAsyncSRTP::protect_rtp(void* packet, int* nb_cipher)
785785
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "not ready");
786786
}
787787

788-
// TODO: FIMXE: Remove it.
789-
return SrsSRTP::protect_rtp(packet, nb_cipher);
788+
int nb_plaintext = *nb_cipher;
789+
790+
// Note that we must allocate more bytes than the size of packet, because SRTP
791+
// will write checksum at the end of buffer.
792+
char* buf = new char[kRtcpPacketSize];
793+
memcpy(buf, packet, nb_plaintext);
794+
795+
SrsAsyncSRTPPacket* pkt = new SrsAsyncSRTPPacket(task_);
796+
pkt->msg_->wrap(buf, nb_plaintext);
797+
pkt->is_rtp_ = true;
798+
pkt->do_decrypt_ = false;
799+
_srs_async_srtp->add_packet(pkt);
800+
801+
// Do the job asynchronously.
802+
*nb_cipher = 0;
803+
804+
return srs_success;
790805
}
791806

792807
srs_error_t SrsAsyncSRTP::protect_rtcp(void* packet, int* nb_cipher)
@@ -816,9 +831,7 @@ srs_error_t SrsAsyncSRTP::unprotect_rtp(void* packet, int* nb_plaintext)
816831
_srs_async_srtp->add_packet(pkt);
817832

818833
// Do the job asynchronously.
819-
if (nb_plaintext) {
820-
*nb_plaintext = 0;
821-
}
834+
*nb_plaintext = 0;
822835

823836
return srs_success;
824837
}
@@ -840,9 +853,7 @@ srs_error_t SrsAsyncSRTP::unprotect_rtcp(void* packet, int* nb_plaintext)
840853
_srs_async_srtp->add_packet(pkt);
841854

842855
// Do the job asynchronously.
843-
if (nb_plaintext) {
844-
*nb_plaintext = 0;
845-
}
856+
*nb_plaintext = 0;
846857

847858
return srs_success;
848859
}
@@ -896,6 +907,10 @@ srs_error_t SrsAsyncSRTPTask::cook(SrsAsyncSRTPPacket* pkt)
896907
} else {
897908
err = impl_->unprotect_rtcp(pkt->msg_->payload, &pkt->nb_consumed_);
898909
}
910+
} else {
911+
if (pkt->is_rtp_) {
912+
err = impl_->protect_rtp(pkt->msg_->payload, &pkt->nb_consumed_);
913+
}
899914
}
900915
if (err != srs_success) {
901916
return err;
@@ -921,6 +936,10 @@ srs_error_t SrsAsyncSRTPTask::consume(SrsAsyncSRTPPacket* pkt)
921936
} else {
922937
err = codec_->transport_->on_rtcp_plaintext(payload, pkt->nb_consumed_);
923938
}
939+
} else {
940+
if (pkt->is_rtp_) {
941+
err = codec_->transport_->on_rtp_cipher(payload, pkt->nb_consumed_);
942+
}
924943
}
925944

926945
return err;

0 commit comments

Comments
 (0)