Skip to content

Commit

Permalink
H265RtpDepacketizer: Make start sequence configurable
Browse files Browse the repository at this point in the history
Per PR review feedback, add a parameter to the constructor for
configuring the start sequence to use when writing NALUs.
  • Loading branch information
edmonds committed Apr 17, 2024
1 parent 014ce5b commit 710023d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
7 changes: 6 additions & 1 deletion include/rtc/h265rtpdepacketizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#if RTC_ENABLE_MEDIA

#include "common.hpp"
#include "h265nalunit.hpp"
#include "mediahandler.hpp"
#include "message.hpp"
#include "rtp.hpp"
Expand All @@ -25,14 +26,18 @@ namespace rtc {
/// RTP depacketization for H265
class RTC_CPP_EXPORT H265RtpDepacketizer : public MediaHandler {
public:
H265RtpDepacketizer() = default;
using Separator = NalUnit::Separator;

H265RtpDepacketizer(Separator separator = Separator::LongStartSequence);
virtual ~H265RtpDepacketizer() = default;

void incoming(message_vector &messages, const message_callback &send) override;

private:
std::vector<message_ptr> mRtpBuffer;
const NalUnit::Separator separator;

void addSeparator(binary& accessUnit);
message_vector buildFrames(message_vector::iterator firstPkt, message_vector::iterator lastPkt,
uint32_t timestamp);
};
Expand Down
43 changes: 37 additions & 6 deletions src/h265rtpdepacketizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,44 @@

namespace rtc {

const binary naluStartCode = {byte{0}, byte{0}, byte{0}, byte{1}};
const binary naluLongStartCode = {byte{0}, byte{0}, byte{0}, byte{1}};
const binary naluShortStartCode = {byte{0}, byte{0}, byte{1}};

const uint8_t naluTypeAP = 48;
const uint8_t naluTypeFU = 49;

H265RtpDepacketizer::H265RtpDepacketizer(Separator separator) : separator(separator) {
switch (separator) {
case Separator::StartSequence: [[fallthrough]];
case Separator::LongStartSequence: [[fallthrough]];
case Separator::ShortStartSequence:
break;
case Separator::Length: [[fallthrough]];
default:
throw std::invalid_argument("Invalid separator");
}
}

void H265RtpDepacketizer::addSeparator(binary& accessUnit)
{
switch (separator) {
case Separator::StartSequence: [[fallthrough]];
case Separator::LongStartSequence:
accessUnit.insert(accessUnit.end(),
naluLongStartCode.begin(),
naluLongStartCode.end());
break;
case Separator::ShortStartSequence:
accessUnit.insert(accessUnit.end(),
naluShortStartCode.begin(),
naluShortStartCode.end());
break;
case Separator::Length: [[fallthrough]];
default:
throw std::invalid_argument("Invalid separator");
}
}

message_vector H265RtpDepacketizer::buildFrames(message_vector::iterator begin,
message_vector::iterator end, uint32_t timestamp) {
message_vector out = {};
Expand Down Expand Up @@ -52,8 +85,7 @@ message_vector H265RtpDepacketizer::buildFrames(message_vector::iterator begin,
std::to_integer<uint8_t>(pkt->at(rtpHeaderSize + sizeof(H265NalUnitHeader)))};

if (nFrags++ == 0) {
accessUnit.insert(accessUnit.end(), naluStartCode.begin(), naluStartCode.end());

addSeparator(accessUnit);
nalUnitHeader.setUnitType(nalUnitFragmentHeader.unitType());
accessUnit.emplace_back(byte(nalUnitHeader._first));
accessUnit.emplace_back(byte(nalUnitHeader._second));
Expand All @@ -76,8 +108,7 @@ message_vector H265RtpDepacketizer::buildFrames(message_vector::iterator begin,
throw std::runtime_error("H265 AP declared size is larger than buffer");
}

accessUnit.insert(accessUnit.end(), naluStartCode.begin(), naluStartCode.end());

addSeparator(accessUnit);
accessUnit.insert(accessUnit.end(), pkt->begin() + currOffset,
pkt->begin() + currOffset + naluSize);

Expand All @@ -86,7 +117,7 @@ message_vector H265RtpDepacketizer::buildFrames(message_vector::iterator begin,
} else if (nalUnitHeader.unitType() < naluTypeAP) {
// "NAL units with NAL unit type values in the range of 0 to 47, inclusive, may be
// passed to the decoder."
accessUnit.insert(accessUnit.end(), naluStartCode.begin(), naluStartCode.end());
addSeparator(accessUnit);
accessUnit.insert(accessUnit.end(), pkt->begin() + rtpHeaderSize, pkt->end());
} else {
// "NAL-unit-like structures with NAL unit type values in the range of 48 to 63,
Expand Down

0 comments on commit 710023d

Please sign in to comment.