danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h" |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #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" |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace rtcp { |
danilchap | 6ce1adc | 2016-05-26 05:29:14 -0700 | [diff] [blame] | 20 | constexpr uint8_t Tmmbr::kFeedbackMessageType; |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 21 | // 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 | // : : |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 35 | // Temporary Maximum Media Stream Bit Rate Request (TMMBR) (RFC 5104). |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 36 | // The Feedback Control Information (FCI) for the TMMBR |
| 37 | // consists of one or more FCI entries. |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 38 | // FCI: |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 39 | // 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 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 46 | |
| 47 | Tmmbr::Tmmbr() = default; |
| 48 | |
| 49 | Tmmbr::~Tmmbr() = default; |
| 50 | |
danilchap | 6ce1adc | 2016-05-26 05:29:14 -0700 | [diff] [blame] | 51 | bool Tmmbr::Parse(const CommonHeader& packet) { |
| 52 | RTC_DCHECK_EQ(packet.type(), kPacketType); |
| 53 | RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType); |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 54 | |
danilchap | 6ce1adc | 2016-05-26 05:29:14 -0700 | [diff] [blame] | 55 | if (packet.payload_size_bytes() < kCommonFeedbackLength + TmmbItem::kLength) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 56 | RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes() |
| 57 | << " is too small for a TMMBR."; |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 58 | return false; |
| 59 | } |
danilchap | 6ce1adc | 2016-05-26 05:29:14 -0700 | [diff] [blame] | 60 | size_t items_size_bytes = packet.payload_size_bytes() - kCommonFeedbackLength; |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 61 | if (items_size_bytes % TmmbItem::kLength != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 62 | RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes() |
| 63 | << " is not valid for a TMMBR."; |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 64 | return false; |
| 65 | } |
danilchap | 6ce1adc | 2016-05-26 05:29:14 -0700 | [diff] [blame] | 66 | ParseCommonFeedback(packet.payload()); |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 67 | |
danilchap | 6ce1adc | 2016-05-26 05:29:14 -0700 | [diff] [blame] | 68 | const uint8_t* next_item = packet.payload() + kCommonFeedbackLength; |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 69 | size_t number_of_items = items_size_bytes / TmmbItem::kLength; |
| 70 | items_.resize(number_of_items); |
| 71 | for (TmmbItem& item : items_) { |
danilchap | 6a1a8ce | 2016-05-21 09:54:40 -0700 | [diff] [blame] | 72 | if (!item.Parse(next_item)) |
| 73 | return false; |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 74 | next_item += TmmbItem::kLength; |
| 75 | } |
| 76 | return true; |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 77 | } |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 78 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 79 | void Tmmbr::AddTmmbr(const TmmbItem& item) { |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 80 | items_.push_back(item); |
| 81 | } |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 82 | |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 83 | size_t Tmmbr::BlockLength() const { |
| 84 | return kHeaderLength + kCommonFeedbackLength + |
| 85 | TmmbItem::kLength * items_.size(); |
| 86 | } |
| 87 | |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 88 | bool Tmmbr::Create(uint8_t* packet, |
| 89 | size_t* index, |
| 90 | size_t max_length, |
| 91 | RtcpPacket::PacketReadyCallback* callback) const { |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 92 | RTC_DCHECK(!items_.empty()); |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 93 | while (*index + BlockLength() > max_length) { |
| 94 | if (!OnBufferFull(packet, index, callback)) |
| 95 | return false; |
| 96 | } |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 97 | const size_t index_end = *index + BlockLength(); |
| 98 | |
| 99 | CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet, |
| 100 | index); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 101 | RTC_DCHECK_EQ(0, Rtpfb::media_ssrc()); |
danilchap | f174e3a | 2016-02-05 04:56:36 -0800 | [diff] [blame] | 102 | 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); |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |
danilchap | 7e8145f | 2016-01-11 11:49:19 -0800 | [diff] [blame] | 111 | } // namespace rtcp |
| 112 | } // namespace webrtc |