fake_webrtc_call.h 14 KB

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