Alessio Bazzica | 02b76bd | 2020-02-27 15:32:56 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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 | |
| 11 | #include "test/fuzzers/audio_encoder_fuzzer.h" |
| 12 | |
| 13 | #include <cstring> |
| 14 | |
| 15 | #include "rtc_base/buffer.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "test/fuzzers/fuzz_data_helper.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // This function reads bytes from |data_view|, interprets them as RTP timestamp |
| 22 | // and input samples, and sends them for encoding. The process continues until |
| 23 | // no more data is available. |
| 24 | void FuzzAudioEncoder(rtc::ArrayView<const uint8_t> data_view, |
| 25 | std::unique_ptr<AudioEncoder> encoder) { |
| 26 | test::FuzzDataHelper data(data_view); |
| 27 | const size_t block_size_samples = |
| 28 | encoder->SampleRateHz() / 100 * encoder->NumChannels(); |
| 29 | const size_t block_size_bytes = block_size_samples * sizeof(int16_t); |
| 30 | if (data_view.size() / block_size_bytes > 1000) { |
| 31 | // If the size of the fuzzer data is more than 1000 input blocks (i.e., more |
| 32 | // than 10 seconds), then don't fuzz at all for the fear of timing out. |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | rtc::BufferT<int16_t> input_aligned(block_size_samples); |
| 37 | rtc::Buffer encoded; |
| 38 | |
| 39 | // Each round in the loop below will need one block of samples + a 32-bit |
| 40 | // timestamp from the fuzzer input. |
| 41 | const size_t bytes_to_read = block_size_bytes + sizeof(uint32_t); |
| 42 | while (data.CanReadBytes(bytes_to_read)) { |
| 43 | const uint32_t timestamp = data.Read<uint32_t>(); |
| 44 | auto byte_array = data.ReadByteArray(block_size_bytes); |
| 45 | // Align the data by copying to another array. |
| 46 | RTC_DCHECK_EQ(input_aligned.size() * sizeof(int16_t), |
| 47 | byte_array.size() * sizeof(uint8_t)); |
| 48 | memcpy(input_aligned.data(), byte_array.data(), byte_array.size()); |
| 49 | auto info = encoder->Encode(timestamp, input_aligned, &encoded); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | } // namespace webrtc |