rtcpreceivingsession.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) 2020 Staz Modrzynski
  3. * Copyright (c) 2020 Paul-Louis Ageneau
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #if RTC_ENABLE_MEDIA
  20. #include "rtcpreceivingsession.hpp"
  21. #include "track.hpp"
  22. #include "impl/logcounter.hpp"
  23. #include <cmath>
  24. #include <utility>
  25. #ifdef _WIN32
  26. #include <winsock2.h>
  27. #else
  28. #include <arpa/inet.h>
  29. #endif
  30. namespace rtc {
  31. static impl::LogCounter COUNTER_BAD_RTP_HEADER(plog::warning, "Number of malformed RTP headers");
  32. static impl::LogCounter COUNTER_UNKNOWN_PPID(plog::warning, "Number of Unknown PPID messages");
  33. static impl::LogCounter COUNTER_BAD_NOTIF_LEN(plog::warning, "Number of Bad-Lengthed notifications");
  34. static impl::LogCounter COUNTER_BAD_SCTP_STATUS(plog::warning, "Number of unknown SCTP_STATUS errors");
  35. message_ptr RtcpReceivingSession::outgoing(message_ptr ptr) { return ptr; }
  36. message_ptr RtcpReceivingSession::incoming(message_ptr ptr) {
  37. if (ptr->type == Message::Type::Binary) {
  38. auto rtp = reinterpret_cast<const RTP *>(ptr->data());
  39. // https://tools.ietf.org/html/rfc3550#appendix-A.1
  40. if (rtp->version() != 2) {
  41. COUNTER_BAD_RTP_HEADER++;
  42. PLOG_VERBOSE << "RTP packet is not version 2";
  43. return nullptr;
  44. }
  45. if (rtp->payloadType() == 201 || rtp->payloadType() == 200) {
  46. COUNTER_BAD_RTP_HEADER++;
  47. PLOG_VERBOSE << "RTP packet has a payload type indicating RR/SR";
  48. return nullptr;
  49. }
  50. // Padding-processing is a user-level thing
  51. mSsrc = rtp->ssrc();
  52. return ptr;
  53. }
  54. assert(ptr->type == Message::Type::Control);
  55. auto rr = reinterpret_cast<const RTCP_RR *>(ptr->data());
  56. if (rr->header.payloadType() == 201) {
  57. // RR
  58. mSsrc = rr->senderSSRC();
  59. rr->log();
  60. } else if (rr->header.payloadType() == 200) {
  61. // SR
  62. mSsrc = rr->senderSSRC();
  63. auto sr = reinterpret_cast<const RTCP_SR *>(ptr->data());
  64. mSyncRTPTS = sr->rtpTimestamp();
  65. mSyncNTPTS = sr->ntpTimestamp();
  66. sr->log();
  67. // TODO For the time being, we will send RR's/REMB's when we get an SR
  68. pushRR(0);
  69. if (mRequestedBitrate > 0)
  70. pushREMB(mRequestedBitrate);
  71. }
  72. return nullptr;
  73. }
  74. void RtcpReceivingSession::requestBitrate(unsigned int newBitrate) {
  75. mRequestedBitrate = newBitrate;
  76. PLOG_DEBUG << "[GOOG-REMB] Requesting bitrate: " << newBitrate << std::endl;
  77. pushREMB(newBitrate);
  78. }
  79. void RtcpReceivingSession::pushREMB(unsigned int bitrate) {
  80. message_ptr msg = make_message(RTCP_REMB::SizeWithSSRCs(1), Message::Type::Control);
  81. auto remb = reinterpret_cast<RTCP_REMB *>(msg->data());
  82. remb->preparePacket(mSsrc, 1, bitrate);
  83. remb->setSsrc(0, mSsrc);
  84. send(msg);
  85. }
  86. void RtcpReceivingSession::pushRR(unsigned int lastSR_delay) {
  87. auto msg = make_message(RTCP_RR::SizeWithReportBlocks(1), Message::Type::Control);
  88. auto rr = reinterpret_cast<RTCP_RR *>(msg->data());
  89. rr->preparePacket(mSsrc, 1);
  90. rr->getReportBlock(0)->preparePacket(mSsrc, 0, 0, uint16_t(mGreatestSeqNo), 0, 0, mSyncNTPTS,
  91. lastSR_delay);
  92. rr->log();
  93. send(msg);
  94. }
  95. bool RtcpReceivingSession::send(message_ptr msg) {
  96. try {
  97. outgoingCallback(std::move(msg));
  98. return true;
  99. } catch (const std::exception &e) {
  100. LOG_DEBUG << "RTCP tx failed: " << e.what();
  101. }
  102. return false;
  103. }
  104. bool RtcpReceivingSession::requestKeyframe() {
  105. pushPLI();
  106. return true; // TODO Make this false when it is impossible (i.e. Opus).
  107. }
  108. void RtcpReceivingSession::pushPLI() {
  109. auto msg = make_message(RTCP_PLI::Size(), Message::Type::Control);
  110. auto *pli = reinterpret_cast<RTCP_PLI *>(msg->data());
  111. pli->preparePacket(mSsrc);
  112. send(msg);
  113. }
  114. } // namespace rtc
  115. #endif // RTC_ENABLE_MEDIA