blob: f0ceab27a05757e2574afc727b2dfb04c937eae3 [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
philipeldd324862016-05-06 17:06:14 +020014#include <queue>
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/include/module_common_types.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000017
18namespace webrtc {
philipelc3b3f7a2017-03-29 01:23:13 -070019class RtcEventLog;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000020
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();
philipelc3b3f7a2017-03-29 01:23:13 -070026 explicit BitrateProber(RtcEventLog* event_log);
Mirko Bonadeib471c902018-07-18 14:11:27 +020027 ~BitrateProber();
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000028
29 void SetEnabled(bool enable);
30
31 // Returns true if the prober is in a probing session, i.e., it currently
32 // wants packets to be sent out according to the time returned by
33 // TimeUntilNextProbe().
34 bool IsProbing() const;
35
Peter Boström0453ef82016-02-16 16:23:08 +010036 // Initializes a new probing session if the prober is allowed to probe. Does
37 // not initialize the prober unless the packet size is large enough to probe
38 // with.
philipel4a1ec1e2016-08-15 11:51:06 -070039 void OnIncomingPacket(size_t packet_size);
40
isheriffcc5903e2016-10-04 08:29:38 -070041 // Create a cluster used to probe for |bitrate_bps| with |num_probes| number
42 // of probes.
Stefan Holmer0e3213a2017-02-08 15:19:05 +010043 void CreateProbeCluster(int bitrate_bps, int64_t now_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000044
isheriffcc5903e2016-10-04 08:29:38 -070045 // Returns the number of milliseconds until the next probe should be sent to
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000046 // get accurate probing.
47 int TimeUntilNextProbe(int64_t now_ms);
48
philipelc7bf32a2017-02-17 03:59:43 -080049 // Information about the current probing cluster.
50 PacedPacketInfo CurrentCluster() const;
philipeldd324862016-05-06 17:06:14 +020051
isheriffcc5903e2016-10-04 08:29:38 -070052 // Returns the minimum number of bytes that the prober recommends for
53 // the next probe.
54 size_t RecommendedMinProbeSize() const;
Stefan Holmer01b48882015-05-05 10:21:24 +020055
isheriffcc5903e2016-10-04 08:29:38 -070056 // Called to report to the prober that a probe has been sent. In case of
57 // multiple packets per probe, this call would be made at the end of sending
58 // the last packet in probe. |probe_size| is the total size of all packets
59 // in probe.
60 void ProbeSent(int64_t now_ms, size_t probe_size);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000061
62 private:
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070063 enum class ProbingState {
64 // Probing will not be triggered in this state at all times.
65 kDisabled,
66 // Probing is enabled and ready to trigger on the first packet arrival.
67 kInactive,
68 // Probe cluster is filled with the set of data rates to be probed and
69 // probes are being sent.
70 kActive,
71 // Probing is enabled, but currently suspended until an explicit trigger
72 // to start probing again.
73 kSuspended,
74 };
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000075
isheriffcc5903e2016-10-04 08:29:38 -070076 // A probe cluster consists of a set of probes. Each probe in turn can be
sergeyu6dbbd892017-01-17 15:07:59 -080077 // divided into a number of packets to accommodate the MTU on the network.
philipeldd324862016-05-06 17:06:14 +020078 struct ProbeCluster {
philipelc7bf32a2017-02-17 03:59:43 -080079 PacedPacketInfo pace_info;
sergeyu6dbbd892017-01-17 15:07:59 -080080
81 int sent_probes = 0;
82 int sent_bytes = 0;
Stefan Holmer0e3213a2017-02-08 15:19:05 +010083 int64_t time_created_ms = -1;
sergeyu6dbbd892017-01-17 15:07:59 -080084 int64_t time_started_ms = -1;
sergeyu6dbbd892017-01-17 15:07:59 -080085 int retries = 0;
philipeldd324862016-05-06 17:06:14 +020086 };
87
sergeyu6dbbd892017-01-17 15:07:59 -080088 int64_t GetNextProbeTime(const ProbeCluster& cluster);
89
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000090 ProbingState probing_state_;
sergeyu6dbbd892017-01-17 15:07:59 -080091
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000092 // Probe bitrate per packet. These are used to compute the delta relative to
93 // the previous probe packet based on the size and time when that packet was
94 // sent.
philipeldd324862016-05-06 17:06:14 +020095 std::queue<ProbeCluster> clusters_;
sergeyu6dbbd892017-01-17 15:07:59 -080096
97 // Time the next probe should be sent when in kActive state.
98 int64_t next_probe_time_ms_;
99
philipel29dca2c2016-05-13 11:13:05 +0200100 int next_cluster_id_;
philipelc3b3f7a2017-03-29 01:23:13 -0700101 RtcEventLog* const event_log_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000102};
sergeyu6dbbd892017-01-17 15:07:59 -0800103
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000104} // namespace webrtc
sergeyu6dbbd892017-01-17 15:07:59 -0800105
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200106#endif // MODULES_PACING_BITRATE_PROBER_H_