nalunit.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_NAL_UNIT_H
  9. #define RTC_NAL_UNIT_H
  10. #if RTC_ENABLE_MEDIA
  11. #include "common.hpp"
  12. #include <vector>
  13. #include <cassert>
  14. namespace rtc {
  15. #pragma pack(push, 1)
  16. /// Nalu header
  17. struct RTC_CPP_EXPORT NalUnitHeader {
  18. uint8_t _first = 0;
  19. bool forbiddenBit() const { return _first >> 7; }
  20. uint8_t nri() const { return _first >> 5 & 0x03; }
  21. uint8_t idc() const { return _first & 0x60; }
  22. uint8_t unitType() const { return _first & 0x1F; }
  23. void setForbiddenBit(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  24. void setNRI(uint8_t nri) { _first = (_first & 0x9F) | ((nri & 0x03) << 5); }
  25. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  26. };
  27. /// Nalu fragment header
  28. struct RTC_CPP_EXPORT NalUnitFragmentHeader {
  29. uint8_t _first = 0;
  30. bool isStart() const { return _first >> 7; }
  31. bool reservedBit6() const { return (_first >> 5) & 0x01; }
  32. bool isEnd() const { return (_first >> 6) & 0x01; }
  33. uint8_t unitType() const { return _first & 0x1F; }
  34. void setStart(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  35. void setEnd(bool isSet) { _first = (_first & 0xBF) | (isSet << 6); }
  36. void setReservedBit6(bool isSet) { _first = (_first & 0xDF) | (isSet << 5); }
  37. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  38. };
  39. #pragma pack(pop)
  40. enum NalUnitStartSequenceMatch {
  41. NUSM_noMatch,
  42. NUSM_firstZero,
  43. NUSM_secondZero,
  44. NUSM_thirdZero,
  45. NUSM_shortMatch,
  46. NUSM_longMatch
  47. };
  48. static const size_t H264_NAL_HEADER_SIZE = 1;
  49. static const size_t H265_NAL_HEADER_SIZE = 2;
  50. struct NalUnitFragmentA;
  51. /// NAL unit
  52. struct RTC_CPP_EXPORT NalUnit : binary {
  53. static std::vector<binary> GenerateFragments(const std::vector<NalUnit> &nalus,
  54. size_t maxFragmentSize);
  55. enum class Separator {
  56. Length = RTC_NAL_SEPARATOR_LENGTH, // first 4 bytes are NAL unit length
  57. LongStartSequence = RTC_NAL_SEPARATOR_LONG_START_SEQUENCE, // 0x00, 0x00, 0x00, 0x01
  58. ShortStartSequence = RTC_NAL_SEPARATOR_SHORT_START_SEQUENCE, // 0x00, 0x00, 0x01
  59. StartSequence = RTC_NAL_SEPARATOR_START_SEQUENCE, // LongStartSequence or ShortStartSequence
  60. };
  61. static NalUnitStartSequenceMatch StartSequenceMatchSucc(NalUnitStartSequenceMatch match,
  62. std::byte _byte, Separator separator);
  63. enum class Type { H264, H265 };
  64. NalUnit(const NalUnit &unit) = default;
  65. NalUnit(size_t size, bool includingHeader = true, Type type = Type::H264)
  66. : binary(size + (includingHeader ? 0
  67. : (type == Type::H264 ? H264_NAL_HEADER_SIZE
  68. : H265_NAL_HEADER_SIZE))) {}
  69. NalUnit(binary &&data) : binary(std::move(data)) {}
  70. NalUnit(Type type = Type::H264)
  71. : binary(type == Type::H264 ? H264_NAL_HEADER_SIZE : H265_NAL_HEADER_SIZE) {}
  72. template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
  73. bool forbiddenBit() const { return header()->forbiddenBit(); }
  74. uint8_t nri() const { return header()->nri(); }
  75. uint8_t unitType() const { return header()->unitType(); }
  76. binary payload() const {
  77. assert(size() >= 1);
  78. return {begin() + 1, end()};
  79. }
  80. void setForbiddenBit(bool isSet) { header()->setForbiddenBit(isSet); }
  81. void setNRI(uint8_t nri) { header()->setNRI(nri); }
  82. void setUnitType(uint8_t type) { header()->setUnitType(type); }
  83. void setPayload(binary payload) {
  84. assert(size() >= 1);
  85. erase(begin() + 1, end());
  86. insert(end(), payload.begin(), payload.end());
  87. }
  88. std::vector<NalUnitFragmentA> generateFragments(size_t maxFragmentSize) const;
  89. protected:
  90. const NalUnitHeader *header() const {
  91. assert(size() >= 1);
  92. return reinterpret_cast<const NalUnitHeader *>(data());
  93. }
  94. NalUnitHeader *header() {
  95. assert(size() >= 1);
  96. return reinterpret_cast<NalUnitHeader *>(data());
  97. }
  98. };
  99. /// Nal unit fragment A
  100. struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit {
  101. // For backward compatibility, do not use
  102. [[deprecated]] static std::vector<shared_ptr<NalUnitFragmentA>>
  103. fragmentsFrom(shared_ptr<NalUnit> nalu, uint16_t maxFragmentSize);
  104. enum class FragmentType { Start, Middle, End };
  105. NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType,
  106. binary data);
  107. uint8_t unitType() const { return fragmentHeader()->unitType(); }
  108. binary payload() const {
  109. assert(size() >= 2);
  110. return {begin() + 2, end()};
  111. }
  112. FragmentType type() const {
  113. if (fragmentHeader()->isStart()) {
  114. return FragmentType::Start;
  115. } else if (fragmentHeader()->isEnd()) {
  116. return FragmentType::End;
  117. } else {
  118. return FragmentType::Middle;
  119. }
  120. }
  121. void setUnitType(uint8_t type) { fragmentHeader()->setUnitType(type); }
  122. void setPayload(binary payload) {
  123. assert(size() >= 2);
  124. erase(begin() + 2, end());
  125. insert(end(), payload.begin(), payload.end());
  126. }
  127. void setFragmentType(FragmentType type);
  128. protected:
  129. const uint8_t nal_type_fu_A = 28;
  130. NalUnitHeader *fragmentIndicator() { return reinterpret_cast<NalUnitHeader *>(data()); }
  131. const NalUnitHeader *fragmentIndicator() const {
  132. return reinterpret_cast<const NalUnitHeader *>(data());
  133. }
  134. NalUnitFragmentHeader *fragmentHeader() {
  135. return reinterpret_cast<NalUnitFragmentHeader *>(fragmentIndicator() + 1);
  136. }
  137. const NalUnitFragmentHeader *fragmentHeader() const {
  138. return reinterpret_cast<const NalUnitFragmentHeader *>(fragmentIndicator() + 1);
  139. }
  140. };
  141. // For backward compatibility, do not use
  142. class [[deprecated]] RTC_CPP_EXPORT NalUnits : public std::vector<shared_ptr<NalUnit>> {
  143. public:
  144. static const uint16_t defaultMaximumFragmentSize =
  145. uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6
  146. std::vector<shared_ptr<binary>> generateFragments(uint16_t maxFragmentSize);
  147. };
  148. } // namespace rtc
  149. #endif /* RTC_ENABLE_MEDIA */
  150. #endif /* RTC_NAL_UNIT_H */