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; |
24 | |
25 // The maximum time period of 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 kMaxClusterHistoryInMs = 5000; | |
29 | |
30 // The maximum time interval between first and the last probe on a cluster | |
31 // on the side side as well as the receive side. | |
stefan-webrtc
2016/08/13 10:05:10
sender side
Irfan
2016/08/15 07:00:07
Done.
| |
32 constexpr int kMaxProbeIntervalInMs = 1000; | |
26 } | 33 } |
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, |
34 | 41 int min_clusters) { |
35 bool ProbingResult::valid() const { | 42 RTC_DCHECK(packet_info.probe_cluster_id != PacketInfo::kNotAProbe); |
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 | 43 |
50 AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id]; | 44 AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id]; |
51 cluster->first_send_ms = | 45 cluster->first_send_ms = |
52 std::min(cluster->first_send_ms, packet_info.send_time_ms); | 46 std::min(cluster->first_send_ms, packet_info.send_time_ms); |
53 cluster->last_send_ms = | 47 cluster->last_send_ms = |
54 std::max(cluster->last_send_ms, packet_info.send_time_ms); | 48 std::max(cluster->last_send_ms, packet_info.send_time_ms); |
55 cluster->first_receive_ms = | 49 cluster->first_receive_ms = |
56 std::min(cluster->first_receive_ms, packet_info.arrival_time_ms); | 50 std::min(cluster->first_receive_ms, packet_info.arrival_time_ms); |
57 cluster->last_receive_ms = | 51 cluster->last_receive_ms = |
58 std::max(cluster->last_receive_ms, packet_info.arrival_time_ms); | 52 std::max(cluster->last_receive_ms, packet_info.arrival_time_ms); |
59 cluster->size += packet_info.payload_size * 8; | 53 cluster->size += packet_info.payload_size * 8; |
60 cluster->num_probes += 1; | 54 cluster->num_probes++; |
stefan-webrtc
2016/08/13 10:05:10
++cluster...;
Irfan
2016/08/15 07:00:07
Done.
| |
61 | |
62 // Clean up old clusters. | |
63 while (clusters_.size() > kMaxNumSavedClusters) | |
64 clusters_.erase(clusters_.begin()); | |
65 | 55 |
66 if (cluster->num_probes < kMinNumProbesValidCluster) | 56 if (cluster->num_probes < kMinNumProbesValidCluster) |
67 return ProbingResult(); | 57 return -1; |
58 | |
59 // The first time a cluster meets minimum requirement. | |
60 if (cluster->num_probes == kMinNumProbesValidCluster) | |
61 valid_clusters_++; | |
62 | |
63 if (valid_clusters_ < min_clusters) | |
64 return -1; | |
65 | |
66 EraseOldClusters(packet_info.arrival_time_ms - kMaxClusterHistoryInMs); | |
68 | 67 |
69 float send_interval_ms = cluster->last_send_ms - cluster->first_send_ms; | 68 float send_interval_ms = cluster->last_send_ms - cluster->first_send_ms; |
70 float receive_interval_ms = | 69 float receive_interval_ms = |
71 cluster->last_receive_ms - cluster->first_receive_ms; | 70 cluster->last_receive_ms - cluster->first_receive_ms; |
72 | 71 |
73 // Since the send/receive interval does not include the send/receive time of | 72 // 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 | 73 // the last/first packet we expand the interval by the average inverval |
75 // between the probing packets. | 74 // between the probing packets. |
76 float interval_correction = | 75 float interval_correction = |
77 static_cast<float>(cluster->num_probes) / (cluster->num_probes - 1); | 76 static_cast<float>(cluster->num_probes) / (cluster->num_probes - 1); |
78 send_interval_ms *= interval_correction; | 77 send_interval_ms *= interval_correction; |
79 receive_interval_ms *= interval_correction; | 78 receive_interval_ms *= interval_correction; |
80 | 79 |
81 if (send_interval_ms == 0 || receive_interval_ms == 0) { | 80 if (send_interval_ms <= 0 || receive_interval_ms <= 0 || |
81 send_interval_ms > kMaxProbeIntervalInMs || | |
82 receive_interval_ms > kMaxProbeIntervalInMs) { | |
82 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" | 83 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" |
83 << " [cluster id: " << packet_info.probe_cluster_id | 84 << " [cluster id: " << packet_info.probe_cluster_id |
84 << "] [send interval: " << send_interval_ms << " ms]" | 85 << "] [send interval: " << send_interval_ms << " ms]" |
85 << " [receive interval: " << receive_interval_ms << " ms]"; | 86 << " [receive interval: " << receive_interval_ms << " ms]"; |
86 | 87 return -1; |
87 return ProbingResult(); | |
88 } | 88 } |
89 float send_bps = static_cast<float>(cluster->size) / send_interval_ms * 1000; | 89 float send_bps = static_cast<float>(cluster->size) / send_interval_ms * 1000; |
90 float receive_bps = | 90 float receive_bps = |
91 static_cast<float>(cluster->size) / receive_interval_ms * 1000; | 91 static_cast<float>(cluster->size) / receive_interval_ms * 1000; |
92 float ratio = receive_bps / send_bps; | 92 float ratio = receive_bps / send_bps; |
93 if (ratio > kValidRatio) { | 93 if (ratio > kValidRatio) { |
94 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" | 94 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" |
95 << " [cluster id: " << packet_info.probe_cluster_id | 95 << " [cluster id: " << packet_info.probe_cluster_id |
96 << "] [send: " << cluster->size << " bytes / " | 96 << "] [send: " << cluster->size << " bytes / " |
97 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" | 97 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" |
98 << " [receive: " << cluster->size << " bytes / " | 98 << " [receive: " << cluster->size << " bytes / " |
99 << receive_interval_ms << " ms = " << receive_bps / 1000 | 99 << receive_interval_ms << " ms = " << receive_bps / 1000 |
100 << " kb/s]" | 100 << " kb/s]" |
101 << " [ratio: " << receive_bps / 1000 << " / " | 101 << " [ratio: " << receive_bps / 1000 << " / " |
102 << send_bps / 1000 << " = " << ratio << " > kValidRatio (" | 102 << send_bps / 1000 << " = " << ratio << " > kValidRatio (" |
103 << kValidRatio << ")]"; | 103 << kValidRatio << ")]"; |
104 | 104 return -1; |
105 return ProbingResult(); | |
106 } | 105 } |
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" | 106 LOG(LS_INFO) << "Probing successful" |
111 << " [cluster id: " << packet_info.probe_cluster_id | 107 << " [cluster id: " << packet_info.probe_cluster_id |
112 << "] [send: " << cluster->size << " bytes / " | 108 << "] [send: " << cluster->size << " bytes / " |
113 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" | 109 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" |
114 << " [receive: " << cluster->size << " bytes / " | 110 << " [receive: " << cluster->size << " bytes / " |
115 << receive_interval_ms << " ms = " << receive_bps / 1000 | 111 << receive_interval_ms << " ms = " << receive_bps / 1000 |
116 << " kb/s]"; | 112 << " kb/s]"; |
113 cluster->bps = std::min(send_bps, receive_bps); | |
114 return HighestBitrateOnClusters(); | |
115 } | |
117 | 116 |
118 return ProbingResult(result_bps, packet_info.arrival_time_ms); | 117 void ProbeBitrateEstimator::EraseOldClusters(int64_t timestamp_ms) { |
118 for (auto it = clusters_.begin(); it != clusters_.end();) { | |
119 if (it->second.last_receive_ms < timestamp_ms) { | |
120 if (it->second.num_probes >= kMinNumProbesValidCluster) | |
121 valid_clusters_--; | |
stefan-webrtc
2016/08/13 10:05:11
--valid_clusters_;
Irfan
2016/08/15 07:00:07
Done.
| |
122 it = clusters_.erase(it); | |
123 } else { | |
124 ++it; | |
125 } | |
126 } | |
119 } | 127 } |
128 | |
129 int ProbeBitrateEstimator::HighestBitrateOnClusters() { | |
stefan-webrtc
2016/08/13 10:05:11
Our previous reasoning has been that if a probe cl
Irfan
2016/08/15 07:00:07
Given that probing uses 5 samples to get a quick i
| |
130 int highest_bps = 0; | |
131 for (const auto& kv : clusters_) { | |
132 if (kv.second.bps > highest_bps) | |
133 highest_bps = kv.second.bps; | |
134 } | |
135 return highest_bps; | |
136 } | |
137 | |
120 } // namespace webrtc | 138 } // namespace webrtc |
OLD | NEW |