tor-browser

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

bitrate_prober.h (4887B)


      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 #ifndef MODULES_PACING_BITRATE_PROBER_H_
     12 #define MODULES_PACING_BITRATE_PROBER_H_
     13 
     14 #include <stddef.h>
     15 
     16 #include <optional>
     17 #include <queue>
     18 
     19 #include "api/field_trials_view.h"
     20 #include "api/transport/network_types.h"
     21 #include "api/units/data_size.h"
     22 #include "api/units/time_delta.h"
     23 #include "api/units/timestamp.h"
     24 #include "rtc_base/experiments/field_trial_parser.h"
     25 
     26 namespace webrtc {
     27 class RtcEventLog;
     28 
     29 struct BitrateProberConfig {
     30  explicit BitrateProberConfig(const FieldTrialsView* key_value_config);
     31  BitrateProberConfig(const BitrateProberConfig&) = default;
     32  BitrateProberConfig& operator=(const BitrateProberConfig&) = default;
     33  ~BitrateProberConfig() = default;
     34 
     35  // Maximum amount of time each probe can be delayed.
     36  FieldTrialParameter<TimeDelta> max_probe_delay;
     37  // This is used to start sending a probe after a large enough packet.
     38  // The min packet size is scaled with the bitrate we're probing at.
     39  // This defines the max min packet size, meaning that on high bitrates
     40  // a packet of at least this size is needed to trigger sending a probe.
     41  FieldTrialParameter<DataSize> min_packet_size;
     42 
     43  // If true, `min_packet_size` is ignored.
     44  bool allow_start_probing_immediately = false;
     45 };
     46 
     47 // Note that this class isn't thread-safe by itself and therefore relies
     48 // on being protected by the caller.
     49 class BitrateProber {
     50 public:
     51  explicit BitrateProber(const FieldTrialsView& field_trials);
     52  ~BitrateProber() = default;
     53 
     54  void SetEnabled(bool enable);
     55  void SetAllowProbeWithoutMediaPacket(bool allow);
     56 
     57  // Returns true if the prober is in a probing session, i.e., it currently
     58  // wants packets to be sent out according to the time returned by
     59  // TimeUntilNextProbe().
     60  bool is_probing() const { return probing_state_ == ProbingState::kActive; }
     61 
     62  // Initializes a new probing session if the prober is allowed to probe. Does
     63  // not initialize the prober unless the packet size is large enough to probe
     64  // with.
     65  void OnIncomingPacket(DataSize packet_size);
     66 
     67  // Create a cluster used to probe.
     68  void CreateProbeCluster(const ProbeClusterConfig& cluster_config);
     69  // Returns the time at which the next probe should be sent to get accurate
     70  // probing. If probing is not desired at this time, Timestamp::PlusInfinity()
     71  // will be returned.
     72  // TODO(bugs.webrtc.org/11780): Remove `now` argument when old mode is gone.
     73  Timestamp NextProbeTime(Timestamp now) const;
     74 
     75  // Information about the current probing cluster.
     76  std::optional<PacedPacketInfo> CurrentCluster(Timestamp now);
     77 
     78  // Returns the minimum number of bytes that the prober recommends for
     79  // the next probe, or zero if not probing. A probe can consist of multiple
     80  // packets that are sent back to back.
     81  DataSize RecommendedMinProbeSize() const;
     82 
     83  // Called to report to the prober that a probe has been sent. In case of
     84  // multiple packets per probe, this call would be made at the end of sending
     85  // the last packet in probe. `size` is the total size of all packets in probe.
     86  void ProbeSent(Timestamp now, DataSize size);
     87 
     88 private:
     89  enum class ProbingState {
     90    // Probing will not be triggered in this state at all times.
     91    kDisabled,
     92    // Probing is enabled and ready to trigger on the first packet arrival if
     93    // there is a probe cluster.
     94    kInactive,
     95    // Probe cluster is filled with the set of data rates to be probed and
     96    // probes are being sent.
     97    kActive,
     98  };
     99 
    100  // A probe cluster consists of a set of probes. Each probe in turn can be
    101  // divided into a number of packets to accommodate the MTU on the network.
    102  struct ProbeCluster {
    103    PacedPacketInfo pace_info;
    104 
    105    int sent_probes = 0;
    106    int sent_bytes = 0;
    107    TimeDelta min_probe_delta = TimeDelta::Zero();
    108    Timestamp requested_at = Timestamp::MinusInfinity();
    109    Timestamp started_at = Timestamp::MinusInfinity();
    110  };
    111 
    112  Timestamp CalculateNextProbeTime(const ProbeCluster& cluster) const;
    113 
    114  void MaybeSetActiveState(DataSize packet_size);
    115  bool ReadyToSetActiveState(DataSize packet_size) const;
    116 
    117  ProbingState probing_state_;
    118 
    119  // Probe bitrate per packet. These are used to compute the delta relative to
    120  // the previous probe packet based on the size and time when that packet was
    121  // sent.
    122  std::queue<ProbeCluster> clusters_;
    123 
    124  // Time the next probe should be sent when in kActive state.
    125  Timestamp next_probe_time_;
    126 
    127  BitrateProberConfig config_;
    128 };
    129 
    130 }  // namespace webrtc
    131 
    132 #endif  // MODULES_PACING_BITRATE_PROBER_H_