blob: bf8e1894d0e41b936322dd5f7db4f51bd0042e22 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.orgdf697752012-02-08 10:22:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/test/TestStereo.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
tina.legrand@webrtc.orga092cbf2013-02-14 09:28:10 +000013#include <string>
kjellander@webrtc.org5490c712011-12-21 13:34:18 +000014
Niels Möller2edab4c2018-10-22 09:48:08 +020015#include "absl/strings/match.h"
Karl Wiberg5817d3d2018-04-06 10:06:42 +020016#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wibergd363db12018-09-24 14:45:24 +020017#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +000018#include "modules/audio_coding/codecs/audio_format_conversion.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +000020#include "modules/audio_coding/test/utility.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020021#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
23#include "test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000025namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27// Class for simulating packet handling
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000028TestPackStereo::TestPackStereo()
29 : receiver_acm_(NULL),
30 seq_no_(0),
31 timestamp_diff_(0),
32 last_in_timestamp_(0),
33 total_bytes_(0),
34 payload_size_(0),
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +000035 codec_mode_(kNotSet),
Yves Gerey665174f2018-06-19 15:03:05 +020036 lost_packet_(false) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000037
Yves Gerey665174f2018-06-19 15:03:05 +020038TestPackStereo::~TestPackStereo() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000039
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000040void TestPackStereo::RegisterReceiverACM(AudioCodingModule* acm) {
41 receiver_acm_ = acm;
42 return;
43}
niklase@google.com470e71d2011-07-07 08:21:25 +000044
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000045int32_t TestPackStereo::SendData(const FrameType frame_type,
46 const uint8_t payload_type,
47 const uint32_t timestamp,
48 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000049 const size_t payload_size,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000050 const RTPFragmentationHeader* fragmentation) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000051 WebRtcRTPHeader rtp_info;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000052 int32_t status = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000054 rtp_info.header.markerBit = false;
55 rtp_info.header.ssrc = 0;
56 rtp_info.header.sequenceNumber = seq_no_++;
57 rtp_info.header.payloadType = payload_type;
58 rtp_info.header.timestamp = timestamp;
pbos22993e12015-10-19 02:39:06 -070059 if (frame_type == kEmptyFrame) {
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +000060 // Skip this frame
61 return 0;
62 }
niklase@google.com470e71d2011-07-07 08:21:25 +000063
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000064 if (lost_packet_ == false) {
Yves Gerey665174f2018-06-19 15:03:05 +020065 status =
66 receiver_acm_->IncomingPacket(payload_data, payload_size, rtp_info);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000068 if (frame_type != kAudioFrameCN) {
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +000069 payload_size_ = static_cast<int>(payload_size);
niklase@google.com470e71d2011-07-07 08:21:25 +000070 } else {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000071 payload_size_ = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000072 }
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +000073
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000074 timestamp_diff_ = timestamp - last_in_timestamp_;
75 last_in_timestamp_ = timestamp;
76 total_bytes_ += payload_size;
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +000077 }
78 return status;
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
pbos@webrtc.org0946a562013-04-09 00:28:06 +000081uint16_t TestPackStereo::payload_size() {
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +000082 return static_cast<uint16_t>(payload_size_);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
pbos@webrtc.org0946a562013-04-09 00:28:06 +000085uint32_t TestPackStereo::timestamp_diff() {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000086 return timestamp_diff_;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000089void TestPackStereo::reset_payload_size() {
90 payload_size_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +000093void TestPackStereo::set_codec_mode(enum StereoMonoMode mode) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000094 codec_mode_ = mode;
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +000095}
96
97void TestPackStereo::set_lost_packet(bool lost) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000098 lost_packet_ = lost;
niklase@google.com470e71d2011-07-07 08:21:25 +000099}
100
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000101TestStereo::TestStereo(int test_mode)
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200102 : acm_a_(AudioCodingModule::Create(
103 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
104 acm_b_(AudioCodingModule::Create(
105 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000106 channel_a2b_(NULL),
107 test_cntr_(0),
108 pack_size_samp_(0),
109 pack_size_bytes_(0),
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200110 counter_(0) {
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000111 // test_mode = 0 for silent test (auto test)
112 test_mode_ = test_mode;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
114
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000115TestStereo::~TestStereo() {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000116 if (channel_a2b_ != NULL) {
117 delete channel_a2b_;
118 channel_a2b_ = NULL;
119 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000122void TestStereo::Perform() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000123 uint16_t frequency_hz;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000124 int audio_channels;
125 int codec_channels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000127 // Open both mono and stereo test files in 32 kHz.
Yves Gerey665174f2018-06-19 15:03:05 +0200128 const std::string file_name_stereo =
129 webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm");
130 const std::string file_name_mono =
131 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000132 frequency_hz = 32000;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000133 in_file_stereo_ = new PCMFile();
134 in_file_mono_ = new PCMFile();
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000135 in_file_stereo_->Open(file_name_stereo, frequency_hz, "rb");
136 in_file_stereo_->ReadStereo(true);
137 in_file_mono_->Open(file_name_mono, frequency_hz, "rb");
138 in_file_mono_->ReadStereo(false);
139
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000140 // Create and initialize two ACMs, one for each side of a one-to-one call.
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000141 ASSERT_TRUE((acm_a_.get() != NULL) && (acm_b_.get() != NULL));
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000142 EXPECT_EQ(0, acm_a_->InitializeReceiver());
143 EXPECT_EQ(0, acm_b_->InitializeReceiver());
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000144
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000145 // Register all available codes as receiving codecs.
146 uint8_t num_encoders = acm_a_->NumberOfCodecs();
147 CodecInst my_codec_param;
148 for (uint8_t n = 0; n < num_encoders; n++) {
149 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
150 EXPECT_EQ(true, acm_b_->RegisterReceiveCodec(
151 my_codec_param.pltype, CodecInstToSdp(my_codec_param)));
152 }
153
154 // Test that unregister all receive codecs works.
155 for (uint8_t n = 0; n < num_encoders; n++) {
156 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
157 EXPECT_EQ(0, acm_b_->UnregisterReceiveCodec(my_codec_param.pltype));
158 }
159
160 // Register all available codes as receiving codecs once more.
161 for (uint8_t n = 0; n < num_encoders; n++) {
162 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
163 EXPECT_EQ(true, acm_b_->RegisterReceiveCodec(
164 my_codec_param.pltype, CodecInstToSdp(my_codec_param)));
165 }
tina.legrand@webrtc.orgae1c4542012-03-12 08:41:30 +0000166
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000167 // Create and connect the channel.
168 channel_a2b_ = new TestPackStereo;
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000169 EXPECT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_));
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000170 channel_a2b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.orgdf697752012-02-08 10:22:21 +0000171
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000172 char codec_pcma_temp[] = "PCMA";
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200173 RegisterSendCodec('A', codec_pcma_temp, 8000, 64000, 80, 2);
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000174 if (test_mode_ != 0) {
175 printf("\n");
176 }
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000177
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000178 //
179 // Test Stereo-To-Stereo for all codecs.
180 //
181 audio_channels = 2;
182 codec_channels = 2;
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000183
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000184 // All codecs are tested for all allowed sampling frequencies, rates and
185 // packet sizes.
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000186 if (test_mode_ != 0) {
187 printf("===========================================================\n");
188 printf("Test number: %d\n", test_cntr_ + 1);
189 printf("Test type: Stereo-to-stereo\n");
190 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000191 channel_a2b_->set_codec_mode(kStereo);
192 test_cntr_++;
193 OpenOutFile(test_cntr_);
194 char codec_g722[] = "G722";
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200195 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000196 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200197 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000198 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200199 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000200 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200201 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000202 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200203 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000204 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200205 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000206 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000207 out_file_.Close();
Karl Wibergeb254b42017-11-01 15:08:12 +0100208
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000209 if (test_mode_ != 0) {
210 printf("===========================================================\n");
211 printf("Test number: %d\n", test_cntr_ + 1);
212 printf("Test type: Stereo-to-stereo\n");
213 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000214 channel_a2b_->set_codec_mode(kStereo);
215 test_cntr_++;
216 OpenOutFile(test_cntr_);
217 char codec_l16[] = "L16";
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200218 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000219 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200220 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000221 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200222 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000223 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200224 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000225 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000226 out_file_.Close();
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000227
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000228 if (test_mode_ != 0) {
229 printf("===========================================================\n");
230 printf("Test number: %d\n", test_cntr_ + 1);
231 printf("Test type: Stereo-to-stereo\n");
232 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000233 test_cntr_++;
234 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200235 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000236 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200237 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000238 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200239 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000240 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200241 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000242 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000243 out_file_.Close();
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000244
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000245 if (test_mode_ != 0) {
246 printf("===========================================================\n");
247 printf("Test number: %d\n", test_cntr_ + 1);
248 printf("Test type: Stereo-to-stereo\n");
249 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000250 test_cntr_++;
251 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200252 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000253 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200254 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000255 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000256 out_file_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000257#ifdef PCMA_AND_PCMU
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000258 if (test_mode_ != 0) {
259 printf("===========================================================\n");
260 printf("Test number: %d\n", test_cntr_ + 1);
261 printf("Test type: Stereo-to-stereo\n");
262 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000263 channel_a2b_->set_codec_mode(kStereo);
264 audio_channels = 2;
265 codec_channels = 2;
266 test_cntr_++;
267 OpenOutFile(test_cntr_);
268 char codec_pcma[] = "PCMA";
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200269 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000270 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200271 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000272 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200273 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000274 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200275 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000276 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200277 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000278 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200279 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000280 Run(channel_a2b_, audio_channels, codec_channels);
Fredrik Solenberg056f9732018-12-03 15:22:16 +0100281
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000282 out_file_.Close();
283 if (test_mode_ != 0) {
284 printf("===========================================================\n");
285 printf("Test number: %d\n", test_cntr_ + 1);
286 printf("Test type: Stereo-to-stereo\n");
287 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000288 test_cntr_++;
289 OpenOutFile(test_cntr_);
290 char codec_pcmu[] = "PCMU";
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200291 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000292 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200293 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000294 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200295 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000296 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200297 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000298 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200299 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000300 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200301 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000302 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000303 out_file_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000304#endif
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000305#ifdef WEBRTC_CODEC_OPUS
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000306 if (test_mode_ != 0) {
307 printf("===========================================================\n");
308 printf("Test number: %d\n", test_cntr_ + 1);
309 printf("Test type: Stereo-to-stereo\n");
310 }
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000311 channel_a2b_->set_codec_mode(kStereo);
312 audio_channels = 2;
313 codec_channels = 2;
314 test_cntr_++;
315 OpenOutFile(test_cntr_);
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +0000316
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000317 char codec_opus[] = "opus";
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +0000318 // Run Opus with 10 ms frame size.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200319 RegisterSendCodec('A', codec_opus, 48000, 64000, 480, codec_channels);
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000320 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +0000321 // Run Opus with 20 ms frame size.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200322 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 2, codec_channels);
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +0000323 Run(channel_a2b_, audio_channels, codec_channels);
324 // Run Opus with 40 ms frame size.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200325 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, codec_channels);
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +0000326 Run(channel_a2b_, audio_channels, codec_channels);
327 // Run Opus with 60 ms frame size.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200328 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 6, codec_channels);
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +0000329 Run(channel_a2b_, audio_channels, codec_channels);
330 // Run Opus with 20 ms frame size and different bitrates.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200331 RegisterSendCodec('A', codec_opus, 48000, 40000, 960, codec_channels);
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000332 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200333 RegisterSendCodec('A', codec_opus, 48000, 510000, 960, codec_channels);
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000334 Run(channel_a2b_, audio_channels, codec_channels);
335 out_file_.Close();
336#endif
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000337 //
338 // Test Mono-To-Stereo for all codecs.
339 //
340 audio_channels = 1;
341 codec_channels = 2;
niklase@google.com470e71d2011-07-07 08:21:25 +0000342
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000343 if (test_mode_ != 0) {
344 printf("===============================================================\n");
345 printf("Test number: %d\n", test_cntr_ + 1);
346 printf("Test type: Mono-to-stereo\n");
347 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000348 test_cntr_++;
349 channel_a2b_->set_codec_mode(kStereo);
350 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200351 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000352 Run(channel_a2b_, audio_channels, codec_channels);
353 out_file_.Close();
Karl Wibergeb254b42017-11-01 15:08:12 +0100354
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000355 if (test_mode_ != 0) {
356 printf("===============================================================\n");
357 printf("Test number: %d\n", test_cntr_ + 1);
358 printf("Test type: Mono-to-stereo\n");
359 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000360 test_cntr_++;
361 channel_a2b_->set_codec_mode(kStereo);
362 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200363 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000364 Run(channel_a2b_, audio_channels, codec_channels);
365 out_file_.Close();
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000366 if (test_mode_ != 0) {
367 printf("===============================================================\n");
368 printf("Test number: %d\n", test_cntr_ + 1);
369 printf("Test type: Mono-to-stereo\n");
370 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000371 test_cntr_++;
372 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200373 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000374 Run(channel_a2b_, audio_channels, codec_channels);
375 out_file_.Close();
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000376 if (test_mode_ != 0) {
377 printf("===============================================================\n");
378 printf("Test number: %d\n", test_cntr_ + 1);
379 printf("Test type: Mono-to-stereo\n");
380 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000381 test_cntr_++;
382 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200383 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000384 Run(channel_a2b_, audio_channels, codec_channels);
385 out_file_.Close();
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000386#ifdef PCMA_AND_PCMU
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000387 if (test_mode_ != 0) {
388 printf("===============================================================\n");
389 printf("Test number: %d\n", test_cntr_ + 1);
390 printf("Test type: Mono-to-stereo\n");
391 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000392 test_cntr_++;
393 channel_a2b_->set_codec_mode(kStereo);
394 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200395 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000396 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200397 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000398 Run(channel_a2b_, audio_channels, codec_channels);
399 out_file_.Close();
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000400#endif
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000401#ifdef WEBRTC_CODEC_OPUS
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000402 if (test_mode_ != 0) {
403 printf("===============================================================\n");
404 printf("Test number: %d\n", test_cntr_ + 1);
405 printf("Test type: Mono-to-stereo\n");
406 }
407
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000408 // Keep encode and decode in stereo.
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000409 test_cntr_++;
410 channel_a2b_->set_codec_mode(kStereo);
411 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200412 RegisterSendCodec('A', codec_opus, 48000, 64000, 960, codec_channels);
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000413 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000414
415 // Encode in mono, decode in stereo mode.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200416 RegisterSendCodec('A', codec_opus, 48000, 64000, 960, 1);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000417 Run(channel_a2b_, audio_channels, codec_channels);
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000418 out_file_.Close();
419#endif
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000420
421 //
422 // Test Stereo-To-Mono for all codecs.
423 //
424 audio_channels = 2;
425 codec_channels = 1;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000426 channel_a2b_->set_codec_mode(kMono);
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000427
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000428 // Run stereo audio and mono codec.
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000429 if (test_mode_ != 0) {
430 printf("===============================================================\n");
431 printf("Test number: %d\n", test_cntr_ + 1);
432 printf("Test type: Stereo-to-mono\n");
433 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000434 test_cntr_++;
435 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200436 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels);
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000437
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000438 Run(channel_a2b_, audio_channels, codec_channels);
439 out_file_.Close();
Karl Wibergeb254b42017-11-01 15:08:12 +0100440
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000441 if (test_mode_ != 0) {
442 printf("===============================================================\n");
443 printf("Test number: %d\n", test_cntr_ + 1);
444 printf("Test type: Stereo-to-mono\n");
445 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000446 test_cntr_++;
447 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200448 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000449 Run(channel_a2b_, audio_channels, codec_channels);
450 out_file_.Close();
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000451 if (test_mode_ != 0) {
452 printf("===============================================================\n");
453 printf("Test number: %d\n", test_cntr_ + 1);
454 printf("Test type: Stereo-to-mono\n");
455 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000456 test_cntr_++;
457 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200458 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000459 Run(channel_a2b_, audio_channels, codec_channels);
460 out_file_.Close();
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000461 if (test_mode_ != 0) {
462 printf("==============================================================\n");
463 printf("Test number: %d\n", test_cntr_ + 1);
464 printf("Test type: Stereo-to-mono\n");
465 }
tina.legrand@webrtc.orga092cbf2013-02-14 09:28:10 +0000466 test_cntr_++;
467 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200468 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels);
tina.legrand@webrtc.orga092cbf2013-02-14 09:28:10 +0000469 Run(channel_a2b_, audio_channels, codec_channels);
470 out_file_.Close();
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000471#ifdef PCMA_AND_PCMU
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000472 if (test_mode_ != 0) {
473 printf("===============================================================\n");
474 printf("Test number: %d\n", test_cntr_ + 1);
475 printf("Test type: Stereo-to-mono\n");
476 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000477 test_cntr_++;
478 OpenOutFile(test_cntr_);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200479 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000480 Run(channel_a2b_, audio_channels, codec_channels);
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200481 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000482 Run(channel_a2b_, audio_channels, codec_channels);
483 out_file_.Close();
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000484#endif
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000485#ifdef WEBRTC_CODEC_OPUS
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000486 if (test_mode_ != 0) {
487 printf("===============================================================\n");
488 printf("Test number: %d\n", test_cntr_ + 1);
489 printf("Test type: Stereo-to-mono\n");
490 }
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000491 test_cntr_++;
492 OpenOutFile(test_cntr_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000493 // Encode and decode in mono.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200494 RegisterSendCodec('A', codec_opus, 48000, 32000, 960, codec_channels);
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000495 CodecInst opus_codec_param;
496 for (uint8_t n = 0; n < num_encoders; n++) {
497 EXPECT_EQ(0, acm_b_->Codec(n, &opus_codec_param));
498 if (!strcmp(opus_codec_param.plname, "opus")) {
499 opus_codec_param.channels = 1;
500 EXPECT_EQ(true,
501 acm_b_->RegisterReceiveCodec(opus_codec_param.pltype,
502 CodecInstToSdp(opus_codec_param)));
503 break;
504 }
505 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000506 Run(channel_a2b_, audio_channels, codec_channels);
507
508 // Encode in stereo, decode in mono.
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200509 RegisterSendCodec('A', codec_opus, 48000, 32000, 960, 2);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000510 Run(channel_a2b_, audio_channels, codec_channels);
511
512 out_file_.Close();
513
514 // Test switching between decoding mono and stereo for Opus.
515
516 // Decode in mono.
517 test_cntr_++;
518 OpenOutFile(test_cntr_);
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000519 if (test_mode_ != 0) {
520 // Print out codec and settings
521 printf(
522 "Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
523 " Decode: mono\n",
524 test_cntr_);
525 }
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000526 Run(channel_a2b_, audio_channels, codec_channels);
527 out_file_.Close();
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000528 // Decode in stereo.
529 test_cntr_++;
530 OpenOutFile(test_cntr_);
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000531 if (test_mode_ != 0) {
532 // Print out codec and settings
533 printf(
534 "Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
535 " Decode: stereo\n",
536 test_cntr_);
537 }
538 opus_codec_param.channels = 2;
539 EXPECT_EQ(true,
540 acm_b_->RegisterReceiveCodec(opus_codec_param.pltype,
541 CodecInstToSdp(opus_codec_param)));
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000542 Run(channel_a2b_, audio_channels, 2);
543 out_file_.Close();
544 // Decode in mono.
545 test_cntr_++;
546 OpenOutFile(test_cntr_);
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000547 if (test_mode_ != 0) {
548 // Print out codec and settings
549 printf(
550 "Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
551 " Decode: mono\n",
552 test_cntr_);
553 }
554 opus_codec_param.channels = 1;
555 EXPECT_EQ(true,
556 acm_b_->RegisterReceiveCodec(opus_codec_param.pltype,
557 CodecInstToSdp(opus_codec_param)));
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000558 Run(channel_a2b_, audio_channels, codec_channels);
559 out_file_.Close();
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000560
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000561#endif
tina.legrand@webrtc.org6b6ff552012-01-11 10:12:54 +0000562
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000563 // Print out which codecs were tested, and which were not, in the run.
564 if (test_mode_ != 0) {
565 printf("\nThe following codecs was INCLUDED in the test:\n");
566 printf(" G.722\n");
567 printf(" PCM16\n");
568 printf(" G.711\n");
569#ifdef WEBRTC_CODEC_OPUS
570 printf(" Opus\n");
571#endif
572 printf(
573 "\nTo complete the test, listen to the %d number of output "
574 "files.\n",
575 test_cntr_);
576 }
577
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000578 // Delete the file pointers.
579 delete in_file_stereo_;
580 delete in_file_mono_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000581}
582
583// Register Codec to use in the test
584//
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000585// Input: side - which ACM to use, 'A' or 'B'
586// codec_name - name to use when register the codec
587// sampling_freq_hz - sampling frequency in Herz
588// rate - bitrate in bytes
589// pack_size - packet size in samples
590// channels - number of channels; 1 for mono, 2 for stereo
Yves Gerey665174f2018-06-19 15:03:05 +0200591void TestStereo::RegisterSendCodec(char side,
592 char* codec_name,
593 int32_t sampling_freq_hz,
594 int rate,
595 int pack_size,
Karl Wiberg36b37dc2018-09-24 10:52:32 +0200596 int channels) {
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000597 if (test_mode_ != 0) {
598 // Print out codec and settings
599 printf("Codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
600 sampling_freq_hz, rate, pack_size);
601 }
602
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000603 // Store packet size in samples, used to validate the received packet
604 pack_size_samp_ = pack_size;
605
606 // Store the expected packet size in bytes, used to validate the received
607 // packet. Add 0.875 to always round up to a whole byte.
pbos@webrtc.orgd8ca7232014-12-10 11:49:13 +0000608 pack_size_bytes_ = (uint16_t)(static_cast<float>(pack_size * rate) /
609 static_cast<float>(sampling_freq_hz * 8) +
610 0.875);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000611
612 // Set pointer to the ACM where to register the codec
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000613 AudioCodingModule* my_acm = NULL;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000614 switch (side) {
615 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000616 my_acm = acm_a_.get();
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000617 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000618 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000619 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000620 my_acm = acm_b_.get();
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000621 break;
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +0000622 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000623 default:
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000624 break;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000625 }
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000626 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000627
Karl Wibergd363db12018-09-24 14:45:24 +0200628 auto encoder_factory = CreateBuiltinAudioEncoderFactory();
Niels Möller2edab4c2018-10-22 09:48:08 +0200629 const int clockrate_hz = absl::EqualsIgnoreCase(codec_name, "g722")
Karl Wibergd363db12018-09-24 14:45:24 +0200630 ? sampling_freq_hz / 2
631 : sampling_freq_hz;
632 const std::string ptime = rtc::ToString(rtc::CheckedDivExact(
633 pack_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
634 SdpAudioFormat::Parameters params = {{"ptime", ptime}};
635 RTC_CHECK(channels == 1 || channels == 2);
Niels Möller2edab4c2018-10-22 09:48:08 +0200636 if (absl::EqualsIgnoreCase(codec_name, "opus")) {
Karl Wibergd363db12018-09-24 14:45:24 +0200637 if (channels == 2) {
638 params["stereo"] = "1";
639 }
640 channels = 2;
641 params["maxaveragebitrate"] = rtc::ToString(rate);
642 }
643 constexpr int payload_type = 17;
644 auto encoder = encoder_factory->MakeAudioEncoder(
645 payload_type, SdpAudioFormat(codec_name, clockrate_hz, channels, params),
646 absl::nullopt);
647 EXPECT_NE(nullptr, encoder);
648 my_acm->SetEncoder(std::move(encoder));
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000649
650 send_codec_name_ = codec_name;
niklase@google.com470e71d2011-07-07 08:21:25 +0000651}
652
Yves Gerey665174f2018-06-19 15:03:05 +0200653void TestStereo::Run(TestPackStereo* channel,
654 int in_channels,
655 int out_channels,
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +0000656 int percent_loss) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000657 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000658
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000659 int32_t out_freq_hz_b = out_file_.SamplingFrequency();
660 uint16_t rec_size;
661 uint32_t time_stamp_diff;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000662 channel->reset_payload_size();
663 int error_count = 0;
tina.legrand@webrtc.org65d61c32014-06-05 13:42:51 +0000664 int variable_bytes = 0;
665 int variable_packets = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100666 // Set test length to 500 ms (50 blocks of 10 ms each).
667 in_file_mono_->SetNum10MsBlocksToRead(50);
668 in_file_stereo_->SetNum10MsBlocksToRead(50);
669 // Fast-forward 1 second (100 blocks) since the files start with silence.
670 in_file_stereo_->FastForward(100);
671 in_file_mono_->FastForward(100);
niklase@google.com470e71d2011-07-07 08:21:25 +0000672
andresp@webrtc.orgd0b436a2014-01-13 13:15:59 +0000673 while (1) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000674 // Simulate packet loss by setting |packet_loss_| to "true" in
675 // |percent_loss| percent of the loops.
676 if (percent_loss > 0) {
677 if (counter_ == floor((100 / percent_loss) + 0.5)) {
678 counter_ = 0;
679 channel->set_lost_packet(true);
680 } else {
681 channel->set_lost_packet(false);
682 }
683 counter_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000684 }
685
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000686 // Add 10 msec to ACM
687 if (in_channels == 1) {
688 if (in_file_mono_->EndOfFile()) {
689 break;
690 }
691 in_file_mono_->Read10MsData(audio_frame);
692 } else {
693 if (in_file_stereo_->EndOfFile()) {
694 break;
695 }
696 in_file_stereo_->Read10MsData(audio_frame);
697 }
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000698 EXPECT_GE(acm_a_->Add10MsData(audio_frame), 0);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000699
tina.legrand@webrtc.org65d61c32014-06-05 13:42:51 +0000700 // Verify that the received packet size matches the settings.
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000701 rec_size = channel->payload_size();
702 if ((0 < rec_size) & (rec_size < 65535)) {
tina.legrand@webrtc.org65d61c32014-06-05 13:42:51 +0000703 if (strcmp(send_codec_name_, "opus") == 0) {
704 // Opus is a variable rate codec, hence calculate the average packet
705 // size, and later make sure the average is in the right range.
706 variable_bytes += rec_size;
707 variable_packets++;
708 } else {
709 // For fixed rate codecs, check that packet size is correct.
Yves Gerey665174f2018-06-19 15:03:05 +0200710 if ((rec_size != pack_size_bytes_ * out_channels) &&
711 (pack_size_bytes_ < 65535)) {
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000712 error_count++;
713 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000714 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000715 // Verify that the timestamp is updated with expected length
716 time_stamp_diff = channel->timestamp_diff();
717 if ((counter_ > 10) && (time_stamp_diff != pack_size_samp_)) {
718 error_count++;
719 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 }
721
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000722 // Run received side of ACM
henrik.lundind4ccb002016-05-17 12:21:55 -0700723 bool muted;
724 EXPECT_EQ(0, acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
725 ASSERT_FALSE(muted);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000726
727 // Write output speech to file
728 out_file_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700729 audio_frame.data(),
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000730 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000731 }
732
tina.legrand@webrtc.org3ddc9742012-06-27 09:25:50 +0000733 EXPECT_EQ(0, error_count);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000734
tina.legrand@webrtc.org65d61c32014-06-05 13:42:51 +0000735 // Check that packet size is in the right range for variable rate codecs,
736 // such as Opus.
737 if (variable_packets > 0) {
738 variable_bytes /= variable_packets;
Henrik Lundin4d682082015-12-10 16:24:39 +0100739 EXPECT_NEAR(variable_bytes, pack_size_bytes_, 18);
tina.legrand@webrtc.org65d61c32014-06-05 13:42:51 +0000740 }
741
andresp@webrtc.orgd0b436a2014-01-13 13:15:59 +0000742 if (in_file_mono_->EndOfFile()) {
743 in_file_mono_->Rewind();
744 }
745 if (in_file_stereo_->EndOfFile()) {
746 in_file_stereo_->Rewind();
747 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000748 // Reset in case we ended with a lost packet
749 channel->set_lost_packet(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000750}
751
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000752void TestStereo::OpenOutFile(int16_t test_number) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000753 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200754 rtc::StringBuilder file_stream;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000755 file_stream << webrtc::test::OutputPath() << "teststereo_out_" << test_number
Yves Gerey665174f2018-06-19 15:03:05 +0200756 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000757 file_name = file_stream.str();
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000758 out_file_.Open(file_name, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000761void TestStereo::DisplaySendReceiveCodec() {
762 auto send_codec = acm_a_->SendCodec();
763 if (test_mode_ != 0) {
764 ASSERT_TRUE(send_codec);
765 printf("%s -> ", send_codec->plname);
766 }
767 CodecInst receive_codec;
768 acm_b_->ReceiveCodec(&receive_codec);
769 if (test_mode_ != 0) {
770 printf("%s\n", receive_codec.plname);
771 }
772}
773
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000774} // namespace webrtc