blob: 86379ea9be463bf79947ddba1cea94b8f497067e [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
11#ifndef WEBRTC_MODULES_PACING_BITRATE_PROBER_H_
12#define WEBRTC_MODULES_PACING_BITRATE_PROBER_H_
13
philipeldd324862016-05-06 17:06:14 +020014#include <queue>
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000015
philipel4a1ec1e2016-08-15 11:51:06 -070016#include "webrtc/base/basictypes.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21// Note that this class isn't thread-safe by itself and therefore relies
22// on being protected by the caller.
23class BitrateProber {
24 public:
25 BitrateProber();
26
27 void SetEnabled(bool enable);
28
29 // Returns true if the prober is in a probing session, i.e., it currently
30 // wants packets to be sent out according to the time returned by
31 // TimeUntilNextProbe().
32 bool IsProbing() const;
33
Peter Boström0453ef82016-02-16 16:23:08 +010034 // Initializes a new probing session if the prober is allowed to probe. Does
35 // not initialize the prober unless the packet size is large enough to probe
36 // with.
philipel4a1ec1e2016-08-15 11:51:06 -070037 void OnIncomingPacket(size_t packet_size);
38
isheriffcc5903e2016-10-04 08:29:38 -070039 // Create a cluster used to probe for |bitrate_bps| with |num_probes| number
40 // of probes.
philipelfd58b612017-01-04 07:05:25 -080041 void CreateProbeCluster(int bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000042
isheriffcc5903e2016-10-04 08:29:38 -070043 // Returns the number of milliseconds until the next probe should be sent to
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000044 // get accurate probing.
45 int TimeUntilNextProbe(int64_t now_ms);
46
philipeldd324862016-05-06 17:06:14 +020047 // Which cluster that is currently being used for probing.
48 int CurrentClusterId() const;
49
isheriffcc5903e2016-10-04 08:29:38 -070050 // Returns the minimum number of bytes that the prober recommends for
51 // the next probe.
52 size_t RecommendedMinProbeSize() const;
Stefan Holmer01b48882015-05-05 10:21:24 +020053
isheriffcc5903e2016-10-04 08:29:38 -070054 // Called to report to the prober that a probe has been sent. In case of
55 // multiple packets per probe, this call would be made at the end of sending
56 // the last packet in probe. |probe_size| is the total size of all packets
57 // in probe.
58 void ProbeSent(int64_t now_ms, size_t probe_size);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000059
60 private:
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070061 enum class ProbingState {
62 // Probing will not be triggered in this state at all times.
63 kDisabled,
64 // Probing is enabled and ready to trigger on the first packet arrival.
65 kInactive,
66 // Probe cluster is filled with the set of data rates to be probed and
67 // probes are being sent.
68 kActive,
69 // Probing is enabled, but currently suspended until an explicit trigger
70 // to start probing again.
71 kSuspended,
72 };
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000073
isheriffcc5903e2016-10-04 08:29:38 -070074 // A probe cluster consists of a set of probes. Each probe in turn can be
sergeyu6dbbd892017-01-17 15:07:59 -080075 // divided into a number of packets to accommodate the MTU on the network.
philipeldd324862016-05-06 17:06:14 +020076 struct ProbeCluster {
philipelfd58b612017-01-04 07:05:25 -080077 int min_probes = 0;
philipelfd58b612017-01-04 07:05:25 -080078 int min_bytes = 0;
philipelfd58b612017-01-04 07:05:25 -080079 int bitrate_bps = 0;
philipeldd324862016-05-06 17:06:14 +020080 int id = -1;
sergeyu6dbbd892017-01-17 15:07:59 -080081
82 int sent_probes = 0;
83 int sent_bytes = 0;
84 int64_t time_started_ms = -1;
85
86 int retries = 0;
philipeldd324862016-05-06 17:06:14 +020087 };
88
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070089 // Resets the state of the prober and clears any cluster/timing data tracked.
90 void ResetState();
91
sergeyu6dbbd892017-01-17 15:07:59 -080092 int64_t GetNextProbeTime(const ProbeCluster& cluster);
93
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000094 ProbingState probing_state_;
sergeyu6dbbd892017-01-17 15:07:59 -080095
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000096 // Probe bitrate per packet. These are used to compute the delta relative to
97 // the previous probe packet based on the size and time when that packet was
98 // sent.
philipeldd324862016-05-06 17:06:14 +020099 std::queue<ProbeCluster> clusters_;
sergeyu6dbbd892017-01-17 15:07:59 -0800100
101 // Time the next probe should be sent when in kActive state.
102 int64_t next_probe_time_ms_;
103
philipel29dca2c2016-05-13 11:13:05 +0200104 int next_cluster_id_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000105};
sergeyu6dbbd892017-01-17 15:07:59 -0800106
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000107} // namespace webrtc
sergeyu6dbbd892017-01-17 15:07:59 -0800108
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000109#endif // WEBRTC_MODULES_PACING_BITRATE_PROBER_H_