blob: 37efad620b539e00f0047c4ffd351a99b7f06370 [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
19#include "call/call.h"
20#include "logging/rtc_event_log/rtc_event_log.h"
21#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"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020027#include "test/scenario/column_printer.h"
Artem Titov40f51152019-01-04 15:45:01 +010028#include "test/scenario/network/network_emulation.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;
55
56 NetworkControlUpdate update_state() const;
57
58 private:
59 NetworkControlUpdate Update(NetworkControlUpdate update);
60 const std::unique_ptr<NetworkControllerInterface> controller_;
61 NetworkControlUpdate update_state_;
62};
63
Sebastian Jansson98b07e92018-09-27 13:47:01 +020064class LoggingNetworkControllerFactory
65 : public NetworkControllerFactoryInterface {
66 public:
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020067 LoggingNetworkControllerFactory(LogWriterFactoryInterface* log_writer_factory,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020068 TransportControllerConfig config);
69 RTC_DISALLOW_COPY_AND_ASSIGN(LoggingNetworkControllerFactory);
70 ~LoggingNetworkControllerFactory();
71 std::unique_ptr<NetworkControllerInterface> Create(
72 NetworkControllerConfig config) override;
73 TimeDelta GetProcessInterval() const override;
74 // TODO(srte): Consider using the Columnprinter interface for this.
75 void LogCongestionControllerStats(Timestamp at_time);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020076
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020077 NetworkControlUpdate GetUpdate() const;
78
Sebastian Jansson98b07e92018-09-27 13:47:01 +020079 private:
Sebastian Jansson871ac422019-05-17 17:53:44 +020080 GoogCcDebugFactory goog_cc_factory_;
Sebastian Janssonf0d03122018-12-18 15:53:04 +010081 NetworkControllerFactoryInterface* cc_factory_ = nullptr;
Sebastian Jansson871ac422019-05-17 17:53:44 +020082 bool print_cc_state_ = false;
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020083 NetworkControleUpdateCache* last_controller_ = nullptr;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020084};
85
Sebastian Jansson800e1212018-10-22 11:49:03 +020086struct CallClientFakeAudio {
87 rtc::scoped_refptr<AudioProcessing> apm;
88 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device;
89 rtc::scoped_refptr<AudioState> audio_state;
90};
Sebastian Jansson98b07e92018-09-27 13:47:01 +020091// CallClient represents a participant in a call scenario. It is created by the
92// Scenario class and is used as sender and receiver when setting up a media
93// stream session.
Artem Titov40f51152019-01-04 15:45:01 +010094class CallClient : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020095 public:
Sebastian Jansson105a10a2019-04-01 09:18:14 +020096 CallClient(TimeController* time_controller,
Sebastian Jansson52de8b02019-01-16 17:25:44 +010097 std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
Sebastian Jansson9a4f38e2018-12-19 13:14:41 +010098 CallClientConfig config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020099 RTC_DISALLOW_COPY_AND_ASSIGN(CallClient);
100
101 ~CallClient();
102 ColumnPrinter StatsPrinter();
103 Call::Stats GetStats();
Sebastian Jansson800e1212018-10-22 11:49:03 +0200104 DataRate send_bandwidth() {
105 return DataRate::bps(GetStats().send_bandwidth_bps);
106 }
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200107 DataRate target_rate() const;
108 DataRate link_capacity() const;
109 DataRate padding_rate() const;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200110
Artem Titov40f51152019-01-04 15:45:01 +0100111 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100112 std::unique_ptr<RtcEventLogOutput> GetLogWriter(std::string name);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200113
114 private:
115 friend class Scenario;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200116 friend class CallClientPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200117 friend class SendVideoStream;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200118 friend class VideoStreamPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200119 friend class ReceiveVideoStream;
120 friend class SendAudioStream;
121 friend class ReceiveAudioStream;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200122 friend class AudioStreamPair;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200123 friend class NetworkNodeTransport;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200124 uint32_t GetNextVideoSsrc();
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100125 uint32_t GetNextVideoLocalSsrc();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200126 uint32_t GetNextAudioSsrc();
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100127 uint32_t GetNextAudioLocalSsrc();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200128 uint32_t GetNextRtxSsrc();
129 std::string GetNextPriorityId();
Sebastian Janssonfd201712018-11-12 16:44:16 +0100130 void AddExtensions(std::vector<RtpExtension> extensions);
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200131 void SendTask(std::function<void()> task);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200132
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200133 TimeController* const time_controller_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200134 Clock* clock_;
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100135 const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_;
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200136 std::unique_ptr<RtcEventLog> event_log_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200137 LoggingNetworkControllerFactory network_controller_factory_;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200138 CallClientFakeAudio fake_audio_setup_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200139 std::unique_ptr<Call> call_;
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200140 std::unique_ptr<NetworkNodeTransport> transport_;
141 std::unique_ptr<RtpHeaderParser> const header_parser_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200142
Artem Titov40f51152019-01-04 15:45:01 +0100143 // Stores the configured overhead per known destination endpoint. This is used
144 // to subtract the overhead before processing.
Artem Titov4cd433e2019-04-01 11:01:16 +0200145 std::map<rtc::IPAddress, DataSize> route_overhead_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200146 int next_video_ssrc_index_ = 0;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100147 int next_video_local_ssrc_index_ = 0;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200148 int next_rtx_ssrc_index_ = 0;
149 int next_audio_ssrc_index_ = 0;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100150 int next_audio_local_ssrc_index_ = 0;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200151 int next_priority_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_