blob: 49939ed8e6117b390b0d31bdbd5db1bb912e3a4c [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"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020020#include "call/call.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020021#include "modules/audio_device/include/test_audio_device.h"
Sebastian Jansson871ac422019-05-17 17:53:44 +020022#include "modules/congestion_controller/goog_cc/test/goog_cc_printer.h"
Sebastian Jansson800e1212018-10-22 11:49:03 +020023#include "modules/rtp_rtcp/include/rtp_header_parser.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/constructor_magic.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"
Sebastian Jansson105a10a2019-04-01 09:18:14 +020031#include "test/time_controller/time_controller.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020032
33namespace webrtc {
34
35namespace test {
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020036// Helper class to capture network controller state.
37class 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;
Sebastian Jansson49167de2019-06-27 15:59:03 +020055 NetworkControlUpdate OnNetworkStateEstimate(
56 NetworkStateEstimate msg) override;
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020057
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
Sebastian Jansson98b07e92018-09-27 13:47:01 +020066class LoggingNetworkControllerFactory
67 : public NetworkControllerFactoryInterface {
68 public:
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020069 LoggingNetworkControllerFactory(LogWriterFactoryInterface* log_writer_factory,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020070 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);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020078
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020079 NetworkControlUpdate GetUpdate() const;
80
Sebastian Jansson98b07e92018-09-27 13:47:01 +020081 private:
Sebastian Jansson871ac422019-05-17 17:53:44 +020082 GoogCcDebugFactory goog_cc_factory_;
Sebastian Janssonf0d03122018-12-18 15:53:04 +010083 NetworkControllerFactoryInterface* cc_factory_ = nullptr;
Sebastian Jansson871ac422019-05-17 17:53:44 +020084 bool print_cc_state_ = false;
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020085 NetworkControleUpdateCache* last_controller_ = nullptr;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020086};
87
Sebastian Jansson800e1212018-10-22 11:49:03 +020088struct CallClientFakeAudio {
89 rtc::scoped_refptr<AudioProcessing> apm;
90 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device;
91 rtc::scoped_refptr<AudioState> audio_state;
92};
Sebastian Jansson98b07e92018-09-27 13:47:01 +020093// 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.
Artem Titov40f51152019-01-04 15:45:01 +010096class CallClient : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020097 public:
Sebastian Jansson105a10a2019-04-01 09:18:14 +020098 CallClient(TimeController* time_controller,
Sebastian Jansson52de8b02019-01-16 17:25:44 +010099 std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
Sebastian Jansson9a4f38e2018-12-19 13:14:41 +0100100 CallClientConfig config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200101 RTC_DISALLOW_COPY_AND_ASSIGN(CallClient);
102
103 ~CallClient();
104 ColumnPrinter StatsPrinter();
105 Call::Stats GetStats();
Sebastian Jansson800e1212018-10-22 11:49:03 +0200106 DataRate send_bandwidth() {
107 return DataRate::bps(GetStats().send_bandwidth_bps);
108 }
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200109 DataRate target_rate() const;
110 DataRate link_capacity() const;
111 DataRate padding_rate() const;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200112
Artem Titov40f51152019-01-04 15:45:01 +0100113 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100114 std::unique_ptr<RtcEventLogOutput> GetLogWriter(std::string name);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200115
116 private:
117 friend class Scenario;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200118 friend class CallClientPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200119 friend class SendVideoStream;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200120 friend class VideoStreamPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200121 friend class ReceiveVideoStream;
122 friend class SendAudioStream;
123 friend class ReceiveAudioStream;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200124 friend class AudioStreamPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200125 friend class NetworkNodeTransport;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200126 uint32_t GetNextVideoSsrc();
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100127 uint32_t GetNextVideoLocalSsrc();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200128 uint32_t GetNextAudioSsrc();
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100129 uint32_t GetNextAudioLocalSsrc();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200130 uint32_t GetNextRtxSsrc();
Sebastian Janssonfd201712018-11-12 16:44:16 +0100131 void AddExtensions(std::vector<RtpExtension> extensions);
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200132 void SendTask(std::function<void()> task);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200133
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200134 TimeController* const time_controller_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200135 Clock* clock_;
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100136 const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_;
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200137 std::unique_ptr<RtcEventLog> event_log_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200138 LoggingNetworkControllerFactory network_controller_factory_;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200139 CallClientFakeAudio fake_audio_setup_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200140 std::unique_ptr<Call> call_;
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200141 std::unique_ptr<NetworkNodeTransport> transport_;
142 std::unique_ptr<RtpHeaderParser> const header_parser_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200143
Artem Titov40f51152019-01-04 15:45:01 +0100144 // Stores the configured overhead per known destination endpoint. This is used
145 // to subtract the overhead before processing.
Artem Titov4cd433e2019-04-01 11:01:16 +0200146 std::map<rtc::IPAddress, DataSize> route_overhead_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200147 int next_video_ssrc_index_ = 0;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100148 int next_video_local_ssrc_index_ = 0;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200149 int next_rtx_ssrc_index_ = 0;
150 int next_audio_ssrc_index_ = 0;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100151 int next_audio_local_ssrc_index_ = 0;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200152 std::map<uint32_t, MediaType> ssrc_media_types_;
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200153 // Defined last so it's destroyed first.
154 TaskQueueForTest task_queue_;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200155};
156
157class CallClientPair {
158 public:
159 RTC_DISALLOW_COPY_AND_ASSIGN(CallClientPair);
160 ~CallClientPair();
161 CallClient* first() { return first_; }
162 CallClient* second() { return second_; }
163 std::pair<CallClient*, CallClient*> forward() { return {first(), second()}; }
164 std::pair<CallClient*, CallClient*> reverse() { return {second(), first()}; }
165
166 private:
167 friend class Scenario;
168 CallClientPair(CallClient* first, CallClient* second)
169 : first_(first), second_(second) {}
170 CallClient* const first_;
171 CallClient* const second_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200172};
173} // namespace test
174} // namespace webrtc
175
176#endif // TEST_SCENARIO_CALL_CLIENT_H_