blob: 2536b54ac0edf756df4f2cb79dec8a66f9f124cc [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/rtp_rtcp/source/byte_io.h"
14#include "modules/rtp_rtcp/source/fec_test_helper.h"
15#include "modules/rtp_rtcp/source/ulpfec_generator.h"
16#include "rtc_base/checks.h"
Stefan Holmer4c1093b2015-12-11 18:25:45 +010017
18namespace webrtc {
19
brandtrece4aba2016-09-20 23:16:28 -070020namespace {
21constexpr uint8_t kFecPayloadType = 96;
22constexpr uint8_t kRedPayloadType = 97;
23} // namespace
24
Stefan Holmer4c1093b2015-12-11 18:25:45 +010025void FuzzOneInput(const uint8_t* data, size_t size) {
brandtr869e7cd2016-10-31 05:27:07 -070026 UlpfecGenerator generator;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010027 size_t i = 0;
28 if (size < 4)
29 return;
Peter Boströmd6b9d772016-04-27 00:18:41 +020030 FecProtectionParams params = {
31 data[i++] % 128, static_cast<int>(data[i++] % 10), kFecMaskBursty};
brandtr1743a192016-11-07 03:36:05 -080032 generator.SetFecParameters(params);
Stefan Holmer4c1093b2015-12-11 18:25:45 +010033 uint16_t seq_num = data[i++];
Ying Wang6a9bd742018-06-15 14:54:16 +020034 uint16_t prev_seq_num = 0;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010035 while (i + 3 < size) {
36 size_t rtp_header_length = data[i++] % 10 + 12;
37 size_t payload_size = data[i++] % 10;
38 if (i + payload_size + rtp_header_length + 2 > size)
39 break;
kwibergbfefb032016-05-01 14:53:46 -070040 std::unique_ptr<uint8_t[]> packet(
Stefan Holmer4c1093b2015-12-11 18:25:45 +010041 new uint8_t[payload_size + rtp_header_length]);
42 memcpy(packet.get(), &data[i], payload_size + rtp_header_length);
Ying Wang6a9bd742018-06-15 14:54:16 +020043
44 // 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.
52 if (protect && static_cast<uint16_t>(seq_num - prev_seq_num) <
53 kUlpfecMaxMediaPackets) {
brandtr869e7cd2016-10-31 05:27:07 -070054 generator.AddRtpPacketAndGenerateFec(packet.get(), payload_size,
55 rtp_header_length);
Ying Wang6a9bd742018-06-15 14:54:16 +020056 prev_seq_num = seq_num;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010057 }
brandtr869e7cd2016-10-31 05:27:07 -070058 const size_t num_fec_packets = generator.NumAvailableFecPackets();
Stefan Holmer4c1093b2015-12-11 18:25:45 +010059 if (num_fec_packets > 0) {
brandtr74811e52016-08-10 00:51:50 -070060 std::vector<std::unique_ptr<RedPacket>> fec_packets =
Yves Gerey665174f2018-06-19 15:03:05 +020061 generator.GetUlpfecPacketsAsRed(kRedPayloadType, kFecPayloadType,
62 100);
Stefan Holmer4c1093b2015-12-11 18:25:45 +010063 RTC_CHECK_EQ(num_fec_packets, fec_packets.size());
Stefan Holmer4c1093b2015-12-11 18:25:45 +010064 }
65 }
66}
67} // namespace webrtc