1 /*
2  *  Copyright (c) 2015 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 WEBRTC_AUDIO_MOCK_VOICE_ENGINE_H_
12 #define WEBRTC_AUDIO_MOCK_VOICE_ENGINE_H_
13 
14 #include <memory>
15 
16 #include "webrtc/modules/audio_device/include/mock_audio_device.h"
17 #include "webrtc/modules/audio_device/include/mock_audio_transport.h"
18 #include "webrtc/modules/audio_processing/include/mock_audio_processing.h"
19 #include "webrtc/test/gmock.h"
20 #include "webrtc/test/mock_voe_channel_proxy.h"
21 #include "webrtc/voice_engine/voice_engine_impl.h"
22 
23 namespace webrtc {
24 namespace test {
25 
26 // NOTE: This class inherits from VoiceEngineImpl so that its clients will be
27 // able to get the various interfaces as usual, via T::GetInterface().
28 class MockVoiceEngine : public VoiceEngineImpl {
29  public:
30   // TODO(nisse): Valid overrides commented out, because the gmock
31   // methods don't use any override declarations, and we want to avoid
32   // warnings from -Winconsistent-missing-override. See
33   // http://crbug.com/428099.
34   MockVoiceEngine(
35       rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr)
decoder_factory_(decoder_factory)36       : decoder_factory_(decoder_factory) {
37     // Increase ref count so this object isn't automatically deleted whenever
38     // interfaces are Release():d.
39     ++_ref_count;
40     // We add this default behavior to make the mock easier to use in tests. It
41     // will create a NiceMock of a voe::ChannelProxy.
42     // TODO(ossu): As long as AudioReceiveStream is implemented as a wrapper
43     // around Channel, we need to make sure ChannelProxy returns the same
44     // decoder factory as the one passed in when creating an AudioReceiveStream.
45     ON_CALL(*this, ChannelProxyFactory(testing::_))
46         .WillByDefault(testing::Invoke([this](int channel_id) {
47           auto* proxy =
48               new testing::NiceMock<webrtc::test::MockVoEChannelProxy>();
49           EXPECT_CALL(*proxy, GetAudioDecoderFactory())
50               .WillRepeatedly(testing::ReturnRef(decoder_factory_));
51           return proxy;
52         }));
53 
54     ON_CALL(*this, audio_device_module())
55         .WillByDefault(testing::Return(&mock_audio_device_));
56     ON_CALL(*this, audio_processing())
57         .WillByDefault(testing::Return(&mock_audio_processing_));
58     ON_CALL(*this, audio_transport())
59         .WillByDefault(testing::Return(&mock_audio_transport_));
60   }
~MockVoiceEngine()61   virtual ~MockVoiceEngine() /* override */ {
62     // Decrease ref count before base class d-tor is called; otherwise it will
63     // trigger an assertion.
64     --_ref_count;
65   }
66 
Release()67   int Release() {
68     return 0;
69   }
70 
71   // Allows injecting a ChannelProxy factory.
72   MOCK_METHOD1(ChannelProxyFactory, voe::ChannelProxy*(int channel_id));
73 
74   // VoiceEngineImpl
GetChannelProxy(int channel_id)75   virtual std::unique_ptr<voe::ChannelProxy> GetChannelProxy(
76       int channel_id) /* override */ {
77     return std::unique_ptr<voe::ChannelProxy>(ChannelProxyFactory(channel_id));
78   }
79 
80   // VoEAudioProcessing
81   MOCK_METHOD2(SetNsStatus, int(bool enable, NsModes mode));
82   MOCK_METHOD2(GetNsStatus, int(bool& enabled, NsModes& mode));
83   MOCK_METHOD2(SetAgcStatus, int(bool enable, AgcModes mode));
84   MOCK_METHOD2(GetAgcStatus, int(bool& enabled, AgcModes& mode));
85   MOCK_METHOD1(SetAgcConfig, int(AgcConfig config));
86   MOCK_METHOD1(GetAgcConfig, int(AgcConfig& config));
87   MOCK_METHOD2(SetEcStatus, int(bool enable, EcModes mode));
88   MOCK_METHOD2(GetEcStatus, int(bool& enabled, EcModes& mode));
89   MOCK_METHOD1(EnableDriftCompensation, int(bool enable));
90   MOCK_METHOD0(DriftCompensationEnabled, bool());
91   MOCK_METHOD1(SetDelayOffsetMs, void(int offset));
92   MOCK_METHOD0(DelayOffsetMs, int());
93   MOCK_METHOD2(SetAecmMode, int(AecmModes mode, bool enableCNG));
94   MOCK_METHOD2(GetAecmMode, int(AecmModes& mode, bool& enabledCNG));
95   MOCK_METHOD1(EnableHighPassFilter, int(bool enable));
96   MOCK_METHOD0(IsHighPassFilterEnabled, bool());
97   MOCK_METHOD1(VoiceActivityIndicator, int(int channel));
98   MOCK_METHOD1(SetEcMetricsStatus, int(bool enable));
99   MOCK_METHOD1(GetEcMetricsStatus, int(bool& enabled));
100   MOCK_METHOD4(GetEchoMetrics, int(int& ERL, int& ERLE, int& RERL, int& A_NLP));
101   MOCK_METHOD3(GetEcDelayMetrics,
102                int(int& delay_median,
103                    int& delay_std,
104                    float& fraction_poor_delays));
105   MOCK_METHOD1(StartDebugRecording, int(const char* fileNameUTF8));
106   MOCK_METHOD1(StartDebugRecording, int(FILE* file_handle));
107   MOCK_METHOD0(StopDebugRecording, int());
108   MOCK_METHOD1(SetTypingDetectionStatus, int(bool enable));
109   MOCK_METHOD1(GetTypingDetectionStatus, int(bool& enabled));
110   MOCK_METHOD1(TimeSinceLastTyping, int(int& seconds));
111   MOCK_METHOD5(SetTypingDetectionParameters,
112                int(int timeWindow,
113                    int costPerTyping,
114                    int reportingThreshold,
115                    int penaltyDecay,
116                    int typeEventDelay));
117   MOCK_METHOD1(EnableStereoChannelSwapping, void(bool enable));
118   MOCK_METHOD0(IsStereoChannelSwappingEnabled, bool());
119 
120   // VoEBase
121   MOCK_METHOD1(RegisterVoiceEngineObserver, int(VoiceEngineObserver& observer));
122   MOCK_METHOD0(DeRegisterVoiceEngineObserver, int());
123   MOCK_METHOD3(
124       Init,
125       int(AudioDeviceModule* external_adm,
126           AudioProcessing* audioproc,
127           const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory));
128   MOCK_METHOD0(audio_processing, AudioProcessing*());
129   MOCK_METHOD0(audio_device_module, AudioDeviceModule*());
130   MOCK_METHOD0(Terminate, int());
131   MOCK_METHOD0(CreateChannel, int());
132   MOCK_METHOD1(CreateChannel, int(const ChannelConfig& config));
133   MOCK_METHOD1(DeleteChannel, int(int channel));
134   MOCK_METHOD1(StartReceive, int(int channel));
135   MOCK_METHOD1(StopReceive, int(int channel));
136   MOCK_METHOD1(StartPlayout, int(int channel));
137   MOCK_METHOD1(StopPlayout, int(int channel));
138   MOCK_METHOD1(StartSend, int(int channel));
139   MOCK_METHOD1(StopSend, int(int channel));
140   MOCK_METHOD1(GetVersion, int(char version[1024]));
141   MOCK_METHOD0(LastError, int());
142   MOCK_METHOD0(audio_transport, AudioTransport*());
143   MOCK_METHOD2(AssociateSendChannel,
144                int(int channel, int accociate_send_channel));
145 
146   // VoECodec
147   MOCK_METHOD0(NumOfCodecs, int());
148   MOCK_METHOD2(GetCodec, int(int index, CodecInst& codec));
149   MOCK_METHOD2(SetSendCodec, int(int channel, const CodecInst& codec));
150   MOCK_METHOD2(GetSendCodec, int(int channel, CodecInst& codec));
151   MOCK_METHOD2(SetBitRate, int(int channel, int bitrate_bps));
152   MOCK_METHOD2(GetRecCodec, int(int channel, CodecInst& codec));
153   MOCK_METHOD2(SetRecPayloadType, int(int channel, const CodecInst& codec));
154   MOCK_METHOD2(GetRecPayloadType, int(int channel, CodecInst& codec));
155   MOCK_METHOD3(SetSendCNPayloadType,
156                int(int channel, int type, PayloadFrequencies frequency));
157   MOCK_METHOD2(SetFECStatus, int(int channel, bool enable));
158   MOCK_METHOD2(GetFECStatus, int(int channel, bool& enabled));
159   MOCK_METHOD4(SetVADStatus,
160                int(int channel, bool enable, VadModes mode, bool disableDTX));
161   MOCK_METHOD4(
162       GetVADStatus,
163       int(int channel, bool& enabled, VadModes& mode, bool& disabledDTX));
164   MOCK_METHOD2(SetOpusMaxPlaybackRate, int(int channel, int frequency_hz));
165   MOCK_METHOD2(SetOpusDtx, int(int channel, bool enable_dtx));
166 
167   // VoEExternalMedia
168   MOCK_METHOD3(RegisterExternalMediaProcessing,
169                int(int channel,
170                    ProcessingTypes type,
171                    VoEMediaProcess& processObject));
172   MOCK_METHOD2(DeRegisterExternalMediaProcessing,
173                int(int channel, ProcessingTypes type));
174   MOCK_METHOD3(GetAudioFrame,
175                int(int channel, int desired_sample_rate_hz, AudioFrame* frame));
176   MOCK_METHOD2(SetExternalMixing, int(int channel, bool enable));
177 
178   // VoEFile
179   MOCK_METHOD7(StartPlayingFileLocally,
180                int(int channel,
181                    const char fileNameUTF8[1024],
182                    bool loop,
183                    FileFormats format,
184                    float volumeScaling,
185                    int startPointMs,
186                    int stopPointMs));
187   MOCK_METHOD6(StartPlayingFileLocally,
188                int(int channel,
189                    InStream* stream,
190                    FileFormats format,
191                    float volumeScaling,
192                    int startPointMs,
193                    int stopPointMs));
194   MOCK_METHOD1(StopPlayingFileLocally, int(int channel));
195   MOCK_METHOD1(IsPlayingFileLocally, int(int channel));
196   MOCK_METHOD6(StartPlayingFileAsMicrophone,
197                int(int channel,
198                    const char fileNameUTF8[1024],
199                    bool loop,
200                    bool mixWithMicrophone,
201                    FileFormats format,
202                    float volumeScaling));
203   MOCK_METHOD5(StartPlayingFileAsMicrophone,
204                int(int channel,
205                    InStream* stream,
206                    bool mixWithMicrophone,
207                    FileFormats format,
208                    float volumeScaling));
209   MOCK_METHOD1(StopPlayingFileAsMicrophone, int(int channel));
210   MOCK_METHOD1(IsPlayingFileAsMicrophone, int(int channel));
211   MOCK_METHOD4(StartRecordingPlayout,
212                int(int channel,
213                    const char* fileNameUTF8,
214                    CodecInst* compression,
215                    int maxSizeBytes));
216   MOCK_METHOD1(StopRecordingPlayout, int(int channel));
217   MOCK_METHOD3(StartRecordingPlayout,
218                int(int channel, OutStream* stream, CodecInst* compression));
219   MOCK_METHOD3(StartRecordingMicrophone,
220                int(const char* fileNameUTF8,
221                    CodecInst* compression,
222                    int maxSizeBytes));
223   MOCK_METHOD2(StartRecordingMicrophone,
224                int(OutStream* stream, CodecInst* compression));
225   MOCK_METHOD0(StopRecordingMicrophone, int());
226 
227   // VoEHardware
228   MOCK_METHOD1(GetNumOfRecordingDevices, int(int& devices));
229   MOCK_METHOD1(GetNumOfPlayoutDevices, int(int& devices));
230   MOCK_METHOD3(GetRecordingDeviceName,
231                int(int index, char strNameUTF8[128], char strGuidUTF8[128]));
232   MOCK_METHOD3(GetPlayoutDeviceName,
233                int(int index, char strNameUTF8[128], char strGuidUTF8[128]));
234   MOCK_METHOD2(SetRecordingDevice,
235                int(int index, StereoChannel recordingChannel));
236   MOCK_METHOD1(SetPlayoutDevice, int(int index));
237   MOCK_METHOD1(SetAudioDeviceLayer, int(AudioLayers audioLayer));
238   MOCK_METHOD1(GetAudioDeviceLayer, int(AudioLayers& audioLayer));
239   MOCK_METHOD1(SetRecordingSampleRate, int(unsigned int samples_per_sec));
240   MOCK_CONST_METHOD1(RecordingSampleRate, int(unsigned int* samples_per_sec));
241   MOCK_METHOD1(SetPlayoutSampleRate, int(unsigned int samples_per_sec));
242   MOCK_CONST_METHOD1(PlayoutSampleRate, int(unsigned int* samples_per_sec));
243   MOCK_CONST_METHOD0(BuiltInAECIsAvailable, bool());
244   MOCK_METHOD1(EnableBuiltInAEC, int(bool enable));
245   MOCK_CONST_METHOD0(BuiltInAGCIsAvailable, bool());
246   MOCK_METHOD1(EnableBuiltInAGC, int(bool enable));
247   MOCK_CONST_METHOD0(BuiltInNSIsAvailable, bool());
248   MOCK_METHOD1(EnableBuiltInNS, int(bool enable));
249 
250   // VoENetEqStats
251   MOCK_METHOD2(GetNetworkStatistics,
252                int(int channel, NetworkStatistics& stats));
253   MOCK_CONST_METHOD2(GetDecodingCallStatistics,
254                      int(int channel, AudioDecodingCallStats* stats));
255 
256   // VoENetwork
257   MOCK_METHOD2(RegisterExternalTransport,
258                int(int channel, Transport& transport));
259   MOCK_METHOD1(DeRegisterExternalTransport, int(int channel));
260   MOCK_METHOD3(ReceivedRTPPacket,
261                int(int channel, const void* data, size_t length));
262   MOCK_METHOD4(ReceivedRTPPacket,
263                int(int channel,
264                    const void* data,
265                    size_t length,
266                    const PacketTime& packet_time));
267   MOCK_METHOD3(ReceivedRTCPPacket,
268                int(int channel, const void* data, size_t length));
269 
270   // VoERTP_RTCP
271   MOCK_METHOD2(SetLocalSSRC, int(int channel, unsigned int ssrc));
272   MOCK_METHOD2(GetLocalSSRC, int(int channel, unsigned int& ssrc));
273   MOCK_METHOD2(GetRemoteSSRC, int(int channel, unsigned int& ssrc));
274   MOCK_METHOD3(SetSendAudioLevelIndicationStatus,
275                int(int channel, bool enable, unsigned char id));
276   MOCK_METHOD3(SetReceiveAudioLevelIndicationStatus,
277                int(int channel, bool enable, unsigned char id));
278   MOCK_METHOD3(SetSendAbsoluteSenderTimeStatus,
279                int(int channel, bool enable, unsigned char id));
280   MOCK_METHOD3(SetReceiveAbsoluteSenderTimeStatus,
281                int(int channel, bool enable, unsigned char id));
282   MOCK_METHOD2(SetRTCPStatus, int(int channel, bool enable));
283   MOCK_METHOD2(GetRTCPStatus, int(int channel, bool& enabled));
284   MOCK_METHOD2(SetRTCP_CNAME, int(int channel, const char cName[256]));
285   MOCK_METHOD2(GetRTCP_CNAME, int(int channel, char cName[256]));
286   MOCK_METHOD2(GetRemoteRTCP_CNAME, int(int channel, char cName[256]));
287   MOCK_METHOD7(GetRemoteRTCPData,
288                int(int channel,
289                    unsigned int& NTPHigh,
290                    unsigned int& NTPLow,
291                    unsigned int& timestamp,
292                    unsigned int& playoutTimestamp,
293                    unsigned int* jitter,
294                    unsigned short* fractionLost));
295   MOCK_METHOD4(GetRTPStatistics,
296                int(int channel,
297                    unsigned int& averageJitterMs,
298                    unsigned int& maxJitterMs,
299                    unsigned int& discardedPackets));
300   MOCK_METHOD2(GetRTCPStatistics, int(int channel, CallStatistics& stats));
301   MOCK_METHOD2(GetRemoteRTCPReportBlocks,
302                int(int channel, std::vector<ReportBlock>* receive_blocks));
303   MOCK_METHOD3(SetREDStatus, int(int channel, bool enable, int redPayloadtype));
304   MOCK_METHOD3(GetREDStatus,
305                int(int channel, bool& enable, int& redPayloadtype));
306   MOCK_METHOD3(SetNACKStatus, int(int channel, bool enable, int maxNoPackets));
307 
308   // VoEVideoSync
309   MOCK_METHOD1(GetPlayoutBufferSize, int(int& buffer_ms));
310   MOCK_METHOD2(SetMinimumPlayoutDelay, int(int channel, int delay_ms));
311   MOCK_METHOD3(GetDelayEstimate,
312                int(int channel,
313                    int* jitter_buffer_delay_ms,
314                    int* playout_buffer_delay_ms));
315   MOCK_CONST_METHOD1(GetLeastRequiredDelayMs, int(int channel));
316   MOCK_METHOD2(SetInitTimestamp, int(int channel, unsigned int timestamp));
317   MOCK_METHOD2(SetInitSequenceNumber, int(int channel, short sequenceNumber));
318   MOCK_METHOD2(GetPlayoutTimestamp, int(int channel, unsigned int& timestamp));
319   MOCK_METHOD3(GetRtpRtcp,
320                int(int channel,
321                    RtpRtcp** rtpRtcpModule,
322                    RtpReceiver** rtp_receiver));
323 
324   // VoEVolumeControl
325   MOCK_METHOD1(SetSpeakerVolume, int(unsigned int volume));
326   MOCK_METHOD1(GetSpeakerVolume, int(unsigned int& volume));
327   MOCK_METHOD1(SetMicVolume, int(unsigned int volume));
328   MOCK_METHOD1(GetMicVolume, int(unsigned int& volume));
329   MOCK_METHOD2(SetInputMute, int(int channel, bool enable));
330   MOCK_METHOD2(GetInputMute, int(int channel, bool& enabled));
331   MOCK_METHOD1(GetSpeechInputLevel, int(unsigned int& level));
332   MOCK_METHOD2(GetSpeechOutputLevel, int(int channel, unsigned int& level));
333   MOCK_METHOD1(GetSpeechInputLevelFullRange, int(unsigned int& level));
334   MOCK_METHOD2(GetSpeechOutputLevelFullRange,
335                int(int channel, unsigned& level));
336   MOCK_METHOD2(SetChannelOutputVolumeScaling, int(int channel, float scaling));
337   MOCK_METHOD2(GetChannelOutputVolumeScaling, int(int channel, float& scaling));
338   MOCK_METHOD3(SetOutputVolumePan, int(int channel, float left, float right));
339   MOCK_METHOD3(GetOutputVolumePan, int(int channel, float& left, float& right));
340 
341  private:
342   // TODO(ossu): I'm not particularly happy about keeping the decoder factory
343   // here, but due to how gmock is implemented, I cannot just keep it in the
344   // functor implementing the default version of ChannelProxyFactory, above.
345   // GMock creates an unfortunate copy of the functor, which would cause us to
346   // return a dangling reference. Fortunately, this should go away once
347   // voe::Channel does.
348   rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
349 
350   MockAudioDeviceModule mock_audio_device_;
351   MockAudioProcessing mock_audio_processing_;
352   MockAudioTransport mock_audio_transport_;
353 };
354 }  // namespace test
355 }  // namespace webrtc
356 
357 #endif  // WEBRTC_AUDIO_MOCK_VOICE_ENGINE_H_
358