kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 1 | /* |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 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 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 11 | #include <algorithm> |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 12 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 13 | #include "webrtc/audio/test/low_bandwidth_audio_test.h" |
| 14 | #include "webrtc/common_audio/wav_file.h" |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 15 | #include "webrtc/rtc_base/flags.h" |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/include/sleep.h" |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 17 | #include "webrtc/test/gtest.h" |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 18 | #include "webrtc/test/testsupport/fileutils.h" |
| 19 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 20 | |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 21 | DEFINE_int(sample_rate_hz, 16000, |
| 22 | "Sample rate (Hz) of the produced audio files."); |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 23 | |
oprypin | 76a1ce7 | 2017-05-04 03:06:18 -0700 | [diff] [blame] | 24 | DEFINE_bool(quick, false, |
| 25 | "Don't do the full audio recording. " |
| 26 | "Used to quickly check that the test runs without crashing."); |
| 27 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 28 | namespace { |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 29 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 30 | // Wait half a second between stopping sending and stopping receiving audio. |
| 31 | constexpr int kExtraRecordTimeMs = 500; |
| 32 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 33 | std::string FileSampleRateSuffix() { |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 34 | return std::to_string(FLAG_sample_rate_hz / 1000); |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 35 | } |
| 36 | |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 37 | } // namespace |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 38 | |
| 39 | namespace webrtc { |
| 40 | namespace test { |
| 41 | |
| 42 | AudioQualityTest::AudioQualityTest() |
| 43 | : EndToEndTest(CallTest::kDefaultTimeoutMs) {} |
| 44 | |
| 45 | size_t AudioQualityTest::GetNumVideoStreams() const { |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 46 | return 0; |
| 47 | } |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 48 | size_t AudioQualityTest::GetNumAudioStreams() const { |
| 49 | return 1; |
| 50 | } |
| 51 | size_t AudioQualityTest::GetNumFlexfecStreams() const { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | std::string AudioQualityTest::AudioInputFile() { |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 56 | return test::ResourcePath("voice_engine/audio_tiny" + FileSampleRateSuffix(), |
| 57 | "wav"); |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | std::string AudioQualityTest::AudioOutputFile() { |
| 61 | const ::testing::TestInfo* const test_info = |
| 62 | ::testing::UnitTest::GetInstance()->current_test_info(); |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 63 | return webrtc::test::OutputPath() + "LowBandwidth_" + test_info->name() + |
| 64 | "_" + FileSampleRateSuffix() + ".wav"; |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::unique_ptr<test::FakeAudioDevice::Capturer> |
| 68 | AudioQualityTest::CreateCapturer() { |
| 69 | return test::FakeAudioDevice::CreateWavFileReader(AudioInputFile()); |
| 70 | } |
| 71 | |
| 72 | std::unique_ptr<test::FakeAudioDevice::Renderer> |
| 73 | AudioQualityTest::CreateRenderer() { |
| 74 | return test::FakeAudioDevice::CreateBoundedWavFileWriter( |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 75 | AudioOutputFile(), FLAG_sample_rate_hz); |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void AudioQualityTest::OnFakeAudioDevicesCreated( |
| 79 | test::FakeAudioDevice* send_audio_device, |
| 80 | test::FakeAudioDevice* recv_audio_device) { |
| 81 | send_audio_device_ = send_audio_device; |
| 82 | } |
| 83 | |
| 84 | FakeNetworkPipe::Config AudioQualityTest::GetNetworkPipeConfig() { |
| 85 | return FakeNetworkPipe::Config(); |
| 86 | } |
| 87 | |
| 88 | test::PacketTransport* AudioQualityTest::CreateSendTransport( |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 89 | SingleThreadedTaskQueueForTesting* task_queue, |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 90 | Call* sender_call) { |
| 91 | return new test::PacketTransport( |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 92 | task_queue, sender_call, this, test::PacketTransport::kSender, |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 93 | test::CallTest::payload_type_map_, GetNetworkPipeConfig()); |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 94 | } |
| 95 | |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 96 | test::PacketTransport* AudioQualityTest::CreateReceiveTransport( |
| 97 | SingleThreadedTaskQueueForTesting* task_queue) { |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 98 | return new test::PacketTransport( |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 99 | task_queue, nullptr, this, test::PacketTransport::kReceiver, |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 100 | test::CallTest::payload_type_map_, GetNetworkPipeConfig()); |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void AudioQualityTest::ModifyAudioConfigs( |
| 104 | AudioSendStream::Config* send_config, |
| 105 | std::vector<AudioReceiveStream::Config>* receive_configs) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 106 | // Large bitrate by default. |
| 107 | const webrtc::SdpAudioFormat kDefaultFormat("OPUS", 48000, 2, |
| 108 | {{"stereo", "1"}}); |
| 109 | send_config->send_codec_spec = |
| 110 | rtc::Optional<AudioSendStream::Config::SendCodecSpec>( |
| 111 | {test::CallTest::kAudioSendPayloadType, kDefaultFormat}); |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void AudioQualityTest::PerformTest() { |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 115 | if (FLAG_quick) { |
oprypin | 76a1ce7 | 2017-05-04 03:06:18 -0700 | [diff] [blame] | 116 | // Let the recording run for a small amount of time to check if it works. |
| 117 | SleepMs(1000); |
| 118 | } else { |
| 119 | // Wait until the input audio file is done... |
| 120 | send_audio_device_->WaitForRecordingEnd(); |
| 121 | // and some extra time to account for network delay. |
| 122 | SleepMs(GetNetworkPipeConfig().queue_delay_ms + kExtraRecordTimeMs); |
| 123 | } |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void AudioQualityTest::OnTestFinished() { |
| 127 | const ::testing::TestInfo* const test_info = |
| 128 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 129 | |
| 130 | // Output information about the input and output audio files so that further |
| 131 | // processing can be done by an external process. |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 132 | printf("TEST %s %s %s\n", test_info->name(), |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 133 | AudioInputFile().c_str(), AudioOutputFile().c_str()); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | using LowBandwidthAudioTest = CallTest; |
| 138 | |
| 139 | TEST_F(LowBandwidthAudioTest, GoodNetworkHighBitrate) { |
| 140 | AudioQualityTest test; |
| 141 | RunBaseTest(&test); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | class Mobile2GNetworkTest : public AudioQualityTest { |
| 146 | void ModifyAudioConfigs(AudioSendStream::Config* send_config, |
| 147 | std::vector<AudioReceiveStream::Config>* receive_configs) override { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 148 | send_config->send_codec_spec = |
| 149 | rtc::Optional<AudioSendStream::Config::SendCodecSpec>( |
| 150 | {test::CallTest::kAudioSendPayloadType, |
| 151 | {"OPUS", |
| 152 | 48000, |
| 153 | 2, |
| 154 | {{"maxaveragebitrate", "6000"}, |
| 155 | {"ptime", "60"}, |
| 156 | {"stereo", "1"}}}}); |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | FakeNetworkPipe::Config GetNetworkPipeConfig() override { |
| 160 | FakeNetworkPipe::Config pipe_config; |
| 161 | pipe_config.link_capacity_kbps = 12; |
| 162 | pipe_config.queue_length_packets = 1500; |
| 163 | pipe_config.queue_delay_ms = 400; |
| 164 | return pipe_config; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | TEST_F(LowBandwidthAudioTest, Mobile2GNetwork) { |
| 169 | Mobile2GNetworkTest test; |
| 170 | RunBaseTest(&test); |
| 171 | } |
| 172 | |
| 173 | } // namespace test |
| 174 | } // namespace webrtc |