OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
11 #include "webrtc/modules/audio_coding/main/test/EncodeDecodeTest.h" | |
12 | |
13 #include <sstream> | |
14 #include <stdio.h> | |
15 #include <stdlib.h> | |
16 | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "webrtc/base/scoped_ptr.h" | |
19 #include "webrtc/common_types.h" | |
20 #include "webrtc/modules/audio_coding/main/include/audio_coding_module.h" | |
21 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h" | |
22 #include "webrtc/modules/audio_coding/main/test/utility.h" | |
23 #include "webrtc/system_wrappers/include/trace.h" | |
24 #include "webrtc/test/testsupport/fileutils.h" | |
25 | |
26 namespace webrtc { | |
27 | |
28 TestPacketization::TestPacketization(RTPStream *rtpStream, uint16_t frequency) | |
29 : _rtpStream(rtpStream), | |
30 _frequency(frequency), | |
31 _seqNo(0) { | |
32 } | |
33 | |
34 TestPacketization::~TestPacketization() { | |
35 } | |
36 | |
37 int32_t TestPacketization::SendData( | |
38 const FrameType /* frameType */, const uint8_t payloadType, | |
39 const uint32_t timeStamp, const uint8_t* payloadData, | |
40 const size_t payloadSize, | |
41 const RTPFragmentationHeader* /* fragmentation */) { | |
42 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize, | |
43 _frequency); | |
44 return 1; | |
45 } | |
46 | |
47 Sender::Sender() | |
48 : _acm(NULL), | |
49 _pcmFile(), | |
50 _audioFrame(), | |
51 _packetization(NULL) { | |
52 } | |
53 | |
54 void Sender::Setup(AudioCodingModule *acm, RTPStream *rtpStream, | |
55 std::string in_file_name, int sample_rate, int channels) { | |
56 struct CodecInst sendCodec; | |
57 int noOfCodecs = acm->NumberOfCodecs(); | |
58 int codecNo; | |
59 | |
60 // Open input file | |
61 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm"); | |
62 _pcmFile.Open(file_name, sample_rate, "rb"); | |
63 if (channels == 2) { | |
64 _pcmFile.ReadStereo(true); | |
65 } | |
66 | |
67 // Set the codec for the current test. | |
68 if ((testMode == 0) || (testMode == 1)) { | |
69 // Set the codec id. | |
70 codecNo = codeId; | |
71 } else { | |
72 // Choose codec on command line. | |
73 printf("List of supported codec.\n"); | |
74 for (int n = 0; n < noOfCodecs; n++) { | |
75 EXPECT_EQ(0, acm->Codec(n, &sendCodec)); | |
76 printf("%d %s\n", n, sendCodec.plname); | |
77 } | |
78 printf("Choose your codec:"); | |
79 ASSERT_GT(scanf("%d", &codecNo), 0); | |
80 } | |
81 | |
82 EXPECT_EQ(0, acm->Codec(codecNo, &sendCodec)); | |
83 | |
84 sendCodec.channels = channels; | |
85 | |
86 EXPECT_EQ(0, acm->RegisterSendCodec(sendCodec)); | |
87 _packetization = new TestPacketization(rtpStream, sendCodec.plfreq); | |
88 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization)); | |
89 | |
90 _acm = acm; | |
91 } | |
92 | |
93 void Sender::Teardown() { | |
94 _pcmFile.Close(); | |
95 delete _packetization; | |
96 } | |
97 | |
98 bool Sender::Add10MsData() { | |
99 if (!_pcmFile.EndOfFile()) { | |
100 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0); | |
101 int32_t ok = _acm->Add10MsData(_audioFrame); | |
102 EXPECT_GE(ok, 0); | |
103 return ok >= 0 ? true : false; | |
104 } | |
105 return false; | |
106 } | |
107 | |
108 void Sender::Run() { | |
109 while (true) { | |
110 if (!Add10MsData()) { | |
111 break; | |
112 } | |
113 } | |
114 } | |
115 | |
116 Receiver::Receiver() | |
117 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO), | |
118 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) { | |
119 } | |
120 | |
121 void Receiver::Setup(AudioCodingModule *acm, RTPStream *rtpStream, | |
122 std::string out_file_name, int channels) { | |
123 struct CodecInst recvCodec = CodecInst(); | |
124 int noOfCodecs; | |
125 EXPECT_EQ(0, acm->InitializeReceiver()); | |
126 | |
127 noOfCodecs = acm->NumberOfCodecs(); | |
128 for (int i = 0; i < noOfCodecs; i++) { | |
129 EXPECT_EQ(0, acm->Codec(i, &recvCodec)); | |
130 if (recvCodec.channels == channels) | |
131 EXPECT_EQ(0, acm->RegisterReceiveCodec(recvCodec)); | |
132 // Forces mono/stereo for Opus. | |
133 if (!strcmp(recvCodec.plname, "opus")) { | |
134 recvCodec.channels = channels; | |
135 EXPECT_EQ(0, acm->RegisterReceiveCodec(recvCodec)); | |
136 } | |
137 } | |
138 | |
139 int playSampFreq; | |
140 std::string file_name; | |
141 std::stringstream file_stream; | |
142 file_stream << webrtc::test::OutputPath() << out_file_name | |
143 << static_cast<int>(codeId) << ".pcm"; | |
144 file_name = file_stream.str(); | |
145 _rtpStream = rtpStream; | |
146 | |
147 if (testMode == 1) { | |
148 playSampFreq = recvCodec.plfreq; | |
149 _pcmFile.Open(file_name, recvCodec.plfreq, "wb+"); | |
150 } else if (testMode == 0) { | |
151 playSampFreq = 32000; | |
152 _pcmFile.Open(file_name, 32000, "wb+"); | |
153 } else { | |
154 printf("\nValid output frequencies:\n"); | |
155 printf("8000\n16000\n32000\n-1,"); | |
156 printf("which means output frequency equal to received signal frequency"); | |
157 printf("\n\nChoose output sampling frequency: "); | |
158 ASSERT_GT(scanf("%d", &playSampFreq), 0); | |
159 file_name = webrtc::test::OutputPath() + out_file_name + ".pcm"; | |
160 _pcmFile.Open(file_name, playSampFreq, "wb+"); | |
161 } | |
162 | |
163 _realPayloadSizeBytes = 0; | |
164 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO]; | |
165 _frequency = playSampFreq; | |
166 _acm = acm; | |
167 _firstTime = true; | |
168 } | |
169 | |
170 void Receiver::Teardown() { | |
171 delete[] _playoutBuffer; | |
172 _pcmFile.Close(); | |
173 if (testMode > 1) { | |
174 Trace::ReturnTrace(); | |
175 } | |
176 } | |
177 | |
178 bool Receiver::IncomingPacket() { | |
179 if (!_rtpStream->EndOfFile()) { | |
180 if (_firstTime) { | |
181 _firstTime = false; | |
182 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload, | |
183 _payloadSizeBytes, &_nextTime); | |
184 if (_realPayloadSizeBytes == 0) { | |
185 if (_rtpStream->EndOfFile()) { | |
186 _firstTime = true; | |
187 return true; | |
188 } else { | |
189 return false; | |
190 } | |
191 } | |
192 } | |
193 | |
194 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, | |
195 _rtpInfo)); | |
196 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload, | |
197 _payloadSizeBytes, &_nextTime); | |
198 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) { | |
199 _firstTime = true; | |
200 } | |
201 } | |
202 return true; | |
203 } | |
204 | |
205 bool Receiver::PlayoutData() { | |
206 AudioFrame audioFrame; | |
207 | |
208 int32_t ok =_acm->PlayoutData10Ms(_frequency, &audioFrame); | |
209 EXPECT_EQ(0, ok); | |
210 if (ok < 0){ | |
211 return false; | |
212 } | |
213 if (_playoutLengthSmpls == 0) { | |
214 return false; | |
215 } | |
216 _pcmFile.Write10MsData(audioFrame.data_, | |
217 audioFrame.samples_per_channel_ * audioFrame.num_channels_); | |
218 return true; | |
219 } | |
220 | |
221 void Receiver::Run() { | |
222 uint8_t counter500Ms = 50; | |
223 uint32_t clock = 0; | |
224 | |
225 while (counter500Ms > 0) { | |
226 if (clock == 0 || clock >= _nextTime) { | |
227 EXPECT_TRUE(IncomingPacket()); | |
228 if (clock == 0) { | |
229 clock = _nextTime; | |
230 } | |
231 } | |
232 if ((clock % 10) == 0) { | |
233 if (!PlayoutData()) { | |
234 clock++; | |
235 continue; | |
236 } | |
237 } | |
238 if (_rtpStream->EndOfFile()) { | |
239 counter500Ms--; | |
240 } | |
241 clock++; | |
242 } | |
243 } | |
244 | |
245 EncodeDecodeTest::EncodeDecodeTest() { | |
246 _testMode = 2; | |
247 Trace::CreateTrace(); | |
248 Trace::SetTraceFile( | |
249 (webrtc::test::OutputPath() + "acm_encdec_trace.txt").c_str()); | |
250 } | |
251 | |
252 EncodeDecodeTest::EncodeDecodeTest(int testMode) { | |
253 //testMode == 0 for autotest | |
254 //testMode == 1 for testing all codecs/parameters | |
255 //testMode > 1 for specific user-input test (as it was used before) | |
256 _testMode = testMode; | |
257 if (_testMode != 0) { | |
258 Trace::CreateTrace(); | |
259 Trace::SetTraceFile( | |
260 (webrtc::test::OutputPath() + "acm_encdec_trace.txt").c_str()); | |
261 } | |
262 } | |
263 | |
264 void EncodeDecodeTest::Perform() { | |
265 int numCodecs = 1; | |
266 int codePars[3]; // Frequency, packet size, rate. | |
267 int numPars[52]; // Number of codec parameters sets (freq, pacsize, rate) | |
268 // to test, for a given codec. | |
269 | |
270 codePars[0] = 0; | |
271 codePars[1] = 0; | |
272 codePars[2] = 0; | |
273 | |
274 rtc::scoped_ptr<AudioCodingModule> acm(AudioCodingModule::Create(0)); | |
275 struct CodecInst sendCodecTmp; | |
276 numCodecs = acm->NumberOfCodecs(); | |
277 | |
278 if (_testMode != 2) { | |
279 for (int n = 0; n < numCodecs; n++) { | |
280 EXPECT_EQ(0, acm->Codec(n, &sendCodecTmp)); | |
281 if (STR_CASE_CMP(sendCodecTmp.plname, "telephone-event") == 0) { | |
282 numPars[n] = 0; | |
283 } else if (STR_CASE_CMP(sendCodecTmp.plname, "cn") == 0) { | |
284 numPars[n] = 0; | |
285 } else if (STR_CASE_CMP(sendCodecTmp.plname, "red") == 0) { | |
286 numPars[n] = 0; | |
287 } else if (sendCodecTmp.channels == 2) { | |
288 numPars[n] = 0; | |
289 } else { | |
290 numPars[n] = 1; | |
291 } | |
292 } | |
293 } else { | |
294 numCodecs = 1; | |
295 numPars[0] = 1; | |
296 } | |
297 | |
298 _receiver.testMode = _testMode; | |
299 | |
300 // Loop over all mono codecs: | |
301 for (int codeId = 0; codeId < numCodecs; codeId++) { | |
302 // Only encode using real mono encoders, not telephone-event and cng. | |
303 for (int loopPars = 1; loopPars <= numPars[codeId]; loopPars++) { | |
304 // Encode all data to file. | |
305 std::string fileName = EncodeToFile(1, codeId, codePars, _testMode); | |
306 | |
307 RTPFile rtpFile; | |
308 rtpFile.Open(fileName.c_str(), "rb"); | |
309 | |
310 _receiver.codeId = codeId; | |
311 | |
312 rtpFile.ReadHeader(); | |
313 _receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1); | |
314 _receiver.Run(); | |
315 _receiver.Teardown(); | |
316 rtpFile.Close(); | |
317 } | |
318 } | |
319 | |
320 // End tracing. | |
321 if (_testMode == 1) { | |
322 Trace::ReturnTrace(); | |
323 } | |
324 } | |
325 | |
326 std::string EncodeDecodeTest::EncodeToFile(int fileType, | |
327 int codeId, | |
328 int* codePars, | |
329 int testMode) { | |
330 rtc::scoped_ptr<AudioCodingModule> acm(AudioCodingModule::Create(1)); | |
331 RTPFile rtpFile; | |
332 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(), | |
333 "encode_decode_rtp"); | |
334 rtpFile.Open(fileName.c_str(), "wb+"); | |
335 rtpFile.WriteHeader(); | |
336 | |
337 // Store for auto_test and logging. | |
338 _sender.testMode = testMode; | |
339 _sender.codeId = codeId; | |
340 | |
341 _sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000, 1); | |
342 if (acm->SendCodec()) { | |
343 _sender.Run(); | |
344 } | |
345 _sender.Teardown(); | |
346 rtpFile.Close(); | |
347 | |
348 return fileName; | |
349 } | |
350 | |
351 } // namespace webrtc | |
OLD | NEW |