Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix h264 freeze. #93

Merged
merged 32 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a1c7145
Improve e2ee, add setSharedKey to KeyProvider.
cloudwebrtc Aug 31, 2023
677bec5
update.
cloudwebrtc Aug 31, 2023
bb20946
reset has_valid_key after RatchetKey.
cloudwebrtc Aug 31, 2023
a4dba5c
update.
cloudwebrtc Aug 31, 2023
c2d3c7d
clone key_handler from share_key for each participant.
cloudwebrtc Aug 31, 2023
5a54963
add RatchetSharedKey and ExportSharedKey.
cloudwebrtc Sep 1, 2023
38894fe
make KeyProvider::SetSharedKey only valid when KeyProviderOptions::sh…
cloudwebrtc Sep 1, 2023
2f8bbd8
remove unused enum.
cloudwebrtc Sep 1, 2023
fcdef0d
Merge pull request #88 from webrtc-sdk/duan/improve-e2ee-api
cloudwebrtc Sep 1, 2023
31774d3
Fix memory leak when creating audio CMSampleBuffer #86
hiroshihorie Sep 1, 2023
7039781
add scalabilityMode for AV1.
cloudwebrtc Sep 8, 2023
de5d25f
fix bug for scalability-mode.
cloudwebrtc Sep 8, 2023
0e4c171
add scalability-mode support for VP9.
cloudwebrtc Sep 8, 2023
6afb155
add failure tolerance for framecryptor.
cloudwebrtc Sep 9, 2023
4911ef8
add failureTolerance for android/objc.
cloudwebrtc Sep 9, 2023
bd21d2c
fix: make H264's unencrypted_bytes consistent with js-sdk.
cloudwebrtc Sep 11, 2023
3c26cf7
wip: ScalabilityModes for android.
cloudwebrtc Sep 13, 2023
7172602
update.
cloudwebrtc Sep 13, 2023
52fce22
wip.
cloudwebrtc Sep 13, 2023
871b74f
wip.
cloudwebrtc Sep 13, 2023
994cc2c
wip.
cloudwebrtc Sep 13, 2023
275277e
wip.
cloudwebrtc Sep 13, 2023
5fdfb76
Fix camera rotation (#92)
hiroshihorie Sep 13, 2023
8b60bcf
done.
cloudwebrtc Sep 13, 2023
e4a4012
fix
cloudwebrtc Sep 13, 2023
6e96936
update.
cloudwebrtc Sep 13, 2023
25372df
Expose audio sample buffers for Android (#89)
davidliu Sep 13, 2023
aba25fb
add SetSifTrailer.
cloudwebrtc Sep 13, 2023
9cda9b3
Merge pull request #91 from webrtc-sdk/e2ee/add-failure-tolerance
cloudwebrtc Sep 14, 2023
2ab452f
Merge pull request #90 from webrtc-sdk/fix/svc-scalability-mode-for-a…
cloudwebrtc Sep 14, 2023
78c7b1f
fix h264 freeze.
cloudwebrtc Sep 14, 2023
ddaa811
Merge branch 'm114_release' into fix/e2ee-fix-h264-freeze
cloudwebrtc Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 72 additions & 21 deletions api/crypto/frame_crypto_transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ const EVP_CIPHER* GetAesCbcAlgorithmFromKeySize(size_t key_size_bytes) {
}
}

inline bool FrameIsH264(webrtc::TransformableFrameInterface* frame,
webrtc::FrameCryptorTransformer::MediaType type) {
switch (type) {
case webrtc::FrameCryptorTransformer::MediaType::kVideoFrame: {
auto videoFrame =
static_cast<webrtc::TransformableVideoFrameInterface*>(frame);
return videoFrame->header().codec ==
webrtc::VideoCodecType::kVideoCodecH264;
}
default:
return false;
}
}

inline bool NeedsRbspUnescaping(const uint8_t* frameData, size_t frameSize) {
for (size_t i = 0; i < frameSize - 3; ++i) {
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved
if (frameData[i] == 0 && frameData[i + 1] == 0 && frameData[i + 2] == 3)
return true;
}
return false;
}

std::string to_uint8_list(const uint8_t* data, int len) {
std::stringstream ss;
ss << "[";
Expand Down Expand Up @@ -395,26 +417,41 @@ void FrameCryptorTransformer::encryptFrame(
key_set->encryption_key, iv, frame_header, payload,
&buffer) == Success) {
rtc::Buffer encrypted_payload(buffer.data(), buffer.size());
rtc::Buffer tag(encrypted_payload.data() + encrypted_payload.size() - 16,
16);
rtc::Buffer data_without_header;
data_without_header.AppendData(encrypted_payload);
data_without_header.AppendData(iv);
data_without_header.AppendData(frame_trailer);

rtc::Buffer data_out;
data_out.AppendData(frame_header);
data_out.AppendData(encrypted_payload);
data_out.AppendData(iv);
data_out.AppendData(frame_trailer);

RTC_CHECK_EQ(data_out.size(), frame_header.size() +
encrypted_payload.size() + iv.size() +
frame_trailer.size());
if (FrameIsH264(frame.get(), type_)) {
H264::WriteRbsp(data_without_header.data(),data_without_header.size(), &data_out);
} else {
data_out.AppendData(data_without_header);
RTC_CHECK_EQ(data_out.size(), frame_header.size() +
encrypted_payload.size() + iv.size() +
frame_trailer.size());
}

frame->SetData(data_out);

RTC_LOG(LS_INFO) << "FrameCryptorTransformer::encryptFrame() ivLength="
<< static_cast<int>(iv.size()) << " unencrypted_bytes="
RTC_LOG(LS_INFO) << "FrameCryptorTransformer::encryptFrame() "
<< "frame length = " << static_cast<int>(date_in.size())
<< " encrypted_length = "
<< static_cast<int>(data_out.size())
<< " ivLength=" << static_cast<int>(iv.size())
<< " unencrypted_bytes="
<< static_cast<int>(unencrypted_bytes)
<< " tag=" << to_hex(tag.data(), tag.size())
<< " key_index=" << static_cast<int>(key_index_)
<< " aesKey="
<< to_hex(key_set->encryption_key.data(),
key_set->encryption_key.size())
<< " iv=" << to_hex(iv.data(), iv.size());

if (last_enc_error_ != FrameCryptionState::kOk) {
last_enc_error_ = FrameCryptionState::kOk;
if (observer_)
Expand Down Expand Up @@ -554,11 +591,34 @@ void FrameCryptorTransformer::decryptFrame(
iv[i] = date_in[date_in.size() - 2 - ivLength + i];
}

rtc::Buffer encrypted_payload(date_in.size() - unencrypted_bytes - ivLength -
2);
for (size_t i = unencrypted_bytes; i < date_in.size() - ivLength - 2; i++) {
encrypted_payload[i - unencrypted_bytes] = date_in[i];
rtc::Buffer encrypted_buffer(date_in.size() - unencrypted_bytes);
for (size_t i = unencrypted_bytes; i < date_in.size(); i++) {
encrypted_buffer[i - unencrypted_bytes] = date_in[i];
}

if (FrameIsH264(frame.get(), type_) &&
NeedsRbspUnescaping(encrypted_buffer.data(), encrypted_buffer.size())) {
encrypted_buffer.SetData(H264::ParseRbsp(encrypted_buffer.data(), encrypted_buffer.size()));
}

rtc::Buffer encrypted_payload(encrypted_buffer.size() - ivLength - 2);
for (size_t i = 0; i < encrypted_payload.size(); i++) {
encrypted_payload[i] = encrypted_buffer[i];
}

rtc::Buffer tag(encrypted_payload.data() + encrypted_payload.size() - 16, 16);
RTC_LOG(LS_INFO) << "FrameCryptorTransformer::decryptFrame() "
<< " frame length = " << static_cast<int>(date_in.size())
<< " ivLength=" << static_cast<int>(iv.size())
<< " unencrypted_bytes="
<< static_cast<int>(unencrypted_bytes)
<< " tag=" << to_hex(tag.data(), tag.size())
<< " key_index=" << static_cast<int>(key_index_)
<< " aesKey="
<< to_hex(key_set->encryption_key.data(),
key_set->encryption_key.size())
<< " iv=" << to_hex(iv.data(), iv.size());

std::vector<uint8_t> buffer;

int ratchet_count = 0;
Expand Down Expand Up @@ -636,15 +696,6 @@ void FrameCryptorTransformer::decryptFrame(
data_out.AppendData(payload);
frame->SetData(data_out);

RTC_LOG(LS_INFO) << "FrameCryptorTransformer::decryptFrame() ivLength="
<< static_cast<int>(ivLength) << " unencrypted_bytes="
<< static_cast<int>(unencrypted_bytes)
<< " key_index=" << static_cast<int>(key_index_)
<< " aesKey="
<< to_hex(key_set->encryption_key.data(),
key_set->encryption_key.size())
<< " iv=" << to_hex(iv.data(), iv.size());

if (last_dec_error_ != FrameCryptionState::kOk) {
last_dec_error_ = FrameCryptionState::kOk;
if (observer_)
Expand Down