blob: 91dbfd6f2c3a68ba0bed8da411e42be4bc562248 [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <ctype.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <stdio.h>
15#include <string.h>
16
kwiberg37478382016-02-14 20:40:57 -080017#include <memory>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg91957c12018-09-26 11:22:50 +020020#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/test/PCMFile.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "test/testsupport/file_utils.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#define MAX_FILE_NAME_LENGTH_BYTE 500
28
Karl Wiberg3a6b6bd2018-09-26 10:38:45 +020029TwoWayCommunication::TwoWayCommunication()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020030 : _acmA(AudioCodingModule::Create(
31 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
32 _acmRefA(AudioCodingModule::Create(
Karl Wiberg3a6b6bd2018-09-26 10:38:45 +020033 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))) {
henrik.lundin1bd0e032015-09-28 06:12:17 -070034 AudioCodingModule::Config config;
Henrik Lundin7687ad52018-07-02 10:14:46 +020035 // The clicks will be more obvious if time-stretching is not allowed.
36 // TODO(henrik.lundin) Really?
37 config.neteq_config.for_test_no_time_stretching = true;
ossue3525782016-05-25 07:37:43 -070038 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
henrik.lundin1bd0e032015-09-28 06:12:17 -070039 _acmB.reset(AudioCodingModule::Create(config));
henrik.lundin1bd0e032015-09-28 06:12:17 -070040 _acmRefB.reset(AudioCodingModule::Create(config));
41}
niklase@google.com470e71d2011-07-07 08:21:25 +000042
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000043TwoWayCommunication::~TwoWayCommunication() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000044 delete _channel_A2B;
45 delete _channel_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000046 delete _channelRef_A2B;
47 delete _channelRef_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000048 _inFileA.Close();
49 _inFileB.Close();
50 _outFileA.Close();
51 _outFileB.Close();
52 _outFileRefA.Close();
53 _outFileRefB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
Karl Wiberg91957c12018-09-26 11:22:50 +020056void TwoWayCommunication::SetUpAutotest(
57 AudioEncoderFactory* const encoder_factory,
58 const SdpAudioFormat& format1,
59 const int payload_type1,
60 const SdpAudioFormat& format2,
61 const int payload_type2) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000062 //--- Set A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020063 _acmA->SetEncoder(
64 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010065 _acmA->SetReceiveCodecs({{payload_type2, format2}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000066
67 //--- Set ref-A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020068 _acmRefA->SetEncoder(
69 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010070 _acmRefA->SetReceiveCodecs({{payload_type2, format2}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000071
72 //--- Set B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020073 _acmB->SetEncoder(
74 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010075 _acmB->SetReceiveCodecs({{payload_type1, format1}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000076
77 //--- Set ref-B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020078 _acmRefB->SetEncoder(
79 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010080 _acmRefB->SetReceiveCodecs({{payload_type1, format1}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000081
82 uint16_t frequencyHz;
83
84 //--- Input A and B
Yves Gerey665174f2018-06-19 15:03:05 +020085 std::string in_file_name =
86 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000087 frequencyHz = 16000;
88 _inFileA.Open(in_file_name, frequencyHz, "rb");
89 _inFileB.Open(in_file_name, frequencyHz, "rb");
90
91 //--- Output A
92 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
93 frequencyHz = 16000;
94 _outFileA.Open(output_file_a, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +020095 std::string output_ref_file_a =
96 webrtc::test::OutputPath() + "ref_outAutotestA.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000097 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
98
99 //--- Output B
100 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
101 frequencyHz = 16000;
102 _outFileB.Open(output_file_b, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +0200103 std::string output_ref_file_b =
104 webrtc::test::OutputPath() + "ref_outAutotestB.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000105 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
106
107 //--- Set A-to-B channel
108 _channel_A2B = new Channel;
109 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000110 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000111 //--- Do the same for the reference
112 _channelRef_A2B = new Channel;
113 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000114 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000115
116 //--- Set B-to-A channel
117 _channel_B2A = new Channel;
118 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000119 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000120 //--- Do the same for reference
121 _channelRef_B2A = new Channel;
122 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000123 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000124}
125
126void TwoWayCommunication::Perform() {
Karl Wiberg91957c12018-09-26 11:22:50 +0200127 const SdpAudioFormat format1("ISAC", 16000, 1);
128 const SdpAudioFormat format2("L16", 8000, 1);
129 constexpr int payload_type1 = 17, payload_type2 = 18;
130
131 auto encoder_factory = CreateBuiltinAudioEncoderFactory();
132
133 SetUpAutotest(encoder_factory.get(), format1, payload_type1, format2,
134 payload_type2);
135
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000136 unsigned int msecPassed = 0;
137 unsigned int secPassed = 0;
138
139 int32_t outFreqHzA = _outFileA.SamplingFrequency();
140 int32_t outFreqHzB = _outFileB.SamplingFrequency();
141
142 AudioFrame audioFrame;
143
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000144 // In the following loop we tests that the code can handle misuse of the APIs.
145 // In the middle of a session with data flowing between two sides, called A
Karl Wibergdd00f112015-08-25 09:37:04 +0200146 // and B, APIs will be called, and the code should continue to run, and be
147 // able to recover.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000148 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000149 msecPassed += 10;
150 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000151 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
152 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000154 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
Henrik Lundin45c64492015-03-30 19:00:44 +0200156 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000157 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700158 bool muted;
159 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
160 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000161 _outFileA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700162 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
163 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000164 _outFileRefA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700165 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
166 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000167 _outFileB.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700168 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
169 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000170 _outFileRefB.Write10MsData(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000172 // Update time counters each time a second of data has passed.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000173 if (msecPassed >= 1000) {
174 msecPassed = 0;
175 secPassed++;
176 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000177 // Re-register send codec on side B.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000178 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
Karl Wiberg91957c12018-09-26 11:22:50 +0200179 _acmB->SetEncoder(encoder_factory->MakeAudioEncoder(
180 payload_type2, format2, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000181 }
Karl Wibergdd00f112015-08-25 09:37:04 +0200182 // Initialize receiver on side A.
183 if (((secPassed % 7) == 6) && (msecPassed == 0))
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000184 EXPECT_EQ(0, _acmA->InitializeReceiver());
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000185 // Re-register codec on side A.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000186 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100187 _acmA->SetReceiveCodecs({{payload_type2, format2}});
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000189 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000192} // namespace webrtc