fir.cc (4454B)
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/fir.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 16 #include "modules/rtp_rtcp/source/byte_io.h" 17 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h" 18 #include "modules/rtp_rtcp/source/rtcp_packet/psfb.h" 19 #include "rtc_base/checks.h" 20 #include "rtc_base/logging.h" 21 22 namespace webrtc { 23 namespace rtcp { 24 // RFC 4585: Feedback format. 25 // Common packet format: 26 // 27 // 0 1 2 3 28 // 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 29 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 30 // |V=2|P| FMT | PT | length | 31 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 32 // | SSRC of packet sender | 33 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 34 // | SSRC of media source (unused) = 0 | 35 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 36 // : Feedback Control Information (FCI) : 37 // : : 38 // Full intra request (FIR) (RFC 5104). 39 // The Feedback Control Information (FCI) for the Full Intra Request 40 // consists of one or more FCI entries. 41 // FCI: 42 // 0 1 2 3 43 // 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 44 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 45 // | SSRC | 46 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 47 // | Seq nr. | Reserved = 0 | 48 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 49 50 Fir::Fir() = default; 51 52 Fir::Fir(const Fir& fir) = default; 53 54 Fir::~Fir() = default; 55 56 bool Fir::Parse(const CommonHeader& packet) { 57 RTC_DCHECK_EQ(packet.type(), kPacketType); 58 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType); 59 60 // The FCI field MUST contain one or more FIR entries. 61 if (packet.payload_size_bytes() < kCommonFeedbackLength + kFciLength) { 62 RTC_LOG(LS_WARNING) << "Packet is too small to be a valid FIR packet."; 63 return false; 64 } 65 66 if ((packet.payload_size_bytes() - kCommonFeedbackLength) % kFciLength != 0) { 67 RTC_LOG(LS_WARNING) << "Invalid size for a valid FIR packet."; 68 return false; 69 } 70 71 ParseCommonFeedback(packet.payload()); 72 73 size_t number_of_fci_items = 74 (packet.payload_size_bytes() - kCommonFeedbackLength) / kFciLength; 75 const uint8_t* next_fci = packet.payload() + kCommonFeedbackLength; 76 items_.resize(number_of_fci_items); 77 for (Request& request : items_) { 78 request.ssrc = ByteReader<uint32_t>::ReadBigEndian(next_fci); 79 request.seq_nr = ByteReader<uint8_t>::ReadBigEndian(next_fci + 4); 80 next_fci += kFciLength; 81 } 82 return true; 83 } 84 85 size_t Fir::BlockLength() const { 86 return kHeaderLength + kCommonFeedbackLength + kFciLength * items_.size(); 87 } 88 89 bool Fir::Create(uint8_t* packet, 90 size_t* index, 91 size_t max_length, 92 PacketReadyCallback callback) const { 93 RTC_DCHECK(!items_.empty()); 94 while (*index + BlockLength() > max_length) { 95 if (!OnBufferFull(packet, index, callback)) 96 return false; 97 } 98 size_t index_end = *index + BlockLength(); 99 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet, 100 index); 101 RTC_DCHECK_EQ(Psfb::media_ssrc(), 0); 102 CreateCommonFeedback(packet + *index); 103 *index += kCommonFeedbackLength; 104 105 constexpr uint32_t kReserved = 0; 106 for (const Request& request : items_) { 107 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, request.ssrc); 108 ByteWriter<uint8_t>::WriteBigEndian(packet + *index + 4, request.seq_nr); 109 ByteWriter<uint32_t, 3>::WriteBigEndian(packet + *index + 5, kReserved); 110 *index += kFciLength; 111 } 112 RTC_CHECK_EQ(*index, index_end); 113 return true; 114 } 115 } // namespace rtcp 116 } // namespace webrtc