blob: 34f31029b63a3cb85c6dd6028371c4a032987a34 [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/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"
danilchap7e8145f2016-01-11 11:49:19 -080017
18namespace webrtc {
19namespace rtcp {
danilchap6ce1adc2016-05-26 05:29:14 -070020constexpr uint8_t Tmmbr::kFeedbackMessageType;
danilchapf174e3a2016-02-05 04:56:36 -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// : :
danilchap7e8145f2016-01-11 11:49:19 -080035// Temporary Maximum Media Stream Bit Rate Request (TMMBR) (RFC 5104).
danilchapf174e3a2016-02-05 04:56:36 -080036// The Feedback Control Information (FCI) for the TMMBR
37// consists of one or more FCI entries.
danilchap7e8145f2016-01-11 11:49:19 -080038// FCI:
danilchapf174e3a2016-02-05 04:56:36 -080039// 0 1 2 3
40// 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
41// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42// | SSRC |
43// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44// | MxTBR Exp | MxTBR Mantissa |Measured Overhead|
45// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
eladalon8fa21c42017-06-16 07:07:47 -070046
47Tmmbr::Tmmbr() = default;
48
49Tmmbr::~Tmmbr() = default;
50
danilchap6ce1adc2016-05-26 05:29:14 -070051bool Tmmbr::Parse(const CommonHeader& packet) {
52 RTC_DCHECK_EQ(packet.type(), kPacketType);
53 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
danilchap7e8145f2016-01-11 11:49:19 -080054
danilchap6ce1adc2016-05-26 05:29:14 -070055 if (packet.payload_size_bytes() < kCommonFeedbackLength + TmmbItem::kLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010056 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
57 << " is too small for a TMMBR.";
danilchapf174e3a2016-02-05 04:56:36 -080058 return false;
59 }
danilchap6ce1adc2016-05-26 05:29:14 -070060 size_t items_size_bytes = packet.payload_size_bytes() - kCommonFeedbackLength;
danilchapf174e3a2016-02-05 04:56:36 -080061 if (items_size_bytes % TmmbItem::kLength != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010062 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
63 << " is not valid for a TMMBR.";
danilchapf174e3a2016-02-05 04:56:36 -080064 return false;
65 }
danilchap6ce1adc2016-05-26 05:29:14 -070066 ParseCommonFeedback(packet.payload());
danilchapf174e3a2016-02-05 04:56:36 -080067
danilchap6ce1adc2016-05-26 05:29:14 -070068 const uint8_t* next_item = packet.payload() + kCommonFeedbackLength;
danilchapf174e3a2016-02-05 04:56:36 -080069 size_t number_of_items = items_size_bytes / TmmbItem::kLength;
70 items_.resize(number_of_items);
71 for (TmmbItem& item : items_) {
danilchap6a1a8ce2016-05-21 09:54:40 -070072 if (!item.Parse(next_item))
73 return false;
danilchapf174e3a2016-02-05 04:56:36 -080074 next_item += TmmbItem::kLength;
75 }
76 return true;
danilchap7e8145f2016-01-11 11:49:19 -080077}
danilchapf174e3a2016-02-05 04:56:36 -080078
danilchap822a16f2016-09-27 09:27:47 -070079void Tmmbr::AddTmmbr(const TmmbItem& item) {
danilchapf174e3a2016-02-05 04:56:36 -080080 items_.push_back(item);
81}
danilchap7e8145f2016-01-11 11:49:19 -080082
eladalon8fa21c42017-06-16 07:07:47 -070083size_t Tmmbr::BlockLength() const {
84 return kHeaderLength + kCommonFeedbackLength +
85 TmmbItem::kLength * items_.size();
86}
87
danilchap7e8145f2016-01-11 11:49:19 -080088bool Tmmbr::Create(uint8_t* packet,
89 size_t* index,
90 size_t max_length,
91 RtcpPacket::PacketReadyCallback* callback) const {
danilchapf174e3a2016-02-05 04:56:36 -080092 RTC_DCHECK(!items_.empty());
danilchap7e8145f2016-01-11 11:49:19 -080093 while (*index + BlockLength() > max_length) {
94 if (!OnBufferFull(packet, index, callback))
95 return false;
96 }
danilchapf174e3a2016-02-05 04:56:36 -080097 const size_t index_end = *index + BlockLength();
98
99 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
100 index);
kwibergaf476c72016-11-28 15:21:39 -0800101 RTC_DCHECK_EQ(0, Rtpfb::media_ssrc());
danilchapf174e3a2016-02-05 04:56:36 -0800102 CreateCommonFeedback(packet + *index);
103 *index += kCommonFeedbackLength;
104 for (const TmmbItem& item : items_) {
105 item.Create(packet + *index);
106 *index += TmmbItem::kLength;
107 }
108 RTC_CHECK_EQ(index_end, *index);
danilchap7e8145f2016-01-11 11:49:19 -0800109 return true;
110}
danilchap7e8145f2016-01-11 11:49:19 -0800111} // namespace rtcp
112} // namespace webrtc