blob: 489844039b6df76ed2682972b69ca04d5cbc5920 [file] [log] [blame]
Sebastian Jansson98b07e92018-09-27 13:47:01 +02001/*
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_
Bjorn Terelius5c2f1f02019-01-16 17:45:05 +010012
13#include <map>
Sebastian Jansson98b07e92018-09-27 13:47:01 +020014#include <memory>
15#include <string>
Bjorn Terelius5c2f1f02019-01-16 17:45:05 +010016#include <utility>
Sebastian Jansson98b07e92018-09-27 13:47:01 +020017#include <vector>
18
Danil Chapovalov83bbe912019-08-07 12:24:53 +020019#include "api/rtc_event_log/rtc_event_log.h"
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080020#include "api/test/time_controller.h"
Christoffer Rodbro8649e492020-12-15 20:20:54 +010021#include "api/units/data_rate.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020022#include "call/call.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020023#include "modules/audio_device/include/test_audio_device.h"
Sebastian Jansson871ac422019-05-17 17:53:44 +020024#include "modules/congestion_controller/goog_cc/test/goog_cc_printer.h"
Sebastian Jansson105a10a2019-04-01 09:18:14 +020025#include "rtc_base/task_queue_for_test.h"
Sebastian Jansson52de8b02019-01-16 17:25:44 +010026#include "test/logging/log_writer.h"
Artem Titov386802e2019-07-05 10:48:17 +020027#include "test/network/network_emulation.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020028#include "test/scenario/column_printer.h"
Sebastian Jansson800e1212018-10-22 11:49:03 +020029#include "test/scenario/network_node.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020030#include "test/scenario/scenario_config.h"
31
32namespace webrtc {
33
34namespace test {
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020035// Helper class to capture network controller state.
36class 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;
Sebastian Jansson49167de2019-06-27 15:59:03 +020054 NetworkControlUpdate OnNetworkStateEstimate(
55 NetworkStateEstimate msg) override;
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020056
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
Sebastian Jansson98b07e92018-09-27 13:47:01 +020065class LoggingNetworkControllerFactory
66 : public NetworkControllerFactoryInterface {
67 public:
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020068 LoggingNetworkControllerFactory(LogWriterFactoryInterface* log_writer_factory,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020069 TransportControllerConfig config);
Artem Titov6cae2d52022-01-26 15:01:10 +000070
Sebastian Jansson98b07e92018-09-27 13:47:01 +020071 ~LoggingNetworkControllerFactory();
Artem Titov6cae2d52022-01-26 15:01:10 +000072
73 LoggingNetworkControllerFactory(const LoggingNetworkControllerFactory&) =
74 delete;
75 LoggingNetworkControllerFactory& operator=(
76 const LoggingNetworkControllerFactory&) = delete;
77
Sebastian Jansson98b07e92018-09-27 13:47:01 +020078 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);
Christoffer Rodbro8649e492020-12-15 20:20:54 +010083 void SetRemoteBitrateEstimate(RemoteBitrateReport msg);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020084
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020085 NetworkControlUpdate GetUpdate() const;
86
Sebastian Jansson98b07e92018-09-27 13:47:01 +020087 private:
Sebastian Jansson871ac422019-05-17 17:53:44 +020088 GoogCcDebugFactory goog_cc_factory_;
Sebastian Janssonf0d03122018-12-18 15:53:04 +010089 NetworkControllerFactoryInterface* cc_factory_ = nullptr;
Sebastian Jansson871ac422019-05-17 17:53:44 +020090 bool print_cc_state_ = false;
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020091 NetworkControleUpdateCache* last_controller_ = nullptr;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020092};
93
Sebastian Jansson800e1212018-10-22 11:49:03 +020094struct CallClientFakeAudio {
95 rtc::scoped_refptr<AudioProcessing> apm;
96 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device;
97 rtc::scoped_refptr<AudioState> audio_state;
98};
Sebastian Jansson98b07e92018-09-27 13:47:01 +020099// 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.
Artem Titov40f51152019-01-04 15:45:01 +0100102class CallClient : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200103 public:
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200104 CallClient(TimeController* time_controller,
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100105 std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
Sebastian Jansson9a4f38e2018-12-19 13:14:41 +0100106 CallClientConfig config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200107
108 ~CallClient();
Artem Titov6cae2d52022-01-26 15:01:10 +0000109
110 CallClient(const CallClient&) = delete;
111 CallClient& operator=(const CallClient&) = delete;
112
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200113 ColumnPrinter StatsPrinter();
114 Call::Stats GetStats();
Sebastian Jansson800e1212018-10-22 11:49:03 +0200115 DataRate send_bandwidth() {
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100116 return DataRate::BitsPerSec(GetStats().send_bandwidth_bps);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200117 }
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200118 DataRate target_rate() const;
Florent Castelli4e615d52019-08-22 16:09:06 +0200119 DataRate stable_target_rate() const;
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200120 DataRate padding_rate() const;
Erik Språng3e3e1662020-10-06 21:51:21 +0200121 void UpdateBitrateConstraints(const BitrateConstraints& constraints);
Christoffer Rodbro8649e492020-12-15 20:20:54 +0100122 void SetRemoteBitrate(DataRate bitrate);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200123
Artem Titov40f51152019-01-04 15:45:01 +0100124 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100125 std::unique_ptr<RtcEventLogOutput> GetLogWriter(std::string name);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200126
Tommi3c9bcc12020-04-15 16:45:47 +0200127 // 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
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200132 private:
133 friend class Scenario;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200134 friend class CallClientPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200135 friend class SendVideoStream;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200136 friend class VideoStreamPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200137 friend class ReceiveVideoStream;
138 friend class SendAudioStream;
139 friend class ReceiveAudioStream;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200140 friend class AudioStreamPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200141 friend class NetworkNodeTransport;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200142 uint32_t GetNextVideoSsrc();
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100143 uint32_t GetNextVideoLocalSsrc();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200144 uint32_t GetNextAudioSsrc();
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100145 uint32_t GetNextAudioLocalSsrc();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200146 uint32_t GetNextRtxSsrc();
Sebastian Jansson77bd3852020-01-17 13:05:54 +0100147 int16_t Bind(EmulatedEndpoint* endpoint);
148 void UnBind();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200149
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200150 TimeController* const time_controller_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200151 Clock* clock_;
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100152 const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_;
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200153 std::unique_ptr<RtcEventLog> event_log_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200154 LoggingNetworkControllerFactory network_controller_factory_;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200155 CallClientFakeAudio fake_audio_setup_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200156 std::unique_ptr<Call> call_;
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200157 std::unique_ptr<NetworkNodeTransport> transport_;
Sebastian Jansson77bd3852020-01-17 13:05:54 +0100158 std::vector<std::pair<EmulatedEndpoint*, uint16_t>> endpoints_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200159
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200160 int next_video_ssrc_index_ = 0;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100161 int next_video_local_ssrc_index_ = 0;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200162 int next_rtx_ssrc_index_ = 0;
163 int next_audio_ssrc_index_ = 0;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100164 int next_audio_local_ssrc_index_ = 0;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200165 std::map<uint32_t, MediaType> ssrc_media_types_;
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200166 // Defined last so it's destroyed first.
167 TaskQueueForTest task_queue_;
Erik Språng014dd3c2019-11-28 13:44:25 +0100168
Tommi25c77c12020-05-25 17:44:55 +0200169 rtc::scoped_refptr<SharedModuleThread> module_thread_;
170
Erik Språng014dd3c2019-11-28 13:44:25 +0100171 const FieldTrialBasedConfig field_trials_;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200172};
173
174class CallClientPair {
175 public:
Sebastian Jansson800e1212018-10-22 11:49:03 +0200176 ~CallClientPair();
Artem Titov6cae2d52022-01-26 15:01:10 +0000177
178 CallClientPair(const CallClientPair&) = delete;
179 CallClientPair& operator=(const CallClientPair&) = delete;
180
Sebastian Jansson800e1212018-10-22 11:49:03 +0200181 CallClient* first() { return first_; }
182 CallClient* second() { return second_; }
183 std::pair<CallClient*, CallClient*> forward() { return {first(), second()}; }
184 std::pair<CallClient*, CallClient*> reverse() { return {second(), first()}; }
185
186 private:
187 friend class Scenario;
188 CallClientPair(CallClient* first, CallClient* second)
189 : first_(first), second_(second) {}
190 CallClient* const first_;
191 CallClient* const second_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200192};
193} // namespace test
194} // namespace webrtc
195
196#endif // TEST_SCENARIO_CALL_CLIENT_H_