1 /*
2  *  Copyright 2018 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 #ifndef TEST_SCENARIO_CALL_CLIENT_H_
11 #define TEST_SCENARIO_CALL_CLIENT_H_
12 
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include "api/rtc_event_log/rtc_event_log.h"
20 #include "api/test/time_controller.h"
21 #include "call/call.h"
22 #include "modules/audio_device/include/test_audio_device.h"
23 #include "modules/congestion_controller/goog_cc/test/goog_cc_printer.h"
24 #include "rtc_base/constructor_magic.h"
25 #include "rtc_base/task_queue_for_test.h"
26 #include "test/logging/log_writer.h"
27 #include "test/network/network_emulation.h"
28 #include "test/rtp_header_parser.h"
29 #include "test/scenario/column_printer.h"
30 #include "test/scenario/network_node.h"
31 #include "test/scenario/scenario_config.h"
32 
33 namespace webrtc {
34 
35 namespace test {
36 // Helper class to capture network controller state.
37 class NetworkControleUpdateCache : public NetworkControllerInterface {
38  public:
39   explicit NetworkControleUpdateCache(
40       std::unique_ptr<NetworkControllerInterface> controller);
41 
42   NetworkControlUpdate OnNetworkAvailability(NetworkAvailability msg) override;
43   NetworkControlUpdate OnNetworkRouteChange(NetworkRouteChange msg) override;
44   NetworkControlUpdate OnProcessInterval(ProcessInterval msg) override;
45   NetworkControlUpdate OnRemoteBitrateReport(RemoteBitrateReport msg) override;
46   NetworkControlUpdate OnRoundTripTimeUpdate(RoundTripTimeUpdate msg) override;
47   NetworkControlUpdate OnSentPacket(SentPacket msg) override;
48   NetworkControlUpdate OnReceivedPacket(ReceivedPacket msg) override;
49   NetworkControlUpdate OnStreamsConfig(StreamsConfig msg) override;
50   NetworkControlUpdate OnTargetRateConstraints(
51       TargetRateConstraints msg) override;
52   NetworkControlUpdate OnTransportLossReport(TransportLossReport msg) override;
53   NetworkControlUpdate OnTransportPacketsFeedback(
54       TransportPacketsFeedback msg) override;
55   NetworkControlUpdate OnNetworkStateEstimate(
56       NetworkStateEstimate msg) override;
57 
58   NetworkControlUpdate update_state() const;
59 
60  private:
61   NetworkControlUpdate Update(NetworkControlUpdate update);
62   const std::unique_ptr<NetworkControllerInterface> controller_;
63   NetworkControlUpdate update_state_;
64 };
65 
66 class LoggingNetworkControllerFactory
67     : public NetworkControllerFactoryInterface {
68  public:
69   LoggingNetworkControllerFactory(LogWriterFactoryInterface* log_writer_factory,
70                                   TransportControllerConfig config);
71   RTC_DISALLOW_COPY_AND_ASSIGN(LoggingNetworkControllerFactory);
72   ~LoggingNetworkControllerFactory();
73   std::unique_ptr<NetworkControllerInterface> Create(
74       NetworkControllerConfig config) override;
75   TimeDelta GetProcessInterval() const override;
76   // TODO(srte): Consider using the Columnprinter interface for this.
77   void LogCongestionControllerStats(Timestamp at_time);
78 
79   NetworkControlUpdate GetUpdate() const;
80 
81  private:
82   GoogCcDebugFactory goog_cc_factory_;
83   NetworkControllerFactoryInterface* cc_factory_ = nullptr;
84   bool print_cc_state_ = false;
85   NetworkControleUpdateCache* last_controller_ = nullptr;
86 };
87 
88 struct CallClientFakeAudio {
89   rtc::scoped_refptr<AudioProcessing> apm;
90   rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device;
91   rtc::scoped_refptr<AudioState> audio_state;
92 };
93 // CallClient represents a participant in a call scenario. It is created by the
94 // Scenario class and is used as sender and receiver when setting up a media
95 // stream session.
96 class CallClient : public EmulatedNetworkReceiverInterface {
97  public:
98   CallClient(TimeController* time_controller,
99              std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
100              CallClientConfig config);
101   RTC_DISALLOW_COPY_AND_ASSIGN(CallClient);
102 
103   ~CallClient();
104   ColumnPrinter StatsPrinter();
105   Call::Stats GetStats();
send_bandwidth()106   DataRate send_bandwidth() {
107     return DataRate::BitsPerSec(GetStats().send_bandwidth_bps);
108   }
109   DataRate target_rate() const;
110   DataRate stable_target_rate() const;
111   DataRate padding_rate() const;
112   void UpdateBitrateConstraints(const BitrateConstraints& constraints);
113 
114   void OnPacketReceived(EmulatedIpPacket packet) override;
115   std::unique_ptr<RtcEventLogOutput> GetLogWriter(std::string name);
116 
117   // Exposed publicly so that tests can execute tasks such as querying stats
118   // for media streams in the expected runtime environment (essentially what
119   // CallClient does internally for GetStats()).
120   void SendTask(std::function<void()> task);
121 
122  private:
123   friend class Scenario;
124   friend class CallClientPair;
125   friend class SendVideoStream;
126   friend class VideoStreamPair;
127   friend class ReceiveVideoStream;
128   friend class SendAudioStream;
129   friend class ReceiveAudioStream;
130   friend class AudioStreamPair;
131   friend class NetworkNodeTransport;
132   uint32_t GetNextVideoSsrc();
133   uint32_t GetNextVideoLocalSsrc();
134   uint32_t GetNextAudioSsrc();
135   uint32_t GetNextAudioLocalSsrc();
136   uint32_t GetNextRtxSsrc();
137   void AddExtensions(std::vector<RtpExtension> extensions);
138   int16_t Bind(EmulatedEndpoint* endpoint);
139   void UnBind();
140 
141   TimeController* const time_controller_;
142   Clock* clock_;
143   const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_;
144   std::unique_ptr<RtcEventLog> event_log_;
145   LoggingNetworkControllerFactory network_controller_factory_;
146   CallClientFakeAudio fake_audio_setup_;
147   std::unique_ptr<Call> call_;
148   std::unique_ptr<NetworkNodeTransport> transport_;
149   std::unique_ptr<RtpHeaderParser> const header_parser_;
150   std::vector<std::pair<EmulatedEndpoint*, uint16_t>> endpoints_;
151 
152   int next_video_ssrc_index_ = 0;
153   int next_video_local_ssrc_index_ = 0;
154   int next_rtx_ssrc_index_ = 0;
155   int next_audio_ssrc_index_ = 0;
156   int next_audio_local_ssrc_index_ = 0;
157   std::map<uint32_t, MediaType> ssrc_media_types_;
158   // Defined last so it's destroyed first.
159   TaskQueueForTest task_queue_;
160 
161   rtc::scoped_refptr<SharedModuleThread> module_thread_;
162 
163   const FieldTrialBasedConfig field_trials_;
164 };
165 
166 class CallClientPair {
167  public:
168   RTC_DISALLOW_COPY_AND_ASSIGN(CallClientPair);
169   ~CallClientPair();
first()170   CallClient* first() { return first_; }
second()171   CallClient* second() { return second_; }
forward()172   std::pair<CallClient*, CallClient*> forward() { return {first(), second()}; }
reverse()173   std::pair<CallClient*, CallClient*> reverse() { return {second(), first()}; }
174 
175  private:
176   friend class Scenario;
CallClientPair(CallClient * first,CallClient * second)177   CallClientPair(CallClient* first, CallClient* second)
178       : first_(first), second_(second) {}
179   CallClient* const first_;
180   CallClient* const second_;
181 };
182 }  // namespace test
183 }  // namespace webrtc
184 
185 #endif  // TEST_SCENARIO_CALL_CLIENT_H_
186