niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
tina.legrand@webrtc.org | ae1c454 | 2012-03-12 08:41:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/test/EncodeDecodeTest.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | #include <memory> |
tina.legrand@webrtc.org | 5e7ca60 | 2012-06-12 07:16:24 +0000 | [diff] [blame] | 16 | |
Karl Wiberg | 5817d3d | 2018-04-06 10:06:42 +0200 | [diff] [blame] | 17 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
Karl Wiberg | 658a552 | 2018-08-15 15:20:49 +0200 | [diff] [blame] | 18 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_coding/include/audio_coding_module.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 20 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 22 | #include "test/testsupport/file_utils.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 26 | TestPacketization::TestPacketization(RTPStream *rtpStream, uint16_t frequency) |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 27 | : _rtpStream(rtpStream), |
| 28 | _frequency(frequency), |
| 29 | _seqNo(0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 32 | TestPacketization::~TestPacketization() { |
| 33 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 35 | int32_t TestPacketization::SendData( |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 36 | const FrameType /* frameType */, const uint8_t payloadType, |
| 37 | const uint32_t timeStamp, const uint8_t* payloadData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 38 | const size_t payloadSize, |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 39 | const RTPFragmentationHeader* /* fragmentation */) { |
| 40 | _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize, |
| 41 | _frequency); |
| 42 | return 1; |
| 43 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 45 | Sender::Sender() |
| 46 | : _acm(NULL), |
| 47 | _pcmFile(), |
| 48 | _audioFrame(), |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 49 | _packetization(NULL) { |
| 50 | } |
| 51 | |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 52 | void Sender::Setup(AudioCodingModule *acm, RTPStream *rtpStream, |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 53 | std::string in_file_name, int in_sample_rate, |
| 54 | int payload_type, SdpAudioFormat format) { |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 +0000 | [diff] [blame] | 55 | // Open input file |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 56 | const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm"); |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 57 | _pcmFile.Open(file_name, in_sample_rate, "rb"); |
| 58 | if (format.num_channels == 2) { |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 59 | _pcmFile.ReadStereo(true); |
| 60 | } |
Henrik Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 61 | // Set test length to 500 ms (50 blocks of 10 ms each). |
| 62 | _pcmFile.SetNum10MsBlocksToRead(50); |
| 63 | // Fast-forward 1 second (100 blocks) since the file starts with silence. |
| 64 | _pcmFile.FastForward(100); |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 +0000 | [diff] [blame] | 65 | |
Karl Wiberg | 658a552 | 2018-08-15 15:20:49 +0200 | [diff] [blame] | 66 | acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder( |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 67 | payload_type, format, absl::nullopt)); |
| 68 | _packetization = new TestPacketization(rtpStream, format.clockrate_hz); |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 69 | EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization)); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 70 | |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 +0000 | [diff] [blame] | 71 | _acm = acm; |
| 72 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 73 | |
| 74 | void Sender::Teardown() { |
| 75 | _pcmFile.Close(); |
| 76 | delete _packetization; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | } |
| 78 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 79 | bool Sender::Add10MsData() { |
| 80 | if (!_pcmFile.EndOfFile()) { |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 81 | EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0); |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 82 | int32_t ok = _acm->Add10MsData(_audioFrame); |
henrik.lundin@webrtc.org | f56c162 | 2015-03-02 12:29:30 +0000 | [diff] [blame] | 83 | EXPECT_GE(ok, 0); |
| 84 | return ok >= 0 ? true : false; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 85 | } |
| 86 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 89 | void Sender::Run() { |
| 90 | while (true) { |
| 91 | if (!Add10MsData()) { |
| 92 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | Receiver::Receiver() |
| 98 | : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO), |
| 99 | _payloadSizeBytes(MAX_INCOMING_PAYLOAD) { |
| 100 | } |
| 101 | |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 102 | void Receiver::Setup(AudioCodingModule *acm, RTPStream *rtpStream, |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 103 | std::string out_file_name, size_t channels, int file_num) { |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 104 | EXPECT_EQ(0, acm->InitializeReceiver()); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 105 | |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 106 | if (channels == 1) { |
| 107 | acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}}, |
| 108 | {104, {"ISAC", 32000, 1}}, |
| 109 | {107, {"L16", 8000, 1}}, |
| 110 | {108, {"L16", 16000, 1}}, |
| 111 | {109, {"L16", 32000, 1}}, |
| 112 | {0, {"PCMU", 8000, 1}}, |
| 113 | {8, {"PCMA", 8000, 1}}, |
| 114 | {102, {"ILBC", 8000, 1}}, |
| 115 | {9, {"G722", 8000, 1}}, |
| 116 | {120, {"OPUS", 48000, 2}}, |
| 117 | {13, {"CN", 8000, 1}}, |
| 118 | {98, {"CN", 16000, 1}}, |
| 119 | {99, {"CN", 32000, 1}}}); |
| 120 | } else { |
| 121 | ASSERT_EQ(channels, 2u); |
| 122 | acm->SetReceiveCodecs({{111, {"L16", 8000, 2}}, |
| 123 | {112, {"L16", 16000, 2}}, |
| 124 | {113, {"L16", 32000, 2}}, |
| 125 | {110, {"PCMU", 8000, 2}}, |
| 126 | {118, {"PCMA", 8000, 2}}, |
| 127 | {119, {"G722", 8000, 2}}, |
| 128 | {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}}); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 129 | } |
phoglund@webrtc.org | d1a860b | 2012-01-26 14:49:28 +0000 | [diff] [blame] | 130 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 131 | int playSampFreq; |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 +0000 | [diff] [blame] | 132 | std::string file_name; |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 133 | rtc::StringBuilder file_stream; |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 134 | file_stream << webrtc::test::OutputPath() << out_file_name << file_num |
| 135 | << ".pcm"; |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 +0000 | [diff] [blame] | 136 | file_name = file_stream.str(); |
| 137 | _rtpStream = rtpStream; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 138 | |
Karl Wiberg | 88aee28 | 2018-06-14 13:12:05 +0200 | [diff] [blame] | 139 | playSampFreq = 32000; |
| 140 | _pcmFile.Open(file_name, 32000, "wb+"); |
phoglund@webrtc.org | d1a860b | 2012-01-26 14:49:28 +0000 | [diff] [blame] | 141 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 142 | _realPayloadSizeBytes = 0; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 143 | _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO]; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 144 | _frequency = playSampFreq; |
| 145 | _acm = acm; |
| 146 | _firstTime = true; |
| 147 | } |
| 148 | |
| 149 | void Receiver::Teardown() { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 150 | delete[] _playoutBuffer; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 151 | _pcmFile.Close(); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool Receiver::IncomingPacket() { |
| 155 | if (!_rtpStream->EndOfFile()) { |
| 156 | if (_firstTime) { |
| 157 | _firstTime = false; |
| 158 | _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload, |
| 159 | _payloadSizeBytes, &_nextTime); |
andrew@webrtc.org | 975e4a3 | 2012-01-17 19:27:33 +0000 | [diff] [blame] | 160 | if (_realPayloadSizeBytes == 0) { |
| 161 | if (_rtpStream->EndOfFile()) { |
| 162 | _firstTime = true; |
| 163 | return true; |
| 164 | } else { |
andrew@webrtc.org | 975e4a3 | 2012-01-17 19:27:33 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 167 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 168 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 170 | EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, |
| 171 | _rtpInfo)); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 172 | _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload, |
| 173 | _payloadSizeBytes, &_nextTime); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 174 | if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) { |
| 175 | _firstTime = true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 177 | } |
| 178 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | } |
| 180 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 181 | bool Receiver::PlayoutData() { |
| 182 | AudioFrame audioFrame; |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 183 | bool muted; |
| 184 | int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted); |
| 185 | if (muted) { |
| 186 | ADD_FAILURE(); |
| 187 | return false; |
| 188 | } |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 189 | EXPECT_EQ(0, ok); |
| 190 | if (ok < 0){ |
| 191 | return false; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 192 | } |
| 193 | if (_playoutLengthSmpls == 0) { |
| 194 | return false; |
| 195 | } |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 196 | _pcmFile.Write10MsData(audioFrame.data(), |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 197 | audioFrame.samples_per_channel_ * audioFrame.num_channels_); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
| 201 | void Receiver::Run() { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 202 | uint8_t counter500Ms = 50; |
| 203 | uint32_t clock = 0; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 204 | |
| 205 | while (counter500Ms > 0) { |
| 206 | if (clock == 0 || clock >= _nextTime) { |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 207 | EXPECT_TRUE(IncomingPacket()); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 208 | if (clock == 0) { |
| 209 | clock = _nextTime; |
| 210 | } |
| 211 | } |
| 212 | if ((clock % 10) == 0) { |
| 213 | if (!PlayoutData()) { |
| 214 | clock++; |
| 215 | continue; |
| 216 | } |
| 217 | } |
| 218 | if (_rtpStream->EndOfFile()) { |
| 219 | counter500Ms--; |
| 220 | } |
| 221 | clock++; |
| 222 | } |
| 223 | } |
| 224 | |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 225 | EncodeDecodeTest::EncodeDecodeTest() = default; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 226 | |
| 227 | void EncodeDecodeTest::Perform() { |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 228 | const std::map<int, SdpAudioFormat> send_codecs = {{103, {"ISAC", 16000, 1}}, |
| 229 | {104, {"ISAC", 32000, 1}}, |
| 230 | {107, {"L16", 8000, 1}}, |
| 231 | {108, {"L16", 16000, 1}}, |
| 232 | {109, {"L16", 32000, 1}}, |
| 233 | {0, {"PCMU", 8000, 1}}, |
| 234 | {8, {"PCMA", 8000, 1}}, |
| 235 | #ifdef WEBRTC_CODEC_ILBC |
| 236 | {102, {"ILBC", 8000, 1}}, |
| 237 | #endif |
| 238 | {9, {"G722", 8000, 1}}}; |
| 239 | int file_num = 0; |
| 240 | for (const auto& send_codec : send_codecs) { |
| 241 | RTPFile rtpFile; |
| 242 | std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create( |
| 243 | AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 244 | |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 245 | std::string fileName = webrtc::test::TempFilename( |
| 246 | webrtc::test::OutputPath(), "encode_decode_rtp"); |
| 247 | rtpFile.Open(fileName.c_str(), "wb+"); |
| 248 | rtpFile.WriteHeader(); |
| 249 | Sender sender; |
| 250 | sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000, |
| 251 | send_codec.first, send_codec.second); |
| 252 | sender.Run(); |
| 253 | sender.Teardown(); |
| 254 | rtpFile.Close(); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 255 | |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 256 | rtpFile.Open(fileName.c_str(), "rb"); |
| 257 | rtpFile.ReadHeader(); |
| 258 | Receiver receiver; |
| 259 | receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num); |
| 260 | receiver.Run(); |
| 261 | receiver.Teardown(); |
| 262 | rtpFile.Close(); |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 263 | |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 264 | file_num++; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 265 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 266 | } |
| 267 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 268 | } // namespace webrtc |