Home | History | Annotate | Download | only in pacing
      1 /*
      2  *  Copyright (c) 2014 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/modules/pacing/bitrate_prober.h"
     12 
     13 #include <assert.h>
     14 #include <algorithm>
     15 #include <limits>
     16 #include <sstream>
     17 
     18 #include "webrtc/base/logging.h"
     19 #include "webrtc/modules/pacing/paced_sender.h"
     20 
     21 namespace webrtc {
     22 
     23 namespace {
     24 int ComputeDeltaFromBitrate(size_t packet_size, int bitrate_bps) {
     25   assert(bitrate_bps > 0);
     26   // Compute the time delta needed to send packet_size bytes at bitrate_bps
     27   // bps. Result is in milliseconds.
     28   return static_cast<int>(1000ll * static_cast<int64_t>(packet_size) * 8ll /
     29       bitrate_bps);
     30 }
     31 }  // namespace
     32 
     33 BitrateProber::BitrateProber()
     34     : probing_state_(kDisabled),
     35       packet_size_last_send_(0),
     36       time_last_send_ms_(-1) {
     37 }
     38 
     39 void BitrateProber::SetEnabled(bool enable) {
     40   if (enable) {
     41     if (probing_state_ == kDisabled) {
     42       probing_state_ = kAllowedToProbe;
     43       LOG(LS_INFO) << "Initial bandwidth probing enabled";
     44     }
     45   } else {
     46     probing_state_ = kDisabled;
     47     LOG(LS_INFO) << "Initial bandwidth probing disabled";
     48   }
     49 }
     50 
     51 bool BitrateProber::IsProbing() const {
     52   return probing_state_ == kProbing;
     53 }
     54 
     55 void BitrateProber::MaybeInitializeProbe(int bitrate_bps) {
     56   if (probing_state_ != kAllowedToProbe)
     57     return;
     58   probe_bitrates_.clear();
     59   // Max number of packets used for probing.
     60   const int kMaxNumProbes = 2;
     61   const int kPacketsPerProbe = 5;
     62   const float kProbeBitrateMultipliers[kMaxNumProbes] = {3, 6};
     63   int bitrates_bps[kMaxNumProbes];
     64   std::stringstream bitrate_log;
     65   bitrate_log << "Start probing for bandwidth, bitrates:";
     66   for (int i = 0; i < kMaxNumProbes; ++i) {
     67     bitrates_bps[i] = kProbeBitrateMultipliers[i] * bitrate_bps;
     68     bitrate_log << " " << bitrates_bps[i];
     69     // We need one extra to get 5 deltas for the first probe.
     70     if (i == 0)
     71       probe_bitrates_.push_back(bitrates_bps[i]);
     72     for (int j = 0; j < kPacketsPerProbe; ++j)
     73       probe_bitrates_.push_back(bitrates_bps[i]);
     74   }
     75   bitrate_log << ", num packets: " << probe_bitrates_.size();
     76   LOG(LS_INFO) << bitrate_log.str().c_str();
     77   probing_state_ = kProbing;
     78 }
     79 
     80 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
     81   if (probing_state_ != kDisabled && probe_bitrates_.empty()) {
     82     probing_state_ = kWait;
     83   }
     84   if (probe_bitrates_.empty()) {
     85     // No probe started, or waiting for next probe.
     86     return -1;
     87   }
     88   int64_t elapsed_time_ms = now_ms - time_last_send_ms_;
     89   // We will send the first probe packet immediately if no packet has been
     90   // sent before.
     91   int time_until_probe_ms = 0;
     92   if (packet_size_last_send_ > PacedSender::kMinProbePacketSize &&
     93       probing_state_ == kProbing) {
     94     int next_delta_ms = ComputeDeltaFromBitrate(packet_size_last_send_,
     95                                                 probe_bitrates_.front());
     96     time_until_probe_ms = next_delta_ms - elapsed_time_ms;
     97     // There is no point in trying to probe with less than 1 ms between packets
     98     // as it essentially means trying to probe at infinite bandwidth.
     99     const int kMinProbeDeltaMs = 1;
    100     // If we have waited more than 3 ms for a new packet to probe with we will
    101     // consider this probing session over.
    102     const int kMaxProbeDelayMs = 3;
    103     if (next_delta_ms < kMinProbeDeltaMs ||
    104         time_until_probe_ms < -kMaxProbeDelayMs) {
    105       // We currently disable probing after the first probe, as we only want
    106       // to probe at the beginning of a connection. We should set this to
    107       // kWait if we later want to probe periodically.
    108       probing_state_ = kWait;
    109       LOG(LS_INFO) << "Next delta too small, stop probing.";
    110       time_until_probe_ms = 0;
    111     }
    112   }
    113   return std::max(time_until_probe_ms, 0);
    114 }
    115 
    116 size_t BitrateProber::RecommendedPacketSize() const {
    117   return packet_size_last_send_;
    118 }
    119 
    120 void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) {
    121   assert(packet_size > 0);
    122   packet_size_last_send_ = packet_size;
    123   time_last_send_ms_ = now_ms;
    124   if (probing_state_ != kProbing)
    125     return;
    126   if (!probe_bitrates_.empty())
    127     probe_bitrates_.pop_front();
    128 }
    129 }  // namespace webrtc
    130