blob: f57e5749c2ded05a9ca1bc2f8620c7ae231fccd1 [file] [log] [blame]
danilchapef3d8052016-01-11 03:31:08 -08001/*
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/tmmbn.h"
danilchapef3d8052016-01-11 03:31:08 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
14#include "rtc_base/checks.h"
15#include "rtc_base/logging.h"
danilchapef3d8052016-01-11 03:31:08 -080016
17namespace webrtc {
18namespace rtcp {
danilchap6ce1adc2016-05-26 05:29:14 -070019constexpr uint8_t Tmmbn::kFeedbackMessageType;
danilchap7336eeb2016-02-08 03:35:10 -080020// RFC 4585: Feedback format.
21// Common packet format:
22//
23// 0 1 2 3
24// 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
25// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26// |V=2|P| FMT | PT | length |
27// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28// | SSRC of packet sender |
29// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30// | SSRC of media source (unused) = 0 |
31// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32// : Feedback Control Information (FCI) :
33// : :
danilchapef3d8052016-01-11 03:31:08 -080034// Temporary Maximum Media Stream Bit Rate Notification (TMMBN) (RFC 5104).
danilchap7336eeb2016-02-08 03:35:10 -080035// The Feedback Control Information (FCI) consists of zero, one, or more
36// TMMBN FCI entries.
danilchapef3d8052016-01-11 03:31:08 -080037// 0 1 2 3
38// 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
39// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40// | SSRC |
41// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42// | MxTBR Exp | MxTBR Mantissa |Measured Overhead|
43// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
eladalon8fa21c42017-06-16 07:07:47 -070044
45Tmmbn::Tmmbn() = default;
46
47Tmmbn::~Tmmbn() = default;
48
danilchap6ce1adc2016-05-26 05:29:14 -070049bool Tmmbn::Parse(const CommonHeader& packet) {
50 RTC_DCHECK_EQ(packet.type(), kPacketType);
51 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
danilchapef3d8052016-01-11 03:31:08 -080052
danilchap6ce1adc2016-05-26 05:29:14 -070053 if (packet.payload_size_bytes() < kCommonFeedbackLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010054 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
55 << " is too small for TMMBN.";
danilchapef3d8052016-01-11 03:31:08 -080056 return false;
57 }
danilchap6ce1adc2016-05-26 05:29:14 -070058 size_t items_size_bytes = packet.payload_size_bytes() - kCommonFeedbackLength;
danilchap7336eeb2016-02-08 03:35:10 -080059 if (items_size_bytes % TmmbItem::kLength != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010060 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
61 << " is not valid for TMMBN.";
danilchap7336eeb2016-02-08 03:35:10 -080062 return false;
63 }
danilchap6ce1adc2016-05-26 05:29:14 -070064 ParseCommonFeedback(packet.payload());
65 const uint8_t* next_item = packet.payload() + kCommonFeedbackLength;
danilchap7336eeb2016-02-08 03:35:10 -080066
67 size_t number_of_items = items_size_bytes / TmmbItem::kLength;
68 items_.resize(number_of_items);
69 for (TmmbItem& item : items_) {
danilchap6a1a8ce2016-05-21 09:54:40 -070070 if (!item.Parse(next_item))
71 return false;
danilchap7336eeb2016-02-08 03:35:10 -080072 next_item += TmmbItem::kLength;
73 }
danilchapef3d8052016-01-11 03:31:08 -080074 return true;
75}
76
danilchap822a16f2016-09-27 09:27:47 -070077void Tmmbn::AddTmmbr(const TmmbItem& item) {
danilchap7336eeb2016-02-08 03:35:10 -080078 items_.push_back(item);
79}
80
eladalon8fa21c42017-06-16 07:07:47 -070081size_t Tmmbn::BlockLength() const {
82 return kHeaderLength + kCommonFeedbackLength +
83 TmmbItem::kLength * items_.size();
84}
85
danilchapef3d8052016-01-11 03:31:08 -080086bool Tmmbn::Create(uint8_t* packet,
87 size_t* index,
88 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010089 PacketReadyCallback callback) const {
danilchapef3d8052016-01-11 03:31:08 -080090 while (*index + BlockLength() > max_length) {
91 if (!OnBufferFull(packet, index, callback))
92 return false;
93 }
danilchap7336eeb2016-02-08 03:35:10 -080094 const size_t index_end = *index + BlockLength();
95
96 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
97 index);
kwibergaf476c72016-11-28 15:21:39 -080098 RTC_DCHECK_EQ(0, Rtpfb::media_ssrc());
danilchap7336eeb2016-02-08 03:35:10 -080099 CreateCommonFeedback(packet + *index);
100 *index += kCommonFeedbackLength;
101 for (const TmmbItem& item : items_) {
102 item.Create(packet + *index);
103 *index += TmmbItem::kLength;
104 }
105 RTC_CHECK_EQ(index_end, *index);
danilchapef3d8052016-01-11 03:31:08 -0800106 return true;
107}
danilchapef3d8052016-01-11 03:31:08 -0800108} // namespace rtcp
109} // namespace webrtc