OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 <cmath> |
| 12 #include <memory> |
| 13 #include <vector> |
| 14 |
| 15 #include "webrtc/base/array_view.h" |
| 16 #include "webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h" |
| 17 #include "webrtc/modules/audio_coding/neteq/tools/audio_checksum.h" |
| 18 #include "webrtc/modules/audio_coding/neteq/tools/encode_neteq_input.h" |
| 19 #include "webrtc/modules/audio_coding/neteq/tools/neteq_test.h" |
| 20 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 21 |
| 22 namespace webrtc { |
| 23 namespace test { |
| 24 namespace { |
| 25 constexpr int kPayloadType = 95; |
| 26 |
| 27 class SineGenerator : public EncodeNetEqInput::Generator { |
| 28 public: |
| 29 explicit SineGenerator(int sample_rate_hz) |
| 30 : sample_rate_hz_(sample_rate_hz) {} |
| 31 |
| 32 rtc::ArrayView<const int16_t> Generate(size_t num_samples) override { |
| 33 if (samples_.size() < num_samples) { |
| 34 samples_.resize(num_samples); |
| 35 } |
| 36 |
| 37 rtc::ArrayView<int16_t> output(samples_.data(), num_samples); |
| 38 for (auto& x : output) { |
| 39 x = static_cast<int16_t>(2000.0 * std::sin(phase_)); |
| 40 phase_ += 2 * kPi * kFreqHz / sample_rate_hz_; |
| 41 } |
| 42 return output; |
| 43 } |
| 44 |
| 45 private: |
| 46 static constexpr int kFreqHz = 300; // The sinewave frequency. |
| 47 const int sample_rate_hz_; |
| 48 const double kPi = std::acos(-1); |
| 49 std::vector<int16_t> samples_; |
| 50 double phase_ = 0.0; |
| 51 }; |
| 52 |
| 53 class FuzzRtpInput : public NetEqInput { |
| 54 public: |
| 55 explicit FuzzRtpInput(rtc::ArrayView<const uint8_t> data) : data_(data) { |
| 56 AudioEncoderPcm16B::Config config; |
| 57 config.payload_type = kPayloadType; |
| 58 config.sample_rate_hz = 32000; |
| 59 std::unique_ptr<AudioEncoder> encoder(new AudioEncoderPcm16B(config)); |
| 60 std::unique_ptr<EncodeNetEqInput::Generator> generator( |
| 61 new SineGenerator(config.sample_rate_hz)); |
| 62 input_.reset(new EncodeNetEqInput(std::move(generator), std::move(encoder), |
| 63 std::numeric_limits<int64_t>::max())); |
| 64 packet_ = input_->PopPacket(); |
| 65 FuzzHeader(); |
| 66 } |
| 67 |
| 68 rtc::Optional<int64_t> NextPacketTime() const override { |
| 69 return rtc::Optional<int64_t>(packet_->time_ms); |
| 70 } |
| 71 |
| 72 rtc::Optional<int64_t> NextOutputEventTime() const override { |
| 73 return input_->NextOutputEventTime(); |
| 74 } |
| 75 |
| 76 std::unique_ptr<PacketData> PopPacket() override { |
| 77 RTC_DCHECK(packet_); |
| 78 std::unique_ptr<PacketData> packet_to_return = std::move(packet_); |
| 79 packet_ = input_->PopPacket(); |
| 80 FuzzHeader(); |
| 81 return packet_to_return; |
| 82 } |
| 83 |
| 84 void AdvanceOutputEvent() override { return input_->AdvanceOutputEvent(); } |
| 85 |
| 86 bool ended() const override { return ended_; } |
| 87 |
| 88 rtc::Optional<RTPHeader> NextHeader() const override { |
| 89 RTC_DCHECK(packet_); |
| 90 return rtc::Optional<RTPHeader>(packet_->header.header); |
| 91 } |
| 92 |
| 93 private: |
| 94 void FuzzHeader() { |
| 95 constexpr size_t kNumBytesToFuzz = 11; |
| 96 if (data_ix_ + kNumBytesToFuzz > data_.size()) { |
| 97 ended_ = true; |
| 98 return; |
| 99 } |
| 100 RTC_DCHECK(packet_); |
| 101 const size_t start_ix = data_ix_; |
| 102 packet_->header.header.payloadType = |
| 103 ByteReader<uint8_t>::ReadLittleEndian(&data_[data_ix_]); |
| 104 packet_->header.header.payloadType &= 0x7F; |
| 105 data_ix_ += sizeof(uint8_t); |
| 106 packet_->header.header.sequenceNumber = |
| 107 ByteReader<uint16_t>::ReadLittleEndian(&data_[data_ix_]); |
| 108 data_ix_ += sizeof(uint16_t); |
| 109 packet_->header.header.timestamp = |
| 110 ByteReader<uint32_t>::ReadLittleEndian(&data_[data_ix_]); |
| 111 data_ix_ += sizeof(uint32_t); |
| 112 packet_->header.header.ssrc = |
| 113 ByteReader<uint32_t>::ReadLittleEndian(&data_[data_ix_]); |
| 114 data_ix_ += sizeof(uint32_t); |
| 115 RTC_CHECK_EQ(data_ix_ - start_ix, kNumBytesToFuzz); |
| 116 } |
| 117 |
| 118 bool ended_ = false; |
| 119 rtc::ArrayView<const uint8_t> data_; |
| 120 size_t data_ix_ = 0; |
| 121 std::unique_ptr<EncodeNetEqInput> input_; |
| 122 std::unique_ptr<PacketData> packet_; |
| 123 }; |
| 124 } // namespace |
| 125 |
| 126 void FuzzOneInputTest(const uint8_t* data, size_t size) { |
| 127 std::unique_ptr<FuzzRtpInput> input( |
| 128 new FuzzRtpInput(rtc::ArrayView<const uint8_t>(data, size))); |
| 129 std::unique_ptr<AudioChecksum> output(new AudioChecksum); |
| 130 NetEqTestErrorCallback dummy_callback; // Does nothing with error callbacks. |
| 131 NetEq::Config config; |
| 132 NetEqTest::DecoderMap codecs; |
| 133 codecs[0] = std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu"); |
| 134 codecs[8] = std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma"); |
| 135 codecs[103] = std::make_pair(NetEqDecoder::kDecoderISAC, "isac"); |
| 136 codecs[104] = std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb"); |
| 137 codecs[111] = std::make_pair(NetEqDecoder::kDecoderOpus, "opus"); |
| 138 codecs[93] = std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb"); |
| 139 codecs[94] = std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb"); |
| 140 codecs[96] = |
| 141 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48"); |
| 142 codecs[9] = std::make_pair(NetEqDecoder::kDecoderG722, "g722"); |
| 143 codecs[106] = std::make_pair(NetEqDecoder::kDecoderAVT, "avt"); |
| 144 codecs[117] = std::make_pair(NetEqDecoder::kDecoderRED, "red"); |
| 145 codecs[13] = std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb"); |
| 146 codecs[98] = std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb"); |
| 147 codecs[99] = std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32"); |
| 148 codecs[100] = std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48"); |
| 149 // This is the payload type that will be used for encoding. |
| 150 codecs[kPayloadType] = |
| 151 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32"); |
| 152 NetEqTest::ExtDecoderMap ext_codecs; |
| 153 |
| 154 NetEqTest test(config, codecs, ext_codecs, std::move(input), |
| 155 std::move(output), &dummy_callback); |
| 156 test.Run(); |
| 157 } |
| 158 |
| 159 } // namespace test |
| 160 |
| 161 void FuzzOneInput(const uint8_t* data, size_t size) { |
| 162 test::FuzzOneInputTest(data, size); |
| 163 } |
| 164 |
| 165 } // namespace webrtc |
OLD | NEW |