blob: b42415a21a7c0b08b325d26b1e177a89fb53ac5d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org91b359e2012-02-28 17:26:14 +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
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +000011#include "TwoWayCommunication.h"
12
niklase@google.com470e71d2011-07-07 08:21:25 +000013#include <stdio.h>
14#include <string.h>
15
kwiberg37478382016-02-14 20:40:57 -080016#include <memory>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg91957c12018-09-26 11:22:50 +020019#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/test/PCMFile.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "test/testsupport/file_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000024namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26#define MAX_FILE_NAME_LENGTH_BYTE 500
27
Karl Wiberg3a6b6bd2018-09-26 10:38:45 +020028TwoWayCommunication::TwoWayCommunication()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020029 : _acmA(AudioCodingModule::Create(
30 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
31 _acmRefA(AudioCodingModule::Create(
Karl Wiberg3a6b6bd2018-09-26 10:38:45 +020032 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))) {
henrik.lundin1bd0e032015-09-28 06:12:17 -070033 AudioCodingModule::Config config;
Henrik Lundin7687ad52018-07-02 10:14:46 +020034 // The clicks will be more obvious if time-stretching is not allowed.
35 // TODO(henrik.lundin) Really?
36 config.neteq_config.for_test_no_time_stretching = true;
ossue3525782016-05-25 07:37:43 -070037 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
henrik.lundin1bd0e032015-09-28 06:12:17 -070038 _acmB.reset(AudioCodingModule::Create(config));
henrik.lundin1bd0e032015-09-28 06:12:17 -070039 _acmRefB.reset(AudioCodingModule::Create(config));
40}
niklase@google.com470e71d2011-07-07 08:21:25 +000041
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000042TwoWayCommunication::~TwoWayCommunication() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000043 delete _channel_A2B;
44 delete _channel_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000045 delete _channelRef_A2B;
46 delete _channelRef_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000047 _inFileA.Close();
48 _inFileB.Close();
49 _outFileA.Close();
50 _outFileB.Close();
51 _outFileRefA.Close();
52 _outFileRefB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +000053}
54
Karl Wiberg91957c12018-09-26 11:22:50 +020055void TwoWayCommunication::SetUpAutotest(
56 AudioEncoderFactory* const encoder_factory,
57 const SdpAudioFormat& format1,
58 const int payload_type1,
59 const SdpAudioFormat& format2,
60 const int payload_type2) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000061 //--- Set A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020062 _acmA->SetEncoder(
63 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010064 _acmA->SetReceiveCodecs({{payload_type2, format2}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000065
66 //--- Set ref-A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020067 _acmRefA->SetEncoder(
68 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010069 _acmRefA->SetReceiveCodecs({{payload_type2, format2}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000070
71 //--- Set B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020072 _acmB->SetEncoder(
73 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010074 _acmB->SetReceiveCodecs({{payload_type1, format1}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000075
76 //--- Set ref-B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020077 _acmRefB->SetEncoder(
78 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010079 _acmRefB->SetReceiveCodecs({{payload_type1, format1}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000080
81 uint16_t frequencyHz;
82
83 //--- Input A and B
Yves Gerey665174f2018-06-19 15:03:05 +020084 std::string in_file_name =
85 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000086 frequencyHz = 16000;
87 _inFileA.Open(in_file_name, frequencyHz, "rb");
88 _inFileB.Open(in_file_name, frequencyHz, "rb");
89
90 //--- Output A
91 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
92 frequencyHz = 16000;
93 _outFileA.Open(output_file_a, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +020094 std::string output_ref_file_a =
95 webrtc::test::OutputPath() + "ref_outAutotestA.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000096 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
97
98 //--- Output B
99 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
100 frequencyHz = 16000;
101 _outFileB.Open(output_file_b, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +0200102 std::string output_ref_file_b =
103 webrtc::test::OutputPath() + "ref_outAutotestB.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000104 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
105
106 //--- Set A-to-B channel
107 _channel_A2B = new Channel;
108 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000109 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000110 //--- Do the same for the reference
111 _channelRef_A2B = new Channel;
112 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000113 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000114
115 //--- Set B-to-A channel
116 _channel_B2A = new Channel;
117 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000118 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000119 //--- Do the same for reference
120 _channelRef_B2A = new Channel;
121 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000122 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000123}
124
125void TwoWayCommunication::Perform() {
Karl Wiberg91957c12018-09-26 11:22:50 +0200126 const SdpAudioFormat format1("ISAC", 16000, 1);
127 const SdpAudioFormat format2("L16", 8000, 1);
128 constexpr int payload_type1 = 17, payload_type2 = 18;
129
130 auto encoder_factory = CreateBuiltinAudioEncoderFactory();
131
132 SetUpAutotest(encoder_factory.get(), format1, payload_type1, format2,
133 payload_type2);
134
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000135 unsigned int msecPassed = 0;
136 unsigned int secPassed = 0;
137
138 int32_t outFreqHzA = _outFileA.SamplingFrequency();
139 int32_t outFreqHzB = _outFileB.SamplingFrequency();
140
141 AudioFrame audioFrame;
142
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000143 // In the following loop we tests that the code can handle misuse of the APIs.
144 // In the middle of a session with data flowing between two sides, called A
Karl Wibergdd00f112015-08-25 09:37:04 +0200145 // and B, APIs will be called, and the code should continue to run, and be
146 // able to recover.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000147 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000148 msecPassed += 10;
149 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000150 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
151 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000153 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
Henrik Lundin45c64492015-03-30 19:00:44 +0200155 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000156 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700157 bool muted;
158 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
159 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000160 _outFileA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700161 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
162 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000163 _outFileRefA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700164 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
165 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000166 _outFileB.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700167 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
168 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000169 _outFileRefB.Write10MsData(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000171 // Update time counters each time a second of data has passed.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000172 if (msecPassed >= 1000) {
173 msecPassed = 0;
174 secPassed++;
175 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000176 // Re-register send codec on side B.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000177 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
Karl Wiberg91957c12018-09-26 11:22:50 +0200178 _acmB->SetEncoder(encoder_factory->MakeAudioEncoder(
179 payload_type2, format2, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 }
Karl Wibergdd00f112015-08-25 09:37:04 +0200181 // Initialize receiver on side A.
182 if (((secPassed % 7) == 6) && (msecPassed == 0))
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000183 EXPECT_EQ(0, _acmA->InitializeReceiver());
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000184 // Re-register codec on side A.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000185 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100186 _acmA->SetReceiveCodecs({{payload_type2, format2}});
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000188 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000191} // namespace webrtc