henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 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 | |
kwiberg | 529662a | 2017-09-04 05:43:17 -0700 | [diff] [blame] | 15 | #include "webrtc/api/array_view.h" |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 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_); |
henrik.lundin | 246ef3e | 2017-04-24 09:14:32 -0700 | [diff] [blame] | 90 | return rtc::Optional<RTPHeader>(packet_->header); |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 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_; |
henrik.lundin | 246ef3e | 2017-04-24 09:14:32 -0700 | [diff] [blame] | 102 | packet_->header.payloadType = |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 103 | ByteReader<uint8_t>::ReadLittleEndian(&data_[data_ix_]); |
henrik.lundin | 246ef3e | 2017-04-24 09:14:32 -0700 | [diff] [blame] | 104 | packet_->header.payloadType &= 0x7F; |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 105 | data_ix_ += sizeof(uint8_t); |
henrik.lundin | 246ef3e | 2017-04-24 09:14:32 -0700 | [diff] [blame] | 106 | packet_->header.sequenceNumber = |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 107 | ByteReader<uint16_t>::ReadLittleEndian(&data_[data_ix_]); |
| 108 | data_ix_ += sizeof(uint16_t); |
henrik.lundin | 246ef3e | 2017-04-24 09:14:32 -0700 | [diff] [blame] | 109 | packet_->header.timestamp = |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 110 | ByteReader<uint32_t>::ReadLittleEndian(&data_[data_ix_]); |
| 111 | data_ix_ += sizeof(uint32_t); |
henrik.lundin | 246ef3e | 2017-04-24 09:14:32 -0700 | [diff] [blame] | 112 | packet_->header.ssrc = |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 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); |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 130 | NetEqTest::Callbacks callbacks; |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 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"); |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 144 | codecs[114] = std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16"); |
| 145 | codecs[115] = std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32"); |
| 146 | codecs[116] = std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48"); |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 147 | codecs[117] = std::make_pair(NetEqDecoder::kDecoderRED, "red"); |
| 148 | codecs[13] = std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb"); |
| 149 | codecs[98] = std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb"); |
| 150 | codecs[99] = std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32"); |
| 151 | codecs[100] = std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48"); |
| 152 | // This is the payload type that will be used for encoding. |
| 153 | codecs[kPayloadType] = |
| 154 | std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32"); |
| 155 | NetEqTest::ExtDecoderMap ext_codecs; |
| 156 | |
| 157 | NetEqTest test(config, codecs, ext_codecs, std::move(input), |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 158 | std::move(output), callbacks); |
henrik.lundin | 58466f6 | 2016-10-05 02:27:42 -0700 | [diff] [blame] | 159 | test.Run(); |
| 160 | } |
| 161 | |
| 162 | } // namespace test |
| 163 | |
| 164 | void FuzzOneInput(const uint8_t* data, size_t size) { |
| 165 | test::FuzzOneInputTest(data, size); |
| 166 | } |
| 167 | |
| 168 | } // namespace webrtc |