1 /*
2  * libjingle
3  * Copyright 2015 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 // This file contains fake implementations, for use in unit tests, of the
29 // following classes:
30 //
31 //   webrtc::Call
32 //   webrtc::AudioSendStream
33 //   webrtc::AudioReceiveStream
34 //   webrtc::VideoSendStream
35 //   webrtc::VideoReceiveStream
36 
37 #ifndef TALK_MEDIA_WEBRTC_FAKEWEBRTCCALL_H_
38 #define TALK_MEDIA_WEBRTC_FAKEWEBRTCCALL_H_
39 
40 #include <vector>
41 
42 #include "webrtc/call.h"
43 #include "webrtc/audio_receive_stream.h"
44 #include "webrtc/audio_send_stream.h"
45 #include "webrtc/video_frame.h"
46 #include "webrtc/video_receive_stream.h"
47 #include "webrtc/video_send_stream.h"
48 
49 namespace cricket {
50 class FakeAudioSendStream final : public webrtc::AudioSendStream {
51  public:
52   struct TelephoneEvent {
53     int payload_type = -1;
54     uint8_t event_code = 0;
55     uint32_t duration_ms = 0;
56   };
57 
58   explicit FakeAudioSendStream(const webrtc::AudioSendStream::Config& config);
59 
60   const webrtc::AudioSendStream::Config& GetConfig() const;
61   void SetStats(const webrtc::AudioSendStream::Stats& stats);
62   TelephoneEvent GetLatestTelephoneEvent() const;
63 
64  private:
65   // webrtc::SendStream implementation.
Start()66   void Start() override {}
Stop()67   void Stop() override {}
SignalNetworkState(webrtc::NetworkState state)68   void SignalNetworkState(webrtc::NetworkState state) override {}
DeliverRtcp(const uint8_t * packet,size_t length)69   bool DeliverRtcp(const uint8_t* packet, size_t length) override {
70     return true;
71   }
72 
73   // webrtc::AudioSendStream implementation.
74   bool SendTelephoneEvent(int payload_type, uint8_t event,
75                           uint32_t duration_ms) override;
76   webrtc::AudioSendStream::Stats GetStats() const override;
77 
78   TelephoneEvent latest_telephone_event_;
79   webrtc::AudioSendStream::Config config_;
80   webrtc::AudioSendStream::Stats stats_;
81 };
82 
83 class FakeAudioReceiveStream final : public webrtc::AudioReceiveStream {
84  public:
85   explicit FakeAudioReceiveStream(
86       const webrtc::AudioReceiveStream::Config& config);
87 
88   const webrtc::AudioReceiveStream::Config& GetConfig() const;
89   void SetStats(const webrtc::AudioReceiveStream::Stats& stats);
received_packets()90   int received_packets() const { return received_packets_; }
91   void IncrementReceivedPackets();
92 
93  private:
94   // webrtc::ReceiveStream implementation.
Start()95   void Start() override {}
Stop()96   void Stop() override {}
SignalNetworkState(webrtc::NetworkState state)97   void SignalNetworkState(webrtc::NetworkState state) override {}
DeliverRtcp(const uint8_t * packet,size_t length)98   bool DeliverRtcp(const uint8_t* packet, size_t length) override {
99     return true;
100   }
DeliverRtp(const uint8_t * packet,size_t length,const webrtc::PacketTime & packet_time)101   bool DeliverRtp(const uint8_t* packet,
102                   size_t length,
103                   const webrtc::PacketTime& packet_time) override {
104     return true;
105   }
106 
107   // webrtc::AudioReceiveStream implementation.
108   webrtc::AudioReceiveStream::Stats GetStats() const override;
109   void SetSink(rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) override;
110 
111   webrtc::AudioReceiveStream::Config config_;
112   webrtc::AudioReceiveStream::Stats stats_;
113   int received_packets_;
114   rtc::scoped_ptr<webrtc::AudioSinkInterface> sink_;
115 };
116 
117 class FakeVideoSendStream final : public webrtc::VideoSendStream,
118                                   public webrtc::VideoCaptureInput {
119  public:
120   FakeVideoSendStream(const webrtc::VideoSendStream::Config& config,
121                       const webrtc::VideoEncoderConfig& encoder_config);
122   webrtc::VideoSendStream::Config GetConfig() const;
123   webrtc::VideoEncoderConfig GetEncoderConfig() const;
124   std::vector<webrtc::VideoStream> GetVideoStreams();
125 
126   bool IsSending() const;
127   bool GetVp8Settings(webrtc::VideoCodecVP8* settings) const;
128   bool GetVp9Settings(webrtc::VideoCodecVP9* settings) const;
129 
130   int GetNumberOfSwappedFrames() const;
131   int GetLastWidth() const;
132   int GetLastHeight() const;
133   int64_t GetLastTimestamp() const;
134   void SetStats(const webrtc::VideoSendStream::Stats& stats);
135 
136  private:
137   void IncomingCapturedFrame(const webrtc::VideoFrame& frame) override;
138 
139   // webrtc::SendStream implementation.
140   void Start() override;
141   void Stop() override;
SignalNetworkState(webrtc::NetworkState state)142   void SignalNetworkState(webrtc::NetworkState state) override {}
DeliverRtcp(const uint8_t * packet,size_t length)143   bool DeliverRtcp(const uint8_t* packet, size_t length) override {
144     return true;
145   }
146 
147   // webrtc::VideoSendStream implementation.
148   webrtc::VideoSendStream::Stats GetStats() override;
149   bool ReconfigureVideoEncoder(
150       const webrtc::VideoEncoderConfig& config) override;
151   webrtc::VideoCaptureInput* Input() override;
152 
153   bool sending_;
154   webrtc::VideoSendStream::Config config_;
155   webrtc::VideoEncoderConfig encoder_config_;
156   bool codec_settings_set_;
157   union VpxSettings {
158     webrtc::VideoCodecVP8 vp8;
159     webrtc::VideoCodecVP9 vp9;
160   } vpx_settings_;
161   int num_swapped_frames_;
162   webrtc::VideoFrame last_frame_;
163   webrtc::VideoSendStream::Stats stats_;
164 };
165 
166 class FakeVideoReceiveStream final : public webrtc::VideoReceiveStream {
167  public:
168   explicit FakeVideoReceiveStream(
169       const webrtc::VideoReceiveStream::Config& config);
170 
171   webrtc::VideoReceiveStream::Config GetConfig();
172 
173   bool IsReceiving() const;
174 
175   void InjectFrame(const webrtc::VideoFrame& frame, int time_to_render_ms);
176 
177   void SetStats(const webrtc::VideoReceiveStream::Stats& stats);
178 
179  private:
180   // webrtc::ReceiveStream implementation.
181   void Start() override;
182   void Stop() override;
SignalNetworkState(webrtc::NetworkState state)183   void SignalNetworkState(webrtc::NetworkState state) override {}
DeliverRtcp(const uint8_t * packet,size_t length)184   bool DeliverRtcp(const uint8_t* packet, size_t length) override {
185     return true;
186   }
DeliverRtp(const uint8_t * packet,size_t length,const webrtc::PacketTime & packet_time)187   bool DeliverRtp(const uint8_t* packet,
188                   size_t length,
189                   const webrtc::PacketTime& packet_time) override {
190     return true;
191   }
192 
193   // webrtc::VideoReceiveStream implementation.
194   webrtc::VideoReceiveStream::Stats GetStats() const override;
195 
196   webrtc::VideoReceiveStream::Config config_;
197   bool receiving_;
198   webrtc::VideoReceiveStream::Stats stats_;
199 };
200 
201 class FakeCall final : public webrtc::Call, public webrtc::PacketReceiver {
202  public:
203   explicit FakeCall(const webrtc::Call::Config& config);
204   ~FakeCall() override;
205 
206   webrtc::Call::Config GetConfig() const;
207   const std::vector<FakeVideoSendStream*>& GetVideoSendStreams();
208   const std::vector<FakeVideoReceiveStream*>& GetVideoReceiveStreams();
209 
210   const std::vector<FakeAudioSendStream*>& GetAudioSendStreams();
211   const FakeAudioSendStream* GetAudioSendStream(uint32_t ssrc);
212   const std::vector<FakeAudioReceiveStream*>& GetAudioReceiveStreams();
213   const FakeAudioReceiveStream* GetAudioReceiveStream(uint32_t ssrc);
214 
last_sent_packet()215   rtc::SentPacket last_sent_packet() const { return last_sent_packet_; }
216   webrtc::NetworkState GetNetworkState() const;
217   int GetNumCreatedSendStreams() const;
218   int GetNumCreatedReceiveStreams() const;
219   void SetStats(const webrtc::Call::Stats& stats);
220 
221  private:
222   webrtc::AudioSendStream* CreateAudioSendStream(
223       const webrtc::AudioSendStream::Config& config) override;
224   void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
225 
226   webrtc::AudioReceiveStream* CreateAudioReceiveStream(
227       const webrtc::AudioReceiveStream::Config& config) override;
228   void DestroyAudioReceiveStream(
229       webrtc::AudioReceiveStream* receive_stream) override;
230 
231   webrtc::VideoSendStream* CreateVideoSendStream(
232       const webrtc::VideoSendStream::Config& config,
233       const webrtc::VideoEncoderConfig& encoder_config) override;
234   void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
235 
236   webrtc::VideoReceiveStream* CreateVideoReceiveStream(
237       const webrtc::VideoReceiveStream::Config& config) override;
238   void DestroyVideoReceiveStream(
239       webrtc::VideoReceiveStream* receive_stream) override;
240   webrtc::PacketReceiver* Receiver() override;
241 
242   DeliveryStatus DeliverPacket(webrtc::MediaType media_type,
243                                const uint8_t* packet,
244                                size_t length,
245                                const webrtc::PacketTime& packet_time) override;
246 
247   webrtc::Call::Stats GetStats() const override;
248 
249   void SetBitrateConfig(
250       const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
251   void SignalNetworkState(webrtc::NetworkState state) override;
252   void OnSentPacket(const rtc::SentPacket& sent_packet) override;
253 
254   webrtc::Call::Config config_;
255   webrtc::NetworkState network_state_;
256   rtc::SentPacket last_sent_packet_;
257   webrtc::Call::Stats stats_;
258   std::vector<FakeVideoSendStream*> video_send_streams_;
259   std::vector<FakeAudioSendStream*> audio_send_streams_;
260   std::vector<FakeVideoReceiveStream*> video_receive_streams_;
261   std::vector<FakeAudioReceiveStream*> audio_receive_streams_;
262 
263   int num_created_send_streams_;
264   int num_created_receive_streams_;
265 };
266 
267 }  // namespace cricket
268 #endif  // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_UNITTEST_H_
269