Sebastian Jansson | 71a091e | 2018-09-27 19:08:21 +0200 | [diff] [blame] | 1 | /* |
| 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_SIMULATED_TIME_H_ |
| 11 | #define TEST_SCENARIO_SIMULATED_TIME_H_ |
| 12 | |
| 13 | #include <deque> |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <set> |
| 17 | #include <string> |
| 18 | #include <utility> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "test/scenario/call_client.h" |
| 22 | #include "test/scenario/column_printer.h" |
| 23 | #include "test/scenario/network_node.h" |
| 24 | #include "test/scenario/scenario_config.h" |
| 25 | |
| 26 | namespace webrtc { |
| 27 | namespace test { |
| 28 | class PacketStream { |
| 29 | public: |
| 30 | explicit PacketStream(PacketStreamConfig config); |
| 31 | |
| 32 | private: |
| 33 | std::vector<int64_t> PullPackets(Timestamp at_time); |
| 34 | void OnTargetRateUpdate(DataRate target_rate); |
| 35 | |
| 36 | friend class SimulatedTimeClient; |
| 37 | PacketStreamConfig config_; |
| 38 | bool next_frame_is_keyframe_ = true; |
| 39 | Timestamp next_frame_time_ = Timestamp::MinusInfinity(); |
| 40 | DataRate target_rate_ = DataRate::Zero(); |
| 41 | int64_t budget_ = 0; |
| 42 | }; |
| 43 | |
| 44 | class SimulatedFeedback : NetworkReceiverInterface { |
| 45 | public: |
| 46 | SimulatedFeedback(SimulatedTimeClientConfig config, |
| 47 | uint64_t return_receiver_id, |
| 48 | NetworkNode* return_node); |
| 49 | bool TryDeliverPacket(rtc::CopyOnWriteBuffer packet, |
| 50 | uint64_t receiver, |
| 51 | Timestamp at_time) override; |
| 52 | |
| 53 | private: |
| 54 | friend class SimulatedTimeClient; |
| 55 | const SimulatedTimeClientConfig config_; |
| 56 | const uint64_t return_receiver_id_; |
| 57 | NetworkNode* return_node_; |
| 58 | Timestamp last_feedback_time_ = Timestamp::MinusInfinity(); |
| 59 | int32_t next_feedback_seq_num_ = 1; |
| 60 | std::map<int64_t, Timestamp> receive_times_; |
| 61 | }; |
| 62 | |
| 63 | struct SimpleFeedbackReportPacket { |
| 64 | struct ReceiveInfo { |
| 65 | int64_t sequence_number; |
| 66 | Timestamp receive_time; |
| 67 | }; |
| 68 | std::vector<ReceiveInfo> receive_times; |
| 69 | }; |
| 70 | |
| 71 | SimpleFeedbackReportPacket FeedbackFromBuffer( |
| 72 | rtc::CopyOnWriteBuffer raw_buffer); |
| 73 | rtc::CopyOnWriteBuffer FeedbackToBuffer( |
| 74 | const SimpleFeedbackReportPacket packet); |
| 75 | |
| 76 | class SimulatedSender { |
| 77 | public: |
| 78 | struct PacketReadyToSend { |
| 79 | SentPacket send_info; |
| 80 | rtc::CopyOnWriteBuffer data; |
| 81 | }; |
| 82 | struct PendingPacket { |
| 83 | int64_t size; |
| 84 | }; |
| 85 | |
| 86 | SimulatedSender(NetworkNode* send_node, uint64_t send_receiver_id); |
| 87 | SimulatedSender(const SimulatedSender&) = delete; |
| 88 | ~SimulatedSender(); |
| 89 | TransportPacketsFeedback PullFeedbackReport(SimpleFeedbackReportPacket report, |
| 90 | Timestamp at_time); |
| 91 | std::vector<PacketReadyToSend> PaceAndPullSendPackets(Timestamp at_time); |
| 92 | void Update(NetworkControlUpdate update); |
| 93 | |
| 94 | private: |
| 95 | friend class SimulatedTimeClient; |
| 96 | NetworkNode* send_node_; |
| 97 | uint64_t send_receiver_id_; |
| 98 | PacerConfig pacer_config_; |
| 99 | DataSize max_in_flight_ = DataSize::Infinity(); |
| 100 | |
| 101 | std::deque<PendingPacket> packet_queue_; |
| 102 | std::vector<SentPacket> sent_packets_; |
| 103 | |
| 104 | Timestamp last_update_ = Timestamp::MinusInfinity(); |
| 105 | int64_t pacing_budget_ = 0; |
| 106 | int64_t next_sequence_number_ = 1; |
| 107 | int64_t next_feedback_seq_num_ = 1; |
| 108 | DataSize data_in_flight_ = DataSize::Zero(); |
| 109 | }; |
| 110 | |
| 111 | // SimulatedTimeClient emulates core parts of the behavior of WebRTC from the |
| 112 | // perspective of congestion controllers. This is intended for use in functional |
| 113 | // unit tests to ensure that congestion controllers behave in a reasonable way. |
| 114 | // It does not, however, completely simulate the actual behavior of WebRTC. For |
| 115 | // a more accurate simulation, use the real time only CallClient. |
| 116 | class SimulatedTimeClient : NetworkReceiverInterface { |
| 117 | public: |
| 118 | SimulatedTimeClient(std::string log_filename, |
| 119 | SimulatedTimeClientConfig config, |
| 120 | std::vector<PacketStreamConfig> stream_configs, |
| 121 | std::vector<NetworkNode*> send_link, |
| 122 | std::vector<NetworkNode*> return_link, |
| 123 | uint64_t send_receiver_id, |
| 124 | uint64_t return_receiver_id, |
| 125 | Timestamp at_time); |
| 126 | SimulatedTimeClient(const SimulatedTimeClient&) = delete; |
| 127 | ~SimulatedTimeClient(); |
| 128 | void Update(NetworkControlUpdate update); |
| 129 | void CongestionProcess(Timestamp at_time); |
| 130 | void PacerProcess(Timestamp at_time); |
| 131 | void ProcessFrames(Timestamp at_time); |
| 132 | TimeDelta GetNetworkControllerProcessInterval() const; |
| 133 | double target_rate_kbps() const; |
| 134 | |
| 135 | bool TryDeliverPacket(rtc::CopyOnWriteBuffer packet, |
| 136 | uint64_t receiver, |
| 137 | Timestamp at_time) override; |
| 138 | |
| 139 | private: |
| 140 | friend class Scenario; |
| 141 | LoggingNetworkControllerFactory network_controller_factory_; |
| 142 | std::unique_ptr<NetworkControllerInterface> congestion_controller_; |
| 143 | std::vector<NetworkNode*> send_link_; |
| 144 | std::vector<NetworkNode*> return_link_; |
| 145 | SimulatedSender sender_; |
| 146 | SimulatedFeedback feedback_; |
| 147 | DataRate target_rate_ = DataRate::Infinity(); |
| 148 | FILE* packet_log_ = nullptr; |
| 149 | |
| 150 | std::vector<std::unique_ptr<PacketStream>> packet_streams_; |
| 151 | }; |
| 152 | } // namespace test |
| 153 | } // namespace webrtc |
| 154 | |
| 155 | #endif // TEST_SCENARIO_SIMULATED_TIME_H_ |