blob: 2079a94d9cf22546c56eb7bc65550223c908bb7e [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
niklase@google.com470e71d2011-07-07 08:21:25 +000019#ifdef WIN32
20#include <Windows.h>
21#endif
22
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg91957c12018-09-26 11:22:50 +020024#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020025#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_coding/test/PCMFile.h"
27#include "modules/audio_coding/test/utility.h"
28#include "test/gtest.h"
29#include "test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000030
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000031namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000032
33#define MAX_FILE_NAME_LENGTH_BYTE 500
34
Karl Wiberg3a6b6bd2018-09-26 10:38:45 +020035TwoWayCommunication::TwoWayCommunication()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020036 : _acmA(AudioCodingModule::Create(
37 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
38 _acmRefA(AudioCodingModule::Create(
Karl Wiberg3a6b6bd2018-09-26 10:38:45 +020039 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))) {
henrik.lundin1bd0e032015-09-28 06:12:17 -070040 AudioCodingModule::Config config;
Henrik Lundin7687ad52018-07-02 10:14:46 +020041 // The clicks will be more obvious if time-stretching is not allowed.
42 // TODO(henrik.lundin) Really?
43 config.neteq_config.for_test_no_time_stretching = true;
ossue3525782016-05-25 07:37:43 -070044 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
henrik.lundin1bd0e032015-09-28 06:12:17 -070045 _acmB.reset(AudioCodingModule::Create(config));
henrik.lundin1bd0e032015-09-28 06:12:17 -070046 _acmRefB.reset(AudioCodingModule::Create(config));
47}
niklase@google.com470e71d2011-07-07 08:21:25 +000048
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000049TwoWayCommunication::~TwoWayCommunication() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000050 delete _channel_A2B;
51 delete _channel_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000052 delete _channelRef_A2B;
53 delete _channelRef_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000054 _inFileA.Close();
55 _inFileB.Close();
56 _outFileA.Close();
57 _outFileB.Close();
58 _outFileRefA.Close();
59 _outFileRefB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
61
Karl Wiberg91957c12018-09-26 11:22:50 +020062void TwoWayCommunication::SetUpAutotest(
63 AudioEncoderFactory* const encoder_factory,
64 const SdpAudioFormat& format1,
65 const int payload_type1,
66 const SdpAudioFormat& format2,
67 const int payload_type2) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000068 //--- Set A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020069 _acmA->SetEncoder(
70 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
71 EXPECT_EQ(true, _acmA->RegisterReceiveCodec(payload_type2, format2));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000072
73 //--- Set ref-A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020074 _acmRefA->SetEncoder(
75 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
76 EXPECT_EQ(true, _acmRefA->RegisterReceiveCodec(payload_type2, format2));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000077
78 //--- Set B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020079 _acmB->SetEncoder(
80 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
81 EXPECT_EQ(true, _acmB->RegisterReceiveCodec(payload_type1, format1));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000082
83 //--- Set ref-B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020084 _acmRefB->SetEncoder(
85 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
86 EXPECT_EQ(true, _acmRefB->RegisterReceiveCodec(payload_type1, format1));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000087
88 uint16_t frequencyHz;
89
90 //--- Input A and B
Yves Gerey665174f2018-06-19 15:03:05 +020091 std::string in_file_name =
92 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000093 frequencyHz = 16000;
94 _inFileA.Open(in_file_name, frequencyHz, "rb");
95 _inFileB.Open(in_file_name, frequencyHz, "rb");
96
97 //--- Output A
98 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
99 frequencyHz = 16000;
100 _outFileA.Open(output_file_a, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +0200101 std::string output_ref_file_a =
102 webrtc::test::OutputPath() + "ref_outAutotestA.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000103 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
104
105 //--- Output B
106 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
107 frequencyHz = 16000;
108 _outFileB.Open(output_file_b, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +0200109 std::string output_ref_file_b =
110 webrtc::test::OutputPath() + "ref_outAutotestB.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000111 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
112
113 //--- Set A-to-B channel
114 _channel_A2B = new Channel;
115 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000116 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000117 //--- Do the same for the reference
118 _channelRef_A2B = new Channel;
119 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000120 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000121
122 //--- Set B-to-A channel
123 _channel_B2A = new Channel;
124 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000125 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000126 //--- Do the same for reference
127 _channelRef_B2A = new Channel;
128 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000129 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000130}
131
132void TwoWayCommunication::Perform() {
Karl Wiberg91957c12018-09-26 11:22:50 +0200133 const SdpAudioFormat format1("ISAC", 16000, 1);
134 const SdpAudioFormat format2("L16", 8000, 1);
135 constexpr int payload_type1 = 17, payload_type2 = 18;
136
137 auto encoder_factory = CreateBuiltinAudioEncoderFactory();
138
139 SetUpAutotest(encoder_factory.get(), format1, payload_type1, format2,
140 payload_type2);
141
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000142 unsigned int msecPassed = 0;
143 unsigned int secPassed = 0;
144
145 int32_t outFreqHzA = _outFileA.SamplingFrequency();
146 int32_t outFreqHzB = _outFileB.SamplingFrequency();
147
148 AudioFrame audioFrame;
149
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000150 // In the following loop we tests that the code can handle misuse of the APIs.
151 // In the middle of a session with data flowing between two sides, called A
Karl Wibergdd00f112015-08-25 09:37:04 +0200152 // and B, APIs will be called, and the code should continue to run, and be
153 // able to recover.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000154 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000155 msecPassed += 10;
156 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000157 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
158 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000160 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
Henrik Lundin45c64492015-03-30 19:00:44 +0200162 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000163 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700164 bool muted;
165 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
166 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000167 _outFileA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700168 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
169 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000170 _outFileRefA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700171 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
172 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000173 _outFileB.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700174 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
175 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000176 _outFileRefB.Write10MsData(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000178 // Update time counters each time a second of data has passed.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000179 if (msecPassed >= 1000) {
180 msecPassed = 0;
181 secPassed++;
182 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000183 // Re-register send codec on side B.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000184 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
Karl Wiberg91957c12018-09-26 11:22:50 +0200185 _acmB->SetEncoder(encoder_factory->MakeAudioEncoder(
186 payload_type2, format2, absl::nullopt));
kwiberg1fd4a4a2015-11-03 11:20:50 -0800187 EXPECT_TRUE(_acmB->SendCodec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 }
Karl Wibergdd00f112015-08-25 09:37:04 +0200189 // Initialize receiver on side A.
190 if (((secPassed % 7) == 6) && (msecPassed == 0))
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000191 EXPECT_EQ(0, _acmA->InitializeReceiver());
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000192 // Re-register codec on side A.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000193 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
Karl Wiberg91957c12018-09-26 11:22:50 +0200194 EXPECT_EQ(true, _acmA->RegisterReceiveCodec(payload_type2, format2));
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000196 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000197}
198
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000199} // namespace webrtc