Line data Source code
1 : /*
2 : * Copyright (c) 2016 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/sender_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 SenderReport::kPacketType;
21 : // Sender report (SR) (RFC 3550).
22 : // 0 1 2 3
23 : // 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
24 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25 : // |V=2|P| RC | PT=SR=200 | length |
26 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27 : // 0 | SSRC of sender |
28 : // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
29 : // 4 | NTP timestamp, most significant word |
30 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 : // 8 | NTP timestamp, least significant word |
32 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 : // 12 | RTP timestamp |
34 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 : // 16 | sender's packet count |
36 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 : // 20 | sender's octet count |
38 : // 24 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
39 :
40 0 : SenderReport::SenderReport()
41 : : sender_ssrc_(0),
42 : rtp_timestamp_(0),
43 : sender_packet_count_(0),
44 0 : sender_octet_count_(0) {}
45 :
46 0 : bool SenderReport::Parse(const CommonHeader& packet) {
47 0 : RTC_DCHECK_EQ(packet.type(), kPacketType);
48 :
49 0 : const uint8_t report_block_count = packet.count();
50 0 : if (packet.payload_size_bytes() <
51 0 : kSenderBaseLength + report_block_count * ReportBlock::kLength) {
52 0 : LOG(LS_WARNING) << "Packet is too small to contain all the data.";
53 0 : return false;
54 : }
55 : // Read SenderReport header.
56 0 : const uint8_t* const payload = packet.payload();
57 0 : sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&payload[0]);
58 0 : uint32_t secs = ByteReader<uint32_t>::ReadBigEndian(&payload[4]);
59 0 : uint32_t frac = ByteReader<uint32_t>::ReadBigEndian(&payload[8]);
60 0 : ntp_.Set(secs, frac);
61 0 : rtp_timestamp_ = ByteReader<uint32_t>::ReadBigEndian(&payload[12]);
62 0 : sender_packet_count_ = ByteReader<uint32_t>::ReadBigEndian(&payload[16]);
63 0 : sender_octet_count_ = ByteReader<uint32_t>::ReadBigEndian(&payload[20]);
64 0 : report_blocks_.resize(report_block_count);
65 0 : const uint8_t* next_block = payload + kSenderBaseLength;
66 0 : for (ReportBlock& block : report_blocks_) {
67 0 : bool block_parsed = block.Parse(next_block, ReportBlock::kLength);
68 0 : RTC_DCHECK(block_parsed);
69 0 : next_block += ReportBlock::kLength;
70 : }
71 : // Double check we didn't read beyond provided buffer.
72 0 : RTC_DCHECK_LE(next_block - payload,
73 0 : static_cast<ptrdiff_t>(packet.payload_size_bytes()));
74 0 : return true;
75 : }
76 :
77 0 : bool SenderReport::Create(uint8_t* packet,
78 : size_t* index,
79 : size_t max_length,
80 : RtcpPacket::PacketReadyCallback* callback) const {
81 0 : while (*index + BlockLength() > max_length) {
82 0 : if (!OnBufferFull(packet, index, callback))
83 0 : return false;
84 : }
85 0 : const size_t index_end = *index + BlockLength();
86 :
87 0 : CreateHeader(report_blocks_.size(), kPacketType, HeaderLength(), packet,
88 0 : index);
89 : // Write SenderReport header.
90 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], sender_ssrc_);
91 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 4], ntp_.seconds());
92 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 8], ntp_.fractions());
93 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 12], rtp_timestamp_);
94 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 16],
95 0 : sender_packet_count_);
96 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 20],
97 0 : sender_octet_count_);
98 0 : *index += kSenderBaseLength;
99 : // Write report blocks.
100 0 : for (const ReportBlock& block : report_blocks_) {
101 0 : block.Create(packet + *index);
102 0 : *index += ReportBlock::kLength;
103 : }
104 : // Ensure bytes written match expected.
105 0 : RTC_DCHECK_EQ(*index, index_end);
106 0 : return true;
107 : }
108 :
109 0 : bool SenderReport::AddReportBlock(const ReportBlock& block) {
110 0 : if (report_blocks_.size() >= kMaxNumberOfReportBlocks) {
111 0 : LOG(LS_WARNING) << "Max report blocks reached.";
112 0 : return false;
113 : }
114 0 : report_blocks_.push_back(block);
115 0 : return true;
116 : }
117 :
118 : } // namespace rtcp
119 : } // namespace webrtc
|