Skip to content

Commit

Permalink
refactor naming
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 5, 2024
1 parent e057e2a commit 9c53286
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 98 deletions.
66 changes: 33 additions & 33 deletions src/dpp/dave/codec_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ std::optional<IndexStartCodeSizePair> FindNextH26XNaluIndex(const uint8_t* buffe
return std::nullopt;
}

bool process_frame_opus(OutboundFrameProcessor& processor, array_view<const uint8_t> frame)
bool process_frame_opus(outbound_frame_processor& processor, array_view<const uint8_t> frame)
{
processor.AddEncryptedBytes(frame.data(), frame.size());
processor.add_encrypted_bytes(frame.data(), frame.size());
return true;
}

bool process_frame_vp8(OutboundFrameProcessor& processor, array_view<const uint8_t> frame)
bool process_frame_vp8(outbound_frame_processor& processor, array_view<const uint8_t> frame)
{
constexpr uint8_t kVP8KeyFrameUnencryptedBytes = 10;
constexpr uint8_t kVP8DeltaFrameUnencryptedBytes = 1;
Expand All @@ -169,21 +169,21 @@ bool process_frame_vp8(OutboundFrameProcessor& processor, array_view<const uint8
unencryptedHeaderBytes = kVP8DeltaFrameUnencryptedBytes;
}

processor.AddUnencryptedBytes(frame.data(), unencryptedHeaderBytes);
processor.AddEncryptedBytes(frame.data() + unencryptedHeaderBytes,
frame.size() - unencryptedHeaderBytes);
processor.add_unencrypted_bytes(frame.data(), unencryptedHeaderBytes);
processor.add_encrypted_bytes(frame.data() + unencryptedHeaderBytes,
frame.size() - unencryptedHeaderBytes);
return true;
}

bool process_frame_vp9(OutboundFrameProcessor& processor, array_view<const uint8_t> frame)
bool process_frame_vp9(outbound_frame_processor& processor, array_view<const uint8_t> frame)
{
// payload descriptor is unencrypted in each packet
// and includes all information the depacketizer needs
processor.AddEncryptedBytes(frame.data(), frame.size());
processor.add_encrypted_bytes(frame.data(), frame.size());
return true;
}

