OLD | NEW |
(Empty) | |
| 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 "webrtc/tools/network_tester/packet_sender.h" |
| 12 |
| 13 #include <string> |
| 14 #include <utility> |
| 15 |
| 16 #include "webrtc/base/timeutils.h" |
| 17 #include "webrtc/tools/network_tester/config_reader.h" |
| 18 #include "webrtc/tools/network_tester/test_controller.h" |
| 19 |
| 20 namespace webrtc { |
| 21 |
| 22 namespace { |
| 23 |
| 24 class SendPacketTask : public rtc::QueuedTask { |
| 25 public: |
| 26 explicit SendPacketTask(PacketSender* packet_sender) |
| 27 : packet_sender_(packet_sender) {} |
| 28 |
| 29 private: |
| 30 bool Run() override { |
| 31 if (packet_sender_->IsSending()) { |
| 32 packet_sender_->SendPacket(); |
| 33 rtc::TaskQueue::Current()->PostDelayedTask( |
| 34 std::unique_ptr<QueuedTask>(this), |
| 35 packet_sender_->GetSendIntervalMs()); |
| 36 return false; |
| 37 } else { |
| 38 return true; |
| 39 } |
| 40 } |
| 41 PacketSender* const packet_sender_; |
| 42 }; |
| 43 |
| 44 class UpdateTestSettingTask : public rtc::QueuedTask { |
| 45 public: |
| 46 UpdateTestSettingTask(PacketSender* packet_sender, |
| 47 std::unique_ptr<ConfigReader> config_reader) |
| 48 : packet_sender_(packet_sender), |
| 49 config_reader_(std::move(config_reader)) {} |
| 50 |
| 51 private: |
| 52 bool Run() override { |
| 53 auto config = config_reader_->GetNextConfig(); |
| 54 if (config) { |
| 55 packet_sender_->UpdateTestSetting((*config).packet_size, |
| 56 (*config).packet_send_interval_ms); |
| 57 rtc::TaskQueue::Current()->PostDelayedTask( |
| 58 std::unique_ptr<QueuedTask>(this), (*config).execution_time_ms); |
| 59 return false; |
| 60 } else { |
| 61 packet_sender_->StopSending(); |
| 62 return true; |
| 63 } |
| 64 } |
| 65 PacketSender* const packet_sender_; |
| 66 const std::unique_ptr<ConfigReader> config_reader_; |
| 67 }; |
| 68 |
| 69 } // namespace |
| 70 |
| 71 PacketSender::PacketSender(TestController* test_controller, |
| 72 const std::string& config_file_path) |
| 73 : packet_size_(0), |
| 74 send_interval_ms_(0), |
| 75 sequence_number_(0), |
| 76 sending_(false), |
| 77 config_file_path_(config_file_path), |
| 78 test_controller_(test_controller), |
| 79 worker_queue_("Packet Sender", rtc::TaskQueue::Priority::HIGH) {} |
| 80 |
| 81 PacketSender::~PacketSender() = default; |
| 82 |
| 83 void PacketSender::StartSending() { |
| 84 worker_queue_checker_.Detach(); |
| 85 worker_queue_.PostTask([this]() { |
| 86 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_queue_checker_); |
| 87 sending_ = true; |
| 88 }); |
| 89 worker_queue_.PostTask( |
| 90 std::unique_ptr<rtc::QueuedTask>(new UpdateTestSettingTask( |
| 91 this, |
| 92 std::unique_ptr<ConfigReader>(new ConfigReader(config_file_path_))))); |
| 93 worker_queue_.PostTask( |
| 94 std::unique_ptr<rtc::QueuedTask>(new SendPacketTask(this))); |
| 95 } |
| 96 |
| 97 void PacketSender::StopSending() { |
| 98 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_queue_checker_); |
| 99 sending_ = false; |
| 100 test_controller_->OnTestDone(); |
| 101 } |
| 102 |
| 103 bool PacketSender::IsSending() const { |
| 104 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_queue_checker_); |
| 105 return sending_; |
| 106 } |
| 107 |
| 108 void PacketSender::SendPacket() { |
| 109 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_queue_checker_); |
| 110 NetworkTesterPacket packet; |
| 111 packet.set_type(NetworkTesterPacket::TEST_DATA); |
| 112 packet.set_sequence_number(sequence_number_++); |
| 113 packet.set_send_timestamp(rtc::TimeMicros()); |
| 114 test_controller_->SendData(packet, rtc::Optional<size_t>(packet_size_)); |
| 115 } |
| 116 |
| 117 int64_t PacketSender::GetSendIntervalMs() const { |
| 118 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_queue_checker_); |
| 119 return send_interval_ms_; |
| 120 } |
| 121 |
| 122 void PacketSender::UpdateTestSetting(size_t packet_size, |
| 123 int64_t send_interval_ms) { |
| 124 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_queue_checker_); |
| 125 send_interval_ms_ = send_interval_ms; |
| 126 packet_size_ = packet_size; |
| 127 } |
| 128 |
| 129 } // namespace webrtc |
OLD | NEW |