blob: ce9d8fdbc8dd8d6d47f6f2303fcfb28cf88efe83 [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"
Stefan Holmer4c1093b2015-12-11 18:25:45 +010018
19namespace webrtc {
20
brandtrece4aba2016-09-20 23:16:28 -070021namespace {
22constexpr uint8_t kFecPayloadType = 96;
23constexpr uint8_t kRedPayloadType = 97;
24} // namespace
25
Stefan Holmer4c1093b2015-12-11 18:25:45 +010026void FuzzOneInput(const uint8_t* data, size_t size) {
brandtr869e7cd2016-10-31 05:27:07 -070027 UlpfecGenerator generator;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010028 size_t i = 0;
29 if (size < 4)
30 return;
Peter Boströmd6b9d772016-04-27 00:18:41 +020031 FecProtectionParams params = {
32 data[i++] % 128, static_cast<int>(data[i++] % 10), kFecMaskBursty};
brandtr1743a192016-11-07 03:36:05 -080033 generator.SetFecParameters(params);
Stefan Holmer4c1093b2015-12-11 18:25:45 +010034 uint16_t seq_num = data[i++];
Ying Wang6a9bd742018-06-15 14:54:16 +020035 uint16_t prev_seq_num = 0;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010036 while (i + 3 < size) {
37 size_t rtp_header_length = data[i++] % 10 + 12;
38 size_t payload_size = data[i++] % 10;
39 if (i + payload_size + rtp_header_length + 2 > size)
40 break;
kwibergbfefb032016-05-01 14:53:46 -070041 std::unique_ptr<uint8_t[]> packet(
Stefan Holmer4c1093b2015-12-11 18:25:45 +010042 new uint8_t[payload_size + rtp_header_length]);
43 memcpy(packet.get(), &data[i], payload_size + rtp_header_length);
Ying Wang6a9bd742018-06-15 14:54:16 +020044
45 // Make sure sequence numbers are increasing.
Stefan Holmer4c1093b2015-12-11 18:25:45 +010046 ByteWriter<uint16_t>::WriteBigEndian(&packet[2], seq_num++);
47 i += payload_size + rtp_header_length;
Peter Boströmf5b804b2016-01-29 16:26:43 +010048 const bool protect = data[i++] % 2 == 1;
Ying Wang6a9bd742018-06-15 14:54:16 +020049
50 // Check the sequence numbers are monotonic. In rare case the packets number
51 // may loop around and in the same FEC-protected group the packet sequence
52 // number became out of order.
Ying Wang6b33e602018-07-02 17:28:07 +020053 if (protect && IsNewerSequenceNumber(seq_num, prev_seq_num) &&
54 seq_num < prev_seq_num + kUlpfecMaxMediaPackets) {
brandtr869e7cd2016-10-31 05:27:07 -070055 generator.AddRtpPacketAndGenerateFec(packet.get(), payload_size,
56 rtp_header_length);
Ying Wang6a9bd742018-06-15 14:54:16 +020057 prev_seq_num = seq_num;
Stefan Holmer4c1093b2015-12-11 18:25:45 +010058 }
brandtr869e7cd2016-10-31 05:27:07 -070059 const size_t num_fec_packets = generator.NumAvailableFecPackets();
Stefan Holmer4c1093b2015-12-11 18:25:45 +010060 if (num_fec_packets > 0) {
brandtr74811e52016-08-10 00:51:50 -070061 std::vector<std::unique_ptr<RedPacket>> fec_packets =
Yves Gerey665174f2018-06-19 15:03:05 +020062 generator.GetUlpfecPacketsAsRed(kRedPayloadType, kFecPayloadType,
63 100);
Stefan Holmer4c1093b2015-12-11 18:25:45 +010064 RTC_CHECK_EQ(num_fec_packets, fec_packets.size());
Stefan Holmer4c1093b2015-12-11 18:25:45 +010065 }
66 }
67}
68} // namespace webrtc