/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef MOCK_CALL_H_ #define MOCK_CALL_H_ #include "gmock/gmock.h" #include "mozilla/Assertions.h" #include "mozilla/Maybe.h" #include "mozilla/media/MediaUtils.h" #include "WebrtcCallWrapper.h" #include "PeerConnectionCtx.h" // libwebrtc #include "api/call/audio_sink.h" #include "call/call.h" namespace test { class MockCallWrapper; class MockAudioSendStream : public webrtc::AudioSendStream { public: explicit MockAudioSendStream(RefPtr aCallWrapper) : mCallWrapper(std::move(aCallWrapper)) {} const webrtc::AudioSendStream::Config& GetConfig() const override; void Reconfigure(const Config& config, webrtc::SetParametersCallback callback) override; void Start() override {} void Stop() override {} void SendAudioData(std::unique_ptr audio_frame) override { } bool SendTelephoneEvent(int payload_type, int payload_frequency, int event, int duration_ms) override { return true; } void SetMuted(bool muted) override {} Stats GetStats() const override { return mStats; } Stats GetStats(bool has_remote_tracks) const override { return mStats; } virtual ~MockAudioSendStream() {} const RefPtr mCallWrapper; webrtc::AudioSendStream::Stats mStats; }; class MockAudioReceiveStream : public webrtc::AudioReceiveStreamInterface { public: explicit MockAudioReceiveStream(RefPtr aCallWrapper) : mCallWrapper(std::move(aCallWrapper)) {} void Start() override {} void Stop() override {} bool IsRunning() const override { return true; } Stats GetStats(bool get_and_clear_legacy_stats) const override { return mStats; } void SetSink(webrtc::AudioSinkInterface* sink) override {} void SetGain(float gain) override {} std::vector GetSources() const override { return mRtpSources; } virtual void SetDepacketizerToDecoderFrameTransformer( rtc::scoped_refptr frame_transformer) override { // Unimplemented after webrtc.org e2561e17e2 removed the Reconfigure // method. MOZ_ASSERT(false); } virtual void SetDecoderMap( std::map decoder_map) override; virtual void SetNackHistory(int history_ms) override { // Unimplemented after webrtc.org e2561e17e2 removed the Reconfigure // method. MOZ_ASSERT(false); } virtual void SetNonSenderRttMeasurement(bool enabled) override {} void SetFrameDecryptor(rtc::scoped_refptr frame_decryptor) override {} void SetRtpExtensions(std::vector extensions) override; webrtc::RtpHeaderExtensionMap GetRtpExtensionMap() const override; bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override { return false; } int GetBaseMinimumPlayoutDelayMs() const override { return 0; } uint32_t remote_ssrc() const override { return 0; } virtual ~MockAudioReceiveStream() {} const RefPtr mCallWrapper; webrtc::AudioReceiveStreamInterface::Stats mStats; std::vector mRtpSources; }; class MockVideoSendStream : public webrtc::VideoSendStream { public: explicit MockVideoSendStream(RefPtr aCallWrapper) : mCallWrapper(std::move(aCallWrapper)) {} void Start() override {} void Stop() override {} bool started() override { return false; } void SetSource( rtc::VideoSourceInterface* source, const webrtc::DegradationPreference& degradation_preference) override {} void ReconfigureVideoEncoder(webrtc::VideoEncoderConfig config) override; void ReconfigureVideoEncoder(webrtc::VideoEncoderConfig config, webrtc::SetParametersCallback callback) override; Stats GetStats() override { return mStats; } void StartPerRtpStream(const std::vector active_layers) override {} void AddAdaptationResource( rtc::scoped_refptr resource) override {} std::vector> GetAdaptationResources() override { return std::vector>(); } void GenerateKeyFrame(const std::vector& rids) override {} virtual ~MockVideoSendStream() {} const RefPtr mCallWrapper; webrtc::VideoSendStream::Stats mStats; }; class MockVideoReceiveStream : public webrtc::VideoReceiveStreamInterface { public: explicit MockVideoReceiveStream(RefPtr aCallWrapper) : mCallWrapper(std::move(aCallWrapper)) {} void Start() override {} void Stop() override {} Stats GetStats() const override { return mStats; } std::vector GetSources() const override { return std::vector(); } bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override { return false; } int GetBaseMinimumPlayoutDelayMs() const override { return 0; } void SetFrameDecryptor(rtc::scoped_refptr frame_decryptor) override {} void SetDepacketizerToDecoderFrameTransformer( rtc::scoped_refptr frame_transformer) override {} RecordingState SetAndGetRecordingState(RecordingState state, bool generate_key_frame) override { return {}; } void GenerateKeyFrame() override {} void SetRtcpMode(webrtc::RtcpMode mode) override {} void SetFlexFecProtection( webrtc::RtpPacketSinkInterface* flexfec_sink) override {} void SetLossNotificationEnabled(bool enabled) override {} void SetNackHistory(webrtc::TimeDelta history) override {} void SetProtectionPayloadTypes(int red_payload_type, int ulpfec_payload_type) override {} void SetRtcpXr(Config::Rtp::RtcpXr rtcp_xr) override {} virtual void SetAssociatedPayloadTypes( std::map associated_payload_types) override {} void SetRtpExtensions(std::vector extensions) override { } webrtc::RtpHeaderExtensionMap GetRtpExtensionMap() const override; virtual ~MockVideoReceiveStream() {} const RefPtr mCallWrapper; webrtc::VideoReceiveStreamInterface::Stats mStats; }; class MockCall : public webrtc::Call { public: explicit MockCall(RefPtr aCallWrapper) : mCallWrapper(std::move(aCallWrapper)) {} webrtc::AudioSendStream* CreateAudioSendStream( const webrtc::AudioSendStream::Config& config) override { MOZ_RELEASE_ASSERT(!mAudioSendConfig); mAudioSendConfig = mozilla::Some(config); return new MockAudioSendStream(mCallWrapper); } void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override { mAudioSendConfig = mozilla::Nothing(); delete static_cast(send_stream); } webrtc::AudioReceiveStreamInterface* CreateAudioReceiveStream( const webrtc::AudioReceiveStreamInterface::Config& config) override { MOZ_RELEASE_ASSERT(!mAudioReceiveConfig); mAudioReceiveConfig = mozilla::Some(config); return new MockAudioReceiveStream(mCallWrapper); } void DestroyAudioReceiveStream( webrtc::AudioReceiveStreamInterface* receive_stream) override { mAudioReceiveConfig = mozilla::Nothing(); delete static_cast(receive_stream); } webrtc::VideoSendStream* CreateVideoSendStream( webrtc::VideoSendStream::Config config, webrtc::VideoEncoderConfig encoder_config) override { MOZ_RELEASE_ASSERT(!mVideoSendConfig); MOZ_RELEASE_ASSERT(!mVideoSendEncoderConfig); mVideoSendConfig = mozilla::Some(std::move(config)); mVideoSendEncoderConfig = mozilla::Some(std::move(encoder_config)); return new MockVideoSendStream(mCallWrapper); } void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override { mVideoSendConfig = mozilla::Nothing(); mVideoSendEncoderConfig = mozilla::Nothing(); delete static_cast(send_stream); } webrtc::VideoReceiveStreamInterface* CreateVideoReceiveStream( webrtc::VideoReceiveStreamInterface::Config configuration) override { MOZ_RELEASE_ASSERT(!mVideoReceiveConfig); mVideoReceiveConfig = mozilla::Some(std::move(configuration)); return new MockVideoReceiveStream(mCallWrapper); } void DestroyVideoReceiveStream( webrtc::VideoReceiveStreamInterface* receive_stream) override { mVideoReceiveConfig = mozilla::Nothing(); delete static_cast(receive_stream); } webrtc::FlexfecReceiveStream* CreateFlexfecReceiveStream( const webrtc::FlexfecReceiveStream::Config config) override { return nullptr; } void DestroyFlexfecReceiveStream( webrtc::FlexfecReceiveStream* receive_stream) override {} void AddAdaptationResource( rtc::scoped_refptr resource) override {} webrtc::PacketReceiver* Receiver() override { return nullptr; } webrtc::RtpTransportControllerSendInterface* GetTransportControllerSend() override { return nullptr; } Stats GetStats() const override { return mStats; } void SignalChannelNetworkState(webrtc::MediaType media, webrtc::NetworkState state) override {} void OnAudioTransportOverheadChanged( int transport_overhead_per_packet) override {} void OnLocalSsrcUpdated(webrtc::AudioReceiveStreamInterface& stream, uint32_t local_ssrc) override {} void OnLocalSsrcUpdated(webrtc::VideoReceiveStreamInterface& stream, uint32_t local_ssrc) override {} void OnLocalSsrcUpdated(webrtc::FlexfecReceiveStream& stream, uint32_t local_ssrc) override {} void OnUpdateSyncGroup(webrtc::AudioReceiveStreamInterface& stream, absl::string_view sync_group) override {} void OnSentPacket(const rtc::SentPacket& sent_packet) override {} void SetClientBitratePreferences( const webrtc::BitrateSettings& preferences) override {} std::vector CreateEncoderStreams(int width, int height) { return mVideoSendEncoderConfig->video_stream_factory->CreateEncoderStreams( width, height, *mVideoSendEncoderConfig); } virtual const webrtc::WebRtcKeyValueConfig& trials() const override { return mUnusedConfig; } virtual webrtc::TaskQueueBase* network_thread() const override { return nullptr; } virtual webrtc::TaskQueueBase* worker_thread() const override { return nullptr; } virtual ~MockCall(){}; const RefPtr mCallWrapper; mozilla::Maybe mAudioReceiveConfig; mozilla::Maybe mAudioSendConfig; mozilla::Maybe mVideoReceiveConfig; mozilla::Maybe mVideoSendConfig; mozilla::Maybe mVideoSendEncoderConfig; webrtc::Call::Stats mStats; webrtc::NoTrialsConfig mUnusedConfig; }; class MockCallWrapper : public mozilla::WebrtcCallWrapper { public: MockCallWrapper( RefPtr aSharedState, mozilla::UniquePtr aVideoBitrateAllocatorFactory, mozilla::UniquePtr aEventLog, mozilla::UniquePtr aTaskQueueFactory, const mozilla::dom::RTCStatsTimestampMaker& aTimestampMaker, mozilla::UniquePtr aShutdownTicket) : mozilla::WebrtcCallWrapper( std::move(aSharedState), std::move(aVideoBitrateAllocatorFactory), std::move(aEventLog), std::move(aTaskQueueFactory), aTimestampMaker, std::move(aShutdownTicket)) {} static RefPtr Create() { auto state = mozilla::MakeRefPtr( mozilla::AbstractThread::GetCurrent(), webrtc::AudioState::Config(), nullptr, nullptr); auto wrapper = mozilla::MakeRefPtr( state, nullptr, nullptr, nullptr, mozilla::dom::RTCStatsTimestampMaker::Create(), nullptr); wrapper->SetCall(mozilla::WrapUnique(new MockCall(wrapper))); return wrapper; } MockCall* GetMockCall() { return static_cast(Call()); } }; } // namespace test #endif