Danil Chapovalov | 398a7c6 | 2017-10-24 17:07:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "modules/rtp_rtcp/source/rtcp_transceiver_impl.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "api/call/transport.h" |
| 17 | #include "modules/rtp_rtcp/include/receive_statistics.h" |
| 18 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 19 | #include "modules/rtp_rtcp/source/rtcp_packet.h" |
| 20 | #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h" |
| 21 | #include "modules/rtp_rtcp/source/rtcp_packet/report_block.h" |
Danil Chapovalov | 78161ca | 2017-10-26 12:09:41 +0200 | [diff] [blame] | 22 | #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h" |
Danil Chapovalov | 398a7c6 | 2017-10-24 17:07:05 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | namespace { |
| 27 | |
| 28 | // Helper to put several RTCP packets into lower layer datagram composing |
| 29 | // Compound or Reduced-Size RTCP packet, as defined by RFC 5506 section 2. |
| 30 | class PacketSender : public rtcp::RtcpPacket::PacketReadyCallback { |
| 31 | public: |
| 32 | PacketSender(Transport* transport, size_t max_packet_size) |
| 33 | : transport_(transport), max_packet_size_(max_packet_size) { |
| 34 | RTC_CHECK_LE(max_packet_size, IP_PACKET_SIZE); |
| 35 | } |
| 36 | ~PacketSender() override { |
| 37 | RTC_DCHECK_EQ(index_, 0) << "Unsent rtcp packet."; |
| 38 | } |
| 39 | |
| 40 | // Appends a packet to pending compound packet. |
| 41 | // Sends rtcp compound packet if buffer was already full and resets buffer. |
| 42 | void AppendPacket(const rtcp::RtcpPacket& packet) { |
| 43 | packet.Create(buffer_, &index_, max_packet_size_, this); |
| 44 | } |
| 45 | |
| 46 | // Sends pending rtcp compound packet. |
| 47 | void Send() { |
| 48 | if (index_ > 0) { |
| 49 | OnPacketReady(buffer_, index_); |
| 50 | index_ = 0; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | // Implements RtcpPacket::PacketReadyCallback |
| 56 | void OnPacketReady(uint8_t* data, size_t length) override { |
| 57 | transport_->SendRtcp(data, length); |
| 58 | } |
| 59 | |
| 60 | Transport* const transport_; |
| 61 | const size_t max_packet_size_; |
| 62 | size_t index_ = 0; |
| 63 | uint8_t buffer_[IP_PACKET_SIZE]; |
| 64 | }; |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | RtcpTransceiverImpl::RtcpTransceiverImpl(const RtcpTransceiverConfig& config) |
| 69 | : config_(config) { |
| 70 | RTC_CHECK(config_.Validate()); |
| 71 | } |
| 72 | |
| 73 | RtcpTransceiverImpl::~RtcpTransceiverImpl() = default; |
| 74 | |
| 75 | void RtcpTransceiverImpl::SendCompoundPacket() { |
| 76 | PacketSender sender(config_.outgoing_transport, config_.max_packet_size); |
| 77 | |
| 78 | rtcp::ReceiverReport rr; |
| 79 | rr.SetSenderSsrc(config_.feedback_ssrc); |
| 80 | if (config_.receive_statistics) { |
| 81 | // TODO(danilchap): Support sending more than |
| 82 | // |ReceiverReport::kMaxNumberOfReportBlocks| per compound rtcp packet. |
| 83 | std::vector<rtcp::ReportBlock> report_blocks = |
| 84 | config_.receive_statistics->RtcpReportBlocks( |
| 85 | rtcp::ReceiverReport::kMaxNumberOfReportBlocks); |
| 86 | // TODO(danilchap): Fill in LastSr/DelayLastSr fields of report blocks |
| 87 | // when RtcpTransceiver handles incoming sender reports. |
| 88 | rr.SetReportBlocks(std::move(report_blocks)); |
| 89 | } |
| 90 | sender.AppendPacket(rr); |
Danil Chapovalov | 78161ca | 2017-10-26 12:09:41 +0200 | [diff] [blame] | 91 | if (!config_.cname.empty()) { |
| 92 | rtcp::Sdes sdes; |
| 93 | bool added = sdes.AddCName(config_.feedback_ssrc, config_.cname); |
| 94 | RTC_DCHECK(added) << "Failed to add cname " << config_.cname |
| 95 | << " to rtcp sdes packet."; |
| 96 | sender.AppendPacket(sdes); |
| 97 | } |
Danil Chapovalov | 398a7c6 | 2017-10-24 17:07:05 +0200 | [diff] [blame] | 98 | |
| 99 | sender.Send(); |
| 100 | } |
| 101 | |
| 102 | } // namespace webrtc |