blob: 0690750bbd50f0ef90c8a3646d857150d1a8f51c [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"
philipelc7bf32a2017-02-17 03:59:43 -080017#include "webrtc/modules/include/module_common_types.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000018#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
22// Note that this class isn't thread-safe by itself and therefore relies
23// on being protected by the caller.
24class BitrateProber {
25 public:
26 BitrateProber();
27
28 void SetEnabled(bool enable);
29
30 // Returns true if the prober is in a probing session, i.e., it currently
31 // wants packets to be sent out according to the time returned by
32 // TimeUntilNextProbe().
33 bool IsProbing() const;
34
Peter Boström0453ef82016-02-16 16:23:08 +010035 // Initializes a new probing session if the prober is allowed to probe. Does
36 // not initialize the prober unless the packet size is large enough to probe
37 // with.
philipel4a1ec1e2016-08-15 11:51:06 -070038 void OnIncomingPacket(size_t packet_size);
39
isheriffcc5903e2016-10-04 08:29:38 -070040 // Create a cluster used to probe for |bitrate_bps| with |num_probes| number
41 // of probes.
Stefan Holmer0e3213a2017-02-08 15:19:05 +010042 void CreateProbeCluster(int bitrate_bps, int64_t now_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000043
isheriffcc5903e2016-10-04 08:29:38 -070044 // Returns the number of milliseconds until the next probe should be sent to
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000045 // get accurate probing.
46 int TimeUntilNextProbe(int64_t now_ms);
47
philipelc7bf32a2017-02-17 03:59:43 -080048 // Information about the current probing cluster.
49 PacedPacketInfo CurrentCluster() const;
philipeldd324862016-05-06 17:06:14 +020050
isheriffcc5903e2016-10-04 08:29:38 -070051 // Returns the minimum number of bytes that the prober recommends for
52 // the next probe.
53 size_t RecommendedMinProbeSize() const;
Stefan Holmer01b48882015-05-05 10:21:24 +020054
isheriffcc5903e2016-10-04 08:29:38 -070055 // Called to report to the prober that a probe has been sent. In case of
56 // multiple packets per probe, this call would be made at the end of sending
57 // the last packet in probe. |probe_size| is the total size of all packets
58 // in probe.
59 void ProbeSent(int64_t now_ms, size_t probe_size);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000060
61 private:
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070062 enum class ProbingState {
63 // Probing will not be triggered in this state at all times.
64 kDisabled,
65 // Probing is enabled and ready to trigger on the first packet arrival.
66 kInactive,
67 // Probe cluster is filled with the set of data rates to be probed and
68 // probes are being sent.
69 kActive,
70 // Probing is enabled, but currently suspended until an explicit trigger
71 // to start probing again.
72 kSuspended,
73 };
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000074
isheriffcc5903e2016-10-04 08:29:38 -070075 // A probe cluster consists of a set of probes. Each probe in turn can be
sergeyu6dbbd892017-01-17 15:07:59 -080076 // divided into a number of packets to accommodate the MTU on the network.
philipeldd324862016-05-06 17:06:14 +020077 struct ProbeCluster {
philipelc7bf32a2017-02-17 03:59:43 -080078 PacedPacketInfo pace_info;
sergeyu6dbbd892017-01-17 15:07:59 -080079
80 int sent_probes = 0;
81 int sent_bytes = 0;
Stefan Holmer0e3213a2017-02-08 15:19:05 +010082 int64_t time_created_ms = -1;
sergeyu6dbbd892017-01-17 15:07:59 -080083 int64_t time_started_ms = -1;
sergeyu6dbbd892017-01-17 15:07:59 -080084 int retries = 0;
philipeldd324862016-05-06 17:06:14 +020085 };
86
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070087 // Resets the state of the prober and clears any cluster/timing data tracked.
Stefan Holmer0e3213a2017-02-08 15:19:05 +010088 void ResetState(int64_t now_ms);
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070089
sergeyu6dbbd892017-01-17 15:07:59 -080090 int64_t GetNextProbeTime(const ProbeCluster& cluster);
91
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000092 ProbingState probing_state_;
sergeyu6dbbd892017-01-17 15:07:59 -080093
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000094 // Probe bitrate per packet. These are used to compute the delta relative to
95 // the previous probe packet based on the size and time when that packet was
96 // sent.
philipeldd324862016-05-06 17:06:14 +020097 std::queue<ProbeCluster> clusters_;
sergeyu6dbbd892017-01-17 15:07:59 -080098
99 // Time the next probe should be sent when in kActive state.
100 int64_t next_probe_time_ms_;
101
philipel29dca2c2016-05-13 11:13:05 +0200102 int next_cluster_id_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000103};
sergeyu6dbbd892017-01-17 15:07:59 -0800104
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000105} // namespace webrtc
sergeyu6dbbd892017-01-17 15:07:59 -0800106
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000107#endif // WEBRTC_MODULES_PACING_BITRATE_PROBER_H_