blob: 0a9f961d8730de7504b9e9b3c6585c5a8b6d081d [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ångbb56d4b2019-11-04 13:53:09 +000064 void CreateProbeCluster(int bitrate_bps, int64_t now_ms, int cluster_id);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000065
Erik Språngbb56d4b2019-11-04 13:53:09 +000066 // Returns the number of milliseconds until the next probe should be sent to
67 // get accurate probing.
68 int TimeUntilNextProbe(int64_t now_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000069
philipelc7bf32a2017-02-17 03:59:43 -080070 // Information about the current probing cluster.
71 PacedPacketInfo CurrentCluster() const;
philipeldd324862016-05-06 17:06:14 +020072
isheriffcc5903e2016-10-04 08:29:38 -070073 // Returns the minimum number of bytes that the prober recommends for
74 // the next probe.
75 size_t RecommendedMinProbeSize() const;
Stefan Holmer01b48882015-05-05 10:21:24 +020076
isheriffcc5903e2016-10-04 08:29:38 -070077 // Called to report to the prober that a probe has been sent. In case of
78 // multiple packets per probe, this call would be made at the end of sending
79 // the last packet in probe. |probe_size| is the total size of all packets
80 // in probe.
Erik Språngbb56d4b2019-11-04 13:53:09 +000081 void ProbeSent(int64_t now_ms, size_t probe_size);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000082
83 private:
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070084 enum class ProbingState {
85 // Probing will not be triggered in this state at all times.
86 kDisabled,
87 // Probing is enabled and ready to trigger on the first packet arrival.
88 kInactive,
89 // Probe cluster is filled with the set of data rates to be probed and
90 // probes are being sent.
91 kActive,
92 // Probing is enabled, but currently suspended until an explicit trigger
93 // to start probing again.
94 kSuspended,
95 };
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000096
isheriffcc5903e2016-10-04 08:29:38 -070097 // A probe cluster consists of a set of probes. Each probe in turn can be
sergeyu6dbbd892017-01-17 15:07:59 -080098 // divided into a number of packets to accommodate the MTU on the network.
philipeldd324862016-05-06 17:06:14 +020099 struct ProbeCluster {
philipelc7bf32a2017-02-17 03:59:43 -0800100 PacedPacketInfo pace_info;
sergeyu6dbbd892017-01-17 15:07:59 -0800101
102 int sent_probes = 0;
103 int sent_bytes = 0;
Erik Språngbb56d4b2019-11-04 13:53:09 +0000104 int64_t time_created_ms = -1;
105 int64_t time_started_ms = -1;
sergeyu6dbbd892017-01-17 15:07:59 -0800106 int retries = 0;
philipeldd324862016-05-06 17:06:14 +0200107 };
108
Erik Språngbb56d4b2019-11-04 13:53:09 +0000109 int64_t GetNextProbeTime(const ProbeCluster& cluster);
sergeyu6dbbd892017-01-17 15:07:59 -0800110
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000111 ProbingState probing_state_;
sergeyu6dbbd892017-01-17 15:07:59 -0800112
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000113 // Probe bitrate per packet. These are used to compute the delta relative to
114 // the previous probe packet based on the size and time when that packet was
115 // sent.
philipeldd324862016-05-06 17:06:14 +0200116 std::queue<ProbeCluster> clusters_;
sergeyu6dbbd892017-01-17 15:07:59 -0800117
118 // Time the next probe should be sent when in kActive state.
Erik Språngbb56d4b2019-11-04 13:53:09 +0000119 int64_t next_probe_time_ms_;
Jonas Olsson8f433842019-03-25 10:10:31 +0100120
121 int total_probe_count_;
122 int total_failed_probe_count_;
Jonas Olsson24923e82019-03-27 14:19:04 +0100123
124 BitrateProberConfig config_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000125};
sergeyu6dbbd892017-01-17 15:07:59 -0800126
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000127} // namespace webrtc
sergeyu6dbbd892017-01-17 15:07:59 -0800128
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200129#endif // MODULES_PACING_BITRATE_PROBER_H_