ivoc | 860249e | 2017-05-16 06:50:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 <math.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <bitset> |
| 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/residual_echo_detector.h" |
| 19 | #include "rtc_base/checks.h" |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 20 | #include "rtc_base/refcountedobject.h" |
ivoc | 860249e | 2017-05-16 06:50:11 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | void FuzzOneInput(const uint8_t* data, size_t size) { |
| 25 | // Number of times to update the echo detector. |
| 26 | constexpr size_t kNrOfUpdates = 7; |
| 27 | // Each round of updates requires a call to both AnalyzeRender and |
| 28 | // AnalyzeCapture, so the amount of needed input bytes doubles. Also, two |
| 29 | // bytes are used to set the call order. |
| 30 | constexpr size_t kNrOfNeededInputBytes = 2 * kNrOfUpdates * sizeof(float) + 2; |
| 31 | // The maximum audio energy that an audio frame can have is equal to the |
| 32 | // number of samples in the frame multiplied by 2^30. We use a single sample |
| 33 | // to represent an audio frame in this test, so it should have a maximum value |
| 34 | // equal to the square root of that value. |
| 35 | const float maxFuzzedValue = sqrtf(20 * 48) * 32768; |
| 36 | if (size < kNrOfNeededInputBytes) { |
| 37 | return; |
| 38 | } |
| 39 | size_t read_idx = 0; |
| 40 | // Use the first two bytes to choose the call order. |
| 41 | uint16_t call_order_int; |
| 42 | memcpy(&call_order_int, &data[read_idx], 2); |
| 43 | read_idx += 2; |
| 44 | std::bitset<16> call_order(call_order_int); |
| 45 | |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 46 | rtc::scoped_refptr<ResidualEchoDetector> echo_detector = |
| 47 | new rtc::RefCountedObject<ResidualEchoDetector>(); |
ivoc | 860249e | 2017-05-16 06:50:11 -0700 | [diff] [blame] | 48 | std::vector<float> input(1); |
| 49 | // Call AnalyzeCaptureAudio once to prevent the flushing of the buffer. |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 50 | echo_detector->AnalyzeCaptureAudio(input); |
ivoc | 860249e | 2017-05-16 06:50:11 -0700 | [diff] [blame] | 51 | for (size_t i = 0; i < 2 * kNrOfUpdates; ++i) { |
| 52 | // Convert 4 input bytes to a float. |
| 53 | RTC_DCHECK_LE(read_idx + sizeof(float), size); |
| 54 | memcpy(input.data(), &data[read_idx], sizeof(float)); |
| 55 | read_idx += sizeof(float); |
| 56 | if (!isfinite(input[0]) || fabs(input[0]) > maxFuzzedValue) { |
| 57 | // Ignore infinity, nan values and values that are unrealistically large. |
| 58 | continue; |
| 59 | } |
| 60 | if (call_order[i]) { |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 61 | echo_detector->AnalyzeRenderAudio(input); |
ivoc | 860249e | 2017-05-16 06:50:11 -0700 | [diff] [blame] | 62 | } else { |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 63 | echo_detector->AnalyzeCaptureAudio(input); |
ivoc | 860249e | 2017-05-16 06:50:11 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } // namespace webrtc |