h265rtppacketizer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Copyright (c) 2023 Zita Liao (Dolby)
  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 "h265rtppacketizer.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<H265NalUnits> H265RtpPacketizer::splitMessage(binary_ptr message) {
  19. auto nalus = std::make_shared<H265NalUnits>();
  20. if (separator == NalUnit::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. uint32_t length;
  29. std::memcpy(&length, message->data() + index, sizeof(uint32_t));
  30. length = ntohl(length);
  31. auto naluStartIndex = index + 4;
  32. auto naluEndIndex = naluStartIndex + length;
  33. assert(naluEndIndex <= message->size());
  34. if (naluEndIndex > message->size()) {
  35. LOG_WARNING << "Invalid NAL Unit data (incomplete unit), ignoring!";
  36. break;
  37. }
  38. auto begin = message->begin() + naluStartIndex;
  39. auto end = message->begin() + naluEndIndex;
  40. nalus->push_back(std::make_shared<H265NalUnit>(begin, end));
  41. index = naluEndIndex;
  42. }
  43. } else {
  44. NalUnitStartSequenceMatch match = NUSM_noMatch;
  45. size_t index = 0;
  46. while (index < message->size()) {
  47. match = NalUnit::StartSequenceMatchSucc(match, (*message)[index++], separator);
  48. if (match == NUSM_longMatch || match == NUSM_shortMatch) {
  49. match = NUSM_noMatch;
  50. break;
  51. }
  52. }
  53. size_t naluStartIndex = index;
  54. while (index < message->size()) {
  55. match = NalUnit::StartSequenceMatchSucc(match, (*message)[index], separator);
  56. if (match == NUSM_longMatch || match == NUSM_shortMatch) {
  57. auto sequenceLength = match == NUSM_longMatch ? 4 : 3;
  58. size_t naluEndIndex = index - sequenceLength;
  59. match = NUSM_noMatch;
  60. auto begin = message->begin() + naluStartIndex;
  61. auto end = message->begin() + naluEndIndex + 1;
  62. nalus->push_back(std::make_shared<H265NalUnit>(begin, end));
  63. naluStartIndex = index + 1;
  64. }
  65. index++;
  66. }
  67. auto begin = message->begin() + naluStartIndex;
  68. auto end = message->end();
  69. nalus->push_back(std::make_shared<H265NalUnit>(begin, end));
  70. }
  71. return nalus;
  72. }
  73. H265RtpPacketizer::H265RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
  74. uint16_t maxFragmentSize)
  75. : RtpPacketizer(std::move(rtpConfig)), maxFragmentSize(maxFragmentSize),
  76. separator(NalUnit::Separator::Length) {}
  77. H265RtpPacketizer::H265RtpPacketizer(NalUnit::Separator separator,
  78. shared_ptr<RtpPacketizationConfig> rtpConfig,
  79. uint16_t maxFragmentSize)
  80. : RtpPacketizer(std::move(rtpConfig)), maxFragmentSize(maxFragmentSize),
  81. separator(separator) {}
  82. void H265RtpPacketizer::outgoing(message_vector &messages, [[maybe_unused]] const message_callback &send) {
  83. message_vector result;
  84. for (const auto &message : messages) {
  85. auto nalus = splitMessage(message);
  86. auto fragments = nalus->generateFragments(maxFragmentSize);
  87. if (fragments.size() == 0)
  88. continue;
  89. for (size_t i = 0; i < fragments.size() - 1; i++)
  90. result.push_back(packetize(fragments[i], false));
  91. result.push_back(packetize(fragments[fragments.size() - 1], true));
  92. }
  93. messages.swap(result);
  94. }
  95. } // namespace rtc
  96. #endif /* RTC_ENABLE_MEDIA */