blob: c2f9ad8c4713db52ea8d000b6ca1d554d31d5adf [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
14#include <cstddef>
15#include <list>
philipeldd324862016-05-06 17:06:14 +020016#include <queue>
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000017
18#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.
Per28a44562016-05-04 17:12:51 +020038 void OnIncomingPacket(uint32_t bitrate_bps,
39 size_t packet_size,
40 int64_t now_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000041
42 // Returns the number of milliseconds until the next packet should be sent to
43 // get accurate probing.
44 int TimeUntilNextProbe(int64_t now_ms);
45
philipeldd324862016-05-06 17:06:14 +020046 // Which cluster that is currently being used for probing.
47 int CurrentClusterId() const;
48
Stefan Holmer01b48882015-05-05 10:21:24 +020049 // Returns the number of bytes that the prober recommends for the next probe
50 // packet.
51 size_t RecommendedPacketSize() const;
52
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000053 // Called to report to the prober that a packet has been sent, which helps the
54 // prober know when to move to the next packet in a probe.
55 void PacketSent(int64_t now_ms, size_t packet_size);
56
57 private:
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070058 enum class ProbingState {
59 // Probing will not be triggered in this state at all times.
60 kDisabled,
61 // Probing is enabled and ready to trigger on the first packet arrival.
62 kInactive,
63 // Probe cluster is filled with the set of data rates to be probed and
64 // probes are being sent.
65 kActive,
66 // Probing is enabled, but currently suspended until an explicit trigger
67 // to start probing again.
68 kSuspended,
69 };
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000070
philipeldd324862016-05-06 17:06:14 +020071 struct ProbeCluster {
72 int max_probe_packets = 0;
73 int sent_probe_packets = 0;
74 int probe_bitrate_bps = 0;
75 int id = -1;
76 };
77
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070078 // Resets the state of the prober and clears any cluster/timing data tracked.
79 void ResetState();
80
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000081 ProbingState probing_state_;
82 // Probe bitrate per packet. These are used to compute the delta relative to
83 // the previous probe packet based on the size and time when that packet was
84 // sent.
philipeldd324862016-05-06 17:06:14 +020085 std::queue<ProbeCluster> clusters_;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070086 size_t packet_size_last_sent_;
87 // The last time a probe was sent.
88 int64_t time_last_probe_sent_ms_;
philipel29dca2c2016-05-13 11:13:05 +020089 int next_cluster_id_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000090};
91} // namespace webrtc
92#endif // WEBRTC_MODULES_PACING_BITRATE_PROBER_H_