Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / voice_engine / test / auto_test / voe_standard_test.cc
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/voice_engine/test/auto_test/voe_standard_test.h"
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "webrtc/engine_configurations.h"
17 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
18 #include "webrtc/system_wrappers/interface/event_wrapper.h"
19 #include "webrtc/voice_engine/include/voe_neteq_stats.h"
20 #include "webrtc/voice_engine/test/auto_test/automated_mode.h"
21 #include "webrtc/voice_engine/test/auto_test/voe_cpu_test.h"
22 #include "webrtc/voice_engine/test/auto_test/voe_stress_test.h"
23 #include "webrtc/voice_engine/test/auto_test/voe_test_defines.h"
24 #include "webrtc/voice_engine/test/auto_test/voe_unit_test.h"
25 #include "webrtc/voice_engine/voice_engine_defines.h"
26
27 DEFINE_bool(include_timing_dependent_tests, true,
28             "If true, we will include tests / parts of tests that are known "
29             "to break in slow execution environments (such as valgrind).");
30 DEFINE_bool(automated, false,
31             "If true, we'll run the automated tests we have in noninteractive "
32             "mode.");
33 DEFINE_bool(use_acm_version_2, false,
34             "If true, we'll run the tests with Audio Coding Module version 2.");
35
36 using namespace webrtc;
37
38 namespace voetest {
39
40 int dummy = 0;  // Dummy used in different functions to avoid warnings
41
42 void SubAPIManager::DisplayStatus() const {
43   TEST_LOG("Supported sub APIs:\n\n");
44   if (_base)
45     TEST_LOG("  Base\n");
46   if (_callReport)
47     TEST_LOG("  CallReport\n");
48   if (_codec)
49     TEST_LOG("  Codec\n");
50   if (_dtmf)
51     TEST_LOG("  Dtmf\n");
52   if (_encryption)
53     TEST_LOG("  Encryption\n");
54   if (_externalMedia)
55     TEST_LOG("  ExternalMedia\n");
56   if (_file)
57     TEST_LOG("  File\n");
58   if (_hardware)
59     TEST_LOG("  Hardware\n");
60   if (_netEqStats)
61     TEST_LOG("  NetEqStats\n");
62   if (_network)
63     TEST_LOG("  Network\n");
64   if (_rtp_rtcp)
65     TEST_LOG("  RTP_RTCP\n");
66   if (_videoSync)
67     TEST_LOG("  VideoSync\n");
68   if (_volumeControl)
69     TEST_LOG("  VolumeControl\n");
70   if (_apm)
71     TEST_LOG("  AudioProcessing\n");
72   ANL();
73   TEST_LOG("Excluded sub APIs:\n\n");
74   if (!_base)
75     TEST_LOG("  Base\n");
76   if (!_callReport)
77     TEST_LOG("  CallReport\n");
78   if (!_codec)
79     TEST_LOG("  Codec\n");
80   if (!_dtmf)
81     TEST_LOG("  Dtmf\n");
82   if (!_encryption)
83     TEST_LOG("  Encryption\n");
84   if (!_externalMedia)
85     TEST_LOG("  ExternamMedia\n");
86   if (!_file)
87     TEST_LOG("  File\n");
88   if (!_hardware)
89     TEST_LOG("  Hardware\n");
90   if (!_netEqStats)
91     TEST_LOG("  NetEqStats\n");
92   if (!_network)
93     TEST_LOG("  Network\n");
94   if (!_rtp_rtcp)
95     TEST_LOG("  RTP_RTCP\n");
96   if (!_videoSync)
97     TEST_LOG("  VideoSync\n");
98   if (!_volumeControl)
99     TEST_LOG("  VolumeControl\n");
100   if (!_apm)
101     TEST_LOG("  AudioProcessing\n");
102   ANL();
103 }
104
105 VoETestManager::VoETestManager()
106     : initialized_(false),
107       voice_engine_(NULL),
108       voe_base_(0),
109       voe_call_report_(0),
110       voe_codec_(0),
111       voe_dtmf_(0),
112       voe_encrypt_(0),
113       voe_xmedia_(0),
114       voe_file_(0),
115       voe_hardware_(0),
116       voe_network_(0),
117 #ifdef WEBRTC_VOICE_ENGINE_NETEQ_STATS_API
118       voe_neteq_stats_(NULL),
119 #endif
120       voe_rtp_rtcp_(0),
121       voe_vsync_(0),
122       voe_volume_control_(0),
123       voe_apm_(0) {
124 }
125
126 VoETestManager::~VoETestManager() {
127 }
128
129 bool VoETestManager::Init() {
130   if (initialized_)
131     return true;
132
133   if (VoiceEngine::SetTraceFile(NULL) != -1) {
134     // should not be possible to call a Trace method before the VoE is
135     // created
136     TEST_LOG("\nError at line: %i (VoiceEngine::SetTraceFile()"
137       "should fail)!\n", __LINE__);
138     return false;
139   }
140
141   // TODO(minyue): Remove when the old ACM is removed (latest 2014-04-01).
142   config_.Set<AudioCodingModuleFactory>(FLAGS_use_acm_version_2 ?
143       new NewAudioCodingModuleFactory() :
144       new AudioCodingModuleFactory());
145   voice_engine_ = VoiceEngine::Create(config_);
146
147   if (!voice_engine_) {
148     TEST_LOG("Failed to create VoiceEngine\n");
149     return false;
150   }
151
152   return true;
153 }
154
155 void VoETestManager::GetInterfaces() {
156   if (voice_engine_) {
157     voe_base_ = VoEBase::GetInterface(voice_engine_);
158     voe_codec_ = VoECodec::GetInterface(voice_engine_);
159     voe_volume_control_ = VoEVolumeControl::GetInterface(voice_engine_);
160     voe_dtmf_ = VoEDtmf::GetInterface(voice_engine_);
161     voe_rtp_rtcp_ = VoERTP_RTCP::GetInterface(voice_engine_);
162     voe_apm_ = VoEAudioProcessing::GetInterface(voice_engine_);
163     voe_network_ = VoENetwork::GetInterface(voice_engine_);
164     voe_file_ = VoEFile::GetInterface(voice_engine_);
165 #ifdef _TEST_VIDEO_SYNC_
166     voe_vsync_ = VoEVideoSync::GetInterface(voice_engine_);
167 #endif
168     voe_encrypt_ = VoEEncryption::GetInterface(voice_engine_);
169     voe_hardware_ = VoEHardware::GetInterface(voice_engine_);
170     // Set the audio layer to use in all tests
171     if (voe_hardware_) {
172       int res = voe_hardware_->SetAudioDeviceLayer(TESTED_AUDIO_LAYER);
173       if (res < 0) {
174         printf("\nERROR: failed to set audio layer to use in "
175           "testing\n");
176       } else {
177         printf("\nAudio layer %d will be used in testing\n",
178                TESTED_AUDIO_LAYER);
179       }
180     }
181 #ifdef _TEST_XMEDIA_
182     voe_xmedia_ = VoEExternalMedia::GetInterface(voice_engine_);
183 #endif
184 #ifdef _TEST_CALL_REPORT_
185     voe_call_report_ = VoECallReport::GetInterface(voice_engine_);
186 #endif
187 #ifdef WEBRTC_VOICE_ENGINE_NETEQ_STATS_API
188     voe_neteq_stats_ = VoENetEqStats::GetInterface(voice_engine_);
189 #endif
190   }
191 }
192
193 int VoETestManager::ReleaseInterfaces() {
194   bool releaseOK(true);
195
196   if (voe_base_) {
197     voe_base_->Release();
198     voe_base_ = NULL;
199   }
200   if (voe_codec_) {
201     voe_codec_->Release();
202     voe_codec_ = NULL;
203   }
204   if (voe_volume_control_) {
205     voe_volume_control_->Release();
206     voe_volume_control_ = NULL;
207   }
208   if (voe_dtmf_) {
209     voe_dtmf_->Release();
210     voe_dtmf_ = NULL;
211   }
212   if (voe_rtp_rtcp_) {
213     voe_rtp_rtcp_->Release();
214     voe_rtp_rtcp_ = NULL;
215   }
216   if (voe_apm_) {
217     voe_apm_->Release();
218     voe_apm_ = NULL;
219   }
220   if (voe_network_) {
221     voe_network_->Release();
222     voe_network_ = NULL;
223   }
224   if (voe_file_) {
225     voe_file_->Release();
226     voe_file_ = NULL;
227   }
228 #ifdef _TEST_VIDEO_SYNC_
229   if (voe_vsync_) {
230     voe_vsync_->Release();
231     voe_vsync_ = NULL;
232   }
233 #endif
234   if (voe_encrypt_) {
235     voe_encrypt_->Release();
236     voe_encrypt_ = NULL;
237   }
238   if (voe_hardware_) {
239     voe_hardware_->Release();
240     voe_hardware_ = NULL;
241   }
242 #ifdef _TEST_XMEDIA_
243   if (voe_xmedia_) {
244     voe_xmedia_->Release();
245     voe_xmedia_ = NULL;
246   }
247 #endif
248 #ifdef _TEST_CALL_REPORT_
249   if (voe_call_report_) {
250     voe_call_report_->Release();
251     voe_call_report_ = NULL;
252   }
253 #endif
254 #ifdef WEBRTC_VOICE_ENGINE_NETEQ_STATS_API
255   if (voe_neteq_stats_) {
256     voe_neteq_stats_->Release();
257     voe_neteq_stats_ = NULL;
258   }
259 #endif
260   if (false == VoiceEngine::Delete(voice_engine_)) {
261     TEST_LOG("\n\nVoiceEngine::Delete() failed. \n");
262     releaseOK = false;
263   }
264
265   if (VoiceEngine::SetTraceFile(NULL) != -1) {
266     TEST_LOG("\nError at line: %i (VoiceEngine::SetTraceFile()"
267       "should fail)!\n", __LINE__);
268   }
269
270   return (releaseOK == true) ? 0 : -1;
271 }
272
273 int run_auto_test(TestType test_type) {
274   assert(test_type != Standard);
275
276   SubAPIManager api_manager;
277   api_manager.DisplayStatus();
278
279   ////////////////////////////////////
280   // Create VoiceEngine and sub API:s
281
282   voetest::VoETestManager test_manager;
283   if (!test_manager.Init()) {
284     return -1;
285   }
286   test_manager.GetInterfaces();
287
288   int result = -1;
289   if (test_type == Stress) {
290     VoEStressTest stressTest(test_manager);
291     result = stressTest.DoTest();
292   } else if (test_type == Unit) {
293     VoEUnitTest unitTest(test_manager);
294     result = unitTest.DoTest();
295   } else if (test_type == CPU) {
296     VoECpuTest cpuTest(test_manager);
297     result = cpuTest.DoTest();
298   } else {
299     // Should never end up here
300     assert(false);
301   }
302
303   //////////////////
304   // Release/Delete
305
306   int release_ok = test_manager.ReleaseInterfaces();
307
308   if ((0 == result) && (release_ok != -1)) {
309     TEST_LOG("\n\n*** All tests passed *** \n\n");
310   } else {
311     TEST_LOG("\n\n*** Test failed! *** \n");
312   }
313
314   return 0;
315 }
316 }  // namespace voetest
317
318 int RunInManualMode() {
319   using namespace voetest;
320
321   SubAPIManager api_manager;
322   api_manager.DisplayStatus();
323
324   printf("----------------------------\n");
325   printf("Select type of test\n\n");
326   printf(" (0)  Quit\n");
327   printf(" (1)  Standard test\n");
328   printf(" (2)  [OBSOLETE: Extended test(s)...]\n");
329   printf(" (3)  Stress test(s)...\n");
330   printf(" (4)  Unit test(s)...\n");
331   printf(" (5)  CPU & memory reference test [Windows]...\n");
332   printf("\n: ");
333
334   int selection(0);
335   dummy = scanf("%d", &selection);
336
337   TestType test_type = Invalid;
338   switch (selection) {
339     case 0:
340       return 0;
341     case 1:
342       test_type = Standard;
343       break;
344     case 2:
345       break;
346     case 3:
347       test_type = Stress;
348       break;
349     case 4:
350       test_type = Unit;
351       break;
352     case 5:
353       test_type = CPU;
354       break;
355     default:
356       TEST_LOG("Invalid selection!\n");
357       return 0;
358   }
359
360   if (test_type == Standard) {
361     TEST_LOG("\n\n+++ Running standard tests +++\n\n");
362
363     // Currently, all googletest-rewritten tests are in the "automated" suite.
364     return RunInAutomatedMode();
365   }
366
367   // Function that can be called from other entry functions.
368   return run_auto_test(test_type);
369 }
370
371 // ----------------------------------------------------------------------------
372 //                                       main
373 // ----------------------------------------------------------------------------
374
375 #if !defined(WEBRTC_IOS)
376 int main(int argc, char** argv) {
377   // This function and RunInAutomatedMode is defined in automated_mode.cc
378   // to avoid macro clashes with googletest (for instance ASSERT_TRUE).
379   InitializeGoogleTest(&argc, argv);
380   google::ParseCommandLineFlags(&argc, &argv, true);
381
382   if (FLAGS_automated) {
383     return RunInAutomatedMode();
384   }
385
386   return RunInManualMode();
387 }
388 #endif //#if !defined(WEBRTC_IOS)