Zita Liao преди 1 година
родител
ревизия
d8f85150c1
променени са 7 файла, в които са добавени 93 реда и са изтрити 154 реда
  1. 2 10
      include/rtc/h264rtppacketizer.hpp
  2. 7 16
      include/rtc/h265nalunit.hpp
  3. 2 10
      include/rtc/h265rtppacketizer.hpp
  4. 70 2
      include/rtc/nalunit.hpp
  5. 2 2
      src/capi.cpp
  6. 5 57
      src/h264rtppacketizer.cpp
  7. 5 57
      src/h265rtppacketizer.cpp

+ 2 - 10
include/rtc/h264rtppacketizer.hpp

@@ -27,15 +27,7 @@ public:
 	/// Default clock rate for H264 in RTP
 	inline static const uint32_t defaultClockRate = 90 * 1000;
 
-	/// NAL unit separator
-	enum class Separator {
-		Length = RTC_NAL_SEPARATOR_LENGTH, // first 4 bytes are NAL unit length
-		LongStartSequence = RTC_NAL_SEPARATOR_LONG_START_SEQUENCE,   // 0x00, 0x00, 0x00, 0x01
-		ShortStartSequence = RTC_NAL_SEPARATOR_SHORT_START_SEQUENCE, // 0x00, 0x00, 0x01
-		StartSequence = RTC_NAL_SEPARATOR_START_SEQUENCE, // LongStartSequence or ShortStartSequence
-	};
-
-	H264RtpPacketizer(H264RtpPacketizer::Separator separator,
+	H264RtpPacketizer(NalUnit::Separator separator,
 	                  shared_ptr<RtpPacketizationConfig> rtpConfig,
 	                  uint16_t maximumFragmentSize = NalUnits::defaultMaximumFragmentSize);
 
@@ -51,7 +43,7 @@ public:
 	                                                    message_ptr control) override;
 
 private:
-	const Separator separator;
+	const NalUnit::Separator separator;
 };
 
 } // namespace rtc

+ 7 - 16
include/rtc/h265nalunit.hpp

@@ -12,6 +12,7 @@
 #if RTC_ENABLE_MEDIA
 
 #include "common.hpp"
+#include "nalunit.hpp"
 
 #include <cassert>
 
