rtppacketizer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if (rtpConfig->mid.has_value())
  25. rtpExtHeaderSize += (1 + rtpConfig->mid->length());
  26. if (rtpConfig->rid.has_value())
  27. rtpExtHeaderSize += (1 + rtpConfig->rid->length());
  28. if (rtpExtHeaderSize != 0)
  29. rtpExtHeaderSize += 4;
  30. auto message = make_message(RtpHeaderSize + rtpExtHeaderSize + payload->size());
  31. auto *rtp = (RtpHeader *)message->data();
  32. rtp->setPayloadType(rtpConfig->payloadType);
  33. rtp->setSeqNumber(rtpConfig->sequenceNumber++); // increase sequence number
  34. rtp->setTimestamp(rtpConfig->timestamp);
  35. rtp->setSsrc(rtpConfig->ssrc);
  36. if (mark) {
  37. rtp->setMarker(true);
  38. }
  39. if (rtpExtHeaderSize) {
  40. rtp->setExtension(true);
  41. auto extHeader = rtp->getExtensionHeader();
  42. extHeader->setProfileSpecificId(0xbede);
  43. auto headerLength = static_cast<uint16_t>(rtpExtHeaderSize - 4);
  44. headerLength = static_cast<uint16_t>((headerLength + 3) / 4);
  45. extHeader->setHeaderLength(headerLength);
  46. extHeader->clearBody();
  47. size_t offset = 0;
  48. if (setVideoRotation) {
  49. extHeader->writeCurrentVideoOrientation(offset, rtpConfig->videoOrientationId,
  50. rtpConfig->videoOrientation);
  51. offset += 2;
  52. }
  53. if (rtpConfig->mid.has_value()) {
  54. extHeader->writeOneByteHeader(
  55. offset, rtpConfig->midId,
  56. reinterpret_cast<const std::byte *>(rtpConfig->mid->c_str()),
  57. rtpConfig->mid->length());
  58. offset += (1 + rtpConfig->mid->length());
  59. }
  60. if (rtpConfig->rid.has_value()) {
  61. extHeader->writeOneByteHeader(
  62. offset, rtpConfig->ridId,
  63. reinterpret_cast<const std::byte *>(rtpConfig->rid->c_str()),
  64. rtpConfig->rid->length());
  65. }
  66. }
  67. rtp->preparePacket();
  68. std::memcpy(message->data() + RtpHeaderSize + rtpExtHeaderSize, payload->data(),
  69. payload->size());
  70. return message;
  71. }
  72. void RtpPacketizer::media([[maybe_unused]] const Description::Media &desc) {}
  73. void RtpPacketizer::outgoing([[maybe_unused]] message_vector &messages,
  74. [[maybe_unused]] const message_callback &send) {
  75. // Default implementation
  76. for (auto &message : messages)
  77. message = packetize(message, false);
  78. }
  79. } // namespace rtc
  80. #endif /* RTC_ENABLE_MEDIA */