xref: /aosp_15_r20/external/webrtc/rtc_tools/network_tester/test_controller.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "rtc_tools/network_tester/test_controller.h"
12 
13 #include <limits>
14 
15 #include "absl/types/optional.h"
16 #include "rtc_base/checks.h"
17 #include "rtc_base/internal/default_socket_server.h"
18 #include "rtc_base/ip_address.h"
19 #include "rtc_base/logging.h"
20 #include "rtc_base/thread.h"
21 
22 namespace webrtc {
23 
TestController(int min_port,int max_port,const std::string & config_file_path,const std::string & log_file_path)24 TestController::TestController(int min_port,
25                                int max_port,
26                                const std::string& config_file_path,
27                                const std::string& log_file_path)
28     : socket_server_(rtc::CreateDefaultSocketServer()),
29       packet_sender_thread_(
30           std::make_unique<rtc::Thread>(socket_server_.get())),
31       socket_factory_(socket_server_.get()),
32       config_file_path_(config_file_path),
33       packet_logger_(log_file_path),
34       local_test_done_(false),
35       remote_test_done_(false),
36       task_safety_flag_(PendingTaskSafetyFlag::CreateDetached()) {
37   RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
38   send_data_.fill(42);
39   packet_sender_thread_->SetName("PacketSender", nullptr);
40   packet_sender_thread_->Start();
41   packet_sender_thread_->BlockingCall([&] {
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 
~TestController()50 TestController::~TestController() {
51   RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
52   packet_sender_thread_->BlockingCall(
53       [this]() { task_safety_flag_->SetNotAlive(); });
54 }
55 
SendConnectTo(const std::string & hostname,int port)56 void TestController::SendConnectTo(const std::string& hostname, int port) {
57   RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
58   remote_address_ = rtc::SocketAddress(hostname, port);
59   NetworkTesterPacket packet;
60   packet.set_type(NetworkTesterPacket::HAND_SHAKING);
61   SendData(packet, absl::nullopt);
62   MutexLock scoped_lock(&test_done_lock_);
63   local_test_done_ = false;
64   remote_test_done_ = false;
65 }
66 
SendData(const NetworkTesterPacket & packet,absl::optional<size_t> data_size)67 void TestController::SendData(const NetworkTesterPacket& packet,
68                               absl::optional<size_t> data_size) {
69   if (!packet_sender_thread_->IsCurrent()) {
70     packet_sender_thread_->PostTask(SafeTask(
71         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 
78   // Can be call from packet_sender or from test_controller thread.
79   size_t packet_size = packet.ByteSizeLong();
80   send_data_[0] = packet_size;
81   packet_size++;
82   packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
83   if (data_size && *data_size > packet_size)
84     packet_size = *data_size;
85   udp_socket_->SendTo((const void*)send_data_.data(), packet_size,
86                       remote_address_, rtc::PacketOptions());
87 }
88 
OnTestDone()89 void TestController::OnTestDone() {
90   RTC_DCHECK_RUN_ON(packet_sender_thread_.get());
91   NetworkTesterPacket packet;
92   packet.set_type(NetworkTesterPacket::TEST_DONE);
93   SendData(packet, absl::nullopt);
94   MutexLock scoped_lock(&test_done_lock_);
95   local_test_done_ = true;
96 }
97 
IsTestDone()98 bool TestController::IsTestDone() {
99   RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
100   MutexLock scoped_lock(&test_done_lock_);
101   return local_test_done_ && remote_test_done_;
102 }
103 
OnReadPacket(rtc::AsyncPacketSocket * socket,const char * data,size_t len,const rtc::SocketAddress & remote_addr,const int64_t & packet_time_us)104 void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket,
105                                   const char* data,
106                                   size_t len,
107                                   const rtc::SocketAddress& remote_addr,
108                                   const int64_t& packet_time_us) {
109   RTC_DCHECK_RUN_ON(packet_sender_thread_.get());
110   RTC_LOG(LS_VERBOSE) << "OnReadPacket";
111   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);
120       remote_address_ = remote_addr;
121       SendData(packet, absl::nullopt);
122       packet_sender_.reset(new PacketSender(this, packet_sender_thread_.get(),
123                                             task_safety_flag_,
124                                             config_file_path_));
125       packet_sender_->StartSending();
126       MutexLock scoped_lock(&test_done_lock_);
127       local_test_done_ = false;
128       remote_test_done_ = false;
129       break;
130     }
131     case NetworkTesterPacket::TEST_START: {
132       packet_sender_.reset(new PacketSender(this, packet_sender_thread_.get(),
133                                             task_safety_flag_,
134                                             config_file_path_));
135       packet_sender_->StartSending();
136       MutexLock scoped_lock(&test_done_lock_);
137       local_test_done_ = false;
138       remote_test_done_ = false;
139       break;
140     }
141     case NetworkTesterPacket::TEST_DATA: {
142       packet.set_arrival_timestamp(packet_time_us);
143       packet.set_packet_size(len);
144       packet_logger_.LogPacket(packet);
145       break;
146     }
147     case NetworkTesterPacket::TEST_DONE: {
148       MutexLock scoped_lock(&test_done_lock_);
149       remote_test_done_ = true;
150       break;
151     }
152     default: {
153       RTC_DCHECK_NOTREACHED();
154     }
155   }
156 }
157 
158 }  // namespace webrtc
159