xref: /aosp_15_r20/external/webrtc/test/mock_audio_encoder.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 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 TEST_MOCK_AUDIO_ENCODER_H_
12 #define TEST_MOCK_AUDIO_ENCODER_H_
13 
14 #include <string>
15 
16 #include "api/array_view.h"
17 #include "api/audio_codecs/audio_encoder.h"
18 #include "test/gmock.h"
19 
20 namespace webrtc {
21 
22 class MockAudioEncoder : public AudioEncoder {
23  public:
24   MockAudioEncoder();
25   ~MockAudioEncoder();
26   MOCK_METHOD(int, SampleRateHz, (), (const, override));
27   MOCK_METHOD(size_t, NumChannels, (), (const, override));
28   MOCK_METHOD(int, RtpTimestampRateHz, (), (const, override));
29   MOCK_METHOD(size_t, Num10MsFramesInNextPacket, (), (const, override));
30   MOCK_METHOD(size_t, Max10MsFramesInAPacket, (), (const, override));
31   MOCK_METHOD(int, GetTargetBitrate, (), (const, override));
32   MOCK_METHOD((absl::optional<std::pair<TimeDelta, TimeDelta>>),
33               GetFrameLengthRange,
34               (),
35               (const, override));
36 
37   MOCK_METHOD(void, Reset, (), (override));
38   MOCK_METHOD(bool, SetFec, (bool enable), (override));
39   MOCK_METHOD(bool, SetDtx, (bool enable), (override));
40   MOCK_METHOD(bool, SetApplication, (Application application), (override));
41   MOCK_METHOD(void, SetMaxPlaybackRate, (int frequency_hz), (override));
42   MOCK_METHOD(void,
43               OnReceivedUplinkBandwidth,
44               (int target_audio_bitrate_bps,
45                absl::optional<int64_t> probing_interval_ms),
46               (override));
47   MOCK_METHOD(void,
48               OnReceivedUplinkPacketLossFraction,
49               (float uplink_packet_loss_fraction),
50               (override));
51   MOCK_METHOD(void,
52               OnReceivedOverhead,
53               (size_t overhead_bytes_per_packet),
54               (override));
55 
56   MOCK_METHOD(bool,
57               EnableAudioNetworkAdaptor,
58               (const std::string& config_string, RtcEventLog*),
59               (override));
60 
61   // Note, we explicitly chose not to create a mock for the Encode method.
62   MOCK_METHOD(EncodedInfo,
63               EncodeImpl,
64               (uint32_t timestamp,
65                rtc::ArrayView<const int16_t> audio,
66                rtc::Buffer*),
67               (override));
68 
69   class FakeEncoding {
70    public:
71     // Creates a functor that will return `info` and adjust the rtc::Buffer
72     // given as input to it, so it is info.encoded_bytes larger.
73     explicit FakeEncoding(const AudioEncoder::EncodedInfo& info);
74 
75     // Shorthand version of the constructor above, for when only setting
76     // encoded_bytes in the EncodedInfo object matters.
77     explicit FakeEncoding(size_t encoded_bytes);
78 
79     AudioEncoder::EncodedInfo operator()(uint32_t timestamp,
80                                          rtc::ArrayView<const int16_t> audio,
81                                          rtc::Buffer* encoded);
82 
83    private:
84     AudioEncoder::EncodedInfo info_;
85   };
86 
87   class CopyEncoding {
88    public:
89     ~CopyEncoding();
90 
91     // Creates a functor that will return `info` and append the data in the
92     // payload to the buffer given as input to it. Up to info.encoded_bytes are
93     // appended - make sure the payload is big enough!  Since it uses an
94     // ArrayView, it _does not_ copy the payload. Make sure it doesn't fall out
95     // of scope!
96     CopyEncoding(AudioEncoder::EncodedInfo info,
97                  rtc::ArrayView<const uint8_t> payload);
98 
99     // Shorthand version of the constructor above, for when you wish to append
100     // the whole payload and do not care about any EncodedInfo attribute other
101     // than encoded_bytes.
102     explicit CopyEncoding(rtc::ArrayView<const uint8_t> payload);
103 
104     AudioEncoder::EncodedInfo operator()(uint32_t timestamp,
105                                          rtc::ArrayView<const int16_t> audio,
106                                          rtc::Buffer* encoded);
107 
108    private:
109     AudioEncoder::EncodedInfo info_;
110     rtc::ArrayView<const uint8_t> payload_;
111   };
112 };
113 
114 }  // namespace webrtc
115 
116 #endif  // TEST_MOCK_AUDIO_ENCODER_H_
117