nalunit.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_NAL_UNIT_H
  19. #define RTC_NAL_UNIT_H
  20. #if RTC_ENABLE_MEDIA
  21. #include "common.hpp"
  22. #include <cassert>
  23. namespace rtc {
  24. #pragma pack(push, 1)
  25. /// Nalu header
  26. struct RTC_CPP_EXPORT NalUnitHeader {
  27. uint8_t _first = 0;
  28. bool forbiddenBit() { return _first >> 7; }
  29. uint8_t nri() { return _first >> 5 & 0x03; }
  30. uint8_t unitType() { return _first & 0x1F; }
  31. void setForbiddenBit(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  32. void setNRI(uint8_t nri) { _first = (_first & 0x9F) | ((nri & 0x03) << 5); }
  33. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  34. };
  35. /// Nalu fragment header
  36. struct RTC_CPP_EXPORT NalUnitFragmentHeader {
  37. uint8_t _first = 0;
  38. bool isStart() { return _first >> 7; }
  39. bool reservedBit6() { return (_first >> 6) & 0x01; }
  40. bool isEnd() { return (_first >> 5) & 0x01; }
  41. uint8_t unitType() { return _first & 0x1F; }
  42. void setStart(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  43. void setEnd(bool isSet) { _first = (_first & 0xBF) | (isSet << 6); }
  44. void setReservedBit6(bool isSet) { _first = (_first & 0xDF) | (isSet << 5); }
  45. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  46. };
  47. #pragma pack(pop)
  48. /// Nal unit
  49. struct RTC_CPP_EXPORT NalUnit : binary {
  50. NalUnit(const NalUnit &unit) = default;
  51. NalUnit(size_t size, bool includingHeader = true) : binary(size + (includingHeader ? 0 : 1)) {}
  52. template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
  53. NalUnit(binary &&data) : binary(std::move(data)) {}
  54. NalUnit() : binary(1) {}
  55. bool forbiddenBit() { return header()->forbiddenBit(); }
  56. uint8_t nri() { return header()->nri(); }
  57. uint8_t unitType() { return header()->unitType(); }
  58. binary payload() {
  59. assert(size() >= 1);
  60. return {begin() + 1, end()};
  61. }
  62. void setForbiddenBit(bool isSet) { header()->setForbiddenBit(isSet); }
  63. void setNRI(uint8_t nri) { header()->setNRI(nri); }
  64. void setUnitType(uint8_t type) { header()->setUnitType(type); }
  65. void setPayload(binary payload) {
  66. assert(size() >= 1);
  67. erase(begin() + 1, end());
  68. insert(end(), payload.begin(), payload.end());
  69. }
  70. protected:
  71. NalUnitHeader *header() {
  72. assert(size() >= 1);
  73. return (NalUnitHeader *)data();
  74. }
  75. };
  76. /// Nal unit fragment A
  77. struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit {
  78. enum class FragmentType { Start, Middle, End };
  79. NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType,
  80. binary data);
  81. static std::vector<shared_ptr<NalUnitFragmentA>> fragmentsFrom(shared_ptr<NalUnit> nalu,
  82. uint16_t maximumFragmentSize);
  83. uint8_t unitType() { return fragmentHeader()->unitType(); }
  84. binary payload() {
  85. assert(size() >= 2);
  86. return {begin() + 2, end()};
  87. }
  88. FragmentType type() {
  89. if (fragmentHeader()->isStart()) {
  90. return FragmentType::Start;
  91. } else if (fragmentHeader()->isEnd()) {
  92. return FragmentType::End;
  93. } else {
  94. return FragmentType::Middle;
  95. }
  96. }
  97. void setUnitType(uint8_t type) { fragmentHeader()->setUnitType(type); }
  98. void setPayload(binary payload) {
  99. assert(size() >= 2);
  100. erase(begin() + 2, end());
  101. insert(end(), payload.begin(), payload.end());
  102. }
  103. void setFragmentType(FragmentType type);
  104. protected:
  105. NalUnitHeader *fragmentIndicator() { return (NalUnitHeader *)data(); }
  106. NalUnitFragmentHeader *fragmentHeader() {
  107. return (NalUnitFragmentHeader *)fragmentIndicator() + 1;
  108. }
  109. const uint8_t nal_type_fu_A = 28;
  110. };
  111. class RTC_CPP_EXPORT NalUnits : public std::vector<shared_ptr<NalUnit>> {
  112. public:
  113. static const uint16_t defaultMaximumFragmentSize =
  114. uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6
  115. std::vector<shared_ptr<binary>> generateFragments(uint16_t maximumFragmentSize);
  116. };
  117. } // namespace rtc
  118. #endif /* RTC_ENABLE_MEDIA */
  119. #endif /* RTC_NAL_UNIT_H */