blob: 93c12d5672982eaafc99add192d41973ab233a4b [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <cstdint>
danilchap822a16f2016-09-27 09:27:47 -070014#include <utility>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#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"
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010020
21namespace webrtc {
22namespace rtcp {
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);
Elad Alon74f0a512019-02-23 22:16:52 +010050 RTC_DCHECK_EQ(packet.fmt(), Psfb::kAfbMessageType);
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])) {
danilchapd8dccd52016-01-20 12:08:51 -080059 return false;
60 }
61 uint8_t number_of_ssrcs = payload[12];
Danil Chapovalovd215ade2016-05-12 15:25:39 +020062 if (packet.payload_size_bytes() !=
danilchapd8dccd52016-01-20 12:08:51 -080063 kCommonFeedbackLength + (2 + number_of_ssrcs) * 4) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010064 RTC_LOG(LS_WARNING) << "Payload size " << packet.payload_size_bytes()
65 << " does not match " << number_of_ssrcs << " ssrcs.";
danilchapd8dccd52016-01-20 12:08:51 -080066 return false;
67 }
68
69 ParseCommonFeedback(payload);
70 uint8_t exponenta = payload[13] >> 2;
Danil Chapovalovd215ade2016-05-12 15:25:39 +020071 uint64_t mantissa = (static_cast<uint32_t>(payload[13] & 0x03) << 16) |
danilchapd8dccd52016-01-20 12:08:51 -080072 ByteReader<uint16_t>::ReadBigEndian(&payload[14]);
73 bitrate_bps_ = (mantissa << exponenta);
Danil Chapovalovd215ade2016-05-12 15:25:39 +020074 bool shift_overflow = (bitrate_bps_ >> exponenta) != mantissa;
75 if (shift_overflow) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010076 RTC_LOG(LS_ERROR) << "Invalid remb bitrate value : " << mantissa << "*2^"
77 << static_cast<int>(exponenta);
Danil Chapovalovd215ade2016-05-12 15:25:39 +020078 return false;
79 }
danilchapd8dccd52016-01-20 12:08:51 -080080
81 const uint8_t* next_ssrc = payload + 16;
82 ssrcs_.clear();
83 ssrcs_.reserve(number_of_ssrcs);
84 for (uint8_t i = 0; i < number_of_ssrcs; ++i) {
85 ssrcs_.push_back(ByteReader<uint32_t>::ReadBigEndian(next_ssrc));
86 next_ssrc += sizeof(uint32_t);
87 }
88
89 return true;
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010090}
danilchapd8dccd52016-01-20 12:08:51 -080091
danilchap822a16f2016-09-27 09:27:47 -070092bool Remb::SetSsrcs(std::vector<uint32_t> ssrcs) {
93 if (ssrcs.size() > kMaxNumberOfSsrcs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_WARNING) << "Not enough space for all given SSRCs.";
danilchapd8dccd52016-01-20 12:08:51 -080095 return false;
96 }
danilchap822a16f2016-09-27 09:27:47 -070097 ssrcs_ = std::move(ssrcs);
danilchapd8dccd52016-01-20 12:08:51 -080098 return true;
99}
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100100
eladalon8fa21c42017-06-16 07:07:47 -0700101size_t Remb::BlockLength() const {
102 return kHeaderLength + kCommonFeedbackLength + (2 + ssrcs_.size()) * 4;
103}
104
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100105bool Remb::Create(uint8_t* packet,
106 size_t* index,
107 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100108 PacketReadyCallback callback) const {
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100109 while (*index + BlockLength() > max_length) {
110 if (!OnBufferFull(packet, index, callback))
111 return false;
112 }
danilchapd8dccd52016-01-20 12:08:51 -0800113 size_t index_end = *index + BlockLength();
Elad Alon74f0a512019-02-23 22:16:52 +0100114 CreateHeader(Psfb::kAfbMessageType, kPacketType, HeaderLength(), packet,
danilchapd8dccd52016-01-20 12:08:51 -0800115 index);
kwibergaf476c72016-11-28 15:21:39 -0800116 RTC_DCHECK_EQ(0, Psfb::media_ssrc());
danilchapd8dccd52016-01-20 12:08:51 -0800117 CreateCommonFeedback(packet + *index);
118 *index += kCommonFeedbackLength;
119
120 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, kUniqueIdentifier);
121 *index += sizeof(uint32_t);
122 const uint32_t kMaxMantissa = 0x3ffff; // 18 bits.
Danil Chapovalovd215ade2016-05-12 15:25:39 +0200123 uint64_t mantissa = bitrate_bps_;
danilchapd8dccd52016-01-20 12:08:51 -0800124 uint8_t exponenta = 0;
125 while (mantissa > kMaxMantissa) {
126 mantissa >>= 1;
127 ++exponenta;
128 }
Danil Chapovalov6c170572017-09-15 16:48:14 +0200129 packet[(*index)++] = static_cast<uint8_t>(ssrcs_.size());
danilchapd8dccd52016-01-20 12:08:51 -0800130 packet[(*index)++] = (exponenta << 2) | (mantissa >> 16);
131 ByteWriter<uint16_t>::WriteBigEndian(packet + *index, mantissa & 0xffff);
132 *index += sizeof(uint16_t);
133
134 for (uint32_t ssrc : ssrcs_) {
135 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, ssrc);
136 *index += sizeof(uint32_t);
137 }
138 RTC_DCHECK_EQ(index_end, *index);
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100139 return true;
140}
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100141} // namespace rtcp
142} // namespace webrtc