• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
52   MOCK_METHOD(bool,
53               EnableAudioNetworkAdaptor,
54               (const std::string& config_string, RtcEventLog*),
55               (override));
56 
57   // Note, we explicitly chose not to create a mock for the Encode method.
58   MOCK_METHOD(EncodedInfo,
59               EncodeImpl,
60               (uint32_t timestamp,
61                rtc::ArrayView<const int16_t> audio,
62                rtc::Buffer*),
63               (override));
64 
65   class FakeEncoding {
66    public:
67     // Creates a functor that will return |info| and adjust the rtc::Buffer
68     // given as input to it, so it is info.encoded_bytes larger.
69     explicit FakeEncoding(const AudioEncoder::EncodedInfo& info);
70 
71     // Shorthand version of the constructor above, for when only setting
72     // encoded_bytes in the EncodedInfo object matters.
73     explicit FakeEncoding(size_t encoded_bytes);
74 
75     AudioEncoder::EncodedInfo operator()(uint32_t timestamp,
76                                          rtc::ArrayView<const int16_t> audio,
77                                          rtc::Buffer* encoded);
78 
79    private:
80     AudioEncoder::EncodedInfo info_;
81   };
82 
83   class CopyEncoding {
84    public:
85     ~CopyEncoding();
86 
87     // Creates a functor that will return |info| and append the data in the
88     // payload to the buffer given as input to it. Up to info.encoded_bytes are
89     // appended - make sure the payload is big enough!  Since it uses an
90     // ArrayView, it _does not_ copy the payload. Make sure it doesn't fall out
91     // of scope!
92     CopyEncoding(AudioEncoder::EncodedInfo info,
93                  rtc::ArrayView<const uint8_t> payload);
94 
95     // Shorthand version of the constructor above, for when you wish to append
96     // the whole payload and do not care about any EncodedInfo attribute other
97     // than encoded_bytes.
98     explicit CopyEncoding(rtc::ArrayView<const uint8_t> payload);
99 
100     AudioEncoder::EncodedInfo operator()(uint32_t timestamp,
101                                          rtc::ArrayView<const int16_t> audio,
102                                          rtc::Buffer* encoded);
103 
104    private:
105     AudioEncoder::EncodedInfo info_;
106     rtc::ArrayView<const uint8_t> payload_;
107   };
108 };
109 
110 }  // namespace webrtc
111 
112 #endif  // TEST_MOCK_AUDIO_ENCODER_H_
113