bool process_frame_h264(OutboundFrameProcessor& processor, array_view<const uint8_t> frame)
bool process_frame_h264(outbound_frame_processor& processor, array_view<const uint8_t> frame)
{
// minimize the amount of unencrypted header data for H264 depending on the NAL unit
// type from WebRTC, see: src/modules/rtp_rtcp/source/rtp_format_h264.cc
Expand Down Expand Up @@ -215,7 +215,7 @@ bool process_frame_h264(OutboundFrameProcessor& processor, array_view<const uint

// Because WebRTC will convert them all start codes to 4-byte on the receiver side
// always write a long start code and then the NAL unit
processor.AddUnencryptedBytes(kH26XNaluLongStartCode, sizeof(kH26XNaluLongStartCode));
processor.add_unencrypted_bytes(kH26XNaluLongStartCode, sizeof(kH26XNaluLongStartCode));

auto nextNaluIndexPair =
FindNextH26XNaluIndex(frame.data(), frame.size(), nalUnitStartIndex);
Expand All @@ -230,16 +230,16 @@ bool process_frame_h264(OutboundFrameProcessor& processor, array_view<const uint
auto nalUnitPPSBytes = BytesCoveringH264PPS(frame.data() + nalUnitPayloadStart,
frame.size() - nalUnitPayloadStart);

processor.AddUnencryptedBytes(frame.data() + nalUnitStartIndex,
kH264NalUnitHeaderSize + nalUnitPPSBytes);
processor.AddEncryptedBytes(
frame.data() + nalUnitStartIndex + kH264NalUnitHeaderSize + nalUnitPPSBytes,
nextNaluStart - nalUnitStartIndex - kH264NalUnitHeaderSize - nalUnitPPSBytes);
processor.add_unencrypted_bytes(frame.data() + nalUnitStartIndex,
kH264NalUnitHeaderSize + nalUnitPPSBytes);
processor.add_encrypted_bytes(
frame.data() + nalUnitStartIndex + kH264NalUnitHeaderSize + nalUnitPPSBytes,
nextNaluStart - nalUnitStartIndex - kH264NalUnitHeaderSize - nalUnitPPSBytes);
}
else {
// copy the whole NAL unit
processor.AddUnencryptedBytes(frame.data() + nalUnitStartIndex,
nextNaluStart - nalUnitStartIndex);
processor.add_unencrypted_bytes(frame.data() + nalUnitStartIndex,
nextNaluStart - nalUnitStartIndex);
}

naluIndexPair = nextNaluIndexPair;
Expand All @@ -248,7 +248,7 @@ bool process_frame_h264(OutboundFrameProcessor& processor, array_view<const uint
return true;
}

bool process_frame_h265(OutboundFrameProcessor& processor, array_view<const uint8_t> frame)
bool process_frame_h265(outbound_frame_processor& processor, array_view<const uint8_t> frame)
{
// minimize the amount of unencrypted header data for H265 depending on the NAL unit
// type from WebRTC, see: src/modules/rtp_rtcp/source/rtp_format_h265.cc
Expand Down Expand Up @@ -279,7 +279,7 @@ bool process_frame_h265(OutboundFrameProcessor& processor, array_view<const uint

// Because WebRTC will convert them all start codes to 4-byte on the receiver side
// always write a long start code and then the NAL unit
processor.AddUnencryptedBytes(kH26XNaluLongStartCode, sizeof(kH26XNaluLongStartCode));
processor.add_unencrypted_bytes(kH26XNaluLongStartCode, sizeof(kH26XNaluLongStartCode));

auto nextNaluIndexPair =
FindNextH26XNaluIndex(frame.data(), frame.size(), nalUnitStartIndex);
Expand All @@ -289,14 +289,14 @@ bool process_frame_h265(OutboundFrameProcessor& processor, array_view<const uint

if (nalType < kH265NalTypeVclCutoff) {
// found a VCL NAL, encrypt the payload only
processor.AddUnencryptedBytes(frame.data() + nalUnitStartIndex, kH265NalUnitHeaderSize);
processor.AddEncryptedBytes(frame.data() + nalUnitStartIndex + kH265NalUnitHeaderSize,
nextNaluStart - nalUnitStartIndex - kH265NalUnitHeaderSize);
processor.add_unencrypted_bytes(frame.data() + nalUnitStartIndex, kH265NalUnitHeaderSize);
processor.add_encrypted_bytes(frame.data() + nalUnitStartIndex + kH265NalUnitHeaderSize,
nextNaluStart - nalUnitStartIndex - kH265NalUnitHeaderSize);
}
else {
// copy the whole NAL unit
processor.AddUnencryptedBytes(frame.data() + nalUnitStartIndex,
nextNaluStart - nalUnitStartIndex);
processor.add_unencrypted_bytes(frame.data() + nalUnitStartIndex,
nextNaluStart - nalUnitStartIndex);
}

naluIndexPair = nextNaluIndexPair;
Expand All @@ -305,7 +305,7 @@ bool process_frame_h265(OutboundFrameProcessor& processor, array_view<const uint
return true;
}

bool process_frame_av1(OutboundFrameProcessor& processor, array_view<const uint8_t> frame)
bool process_frame_av1(outbound_frame_processor& processor, array_view<const uint8_t> frame)
{
constexpr uint8_t kAv1ObuHeaderHasExtensionMask = 0b0'0000'100;
constexpr uint8_t kAv1ObuHeaderHasSizeMask = 0b0'0000'010;
Expand Down Expand Up @@ -382,11 +382,11 @@ bool process_frame_av1(OutboundFrameProcessor& processor, array_view<const uint8
}

// write the OBU header unencrypted
processor.AddUnencryptedBytes(&obuHeader, sizeof(obuHeader));
processor.add_unencrypted_bytes(&obuHeader, sizeof(obuHeader));
if (obuHasExtension) {
// write the extension byte unencrypted
processor.AddUnencryptedBytes(frame.data() + obuHeaderIndex + sizeof(obuHeader),
kObuExtensionSizeBytes);
processor.add_unencrypted_bytes(frame.data() + obuHeaderIndex + sizeof(obuHeader),
kObuExtensionSizeBytes);
}

// write the OBU payload size unencrypted if it was present and we didn't rewrite
Expand All @@ -397,28 +397,28 @@ bool process_frame_av1(OutboundFrameProcessor& processor, array_view<const uint8
// we sanitize the size by re-writing it ourselves
uint8_t leb128Buffer[Leb128MaxSize];
size_t additionalBytesToWrite = WriteLeb128(obuPayloadSize, leb128Buffer);
processor.AddUnencryptedBytes(leb128Buffer, additionalBytesToWrite);
processor.add_unencrypted_bytes(leb128Buffer, additionalBytesToWrite);
}

// add the OBU payload, encrypted
processor.AddEncryptedBytes(frame.data() + obuPayloadIndex, obuPayloadSize);
processor.add_encrypted_bytes(frame.data() + obuPayloadIndex, obuPayloadSize);
}
}

return true;
}

bool validate_encrypted_frame(OutboundFrameProcessor& processor, array_view<uint8_t> frame)
bool validate_encrypted_frame(outbound_frame_processor& processor, array_view<uint8_t> frame)
{
auto codec = processor.GetCodec();
auto codec = processor.get_codec();
if (codec != Codec::H264 && codec != Codec::H265) {
return true;
}

static_assert(kH26XNaluShortStartSequenceSize - 1 >= 0, "Padding will overflow!");
constexpr size_t Padding = kH26XNaluShortStartSequenceSize - 1;

const auto& unencryptedRanges = processor.GetUnencryptedRanges();
const auto& unencryptedRanges = processor.get_unencrypted_ranges();

// H264 and H265 ciphertexts cannot contain a 3 or 4 byte start code {0, 0, 1}
// otherwise the packetizer gets confused
Expand Down
14 changes: 7 additions & 7 deletions src/dpp/dave/codec_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@

namespace dpp::dave::codec_utils {

bool process_frame_opus(OutboundFrameProcessor & processor, array_view<const uint8_t> frame);
bool process_frame_vp8(OutboundFrameProcessor & processor, array_view<const uint8_t> frame);
bool process_frame_vp9(OutboundFrameProcessor & processor, array_view<const uint8_t> frame);
bool process_frame_h264(OutboundFrameProcessor & processor, array_view<const uint8_t> frame);
bool process_frame_h265(OutboundFrameProcessor & processor, array_view<const uint8_t> frame);
bool process_frame_av1(OutboundFrameProcessor & processor, array_view<const uint8_t> frame);
bool process_frame_opus(outbound_frame_processor & processor, array_view<const uint8_t> frame);
bool process_frame_vp8(outbound_frame_processor & processor, array_view<const uint8_t> frame);
bool process_frame_vp9(outbound_frame_processor & processor, array_view<const uint8_t> frame);
bool process_frame_h264(outbound_frame_processor & processor, array_view<const uint8_t> frame);
bool process_frame_h265(outbound_frame_processor & processor, array_view<const uint8_t> frame);
bool process_frame_av1(outbound_frame_processor & processor, array_view<const uint8_t> frame);

bool validate_encrypted_frame(OutboundFrameProcessor& processor, array_view<uint8_t> frame);
bool validate_encrypted_frame(outbound_frame_processor& processor, array_view<uint8_t> frame);

} // namespace dpp::dave::codec_utils

Expand Down
16 changes: 8 additions & 8 deletions src/dpp/dave/decryptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ size_t decryptor::decrypt(media_type mediaType,
// Process the incoming frame
// This will check whether it looks like a valid encrypted frame
// and if so it will parse it into its different components
localFrame->ParseFrame(encryptedFrame);
localFrame->parse_frame(encryptedFrame);

// If the frame is not encrypted and we can pass it through, do it
bool canUsePassThrough = allowPassThroughUntil_ > start;
if (!localFrame->IsEncrypted() && canUsePassThrough) {
if (!localFrame->is_encrypted() && canUsePassThrough) {
if (encryptedFrame.data() != frame.data()) {
std::memcpy(frame.data(), encryptedFrame.data(), encryptedFrame.size());
}
Expand All @@ -108,7 +108,7 @@ size_t decryptor::decrypt(media_type mediaType,
}

// If the frame is not encrypted and we can't pass it through, fail
if (!localFrame->IsEncrypted()) {
if (!localFrame->is_encrypted()) {
DISCORD_LOG(LS_INFO)
<< "decrypt failed, frame is not encrypted and pass through is disabled";
stats_[mediaType].decrypt_failure++;
Expand All @@ -129,7 +129,7 @@ size_t decryptor::decrypt(media_type mediaType,
size_t bytesWritten = 0;
if (success) {
stats_[mediaType].decrypt_success++;
bytesWritten = localFrame->ReconstructFrame(frame);
bytesWritten = localFrame->reconstruct_frame(frame);
}
else {
stats_[mediaType].decrypt_failure++;
Expand Down Expand Up @@ -160,12 +160,12 @@ bool decryptor::decrypt_impl(aead_cipher_manager& cipher_manager,
inbound_frame_processor& encryptedFrame,
array_view<uint8_t> frame)
{
auto tag = encryptedFrame.GetTag();
auto truncatedNonce = encryptedFrame.GetTruncatedNonce();
auto tag = encryptedFrame.get_tag();
auto truncatedNonce = encryptedFrame.get_truncated_nonce();

auto authenticatedData = encryptedFrame.GetAuthenticatedData();
auto authenticatedData = encryptedFrame.get_authenticated_data();
auto ciphertext = encryptedFrame.GetCiphertext();
auto plaintext = encryptedFrame.GetPlaintext();
auto plaintext = encryptedFrame.get_plaintext();

// expand the truncated nonce to the full sized one needed for decryption
auto nonceBuffer = std::array<uint8_t, AES_GCM_128_NONCE_BYTES>();
Expand Down
18 changes: 9 additions & 9 deletions src/dpp/dave/encryptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ int encryptor::encrypt(media_type mediaType,
auto frameProcessor = get_or_create_frame_processor();
ScopeExit cleanup([&] { return_frame_processor(std::move(frameProcessor)); });

frameProcessor->ProcessFrame(frame, codec);
frameProcessor->process_frame(frame, codec);

const auto& unencryptedBytes = frameProcessor->GetUnencryptedBytes();
const auto& encryptedBytes = frameProcessor->GetEncryptedBytes();
auto& ciphertextBytes = frameProcessor->GetCiphertextBytes();
const auto& unencryptedBytes = frameProcessor->get_unencrypted_bytes();
const auto& encryptedBytes = frameProcessor->get_encrypted_bytes();
auto& ciphertextBytes = frameProcessor->get_ciphertext_bytes();

const auto& unencryptedRanges = frameProcessor->GetUnencryptedRanges();
const auto& unencryptedRanges = frameProcessor->get_unencrypted_ranges();
auto unencryptedRangesSize = UnencryptedRangesSize(unencryptedRanges);

auto additionalData = make_array_view(unencryptedBytes.data(), unencryptedBytes.size());
Expand Down Expand Up @@ -151,7 +151,7 @@ int encryptor::encrypt(media_type mediaType,
break;
}

auto reconstructedFrameSize = frameProcessor->ReconstructFrame(encryptedFrame);
auto reconstructedFrameSize = frameProcessor->reconstruct_frame(encryptedFrame);
assert(reconstructedFrameSize == frameSize && "Failed to reconstruct frame");

auto nonceSize = Leb128Size(truncatedNonce);
Expand Down Expand Up @@ -262,18 +262,18 @@ Codec encryptor::codec_for_ssrc(uint32_t ssrc)
}
}

std::unique_ptr<OutboundFrameProcessor> encryptor::get_or_create_frame_processor()
std::unique_ptr<outbound_frame_processor> encryptor::get_or_create_frame_processor()
{
std::lock_guard<std::mutex> lock(frameProcessorsMutex_);
if (frameProcessors_.empty()) {
return std::make_unique<OutboundFrameProcessor>();
return std::make_unique<outbound_frame_processor>();
}
auto frameProcessor = std::move(frameProcessors_.back());
frameProcessors_.pop_back();
return frameProcessor;
}

void encryptor::return_frame_processor(std::unique_ptr<OutboundFrameProcessor> frameProcessor)
void encryptor::return_frame_processor(std::unique_ptr<outbound_frame_processor> frameProcessor)
{
std::lock_guard<std::mutex> lock(frameProcessorsMutex_);
frameProcessors_.push_back(std::move(frameProcessor));
Expand Down
6 changes: 3 additions & 3 deletions src/dpp/dave/encryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class encryptor {
};

private:
std::unique_ptr<OutboundFrameProcessor> get_or_create_frame_processor();
void return_frame_processor(std::unique_ptr<OutboundFrameProcessor> frameProcessor);
std::unique_ptr<outbound_frame_processor> get_or_create_frame_processor();
void return_frame_processor(std::unique_ptr<outbound_frame_processor> frameProcessor);

using cryptor_and_nonce = std::pair<std::shared_ptr<cipher_interface>, truncated_sync_nonce>;
cryptor_and_nonce get_next_cryptor_and_nonce();
Expand All @@ -99,7 +99,7 @@ class encryptor {
truncated_sync_nonce truncatedNonce_{0};

std::mutex frameProcessorsMutex_;
std::vector<std::unique_ptr<OutboundFrameProcessor>> frameProcessors_;
std::vector<std::unique_ptr<outbound_frame_processor>> frameProcessors_;

using SsrcCodecPair = std::pair<uint32_t, Codec>;
std::vector<SsrcCodecPair> ssrcCodecPairs_;
Expand Down
Loading

0 comments on commit 9c53286

Please sign in to comment.