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 "modules/rtp_rtcp/source/rtcp_packet/extended_jitter_report.h"
12
13 #include <cstdint>
14 #include <utility>
15
16 #include "modules/rtp_rtcp/source/byte_io.h"
17 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/logging.h"
20
21 namespace webrtc {
22 namespace rtcp {
23 constexpr uint8_t ExtendedJitterReport::kPacketType;
24 // Transmission Time Offsets in RTP Streams (RFC 5450).
25 //
26 // 0 1 2 3
27 // 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
28 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29 // hdr |V=2|P| RC | PT=IJ=195 | length |
30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 // | inter-arrival jitter |
32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 // . .
34 // . .
35 // . .
36 // | inter-arrival jitter |
37 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 //
39 // If present, this RTCP packet must be placed after a receiver report
40 // (inside a compound RTCP packet), and MUST have the same value for RC
41 // (reception report count) as the receiver report.
42
43 ExtendedJitterReport::ExtendedJitterReport() = default;
44
45 ExtendedJitterReport::~ExtendedJitterReport() = default;
46
Parse(const CommonHeader & packet)47 bool ExtendedJitterReport::Parse(const CommonHeader& packet) {
48 RTC_DCHECK_EQ(packet.type(), kPacketType);
49
50 const uint8_t number_of_jitters = packet.count();
51
52 if (packet.payload_size_bytes() < number_of_jitters * kJitterSizeBytes) {
53 RTC_LOG(LS_WARNING) << "Packet is too small to contain all the jitter.";
54 return false;
55 }
56
57 inter_arrival_jitters_.resize(number_of_jitters);
58 for (size_t index = 0; index < number_of_jitters; ++index) {
59 inter_arrival_jitters_[index] = ByteReader<uint32_t>::ReadBigEndian(
60 &packet.payload()[index * kJitterSizeBytes]);
61 }
62
63 return true;
64 }
65
SetJitterValues(std::vector<uint32_t> values)66 bool ExtendedJitterReport::SetJitterValues(std::vector<uint32_t> values) {
67 if (values.size() > kMaxNumberOfJitterValues) {
68 RTC_LOG(LS_WARNING) << "Too many inter-arrival jitter items.";
69 return false;
70 }
71 inter_arrival_jitters_ = std::move(values);
72 return true;
73 }
74
BlockLength() const75 size_t ExtendedJitterReport::BlockLength() const {
76 return kHeaderLength + kJitterSizeBytes * inter_arrival_jitters_.size();
77 }
78
Create(uint8_t * packet,size_t * index,size_t max_length,PacketReadyCallback callback) const79 bool ExtendedJitterReport::Create(uint8_t* packet,
80 size_t* index,
81 size_t max_length,
82 PacketReadyCallback callback) const {
83 while (*index + BlockLength() > max_length) {
84 if (!OnBufferFull(packet, index, callback))
85 return false;
86 }
87 const size_t index_end = *index + BlockLength();
88 size_t length = inter_arrival_jitters_.size();
89 CreateHeader(length, kPacketType, length, packet, index);
90
91 for (uint32_t jitter : inter_arrival_jitters_) {
92 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, jitter);
93 *index += kJitterSizeBytes;
94 }
95 // Sanity check.
96 RTC_DCHECK_EQ(index_end, *index);
97 return true;
98 }
99
100 } // namespace rtcp
101 } // namespace webrtc
102