rtcpsenderreportable.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef RTCPSenderReporter_hpp
  19. #define RTCPSenderReporter_hpp
  20. #if RTC_ENABLE_MEDIA
  21. #include "message.hpp"
  22. #include "rtppacketizationconfig.hpp"
  23. namespace rtc {
  24. /// Class for sending RTCP SR
  25. class RTC_CPP_EXPORT RTCPSenderReportable {
  26. bool needsToReport = false;
  27. uint32_t packetCount = 0;
  28. uint32_t payloadOctets = 0;
  29. double timeOffset = 0;
  30. uint32_t _previousReportedTimestamp = 0;
  31. void addToReport(RTP * rtp, uint32_t rtpSize);
  32. message_ptr getSenderReport(uint32_t timestamp);
  33. protected:
  34. /// Outgoing callback for sender reports
  35. synchronized_callback<message_ptr> senderReportOutgoingCallback;
  36. public:
  37. static uint64_t secondsToNTP(double seconds);
  38. /// Timestamp of previous sender report
  39. const uint32_t & previousReportedTimestamp = _previousReportedTimestamp;
  40. /// RTP configuration
  41. const std::shared_ptr<RTPPacketizationConfig> rtpConfig;
  42. RTCPSenderReportable(std::shared_ptr<RTPPacketizationConfig> rtpConfig);
  43. /// Set `needsToReport` flag. Sender report will be sent before next RTP packet with same timestamp.
  44. void setNeedsToReport();
  45. /// Set offset to compute NTS for RTCP SR packets. Offset represents relation between real start time and timestamp of the stream in RTP packets
  46. /// @note `time_offset = rtpConfig->startTime_s - rtpConfig->timestampToSeconds(rtpConfig->timestamp)`
  47. void startRecording();
  48. /// Send RTCP SR with given timestamp
  49. /// @param timestamp timestamp of the RTCP SR
  50. void sendReport(uint32_t timestamp);
  51. protected:
  52. /// Calls given block with function for statistics. Sends RTCP SR packet with current timestamp before `block` call if `needs_to_report` flag is true.
  53. /// @param block Block of code to run. This block has function for rtp stats recording.
  54. template <typename T>
  55. T withStatsRecording(std::function<T (std::function<void (message_ptr)>)> block) {
  56. if (needsToReport) {
  57. sendReport(rtpConfig->timestamp);
  58. needsToReport = false;
  59. }
  60. auto result = block([this](message_ptr _rtp) {
  61. auto rtp = reinterpret_cast<RTP *>(_rtp->data());
  62. this->addToReport(rtp, _rtp->size());
  63. });
  64. return result;
  65. }
  66. };
  67. } // namespace
  68. #endif /* RTC_ENABLE_MEDIA */
  69. #endif /* RTCPSenderReporter_hpp */