Skip to content

Commit

Permalink
RTC: Refine unprotect_rtp to reuse cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Feb 10, 2021
1 parent 719df6f commit aec2745
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 7 additions & 6 deletions trunk/src/app/srs_app_rtc_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ srs_error_t SrsPlaintextTransport::protect_rtp2(void* rtp_hdr, int* len_ptr)

srs_error_t SrsPlaintextTransport::unprotect_rtp(const char* cipher, char* plaintext, int& nb_plaintext)
{
memcpy(plaintext, cipher, nb_plaintext);
return srs_success;
}

Expand Down Expand Up @@ -1148,19 +1147,21 @@ srs_error_t SrsRtcPublishStream::on_rtp(char* data, int nb_data)

// Decrypt the cipher to plaintext RTP data.
int nb_unprotected_buf = nb_data;
char* unprotected_buf = new char[kRtpPacketSize];
if ((err = session_->transport_->unprotect_rtp(data, unprotected_buf, nb_unprotected_buf)) != srs_success) {
if ((err = session_->transport_->unprotect_rtp(data, NULL, nb_unprotected_buf)) != srs_success) {
// We try to decode the RTP header for more detail error informations.
SrsBuffer b(data, nb_data); SrsRtpHeader h; h.ignore_padding(true);
srs_error_t r0 = h.decode(&b); srs_freep(r0); // Ignore any error for header decoding.

err = srs_error_wrap(err, "marker=%u, pt=%u, seq=%u, ts=%u, ssrc=%u, pad=%u, payload=%uB", h.get_marker(), h.get_payload_type(),
h.get_sequence(), h.get_timestamp(), h.get_ssrc(), h.get_padding(), nb_data - b.pos());

srs_freepa(unprotected_buf);
return err;
}

srs_assert(nb_unprotected_buf > 0);
char* unprotected_buf = new char[nb_unprotected_buf];
memcpy(unprotected_buf, data, nb_unprotected_buf);

if (_srs_blackhole->blackhole) {
_srs_blackhole->sendto(unprotected_buf, nb_unprotected_buf);
}
Expand All @@ -1172,8 +1173,8 @@ srs_error_t SrsRtcPublishStream::on_rtp(char* data, int nb_data)
srs_error_t r0 = h.decode(&b); srs_freep(r0); // Ignore any error for header decoding.

int nb_header = h.nb_bytes();
const char* body = unprotected_buf + nb_header;
int nb_body = nb_unprotected_buf - nb_header;
const char* body = data + nb_header;
int nb_body = nb_data - nb_header;
return srs_error_wrap(err, "cipher=%u, plaintext=%u, body=[%s]", nb_data, nb_unprotected_buf,
srs_string_dumps_hex(body, nb_body, 8).c_str());
}
Expand Down
4 changes: 1 addition & 3 deletions trunk/src/app/srs_app_rtc_dtls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,10 +1019,8 @@ srs_error_t SrsSRTP::unprotect_rtp(const char* cipher, char* plaintext, int& nb_
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "not ready");
}

memcpy(plaintext, cipher, nb_plaintext);

srtp_err_status_t r0 = srtp_err_status_ok;
if ((r0 = srtp_unprotect(recv_ctx_, plaintext, &nb_plaintext)) != srtp_err_status_ok) {
if ((r0 = srtp_unprotect(recv_ctx_, (void*)cipher, &nb_plaintext)) != srtp_err_status_ok) {
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "rtp unprotect r0=%u", r0);
}

Expand Down

0 comments on commit aec2745

Please sign in to comment.