blob: 6205dad6b7b597d3166801570ba6c6441a4aacfc [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/byte_io.h"
14#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
15#include "rtc_base/checks.h"
16#include "rtc_base/logging.h"
danilchapef3d8052016-01-11 03:31:08 -080017
18namespace webrtc {
19namespace rtcp {
danilchap6ce1adc2016-05-26 05:29:14 -070020constexpr uint8_t Tmmbn::kFeedbackMessageType;
danilchap7336eeb2016-02-08 03:35:10 -080021// RFC 4585: Feedback format.
22// Common packet format:
23//
24// 0 1 2 3
25// 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
26// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27// |V=2|P| FMT | PT | length |
28// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29// | SSRC of packet sender |
30// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31// | SSRC of media source (unused) = 0 |
32// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33// : Feedback Control Information (FCI) :
34// : :
danilchapef3d8052016-01-11 03:31:08 -080035// Temporary Maximum Media Stream Bit Rate Notification (TMMBN) (RFC 5104).
danilchap7336eeb2016-02-08 03:35:10 -080036// The Feedback Control Information (FCI) consists of zero, one, or more
37// TMMBN FCI entries.
danilchapef3d8052016-01-11 03:31:08 -080038// 0 1 2 3
39// 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
40// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41// | SSRC |
42// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43// | MxTBR Exp | MxTBR Mantissa |Measured Overhead|
44// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
eladalon8fa21c42017-06-16 07:07:47 -070045
46Tmmbn::Tmmbn() = default;
47
48Tmmbn::~Tmmbn() = default;
49
danilchap6ce1adc2016-05-26 05:29:14 -070050bool Tmmbn::Parse(const CommonHeader& packet) {
51 RTC_DCHECK_EQ(packet.type(), kPacketType);
52 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
danilchapef3d8052016-01-11 03:31:08 -080053
danilchap6ce1adc2016-05-26 05:29:14 -070054 if (packet.payload_size_bytes() < kCommonFeedbackLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010055 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
56 << " is too small for TMMBN.";
danilchapef3d8052016-01-11 03:31:08 -080057 return false;
58 }
danilchap6ce1adc2016-05-26 05:29:14 -070059 size_t items_size_bytes = packet.payload_size_bytes() - kCommonFeedbackLength;
danilchap7336eeb2016-02-08 03:35:10 -080060 if (items_size_bytes % TmmbItem::kLength != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010061 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
62 << " is not valid for TMMBN.";
danilchap7336eeb2016-02-08 03:35:10 -080063 return false;
64 }
danilchap6ce1adc2016-05-26 05:29:14 -070065 ParseCommonFeedback(packet.payload());
66 const uint8_t* next_item = packet.payload() + kCommonFeedbackLength;
danilchap7336eeb2016-02-08 03:35:10 -080067
68 size_t number_of_items = items_size_bytes / TmmbItem::kLength;
69 items_.resize(number_of_items);
70 for (TmmbItem& item : items_) {
danilchap6a1a8ce2016-05-21 09:54:40 -070071 if (!item.Parse(next_item))
72 return false;
danilchap7336eeb2016-02-08 03:35:10 -080073 next_item += TmmbItem::kLength;
74 }
danilchapef3d8052016-01-11 03:31:08 -080075 return true;
76}
77
danilchap822a16f2016-09-27 09:27:47 -070078void Tmmbn::AddTmmbr(const TmmbItem& item) {
danilchap7336eeb2016-02-08 03:35:10 -080079 items_.push_back(item);
80}
81
eladalon8fa21c42017-06-16 07:07:47 -070082size_t Tmmbn::BlockLength() const {
83 return kHeaderLength + kCommonFeedbackLength +
84 TmmbItem::kLength * items_.size();
85}
86
danilchapef3d8052016-01-11 03:31:08 -080087bool Tmmbn::Create(uint8_t* packet,
88 size_t* index,
89 size_t max_length,
90 RtcpPacket::PacketReadyCallback* callback) const {
91 while (*index + BlockLength() > max_length) {
92 if (!OnBufferFull(packet, index, callback))
93 return false;
94 }
danilchap7336eeb2016-02-08 03:35:10 -080095 const size_t index_end = *index + BlockLength();
96
97 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
98 index);
kwibergaf476c72016-11-28 15:21:39 -080099 RTC_DCHECK_EQ(0, Rtpfb::media_ssrc());
danilchap7336eeb2016-02-08 03:35:10 -0800100 CreateCommonFeedback(packet + *index);
101 *index += kCommonFeedbackLength;
102 for (const TmmbItem& item : items_) {
103 item.Create(packet + *index);
104 *index += TmmbItem::kLength;
105 }
106 RTC_CHECK_EQ(index_end, *index);
danilchapef3d8052016-01-11 03:31:08 -0800107 return true;
108}
danilchapef3d8052016-01-11 03:31:08 -0800109} // namespace rtcp
110} // namespace webrtc