1 /* 2 * Copyright (c) 2015 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 #ifndef WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_PACKET_SENDER_H_ 12 #define WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_PACKET_SENDER_H_ 13 14 #include <list> 15 #include <limits> 16 #include <set> 17 #include <string> 18 19 #include "webrtc/base/constructormagic.h" 20 #include "webrtc/base/scoped_ptr.h" 21 #include "webrtc/modules/include/module.h" 22 #include "webrtc/modules/remote_bitrate_estimator/test/bwe.h" 23 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.h" 24 25 namespace webrtc { 26 namespace testing { 27 namespace bwe { 28 29 class MetricRecorder; 30 31 class PacketSender : public PacketProcessor { 32 public: PacketSender(PacketProcessorListener * listener,int flow_id)33 PacketSender(PacketProcessorListener* listener, int flow_id) 34 : PacketProcessor(listener, flow_id, kSender), 35 running_(true), 36 // For Packet::send_time_us() to be comparable with timestamps from 37 // clock_, the clock of the PacketSender and the Source must be aligned. 38 // We assume that both start at time 0. 39 clock_(0), 40 metric_recorder_(nullptr) {} ~PacketSender()41 virtual ~PacketSender() {} 42 // Call GiveFeedback() with the returned interval in milliseconds, provided 43 // there is a new estimate available. 44 // Note that changing the feedback interval affects the timing of when the 45 // output of the estimators is sampled and therefore the baseline files may 46 // have to be regenerated. 47 virtual int GetFeedbackIntervalMs() const = 0; 48 void SetSenderTimestamps(Packets* in_out); 49 TargetBitrateKbps()50 virtual uint32_t TargetBitrateKbps() { return 0; } 51 52 virtual void Pause(); 53 virtual void Resume(int64_t paused_time_ms); 54 55 void set_metric_recorder(MetricRecorder* metric_recorder); 56 virtual void RecordBitrate(); 57 58 protected: 59 bool running_; // Initialized by default as true. 60 SimulatedClock clock_; 61 62 private: 63 MetricRecorder* metric_recorder_; 64 }; 65 66 class VideoSender : public PacketSender, public BitrateObserver { 67 public: 68 VideoSender(PacketProcessorListener* listener, 69 VideoSource* source, 70 BandwidthEstimatorType estimator); 71 virtual ~VideoSender(); 72 73 int GetFeedbackIntervalMs() const override; 74 void RunFor(int64_t time_ms, Packets* in_out) override; 75 source()76 virtual VideoSource* source() const { return source_; } 77 78 uint32_t TargetBitrateKbps() override; 79 80 // Implements BitrateObserver. 81 void OnNetworkChanged(uint32_t target_bitrate_bps, 82 uint8_t fraction_lost, 83 int64_t rtt) override; 84 85 void Pause() override; 86 void Resume(int64_t paused_time_ms) override; 87 88 protected: 89 void ProcessFeedbackAndGeneratePackets(int64_t time_ms, 90 std::list<FeedbackPacket*>* feedbacks, 91 Packets* generated); 92 93 VideoSource* source_; 94 rtc::scoped_ptr<BweSender> bwe_; 95 int64_t start_of_run_ms_; 96 std::list<Module*> modules_; 97 98 private: 99 uint32_t previous_sending_bitrate_; 100 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSender); 101 }; 102 103 class PacedVideoSender : public VideoSender, public PacedSender::Callback { 104 public: 105 PacedVideoSender(PacketProcessorListener* listener, 106 VideoSource* source, 107 BandwidthEstimatorType estimator); 108 virtual ~PacedVideoSender(); 109 110 void RunFor(int64_t time_ms, Packets* in_out) override; 111 112 // Implements PacedSender::Callback. 113 bool TimeToSendPacket(uint32_t ssrc, 114 uint16_t sequence_number, 115 int64_t capture_time_ms, 116 bool retransmission) override; 117 size_t TimeToSendPadding(size_t bytes) override; 118 119 // Implements BitrateObserver. 120 void OnNetworkChanged(uint32_t target_bitrate_bps, 121 uint8_t fraction_lost, 122 int64_t rtt) override; 123 124 private: 125 int64_t TimeUntilNextProcess(const std::list<Module*>& modules); 126 void CallProcess(const std::list<Module*>& modules); 127 void QueuePackets(Packets* batch, int64_t end_of_batch_time_us); 128 129 PacedSender pacer_; 130 Packets queue_; 131 Packets pacer_queue_; 132 133 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacedVideoSender); 134 }; 135 136 class TcpSender : public PacketSender { 137 public: 138 TcpSender(PacketProcessorListener* listener, int flow_id, int64_t offset_ms); 139 TcpSender(PacketProcessorListener* listener, 140 int flow_id, 141 int64_t offset_ms, 142 int send_limit_bytes); ~TcpSender()143 virtual ~TcpSender() {} 144 145 void RunFor(int64_t time_ms, Packets* in_out) override; GetFeedbackIntervalMs()146 int GetFeedbackIntervalMs() const override { return 10; } 147 148 uint32_t TargetBitrateKbps() override; 149 150 private: 151 struct InFlight { 152 public: InFlightInFlight153 explicit InFlight(const MediaPacket& packet) 154 : sequence_number(packet.header().sequenceNumber), 155 time_ms(packet.send_time_ms()) {} 156 InFlightInFlight157 InFlight(uint16_t seq_num, int64_t now_ms) 158 : sequence_number(seq_num), time_ms(now_ms) {} 159 160 bool operator<(const InFlight& rhs) const { 161 return sequence_number < rhs.sequence_number; 162 } 163 164 uint16_t sequence_number; // Sequence number of a packet in flight, or a 165 // packet which has just been acked. 166 int64_t time_ms; // Time of when the packet left the sender, or when the 167 // ack was received. 168 }; 169 170 void SendPackets(Packets* in_out); 171 void UpdateCongestionControl(const FeedbackPacket* fb); 172 int TriggerTimeouts(); 173 void HandleLoss(); 174 Packets GeneratePackets(size_t num_packets); 175 void UpdateSendBitrateEstimate(size_t num_packets); 176 177 float cwnd_; 178 int ssthresh_; 179 std::set<InFlight> in_flight_; 180 bool ack_received_; 181 uint16_t last_acked_seq_num_; 182 uint16_t next_sequence_number_; 183 int64_t offset_ms_; 184 int64_t last_reduction_time_ms_; 185 int64_t last_rtt_ms_; 186 int total_sent_bytes_; 187 int send_limit_bytes_; // Initialized by default as kNoLimit. 188 int64_t last_generated_packets_ms_; 189 size_t num_recent_sent_packets_; 190 uint32_t bitrate_kbps_; 191 }; 192 } // namespace bwe 193 } // namespace testing 194 } // namespace webrtc 195 #endif // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_PACKET_SENDER_H_ 196