2
0

rtcpsrreporter.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #if RTC_ENABLE_MEDIA
  19. #include "rtcpsrreporter.hpp"
  20. #include <cassert>
  21. #include <cmath>
  22. namespace rtc {
  23. ChainedOutgoingProduct RtcpSrReporter::processOutgoingBinaryMessage(ChainedMessagesProduct messages,
  24. message_ptr control) {
  25. if (needsToReport) {
  26. auto timestamp = rtpConfig->timestamp;
  27. auto sr = getSenderReport(timestamp);
  28. if (control) {
  29. control->insert(control->end(), sr->begin(), sr->end());
  30. } else {
  31. control = sr;
  32. }
  33. needsToReport = false;
  34. }
  35. for (auto message : *messages) {
  36. auto rtp = reinterpret_cast<RtpHeader *>(message->data());
  37. addToReport(rtp, uint32_t(message->size()));
  38. }
  39. return {messages, control};
  40. }
  41. void RtcpSrReporter::startRecording() {
  42. mPreviousReportedTimestamp = rtpConfig->timestamp;
  43. timeOffset = rtpConfig->startTime - rtpConfig->timestampToSeconds(rtpConfig->timestamp);
  44. }
  45. void RtcpSrReporter::addToReport(RtpHeader *rtp, uint32_t rtpSize) {
  46. packetCount += 1;
  47. assert(!rtp->padding());
  48. payloadOctets += rtpSize - uint32_t(rtp->getSize());
  49. }
  50. RtcpSrReporter::RtcpSrReporter(shared_ptr<RtpPacketizationConfig> rtpConfig)
  51. : MediaHandlerElement(), rtpConfig(rtpConfig) {}
  52. uint64_t RtcpSrReporter::secondsToNTP(double seconds) {
  53. return uint64_t(std::round(seconds * double(uint64_t(1) << 32)));
  54. }
  55. void RtcpSrReporter::setNeedsToReport() { needsToReport = true; }
  56. message_ptr RtcpSrReporter::getSenderReport(uint32_t timestamp) {
  57. auto srSize = RtcpSr::Size(0);
  58. auto msg = make_message(srSize + RtcpSdes::Size({{uint8_t(rtpConfig->cname.size())}}),
  59. Message::Control);
  60. auto sr = reinterpret_cast<RtcpSr *>(msg->data());
  61. auto timestamp_s = rtpConfig->timestampToSeconds(timestamp);
  62. auto currentTime = timeOffset + timestamp_s;
  63. sr->setNtpTimestamp(secondsToNTP(currentTime));
  64. sr->setRtpTimestamp(timestamp);
  65. sr->setPacketCount(packetCount);
  66. sr->setOctetCount(payloadOctets);
  67. sr->preparePacket(rtpConfig->ssrc, 0);
  68. auto sdes = reinterpret_cast<RtcpSdes *>(msg->data() + srSize);
  69. auto chunk = sdes->getChunk(0);
  70. chunk->setSSRC(rtpConfig->ssrc);
  71. auto item = chunk->getItem(0);
  72. item->type = 1;
  73. item->setText(rtpConfig->cname);
  74. sdes->preparePacket(1);
  75. mPreviousReportedTimestamp = timestamp;
  76. return msg;
  77. }
  78. } // namespace rtc
  79. #endif /* RTC_ENABLE_MEDIA */