nalunit.hpp 4.4 KB

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