tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rtp_bitrate_configurator.cc (4543B)


      1 /*
      2 *  Copyright (c) 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 
     11 #include "call/rtp_bitrate_configurator.h"
     12 
     13 #include <algorithm>
     14 #include <optional>
     15 
     16 #include "api/transport/bitrate_settings.h"
     17 #include "api/units/data_rate.h"
     18 #include "rtc_base/checks.h"
     19 
     20 namespace {
     21 
     22 // Returns its smallest positive argument. If neither argument is positive,
     23 // returns an arbitrary nonpositive value.
     24 int MinPositive(int a, int b) {
     25  if (a <= 0) {
     26    return b;
     27  }
     28  if (b <= 0) {
     29    return a;
     30  }
     31  return std::min(a, b);
     32 }
     33 
     34 }  // namespace
     35 
     36 namespace webrtc {
     37 RtpBitrateConfigurator::RtpBitrateConfigurator(
     38    const BitrateConstraints& bitrate_config)
     39    : bitrate_config_(bitrate_config), base_bitrate_config_(bitrate_config) {
     40  RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
     41  RTC_DCHECK_GE(bitrate_config.start_bitrate_bps,
     42                bitrate_config.min_bitrate_bps);
     43  if (bitrate_config.max_bitrate_bps != -1) {
     44    RTC_DCHECK_GE(bitrate_config.max_bitrate_bps,
     45                  bitrate_config.start_bitrate_bps);
     46  }
     47 }
     48 
     49 RtpBitrateConfigurator::~RtpBitrateConfigurator() = default;
     50 
     51 BitrateConstraints RtpBitrateConfigurator::GetConfig() const {
     52  return bitrate_config_;
     53 }
     54 
     55 std::optional<BitrateConstraints>
     56 RtpBitrateConfigurator::UpdateWithSdpParameters(
     57    const BitrateConstraints& bitrate_config) {
     58  RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
     59  RTC_DCHECK_NE(bitrate_config.start_bitrate_bps, 0);
     60  if (bitrate_config.max_bitrate_bps != -1) {
     61    RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
     62  }
     63 
     64  std::optional<int> new_start;
     65  // Only update the "start" bitrate if it's set, and different from the old
     66  // value. In practice, this value comes from the x-google-start-bitrate codec
     67  // parameter in SDP, and setting the same remote description twice shouldn't
     68  // restart bandwidth estimation.
     69  if (bitrate_config.start_bitrate_bps != -1 &&
     70      bitrate_config.start_bitrate_bps !=
     71          base_bitrate_config_.start_bitrate_bps) {
     72    new_start.emplace(bitrate_config.start_bitrate_bps);
     73  }
     74  base_bitrate_config_ = bitrate_config;
     75  return UpdateConstraints(new_start);
     76 }
     77 
     78 std::optional<BitrateConstraints>
     79 RtpBitrateConfigurator::UpdateWithClientPreferences(
     80    const BitrateSettings& bitrate_mask) {
     81  bitrate_config_mask_ = bitrate_mask;
     82  return UpdateConstraints(bitrate_mask.start_bitrate_bps);
     83 }
     84 
     85 // Relay cap can change only max bitrate.
     86 std::optional<BitrateConstraints> RtpBitrateConfigurator::UpdateWithRelayCap(
     87    DataRate cap) {
     88  if (cap.IsFinite()) {
     89    RTC_DCHECK(!cap.IsZero());
     90  }
     91  max_bitrate_over_relay_ = cap;
     92  return UpdateConstraints(std::nullopt);
     93 }
     94 
     95 std::optional<BitrateConstraints> RtpBitrateConfigurator::UpdateConstraints(
     96    const std::optional<int>& new_start) {
     97  BitrateConstraints updated;
     98  updated.min_bitrate_bps =
     99      std::max(bitrate_config_mask_.min_bitrate_bps.value_or(0),
    100               base_bitrate_config_.min_bitrate_bps);
    101 
    102  updated.max_bitrate_bps =
    103      MinPositive(bitrate_config_mask_.max_bitrate_bps.value_or(-1),
    104                  base_bitrate_config_.max_bitrate_bps);
    105  updated.max_bitrate_bps =
    106      MinPositive(updated.max_bitrate_bps, max_bitrate_over_relay_.bps_or(-1));
    107 
    108  // If the combined min ends up greater than the combined max, the max takes
    109  // priority.
    110  if (updated.max_bitrate_bps != -1 &&
    111      updated.min_bitrate_bps > updated.max_bitrate_bps) {
    112    updated.min_bitrate_bps = updated.max_bitrate_bps;
    113  }
    114 
    115  // If there is nothing to update (min/max unchanged, no new bandwidth
    116  // estimation start value), return early.
    117  if (updated.min_bitrate_bps == bitrate_config_.min_bitrate_bps &&
    118      updated.max_bitrate_bps == bitrate_config_.max_bitrate_bps &&
    119      !new_start) {
    120    return std::nullopt;
    121  }
    122 
    123  if (new_start) {
    124    // Clamp start by min and max.
    125    updated.start_bitrate_bps = MinPositive(
    126        std::max(*new_start, updated.min_bitrate_bps), updated.max_bitrate_bps);
    127  } else {
    128    updated.start_bitrate_bps = -1;
    129  }
    130  BitrateConstraints config_to_return = updated;
    131  if (!new_start) {
    132    updated.start_bitrate_bps = bitrate_config_.start_bitrate_bps;
    133  }
    134  bitrate_config_ = updated;
    135  return config_to_return;
    136 }
    137 
    138 }  // namespace webrtc