tor-browser

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

delay_constraints.cc (3631B)


      1 /*
      2 *  Copyright (c) 2024 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 "modules/audio_coding/neteq/delay_constraints.h"
     12 
     13 #include <algorithm>
     14 
     15 #include "rtc_base/logging.h"
     16 #include "rtc_base/numerics/safe_minmax.h"
     17 
     18 namespace webrtc {
     19 
     20 constexpr int kMinBaseMinimumDelayMs = 0;
     21 constexpr int kMaxBaseMinimumDelayMs = 10000;
     22 
     23 DelayConstraints::DelayConstraints(int max_packets_in_buffer,
     24                                   int base_minimum_delay_ms)
     25    : max_packets_in_buffer_(max_packets_in_buffer),
     26      base_minimum_delay_ms_(base_minimum_delay_ms),
     27      effective_minimum_delay_ms_(base_minimum_delay_ms),
     28      minimum_delay_ms_(0),
     29      maximum_delay_ms_(0) {}
     30 
     31 int DelayConstraints::Clamp(int delay_ms) const {
     32  delay_ms = std::max(delay_ms, effective_minimum_delay_ms_);
     33  if (maximum_delay_ms_ > 0) {
     34    delay_ms = std::min(delay_ms, maximum_delay_ms_);
     35  }
     36  if (packet_len_ms_ > 0) {
     37    // Limit to 75% of maximum buffer size.
     38    delay_ms =
     39        std::min(delay_ms, 3 * max_packets_in_buffer_ * packet_len_ms_ / 4);
     40  }
     41  return delay_ms;
     42 }
     43 
     44 bool DelayConstraints::SetPacketAudioLength(int length_ms) {
     45  if (length_ms <= 0) {
     46    RTC_LOG_F(LS_ERROR) << "length_ms = " << length_ms;
     47    return false;
     48  }
     49  packet_len_ms_ = length_ms;
     50  return true;
     51 }
     52 
     53 bool DelayConstraints::IsValidMinimumDelay(int delay_ms) const {
     54  return 0 <= delay_ms && delay_ms <= MinimumDelayUpperBound();
     55 }
     56 
     57 bool DelayConstraints::IsValidBaseMinimumDelay(int delay_ms) const {
     58  return kMinBaseMinimumDelayMs <= delay_ms &&
     59         delay_ms <= kMaxBaseMinimumDelayMs;
     60 }
     61 
     62 bool DelayConstraints::SetMinimumDelay(int delay_ms) {
     63  if (!IsValidMinimumDelay(delay_ms)) {
     64    return false;
     65  }
     66 
     67  minimum_delay_ms_ = delay_ms;
     68  UpdateEffectiveMinimumDelay();
     69  return true;
     70 }
     71 
     72 bool DelayConstraints::SetMaximumDelay(int delay_ms) {
     73  // If `delay_ms` is zero then it unsets the maximum delay and target level is
     74  // unconstrained by maximum delay.
     75  if (delay_ms != 0 && delay_ms < minimum_delay_ms_) {
     76    // Maximum delay shouldn't be less than minimum delay or less than a packet.
     77    return false;
     78  }
     79 
     80  maximum_delay_ms_ = delay_ms;
     81  UpdateEffectiveMinimumDelay();
     82  return true;
     83 }
     84 
     85 bool DelayConstraints::SetBaseMinimumDelay(int delay_ms) {
     86  if (!IsValidBaseMinimumDelay(delay_ms)) {
     87    return false;
     88  }
     89 
     90  base_minimum_delay_ms_ = delay_ms;
     91  UpdateEffectiveMinimumDelay();
     92  return true;
     93 }
     94 
     95 int DelayConstraints::GetBaseMinimumDelay() const {
     96  return base_minimum_delay_ms_;
     97 }
     98 
     99 void DelayConstraints::UpdateEffectiveMinimumDelay() {
    100  // Clamp `base_minimum_delay_ms_` into the range which can be effectively
    101  // used.
    102  const int base_minimum_delay_ms =
    103      SafeClamp(base_minimum_delay_ms_, 0, MinimumDelayUpperBound());
    104  effective_minimum_delay_ms_ =
    105      std::max(minimum_delay_ms_, base_minimum_delay_ms);
    106 }
    107 
    108 int DelayConstraints::MinimumDelayUpperBound() const {
    109  // Choose the lowest possible bound discarding 0 cases which mean the value
    110  // is not set and unconstrained.
    111  int q75 = max_packets_in_buffer_ * packet_len_ms_ * 3 / 4;
    112  q75 = q75 > 0 ? q75 : kMaxBaseMinimumDelayMs;
    113  const int maximum_delay_ms =
    114      maximum_delay_ms_ > 0 ? maximum_delay_ms_ : kMaxBaseMinimumDelayMs;
    115  return std::min(maximum_delay_ms, q75);
    116 }
    117 
    118 }  // namespace webrtc