xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbn.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
12 
13 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/logging.h"
16 
17 namespace webrtc {
18 namespace rtcp {
19 constexpr uint8_t Tmmbn::kFeedbackMessageType;
20 // 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 //   :                                                               :
34 // Temporary Maximum Media Stream Bit Rate Notification (TMMBN) (RFC 5104).
35 // The Feedback Control Information (FCI) consists of zero, one, or more
36 // TMMBN FCI entries.
37 //    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 //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 
45 Tmmbn::Tmmbn() = default;
46 
47 Tmmbn::~Tmmbn() = default;
48 
Parse(const CommonHeader & packet)49 bool Tmmbn::Parse(const CommonHeader& packet) {
50   RTC_DCHECK_EQ(packet.type(), kPacketType);
51   RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
52 
53   if (packet.payload_size_bytes() < kCommonFeedbackLength) {
54     RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
55                         << " is too small for TMMBN.";
56     return false;
57   }
58   size_t items_size_bytes = packet.payload_size_bytes() - kCommonFeedbackLength;
59   if (items_size_bytes % TmmbItem::kLength != 0) {
60     RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
61                         << " is not valid for TMMBN.";
62     return false;
63   }
64   ParseCommonFeedback(packet.payload());
65   const uint8_t* next_item = packet.payload() + kCommonFeedbackLength;
66 
67   size_t number_of_items = items_size_bytes / TmmbItem::kLength;
68   items_.resize(number_of_items);
69   for (TmmbItem& item : items_) {
70     if (!item.Parse(next_item))
71       return false;
72     next_item += TmmbItem::kLength;
73   }
74   return true;
75 }
76 
AddTmmbr(const TmmbItem & item)77 void Tmmbn::AddTmmbr(const TmmbItem& item) {
78   items_.push_back(item);
79 }
80 
BlockLength() const81 size_t Tmmbn::BlockLength() const {
82   return kHeaderLength + kCommonFeedbackLength +
83          TmmbItem::kLength * items_.size();
84 }
85 
Create(uint8_t * packet,size_t * index,size_t max_length,PacketReadyCallback callback) const86 bool Tmmbn::Create(uint8_t* packet,
87                    size_t* index,
88                    size_t max_length,
89                    PacketReadyCallback callback) const {
90   while (*index + BlockLength() > max_length) {
91     if (!OnBufferFull(packet, index, callback))
92       return false;
93   }
94   const size_t index_end = *index + BlockLength();
95 
96   CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
97                index);
98   RTC_DCHECK_EQ(0, Rtpfb::media_ssrc());
99   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);
106   return true;
107 }
108 }  // namespace rtcp
109 }  // namespace webrtc
110