OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/congestion_controller/probe_bitrate_estimator.h" | 11 #include "webrtc/modules/congestion_controller/probe_bitrate_estimator.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
| 15 #include "webrtc/base/checks.h" |
15 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 // Max number of saved clusters. | |
19 constexpr size_t kMaxNumSavedClusters = 5; | |
20 | |
21 // The minumum number of probes we need for a valid cluster. | 19 // The minumum number of probes we need for a valid cluster. |
22 constexpr int kMinNumProbesValidCluster = 4; | 20 constexpr int kMinNumProbesValidCluster = 4; |
23 | 21 |
24 // The maximum (receive rate)/(send rate) ratio for a valid estimate. | 22 // The maximum (receive rate)/(send rate) ratio for a valid estimate. |
25 constexpr float kValidRatio = 1.2f; | 23 constexpr float kValidRatio = 1.2f; |
26 } | 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 |
27 | 34 |
28 namespace webrtc { | 35 namespace webrtc { |
29 | 36 |
30 ProbingResult::ProbingResult() : bps(kNoEstimate), timestamp(0) {} | 37 ProbeBitrateEstimator::ProbeBitrateEstimator() {} |
31 | 38 |
32 ProbingResult::ProbingResult(int bps, int64_t timestamp) | 39 int ProbeBitrateEstimator::HandleProbeAndEstimateBitrate( |
33 : bps(bps), timestamp(timestamp) {} | 40 const PacketInfo& packet_info) { |
| 41 RTC_DCHECK_NE(packet_info.probe_cluster_id, PacketInfo::kNotAProbe); |
34 | 42 |
35 bool ProbingResult::valid() const { | 43 EraseOldClusters(packet_info.arrival_time_ms - kMaxClusterHistoryMs); |
36 return bps != kNoEstimate; | |
37 } | |
38 | |
39 ProbeBitrateEstimator::ProbeBitrateEstimator() : last_valid_cluster_id_(0) {} | |
40 | |
41 ProbingResult ProbeBitrateEstimator::PacketFeedback( | |
42 const PacketInfo& packet_info) { | |
43 // If this is not a probing packet or if this probing packet | |
44 // belongs to an old cluster, do nothing. | |
45 if (packet_info.probe_cluster_id == PacketInfo::kNotAProbe || | |
46 packet_info.probe_cluster_id < last_valid_cluster_id_) { | |
47 return ProbingResult(); | |
48 } | |
49 | 44 |
50 AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id]; | 45 AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id]; |
51 cluster->first_send_ms = | 46 cluster->first_send_ms = |
52 std::min(cluster->first_send_ms, packet_info.send_time_ms); | 47 std::min(cluster->first_send_ms, packet_info.send_time_ms); |
53 cluster->last_send_ms = | 48 cluster->last_send_ms = |
54 std::max(cluster->last_send_ms, packet_info.send_time_ms); | 49 std::max(cluster->last_send_ms, packet_info.send_time_ms); |
55 cluster->first_receive_ms = | 50 cluster->first_receive_ms = |
56 std::min(cluster->first_receive_ms, packet_info.arrival_time_ms); | 51 std::min(cluster->first_receive_ms, packet_info.arrival_time_ms); |
57 cluster->last_receive_ms = | 52 cluster->last_receive_ms = |
58 std::max(cluster->last_receive_ms, packet_info.arrival_time_ms); | 53 std::max(cluster->last_receive_ms, packet_info.arrival_time_ms); |
59 cluster->size += packet_info.payload_size * 8; | 54 cluster->size += packet_info.payload_size * 8; |
60 cluster->num_probes += 1; | 55 ++cluster->num_probes; |
61 | |
62 // Clean up old clusters. | |
63 while (clusters_.size() > kMaxNumSavedClusters) | |
64 clusters_.erase(clusters_.begin()); | |
65 | 56 |
66 if (cluster->num_probes < kMinNumProbesValidCluster) | 57 if (cluster->num_probes < kMinNumProbesValidCluster) |
67 return ProbingResult(); | 58 return -1; |
68 | 59 |
69 float send_interval_ms = cluster->last_send_ms - cluster->first_send_ms; | 60 float send_interval_ms = cluster->last_send_ms - cluster->first_send_ms; |
70 float receive_interval_ms = | 61 float receive_interval_ms = |
71 cluster->last_receive_ms - cluster->first_receive_ms; | 62 cluster->last_receive_ms - cluster->first_receive_ms; |
72 | 63 |
73 // Since the send/receive interval does not include the send/receive time of | 64 // Since the send/receive interval does not include the send/receive time of |
74 // the last/first packet we expand the interval by the average inverval | 65 // the last/first packet we expand the interval by the average inverval |
75 // between the probing packets. | 66 // between the probing packets. |
76 float interval_correction = | 67 float interval_correction = |
77 static_cast<float>(cluster->num_probes) / (cluster->num_probes - 1); | 68 static_cast<float>(cluster->num_probes) / (cluster->num_probes - 1); |
78 send_interval_ms *= interval_correction; | 69 send_interval_ms *= interval_correction; |
79 receive_interval_ms *= interval_correction; | 70 receive_interval_ms *= interval_correction; |
80 | 71 |
81 if (send_interval_ms == 0 || receive_interval_ms == 0) { | 72 if (send_interval_ms <= 0 || send_interval_ms > kMaxProbeIntervalMs || |
| 73 receive_interval_ms <= 0 || receive_interval_ms > kMaxProbeIntervalMs) { |
82 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" | 74 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" |
83 << " [cluster id: " << packet_info.probe_cluster_id | 75 << " [cluster id: " << packet_info.probe_cluster_id |
84 << "] [send interval: " << send_interval_ms << " ms]" | 76 << "] [send interval: " << send_interval_ms << " ms]" |
85 << " [receive interval: " << receive_interval_ms << " ms]"; | 77 << " [receive interval: " << receive_interval_ms << " ms]"; |
86 | 78 return -1; |
87 return ProbingResult(); | |
88 } | 79 } |
89 float send_bps = static_cast<float>(cluster->size) / send_interval_ms * 1000; | 80 float send_bps = static_cast<float>(cluster->size) / send_interval_ms * 1000; |
90 float receive_bps = | 81 float receive_bps = |
91 static_cast<float>(cluster->size) / receive_interval_ms * 1000; | 82 static_cast<float>(cluster->size) / receive_interval_ms * 1000; |
92 float ratio = receive_bps / send_bps; | 83 float ratio = receive_bps / send_bps; |
93 if (ratio > kValidRatio) { | 84 if (ratio > kValidRatio) { |
94 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" | 85 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" |
95 << " [cluster id: " << packet_info.probe_cluster_id | 86 << " [cluster id: " << packet_info.probe_cluster_id |
96 << "] [send: " << cluster->size << " bytes / " | 87 << "] [send: " << cluster->size << " bytes / " |
97 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" | 88 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" |
98 << " [receive: " << cluster->size << " bytes / " | 89 << " [receive: " << cluster->size << " bytes / " |
99 << receive_interval_ms << " ms = " << receive_bps / 1000 | 90 << receive_interval_ms << " ms = " << receive_bps / 1000 |
100 << " kb/s]" | 91 << " kb/s]" |
101 << " [ratio: " << receive_bps / 1000 << " / " | 92 << " [ratio: " << receive_bps / 1000 << " / " |
102 << send_bps / 1000 << " = " << ratio << " > kValidRatio (" | 93 << send_bps / 1000 << " = " << ratio << " > kValidRatio (" |
103 << kValidRatio << ")]"; | 94 << kValidRatio << ")]"; |
104 | 95 return -1; |
105 return ProbingResult(); | |
106 } | 96 } |
107 // We have a valid estimate. | |
108 int result_bps = std::min(send_bps, receive_bps); | |
109 last_valid_cluster_id_ = packet_info.probe_cluster_id; | |
110 LOG(LS_INFO) << "Probing successful" | 97 LOG(LS_INFO) << "Probing successful" |
111 << " [cluster id: " << packet_info.probe_cluster_id | 98 << " [cluster id: " << packet_info.probe_cluster_id |
112 << "] [send: " << cluster->size << " bytes / " | 99 << "] [send: " << cluster->size << " bytes / " |
113 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" | 100 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" |
114 << " [receive: " << cluster->size << " bytes / " | 101 << " [receive: " << cluster->size << " bytes / " |
115 << receive_interval_ms << " ms = " << receive_bps / 1000 | 102 << receive_interval_ms << " ms = " << receive_bps / 1000 |
116 << " kb/s]"; | 103 << " kb/s]"; |
| 104 return std::min(send_bps, receive_bps); |
| 105 } |
117 | 106 |
118 return ProbingResult(result_bps, packet_info.arrival_time_ms); | 107 void ProbeBitrateEstimator::EraseOldClusters(int64_t timestamp_ms) { |
| 108 for (auto it = clusters_.begin(); it != clusters_.end();) { |
| 109 if (it->second.last_receive_ms < timestamp_ms) { |
| 110 it = clusters_.erase(it); |
| 111 } else { |
| 112 ++it; |
| 113 } |
| 114 } |
119 } | 115 } |
120 } // namespace webrtc | 116 } // namespace webrtc |
OLD | NEW |