blob: 9dc745e509c48a090e81f92e98308339a1f35a59 [file] [log] [blame]
danilchap7e8145f2016-01-11 11:49:19 -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/tmmbr.h"
danilchap7e8145f2016-01-11 11:49:19 -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"
danilchap7e8145f2016-01-11 11:49:19 -080016
17namespace webrtc {
18namespace rtcp {
danilchap6ce1adc2016-05-26 05:29:14 -070019constexpr uint8_t Tmmbr::kFeedbackMessageType;
danilchapf174e3a2016-02-05 04:56:36 -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// : :
danilchap7e8145f2016-01-11 11:49:19 -080034// Temporary Maximum Media Stream Bit Rate Request (TMMBR) (RFC 5104).
danilchapf174e3a2016-02-05 04:56:36 -080035// The Feedback Control Information (FCI) for the TMMBR
36// consists of one or more FCI entries.
danilchap7e8145f2016-01-11 11:49:19 -080037// FCI:
danilchapf174e3a2016-02-05 04:56:36 -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
46Tmmbr::Tmmbr() = default;
47
48Tmmbr::~Tmmbr() = default;
49
danilchap6ce1adc2016-05-26 05:29:14 -070050bool Tmmbr::Parse(const CommonHeader& packet) {
51 RTC_DCHECK_EQ(packet.type(), kPacketType);
52 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
danilchap7e8145f2016-01-11 11:49:19 -080053
danilchap6ce1adc2016-05-26 05:29:14 -070054 if (packet.payload_size_bytes() < kCommonFeedbackLength + TmmbItem::kLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010055 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
56 << " is too small for a TMMBR.";
danilchapf174e3a2016-02-05 04:56:36 -080057 return false;
58 }
danilchap6ce1adc2016-05-26 05:29:14 -070059 size_t items_size_bytes = packet.payload_size_bytes() - kCommonFeedbackLength;
danilchapf174e3a2016-02-05 04:56:36 -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 a TMMBR.";
danilchapf174e3a2016-02-05 04:56:36 -080063 return false;
64 }
danilchap6ce1adc2016-05-26 05:29:14 -070065 ParseCommonFeedback(packet.payload());
danilchapf174e3a2016-02-05 04:56:36 -080066
danilchap6ce1adc2016-05-26 05:29:14 -070067 const uint8_t* next_item = packet.payload() + kCommonFeedbackLength;
danilchapf174e3a2016-02-05 04:56:36 -080068 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;
danilchapf174e3a2016-02-05 04:56:36 -080073 next_item += TmmbItem::kLength;
74 }
75 return true;
danilchap7e8145f2016-01-11 11:49:19 -080076}
danilchapf174e3a2016-02-05 04:56:36 -080077
danilchap822a16f2016-09-27 09:27:47 -070078void Tmmbr::AddTmmbr(const TmmbItem& item) {
danilchapf174e3a2016-02-05 04:56:36 -080079 items_.push_back(item);
80}
danilchap7e8145f2016-01-11 11:49:19 -080081
eladalon8fa21c42017-06-16 07:07:47 -070082size_t Tmmbr::BlockLength() const {
83 return kHeaderLength + kCommonFeedbackLength +
84 TmmbItem::kLength * items_.size();
85}
86
danilchap7e8145f2016-01-11 11:49:19 -080087bool Tmmbr::Create(uint8_t* packet,
88 size_t* index,
89 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010090 PacketReadyCallback callback) const {
danilchapf174e3a2016-02-05 04:56:36 -080091 RTC_DCHECK(!items_.empty());
danilchap7e8145f2016-01-11 11:49:19 -080092 while (*index + BlockLength() > max_length) {
93 if (!OnBufferFull(packet, index, callback))
94 return false;
95 }
danilchapf174e3a2016-02-05 04:56:36 -080096 const size_t index_end = *index + BlockLength();
97
98 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
99 index);
kwibergaf476c72016-11-28 15:21:39 -0800100 RTC_DCHECK_EQ(0, Rtpfb::media_ssrc());
danilchapf174e3a2016-02-05 04:56:36 -0800101 CreateCommonFeedback(packet + *index);
102 *index += kCommonFeedbackLength;
103 for (const TmmbItem& item : items_) {
104 item.Create(packet + *index);
105 *index += TmmbItem::kLength;
106 }
107 RTC_CHECK_EQ(index_end, *index);
danilchap7e8145f2016-01-11 11:49:19 -0800108 return true;
109}
danilchap7e8145f2016-01-11 11:49:19 -0800110} // namespace rtcp
111} // namespace webrtc