blob: 3af114e0ba8067dad56ac9563951c9ffe3534916 [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"
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#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) {
Fredrik Solenberg657b2962018-12-05 10:30:25 +010062
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000063 //--- Set A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020064 _acmA->SetEncoder(
65 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010066 _acmA->SetReceiveCodecs({{payload_type2, format2}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000067
68 //--- Set ref-A codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020069 _acmRefA->SetEncoder(
70 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010071 _acmRefA->SetReceiveCodecs({{payload_type2, format2}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000072
73 //--- Set B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020074 _acmB->SetEncoder(
75 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010076 _acmB->SetReceiveCodecs({{payload_type1, format1}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000077
78 //--- Set ref-B codecs
Karl Wiberg91957c12018-09-26 11:22:50 +020079 _acmRefB->SetEncoder(
80 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
Fredrik Solenberg657b2962018-12-05 10:30:25 +010081 _acmRefB->SetReceiveCodecs({{payload_type1, format1}});
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000082
83 uint16_t frequencyHz;
84
85 //--- Input A and B
Yves Gerey665174f2018-06-19 15:03:05 +020086 std::string in_file_name =
87 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000088 frequencyHz = 16000;
89 _inFileA.Open(in_file_name, frequencyHz, "rb");
90 _inFileB.Open(in_file_name, frequencyHz, "rb");
91
92 //--- Output A
93 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
94 frequencyHz = 16000;
95 _outFileA.Open(output_file_a, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +020096 std::string output_ref_file_a =
97 webrtc::test::OutputPath() + "ref_outAutotestA.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000098 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
99
100 //--- Output B
101 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
102 frequencyHz = 16000;
103 _outFileB.Open(output_file_b, frequencyHz, "wb");
Yves Gerey665174f2018-06-19 15:03:05 +0200104 std::string output_ref_file_b =
105 webrtc::test::OutputPath() + "ref_outAutotestB.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000106 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
107
108 //--- Set A-to-B channel
109 _channel_A2B = new Channel;
110 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000111 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000112 //--- Do the same for the reference
113 _channelRef_A2B = new Channel;
114 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000115 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000116
117 //--- Set B-to-A channel
118 _channel_B2A = new Channel;
119 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000120 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000121 //--- Do the same for reference
122 _channelRef_B2A = new Channel;
123 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000124 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000125}
126
127void TwoWayCommunication::Perform() {
Karl Wiberg91957c12018-09-26 11:22:50 +0200128 const SdpAudioFormat format1("ISAC", 16000, 1);
129 const SdpAudioFormat format2("L16", 8000, 1);
130 constexpr int payload_type1 = 17, payload_type2 = 18;
131
132 auto encoder_factory = CreateBuiltinAudioEncoderFactory();
133
134 SetUpAutotest(encoder_factory.get(), format1, payload_type1, format2,
135 payload_type2);
136
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000137 unsigned int msecPassed = 0;
138 unsigned int secPassed = 0;
139
140 int32_t outFreqHzA = _outFileA.SamplingFrequency();
141 int32_t outFreqHzB = _outFileB.SamplingFrequency();
142
143 AudioFrame audioFrame;
144
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000145 // In the following loop we tests that the code can handle misuse of the APIs.
146 // In the middle of a session with data flowing between two sides, called A
Karl Wibergdd00f112015-08-25 09:37:04 +0200147 // and B, APIs will be called, and the code should continue to run, and be
148 // able to recover.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000149 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000150 msecPassed += 10;
151 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000152 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
153 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000155 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
Henrik Lundin45c64492015-03-30 19:00:44 +0200157 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000158 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700159 bool muted;
160 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
161 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000162 _outFileA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700163 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
164 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000165 _outFileRefA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700166 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
167 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000168 _outFileB.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700169 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
170 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000171 _outFileRefB.Write10MsData(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000173 // Update time counters each time a second of data has passed.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000174 if (msecPassed >= 1000) {
175 msecPassed = 0;
176 secPassed++;
177 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000178 // Re-register send codec on side B.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000179 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
Karl Wiberg91957c12018-09-26 11:22:50 +0200180 _acmB->SetEncoder(encoder_factory->MakeAudioEncoder(
181 payload_type2, format2, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000182 }
Karl Wibergdd00f112015-08-25 09:37:04 +0200183 // Initialize receiver on side A.
184 if (((secPassed % 7) == 6) && (msecPassed == 0))
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000185 EXPECT_EQ(0, _acmA->InitializeReceiver());
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000186 // Re-register codec on side A.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000187 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100188 _acmA->SetReceiveCodecs({{payload_type2, format2}});
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000190 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000193} // namespace webrtc