h264rtppacketizer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #if RTC_ENABLE_MEDIA
  9. #include "h264rtppacketizer.hpp"
  10. #include "impl/internals.hpp"
  11. #include <cassert>
  12. #ifdef _WIN32
  13. #include <winsock2.h>
  14. #else
  15. #include <arpa/inet.h>
  16. #endif
  17. namespace rtc {
  18. shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
  19. auto nalus = std::make_shared<NalUnits>();
  20. if (separator == Separator::Length) {
  21. size_t index = 0;
  22. while (index < message->size()) {
  23. assert(index + 4 < message->size());
  24. if (index + 4 >= message->size()) {
  25. LOG_WARNING << "Invalid NAL Unit data (incomplete length), ignoring!";
  26. break;
  27. }
  28. auto lengthPtr = (uint32_t *)(message->data() + index);
  29. uint32_t length = ntohl(*lengthPtr);
  30. auto naluStartIndex = index + 4;
  31. auto naluEndIndex = naluStartIndex + length;
  32. assert(naluEndIndex <= message->size());
  33. if (naluEndIndex > message->size()) {
  34. LOG_WARNING << "Invalid NAL Unit data (incomplete unit), ignoring!";
  35. break;
  36. }
  37. auto begin = message->begin() + naluStartIndex;
  38. auto end = message->begin() + naluEndIndex;
  39. nalus->push_back(std::make_shared<NalUnit>(begin, end));
  40. index = naluEndIndex;
  41. }
  42. } else {
  43. NalUnitStartSequenceMatch match = NUSM_noMatch;
  44. size_t index = 0;
  45. while (index < message->size()) {
  46. match = NalUnit::StartSequenceMatchSucc(match, (*message)[index++], separator);
  47. if (match == NUSM_longMatch || match == NUSM_shortMatch) {
  48. match = NUSM_noMatch;
  49. break;
  50. }
  51. }
  52. size_t naluStartIndex = index;
  53. while (index < message->size()) {
  54. match = NalUnit::StartSequenceMatchSucc(match, (*message)[index], separator);
  55. if (match == NUSM_longMatch || match == NUSM_shortMatch) {
  56. auto sequenceLength = match == NUSM_longMatch ? 4 : 3;
  57. size_t naluEndIndex = index - sequenceLength;
  58. match = NUSM_noMatch;
  59. auto begin = message->begin() + naluStartIndex;
  60. auto end = message->begin() + naluEndIndex + 1;
  61. nalus->push_back(std::make_shared<NalUnit>(begin, end));
  62. naluStartIndex = index + 1;
  63. }
  64. index++;
  65. }
  66. auto begin = message->begin() + naluStartIndex;
  67. auto end = message->end();
  68. nalus->push_back(std::make_shared<NalUnit>(begin, end));
  69. }
  70. return nalus;
  71. }
  72. H264RtpPacketizer::H264RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
  73. uint16_t maximumFragmentSize)
  74. : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize),
  75. separator(Separator::Length) {}
  76. H264RtpPacketizer::H264RtpPacketizer(Separator separator,
  77. shared_ptr<RtpPacketizationConfig> rtpConfig,
  78. uint16_t maximumFragmentSize)
  79. : RtpPacketizer(rtpConfig), MediaHandlerRootElement(), maximumFragmentSize(maximumFragmentSize),
  80. separator(separator) {}
  81. ChainedOutgoingProduct
  82. H264RtpPacketizer::processOutgoingBinaryMessage(ChainedMessagesProduct messages,
  83. message_ptr control) {
  84. ChainedMessagesProduct packets = std::make_shared<std::vector<binary_ptr>>();
  85. for (auto message : *messages) {
  86. auto nalus = splitMessage(message);
  87. auto fragments = nalus->generateFragments(maximumFragmentSize);
  88. if (fragments.size() == 0) {
  89. return ChainedOutgoingProduct();
  90. }
  91. unsigned i = 0;
  92. for (; i < fragments.size() - 1; i++) {
  93. packets->push_back(packetize(fragments[i], false));
  94. }
  95. packets->push_back(packetize(fragments[i], true));
  96. }
  97. return {packets, control};
  98. }
  99. } // namespace rtc
  100. #endif /* RTC_ENABLE_MEDIA */