Skip to content

E2EE for H.265 #187

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

Merged
merged 2 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
63 changes: 63 additions & 0 deletions api/crypto/frame_crypto_transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "absl/types/variant.h"
#include "api/array_view.h"
#include "common_video/h264/h264_common.h"
#include "common_video/h265/h265_common.h"
#include "modules/rtp_rtcp/source/rtp_format_h264.h"
#include "rtc_base/byte_buffer.h"
#include "rtc_base/logging.h"
Expand Down Expand Up @@ -96,6 +97,40 @@ inline bool FrameIsH264(webrtc::TransformableFrameInterface* frame,
}
}

inline bool FrameIsH265(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::kVideoCodecH265;
}
default:
return false;
}
}

inline bool IsH265SliceNalu(webrtc::H265::NaluType nalu_type) {
// VCL NALUs (Video Coding Layer) - slice segments
return nalu_type == webrtc::H265::NaluType::kTrailN ||
nalu_type == webrtc::H265::NaluType::kTrailR ||
nalu_type == webrtc::H265::NaluType::kTsaN ||
nalu_type == webrtc::H265::NaluType::kTsaR ||
nalu_type == webrtc::H265::NaluType::kStsaN ||
nalu_type == webrtc::H265::NaluType::kStsaR ||
nalu_type == webrtc::H265::NaluType::kRadlN ||
nalu_type == webrtc::H265::NaluType::kRadlR ||
nalu_type == webrtc::H265::NaluType::kRaslN ||
nalu_type == webrtc::H265::NaluType::kRaslR ||
nalu_type == webrtc::H265::NaluType::kBlaWLp ||
nalu_type == webrtc::H265::NaluType::kBlaWRadl ||
nalu_type == webrtc::H265::NaluType::kBlaNLp ||
nalu_type == webrtc::H265::NaluType::kIdrWRadl ||
nalu_type == webrtc::H265::NaluType::kIdrNLp ||
nalu_type == webrtc::H265::NaluType::kCra;
}

inline bool NeedsRbspUnescaping(const uint8_t* frameData, size_t frameSize) {
for (size_t i = 0; i < frameSize - 3; ++i) {
if (frameData[i] == 0 && frameData[i + 1] == 0 && frameData[i + 2] == 3)
Expand Down Expand Up @@ -163,6 +198,27 @@ uint8_t get_unencrypted_bytes(webrtc::TransformableFrameInterface* frame,
break;
}
}
} else if (videoFrame->header().codec ==
webrtc::VideoCodecType::kVideoCodecH265) {
rtc::ArrayView<const uint8_t> data_in = frame->GetData();
std::vector<webrtc::H265::NaluIndex> nalu_indices =
webrtc::H265::FindNaluIndices(data_in);

int idx = 0;
for (const auto& index : nalu_indices) {
const uint8_t* slice = data_in.data() + index.payload_start_offset;
webrtc::H265::NaluType nalu_type =
webrtc::H265::ParseNaluType(slice[0]);
if (IsH265SliceNalu(nalu_type)) {
// H.265 has a 2-byte NALU header, so unencrypted bytes = offset + header size
unencrypted_bytes = index.payload_start_offset + webrtc::H265::kNaluHeaderSize;
RTC_LOG(LS_INFO)
<< "H265 NonParameterSetNalu::payload_size: " << index.payload_size
<< ", nalu_type " << static_cast<int>(nalu_type) << ", NaluIndex [" << idx++
<< "] offset: " << index.payload_start_offset << ", unencrypted_bytes: " << unencrypted_bytes;
return unencrypted_bytes;
}
}
}
break;
}
Expand Down Expand Up @@ -413,6 +469,9 @@ void FrameCryptorTransformer::encryptFrame(
if (FrameIsH264(frame.get(), type_)) {
H264::WriteRbsp(data_without_header.data(), data_without_header.size(),
&data_out);
} else if (FrameIsH265(frame.get(), type_)) {
H265::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() +
Expand Down Expand Up @@ -561,6 +620,10 @@ void FrameCryptorTransformer::decryptFrame(
NeedsRbspUnescaping(encrypted_buffer.data(), encrypted_buffer.size())) {
encrypted_buffer.SetData(
H264::ParseRbsp(encrypted_buffer.data(), encrypted_buffer.size()));
} else if (FrameIsH265(frame.get(), type_) &&
NeedsRbspUnescaping(encrypted_buffer.data(), encrypted_buffer.size())) {
encrypted_buffer.SetData(
H265::ParseRbsp(encrypted_buffer.data(), encrypted_buffer.size()));
}

rtc::Buffer encrypted_payload(encrypted_buffer.size() - ivLength - 2);
Expand Down
2 changes: 2 additions & 0 deletions common_video/h265/h265_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ enum NaluType : uint8_t {
kStsaR = 5,
kRadlN = 6,
kRadlR = 7,
kRaslN = 8,
kRaslR = 9,
kBlaWLp = 16,
kBlaWRadl = 17,
kBlaNLp = 18,
Expand Down