h265rtppacketizer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. 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<H265NalUnit>(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<H265NalUnit>(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<H265NalUnit>(begin, end));
  69. }
  70. return nalus;
  71. }
  72. H265RtpPacketizer::H265RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
  73. uint16_t maxFragmentSize)
  74. : RtpPacketizer(std::move(rtpConfig)), maxFragmentSize(maxFragmentSize),
  75. separator(NalUnit::Separator::Length) {}
  76. H265RtpPacketizer::H265RtpPacketizer(NalUnit::Separator separator,
  77. shared_ptr<RtpPacketizationConfig> rtpConfig,
  78. uint16_t maxFragmentSize)
  79. : RtpPacketizer(std::move(rtpConfig)), maxFragmentSize(maxFragmentSize),
  80. separator(separator) {}
  81. void H265RtpPacketizer::outgoing(message_vector &messages, [[maybe_unused]] const message_callback &send) {
  82. message_vector result;
  83. for (const auto &message : messages) {
  84. auto nalus = splitMessage(message);
  85. auto fragments = nalus->generateFragments(maxFragmentSize);
  86. if (fragments.size() == 0)
  87. continue;
  88. for (size_t i = 0; i < fragments.size() - 1; i++)
  89. result.push_back(packetize(fragments[i], false));
  90. result.push_back(packetize(fragments[fragments.size() - 1], true));
  91. }
  92. messages.swap(result);
  93. }
  94. } // namespace rtc
  95. #endif /* RTC_ENABLE_MEDIA */