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