blob: 981a2969539f198b46412a140ea3da0374c553a6 [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
44Remb::~Remb() = default;
45
Danil Chapovalovd215ade2016-05-12 15:25:39 +020046bool Remb::Parse(const CommonHeader& packet) {
47 RTC_DCHECK(packet.type() == kPacketType);
48 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010049
Danil Chapovalovd215ade2016-05-12 15:25:39 +020050 if (packet.payload_size_bytes() < 16) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010051 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
52 << " is too small for Remb packet.";
danilchapd8dccd52016-01-20 12:08:51 -080053 return false;
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010054 }
Danil Chapovalovd215ade2016-05-12 15:25:39 +020055 const uint8_t* const payload = packet.payload();
danilchapd8dccd52016-01-20 12:08:51 -080056 if (kUniqueIdentifier != ByteReader<uint32_t>::ReadBigEndian(&payload[8])) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010057 RTC_LOG(LS_WARNING) << "REMB identifier not found, not a REMB packet.";
danilchapd8dccd52016-01-20 12:08:51 -080058 return false;
59 }
60 uint8_t number_of_ssrcs = payload[12];
Danil Chapovalovd215ade2016-05-12 15:25:39 +020061 if (packet.payload_size_bytes() !=
danilchapd8dccd52016-01-20 12:08:51 -080062 kCommonFeedbackLength + (2 + number_of_ssrcs) * 4) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010063 RTC_LOG(LS_WARNING) << "Payload size " << packet.payload_size_bytes()
64 << " does not match " << number_of_ssrcs << " ssrcs.";
danilchapd8dccd52016-01-20 12:08:51 -080065 return false;
66 }
67
68 ParseCommonFeedback(payload);
69 uint8_t exponenta = payload[13] >> 2;
Danil Chapovalovd215ade2016-05-12 15:25:39 +020070 uint64_t mantissa = (static_cast<uint32_t>(payload[13] & 0x03) << 16) |
danilchapd8dccd52016-01-20 12:08:51 -080071 ByteReader<uint16_t>::ReadBigEndian(&payload[14]);
72 bitrate_bps_ = (mantissa << exponenta);
Danil Chapovalovd215ade2016-05-12 15:25:39 +020073 bool shift_overflow = (bitrate_bps_ >> exponenta) != mantissa;
74 if (shift_overflow) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010075 RTC_LOG(LS_ERROR) << "Invalid remb bitrate value : " << mantissa << "*2^"
76 << static_cast<int>(exponenta);
Danil Chapovalovd215ade2016-05-12 15:25:39 +020077 return false;
78 }
danilchapd8dccd52016-01-20 12:08:51 -080079
80 const uint8_t* next_ssrc = payload + 16;
81 ssrcs_.clear();
82 ssrcs_.reserve(number_of_ssrcs);
83 for (uint8_t i = 0; i < number_of_ssrcs; ++i) {
84 ssrcs_.push_back(ByteReader<uint32_t>::ReadBigEndian(next_ssrc));
85 next_ssrc += sizeof(uint32_t);
86 }
87
88 return true;
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010089}
danilchapd8dccd52016-01-20 12:08:51 -080090
danilchap822a16f2016-09-27 09:27:47 -070091bool Remb::SetSsrcs(std::vector<uint32_t> ssrcs) {
92 if (ssrcs.size() > kMaxNumberOfSsrcs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010093 RTC_LOG(LS_WARNING) << "Not enough space for all given SSRCs.";
danilchapd8dccd52016-01-20 12:08:51 -080094 return false;
95 }
danilchap822a16f2016-09-27 09:27:47 -070096 ssrcs_ = std::move(ssrcs);
danilchapd8dccd52016-01-20 12:08:51 -080097 return true;
98}
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010099
eladalon8fa21c42017-06-16 07:07:47 -0700100size_t Remb::BlockLength() const {
101 return kHeaderLength + kCommonFeedbackLength + (2 + ssrcs_.size()) * 4;
102}
103
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100104bool Remb::Create(uint8_t* packet,
105 size_t* index,
106 size_t max_length,
107 RtcpPacket::PacketReadyCallback* callback) const {
108 while (*index + BlockLength() > max_length) {
109 if (!OnBufferFull(packet, index, callback))
110 return false;
111 }
danilchapd8dccd52016-01-20 12:08:51 -0800112 size_t index_end = *index + BlockLength();
113 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
114 index);
kwibergaf476c72016-11-28 15:21:39 -0800115 RTC_DCHECK_EQ(0, Psfb::media_ssrc());
danilchapd8dccd52016-01-20 12:08:51 -0800116 CreateCommonFeedback(packet + *index);
117 *index += kCommonFeedbackLength;
118
119 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, kUniqueIdentifier);
120 *index += sizeof(uint32_t);
121 const uint32_t kMaxMantissa = 0x3ffff; // 18 bits.
Danil Chapovalovd215ade2016-05-12 15:25:39 +0200122 uint64_t mantissa = bitrate_bps_;
danilchapd8dccd52016-01-20 12:08:51 -0800123 uint8_t exponenta = 0;
124 while (mantissa > kMaxMantissa) {
125 mantissa >>= 1;
126 ++exponenta;
127 }
Danil Chapovalov6c170572017-09-15 16:48:14 +0200128 packet[(*index)++] = static_cast<uint8_t>(ssrcs_.size());
danilchapd8dccd52016-01-20 12:08:51 -0800129 packet[(*index)++] = (exponenta << 2) | (mantissa >> 16);
130 ByteWriter<uint16_t>::WriteBigEndian(packet + *index, mantissa & 0xffff);
131 *index += sizeof(uint16_t);
132
133 for (uint32_t ssrc : ssrcs_) {
134 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, ssrc);
135 *index += sizeof(uint32_t);
136 }
137 RTC_DCHECK_EQ(index_end, *index);
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100138 return true;
139}
Danil Chapovalova5eba6c2016-01-15 12:40:15 +0100140} // namespace rtcp
141} // namespace webrtc