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