blob: 4ba43cc16678e84175be5fda4e3d0ca63c7da0a3 [file] [log] [blame]
minyue939df962017-04-19 01:58:38 -07001/*
2 * Copyright 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_tools/network_tester/test_controller.h"
minyue939df962017-04-19 01:58:38 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <limits>
14
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010015#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "rtc_base/checks.h"
Byoungchan Leec931f702022-07-03 17:20:17 +090017#include "rtc_base/internal/default_socket_server.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/ip_address.h"
Byoungchan Leec931f702022-07-03 17:20:17 +090019#include "rtc_base/logging.h"
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010020#include "rtc_base/thread.h"
21
minyue939df962017-04-19 01:58:38 -070022namespace webrtc {
23
24TestController::TestController(int min_port,
25 int max_port,
michaeltfcea39d2017-04-20 05:39:30 -070026 const std::string& config_file_path,
27 const std::string& log_file_path)
Byoungchan Leec931f702022-07-03 17:20:17 +090028 : socket_server_(rtc::CreateDefaultSocketServer()),
29 packet_sender_thread_(
30 std::make_unique<rtc::Thread>(socket_server_.get())),
31 socket_factory_(socket_server_.get()),
michaelt2fe9ac32017-04-20 06:56:27 -070032 config_file_path_(config_file_path),
michaeltfcea39d2017-04-20 05:39:30 -070033 packet_logger_(log_file_path),
minyue939df962017-04-19 01:58:38 -070034 local_test_done_(false),
Byoungchan Leec931f702022-07-03 17:20:17 +090035 remote_test_done_(false),
36 task_safety_flag_(PendingTaskSafetyFlag::CreateDetached()) {
minyue939df962017-04-19 01:58:38 -070037 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
tschumimd4aebb02017-05-22 00:04:30 -070038 send_data_.fill(42);
Byoungchan Leec931f702022-07-03 17:20:17 +090039 packet_sender_thread_->SetName("PacketSender", nullptr);
40 packet_sender_thread_->Start();
41 packet_sender_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
42 RTC_DCHECK_RUN_ON(packet_sender_thread_.get());
43 udp_socket_ =
44 std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket(
45 rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port));
46 udp_socket_->SignalReadPacket.connect(this, &TestController::OnReadPacket);
47 });
48}
49
50TestController::~TestController() {
51 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
52 packet_sender_thread_->Invoke<void>(
53 RTC_FROM_HERE, [this]() { task_safety_flag_->SetNotAlive(); });
minyue939df962017-04-19 01:58:38 -070054}
55
56void TestController::SendConnectTo(const std::string& hostname, int port) {
57 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010058 remote_address_ = rtc::SocketAddress(hostname, port);
minyue939df962017-04-19 01:58:38 -070059 NetworkTesterPacket packet;
60 packet.set_type(NetworkTesterPacket::HAND_SHAKING);
Danil Chapovalov431abd92018-06-18 12:54:17 +020061 SendData(packet, absl::nullopt);
Byoungchan Leec931f702022-07-03 17:20:17 +090062 MutexLock scoped_lock(&test_done_lock_);
minyue939df962017-04-19 01:58:38 -070063 local_test_done_ = false;
64 remote_test_done_ = false;
65}
66
minyue939df962017-04-19 01:58:38 -070067void TestController::SendData(const NetworkTesterPacket& packet,
Danil Chapovalov431abd92018-06-18 12:54:17 +020068 absl::optional<size_t> data_size) {
Byoungchan Leec931f702022-07-03 17:20:17 +090069 if (!packet_sender_thread_->IsCurrent()) {
Danil Chapovalov5286dcf2022-07-18 17:04:56 +020070 packet_sender_thread_->PostTask(SafeTask(
Byoungchan Leec931f702022-07-03 17:20:17 +090071 task_safety_flag_,
72 [this, packet, data_size]() { this->SendData(packet, data_size); }));
73 return;
74 }
75 RTC_DCHECK_RUN_ON(packet_sender_thread_.get());
76 RTC_LOG(LS_VERBOSE) << "SendData";
77
minyue939df962017-04-19 01:58:38 -070078 // Can be call from packet_sender or from test_controller thread.
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010079 size_t packet_size = packet.ByteSizeLong();
minyue939df962017-04-19 01:58:38 -070080 send_data_[0] = packet_size;
tschumimd4aebb02017-05-22 00:04:30 -070081 packet_size++;
minyue939df962017-04-19 01:58:38 -070082 packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
83 if (data_size && *data_size > packet_size)
84 packet_size = *data_size;
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010085 udp_socket_->SendTo((const void*)send_data_.data(), packet_size,
86 remote_address_, rtc::PacketOptions());
minyue939df962017-04-19 01:58:38 -070087}
88
89void TestController::OnTestDone() {
Byoungchan Leec931f702022-07-03 17:20:17 +090090 RTC_DCHECK_RUN_ON(packet_sender_thread_.get());
minyue939df962017-04-19 01:58:38 -070091 NetworkTesterPacket packet;
92 packet.set_type(NetworkTesterPacket::TEST_DONE);
Danil Chapovalov431abd92018-06-18 12:54:17 +020093 SendData(packet, absl::nullopt);
Byoungchan Leec931f702022-07-03 17:20:17 +090094 MutexLock scoped_lock(&test_done_lock_);
minyue939df962017-04-19 01:58:38 -070095 local_test_done_ = true;
96}
97
98bool TestController::IsTestDone() {
99 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
Byoungchan Leec931f702022-07-03 17:20:17 +0900100 MutexLock scoped_lock(&test_done_lock_);
minyue939df962017-04-19 01:58:38 -0700101 return local_test_done_ && remote_test_done_;
102}
103
104void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket,
105 const char* data,
106 size_t len,
107 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100108 const int64_t& packet_time_us) {
Byoungchan Leec931f702022-07-03 17:20:17 +0900109 RTC_DCHECK_RUN_ON(packet_sender_thread_.get());
110 RTC_LOG(LS_VERBOSE) << "OnReadPacket";
minyue939df962017-04-19 01:58:38 -0700111 size_t packet_size = data[0];
112 std::string receive_data(&data[1], packet_size);
113 NetworkTesterPacket packet;
114 packet.ParseFromString(receive_data);
115 RTC_CHECK(packet.has_type());
116 switch (packet.type()) {
117 case NetworkTesterPacket::HAND_SHAKING: {
118 NetworkTesterPacket packet;
119 packet.set_type(NetworkTesterPacket::TEST_START);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +0100120 remote_address_ = remote_addr;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200121 SendData(packet, absl::nullopt);
Byoungchan Leec931f702022-07-03 17:20:17 +0900122 packet_sender_.reset(new PacketSender(this, packet_sender_thread_.get(),
123 task_safety_flag_,
124 config_file_path_));
minyue939df962017-04-19 01:58:38 -0700125 packet_sender_->StartSending();
Byoungchan Leec931f702022-07-03 17:20:17 +0900126 MutexLock scoped_lock(&test_done_lock_);
minyue939df962017-04-19 01:58:38 -0700127 local_test_done_ = false;
128 remote_test_done_ = false;
129 break;
130 }
131 case NetworkTesterPacket::TEST_START: {
Byoungchan Leec931f702022-07-03 17:20:17 +0900132 packet_sender_.reset(new PacketSender(this, packet_sender_thread_.get(),
133 task_safety_flag_,
134 config_file_path_));
minyue939df962017-04-19 01:58:38 -0700135 packet_sender_->StartSending();
Byoungchan Leec931f702022-07-03 17:20:17 +0900136 MutexLock scoped_lock(&test_done_lock_);
michaelt2fe9ac32017-04-20 06:56:27 -0700137 local_test_done_ = false;
138 remote_test_done_ = false;
minyue939df962017-04-19 01:58:38 -0700139 break;
140 }
141 case NetworkTesterPacket::TEST_DATA: {
Niels Möllere6933812018-11-05 13:01:41 +0100142 packet.set_arrival_timestamp(packet_time_us);
minyue939df962017-04-19 01:58:38 -0700143 packet.set_packet_size(len);
michaeltfcea39d2017-04-20 05:39:30 -0700144 packet_logger_.LogPacket(packet);
minyue939df962017-04-19 01:58:38 -0700145 break;
146 }
147 case NetworkTesterPacket::TEST_DONE: {
Byoungchan Leec931f702022-07-03 17:20:17 +0900148 MutexLock scoped_lock(&test_done_lock_);
minyue939df962017-04-19 01:58:38 -0700149 remote_test_done_ = true;
150 break;
151 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200152 default: {
Artem Titovd3251962021-11-15 16:57:07 +0100153 RTC_DCHECK_NOTREACHED();
Jonas Olssona4d87372019-07-05 19:08:33 +0200154 }
minyue939df962017-04-19 01:58:38 -0700155 }
156}
157
158} // namespace webrtc