2
0

rtcpsrreporter.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #if RTC_ENABLE_MEDIA
  18. #include "rtcpsrreporter.hpp"
  19. namespace rtc {
  20. ChainedOutgoingProduct RtcpSrReporter::processOutgoingBinaryMessage(ChainedMessagesProduct messages, message_ptr control) {
  21. if (needsToReport) {
  22. auto timestamp = rtpConfig->timestamp;
  23. auto sr = getSenderReport(timestamp);
  24. if (control) {
  25. control->insert(control->end(), sr->begin(), sr->end());
  26. } else {
  27. control = sr;
  28. }
  29. needsToReport = false;
  30. }
  31. for (auto message: *messages) {
  32. auto rtp = reinterpret_cast<RTP *>(message->data());
  33. addToReport(rtp, message->size());
  34. }
  35. return {messages, control};
  36. }
  37. void RtcpSrReporter::startRecording() {
  38. _previousReportedTimestamp = rtpConfig->timestamp;
  39. timeOffset = rtpConfig->startTime_s - rtpConfig->timestampToSeconds(rtpConfig->timestamp);
  40. }
  41. void RtcpSrReporter::addToReport(RTP *rtp, uint32_t rtpSize) {
  42. packetCount += 1;
  43. assert(!rtp->padding());
  44. payloadOctets += rtpSize - rtp->getSize();
  45. }
  46. RtcpSrReporter::RtcpSrReporter(std::shared_ptr<RtpPacketizationConfig> rtpConfig)
  47. : MediaHandlerElement(), rtpConfig(rtpConfig) {}
  48. uint64_t RtcpSrReporter::secondsToNTP(double seconds) {
  49. return std::round(seconds * double(uint64_t(1) << 32));
  50. }
  51. void RtcpSrReporter::setNeedsToReport() { needsToReport = true; }
  52. message_ptr RtcpSrReporter::getSenderReport(uint32_t timestamp) {
  53. auto srSize = RTCP_SR::size(0);
  54. auto msg = make_message(srSize + RTCP_SDES::size({{uint8_t(rtpConfig->cname.size())}}),
  55. Message::Type::Control);
  56. auto sr = reinterpret_cast<RTCP_SR *>(msg->data());
  57. auto timestamp_s = rtpConfig->timestampToSeconds(timestamp);
  58. auto currentTime = timeOffset + timestamp_s;
  59. sr->setNtpTimestamp(secondsToNTP(currentTime));
  60. sr->setRtpTimestamp(timestamp);
  61. sr->setPacketCount(packetCount);
  62. sr->setOctetCount(payloadOctets);
  63. sr->preparePacket(rtpConfig->ssrc, 0);
  64. auto sdes = reinterpret_cast<RTCP_SDES *>(msg->data() + srSize);
  65. auto chunk = sdes->getChunk(0);
  66. chunk->setSSRC(rtpConfig->ssrc);
  67. auto item = chunk->getItem(0);
  68. item->type = 1;
  69. item->setText(rtpConfig->cname);
  70. sdes->preparePacket(1);
  71. _previousReportedTimestamp = timestamp;
  72. return msg;
  73. }
  74. } // namespace rtc
  75. #endif /* RTC_ENABLE_MEDIA */