@@ -19,7 +20,6 @@ namespace rtc {
 
 #pragma pack(push, 1)
 
-#define H265_NAL_HEADER_SIZE 2
 #define H265_FU_HEADER_SIZE 1
 /// Nalu header
 struct RTC_CPP_EXPORT H265NalUnitHeader {
@@ -31,16 +31,6 @@ struct RTC_CPP_EXPORT H265NalUnitHeader {
 * nuh_temporal_id_plus1	u(3)
 }
 */
-	//H265NalUnitHeader(uint16_t nalHeader)
-	//	: _first((nalHeader & 0xff00) >> 8)
-	//	, _second(nalHeader & 0xff)
-	//{}
-
-	//H265NalUnitHeader(uint8_t nalHeaderHi, unit8_t nalHeaderLow)
-	//	: _first(nalHeaderHi)
-	//	, _second(nalHeaderLow)
-	//{}
-
 	uint8_t _first = 0; // high byte of header
 	uint8_t _second = 0; // low byte of header
 
@@ -80,12 +70,13 @@ struct RTC_CPP_EXPORT H265NalUnitFragmentHeader {
 #pragma pack(pop)
 
 /// Nal unit
-struct RTC_CPP_EXPORT H265NalUnit : binary {
+struct RTC_CPP_EXPORT H265NalUnit : NalUnit {
 	H265NalUnit(const H265NalUnit &unit) = default;
-	H265NalUnit(size_t size, bool includingHeader = true) : binary(size + (includingHeader ? 0 : H265_NAL_HEADER_SIZE)) {}
-	H265NalUnit(binary &&data) : binary(std::move(data)) {}
-	H265NalUnit() : binary(H265_NAL_HEADER_SIZE) {}
-	template <typename Iterator> H265NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
+	H265NalUnit(size_t size, bool includingHeader = true) : NalUnit(size, includingHeader, NalUnit::Type::H265) {}
+	H265NalUnit(binary &&data) : NalUnit(std::move(data)) {}
+	H265NalUnit() : NalUnit(NalUnit::Type::H265) {}
+
+	template <typename Iterator> H265NalUnit(Iterator begin_, Iterator end_) : NalUnit(begin_, end_) {}
 
 	bool forbiddenBit() const { return header()->forbiddenBit(); }
 	uint8_t unitType() const { return header()->unitType(); }

+ 2 - 10
include/rtc/h265rtppacketizer.hpp

@@ -27,15 +27,7 @@ public:
 	/// Default clock rate for H265 in RTP
 	inline static const uint32_t defaultClockRate = 90 * 1000;
 
-	/// NAL unit separator
-	enum class Separator {
-		Length = RTC_NAL_SEPARATOR_LENGTH, // first 4 bytes are NAL unit length
-		LongStartSequence = RTC_NAL_SEPARATOR_LONG_START_SEQUENCE,   // 0x00, 0x00, 0x00, 0x01
-		ShortStartSequence = RTC_NAL_SEPARATOR_SHORT_START_SEQUENCE, // 0x00, 0x00, 0x01
-		StartSequence = RTC_NAL_SEPARATOR_START_SEQUENCE, // LongStartSequence or ShortStartSequence
-	};
-
-	H265RtpPacketizer(H265RtpPacketizer::Separator separator,
+	H265RtpPacketizer(NalUnit::Separator separator,
 	                  shared_ptr<RtpPacketizationConfig> rtpConfig,
 	                  uint16_t maximumFragmentSize = H265NalUnits::defaultMaximumFragmentSize);
 
@@ -51,7 +43,7 @@ public:
 	                                                    message_ptr control) override;
 
 private:
-	const Separator separator;
+	const NalUnit::Separator separator;
 };
 
 } // namespace rtc

+ 70 - 2
include/rtc/nalunit.hpp

@@ -49,12 +49,29 @@ struct RTC_CPP_EXPORT NalUnitFragmentHeader {
 
 #pragma pack(pop)
 
+typedef enum {
+	NUSM_noMatch,
+	NUSM_firstZero,
+	NUSM_secondZero,
+	NUSM_thirdZero,
+	NUSM_shortMatch,
+	NUSM_longMatch
+} NalUnitStartSequenceMatch;
+
+static const size_t H264_NAL_HEADER_SIZE = 1;
+static const size_t H265_NAL_HEADER_SIZE = 2;
 /// Nal unit
 struct RTC_CPP_EXPORT NalUnit : binary {
+	typedef enum {
+		H264,
+		H265
+	} Type;
+
 	NalUnit(const NalUnit &unit) = default;
-	NalUnit(size_t size, bool includingHeader = true) : binary(size + (includingHeader ? 0 : 1)) {}
+	NalUnit(size_t size, bool includingHeader = true, Type type = H264)
+		: binary(size + (includingHeader ? 0 : (type == H264? H264_NAL_HEADER_SIZE: H265_NAL_HEADER_SIZE))) {}
 	NalUnit(binary &&data) : binary(std::move(data)) {}
-	NalUnit() : binary(1) {}
+	NalUnit(Type type = H264) : binary( type == H264? H264_NAL_HEADER_SIZE: H265_NAL_HEADER_SIZE) {}
 	template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
 
 	bool forbiddenBit() const { return header()->forbiddenBit(); }
@@ -76,6 +93,57 @@ struct RTC_CPP_EXPORT NalUnit : binary {
 		insert(end(), payload.begin(), payload.end());
 	}
 
+	/// NAL unit separator
+	enum class Separator {
+		Length = RTC_NAL_SEPARATOR_LENGTH, // first 4 bytes are NAL unit length
+		LongStartSequence = RTC_NAL_SEPARATOR_LONG_START_SEQUENCE,   // 0x00, 0x00, 0x00, 0x01
+		ShortStartSequence = RTC_NAL_SEPARATOR_SHORT_START_SEQUENCE, // 0x00, 0x00, 0x01
+		StartSequence = RTC_NAL_SEPARATOR_START_SEQUENCE, // LongStartSequence or ShortStartSequence
+	};
+
+	static NalUnitStartSequenceMatch StartSequenceMatchSucc(NalUnitStartSequenceMatch match, std::byte _byte, Separator separator)
+	{
+		assert(separator != Separator::Length);
+		auto byte = (uint8_t)_byte;
+		auto detectShort = separator == Separator::ShortStartSequence ||
+		                   separator == Separator::StartSequence;
+		auto detectLong = separator == Separator::LongStartSequence ||
+		                  separator == Separator::StartSequence;
+		switch (match) {
+		case NUSM_noMatch:
+			if (byte == 0x00) {
+				return NUSM_firstZero;
+			}
+			break;
+		case NUSM_firstZero:
+			if (byte == 0x00) {
+				return NUSM_secondZero;
+			}
+			break;
+		case NUSM_secondZero:
+			if (byte == 0x00 && detectLong) {
+				return NUSM_thirdZero;
+			} else if (byte == 0x00 && detectShort) {
+				return NUSM_secondZero;
+			} else if (byte == 0x01 && detectShort) {
+				return NUSM_shortMatch;
+			}
+			break;
+		case NUSM_thirdZero:
+			if (byte == 0x00 && detectLong) {
+				return NUSM_thirdZero;
+			} else if (byte == 0x01 && detectLong) {
+				return NUSM_longMatch;
+			}
+			break;
+		case NUSM_shortMatch:
+			return NUSM_shortMatch;
+		case NUSM_longMatch:
+			return NUSM_longMatch;
+		}
+		return NUSM_noMatch;
+	}
+
 protected:
 	const NalUnitHeader *header() const {
 		assert(size() >= 1);

+ 2 - 2
src/capi.cpp

@@ -1203,7 +1203,7 @@ int rtcSetH264PacketizationHandler(int tr, const rtcPacketizationHandlerInit *in
 		auto maxFragmentSize = init && init->maxFragmentSize ? init->maxFragmentSize
 		                                                     : RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE;
 		auto packetizer = std::make_shared<H264RtpPacketizer>(
-		    static_cast<rtc::H264RtpPacketizer::Separator>(nalSeparator), rtpConfig,
+		    static_cast<rtc::NalUnit::Separator>(nalSeparator), rtpConfig,
 		    maxFragmentSize);
 		// create H264 handler
 		auto h264Handler = std::make_shared<H264PacketizationHandler>(packetizer);
@@ -1225,7 +1225,7 @@ int rtcSetH265PacketizationHandler(int tr, const rtcPacketizationHandlerInit *in
 		auto maxFragmentSize = init && init->maxFragmentSize ? init->maxFragmentSize
 		                                                     : RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE;
 		auto packetizer = std::make_shared<H265RtpPacketizer>(
-		    static_cast<rtc::H265RtpPacketizer::Separator>(nalSeparator), rtpConfig,
+		    static_cast<rtc::NalUnit::Separator>(nalSeparator), rtpConfig,
 		    maxFragmentSize);
 		// create H265 handler
 		auto h265Handler = std::make_shared<H265PacketizationHandler>(packetizer);

+ 5 - 57
src/h264rtppacketizer.cpp

@@ -22,61 +22,9 @@
 
 namespace rtc {
 
-typedef enum {
-	NUSM_noMatch,
-	NUSM_firstZero,
-	NUSM_secondZero,
-	NUSM_thirdZero,
-	NUSM_shortMatch,
-	NUSM_longMatch
-} NalUnitStartSequenceMatch;
-
-NalUnitStartSequenceMatch StartSequenceMatchSucc(NalUnitStartSequenceMatch match, byte _byte,
-                                                 H264RtpPacketizer::Separator separator) {
-	assert(separator != H264RtpPacketizer::Separator::Length);
-	auto byte = (uint8_t)_byte;
-	auto detectShort = separator == H264RtpPacketizer::Separator::ShortStartSequence ||
-	                   separator == H264RtpPacketizer::Separator::StartSequence;
-	auto detectLong = separator == H264RtpPacketizer::Separator::LongStartSequence ||
-	                  separator == H264RtpPacketizer::Separator::StartSequence;
-	switch (match) {
-	case NUSM_noMatch:
-		if (byte == 0x00) {
-			return NUSM_firstZero;
-		}
-		break;
-	case NUSM_firstZero:
-		if (byte == 0x00) {
-			return NUSM_secondZero;
-		}
-		break;
-	case NUSM_secondZero:
-		if (byte == 0x00 && detectLong) {
-			return NUSM_thirdZero;
-		} else if (byte == 0x00 && detectShort) {
-			return NUSM_secondZero;
-		} else if (byte == 0x01 && detectShort) {
-			return NUSM_shortMatch;
-		}
-		break;
-	case NUSM_thirdZero:
-		if (byte == 0x00 && detectLong) {
-			return NUSM_thirdZero;
-		} else if (byte == 0x01 && detectLong) {
-			return NUSM_longMatch;
-		}
-		break;
-	case NUSM_shortMatch:
-		return NUSM_shortMatch;
-	case NUSM_longMatch:
-		return NUSM_longMatch;
-	}
-	return NUSM_noMatch;
-}
-
 shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
 	auto nalus = std::make_shared<NalUnits>();
-	if (separator == Separator::Length) {
+	if (separator == NalUnit::Separator::Length) {
 		size_t index = 0;
 		while (index < message->size()) {
 			assert(index + 4 < message->size());
@@ -103,7 +51,7 @@ shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
 		NalUnitStartSequenceMatch match = NUSM_noMatch;
 		size_t index = 0;
 		while (index < message->size()) {
-			match = StartSequenceMatchSucc(match, (*message)[index++], separator);
+			match = NalUnit::StartSequenceMatchSucc(match, (*message)[index++], separator);
 			if (match == NUSM_longMatch || match == NUSM_shortMatch) {
 				match = NUSM_noMatch;
 				break;
@@ -113,7 +61,7 @@ shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
 		size_t naluStartIndex = index;
 
 		while (index < message->size()) {
-			match = StartSequenceMatchSucc(match, (*message)[index], separator);
+			match = NalUnit::StartSequenceMatchSucc(match, (*message)[index], separator);
 			if (match == NUSM_longMatch || match == NUSM_shortMatch) {
 				auto sequenceLength = match == NUSM_longMatch ? 4 : 3;
 				size_t naluEndIndex = index - sequenceLength;
@@ -135,9 +83,9 @@ shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
 H264RtpPacketizer::H264RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
                                      uint16_t maximumFragmentSize)
     : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize),
-      separator(Separator::Length) {}
+      separator(NalUnit::Separator::Length) {}
 
-H264RtpPacketizer::H264RtpPacketizer(H264RtpPacketizer::Separator separator,
+H264RtpPacketizer::H264RtpPacketizer(NalUnit::Separator separator,
                                      shared_ptr<RtpPacketizationConfig> rtpConfig,
                                      uint16_t maximumFragmentSize)
     : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize),

+ 5 - 57
src/h265rtppacketizer.cpp

@@ -22,61 +22,9 @@
 
 namespace rtc {
 
-typedef enum {
-	NUSM_noMatch,
-	NUSM_firstZero,
-	NUSM_secondZero,
-	NUSM_thirdZero,
-	NUSM_shortMatch,
-	NUSM_longMatch
-} NalUnitStartSequenceMatch;
-
-NalUnitStartSequenceMatch StartSequenceMatchSucc(NalUnitStartSequenceMatch match, byte _byte,
-                                                 H265RtpPacketizer::Separator separator) {
-	assert(separator != H265RtpPacketizer::Separator::Length);
-	auto byte = (uint8_t)_byte;
-	auto detectShort = separator == H265RtpPacketizer::Separator::ShortStartSequence ||
-	                   separator == H265RtpPacketizer::Separator::StartSequence;
-	auto detectLong = separator == H265RtpPacketizer::Separator::LongStartSequence ||
-	                  separator == H265RtpPacketizer::Separator::StartSequence;
-	switch (match) {
-	case NUSM_noMatch:
-		if (byte == 0x00) {
-			return NUSM_firstZero;
-		}
-		break;
-	case NUSM_firstZero:
-		if (byte == 0x00) {
-			return NUSM_secondZero;
-		}
-		break;
-	case NUSM_secondZero:
-		if (byte == 0x00 && detectLong) {
-			return NUSM_thirdZero;
-		} else if (byte == 0x00 && detectShort) {
-			return NUSM_secondZero;
-		} else if (byte == 0x01 && detectShort) {
-			return NUSM_shortMatch;
-		}
-		break;
-	case NUSM_thirdZero:
-		if (byte == 0x00 && detectLong) {
-			return NUSM_thirdZero;
-		} else if (byte == 0x01 && detectLong) {
-			return NUSM_longMatch;
-		}
-		break;
-	case NUSM_shortMatch:
-		return NUSM_shortMatch;
-	case NUSM_longMatch:
-		return NUSM_longMatch;
-	}
-	return NUSM_noMatch;
-}
-
 shared_ptr<H265NalUnits> H265RtpPacketizer::splitMessage(binary_ptr message) {
 	auto nalus = std::make_shared<H265NalUnits>();
-	if (separator == Separator::Length) {
+	if (separator == NalUnit::Separator::Length) {
 		size_t index = 0;
 		while (index < message->size()) {
 			assert(index + 4 < message->size());
@@ -103,7 +51,7 @@ shared_ptr<H265NalUnits> H265RtpPacketizer::splitMessage(binary_ptr message) {
 		NalUnitStartSequenceMatch match = NUSM_noMatch;
 		size_t index = 0;
 		while (index < message->size()) {
-			match = StartSequenceMatchSucc(match, (*message)[index++], separator);
+			match = NalUnit::StartSequenceMatchSucc(match, (*message)[index++], separator);
 			if (match == NUSM_longMatch || match == NUSM_shortMatch) {
 				match = NUSM_noMatch;
 				break;
@@ -113,7 +61,7 @@ shared_ptr<H265NalUnits> H265RtpPacketizer::splitMessage(binary_ptr message) {
 		size_t naluStartIndex = index;
 
 		while (index < message->size()) {
-			match = StartSequenceMatchSucc(match, (*message)[index], separator);
+			match = NalUnit::StartSequenceMatchSucc(match, (*message)[index], separator);
 			if (match == NUSM_longMatch || match == NUSM_shortMatch) {
 				auto sequenceLength = match == NUSM_longMatch ? 4 : 3;
 				size_t naluEndIndex = index - sequenceLength;
@@ -135,9 +83,9 @@ shared_ptr<H265NalUnits> H265RtpPacketizer::splitMessage(binary_ptr message) {
 H265RtpPacketizer::H265RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
                                      uint16_t maximumFragmentSize)
     : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize),
-      separator(Separator::Length) {}
+      separator(NalUnit::Separator::Length) {}
 
-H265RtpPacketizer::H265RtpPacketizer(H265RtpPacketizer::Separator separator,
+H265RtpPacketizer::H265RtpPacketizer(NalUnit::Separator separator,
                                      shared_ptr<RtpPacketizationConfig> rtpConfig,
                                      uint16_t maximumFragmentSize)
     : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize),