Relanding "Setting up an RTP input fuzzer for NetEq"
The original CL (https://codereview.webrtc.org/2315633002) was reverted since the fuzzer depended on gflags and files in the resources folder; neither of this is allowed for a fuzzer test in Chromium. This new version streamlines the dependencies, and changes the test to generate a sinusoid input audio signal instead of reading from a file. Original commit message: This CL introduces a new fuzzer target neteq_rtp_fuzzer that manipulates the RTP header fields before inserting the packets into NetEq. A few helper classes are also introduced. BUG=webrtc:5447 CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.win:win_chromium_rel_ng;master.tryserver.chromium.android:android_compile_dbg,linux_android_rel_ng;master.tryserver.chromium.linux:linux_chromium_rel_ng;master.tryserver.chromium.mac:mac_chromium_rel_ng,ios-device Review-Url: https://codereview.webrtc.org/2384423002 Cr-Commit-Position: refs/heads/master@{#14523}
This commit is contained in:
parent
d020f3fea0
commit
58466f6d97
@ -868,6 +868,25 @@ rtc_static_library("neteq") {
|
||||
}
|
||||
}
|
||||
|
||||
# Although providing only test support, this target must be outside of the
|
||||
# rtc_include_tests conditional. The reason is that it supports fuzzer tests
|
||||
# that ultimately are built and run as a part of the Chromium ecosystem, which
|
||||
# does not set the rtc_include_tests flag.
|
||||
rtc_source_set("neteq_test_minimal") {
|
||||
testonly = true
|
||||
sources = [
|
||||
"neteq/tools/encode_neteq_input.cc",
|
||||
"neteq/tools/encode_neteq_input.h",
|
||||
"neteq/tools/neteq_test.cc",
|
||||
"neteq/tools/neteq_test.h",
|
||||
]
|
||||
|
||||
if (is_clang) {
|
||||
# Suppress warnings from the Chromium Clang plugins (bugs.webrtc.org/163).
|
||||
suppressed_configs += [ "//build/config/clang:find_bad_constructs" ]
|
||||
}
|
||||
}
|
||||
|
||||
if (rtc_include_tests) {
|
||||
rtc_source_set("acm_receive_test") {
|
||||
testonly = true
|
||||
@ -1156,8 +1175,6 @@ if (rtc_include_tests) {
|
||||
"neteq/tools/neteq_packet_source_input.h",
|
||||
"neteq/tools/neteq_replacement_input.cc",
|
||||
"neteq/tools/neteq_replacement_input.h",
|
||||
"neteq/tools/neteq_test.cc",
|
||||
"neteq/tools/neteq_test.h",
|
||||
"neteq/tools/output_audio_file.h",
|
||||
"neteq/tools/output_wav_file.h",
|
||||
"neteq/tools/packet.cc",
|
||||
@ -1180,6 +1197,7 @@ if (rtc_include_tests) {
|
||||
}
|
||||
|
||||
deps = [
|
||||
":neteq_test_minimal",
|
||||
"../../common_audio",
|
||||
"../../test:rtp_test_utils",
|
||||
"../rtp_rtcp",
|
||||
|
||||
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "webrtc/modules/audio_coding/neteq/tools/encode_neteq_input.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/base/safe_conversions.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
EncodeNetEqInput::EncodeNetEqInput(std::unique_ptr<Generator> generator,
|
||||
std::unique_ptr<AudioEncoder> encoder,
|
||||
int64_t input_duration_ms)
|
||||
: generator_(std::move(generator)),
|
||||
encoder_(std::move(encoder)),
|
||||
input_duration_ms_(input_duration_ms) {
|
||||
CreatePacket();
|
||||
}
|
||||
|
||||
rtc::Optional<int64_t> EncodeNetEqInput::NextPacketTime() const {
|
||||
RTC_DCHECK(packet_data_);
|
||||
return rtc::Optional<int64_t>(static_cast<int64_t>(packet_data_->time_ms));
|
||||
}
|
||||
|
||||
rtc::Optional<int64_t> EncodeNetEqInput::NextOutputEventTime() const {
|
||||
return rtc::Optional<int64_t>(next_output_event_ms_);
|
||||
}
|
||||
|
||||
std::unique_ptr<NetEqInput::PacketData> EncodeNetEqInput::PopPacket() {
|
||||
RTC_DCHECK(packet_data_);
|
||||
// Grab the packet to return...
|
||||
std::unique_ptr<PacketData> packet_to_return = std::move(packet_data_);
|
||||
// ... and line up the next packet for future use.
|
||||
CreatePacket();
|
||||
|
||||
return packet_to_return;
|
||||
}
|
||||
|
||||
void EncodeNetEqInput::AdvanceOutputEvent() {
|
||||
next_output_event_ms_ += kOutputPeriodMs;
|
||||
}
|
||||
|
||||
rtc::Optional<RTPHeader> EncodeNetEqInput::NextHeader() const {
|
||||
RTC_DCHECK(packet_data_);
|
||||
return rtc::Optional<RTPHeader>(packet_data_->header.header);
|
||||
}
|
||||
|
||||
void EncodeNetEqInput::CreatePacket() {
|
||||
// Create a new PacketData object.
|
||||
RTC_DCHECK(!packet_data_);
|
||||
packet_data_.reset(new NetEqInput::PacketData);
|
||||
RTC_DCHECK_EQ(packet_data_->payload.size(), 0u);
|
||||
|
||||
// Loop until we get a packet.
|
||||
AudioEncoder::EncodedInfo info;
|
||||
RTC_DCHECK(!info.send_even_if_empty);
|
||||
int num_blocks = 0;
|
||||
while (packet_data_->payload.size() == 0 && !info.send_even_if_empty) {
|
||||
const size_t num_samples = rtc::CheckedDivExact(
|
||||
static_cast<int>(encoder_->SampleRateHz() * kOutputPeriodMs), 1000);
|
||||
|
||||
info = encoder_->Encode(rtp_timestamp_, generator_->Generate(num_samples),
|
||||
&packet_data_->payload);
|
||||
|
||||
rtp_timestamp_ += rtc::checked_cast<uint32_t>(
|
||||
num_samples * encoder_->RtpTimestampRateHz() /
|
||||
encoder_->SampleRateHz());
|
||||
++num_blocks;
|
||||
}
|
||||
packet_data_->header.header.timestamp = info.encoded_timestamp;
|
||||
packet_data_->header.header.payloadType = info.payload_type;
|
||||
packet_data_->header.header.sequenceNumber = sequence_number_++;
|
||||
packet_data_->time_ms = next_packet_time_ms_;
|
||||
next_packet_time_ms_ += num_blocks * kOutputPeriodMs;
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
71
webrtc/modules/audio_coding/neteq/tools/encode_neteq_input.h
Normal file
71
webrtc/modules/audio_coding/neteq/tools/encode_neteq_input.h
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_ENCODE_NETEQ_INPUT_H_
|
||||
#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_ENCODE_NETEQ_INPUT_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "webrtc/modules/audio_coding/codecs/audio_encoder.h"
|
||||
#include "webrtc/modules/audio_coding/neteq/tools/neteq_input.h"
|
||||
#include "webrtc/modules/include/module_common_types.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
// This class provides a NetEqInput that takes audio from a generator object and
|
||||
// encodes it using a given audio encoder.
|
||||
class EncodeNetEqInput : public NetEqInput {
|
||||
public:
|
||||
// Generator class, to be provided to the EncodeNetEqInput constructor.
|
||||
class Generator {
|
||||
public:
|
||||
virtual ~Generator() = default;
|
||||
// Returns the next num_samples values from the signal generator.
|
||||
virtual rtc::ArrayView<const int16_t> Generate(size_t num_samples) = 0;
|
||||
};
|
||||
|
||||
// The source will end after the given input duration.
|
||||
EncodeNetEqInput(std::unique_ptr<Generator> generator,
|
||||
std::unique_ptr<AudioEncoder> encoder,
|
||||
int64_t input_duration_ms);
|
||||
|
||||
rtc::Optional<int64_t> NextPacketTime() const override;
|
||||
|
||||
rtc::Optional<int64_t> NextOutputEventTime() const override;
|
||||
|
||||
std::unique_ptr<PacketData> PopPacket() override;
|
||||
|
||||
void AdvanceOutputEvent() override;
|
||||
|
||||
bool ended() const override {
|
||||
return next_output_event_ms_ <= input_duration_ms_;
|
||||
}
|
||||
|
||||
rtc::Optional<RTPHeader> NextHeader() const override;
|
||||
|
||||
private:
|
||||
static constexpr int64_t kOutputPeriodMs = 10;
|
||||
|
||||
void CreatePacket();
|
||||
|
||||
std::unique_ptr<Generator> generator_;
|
||||
std::unique_ptr<AudioEncoder> encoder_;
|
||||
std::unique_ptr<PacketData> packet_data_;
|
||||
uint32_t rtp_timestamp_ = 0;
|
||||
int16_t sequence_number_ = 0;
|
||||
int64_t next_packet_time_ms_ = 0;
|
||||
int64_t next_output_event_ms_ = 0;
|
||||
const int64_t input_duration_ms_;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_ENCODE_NETEQ_INPUT_H_
|
||||
@ -65,7 +65,9 @@ class NetEqInput {
|
||||
// time).
|
||||
virtual void AdvanceOutputEvent() = 0;
|
||||
|
||||
// Returns true if the source has come to an end.
|
||||
// Returns true if the source has come to an end. An implementation must
|
||||
// eventually return true from this method, or the test will end up in an
|
||||
// infinite loop.
|
||||
virtual bool ended() const = 0;
|
||||
|
||||
// Returns the RTP header for the next packet, i.e., the packet that will be
|
||||
|
||||
@ -211,6 +211,18 @@ webrtc_fuzzer_test("turn_unwrap_fuzzer") {
|
||||
]
|
||||
}
|
||||
|
||||
webrtc_fuzzer_test("neteq_rtp_fuzzer") {
|
||||
sources = [
|
||||
"neteq_rtp_fuzzer.cc",
|
||||
]
|
||||
deps = [
|
||||
"../../modules/audio_coding:neteq",
|
||||
"../../modules/audio_coding:neteq_test_minimal",
|
||||
"../../modules/audio_coding:pcm16b",
|
||||
"../../modules/rtp_rtcp",
|
||||
]
|
||||
}
|
||||
|
||||
webrtc_fuzzer_test("sdp_parser_fuzzer") {
|
||||
sources = [
|
||||
"sdp_parser_fuzzer.cc",
|
||||
|
||||
165
webrtc/test/fuzzers/neteq_rtp_fuzzer.cc
Normal file
165
webrtc/test/fuzzers/neteq_rtp_fuzzer.cc
Normal file
@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "webrtc/base/array_view.h"
|
||||
#include "webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
|
||||
#include "webrtc/modules/audio_coding/neteq/tools/audio_checksum.h"
|
||||
#include "webrtc/modules/audio_coding/neteq/tools/encode_neteq_input.h"
|
||||
#include "webrtc/modules/audio_coding/neteq/tools/neteq_test.h"
|
||||
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
namespace {
|
||||
constexpr int kPayloadType = 95;
|
||||
|
||||
class SineGenerator : public EncodeNetEqInput::Generator {
|
||||
public:
|
||||
explicit SineGenerator(int sample_rate_hz)
|
||||
: sample_rate_hz_(sample_rate_hz) {}
|
||||
|
||||
rtc::ArrayView<const int16_t> Generate(size_t num_samples) override {
|
||||
if (samples_.size() < num_samples) {
|
||||
samples_.resize(num_samples);
|
||||
}
|
||||
|
||||
rtc::ArrayView<int16_t> output(samples_.data(), num_samples);
|
||||
for (auto& x : output) {
|
||||
x = static_cast<int16_t>(2000.0 * std::sin(phase_));
|
||||
phase_ += 2 * kPi * kFreqHz / sample_rate_hz_;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr int kFreqHz = 300; // The sinewave frequency.
|
||||
const int sample_rate_hz_;
|
||||
const double kPi = std::acos(-1);
|
||||
std::vector<int16_t> samples_;
|
||||
double phase_ = 0.0;
|
||||
};
|
||||
|
||||
class FuzzRtpInput : public NetEqInput {
|
||||
public:
|
||||
explicit FuzzRtpInput(rtc::ArrayView<const uint8_t> data) : data_(data) {
|
||||
AudioEncoderPcm16B::Config config;
|
||||
config.payload_type = kPayloadType;
|
||||
config.sample_rate_hz = 32000;
|
||||
std::unique_ptr<AudioEncoder> encoder(new AudioEncoderPcm16B(config));
|
||||
std::unique_ptr<EncodeNetEqInput::Generator> generator(
|
||||
new SineGenerator(config.sample_rate_hz));
|
||||
input_.reset(new EncodeNetEqInput(std::move(generator), std::move(encoder),
|
||||
std::numeric_limits<int64_t>::max()));
|
||||
packet_ = input_->PopPacket();
|
||||
FuzzHeader();
|
||||
}
|
||||
|
||||
rtc::Optional<int64_t> NextPacketTime() const override {
|
||||
return rtc::Optional<int64_t>(packet_->time_ms);
|
||||
}
|
||||
|
||||
rtc::Optional<int64_t> NextOutputEventTime() const override {
|
||||
return input_->NextOutputEventTime();
|
||||
}
|
||||
|
||||
std::unique_ptr<PacketData> PopPacket() override {
|
||||
RTC_DCHECK(packet_);
|
||||
std::unique_ptr<PacketData> packet_to_return = std::move(packet_);
|
||||
packet_ = input_->PopPacket();
|
||||
FuzzHeader();
|
||||
return packet_to_return;
|
||||
}
|
||||
|
||||
void AdvanceOutputEvent() override { return input_->AdvanceOutputEvent(); }
|
||||
|
||||
bool ended() const override { return ended_; }
|
||||
|
||||
rtc::Optional<RTPHeader> NextHeader() const override {
|
||||
RTC_DCHECK(packet_);
|
||||
return rtc::Optional<RTPHeader>(packet_->header.header);
|
||||
}
|
||||
|
||||
private:
|
||||
void FuzzHeader() {
|
||||
constexpr size_t kNumBytesToFuzz = 11;
|
||||
if (data_ix_ + kNumBytesToFuzz > data_.size()) {
|
||||
ended_ = true;
|
||||
return;
|
||||
}
|
||||
RTC_DCHECK(packet_);
|
||||
const size_t start_ix = data_ix_;
|
||||
packet_->header.header.payloadType =
|
||||
ByteReader<uint8_t>::ReadLittleEndian(&data_[data_ix_]);
|
||||
packet_->header.header.payloadType &= 0x7F;
|
||||
data_ix_ += sizeof(uint8_t);
|
||||
packet_->header.header.sequenceNumber =
|
||||
ByteReader<uint16_t>::ReadLittleEndian(&data_[data_ix_]);
|
||||
data_ix_ += sizeof(uint16_t);
|
||||
packet_->header.header.timestamp =
|
||||
ByteReader<uint32_t>::ReadLittleEndian(&data_[data_ix_]);
|
||||
data_ix_ += sizeof(uint32_t);
|
||||
packet_->header.header.ssrc =
|
||||
ByteReader<uint32_t>::ReadLittleEndian(&data_[data_ix_]);
|
||||
data_ix_ += sizeof(uint32_t);
|
||||
RTC_CHECK_EQ(data_ix_ - start_ix, kNumBytesToFuzz);
|
||||
}
|
||||
|
||||
bool ended_ = false;
|
||||
rtc::ArrayView<const uint8_t> data_;
|
||||
size_t data_ix_ = 0;
|
||||
std::unique_ptr<EncodeNetEqInput> input_;
|
||||
std::unique_ptr<PacketData> packet_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
void FuzzOneInputTest(const uint8_t* data, size_t size) {
|
||||
std::unique_ptr<FuzzRtpInput> input(
|
||||
new FuzzRtpInput(rtc::ArrayView<const uint8_t>(data, size)));
|
||||
std::unique_ptr<AudioChecksum> output(new AudioChecksum);
|
||||
NetEqTestErrorCallback dummy_callback; // Does nothing with error callbacks.
|
||||
NetEq::Config config;
|
||||
NetEqTest::DecoderMap codecs;
|
||||
codecs[0] = std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu");
|
||||
codecs[8] = std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma");
|
||||
codecs[103] = std::make_pair(NetEqDecoder::kDecoderISAC, "isac");
|
||||
codecs[104] = std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb");
|
||||
codecs[111] = std::make_pair(NetEqDecoder::kDecoderOpus, "opus");
|
||||
codecs[93] = std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb");
|
||||
codecs[94] = std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb");
|
||||
codecs[96] =
|
||||
std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48");
|
||||
codecs[9] = std::make_pair(NetEqDecoder::kDecoderG722, "g722");
|
||||
codecs[106] = std::make_pair(NetEqDecoder::kDecoderAVT, "avt");
|
||||
codecs[117] = std::make_pair(NetEqDecoder::kDecoderRED, "red");
|
||||
codecs[13] = std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb");
|
||||
codecs[98] = std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb");
|
||||
codecs[99] = std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32");
|
||||
codecs[100] = std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48");
|
||||
// This is the payload type that will be used for encoding.
|
||||
codecs[kPayloadType] =
|
||||
std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32");
|
||||
NetEqTest::ExtDecoderMap ext_codecs;
|
||||
|
||||
NetEqTest test(config, codecs, ext_codecs, std::move(input),
|
||||
std::move(output), &dummy_callback);
|
||||
test.Run();
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
|
||||
void FuzzOneInput(const uint8_t* data, size_t size) {
|
||||
test::FuzzOneInputTest(data, size);
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
Loading…
x
Reference in New Issue
Block a user