| Index: webrtc/modules/pacing/bbr_paced_sender.cc
|
| diff --git a/webrtc/modules/pacing/bbr_paced_sender.cc b/webrtc/modules/pacing/bbr_paced_sender.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..fa1b25ed4ceaf89ab0124c0255100ff1c030b599
|
| --- /dev/null
|
| +++ b/webrtc/modules/pacing/bbr_paced_sender.cc
|
| @@ -0,0 +1,166 @@
|
| +/*
|
| + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license
|
| + * that can be found in the LICENSE file in the root of the source
|
| + * tree. An additional intellectual property rights grant can be found
|
| + * in the file PATENTS. All contributing project authors may
|
| + * be found in the AUTHORS file in the root of the source tree.
|
| + */
|
| +
|
| +#include "webrtc/modules/pacing/bbr_paced_sender.h"
|
| +
|
| +#include <algorithm>
|
| +#include <queue>
|
| +#include <set>
|
| +#include <vector>
|
| +
|
| +#include "webrtc/modules/pacing/paced_sender.h"
|
| +#include "webrtc/system_wrappers/include/clock.h"
|
| +
|
| +namespace webrtc {
|
| +namespace bbr_paced_sender {
|
| +struct Packet {
|
| + Packet(RtpPacketSender::Priority priority,
|
| + uint32_t ssrc,
|
| + uint16_t seq_number,
|
| + int64_t capture_time_ms,
|
| + int64_t enqueue_time_ms,
|
| + size_t size_in_bytes,
|
| + bool retransmission)
|
| + : priority(priority),
|
| + ssrc(ssrc),
|
| + sequence_number(seq_number),
|
| + capture_time_ms(capture_time_ms),
|
| + enqueue_time_ms(enqueue_time_ms),
|
| + size_in_bytes(size_in_bytes),
|
| + retransmission(retransmission) {}
|
| + RtpPacketSender::Priority priority;
|
| + uint32_t ssrc;
|
| + uint16_t sequence_number;
|
| + int64_t capture_time_ms;
|
| + int64_t enqueue_time_ms;
|
| + size_t size_in_bytes;
|
| + bool retransmission;
|
| +};
|
| +} // namespace bbr_paced_sender
|
| +BbrPacedSender::BbrPacedSender(const Clock* clock,
|
| + PacedSender::PacketSender* packet_sender,
|
| + RtcEventLog* event_log)
|
| + : clock_(clock),
|
| + packet_sender_(packet_sender),
|
| + estimated_bitrate_bps_(100000),
|
| + min_send_bitrate_kbps_(0),
|
| + pacing_bitrate_kbps_(0),
|
| + time_last_update_us_(clock->TimeInMicroseconds()),
|
| + time_last_update_ms_(clock->TimeInMilliseconds()),
|
| + next_packet_send_time_(clock_->TimeInMilliseconds()),
|
| + extra_time_(0.0f),
|
| + packets_(),
|
| + data_inflight_(0),
|
| + // pacing_budget_(0),
|
| + congestion_window_(6000),
|
| + in_probe_rtt_(false) {}
|
| +BbrPacedSender::~BbrPacedSender() {}
|
| +
|
| +void BbrPacedSender::SetEstimatedBitrateAndCongestionWindow(
|
| + uint32_t bitrate_bps,
|
| + bool in_probe_rtt,
|
| + uint64_t congestion_window) {
|
| + estimated_bitrate_bps_ = bitrate_bps;
|
| + congestion_window_ = congestion_window;
|
| + in_probe_rtt_ = in_probe_rtt;
|
| +}
|
| +
|
| +void BbrPacedSender::SetMinBitrate(int min_send_bitrate_bps) {
|
| + min_send_bitrate_kbps_ = min_send_bitrate_bps / 1000;
|
| + pacing_bitrate_kbps_ =
|
| + std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000);
|
| +}
|
| +
|
| +void BbrPacedSender::InsertPacket(RtpPacketSender::Priority priority,
|
| + uint32_t ssrc,
|
| + uint16_t sequence_number,
|
| + int64_t capture_time_ms,
|
| + size_t bytes,
|
| + bool retransmission) {
|
| + int64_t now_ms = clock_->TimeInMilliseconds();
|
| + if (capture_time_ms < 0)
|
| + capture_time_ms = now_ms;
|
| + packets_.push_back(new bbr_paced_sender::Packet(
|
| + priority, ssrc, sequence_number, capture_time_ms, now_ms, bytes,
|
| + retransmission));
|
| +}
|
| +
|
| +int64_t BbrPacedSender::TimeUntilNextProcess() {
|
| + // Once errors absolute value hits 2 milliseconds, add compensating term to
|
| + // the |next_packet_send_time_|, so that we can send packet earlier or later,
|
| + // depending on the error.
|
| + extra_time_ = std::min(extra_time_, 2.0f);
|
| + if (extra_time_ < -1.9f)
|
| + extra_time_ = -2.0f;
|
| + int64_t result =
|
| + std::max<int64_t>(next_packet_send_time_ + time_last_update_ms_ -
|
| + clock_->TimeInMilliseconds(),
|
| + 0);
|
| + if (extra_time_ == 2.0f || extra_time_ == -2.0f) {
|
| + next_packet_send_time_ -= extra_time_;
|
| + result = std::max<int64_t>(next_packet_send_time_ + time_last_update_ms_ -
|
| + clock_->TimeInMilliseconds(),
|
| + 0);
|
| + extra_time_ = 0;
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +void BbrPacedSender::OnBytesAcked(size_t bytes) {
|
| + assert(data_inflight_ >= bytes);
|
| + data_inflight_ -= bytes;
|
| +}
|
| +
|
| +void BbrPacedSender::Process() {
|
| + pacing_bitrate_kbps_ =
|
| + std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000);
|
| + // If we have nothing to send, try sending again in 1 millisecond.
|
| + if (packets_.empty()) {
|
| + next_packet_send_time_ = 1;
|
| + return;
|
| + }
|
| + // If congestion window doesn't allow sending, try again in 1 millisecond.
|
| + if (packets_.front()->size_in_bytes + data_inflight_ > congestion_window_) {
|
| + next_packet_send_time_ = 1;
|
| + return;
|
| + }
|
| + bool sent = TryToSendPacket(packets_.front());
|
| + if (sent) {
|
| + data_inflight_ += packets_.front()->size_in_bytes;
|
| + delete packets_.front();
|
| + packets_.pop_front();
|
| + time_last_update_ms_ = clock_->TimeInMilliseconds();
|
| + if (!packets_.empty()) {
|
| + // Calculate in what time we should send current packet.
|
| + next_packet_send_time_ = (packets_.front()->size_in_bytes * 8000 +
|
| + estimated_bitrate_bps_ / 2) /
|
| + estimated_bitrate_bps_;
|
| + // As rounding errors may happen, |extra_time_| could be positive or
|
| + // negative depending on packet was sent earlier or later, after it hits
|
| + // certain threshold we will send a packet earlier or later depending on
|
| + // error we had so far.
|
| + extra_time_ +=
|
| + (next_packet_send_time_ - packets_.front()->size_in_bytes * 8000.0f /
|
| + estimated_bitrate_bps_ * 1.0f);
|
| + } else {
|
| + // If sending was unsuccessful try again in 1 millisecond.
|
| + next_packet_send_time_ = 1;
|
| + }
|
| + }
|
| +}
|
| +
|
| +bool BbrPacedSender::TryToSendPacket(bbr_paced_sender::Packet* packet) {
|
| + PacedPacketInfo pacing_info;
|
| + return packet_sender_->TimeToSendPacket(packet->ssrc, packet->sequence_number,
|
| + packet->capture_time_ms,
|
| + packet->retransmission, pacing_info);
|
| +}
|
| +
|
| +} // namespace webrtc
|
|
|