1 /*
2  *  Copyright (c) 2011 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 #ifndef MODULES_AUDIO_CODING_TEST_UTILITY_H_
12 #define MODULES_AUDIO_CODING_TEST_UTILITY_H_
13 
14 #include "modules/audio_coding/include/audio_coding_module.h"
15 #include "test/gtest.h"
16 
17 namespace webrtc {
18 
19 //-----------------------------
20 #define CHECK_ERROR(f)                                                         \
21   do {                                                                         \
22     EXPECT_GE(f, 0) << "Error Calling API";                                    \
23   } while(0)
24 
25 //-----------------------------
26 #define CHECK_PROTECTED(f)                                                     \
27   do {                                                                         \
28     if (f >= 0) {                                                              \
29       ADD_FAILURE() << "Error Calling API";                                    \
30     } else {                                                                   \
31       printf("An expected error is caught.\n");                                \
32     }                                                                          \
33   } while(0)
34 
35 //----------------------------
36 #define CHECK_ERROR_MT(f)                                                      \
37   do {                                                                         \
38     if (f < 0) {                                                               \
39       fprintf(stderr, "Error Calling API in file %s at line %d \n",            \
40               __FILE__, __LINE__);                                             \
41     }                                                                          \
42   } while(0)
43 
44 //----------------------------
45 #define CHECK_PROTECTED_MT(f)                                                  \
46   do {                                                                         \
47     if (f >= 0) {                                                              \
48       fprintf(stderr, "Error Calling API in file %s at line %d \n",            \
49               __FILE__, __LINE__);                                             \
50     } else {                                                                   \
51       printf("An expected error is caught.\n");                                \
52     }                                                                          \
53   } while(0)
54 
55 #define DELETE_POINTER(p)                                                      \
56   do {                                                                         \
57     if (p != NULL) {                                                           \
58       delete p;                                                                \
59       p = NULL;                                                                \
60     }                                                                          \
61   } while(0)
62 
63 class ACMTestTimer {
64  public:
65   ACMTestTimer();
66   ~ACMTestTimer();
67 
68   void Reset();
69   void Tick10ms();
70   void Tick1ms();
71   void Tick100ms();
72   void Tick1sec();
73   void CurrentTimeHMS(char* currTime);
74   void CurrentTime(unsigned long& h, unsigned char& m, unsigned char& s,
75                    unsigned short& ms);
76 
77  private:
78   void Adjust();
79 
80   unsigned short _msec;
81   unsigned char _sec;
82   unsigned char _min;
83   unsigned long _hour;
84 };
85 
86 // To avoid clashes with CircularBuffer in APM.
87 namespace test {
88 
89 class CircularBuffer {
90  public:
91   CircularBuffer(uint32_t len);
92   ~CircularBuffer();
93 
94   void SetArithMean(bool enable);
95   void SetVariance(bool enable);
96 
97   void Update(const double newVal);
98   void IsBufferFull();
99 
100   int16_t Variance(double& var);
101   int16_t ArithMean(double& mean);
102 
103  protected:
104   double* _buff;
105   uint32_t _idx;
106   uint32_t _buffLen;
107 
108   bool _buffIsFull;
109   bool _calcAvg;
110   bool _calcVar;
111   double _sum;
112   double _sumSqr;
113 };
114 
115 }  // namespace test
116 
117 int16_t ChooseCodec(CodecInst& codecInst);
118 
119 void PrintCodecs();
120 
121 bool FixedPayloadTypeCodec(const char* payloadName);
122 
123 class VADCallback : public ACMVADCallback {
124  public:
125   VADCallback();
126 
127   int32_t InFrameType(FrameType frame_type) override;
128 
129   void PrintFrameTypes();
130   void Reset();
131 
132  private:
133   uint32_t _numFrameTypes[5];
134 };
135 
136 }  // namespace webrtc
137 
138 #endif  // MODULES_AUDIO_CODING_TEST_UTILITY_H_
139