blob: 306f7a0da9f3197b4878bc6751f30320c870fbf4 [file] [log] [blame]
Stefan Holmer4c1093b2015-12-11 18:25:45 +01001/*
2 * Copyright (c) 2015 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 */
kwibergbfefb032016-05-01 14:53:46 -070010
11#include <memory>
12
Ying Wang6b33e602018-07-02 17:28:07 +020013#include "modules/include/module_common_types_public.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/rtp_rtcp/source/byte_io.h"
15#include "modules/rtp_rtcp/source/fec_test_helper.h"
16#include "modules/rtp_rtcp/source/ulpfec_generator.h"
17#include "rtc_base/checks.h"
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +020018#include "rtc_base/copy_on_write_buffer.h"
Stefan Holmer4c1093b2015-12-11 18:25:45 +010019
20namespace webrtc {
21
brandtrece4aba2016-09-20 23:16:28 -070022namespace {
23constexpr uint8_t kFecPayloadType = 96;
24constexpr uint8_t kRedPayloadType = 97;
25} // namespace
26
Stefan Holmer4c1093b2015-12-11 18:25:45 +010027void FuzzOneInput(const uint8_t* data, size_t size) {
brandtr869e7cd2016-10-31 05:27:07 -070028 UlpfecGenerator generator;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010029 size_t i = 0;
30 if (size < 4)
31 return;
Peter Boströmd6b9d772016-04-27 00:18:41 +020032 FecProtectionParams params = {
33 data[i++] % 128, static_cast<int>(data[i++] % 10), kFecMaskBursty};
brandtr1743a192016-11-07 03:36:05 -080034 generator.SetFecParameters(params);
Stefan Holmer4c1093b2015-12-11 18:25:45 +010035 uint16_t seq_num = data[i++];
Ying Wang6a9bd742018-06-15 14:54:16 +020036 uint16_t prev_seq_num = 0;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010037 while (i + 3 < size) {
38 size_t rtp_header_length = data[i++] % 10 + 12;
39 size_t payload_size = data[i++] % 10;
40 if (i + payload_size + rtp_header_length + 2 > size)
41 break;
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +020042 rtc::CopyOnWriteBuffer packet(&data[i], payload_size + rtp_header_length);
43 packet.EnsureCapacity(IP_PACKET_SIZE);
Ying Wang6a9bd742018-06-15 14:54:16 +020044 // Make sure sequence numbers are increasing.
Stefan Holmer4c1093b2015-12-11 18:25:45 +010045 ByteWriter<uint16_t>::WriteBigEndian(&packet[2], seq_num++);
46 i += payload_size + rtp_header_length;
Peter Boströmf5b804b2016-01-29 16:26:43 +010047 const bool protect = data[i++] % 2 == 1;
Ying Wang6a9bd742018-06-15 14:54:16 +020048
49 // Check the sequence numbers are monotonic. In rare case the packets number
50 // may loop around and in the same FEC-protected group the packet sequence
51 // number became out of order.
Ying Wang6b33e602018-07-02 17:28:07 +020052 if (protect && IsNewerSequenceNumber(seq_num, prev_seq_num) &&
53 seq_num < prev_seq_num + kUlpfecMaxMediaPackets) {
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +020054 generator.AddRtpPacketAndGenerateFec(packet, rtp_header_length);
Ying Wang6a9bd742018-06-15 14:54:16 +020055 prev_seq_num = seq_num;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010056 }
brandtr869e7cd2016-10-31 05:27:07 -070057 const size_t num_fec_packets = generator.NumAvailableFecPackets();
Stefan Holmer4c1093b2015-12-11 18:25:45 +010058 if (num_fec_packets > 0) {
brandtr74811e52016-08-10 00:51:50 -070059 std::vector<std::unique_ptr<RedPacket>> fec_packets =
Yves Gerey665174f2018-06-19 15:03:05 +020060 generator.GetUlpfecPacketsAsRed(kRedPayloadType, kFecPayloadType,
61 100);
Stefan Holmer4c1093b2015-12-11 18:25:45 +010062 RTC_CHECK_EQ(num_fec_packets, fec_packets.size());
Stefan Holmer4c1093b2015-12-11 18:25:45 +010063 }
64 }
65}
66} // namespace webrtc