h264packetizationhandler.cpp 5.4 KB

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