xref: /aosp_15_r20/external/webrtc/modules/audio_coding/test/TestVADDTX.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_TESTVADDTX_H_
12 #define MODULES_AUDIO_CODING_TEST_TESTVADDTX_H_
13 
14 #include <memory>
15 
16 #include "absl/strings/string_view.h"
17 #include "api/audio_codecs/audio_decoder_factory.h"
18 #include "api/audio_codecs/audio_encoder_factory.h"
19 #include "common_audio/vad/include/vad.h"
20 #include "modules/audio_coding/include/audio_coding_module.h"
21 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
22 #include "modules/audio_coding/test/Channel.h"
23 
24 namespace webrtc {
25 
26 // This class records the frame type, and delegates actual sending to the
27 // `next_` AudioPacketizationCallback.
28 class MonitoringAudioPacketizationCallback : public AudioPacketizationCallback {
29  public:
30   explicit MonitoringAudioPacketizationCallback(
31       AudioPacketizationCallback* next);
32 
33   int32_t SendData(AudioFrameType frame_type,
34                    uint8_t payload_type,
35                    uint32_t timestamp,
36                    const uint8_t* payload_data,
37                    size_t payload_len_bytes,
38                    int64_t absolute_capture_timestamp_ms) override;
39 
40   void PrintStatistics();
41   void ResetStatistics();
42   void GetStatistics(uint32_t* stats);
43 
44  private:
45   // 0 - kEmptyFrame
46   // 1 - kAudioFrameSpeech
47   // 2 - kAudioFrameCN
48   uint32_t counter_[3];
49   AudioPacketizationCallback* const next_;
50 };
51 
52 // TestVadDtx is to verify that VAD/DTX perform as they should. It runs through
53 // an audio file and check if the occurrence of various packet types follows
54 // expectation. TestVadDtx needs its derived class to implement the Perform()
55 // to put the test together.
56 class TestVadDtx {
57  public:
58   static const int kOutputFreqHz = 16000;
59 
60   TestVadDtx();
61 
62  protected:
63   // Returns true iff CN was added.
64   bool RegisterCodec(const SdpAudioFormat& codec_format,
65                      absl::optional<Vad::Aggressiveness> vad_mode);
66 
67   // Encoding a file and see if the numbers that various packets occur follow
68   // the expectation. Saves result to a file.
69   // expects[x] means
70   // -1 : do not care,
71   // 0  : there have been no packets of type `x`,
72   // 1  : there have been packets of type `x`,
73   // with `x` indicates the following packet types
74   // 0 - kEmptyFrame
75   // 1 - kAudioFrameSpeech
76   // 2 - kAudioFrameCN
77   void Run(absl::string_view in_filename,
78            int frequency,
79            int channels,
80            absl::string_view out_filename,
81            bool append,
82            const int* expects);
83 
84   const rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_;
85   const rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
86   std::unique_ptr<AudioCodingModule> acm_send_;
87   std::unique_ptr<AudioCodingModule> acm_receive_;
88   std::unique_ptr<Channel> channel_;
89   std::unique_ptr<MonitoringAudioPacketizationCallback> packetization_callback_;
90   uint32_t time_stamp_ = 0x12345678;
91 };
92 
93 // TestWebRtcVadDtx is to verify that the WebRTC VAD/DTX perform as they should.
94 class TestWebRtcVadDtx final : public TestVadDtx {
95  public:
96   TestWebRtcVadDtx();
97 
98   void Perform();
99 
100  private:
101   void RunTestCases(const SdpAudioFormat& codec_format);
102   void Test(bool new_outfile, bool expect_dtx_enabled);
103 
104   int output_file_num_;
105 };
106 
107 // TestOpusDtx is to verify that the Opus DTX performs as it should.
108 class TestOpusDtx final : public TestVadDtx {
109  public:
110   void Perform();
111 };
112 
113 }  // namespace webrtc
114 
115 #endif  // MODULES_AUDIO_CODING_TEST_TESTVADDTX_H_
116