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 |
(...skipping 24 matching lines...) Expand all Loading... |
35 namespace webrtc { | 35 namespace webrtc { |
36 | 36 |
37 ProbeBitrateEstimator::ProbeBitrateEstimator() {} | 37 ProbeBitrateEstimator::ProbeBitrateEstimator() {} |
38 | 38 |
39 int ProbeBitrateEstimator::HandleProbeAndEstimateBitrate( | 39 int ProbeBitrateEstimator::HandleProbeAndEstimateBitrate( |
40 const PacketInfo& packet_info) { | 40 const PacketInfo& packet_info) { |
41 RTC_DCHECK_NE(packet_info.probe_cluster_id, PacketInfo::kNotAProbe); | 41 RTC_DCHECK_NE(packet_info.probe_cluster_id, PacketInfo::kNotAProbe); |
42 | 42 |
43 EraseOldClusters(packet_info.arrival_time_ms - kMaxClusterHistoryMs); | 43 EraseOldClusters(packet_info.arrival_time_ms - kMaxClusterHistoryMs); |
44 | 44 |
| 45 int payload_size_bits = packet_info.payload_size * 8; |
45 AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id]; | 46 AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id]; |
46 cluster->first_send_ms = | 47 |
47 std::min(cluster->first_send_ms, packet_info.send_time_ms); | 48 if (packet_info.send_time_ms < cluster->first_send_ms) { |
48 cluster->last_send_ms = | 49 cluster->first_send_ms = packet_info.send_time_ms; |
49 std::max(cluster->last_send_ms, packet_info.send_time_ms); | 50 } |
50 cluster->first_receive_ms = | 51 if (packet_info.send_time_ms > cluster->last_send_ms) { |
51 std::min(cluster->first_receive_ms, packet_info.arrival_time_ms); | 52 cluster->last_send_ms = packet_info.send_time_ms; |
52 cluster->last_receive_ms = | 53 cluster->size_last_send = payload_size_bits; |
53 std::max(cluster->last_receive_ms, packet_info.arrival_time_ms); | 54 } |
54 cluster->size += packet_info.payload_size * 8; | 55 if (packet_info.arrival_time_ms < cluster->first_receive_ms) { |
55 ++cluster->num_probes; | 56 cluster->first_receive_ms = packet_info.arrival_time_ms; |
| 57 cluster->size_first_receive = payload_size_bits; |
| 58 } |
| 59 if (packet_info.arrival_time_ms > cluster->last_receive_ms) { |
| 60 cluster->last_receive_ms = packet_info.arrival_time_ms; |
| 61 } |
| 62 cluster->size_total += payload_size_bits; |
| 63 cluster->num_probes += 1; |
56 | 64 |
57 if (cluster->num_probes < kMinNumProbesValidCluster) | 65 if (cluster->num_probes < kMinNumProbesValidCluster) |
58 return -1; | 66 return -1; |
59 | 67 |
60 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; |
61 float receive_interval_ms = | 69 float receive_interval_ms = |
62 cluster->last_receive_ms - cluster->first_receive_ms; | 70 cluster->last_receive_ms - cluster->first_receive_ms; |
63 | 71 |
64 // Since the send/receive interval does not include the send/receive time of | |
65 // the last/first packet we expand the interval by the average inverval | |
66 // between the probing packets. | |
67 float interval_correction = | |
68 static_cast<float>(cluster->num_probes) / (cluster->num_probes - 1); | |
69 send_interval_ms *= interval_correction; | |
70 receive_interval_ms *= interval_correction; | |
71 | |
72 if (send_interval_ms <= 0 || send_interval_ms > kMaxProbeIntervalMs || | 72 if (send_interval_ms <= 0 || send_interval_ms > kMaxProbeIntervalMs || |
73 receive_interval_ms <= 0 || receive_interval_ms > kMaxProbeIntervalMs) { | 73 receive_interval_ms <= 0 || receive_interval_ms > kMaxProbeIntervalMs) { |
74 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" | 74 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" |
75 << " [cluster id: " << packet_info.probe_cluster_id | 75 << " [cluster id: " << packet_info.probe_cluster_id |
76 << "] [send interval: " << send_interval_ms << " ms]" | 76 << "] [send interval: " << send_interval_ms << " ms]" |
77 << " [receive interval: " << receive_interval_ms << " ms]"; | 77 << " [receive interval: " << receive_interval_ms << " ms]"; |
78 return -1; | 78 return -1; |
79 } | 79 } |
80 float send_bps = static_cast<float>(cluster->size) / send_interval_ms * 1000; | 80 // Since the |send_interval_ms| does not include the time it takes to actually |
81 float receive_bps = | 81 // send the last packet the size of the last sent packet should not be |
82 static_cast<float>(cluster->size) / receive_interval_ms * 1000; | 82 // included when calculating the send bitrate. |
| 83 RTC_DCHECK_GT(cluster->size_total, cluster->size_last_send); |
| 84 float send_size = cluster->size_total - cluster->size_last_send; |
| 85 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 RTC_DCHECK_GT(cluster->size_total, cluster->size_first_receive); |
| 91 float receive_size = cluster->size_total - cluster->size_first_receive; |
| 92 float receive_bps = receive_size / receive_interval_ms * 1000; |
| 93 |
83 float ratio = receive_bps / send_bps; | 94 float ratio = receive_bps / send_bps; |
84 if (ratio > kValidRatio) { | 95 if (ratio > kValidRatio) { |
85 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" | 96 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" |
86 << " [cluster id: " << packet_info.probe_cluster_id | 97 << " [cluster id: " << packet_info.probe_cluster_id |
87 << "] [send: " << cluster->size << " bytes / " | 98 << "] [send: " << send_size << " bytes / " << send_interval_ms |
88 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" | 99 << " ms = " << send_bps / 1000 << " kb/s]" |
89 << " [receive: " << cluster->size << " bytes / " | 100 << " [receive: " << receive_size << " bytes / " |
90 << receive_interval_ms << " ms = " << receive_bps / 1000 | 101 << receive_interval_ms << " ms = " << receive_bps / 1000 |
91 << " kb/s]" | 102 << " kb/s]" |
92 << " [ratio: " << receive_bps / 1000 << " / " | 103 << " [ratio: " << receive_bps / 1000 << " / " |
93 << send_bps / 1000 << " = " << ratio << " > kValidRatio (" | 104 << send_bps / 1000 << " = " << ratio << " > kValidRatio (" |
94 << kValidRatio << ")]"; | 105 << kValidRatio << ")]"; |
95 return -1; | 106 return -1; |
96 } | 107 } |
97 LOG(LS_INFO) << "Probing successful" | 108 LOG(LS_INFO) << "Probing successful" |
98 << " [cluster id: " << packet_info.probe_cluster_id | 109 << " [cluster id: " << packet_info.probe_cluster_id |
99 << "] [send: " << cluster->size << " bytes / " | 110 << "] [send: " << send_size << " bytes / " << send_interval_ms |
100 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" | 111 << " ms = " << send_bps / 1000 << " kb/s]" |
101 << " [receive: " << cluster->size << " bytes / " | 112 << " [receive: " << receive_size << " bytes / " |
102 << receive_interval_ms << " ms = " << receive_bps / 1000 | 113 << receive_interval_ms << " ms = " << receive_bps / 1000 |
103 << " kb/s]"; | 114 << " kb/s]"; |
104 return std::min(send_bps, receive_bps); | 115 return std::min(send_bps, receive_bps); |
105 } | 116 } |
106 | 117 |
107 void ProbeBitrateEstimator::EraseOldClusters(int64_t timestamp_ms) { | 118 void ProbeBitrateEstimator::EraseOldClusters(int64_t timestamp_ms) { |
108 for (auto it = clusters_.begin(); it != clusters_.end();) { | 119 for (auto it = clusters_.begin(); it != clusters_.end();) { |
109 if (it->second.last_receive_ms < timestamp_ms) { | 120 if (it->second.last_receive_ms < timestamp_ms) { |
110 it = clusters_.erase(it); | 121 it = clusters_.erase(it); |
111 } else { | 122 } else { |
112 ++it; | 123 ++it; |
113 } | 124 } |
114 } | 125 } |
115 } | 126 } |
116 } // namespace webrtc | 127 } // namespace webrtc |
OLD | NEW |