blob: 02406116f6e793e6b02b6466fa8939022165b9c9 [file] [log] [blame]
Danil Chapovalova5eba6c2016-01-15 12:40:15 +01001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010012
danilchap822a16f2016-09-27 09:27:47 -070013#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/source/byte_io.h"
16#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010019
20namespace webrtc {
21namespace rtcp {
Danil Chapovalovd215ade2016-05-12 15:25:39 +020022constexpr uint8_t Remb::kFeedbackMessageType;
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010023// Receiver Estimated Max Bitrate (REMB) (draft-alvestrand-rmcat-remb).
24//
danilchapd8dccd52016-01-20 12:08:51 -080025// 0 1 2 3
26// 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
27// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28// |V=2|P| FMT=15 | PT=206 | length |
29// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
30// 0 | SSRC of packet sender |
31// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32// 4 | Unused = 0 |
33// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34// 8 | Unique identifier 'R' 'E' 'M' 'B' |
35// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36// 12 | Num SSRC | BR Exp | BR Mantissa |
37// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38// 16 | SSRC feedback |
39// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40// : ... :
eladalon8fa21c42017-06-16 07:07:47 -070041
42Remb::Remb() : bitrate_bps_(0) {}
43
Mirko Bonadei55d5ef02018-09-03 09:47:38 +020044Remb::Remb(const Remb& rhs) = default;
45
eladalon8fa21c42017-06-16 07:07:47 -070046Remb::~Remb() = default;
47
Danil Chapovalovd215ade2016-05-12 15:25:39 +020048bool Remb::Parse(const CommonHeader& packet) {
49 RTC_DCHECK(packet.type() == kPacketType);
50 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010051
Danil Chapovalovd215ade2016-05-12 15:25:39 +020052 if (packet.payload_size_bytes() < 16) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010053 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
54 << " is too small for Remb packet.";
danilchapd8dccd52016-01-20 12:08:51 -080055 return false;
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010056 }
Danil Chapovalovd215ade2016-05-12 15:25:39 +020057 const uint8_t* const payload = packet.payload();
danilchapd8dccd52016-01-20 12:08:51 -080058 if (kUniqueIdentifier != ByteReader<uint32_t>::ReadBigEndian(&payload[8])) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LS_WARNING) << "REMB identifier not found, not a REMB packet.";
danilchapd8dccd52016-01-20 12:08:51 -080060 return false;
61 }
62 uint8_t number_of_ssrcs = payload[12];
Danil Chapovalovd215ade2016-05-12 15:25:39 +020063 if (packet.payload_size_bytes() !=
danilchapd8dccd52016-01-20 12:08:51 -080064 kCommonFeedbackLength + (2 + number_of_ssrcs) * 4) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010065 RTC_LOG(LS_WARNING) << "Payload size " << packet.payload_size_bytes()
66 << " does not match " << number_of_ssrcs << " ssrcs.";
danilchapd8dccd52016-01-20 12:08:51 -080067 return false;
68 }
69
70 ParseCommonFeedback(payload);
71 uint8_t exponenta = payload[13] >> 2;
Danil Chapovalovd215ade2016-05-12 15:25:39 +020072 uint64_t mantissa = (static_cast<uint32_t>(payload[13] & 0x03) << 16) |
danilchapd8dccd52016-01-20 12:08:51 -080073 ByteReader<uint16_t>::ReadBigEndian(&payload[14]);
74 bitrate_bps_ = (mantissa << exponenta);
Danil Chapovalovd215ade2016-05-12 15:25:39 +020075 bool shift_overflow = (bitrate_bps_ >> exponenta) != mantissa;
76 if (shift_overflow) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_ERROR) << "Invalid remb bitrate value : " << mantissa << "*2^"
78 << static_cast<int>(exponenta);
Danil Chapovalovd215ade2016-05-12 15:25:39 +020079 return false;
80 }
danilchapd8dccd52016-01-20 12:08:51 -080081
82 const uint8_t* next_ssrc = payload + 16;
83 ssrcs_.clear();
84 ssrcs_.reserve(number_of_ssrcs);
85 for (uint8_t i = 0; i < number_of_ssrcs; ++i) {
86 ssrcs_.push_back(ByteReader<uint32_t>::ReadBigEndian(next_ssrc));
87 next_ssrc += sizeof(uint32_t);
88 }
89
90 return true;
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010091}
danilchapd8dccd52016-01-20 12:08:51 -080092
danilchap822a16f2016-09-27 09:27:47 -070093bool Remb::SetSsrcs(std::vector<uint32_t> ssrcs) {
94 if (ssrcs.size() > kMaxNumberOfSsrcs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010095 RTC_LOG(LS_WARNING) << "Not enough space for all given SSRCs.";
danilchapd8dccd52016-01-20 12:08:51 -080096 return false;
97 }
danilchap822a16f2016-09-27 09:27:47 -070098 ssrcs_ = std::move(ssrcs);
danilchapd8dccd52016-01-20 12:08:51 -080099 return true;
100}
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100101
eladalon8fa21c42017-06-16 07:07:47 -0700102size_t Remb::BlockLength() const {
103 return kHeaderLength + kCommonFeedbackLength + (2 + ssrcs_.size()) * 4;
104}
105
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100106bool Remb::Create(uint8_t* packet,
107 size_t* index,
108 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100109 PacketReadyCallback callback) const {
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100110 while (*index + BlockLength() > max_length) {
111 if (!OnBufferFull(packet, index, callback))
112 return false;
113 }
danilchapd8dccd52016-01-20 12:08:51 -0800114 size_t index_end = *index + BlockLength();
115 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
116 index);
kwibergaf476c72016-11-28 15:21:39 -0800117 RTC_DCHECK_EQ(0, Psfb::media_ssrc());
danilchapd8dccd52016-01-20 12:08:51 -0800118 CreateCommonFeedback(packet + *index);
119 *index += kCommonFeedbackLength;
120
121 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, kUniqueIdentifier);
122 *index += sizeof(uint32_t);
123 const uint32_t kMaxMantissa = 0x3ffff; // 18 bits.
Danil Chapovalovd215ade2016-05-12 15:25:39 +0200124 uint64_t mantissa = bitrate_bps_;
danilchapd8dccd52016-01-20 12:08:51 -0800125 uint8_t exponenta = 0;
126 while (mantissa > kMaxMantissa) {
127 mantissa >>= 1;
128 ++exponenta;
129 }
Danil Chapovalov6c170572017-09-15 16:48:14 +0200130 packet[(*index)++] = static_cast<uint8_t>(ssrcs_.size());
danilchapd8dccd52016-01-20 12:08:51 -0800131 packet[(*index)++] = (exponenta << 2) | (mantissa >> 16);
132 ByteWriter<uint16_t>::WriteBigEndian(packet + *index, mantissa & 0xffff);
133 *index += sizeof(uint16_t);
134
135 for (uint32_t ssrc : ssrcs_) {
136 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, ssrc);
137 *index += sizeof(uint32_t);
138 }
139 RTC_DCHECK_EQ(index_end, *index);
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100140 return true;
141}
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100142} // namespace rtcp
143} // namespace webrtc