rtppacketizer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. const bool setVideoRotation = (rtpConfig->videoOrientationId != 0) &&
  18. (rtpConfig->videoOrientationId <
  19. 15) && // needs fixing if longer extension headers are supported
  20. mark &&
  21. (rtpConfig->videoOrientation != 0);
  22. if (setVideoRotation)
  23. rtpExtHeaderSize += 2;
  24. const bool setPlayoutDelay = (rtpConfig->playoutDelayId > 0 && rtpConfig->playoutDelayId < 15);
  25. if (setPlayoutDelay)
  26. rtpExtHeaderSize += 1;
  27. if (rtpConfig->mid.has_value())
  28. rtpExtHeaderSize += (1 + rtpConfig->mid->length());
  29. if (rtpConfig->rid.has_value())
  30. rtpExtHeaderSize += (1 + rtpConfig->rid->length());
  31. if (rtpExtHeaderSize != 0)
  32. rtpExtHeaderSize += 4;
  33. rtpExtHeaderSize = (rtpExtHeaderSize + 3) & ~3;
  34. auto message = make_message(RtpHeaderSize + rtpExtHeaderSize + payload->size());
  35. auto *rtp = (RtpHeader *)message->data();
  36. rtp->setPayloadType(rtpConfig->payloadType);
  37. rtp->setSeqNumber(rtpConfig->sequenceNumber++); // increase sequence number
  38. rtp->setTimestamp(rtpConfig->timestamp);
  39. rtp->setSsrc(rtpConfig->ssrc);
  40. if (mark) {
  41. rtp->setMarker(true);
  42. }
  43. if (rtpExtHeaderSize) {
  44. rtp->setExtension(true);
  45. auto extHeader = rtp->getExtensionHeader();
  46. extHeader->setProfileSpecificId(0xbede);
  47. auto headerLength = static_cast<uint16_t>(rtpExtHeaderSize / 4) - 1;
  48. extHeader->setHeaderLength(headerLength);
  49. extHeader->clearBody();
  50. size_t offset = 0;
  51. if (setVideoRotation) {
  52. extHeader->writeCurrentVideoOrientation(offset, rtpConfig->videoOrientationId,
  53. rtpConfig->videoOrientation);
  54. offset += 2;
  55. }
  56. if (rtpConfig->mid.has_value()) {
  57. extHeader->writeOneByteHeader(
  58. offset, rtpConfig->midId,
  59. reinterpret_cast<const std::byte *>(rtpConfig->mid->c_str()),
  60. rtpConfig->mid->length());
  61. offset += (1 + rtpConfig->mid->length());
  62. }
  63. if (rtpConfig->rid.has_value()) {
  64. extHeader->writeOneByteHeader(
  65. offset, rtpConfig->ridId,
  66. reinterpret_cast<const std::byte *>(rtpConfig->rid->c_str()),
  67. rtpConfig->rid->length());
  68. }
  69. if (setPlayoutDelay) {
  70. uint16_t min = rtpConfig->playoutDelayMin & 0xFFF;
  71. uint16_t max = rtpConfig->playoutDelayMax & 0xFFF;
  72. // 12 bits for min + 12 bits for max
  73. byte data[] = {
  74. byte((min >> 4) & 0xFF),
  75. byte(((min & 0xF) << 4) | ((max >> 8) & 0xF)),
  76. byte(max & 0xFF)
  77. };
  78. extHeader->writeOneByteHeader(offset, rtpConfig->playoutDelayId, data, 3);
  79. offset += 4;
  80. }
  81. }
  82. rtp->preparePacket();
  83. std::memcpy(message->data() + RtpHeaderSize + rtpExtHeaderSize, payload->data(),
  84. payload->size());
  85. return message;
  86. }
  87. void RtpPacketizer::media([[maybe_unused]] const Description::Media &desc) {}
  88. void RtpPacketizer::outgoing([[maybe_unused]] message_vector &messages,
  89. [[maybe_unused]] const message_callback &send) {
  90. // Default implementation
  91. for (auto &message : messages)
  92. message = packetize(message, false);
  93. }
  94. } // namespace rtc
  95. #endif /* RTC_ENABLE_MEDIA */