1 /*
2 * Copyright (c) 2019 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 #include <stddef.h>
11 #include <stdint.h>
12
13 #include "api/video/video_frame_type.h"
14 #include "modules/rtp_rtcp/source/rtp_format.h"
15 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
16 #include "modules/rtp_rtcp/source/rtp_packetizer_av1.h"
17 #include "rtc_base/checks.h"
18 #include "test/fuzzers/fuzz_data_helper.h"
19
20 namespace webrtc {
FuzzOneInput(const uint8_t * data,size_t size)21 void FuzzOneInput(const uint8_t* data, size_t size) {
22 test::FuzzDataHelper fuzz_input(rtc::MakeArrayView(data, size));
23
24 RtpPacketizer::PayloadSizeLimits limits;
25 limits.max_payload_len = 1200;
26 // Read uint8_t to be sure reduction_lens are much smaller than
27 // max_payload_len and thus limits structure is valid.
28 limits.first_packet_reduction_len = fuzz_input.ReadOrDefaultValue<uint8_t>(0);
29 limits.last_packet_reduction_len = fuzz_input.ReadOrDefaultValue<uint8_t>(0);
30 limits.single_packet_reduction_len =
31 fuzz_input.ReadOrDefaultValue<uint8_t>(0);
32 const VideoFrameType kFrameTypes[] = {VideoFrameType::kVideoFrameKey,
33 VideoFrameType::kVideoFrameDelta};
34 VideoFrameType frame_type = fuzz_input.SelectOneOf(kFrameTypes);
35
36 // Main function under test: RtpPacketizerAv1's constructor.
37 RtpPacketizerAv1 packetizer(fuzz_input.ReadByteArray(fuzz_input.BytesLeft()),
38 limits, frame_type);
39
40 size_t num_packets = packetizer.NumPackets();
41 if (num_packets == 0) {
42 return;
43 }
44 // When packetization was successful, validate NextPacket function too.
45 // While at it, check that packets respect the payload size limits.
46 RtpPacketToSend rtp_packet(nullptr);
47 // Single packet.
48 if (num_packets == 1) {
49 RTC_CHECK(packetizer.NextPacket(&rtp_packet));
50 RTC_CHECK_LE(rtp_packet.payload_size(),
51 limits.max_payload_len - limits.single_packet_reduction_len);
52 return;
53 }
54 // First packet.
55 RTC_CHECK(packetizer.NextPacket(&rtp_packet));
56 RTC_CHECK_LE(rtp_packet.payload_size(),
57 limits.max_payload_len - limits.first_packet_reduction_len);
58 // Middle packets.
59 for (size_t i = 1; i < num_packets - 1; ++i) {
60 RTC_CHECK(packetizer.NextPacket(&rtp_packet))
61 << "Failed to get packet#" << i;
62 RTC_CHECK_LE(rtp_packet.payload_size(), limits.max_payload_len)
63 << "Packet #" << i << " exceeds it's limit";
64 }
65 // Last packet.
66 RTC_CHECK(packetizer.NextPacket(&rtp_packet));
67 RTC_CHECK_LE(rtp_packet.payload_size(),
68 limits.max_payload_len - limits.last_packet_reduction_len);
69 }
70 } // namespace webrtc
71