rtppacketizer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. std::vector<binary> RtpPacketizer::fragment(binary data) {
  16. // Default implementation
  17. return {std::move(data)};
  18. }
  19. message_ptr RtpPacketizer::packetize(const binary &payload, bool mark) {
  20. size_t rtpExtHeaderSize = 0;
  21. bool twoByteHeader = false;
  22. const bool setVideoRotation =
  23. (rtpConfig->videoOrientationId != 0) && mark && (rtpConfig->videoOrientation != 0);
  24. std::optional<DependencyDescriptorWriter> ddWriter;
  25. if (rtpConfig->dependencyDescriptorContext.has_value()) {
  26. ddWriter.emplace(*rtpConfig->dependencyDescriptorContext);
  27. }
  28. // Determine if a two-byte header is necessary
  29. // Check for dependency descriptor extension
  30. if (ddWriter.has_value()) {
  31. auto sizeBytes = ddWriter->getSize();
  32. if (sizeBytes > 16 || rtpConfig->dependencyDescriptorId > 14) {
  33. twoByteHeader = true;
  34. }
  35. }
  36. // Check for other extensions
  37. if ((setVideoRotation && rtpConfig->videoOrientationId > 14) ||
  38. (rtpConfig->mid.has_value() && rtpConfig->midId > 14) ||
  39. (rtpConfig->rid.has_value() && rtpConfig->ridId > 14) ||
  40. rtpConfig->playoutDelayId > 14) {
  41. twoByteHeader = true;
  42. }
  43. size_t headerSize = twoByteHeader ? 2 : 1;
  44. if (setVideoRotation)
  45. rtpExtHeaderSize += headerSize + 1;
  46. const bool setPlayoutDelay = rtpConfig->playoutDelayId > 0;
  47. if (setPlayoutDelay)
  48. rtpExtHeaderSize += headerSize + 3;
  49. if (rtpConfig->mid.has_value())
  50. rtpExtHeaderSize += headerSize + rtpConfig->mid->length();
  51. if (rtpConfig->rid.has_value())
  52. rtpExtHeaderSize += headerSize + rtpConfig->rid->length();
  53. if (ddWriter.has_value()) {
  54. rtpExtHeaderSize += headerSize + ddWriter->getSize();
  55. }
  56. if (rtpExtHeaderSize != 0)
  57. rtpExtHeaderSize += 4;
  58. // Align the size to the multiple of 4 bytes
  59. // according to RFC 3550, sec. 5.3.1.
  60. rtpExtHeaderSize = (rtpExtHeaderSize + 3) & ~3;
  61. auto message = make_message(RtpHeaderSize + rtpExtHeaderSize + payload.size());
  62. auto *rtp = (RtpHeader *)message->data();
  63. rtp->setPayloadType(rtpConfig->payloadType);
  64. rtp->setSeqNumber(rtpConfig->sequenceNumber++); // increase sequence number
  65. rtp->setTimestamp(rtpConfig->timestamp);
  66. rtp->setSsrc(rtpConfig->ssrc);
  67. if (mark) {
  68. rtp->setMarker(true);
  69. }
  70. if (rtpExtHeaderSize) {
  71. rtp->setExtension(true);
  72. auto extHeader = rtp->getExtensionHeader();
  73. extHeader->setProfileSpecificId(twoByteHeader ? 0x1000 : 0xbede);
  74. auto headerLength = static_cast<uint16_t>(rtpExtHeaderSize / 4) - 1;
  75. extHeader->setHeaderLength(headerLength);
  76. extHeader->clearBody();
  77. size_t offset = 0;
  78. if (setVideoRotation) {
  79. offset += extHeader->writeCurrentVideoOrientation(
  80. twoByteHeader, offset, rtpConfig->videoOrientationId, rtpConfig->videoOrientation);
  81. }
  82. if (rtpConfig->mid.has_value()) {
  83. offset +=
  84. extHeader->writeHeader(twoByteHeader, offset, rtpConfig->midId,
  85. reinterpret_cast<const std::byte *>(rtpConfig->mid->c_str()),
  86. rtpConfig->mid->length());
  87. }
  88. if (rtpConfig->rid.has_value()) {
  89. offset +=
  90. extHeader->writeHeader(twoByteHeader, offset, rtpConfig->ridId,
  91. reinterpret_cast<const std::byte *>(rtpConfig->rid->c_str()),
  92. rtpConfig->rid->length());
  93. }
  94. if (ddWriter.has_value()) {
  95. auto sizeBytes = ddWriter->getSize();
  96. std::vector<std::byte> buf(sizeBytes);
  97. ddWriter->writeTo(buf.data(), sizeBytes);
  98. offset += extHeader->writeHeader(
  99. twoByteHeader, offset, rtpConfig->dependencyDescriptorId, buf.data(), sizeBytes);
  100. }
  101. if (setPlayoutDelay) {
  102. uint16_t min = rtpConfig->playoutDelayMin & 0xFFF;
  103. uint16_t max = rtpConfig->playoutDelayMax & 0xFFF;
  104. // 12 bits for min + 12 bits for max
  105. byte data[] = {byte((min >> 4) & 0xFF), byte(((min & 0xF) << 4) | ((max >> 8) & 0xF)),
  106. byte(max & 0xFF)};
  107. offset += extHeader->writeHeader(
  108. twoByteHeader, offset, rtpConfig->playoutDelayId, data, 3);
  109. }
  110. }
  111. rtp->preparePacket();
  112. std::memcpy(message->data() + RtpHeaderSize + rtpExtHeaderSize, payload.data(), payload.size());
  113. return message;
  114. }
  115. message_ptr RtpPacketizer::packetize(shared_ptr<binary> payload, bool mark) {
  116. return packetize(*payload, mark);
  117. }
  118. void RtpPacketizer::media([[maybe_unused]] const Description::Media &desc) {}
  119. void RtpPacketizer::outgoing(message_vector &messages,
  120. [[maybe_unused]] const message_callback &send) {
  121. message_vector result;
  122. for (const auto &message : messages) {
  123. if (const auto &frameInfo = message->frameInfo) {
  124. if (frameInfo->payloadType && frameInfo->payloadType != rtpConfig->payloadType)
  125. continue;
  126. if (frameInfo->timestampSeconds)
  127. rtpConfig->timestamp =
  128. rtpConfig->startTimestamp +
  129. rtpConfig->secondsToTimestamp(
  130. std::chrono::duration<double>(*frameInfo->timestampSeconds).count());
  131. else
  132. rtpConfig->timestamp = frameInfo->timestamp;
  133. }
  134. auto payloads = fragment(std::move(*message));
  135. for (size_t i = 0; i < payloads.size(); i++) {
  136. if (rtpConfig->dependencyDescriptorContext.has_value()) {
  137. auto &ctx = *rtpConfig->dependencyDescriptorContext;
  138. ctx.descriptor.startOfFrame = i == 0;
  139. ctx.descriptor.endOfFrame = i == payloads.size() - 1;
  140. }
  141. bool mark = i == payloads.size() - 1;
  142. result.push_back(packetize(payloads[i], mark));
  143. }
  144. }
  145. messages.swap(result);
  146. }
  147. } // namespace rtc
  148. #endif /* RTC_ENABLE_MEDIA */