h264rtppacketizer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #if RTC_ENABLE_MEDIA
  19. #include "h264rtppacketizer.hpp"
  20. namespace rtc {
  21. typedef enum {
  22. NUSM_noMatch,
  23. NUSM_firstZero,
  24. NUSM_secondZero,
  25. NUSM_thirdZero,
  26. NUSM_shortMatch,
  27. NUSM_longMatch
  28. } NalUnitStartSequenceMatch;
  29. NalUnitStartSequenceMatch StartSequenceMatchSucc(NalUnitStartSequenceMatch match, byte _byte,
  30. H264RtpPacketizer::Separator separator) {
  31. assert(separator != H264RtpPacketizer::Separator::Length);
  32. auto byte = (uint8_t)_byte;
  33. auto detectShort = separator == H264RtpPacketizer::Separator::ShortStartSequence ||
  34. separator == H264RtpPacketizer::Separator::StartSequence;
  35. auto detectLong = separator == H264RtpPacketizer::Separator::LongStartSequence ||
  36. separator == H264RtpPacketizer::Separator::StartSequence;
  37. switch (match) {
  38. case NUSM_noMatch:
  39. if (byte == 0x00) {
  40. return NUSM_firstZero;
  41. }
  42. break;
  43. case NUSM_firstZero:
  44. if (byte == 0x00) {
  45. return NUSM_secondZero;
  46. }
  47. break;
  48. case NUSM_secondZero:
  49. if (byte == 0x00 && detectLong) {
  50. return NUSM_thirdZero;
  51. } else if (byte == 0x01 && detectShort) {
  52. return NUSM_shortMatch;
  53. }
  54. break;
  55. case NUSM_thirdZero:
  56. if (byte == 0x01 && detectLong) {
  57. return NUSM_longMatch;
  58. }
  59. break;
  60. case NUSM_shortMatch:
  61. return NUSM_shortMatch;
  62. case NUSM_longMatch:
  63. return NUSM_longMatch;
  64. }
  65. return NUSM_noMatch;
  66. }
  67. shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
  68. auto nalus = std::make_shared<NalUnits>();
  69. if (separator == Separator::Length) {
  70. unsigned long long index = 0;
  71. while (index < message->size()) {
  72. assert(index + 4 < message->size());
  73. if (index + 4 >= message->size()) {
  74. LOG_WARNING << "Invalid NAL Unit data (incomplete length), ignoring!";
  75. break;
  76. }
  77. auto lengthPtr = (uint32_t *)(message->data() + index);
  78. uint32_t length = ntohl(*lengthPtr);
  79. auto naluStartIndex = index + 4;
  80. auto naluEndIndex = naluStartIndex + length;
  81. assert(naluEndIndex <= message->size());
  82. if (naluEndIndex > message->size()) {
  83. LOG_WARNING << "Invalid NAL Unit data (incomplete unit), ignoring!";
  84. break;
  85. }
  86. auto begin = message->begin() + naluStartIndex;
  87. auto end = message->begin() + naluEndIndex;
  88. nalus->push_back(std::make_shared<NalUnit>(begin, end));
  89. index = naluEndIndex;
  90. }
  91. } else {
  92. NalUnitStartSequenceMatch match = NUSM_noMatch;
  93. unsigned long long index = 0;
  94. while (index < message->size()) {
  95. match = StartSequenceMatchSucc(match, (*message)[index++], separator);
  96. if (match == NUSM_longMatch || match == NUSM_shortMatch) {
  97. match = NUSM_noMatch;
  98. break;
  99. }
  100. }
  101. unsigned long long naluStartIndex = index;
  102. while (index < message->size()) {
  103. match = StartSequenceMatchSucc(match, (*message)[index], separator);
  104. if (match == NUSM_longMatch || match == NUSM_shortMatch) {
  105. auto sequenceLength = match == NUSM_longMatch ? 4 : 3;
  106. unsigned long long naluEndIndex = index - sequenceLength;
  107. match = NUSM_noMatch;
  108. auto begin = message->begin() + naluStartIndex;
  109. auto end = message->begin() + naluEndIndex + 1;
  110. nalus->push_back(std::make_shared<NalUnit>(begin, end));
  111. naluStartIndex = index + 1;
  112. }
  113. index++;
  114. }
  115. auto begin = message->begin() + naluStartIndex;
  116. auto end = message->end();
  117. nalus->push_back(std::make_shared<NalUnit>(begin, end));
  118. }
  119. return nalus;
  120. }
  121. H264RtpPacketizer::H264RtpPacketizer(std::shared_ptr<RtpPacketizationConfig> rtpConfig,
  122. uint16_t maximumFragmentSize)
  123. : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize), separator(Separator::Length) {}
  124. H264RtpPacketizer::H264RtpPacketizer(H264RtpPacketizer::Separator separator, std::shared_ptr<RtpPacketizationConfig> rtpConfig,
  125. uint16_t maximumFragmentSize)
  126. : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize), separator(separator) {}
  127. ChainedOutgoingProduct H264RtpPacketizer::processOutgoingBinaryMessage(ChainedMessagesProduct messages, message_ptr control) {
  128. ChainedMessagesProduct packets = std::make_shared<std::vector<binary_ptr>>();
  129. for (auto message: *messages) {
  130. auto nalus = splitMessage(message);
  131. auto fragments = nalus->generateFragments(maximumFragmentSize);
  132. if (fragments.size() == 0) {
  133. return ChainedOutgoingProduct();
  134. }
  135. unsigned i = 0;
  136. for (; i < fragments.size() - 1; i++) {
  137. packets->push_back(packetize(fragments[i], false));
  138. }
  139. packets->push_back(packetize(fragments[i], true));
  140. }
  141. return {packets, control};
  142. }
  143. } // namespace rtc
  144. #endif /* RTC_ENABLE_MEDIA */