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 "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"
17
18 namespace webrtc {
19 namespace rtcp {
20 constexpr uint8_t Fir::kFeedbackMessageType;
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 // : :
35 // Full intra request (FIR) (RFC 5104).
36 // The Feedback Control Information (FCI) for the Full Intra Request
37 // consists of one or more FCI entries.
38 // FCI:
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 // | Seq nr. | Reserved = 0 |
45 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46
47 Fir::Fir() = default;
48
49 Fir::Fir(const Fir& fir) = default;
50
51 Fir::~Fir() = default;
52
Parse(const CommonHeader & packet)53 bool Fir::Parse(const CommonHeader& packet) {
54 RTC_DCHECK_EQ(packet.type(), kPacketType);
55 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
56
57 // The FCI field MUST contain one or more FIR entries.
58 if (packet.payload_size_bytes() < kCommonFeedbackLength + kFciLength) {
59 RTC_LOG(LS_WARNING) << "Packet is too small to be a valid FIR packet.";
60 return false;
61 }
62
63 if ((packet.payload_size_bytes() - kCommonFeedbackLength) % kFciLength != 0) {
64 RTC_LOG(LS_WARNING) << "Invalid size for a valid FIR packet.";
65 return false;
66 }
67
68 ParseCommonFeedback(packet.payload());
69
70 size_t number_of_fci_items =
71 (packet.payload_size_bytes() - kCommonFeedbackLength) / kFciLength;
72 const uint8_t* next_fci = packet.payload() + kCommonFeedbackLength;
73 items_.resize(number_of_fci_items);
74 for (Request& request : items_) {
75 request.ssrc = ByteReader<uint32_t>::ReadBigEndian(next_fci);
76 request.seq_nr = ByteReader<uint8_t>::ReadBigEndian(next_fci + 4);
77 next_fci += kFciLength;
78 }
79 return true;
80 }
81
BlockLength() const82 size_t Fir::BlockLength() const {
83 return kHeaderLength + kCommonFeedbackLength + kFciLength * items_.size();
84 }
85
Create(uint8_t * packet,size_t * index,size_t max_length,PacketReadyCallback callback) const86 bool Fir::Create(uint8_t* packet,
87 size_t* index,
88 size_t max_length,
89 PacketReadyCallback callback) const {
90 RTC_DCHECK(!items_.empty());
91 while (*index + BlockLength() > max_length) {
92 if (!OnBufferFull(packet, index, callback))
93 return false;
94 }
95 size_t index_end = *index + BlockLength();
96 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
97 index);
98 RTC_DCHECK_EQ(Psfb::media_ssrc(), 0);
99 CreateCommonFeedback(packet + *index);
100 *index += kCommonFeedbackLength;
101
102 constexpr uint32_t kReserved = 0;
103 for (const Request& request : items_) {
104 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, request.ssrc);
105 ByteWriter<uint8_t>::WriteBigEndian(packet + *index + 4, request.seq_nr);
106 ByteWriter<uint32_t, 3>::WriteBigEndian(packet + *index + 5, kReserved);
107 *index += kFciLength;
108 }
109 RTC_CHECK_EQ(*index, index_end);
110 return true;
111 }
112 } // namespace rtcp
113 } // namespace webrtc
114