rtppacketizer.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "rtppacketizer.hpp"
  10. #include <cmath>
  11. #include <cstring>
  12. namespace rtc {
  13. RtpPacketizer::RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig) : rtpConfig(rtpConfig) {}
  14. RtpPacketizer::~RtpPacketizer() {}
  15. message_ptr RtpPacketizer::packetize(shared_ptr<binary> payload, bool mark) {
  16. size_t rtpExtHeaderSize = 0;
  17. bool twoByteHeader = false;
  18. const bool setVideoRotation =
  19. (rtpConfig->videoOrientationId != 0) && mark && (rtpConfig->videoOrientation != 0);
  20. // Determine if a two-byte header is necessary
  21. // Check for dependency descriptor extension
  22. if (rtpConfig->dependencyDescriptorContext.has_value()) {
  23. auto &ctx = *rtpConfig->dependencyDescriptorContext;
  24. DependencyDescriptorWriter writer(ctx.structure, ctx.activeChains, ctx.descriptor);
  25. auto sizeBytes = (writer.getSizeBits() + 7) / 8;
  26. if (sizeBytes > 16 || rtpConfig->dependencyDescriptorId > 14) {
  27. twoByteHeader = true;
  28. }
  29. }
  30. // Check for other extensions
  31. if ((setVideoRotation && rtpConfig->videoOrientationId > 14) ||
  32. (rtpConfig->mid.has_value() && rtpConfig->midId > 14) ||
  33. (rtpConfig->rid.has_value() && rtpConfig->ridId > 14)) {
  34. twoByteHeader = true;
  35. }
  36. size_t headerSize = twoByteHeader ? 2 : 1;
  37. if (setVideoRotation)
  38. rtpExtHeaderSize += headerSize + 1;
  39. const bool setPlayoutDelay = (rtpConfig->playoutDelayId > 0 && rtpConfig->playoutDelayId < 15);
  40. if (setPlayoutDelay)
  41. rtpExtHeaderSize += 4;
  42. if (rtpConfig->mid.has_value())
  43. rtpExtHeaderSize += headerSize + rtpConfig->mid->length();
  44. if (rtpConfig->rid.has_value())
  45. rtpExtHeaderSize += headerSize + rtpConfig->rid->length();
  46. if (rtpConfig->dependencyDescriptorContext.has_value()) {
  47. auto &ctx = *rtpConfig->dependencyDescriptorContext;
  48. DependencyDescriptorWriter writer(ctx.structure, ctx.activeChains, ctx.descriptor);
  49. rtpExtHeaderSize += headerSize + (writer.getSizeBits() + 7) / 8;
  50. }
  51. if (rtpExtHeaderSize != 0)
  52. rtpExtHeaderSize += 4;
  53. rtpExtHeaderSize = (rtpExtHeaderSize + 3) & ~3;
  54. auto message = make_message(RtpHeaderSize + rtpExtHeaderSize + payload->size());
  55. auto *rtp = (RtpHeader *)message->data();
  56. rtp->setPayloadType(rtpConfig->payloadType);
  57. rtp->setSeqNumber(rtpConfig->sequenceNumber++); // increase sequence number
  58. rtp->setTimestamp(rtpConfig->timestamp);
  59. rtp->setSsrc(rtpConfig->ssrc);
  60. if (mark) {
  61. rtp->setMarker(true);
  62. }
  63. if (rtpExtHeaderSize) {
  64. rtp->setExtension(true);
  65. auto extHeader = rtp->getExtensionHeader();
  66. extHeader->setProfileSpecificId(twoByteHeader ? 0x1000 : 0xbede);
  67. auto headerLength = static_cast<uint16_t>(rtpExtHeaderSize / 4) - 1;
  68. extHeader->setHeaderLength(headerLength);
  69. extHeader->clearBody();
  70. size_t offset = 0;
  71. if (setVideoRotation) {
  72. offset += extHeader->writeCurrentVideoOrientation(
  73. twoByteHeader, offset, rtpConfig->videoOrientationId, rtpConfig->videoOrientation);
  74. }
  75. if (rtpConfig->mid.has_value()) {
  76. offset +=
  77. extHeader->writeHeader(twoByteHeader, offset, rtpConfig->midId,
  78. reinterpret_cast<const std::byte *>(rtpConfig->mid->c_str()),
  79. rtpConfig->mid->length());
  80. }
  81. if (rtpConfig->rid.has_value()) {
  82. offset +=
  83. extHeader->writeHeader(twoByteHeader, offset, rtpConfig->ridId,
  84. reinterpret_cast<const std::byte *>(rtpConfig->rid->c_str()),
  85. rtpConfig->rid->length());
  86. }
  87. if (rtpConfig->dependencyDescriptorContext.has_value()) {
  88. auto &ctx = *rtpConfig->dependencyDescriptorContext;
  89. DependencyDescriptorWriter writer(ctx.structure, ctx.activeChains, ctx.descriptor);
  90. auto sizeBits = writer.getSizeBits();
  91. size_t sizeBytes = (sizeBits + 7) / 8;
  92. std::unique_ptr<std::byte[]> buf(new std::byte[sizeBytes]);
  93. writer.writeTo(buf.get(), sizeBits);
  94. offset += extHeader->writeHeader(
  95. twoByteHeader, offset, rtpConfig->dependencyDescriptorId, buf.get(), sizeBytes);
  96. }
  97. if (setPlayoutDelay) {
  98. uint16_t min = rtpConfig->playoutDelayMin & 0xFFF;
  99. uint16_t max = rtpConfig->playoutDelayMax & 0xFFF;
  100. // 12 bits for min + 12 bits for max
  101. byte data[] = {
  102. byte((min >> 4) & 0xFF),
  103. byte(((min & 0xF) << 4) | ((max >> 8) & 0xF)),
  104. byte(max & 0xFF)
  105. };
  106. extHeader->writeOneByteHeader(offset, rtpConfig->playoutDelayId, data, 3);
  107. offset += 4;
  108. }
  109. }
  110. rtp->preparePacket();
  111. std::memcpy(message->data() + RtpHeaderSize + rtpExtHeaderSize, payload->data(),
  112. payload->size());
  113. return message;
  114. }
  115. void RtpPacketizer::media([[maybe_unused]] const Description::Media &desc) {}
  116. void RtpPacketizer::outgoing([[maybe_unused]] message_vector &messages,
  117. [[maybe_unused]] const message_callback &send) {
  118. // Default implementation
  119. for (auto &message : messages)
  120. message = packetize(message, false);
  121. }
  122. } // namespace rtc
  123. #endif /* RTC_ENABLE_MEDIA */