Line data Source code
1 : /*
2 : * Copyright (c) 2016 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 : #include "webrtc/modules/congestion_controller/probe_bitrate_estimator.h"
12 :
13 : #include <algorithm>
14 :
15 : #include "webrtc/base/checks.h"
16 : #include "webrtc/base/logging.h"
17 :
18 : namespace {
19 : // The minumum number of probes we need for a valid cluster.
20 : constexpr int kMinNumProbesValidCluster = 4;
21 :
22 : // The maximum (receive rate)/(send rate) ratio for a valid estimate.
23 : constexpr float kValidRatio = 2.0f;
24 :
25 : // The maximum time period over which the cluster history is retained.
26 : // This is also the maximum time period beyond which a probing burst is not
27 : // expected to last.
28 : constexpr int kMaxClusterHistoryMs = 1000;
29 :
30 : // The maximum time interval between first and the last probe on a cluster
31 : // on the sender side as well as the receive side.
32 : constexpr int kMaxProbeIntervalMs = 1000;
33 : } // namespace
34 :
35 : namespace webrtc {
36 :
37 0 : ProbeBitrateEstimator::ProbeBitrateEstimator() {}
38 :
39 0 : int ProbeBitrateEstimator::HandleProbeAndEstimateBitrate(
40 : const PacketInfo& packet_info) {
41 0 : RTC_DCHECK_NE(packet_info.probe_cluster_id, PacketInfo::kNotAProbe);
42 :
43 0 : EraseOldClusters(packet_info.arrival_time_ms - kMaxClusterHistoryMs);
44 :
45 0 : int payload_size_bits = packet_info.payload_size * 8;
46 0 : AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id];
47 :
48 0 : if (packet_info.send_time_ms < cluster->first_send_ms) {
49 0 : cluster->first_send_ms = packet_info.send_time_ms;
50 : }
51 0 : if (packet_info.send_time_ms > cluster->last_send_ms) {
52 0 : cluster->last_send_ms = packet_info.send_time_ms;
53 0 : cluster->size_last_send = payload_size_bits;
54 : }
55 0 : if (packet_info.arrival_time_ms < cluster->first_receive_ms) {
56 0 : cluster->first_receive_ms = packet_info.arrival_time_ms;
57 0 : cluster->size_first_receive = payload_size_bits;
58 : }
59 0 : if (packet_info.arrival_time_ms > cluster->last_receive_ms) {
60 0 : cluster->last_receive_ms = packet_info.arrival_time_ms;
61 : }
62 0 : cluster->size_total += payload_size_bits;
63 0 : cluster->num_probes += 1;
64 :
65 0 : if (cluster->num_probes < kMinNumProbesValidCluster)
66 0 : return -1;
67 :
68 0 : float send_interval_ms = cluster->last_send_ms - cluster->first_send_ms;
69 : float receive_interval_ms =
70 0 : cluster->last_receive_ms - cluster->first_receive_ms;
71 :
72 0 : if (send_interval_ms <= 0 || send_interval_ms > kMaxProbeIntervalMs ||
73 0 : receive_interval_ms <= 0 || receive_interval_ms > kMaxProbeIntervalMs) {
74 0 : LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval"
75 0 : << " [cluster id: " << packet_info.probe_cluster_id
76 0 : << "] [send interval: " << send_interval_ms << " ms]"
77 0 : << " [receive interval: " << receive_interval_ms << " ms]";
78 0 : return -1;
79 : }
80 : // Since the |send_interval_ms| does not include the time it takes to actually
81 : // send the last packet the size of the last sent packet should not be
82 : // included when calculating the send bitrate.
83 0 : RTC_DCHECK_GT(cluster->size_total, cluster->size_last_send);
84 0 : float send_size = cluster->size_total - cluster->size_last_send;
85 0 : float send_bps = send_size / send_interval_ms * 1000;
86 :
87 : // Since the |receive_interval_ms| does not include the time it takes to
88 : // actually receive the first packet the size of the first received packet
89 : // should not be included when calculating the receive bitrate.
90 0 : RTC_DCHECK_GT(cluster->size_total, cluster->size_first_receive);
91 0 : float receive_size = cluster->size_total - cluster->size_first_receive;
92 0 : float receive_bps = receive_size / receive_interval_ms * 1000;
93 :
94 0 : float ratio = receive_bps / send_bps;
95 0 : if (ratio > kValidRatio) {
96 0 : LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high"
97 0 : << " [cluster id: " << packet_info.probe_cluster_id
98 0 : << "] [send: " << send_size << " bytes / " << send_interval_ms
99 0 : << " ms = " << send_bps / 1000 << " kb/s]"
100 0 : << " [receive: " << receive_size << " bytes / "
101 0 : << receive_interval_ms << " ms = " << receive_bps / 1000
102 : << " kb/s]"
103 0 : << " [ratio: " << receive_bps / 1000 << " / "
104 0 : << send_bps / 1000 << " = " << ratio << " > kValidRatio ("
105 0 : << kValidRatio << ")]";
106 0 : return -1;
107 : }
108 0 : LOG(LS_INFO) << "Probing successful"
109 0 : << " [cluster id: " << packet_info.probe_cluster_id
110 0 : << "] [send: " << send_size << " bytes / " << send_interval_ms
111 0 : << " ms = " << send_bps / 1000 << " kb/s]"
112 0 : << " [receive: " << receive_size << " bytes / "
113 0 : << receive_interval_ms << " ms = " << receive_bps / 1000
114 0 : << " kb/s]";
115 0 : return std::min(send_bps, receive_bps);
116 : }
117 :
118 0 : void ProbeBitrateEstimator::EraseOldClusters(int64_t timestamp_ms) {
119 0 : for (auto it = clusters_.begin(); it != clusters_.end();) {
120 0 : if (it->second.last_receive_ms < timestamp_ms) {
121 0 : it = clusters_.erase(it);
122 : } else {
123 0 : ++it;
124 : }
125 : }
126 0 : }
127 : } // namespace webrtc
|