stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_PACING_BITRATE_PROBER_H_ |
| 12 | #define MODULES_PACING_BITRATE_PROBER_H_ |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 17 | #include <queue> |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 18 | |
Jonas Olsson | 24923e8 | 2019-03-27 14:19:04 +0100 | [diff] [blame] | 19 | #include "api/transport/field_trial_based_config.h" |
Danil Chapovalov | db12856 | 2018-09-17 13:11:50 +0200 | [diff] [blame] | 20 | #include "api/transport/network_types.h" |
Jonas Olsson | 24923e8 | 2019-03-27 14:19:04 +0100 | [diff] [blame] | 21 | #include "rtc_base/experiments/field_trial_parser.h" |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
philipel | c3b3f7a | 2017-03-29 01:23:13 -0700 | [diff] [blame] | 24 | class RtcEventLog; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 25 | |
Jonas Olsson | 24923e8 | 2019-03-27 14:19:04 +0100 | [diff] [blame] | 26 | struct BitrateProberConfig { |
| 27 | explicit BitrateProberConfig(const WebRtcKeyValueConfig* key_value_config); |
| 28 | BitrateProberConfig(const BitrateProberConfig&) = default; |
| 29 | BitrateProberConfig& operator=(const BitrateProberConfig&) = default; |
| 30 | ~BitrateProberConfig() = default; |
| 31 | |
| 32 | // The minimum number probing packets used. |
| 33 | FieldTrialParameter<int> min_probe_packets_sent; |
| 34 | // A minimum interval between probes to allow scheduling to be feasible. |
| 35 | FieldTrialParameter<TimeDelta> min_probe_delta; |
| 36 | // The minimum probing duration. |
| 37 | FieldTrialParameter<TimeDelta> min_probe_duration; |
| 38 | // Maximum amount of time each probe can be delayed. Probe cluster is reset |
| 39 | // and retried from the start when this limit is reached. |
| 40 | FieldTrialParameter<TimeDelta> max_probe_delay; |
| 41 | }; |
| 42 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 43 | // Note that this class isn't thread-safe by itself and therefore relies |
| 44 | // on being protected by the caller. |
| 45 | class BitrateProber { |
| 46 | public: |
Jonas Olsson | 24923e8 | 2019-03-27 14:19:04 +0100 | [diff] [blame] | 47 | explicit BitrateProber(const WebRtcKeyValueConfig& field_trials); |
Mirko Bonadei | b471c90 | 2018-07-18 14:11:27 +0200 | [diff] [blame] | 48 | ~BitrateProber(); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 49 | |
| 50 | void SetEnabled(bool enable); |
| 51 | |
| 52 | // Returns true if the prober is in a probing session, i.e., it currently |
| 53 | // wants packets to be sent out according to the time returned by |
| 54 | // TimeUntilNextProbe(). |
| 55 | bool IsProbing() const; |
| 56 | |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 57 | // Initializes a new probing session if the prober is allowed to probe. Does |
| 58 | // not initialize the prober unless the packet size is large enough to probe |
| 59 | // with. |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 60 | void OnIncomingPacket(size_t packet_size); |
| 61 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 62 | // Create a cluster used to probe for |bitrate_bps| with |num_probes| number |
| 63 | // of probes. |
Erik Språng | b210eeb | 2019-11-05 11:21:48 +0100 | [diff] [blame] | 64 | void CreateProbeCluster(DataRate bitrate, Timestamp now, int cluster_id); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 65 | |
Erik Språng | b210eeb | 2019-11-05 11:21:48 +0100 | [diff] [blame] | 66 | // Returns the at which the next probe should be sent to get accurate probing. |
| 67 | // If probing is not desired at this time, Timestamp::PlusInfinity() will be |
| 68 | // returned. |
| 69 | Timestamp NextProbeTime(Timestamp now) const; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 70 | |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 71 | // Information about the current probing cluster. |
| 72 | PacedPacketInfo CurrentCluster() const; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 73 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 74 | // Returns the minimum number of bytes that the prober recommends for |
| 75 | // the next probe. |
| 76 | size_t RecommendedMinProbeSize() const; |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 77 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 78 | // Called to report to the prober that a probe has been sent. In case of |
| 79 | // multiple packets per probe, this call would be made at the end of sending |
| 80 | // the last packet in probe. |probe_size| is the total size of all packets |
| 81 | // in probe. |
Erik Språng | b210eeb | 2019-11-05 11:21:48 +0100 | [diff] [blame] | 82 | void ProbeSent(Timestamp now, size_t probe_size); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 85 | enum class ProbingState { |
| 86 | // Probing will not be triggered in this state at all times. |
| 87 | kDisabled, |
| 88 | // Probing is enabled and ready to trigger on the first packet arrival. |
| 89 | kInactive, |
| 90 | // Probe cluster is filled with the set of data rates to be probed and |
| 91 | // probes are being sent. |
| 92 | kActive, |
| 93 | // Probing is enabled, but currently suspended until an explicit trigger |
| 94 | // to start probing again. |
| 95 | kSuspended, |
| 96 | }; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 97 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 98 | // A probe cluster consists of a set of probes. Each probe in turn can be |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 99 | // divided into a number of packets to accommodate the MTU on the network. |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 100 | struct ProbeCluster { |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 101 | PacedPacketInfo pace_info; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 102 | |
| 103 | int sent_probes = 0; |
| 104 | int sent_bytes = 0; |
Erik Språng | b210eeb | 2019-11-05 11:21:48 +0100 | [diff] [blame] | 105 | Timestamp created_at = Timestamp::MinusInfinity(); |
| 106 | Timestamp started_at = Timestamp::MinusInfinity(); |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 107 | int retries = 0; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
Erik Språng | b210eeb | 2019-11-05 11:21:48 +0100 | [diff] [blame] | 110 | Timestamp CalculateNextProbeTime(const ProbeCluster& cluster) const; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 111 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 112 | ProbingState probing_state_; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 113 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 114 | // Probe bitrate per packet. These are used to compute the delta relative to |
| 115 | // the previous probe packet based on the size and time when that packet was |
| 116 | // sent. |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 117 | std::queue<ProbeCluster> clusters_; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 118 | |
| 119 | // Time the next probe should be sent when in kActive state. |
Erik Språng | b210eeb | 2019-11-05 11:21:48 +0100 | [diff] [blame] | 120 | Timestamp next_probe_time_; |
Jonas Olsson | 8f43384 | 2019-03-25 10:10:31 +0100 | [diff] [blame] | 121 | |
| 122 | int total_probe_count_; |
| 123 | int total_failed_probe_count_; |
Jonas Olsson | 24923e8 | 2019-03-27 14:19:04 +0100 | [diff] [blame] | 124 | |
| 125 | BitrateProberConfig config_; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 126 | }; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 127 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 128 | } // namespace webrtc |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 129 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 130 | #endif // MODULES_PACING_BITRATE_PROBER_H_ |