Line data Source code
1 : /*
2 : * Copyright (c) 2015 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 "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
12 :
13 : #include "webrtc/base/checks.h"
14 : #include "webrtc/base/logging.h"
15 : #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
16 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
17 :
18 : namespace webrtc {
19 : namespace rtcp {
20 : constexpr uint8_t ReceiverReport::kPacketType;
21 : // RTCP receiver report (RFC 3550).
22 : //
23 : // 0 1 2 3
24 : // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
25 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26 : // |V=2|P| RC | PT=RR=201 | length |
27 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28 : // | SSRC of packet sender |
29 : // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
30 : // | report block(s) |
31 : // | .... |
32 0 : bool ReceiverReport::Parse(const CommonHeader& packet) {
33 0 : RTC_DCHECK_EQ(packet.type(), kPacketType);
34 :
35 0 : const uint8_t report_blocks_count = packet.count();
36 :
37 0 : if (packet.payload_size_bytes() <
38 0 : kRrBaseLength + report_blocks_count * ReportBlock::kLength) {
39 0 : LOG(LS_WARNING) << "Packet is too small to contain all the data.";
40 0 : return false;
41 : }
42 :
43 0 : sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(packet.payload());
44 :
45 0 : const uint8_t* next_report_block = packet.payload() + kRrBaseLength;
46 :
47 0 : report_blocks_.resize(report_blocks_count);
48 0 : for (ReportBlock& block : report_blocks_) {
49 0 : block.Parse(next_report_block, ReportBlock::kLength);
50 0 : next_report_block += ReportBlock::kLength;
51 : }
52 :
53 0 : RTC_DCHECK_LE(next_report_block - packet.payload(),
54 0 : static_cast<ptrdiff_t>(packet.payload_size_bytes()));
55 0 : return true;
56 : }
57 :
58 0 : bool ReceiverReport::Create(uint8_t* packet,
59 : size_t* index,
60 : size_t max_length,
61 : RtcpPacket::PacketReadyCallback* callback) const {
62 0 : while (*index + BlockLength() > max_length) {
63 0 : if (!OnBufferFull(packet, index, callback))
64 0 : return false;
65 : }
66 0 : CreateHeader(report_blocks_.size(), kPacketType, HeaderLength(), packet,
67 0 : index);
68 0 : ByteWriter<uint32_t>::WriteBigEndian(packet + *index, sender_ssrc_);
69 0 : *index += kRrBaseLength;
70 0 : for (const ReportBlock& block : report_blocks_) {
71 0 : block.Create(packet + *index);
72 0 : *index += ReportBlock::kLength;
73 : }
74 0 : return true;
75 : }
76 :
77 0 : bool ReceiverReport::AddReportBlock(const ReportBlock& block) {
78 0 : if (report_blocks_.size() >= kMaxNumberOfReportBlocks) {
79 0 : LOG(LS_WARNING) << "Max report blocks reached.";
80 0 : return false;
81 : }
82 0 : report_blocks_.push_back(block);
83 0 : return true;
84 : }
85 :
86 : } // namespace rtcp
87 : } // namespace webrtc
|