rtppacketizer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. binary_ptr RtpPacketizer::packetize(shared_ptr<binary> payload, bool setMark) {
  15. size_t rtpExtHeaderSize = 0;
  16. const bool setVideoRotation = (rtpConfig->videoOrientationId != 0) &&
  17. (rtpConfig->videoOrientationId <
  18. 15) && // needs fixing if longer extension headers are supported
  19. setMark &&
  20. (rtpConfig->videoOrientation != 0);
  21. if (setVideoRotation) {
  22. rtpExtHeaderSize += 2;
  23. }
  24. if (rtpConfig->mid.has_value()) {
  25. rtpExtHeaderSize += (1 + rtpConfig->mid->length());
  26. }
  27. if (rtpConfig->rid.has_value()) {
  28. rtpExtHeaderSize += (1 + rtpConfig->rid->length());
  29. }
  30. if (rtpExtHeaderSize != 0) {
  31. rtpExtHeaderSize += 4;
  32. }
  33. rtpExtHeaderSize = (rtpExtHeaderSize + 3) & ~3;
  34. auto msg = std::make_shared<binary>(rtpHeaderSize + rtpExtHeaderSize + payload->size());
  35. auto *rtp = (RtpHeader *)msg->data();
  36. rtp->setPayloadType(rtpConfig->payloadType);
  37. // increase sequence number
  38. rtp->setSeqNumber(rtpConfig->sequenceNumber++);
  39. rtp->setTimestamp(rtpConfig->timestamp);
  40. rtp->setSsrc(rtpConfig->ssrc);
  41. if (setMark) {
  42. rtp->setMarker(true);
  43. }
  44. if (rtpExtHeaderSize) {
  45. rtp->setExtension(true);
  46. auto extHeader = rtp->getExtensionHeader();
  47. extHeader->setProfileSpecificId(0xbede);
  48. auto headerLength = static_cast<uint16_t>(rtpExtHeaderSize / 4) - 1;
  49. extHeader->setHeaderLength(headerLength);
  50. extHeader->clearBody();
  51. size_t offset = 0;
  52. if (setVideoRotation) {
  53. extHeader->writeCurrentVideoOrientation(offset, rtpConfig->videoOrientationId,
  54. rtpConfig->videoOrientation);
  55. offset += 2;
  56. }
  57. if (rtpConfig->mid.has_value()) {
  58. extHeader->writeOneByteHeader(
  59. offset, rtpConfig->midId,
  60. reinterpret_cast<const std::byte *>(rtpConfig->mid->c_str()),
  61. rtpConfig->mid->length());
  62. offset += (1 + rtpConfig->mid->length());
  63. }
  64. if (rtpConfig->rid.has_value()) {
  65. extHeader->writeOneByteHeader(
  66. offset, rtpConfig->ridId,
  67. reinterpret_cast<const std::byte *>(rtpConfig->rid->c_str()),
  68. rtpConfig->rid->length());
  69. }
  70. }
  71. rtp->preparePacket();
  72. std::memcpy(msg->data() + rtpHeaderSize + rtpExtHeaderSize, payload->data(), payload->size());
  73. return msg;
  74. }
  75. } // namespace rtc
  76. #endif /* RTC_ENABLE_MEDIA */