blob: 5beb16fc459136748c22cc78d6b135bd14172b36 [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
11#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/fir.h"
12
Danil Chapovalov5679da12016-01-15 13:19:53 +010013#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
danilchap0fc37d72016-05-31 01:36:37 -070014#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/rtc_base/checks.h"
16#include "webrtc/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
49Fir::~Fir() = default;
50
danilchap0fc37d72016-05-31 01:36:37 -070051bool Fir::Parse(const CommonHeader& packet) {
52 RTC_DCHECK_EQ(packet.type(), kPacketType);
53 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
Danil Chapovalov32e590e2016-01-22 11:04:56 +010054
55 // The FCI field MUST contain one or more FIR entries.
danilchap0fc37d72016-05-31 01:36:37 -070056 if (packet.payload_size_bytes() < kCommonFeedbackLength + kFciLength) {
Danil Chapovalov32e590e2016-01-22 11:04:56 +010057 LOG(LS_WARNING) << "Packet is too small to be a valid FIR packet.";
58 return false;
59 }
60
danilchap0fc37d72016-05-31 01:36:37 -070061 if ((packet.payload_size_bytes() - kCommonFeedbackLength) % kFciLength != 0) {
Danil Chapovalov32e590e2016-01-22 11:04:56 +010062 LOG(LS_WARNING) << "Invalid size for a valid FIR packet.";
63 return false;
64 }
65
danilchap0fc37d72016-05-31 01:36:37 -070066 ParseCommonFeedback(packet.payload());
Danil Chapovalov32e590e2016-01-22 11:04:56 +010067
68 size_t number_of_fci_items =
danilchap0fc37d72016-05-31 01:36:37 -070069 (packet.payload_size_bytes() - kCommonFeedbackLength) / kFciLength;
70 const uint8_t* next_fci = packet.payload() + kCommonFeedbackLength;
Danil Chapovalov32e590e2016-01-22 11:04:56 +010071 items_.resize(number_of_fci_items);
72 for (Request& request : items_) {
73 request.ssrc = ByteReader<uint32_t>::ReadBigEndian(next_fci);
74 request.seq_nr = ByteReader<uint8_t>::ReadBigEndian(next_fci + 4);
75 next_fci += kFciLength;
76 }
77 return true;
Danil Chapovalov5679da12016-01-15 13:19:53 +010078}
Danil Chapovalov5679da12016-01-15 13:19:53 +010079
eladalon8fa21c42017-06-16 07:07:47 -070080size_t Fir::BlockLength() const {
81 return kHeaderLength + kCommonFeedbackLength + kFciLength * items_.size();
82}
83
Danil Chapovalov5679da12016-01-15 13:19:53 +010084bool Fir::Create(uint8_t* packet,
85 size_t* index,
86 size_t max_length,
87 RtcpPacket::PacketReadyCallback* callback) const {
Danil Chapovalov32e590e2016-01-22 11:04:56 +010088 RTC_DCHECK(!items_.empty());
Danil Chapovalov5679da12016-01-15 13:19:53 +010089 while (*index + BlockLength() > max_length) {
90 if (!OnBufferFull(packet, index, callback))
91 return false;
92 }
Danil Chapovalov32e590e2016-01-22 11:04:56 +010093 size_t index_end = *index + BlockLength();
94 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
95 index);
kwibergaf476c72016-11-28 15:21:39 -080096 RTC_DCHECK_EQ(Psfb::media_ssrc(), 0);
Danil Chapovalov32e590e2016-01-22 11:04:56 +010097 CreateCommonFeedback(packet + *index);
98 *index += kCommonFeedbackLength;
99
danilchap0fc37d72016-05-31 01:36:37 -0700100 constexpr uint32_t kReserved = 0;
Danil Chapovalov32e590e2016-01-22 11:04:56 +0100101 for (const Request& request : items_) {
102 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, request.ssrc);
103 ByteWriter<uint8_t>::WriteBigEndian(packet + *index + 4, request.seq_nr);
104 ByteWriter<uint32_t, 3>::WriteBigEndian(packet + *index + 5, kReserved);
105 *index += kFciLength;
106 }
107 RTC_CHECK_EQ(*index, index_end);
Danil Chapovalov5679da12016-01-15 13:19:53 +0100108 return true;
109}
Danil Chapovalov5679da12016-01-15 13:19:53 +0100110} // namespace rtcp
111} // namespace webrtc