Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 10 | |
| 11 | #include <memory> |
| 12 | |
Ying Wang | 6b33e60 | 2018-07-02 17:28:07 +0200 | [diff] [blame] | 13 | #include "modules/include/module_common_types_public.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #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 Nikolaevskiy | a5d952f | 2019-09-03 11:07:37 +0200 | [diff] [blame] | 18 | #include "rtc_base/copy_on_write_buffer.h" |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
brandtr | ece4aba | 2016-09-20 23:16:28 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | constexpr uint8_t kFecPayloadType = 96; |
| 24 | constexpr uint8_t kRedPayloadType = 97; |
| 25 | } // namespace |
| 26 | |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 27 | void FuzzOneInput(const uint8_t* data, size_t size) { |
brandtr | 869e7cd | 2016-10-31 05:27:07 -0700 | [diff] [blame] | 28 | UlpfecGenerator generator; |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 29 | size_t i = 0; |
| 30 | if (size < 4) |
| 31 | return; |
Peter Boström | d6b9d77 | 2016-04-27 00:18:41 +0200 | [diff] [blame] | 32 | FecProtectionParams params = { |
| 33 | data[i++] % 128, static_cast<int>(data[i++] % 10), kFecMaskBursty}; |
brandtr | 1743a19 | 2016-11-07 03:36:05 -0800 | [diff] [blame] | 34 | generator.SetFecParameters(params); |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 35 | uint16_t seq_num = data[i++]; |
Ying Wang | 6a9bd74 | 2018-06-15 14:54:16 +0200 | [diff] [blame] | 36 | uint16_t prev_seq_num = 0; |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 37 | 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 Nikolaevskiy | a5d952f | 2019-09-03 11:07:37 +0200 | [diff] [blame] | 42 | rtc::CopyOnWriteBuffer packet(&data[i], payload_size + rtp_header_length); |
| 43 | packet.EnsureCapacity(IP_PACKET_SIZE); |
Ying Wang | 6a9bd74 | 2018-06-15 14:54:16 +0200 | [diff] [blame] | 44 | // Make sure sequence numbers are increasing. |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 45 | ByteWriter<uint16_t>::WriteBigEndian(&packet[2], seq_num++); |
| 46 | i += payload_size + rtp_header_length; |
Peter Boström | f5b804b | 2016-01-29 16:26:43 +0100 | [diff] [blame] | 47 | const bool protect = data[i++] % 2 == 1; |
Ying Wang | 6a9bd74 | 2018-06-15 14:54:16 +0200 | [diff] [blame] | 48 | |
| 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 Wang | 6b33e60 | 2018-07-02 17:28:07 +0200 | [diff] [blame] | 52 | if (protect && IsNewerSequenceNumber(seq_num, prev_seq_num) && |
| 53 | seq_num < prev_seq_num + kUlpfecMaxMediaPackets) { |
Ilya Nikolaevskiy | a5d952f | 2019-09-03 11:07:37 +0200 | [diff] [blame] | 54 | generator.AddRtpPacketAndGenerateFec(packet, rtp_header_length); |
Ying Wang | 6a9bd74 | 2018-06-15 14:54:16 +0200 | [diff] [blame] | 55 | prev_seq_num = seq_num; |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 56 | } |
brandtr | 869e7cd | 2016-10-31 05:27:07 -0700 | [diff] [blame] | 57 | const size_t num_fec_packets = generator.NumAvailableFecPackets(); |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 58 | if (num_fec_packets > 0) { |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 59 | std::vector<std::unique_ptr<RedPacket>> fec_packets = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | generator.GetUlpfecPacketsAsRed(kRedPayloadType, kFecPayloadType, |
| 61 | 100); |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 62 | RTC_CHECK_EQ(num_fec_packets, fec_packets.size()); |
Stefan Holmer | 4c1093b | 2015-12-11 18:25:45 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | } // namespace webrtc |