OLD | NEW |
(Empty) | |
| 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 |
| 17 using webrtc::RTCPUtility::RtcpCommonHeader; |
| 18 |
| 19 namespace webrtc { |
| 20 namespace rtcp { |
| 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 SenderReport::SenderReport() |
| 41 : sender_ssrc_(0), |
| 42 rtp_timestamp_(0), |
| 43 sender_packet_count_(0), |
| 44 sender_octet_count_(0) {} |
| 45 |
| 46 bool SenderReport::Parse(const RtcpCommonHeader& header, |
| 47 const uint8_t* payload) { |
| 48 RTC_DCHECK(header.packet_type == kPacketType); |
| 49 |
| 50 const uint8_t report_block_count = header.count_or_format; |
| 51 if (header.payload_size_bytes < |
| 52 kSenderBaseLength + report_block_count * ReportBlock::kLength) { |
| 53 LOG(LS_WARNING) << "Packet is too small to contain all the data."; |
| 54 return false; |
| 55 } |
| 56 // Read SenderReport header. |
| 57 sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&payload[0]); |
| 58 uint32_t secs = ByteReader<uint32_t>::ReadBigEndian(&payload[4]); |
| 59 uint32_t frac = ByteReader<uint32_t>::ReadBigEndian(&payload[8]); |
| 60 ntp_.Set(secs, frac); |
| 61 rtp_timestamp_ = ByteReader<uint32_t>::ReadBigEndian(&payload[12]); |
| 62 sender_packet_count_ = ByteReader<uint32_t>::ReadBigEndian(&payload[16]); |
| 63 sender_octet_count_ = ByteReader<uint32_t>::ReadBigEndian(&payload[20]); |
| 64 report_blocks_.resize(report_block_count); |
| 65 const uint8_t* next_block = payload + kSenderBaseLength; |
| 66 for (ReportBlock& block : report_blocks_) { |
| 67 bool block_parsed = block.Parse(next_block, ReportBlock::kLength); |
| 68 RTC_DCHECK(block_parsed); |
| 69 next_block += ReportBlock::kLength; |
| 70 } |
| 71 // Double check we didn't read beyond provided buffer. |
| 72 RTC_DCHECK_LE(next_block, payload + header.payload_size_bytes); |
| 73 return true; |
| 74 } |
| 75 |
| 76 bool SenderReport::Create(uint8_t* packet, |
| 77 size_t* index, |
| 78 size_t max_length, |
| 79 RtcpPacket::PacketReadyCallback* callback) const { |
| 80 while (*index + BlockLength() > max_length) { |
| 81 if (!OnBufferFull(packet, index, callback)) |
| 82 return false; |
| 83 } |
| 84 const size_t index_end = *index + BlockLength(); |
| 85 |
| 86 CreateHeader(report_blocks_.size(), kPacketType, HeaderLength(), packet, |
| 87 index); |
| 88 // Write SenderReport header. |
| 89 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], sender_ssrc_); |
| 90 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 4], ntp_.seconds()); |
| 91 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 8], ntp_.fractions()); |
| 92 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 12], rtp_timestamp_); |
| 93 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 16], |
| 94 sender_packet_count_); |
| 95 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 20], |
| 96 sender_octet_count_); |
| 97 *index += kSenderBaseLength; |
| 98 // Write report blocks. |
| 99 for (const ReportBlock& block : report_blocks_) { |
| 100 block.Create(packet + *index); |
| 101 *index += ReportBlock::kLength; |
| 102 } |
| 103 // Ensure bytes written match expected. |
| 104 RTC_DCHECK_EQ(*index, index_end); |
| 105 return true; |
| 106 } |
| 107 |
| 108 bool SenderReport::WithReportBlock(const ReportBlock& block) { |
| 109 if (report_blocks_.size() >= kMaxNumberOfReportBlocks) { |
| 110 LOG(LS_WARNING) << "Max report blocks reached."; |
| 111 return false; |
| 112 } |
| 113 report_blocks_.push_back(block); |
| 114 return true; |
| 115 } |
| 116 |
| 117 } // namespace rtcp |
| 118 } // namespace webrtc |
OLD | NEW |