blob: ec234e8f5fca0c274caa55efee2c1131610bb566 [file] [log] [blame]
stefan@webrtc.org82462aa2014-10-23 11:57:05 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_PACING_BITRATE_PROBER_H_
12#define MODULES_PACING_BITRATE_PROBER_H_
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
philipeldd324862016-05-06 17:06:14 +020017#include <queue>
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000018
Jonas Olsson24923e82019-03-27 14:19:04 +010019#include "api/transport/field_trial_based_config.h"
Danil Chapovalovdb128562018-09-17 13:11:50 +020020#include "api/transport/network_types.h"
Jonas Olsson24923e82019-03-27 14:19:04 +010021#include "rtc_base/experiments/field_trial_parser.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000022
23namespace webrtc {
philipelc3b3f7a2017-03-29 01:23:13 -070024class RtcEventLog;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000025
Jonas Olsson24923e82019-03-27 14:19:04 +010026struct 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.org82462aa2014-10-23 11:57:05 +000043// Note that this class isn't thread-safe by itself and therefore relies
44// on being protected by the caller.
45class BitrateProber {
46 public:
Jonas Olsson24923e82019-03-27 14:19:04 +010047 explicit BitrateProber(const WebRtcKeyValueConfig& field_trials);
Mirko Bonadeib471c902018-07-18 14:11:27 +020048 ~BitrateProber();
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000049
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öm0453ef82016-02-16 16:23:08 +010057 // 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.
philipel4a1ec1e2016-08-15 11:51:06 -070060 void OnIncomingPacket(size_t packet_size);
61
isheriffcc5903e2016-10-04 08:29:38 -070062 // Create a cluster used to probe for |bitrate_bps| with |num_probes| number
63 // of probes.
Erik Språngb210eeb2019-11-05 11:21:48 +010064 void CreateProbeCluster(DataRate bitrate, Timestamp now, int cluster_id);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000065
Erik Språngb210eeb2019-11-05 11:21:48 +010066 // 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.org82462aa2014-10-23 11:57:05 +000070
philipelc7bf32a2017-02-17 03:59:43 -080071 // Information about the current probing cluster.
72 PacedPacketInfo CurrentCluster() const;
philipeldd324862016-05-06 17:06:14 +020073
isheriffcc5903e2016-10-04 08:29:38 -070074 // Returns the minimum number of bytes that the prober recommends for
75 // the next probe.
76 size_t RecommendedMinProbeSize() const;
Stefan Holmer01b48882015-05-05 10:21:24 +020077
isheriffcc5903e2016-10-04 08:29:38 -070078 // 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ångb210eeb2019-11-05 11:21:48 +010082 void ProbeSent(Timestamp now, size_t probe_size);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000083
84 private:
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070085 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.org82462aa2014-10-23 11:57:05 +000097
isheriffcc5903e2016-10-04 08:29:38 -070098 // A probe cluster consists of a set of probes. Each probe in turn can be
sergeyu6dbbd892017-01-17 15:07:59 -080099 // divided into a number of packets to accommodate the MTU on the network.
philipeldd324862016-05-06 17:06:14 +0200100 struct ProbeCluster {
philipelc7bf32a2017-02-17 03:59:43 -0800101 PacedPacketInfo pace_info;
sergeyu6dbbd892017-01-17 15:07:59 -0800102
103 int sent_probes = 0;
104 int sent_bytes = 0;
Erik Språngb210eeb2019-11-05 11:21:48 +0100105 Timestamp created_at = Timestamp::MinusInfinity();
106 Timestamp started_at = Timestamp::MinusInfinity();
sergeyu6dbbd892017-01-17 15:07:59 -0800107 int retries = 0;
philipeldd324862016-05-06 17:06:14 +0200108 };
109
Erik Språngb210eeb2019-11-05 11:21:48 +0100110 Timestamp CalculateNextProbeTime(const ProbeCluster& cluster) const;
sergeyu6dbbd892017-01-17 15:07:59 -0800111
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000112 ProbingState probing_state_;
sergeyu6dbbd892017-01-17 15:07:59 -0800113
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000114 // 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.
philipeldd324862016-05-06 17:06:14 +0200117 std::queue<ProbeCluster> clusters_;
sergeyu6dbbd892017-01-17 15:07:59 -0800118
119 // Time the next probe should be sent when in kActive state.
Erik Språngb210eeb2019-11-05 11:21:48 +0100120 Timestamp next_probe_time_;
Jonas Olsson8f433842019-03-25 10:10:31 +0100121
122 int total_probe_count_;
123 int total_failed_probe_count_;
Jonas Olsson24923e82019-03-27 14:19:04 +0100124
125 BitrateProberConfig config_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000126};
sergeyu6dbbd892017-01-17 15:07:59 -0800127
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000128} // namespace webrtc
sergeyu6dbbd892017-01-17 15:07:59 -0800129
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200130#endif // MODULES_PACING_BITRATE_PROBER_H_