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 // This file contains fake implementations, for use in unit tests, of the 12 // following classes: 13 // 14 // webrtc::Call 15 // webrtc::AudioSendStream 16 // webrtc::AudioReceiveStream 17 // webrtc::VideoSendStream 18 // webrtc::VideoReceiveStream 19 20 #ifndef MEDIA_ENGINE_FAKE_WEBRTC_CALL_H_ 21 #define MEDIA_ENGINE_FAKE_WEBRTC_CALL_H_ 22 23 #include <memory> 24 #include <string> 25 #include <vector> 26 27 #include "api/video/video_frame.h" 28 #include "call/audio_receive_stream.h" 29 #include "call/audio_send_stream.h" 30 #include "call/call.h" 31 #include "call/flexfec_receive_stream.h" 32 #include "call/test/mock_rtp_transport_controller_send.h" 33 #include "call/video_receive_stream.h" 34 #include "call/video_send_stream.h" 35 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 36 #include "rtc_base/buffer.h" 37 38 namespace cricket { 39 class FakeAudioSendStream final : public webrtc::AudioSendStream { 40 public: 41 struct TelephoneEvent { 42 int payload_type = -1; 43 int payload_frequency = -1; 44 int event_code = 0; 45 int duration_ms = 0; 46 }; 47 48 explicit FakeAudioSendStream(int id, 49 const webrtc::AudioSendStream::Config& config); 50 id()51 int id() const { return id_; } 52 const webrtc::AudioSendStream::Config& GetConfig() const override; 53 void SetStats(const webrtc::AudioSendStream::Stats& stats); 54 TelephoneEvent GetLatestTelephoneEvent() const; IsSending()55 bool IsSending() const { return sending_; } muted()56 bool muted() const { return muted_; } 57 58 private: 59 // webrtc::AudioSendStream implementation. 60 void Reconfigure(const webrtc::AudioSendStream::Config& config) override; Start()61 void Start() override { sending_ = true; } Stop()62 void Stop() override { sending_ = false; } SendAudioData(std::unique_ptr<webrtc::AudioFrame> audio_frame)63 void SendAudioData(std::unique_ptr<webrtc::AudioFrame> audio_frame) override { 64 } 65 bool SendTelephoneEvent(int payload_type, 66 int payload_frequency, 67 int event, 68 int duration_ms) override; 69 void SetMuted(bool muted) override; 70 webrtc::AudioSendStream::Stats GetStats() const override; 71 webrtc::AudioSendStream::Stats GetStats( 72 bool has_remote_tracks) const override; 73 74 int id_ = -1; 75 TelephoneEvent latest_telephone_event_; 76 webrtc::AudioSendStream::Config config_; 77 webrtc::AudioSendStream::Stats stats_; 78 bool sending_ = false; 79 bool muted_ = false; 80 }; 81 82 class FakeAudioReceiveStream final : public webrtc::AudioReceiveStream { 83 public: 84 explicit FakeAudioReceiveStream( 85 int id, 86 const webrtc::AudioReceiveStream::Config& config); 87 id()88 int id() const { return id_; } 89 const webrtc::AudioReceiveStream::Config& GetConfig() const; 90 void SetStats(const webrtc::AudioReceiveStream::Stats& stats); received_packets()91 int received_packets() const { return received_packets_; } 92 bool VerifyLastPacket(const uint8_t* data, size_t length) const; sink()93 const webrtc::AudioSinkInterface* sink() const { return sink_; } gain()94 float gain() const { return gain_; } 95 bool DeliverRtp(const uint8_t* packet, size_t length, int64_t packet_time_us); started()96 bool started() const { return started_; } base_mininum_playout_delay_ms()97 int base_mininum_playout_delay_ms() const { 98 return base_mininum_playout_delay_ms_; 99 } 100 101 private: 102 // webrtc::AudioReceiveStream implementation. 103 void Reconfigure(const webrtc::AudioReceiveStream::Config& config) override; Start()104 void Start() override { started_ = true; } Stop()105 void Stop() override { started_ = false; } 106 107 webrtc::AudioReceiveStream::Stats GetStats() const override; 108 void SetSink(webrtc::AudioSinkInterface* sink) override; 109 void SetGain(float gain) override; SetBaseMinimumPlayoutDelayMs(int delay_ms)110 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override { 111 base_mininum_playout_delay_ms_ = delay_ms; 112 return true; 113 } GetBaseMinimumPlayoutDelayMs()114 int GetBaseMinimumPlayoutDelayMs() const override { 115 return base_mininum_playout_delay_ms_; 116 } GetSources()117 std::vector<webrtc::RtpSource> GetSources() const override { 118 return std::vector<webrtc::RtpSource>(); 119 } 120 121 int id_ = -1; 122 webrtc::AudioReceiveStream::Config config_; 123 webrtc::AudioReceiveStream::Stats stats_; 124 int received_packets_ = 0; 125 webrtc::AudioSinkInterface* sink_ = nullptr; 126 float gain_ = 1.0f; 127 rtc::Buffer last_packet_; 128 bool started_ = false; 129 int base_mininum_playout_delay_ms_ = 0; 130 }; 131 132 class FakeVideoSendStream final 133 : public webrtc::VideoSendStream, 134 public rtc::VideoSinkInterface<webrtc::VideoFrame> { 135 public: 136 FakeVideoSendStream(webrtc::VideoSendStream::Config config, 137 webrtc::VideoEncoderConfig encoder_config); 138 ~FakeVideoSendStream() override; 139 const webrtc::VideoSendStream::Config& GetConfig() const; 140 const webrtc::VideoEncoderConfig& GetEncoderConfig() const; 141 const std::vector<webrtc::VideoStream>& GetVideoStreams() const; 142 143 bool IsSending() const; 144 bool GetVp8Settings(webrtc::VideoCodecVP8* settings) const; 145 bool GetVp9Settings(webrtc::VideoCodecVP9* settings) const; 146 bool GetH264Settings(webrtc::VideoCodecH264* settings) const; 147 148 int GetNumberOfSwappedFrames() const; 149 int GetLastWidth() const; 150 int GetLastHeight() const; 151 int64_t GetLastTimestamp() const; 152 void SetStats(const webrtc::VideoSendStream::Stats& stats); num_encoder_reconfigurations()153 int num_encoder_reconfigurations() const { 154 return num_encoder_reconfigurations_; 155 } 156 resolution_scaling_enabled()157 bool resolution_scaling_enabled() const { 158 return resolution_scaling_enabled_; 159 } framerate_scaling_enabled()160 bool framerate_scaling_enabled() const { return framerate_scaling_enabled_; } 161 void InjectVideoSinkWants(const rtc::VideoSinkWants& wants); 162 source()163 rtc::VideoSourceInterface<webrtc::VideoFrame>* source() const { 164 return source_; 165 } 166 167 private: 168 // rtc::VideoSinkInterface<VideoFrame> implementation. 169 void OnFrame(const webrtc::VideoFrame& frame) override; 170 171 // webrtc::VideoSendStream implementation. 172 void UpdateActiveSimulcastLayers( 173 const std::vector<bool> active_layers) override; 174 void Start() override; 175 void Stop() override; 176 void AddAdaptationResource( 177 rtc::scoped_refptr<webrtc::Resource> resource) override; 178 std::vector<rtc::scoped_refptr<webrtc::Resource>> GetAdaptationResources() 179 override; 180 void SetSource( 181 rtc::VideoSourceInterface<webrtc::VideoFrame>* source, 182 const webrtc::DegradationPreference& degradation_preference) override; 183 webrtc::VideoSendStream::Stats GetStats() override; 184 void ReconfigureVideoEncoder(webrtc::VideoEncoderConfig config) override; 185 186 bool sending_; 187 webrtc::VideoSendStream::Config config_; 188 webrtc::VideoEncoderConfig encoder_config_; 189 std::vector<webrtc::VideoStream> video_streams_; 190 rtc::VideoSinkWants sink_wants_; 191 192 bool codec_settings_set_; 193 union CodecSpecificSettings { 194 webrtc::VideoCodecVP8 vp8; 195 webrtc::VideoCodecVP9 vp9; 196 webrtc::VideoCodecH264 h264; 197 } codec_specific_settings_; 198 bool resolution_scaling_enabled_; 199 bool framerate_scaling_enabled_; 200 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_; 201 int num_swapped_frames_; 202 absl::optional<webrtc::VideoFrame> last_frame_; 203 webrtc::VideoSendStream::Stats stats_; 204 int num_encoder_reconfigurations_ = 0; 205 }; 206 207 class FakeVideoReceiveStream final : public webrtc::VideoReceiveStream { 208 public: 209 explicit FakeVideoReceiveStream(webrtc::VideoReceiveStream::Config config); 210 211 const webrtc::VideoReceiveStream::Config& GetConfig() const; 212 213 bool IsReceiving() const; 214 215 void InjectFrame(const webrtc::VideoFrame& frame); 216 217 void SetStats(const webrtc::VideoReceiveStream::Stats& stats); 218 219 void AddSecondarySink(webrtc::RtpPacketSinkInterface* sink) override; 220 void RemoveSecondarySink(const webrtc::RtpPacketSinkInterface* sink) override; 221 222 int GetNumAddedSecondarySinks() const; 223 int GetNumRemovedSecondarySinks() const; 224 GetSources()225 std::vector<webrtc::RtpSource> GetSources() const override { 226 return std::vector<webrtc::RtpSource>(); 227 } 228 base_mininum_playout_delay_ms()229 int base_mininum_playout_delay_ms() const { 230 return base_mininum_playout_delay_ms_; 231 } 232 SetFrameDecryptor(rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor)233 void SetFrameDecryptor(rtc::scoped_refptr<webrtc::FrameDecryptorInterface> 234 frame_decryptor) override {} 235 SetDepacketizerToDecoderFrameTransformer(rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)236 void SetDepacketizerToDecoderFrameTransformer( 237 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) 238 override {} 239 SetAndGetRecordingState(RecordingState state,bool generate_key_frame)240 RecordingState SetAndGetRecordingState(RecordingState state, 241 bool generate_key_frame) override { 242 return RecordingState(); 243 } GenerateKeyFrame()244 void GenerateKeyFrame() override {} 245 246 private: 247 // webrtc::VideoReceiveStream implementation. 248 void Start() override; 249 void Stop() override; 250 251 webrtc::VideoReceiveStream::Stats GetStats() const override; 252 SetBaseMinimumPlayoutDelayMs(int delay_ms)253 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override { 254 base_mininum_playout_delay_ms_ = delay_ms; 255 return true; 256 } 257 GetBaseMinimumPlayoutDelayMs()258 int GetBaseMinimumPlayoutDelayMs() const override { 259 return base_mininum_playout_delay_ms_; 260 } 261 262 webrtc::VideoReceiveStream::Config config_; 263 bool receiving_; 264 webrtc::VideoReceiveStream::Stats stats_; 265 266 int base_mininum_playout_delay_ms_ = 0; 267 268 int num_added_secondary_sinks_; 269 int num_removed_secondary_sinks_; 270 }; 271 272 class FakeFlexfecReceiveStream final : public webrtc::FlexfecReceiveStream { 273 public: 274 explicit FakeFlexfecReceiveStream( 275 const webrtc::FlexfecReceiveStream::Config& config); 276 277 const webrtc::FlexfecReceiveStream::Config& GetConfig() const override; 278 279 private: 280 webrtc::FlexfecReceiveStream::Stats GetStats() const override; 281 282 void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override; 283 284 webrtc::FlexfecReceiveStream::Config config_; 285 }; 286 287 class FakeCall final : public webrtc::Call, public webrtc::PacketReceiver { 288 public: 289 FakeCall(); 290 ~FakeCall() override; 291 GetMockTransportControllerSend()292 webrtc::MockRtpTransportControllerSend* GetMockTransportControllerSend() { 293 return &transport_controller_send_; 294 } 295 296 const std::vector<FakeVideoSendStream*>& GetVideoSendStreams(); 297 const std::vector<FakeVideoReceiveStream*>& GetVideoReceiveStreams(); 298 299 const std::vector<FakeAudioSendStream*>& GetAudioSendStreams(); 300 const FakeAudioSendStream* GetAudioSendStream(uint32_t ssrc); 301 const std::vector<FakeAudioReceiveStream*>& GetAudioReceiveStreams(); 302 const FakeAudioReceiveStream* GetAudioReceiveStream(uint32_t ssrc); 303 const FakeVideoReceiveStream* GetVideoReceiveStream(uint32_t ssrc); 304 305 const std::vector<FakeFlexfecReceiveStream*>& GetFlexfecReceiveStreams(); 306 last_sent_packet()307 rtc::SentPacket last_sent_packet() const { return last_sent_packet_; } 308 309 // This is useful if we care about the last media packet (with id populated) 310 // but not the last ICE packet (with -1 ID). last_sent_nonnegative_packet_id()311 int last_sent_nonnegative_packet_id() const { 312 return last_sent_nonnegative_packet_id_; 313 } 314 315 webrtc::NetworkState GetNetworkState(webrtc::MediaType media) const; 316 int GetNumCreatedSendStreams() const; 317 int GetNumCreatedReceiveStreams() const; 318 void SetStats(const webrtc::Call::Stats& stats); 319 SetClientBitratePreferences(const webrtc::BitrateSettings & preferences)320 void SetClientBitratePreferences( 321 const webrtc::BitrateSettings& preferences) override {} 322 323 private: 324 webrtc::AudioSendStream* CreateAudioSendStream( 325 const webrtc::AudioSendStream::Config& config) override; 326 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override; 327 328 webrtc::AudioReceiveStream* CreateAudioReceiveStream( 329 const webrtc::AudioReceiveStream::Config& config) override; 330 void DestroyAudioReceiveStream( 331 webrtc::AudioReceiveStream* receive_stream) override; 332 333 webrtc::VideoSendStream* CreateVideoSendStream( 334 webrtc::VideoSendStream::Config config, 335 webrtc::VideoEncoderConfig encoder_config) override; 336 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override; 337 338 webrtc::VideoReceiveStream* CreateVideoReceiveStream( 339 webrtc::VideoReceiveStream::Config config) override; 340 void DestroyVideoReceiveStream( 341 webrtc::VideoReceiveStream* receive_stream) override; 342 343 webrtc::FlexfecReceiveStream* CreateFlexfecReceiveStream( 344 const webrtc::FlexfecReceiveStream::Config& config) override; 345 void DestroyFlexfecReceiveStream( 346 webrtc::FlexfecReceiveStream* receive_stream) override; 347 348 void AddAdaptationResource( 349 rtc::scoped_refptr<webrtc::Resource> resource) override; 350 351 webrtc::PacketReceiver* Receiver() override; 352 353 DeliveryStatus DeliverPacket(webrtc::MediaType media_type, 354 rtc::CopyOnWriteBuffer packet, 355 int64_t packet_time_us) override; 356 GetTransportControllerSend()357 webrtc::RtpTransportControllerSendInterface* GetTransportControllerSend() 358 override { 359 return &transport_controller_send_; 360 } 361 362 webrtc::Call::Stats GetStats() const override; 363 364 void SignalChannelNetworkState(webrtc::MediaType media, 365 webrtc::NetworkState state) override; 366 void OnAudioTransportOverheadChanged( 367 int transport_overhead_per_packet) override; 368 void OnSentPacket(const rtc::SentPacket& sent_packet) override; 369 370 ::testing::NiceMock<webrtc::MockRtpTransportControllerSend> 371 transport_controller_send_; 372 373 webrtc::NetworkState audio_network_state_; 374 webrtc::NetworkState video_network_state_; 375 rtc::SentPacket last_sent_packet_; 376 int last_sent_nonnegative_packet_id_ = -1; 377 int next_stream_id_ = 665; 378 webrtc::Call::Stats stats_; 379 std::vector<FakeVideoSendStream*> video_send_streams_; 380 std::vector<FakeAudioSendStream*> audio_send_streams_; 381 std::vector<FakeVideoReceiveStream*> video_receive_streams_; 382 std::vector<FakeAudioReceiveStream*> audio_receive_streams_; 383 std::vector<FakeFlexfecReceiveStream*> flexfec_receive_streams_; 384 385 int num_created_send_streams_; 386 int num_created_receive_streams_; 387 }; 388 389 } // namespace cricket 390 #endif // MEDIA_ENGINE_FAKE_WEBRTC_CALL_H_ 391