nalunit.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <cassert>
  13. namespace rtc {
  14. #pragma pack(push, 1)
  15. /// Nalu header
  16. struct RTC_CPP_EXPORT NalUnitHeader {
  17. uint8_t _first = 0;
  18. bool forbiddenBit() { return _first >> 7; }
  19. uint8_t nri() { return _first >> 5 & 0x03; }
  20. uint8_t unitType() { return _first & 0x1F; }
  21. void setForbiddenBit(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  22. void setNRI(uint8_t nri) { _first = (_first & 0x9F) | ((nri & 0x03) << 5); }
  23. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  24. };
  25. /// Nalu fragment header
  26. struct RTC_CPP_EXPORT NalUnitFragmentHeader {
  27. uint8_t _first = 0;
  28. bool isStart() { return _first >> 7; }
  29. bool reservedBit6() { return (_first >> 5) & 0x01; }
  30. bool isEnd() { return (_first >> 6) & 0x01; }
  31. uint8_t unitType() { return _first & 0x1F; }
  32. void setStart(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  33. void setEnd(bool isSet) { _first = (_first & 0xBF) | (isSet << 6); }
  34. void setReservedBit6(bool isSet) { _first = (_first & 0xDF) | (isSet << 5); }
  35. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  36. };
  37. #pragma pack(pop)
  38. /// Nal unit
  39. struct RTC_CPP_EXPORT NalUnit : binary {
  40. NalUnit(const NalUnit &unit) = default;
  41. NalUnit(size_t size, bool includingHeader = true) : binary(size + (includingHeader ? 0 : 1)) {}
  42. template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
  43. NalUnit(binary &&data) : binary(std::move(data)) {}
  44. NalUnit() : binary(1) {}
  45. bool forbiddenBit() { return header()->forbiddenBit(); }
  46. uint8_t nri() { return header()->nri(); }
  47. uint8_t unitType() { return header()->unitType(); }
  48. binary payload() {
  49. assert(size() >= 1);
  50. return {begin() + 1, end()};
  51. }
  52. void setForbiddenBit(bool isSet) { header()->setForbiddenBit(isSet); }
  53. void setNRI(uint8_t nri) { header()->setNRI(nri); }
  54. void setUnitType(uint8_t type) { header()->setUnitType(type); }
  55. void setPayload(binary payload) {
  56. assert(size() >= 1);
  57. erase(begin() + 1, end());
  58. insert(end(), payload.begin(), payload.end());
  59. }
  60. protected:
  61. NalUnitHeader *header() {
  62. assert(size() >= 1);
  63. return (NalUnitHeader *)data();
  64. }
  65. };
  66. /// Nal unit fragment A
  67. struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit {
  68. enum class FragmentType { Start, Middle, End };
  69. NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType,
  70. binary data);
  71. static std::vector<shared_ptr<NalUnitFragmentA>> fragmentsFrom(shared_ptr<NalUnit> nalu,
  72. uint16_t maximumFragmentSize);
  73. uint8_t unitType() { return fragmentHeader()->unitType(); }
  74. binary payload() {
  75. assert(size() >= 2);
  76. return {begin() + 2, end()};
  77. }
  78. FragmentType type() {
  79. if (fragmentHeader()->isStart()) {
  80. return FragmentType::Start;
  81. } else if (fragmentHeader()->isEnd()) {
  82. return FragmentType::End;
  83. } else {
  84. return FragmentType::Middle;
  85. }
  86. }
  87. void setUnitType(uint8_t type) { fragmentHeader()->setUnitType(type); }
  88. void setPayload(binary payload) {
  89. assert(size() >= 2);
  90. erase(begin() + 2, end());
  91. insert(end(), payload.begin(), payload.end());
  92. }
  93. void setFragmentType(FragmentType type);
  94. protected:
  95. NalUnitHeader *fragmentIndicator() { return (NalUnitHeader *)data(); }
  96. NalUnitFragmentHeader *fragmentHeader() {
  97. return (NalUnitFragmentHeader *)fragmentIndicator() + 1;
  98. }
  99. const uint8_t nal_type_fu_A = 28;
  100. };
  101. class RTC_CPP_EXPORT NalUnits : public std::vector<shared_ptr<NalUnit>> {
  102. public:
  103. static const uint16_t defaultMaximumFragmentSize =
  104. uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6
  105. std::vector<shared_ptr<binary>> generateFragments(uint16_t maximumFragmentSize);
  106. };
  107. } // namespace rtc
  108. #endif /* RTC_ENABLE_MEDIA */
  109. #endif /* RTC_NAL_UNIT_H */