2014-10-23 11:57:05 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 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/bitrate_prober.h"
|
|
|
|
|
|
2015-05-05 10:21:24 +02:00
|
|
|
#include <algorithm>
|
2014-10-23 11:57:05 +00:00
|
|
|
|
2016-05-06 17:06:14 +02:00
|
|
|
#include "webrtc/base/checks.h"
|
2015-12-04 16:13:05 +01:00
|
|
|
#include "webrtc/base/logging.h"
|
2015-11-16 11:12:24 +01:00
|
|
|
#include "webrtc/modules/pacing/paced_sender.h"
|
2014-10-23 11:57:05 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
2016-08-02 12:57:37 -07:00
|
|
|
|
|
|
|
|
// Inactivity threshold above which probing is restarted.
|
|
|
|
|
constexpr int kInactivityThresholdMs = 5000;
|
|
|
|
|
|
2016-05-04 17:12:51 +02:00
|
|
|
int ComputeDeltaFromBitrate(size_t packet_size, uint32_t bitrate_bps) {
|
2016-08-15 11:51:06 -07:00
|
|
|
RTC_CHECK_GT(bitrate_bps, 0u);
|
2014-10-23 11:57:05 +00:00
|
|
|
// Compute the time delta needed to send packet_size bytes at bitrate_bps
|
|
|
|
|
// bps. Result is in milliseconds.
|
2016-08-02 12:57:37 -07:00
|
|
|
return static_cast<int>((1000ll * packet_size * 8) / bitrate_bps);
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
BitrateProber::BitrateProber()
|
2016-08-02 12:57:37 -07:00
|
|
|
: probing_state_(ProbingState::kDisabled),
|
|
|
|
|
packet_size_last_sent_(0),
|
|
|
|
|
time_last_probe_sent_ms_(-1),
|
|
|
|
|
next_cluster_id_(0) {
|
|
|
|
|
SetEnabled(true);
|
|
|
|
|
}
|
2014-10-23 11:57:05 +00:00
|
|
|
|
|
|
|
|
void BitrateProber::SetEnabled(bool enable) {
|
|
|
|
|
if (enable) {
|
2016-08-02 12:57:37 -07:00
|
|
|
if (probing_state_ == ProbingState::kDisabled) {
|
|
|
|
|
probing_state_ = ProbingState::kInactive;
|
|
|
|
|
LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-08-02 12:57:37 -07:00
|
|
|
probing_state_ = ProbingState::kDisabled;
|
|
|
|
|
LOG(LS_INFO) << "Bandwidth probing disabled";
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BitrateProber::IsProbing() const {
|
2016-08-02 12:57:37 -07:00
|
|
|
return probing_state_ == ProbingState::kActive;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-15 11:51:06 -07:00
|
|
|
void BitrateProber::OnIncomingPacket(size_t packet_size) {
|
2016-02-16 16:23:08 +01:00
|
|
|
// Don't initialize probing unless we have something large enough to start
|
|
|
|
|
// probing.
|
2016-08-15 11:51:06 -07:00
|
|
|
if (probing_state_ == ProbingState::kInactive &&
|
|
|
|
|
packet_size >= PacedSender::kMinProbePacketSize) {
|
|
|
|
|
probing_state_ = ProbingState::kActive;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
2016-08-15 11:51:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BitrateProber::ProbeAtBitrate(uint32_t bitrate_bps, int num_packets) {
|
|
|
|
|
ProbeCluster cluster;
|
|
|
|
|
cluster.max_probe_packets = num_packets;
|
|
|
|
|
cluster.probe_bitrate_bps = bitrate_bps;
|
|
|
|
|
cluster.id = next_cluster_id_++;
|
|
|
|
|
clusters_.push(cluster);
|
|
|
|
|
LOG(LS_INFO) << "Probe cluster (bitrate:packets): ("
|
|
|
|
|
<< cluster.probe_bitrate_bps << ":" << cluster.max_probe_packets
|
|
|
|
|
<< ") ";
|
|
|
|
|
if (probing_state_ != ProbingState::kActive)
|
|
|
|
|
probing_state_ = ProbingState::kInactive;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-02 12:57:37 -07:00
|
|
|
void BitrateProber::ResetState() {
|
|
|
|
|
time_last_probe_sent_ms_ = -1;
|
|
|
|
|
packet_size_last_sent_ = 0;
|
2016-08-15 11:51:06 -07:00
|
|
|
|
|
|
|
|
// Recreate all probing clusters.
|
|
|
|
|
std::queue<ProbeCluster> clusters;
|
|
|
|
|
clusters.swap(clusters_);
|
|
|
|
|
while (!clusters.empty()) {
|
|
|
|
|
ProbeAtBitrate(clusters.front().probe_bitrate_bps,
|
|
|
|
|
clusters.front().max_probe_packets);
|
|
|
|
|
clusters.pop();
|
|
|
|
|
}
|
2016-08-02 12:57:37 -07:00
|
|
|
// If its enabled, reset to inactive.
|
|
|
|
|
if (probing_state_ != ProbingState::kDisabled)
|
|
|
|
|
probing_state_ = ProbingState::kInactive;
|
|
|
|
|
}
|
2016-05-06 17:06:14 +02:00
|
|
|
|
2016-08-02 12:57:37 -07:00
|
|
|
int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
|
|
|
|
|
// Probing is not active or probing is already complete.
|
|
|
|
|
if (probing_state_ != ProbingState::kActive || clusters_.empty())
|
2015-02-16 15:47:51 +00:00
|
|
|
return -1;
|
2016-08-02 12:57:37 -07:00
|
|
|
// time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent.
|
|
|
|
|
int64_t elapsed_time_ms;
|
|
|
|
|
if (time_last_probe_sent_ms_ == -1) {
|
|
|
|
|
elapsed_time_ms = 0;
|
|
|
|
|
} else {
|
|
|
|
|
elapsed_time_ms = now_ms - time_last_probe_sent_ms_;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
2016-08-02 12:57:37 -07:00
|
|
|
// If no probes have been sent for a while, abort current probing and
|
|
|
|
|
// reset.
|
|
|
|
|
if (elapsed_time_ms > kInactivityThresholdMs) {
|
|
|
|
|
ResetState();
|
2016-02-16 16:23:08 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
2014-10-23 11:57:05 +00:00
|
|
|
// We will send the first probe packet immediately if no packet has been
|
|
|
|
|
// sent before.
|
|
|
|
|
int time_until_probe_ms = 0;
|
2016-08-02 12:57:37 -07:00
|
|
|
if (packet_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) {
|
2016-05-06 17:06:14 +02:00
|
|
|
int next_delta_ms = ComputeDeltaFromBitrate(
|
2016-08-02 12:57:37 -07:00
|
|
|
packet_size_last_sent_, clusters_.front().probe_bitrate_bps);
|
2014-10-23 11:57:05 +00:00
|
|
|
time_until_probe_ms = next_delta_ms - elapsed_time_ms;
|
|
|
|
|
// There is no point in trying to probe with less than 1 ms between packets
|
|
|
|
|
// as it essentially means trying to probe at infinite bandwidth.
|
|
|
|
|
const int kMinProbeDeltaMs = 1;
|
|
|
|
|
// If we have waited more than 3 ms for a new packet to probe with we will
|
|
|
|
|
// consider this probing session over.
|
|
|
|
|
const int kMaxProbeDelayMs = 3;
|
|
|
|
|
if (next_delta_ms < kMinProbeDeltaMs ||
|
|
|
|
|
time_until_probe_ms < -kMaxProbeDelayMs) {
|
2016-08-02 12:57:37 -07:00
|
|
|
probing_state_ = ProbingState::kSuspended;
|
|
|
|
|
LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend";
|
2014-10-23 11:57:05 +00:00
|
|
|
time_until_probe_ms = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-05 10:21:24 +02:00
|
|
|
return std::max(time_until_probe_ms, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 17:06:14 +02:00
|
|
|
int BitrateProber::CurrentClusterId() const {
|
|
|
|
|
RTC_DCHECK(!clusters_.empty());
|
2016-08-02 12:57:37 -07:00
|
|
|
RTC_DCHECK(ProbingState::kActive == probing_state_);
|
2016-05-06 17:06:14 +02:00
|
|
|
return clusters_.front().id;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-05 10:21:24 +02:00
|
|
|
size_t BitrateProber::RecommendedPacketSize() const {
|
2016-08-02 12:57:37 -07:00
|
|
|
return packet_size_last_sent_;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) {
|
|
|
|
|
assert(packet_size > 0);
|
2016-02-16 16:23:08 +01:00
|
|
|
if (packet_size < PacedSender::kMinProbePacketSize)
|
|
|
|
|
return;
|
2016-08-02 12:57:37 -07:00
|
|
|
packet_size_last_sent_ = packet_size;
|
|
|
|
|
if (probing_state_ != ProbingState::kActive)
|
2014-10-23 11:57:05 +00:00
|
|
|
return;
|
2016-08-02 12:57:37 -07:00
|
|
|
time_last_probe_sent_ms_ = now_ms;
|
2016-05-06 17:06:14 +02:00
|
|
|
if (!clusters_.empty()) {
|
|
|
|
|
ProbeCluster* cluster = &clusters_.front();
|
|
|
|
|
++cluster->sent_probe_packets;
|
|
|
|
|
if (cluster->sent_probe_packets == cluster->max_probe_packets)
|
|
|
|
|
clusters_.pop();
|
2016-06-03 01:41:45 -07:00
|
|
|
if (clusters_.empty())
|
2016-08-02 12:57:37 -07:00
|
|
|
probing_state_ = ProbingState::kSuspended;
|
2016-05-06 17:06:14 +02:00
|
|
|
}
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
} // namespace webrtc
|