blob: fd4a4c947afcae458d2ed13d9c28a51c4ccff344 [file] [log] [blame]
Danil Chapovalov5679da12016-01-15 13:19:53 +01001/*
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/fir.h"
Danil Chapovalov5679da12016-01-15 13:19:53 +010012
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"
Danil Chapovalov5679da12016-01-15 13:19:53 +010017
18namespace webrtc {
19namespace rtcp {
danilchap0fc37d72016-05-31 01:36:37 -070020constexpr uint8_t Fir::kFeedbackMessageType;
Danil Chapovalov32e590e2016-01-22 11:04:56 +010021// 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// : :
Danil Chapovalov5679da12016-01-15 13:19:53 +010035// Full intra request (FIR) (RFC 5104).
Danil Chapovalov32e590e2016-01-22 11:04:56 +010036// The Feedback Control Information (FCI) for the Full Intra Request
37// consists of one or more FCI entries.
Danil Chapovalov5679da12016-01-15 13:19:53 +010038// FCI:
Danil Chapovalov32e590e2016-01-22 11:04:56 +010039// 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// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
eladalon8fa21c42017-06-16 07:07:47 -070046
47Fir::Fir() = default;
48
Bjorn Tereliusdfd5c4b2019-02-13 16:12:09 +010049Fir::Fir(const Fir& fir) = default;
50
eladalon8fa21c42017-06-16 07:07:47 -070051Fir::~Fir() = default;
52
danilchap0fc37d72016-05-31 01:36:37 -070053bool Fir::Parse(const CommonHeader& packet) {
54 RTC_DCHECK_EQ(packet.type(), kPacketType);
55 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
Danil Chapovalov32e590e2016-01-22 11:04:56 +010056
57 // The FCI field MUST contain one or more FIR entries.
danilchap0fc37d72016-05-31 01:36:37 -070058 if (packet.payload_size_bytes() < kCommonFeedbackLength + kFciLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LS_WARNING) << "Packet is too small to be a valid FIR packet.";
Danil Chapovalov32e590e2016-01-22 11:04:56 +010060 return false;
61 }
62
danilchap0fc37d72016-05-31 01:36:37 -070063 if ((packet.payload_size_bytes() - kCommonFeedbackLength) % kFciLength != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010064 RTC_LOG(LS_WARNING) << "Invalid size for a valid FIR packet.";
Danil Chapovalov32e590e2016-01-22 11:04:56 +010065 return false;
66 }
67
danilchap0fc37d72016-05-31 01:36:37 -070068 ParseCommonFeedback(packet.payload());
Danil Chapovalov32e590e2016-01-22 11:04:56 +010069
70 size_t number_of_fci_items =
danilchap0fc37d72016-05-31 01:36:37 -070071 (packet.payload_size_bytes() - kCommonFeedbackLength) / kFciLength;
72 const uint8_t* next_fci = packet.payload() + kCommonFeedbackLength;
Danil Chapovalov32e590e2016-01-22 11:04:56 +010073 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;
Danil Chapovalov5679da12016-01-15 13:19:53 +010080}
Danil Chapovalov5679da12016-01-15 13:19:53 +010081
eladalon8fa21c42017-06-16 07:07:47 -070082size_t Fir::BlockLength() const {
83 return kHeaderLength + kCommonFeedbackLength + kFciLength * items_.size();
84}
85
Danil Chapovalov5679da12016-01-15 13:19:53 +010086bool Fir::Create(uint8_t* packet,
87 size_t* index,
88 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010089 PacketReadyCallback callback) const {
Danil Chapovalov32e590e2016-01-22 11:04:56 +010090 RTC_DCHECK(!items_.empty());
Danil Chapovalov5679da12016-01-15 13:19:53 +010091 while (*index + BlockLength() > max_length) {
92 if (!OnBufferFull(packet, index, callback))
93 return false;
94 }
Danil Chapovalov32e590e2016-01-22 11:04:56 +010095 size_t index_end = *index + BlockLength();
96 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
97 index);
kwibergaf476c72016-11-28 15:21:39 -080098 RTC_DCHECK_EQ(Psfb::media_ssrc(), 0);
Danil Chapovalov32e590e2016-01-22 11:04:56 +010099 CreateCommonFeedback(packet + *index);
100 *index += kCommonFeedbackLength;
101
danilchap0fc37d72016-05-31 01:36:37 -0700102 constexpr uint32_t kReserved = 0;
Danil Chapovalov32e590e2016-01-22 11:04:56 +0100103 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);
Danil Chapovalov5679da12016-01-15 13:19:53 +0100110 return true;
111}
Danil Chapovalov5679da12016-01-15 13:19:53 +0100112} // namespace rtcp
113} // namespace webrtc