| OLD | NEW |
| (Empty) | |
| 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/logging.h" |
| 16 |
| 17 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. |
| 22 constexpr int kMinNumProbesValidCluster = 4; |
| 23 |
| 24 // The maximum (receive rate)/(send rate) ratio for a valid estimate. |
| 25 constexpr float kValidRatio = 1.2f; |
| 26 } |
| 27 |
| 28 namespace webrtc { |
| 29 |
| 30 ProbingResult::ProbingResult() : bps(kNoEstimate), timestamp(0) {} |
| 31 |
| 32 ProbingResult::ProbingResult(int bps, int64_t timestamp) |
| 33 : bps(bps), timestamp(timestamp) {} |
| 34 |
| 35 ProbeBitrateEstimator::ProbeBitrateEstimator() : last_valid_cluster_id_(0) {} |
| 36 |
| 37 ProbingResult ProbeBitrateEstimator::PacketFeedback( |
| 38 const PacketInfo& packet_info) { |
| 39 // If this is not a probing packet or if this probing packet |
| 40 // belongs to an old cluster, do nothing. |
| 41 if (packet_info.probe_cluster_id == PacketInfo::kNotAProbe || |
| 42 packet_info.probe_cluster_id < last_valid_cluster_id_) { |
| 43 return ProbingResult(); |
| 44 } |
| 45 |
| 46 AggregatedCluster* cluster = &clusters_[packet_info.probe_cluster_id]; |
| 47 cluster->first_send_ms = |
| 48 std::min(cluster->first_send_ms, packet_info.send_time_ms); |
| 49 cluster->last_send_ms = |
| 50 std::max(cluster->last_send_ms, packet_info.send_time_ms); |
| 51 cluster->first_receive_ms = |
| 52 std::min(cluster->first_receive_ms, packet_info.arrival_time_ms); |
| 53 cluster->last_receive_ms = |
| 54 std::max(cluster->last_receive_ms, packet_info.arrival_time_ms); |
| 55 cluster->size += packet_info.payload_size; |
| 56 cluster->num_probes += 1; |
| 57 |
| 58 // Clean up old clusters. |
| 59 while (clusters_.size() > kMaxNumSavedClusters) |
| 60 clusters_.erase(clusters_.begin()); |
| 61 |
| 62 if (cluster->num_probes < kMinNumProbesValidCluster) |
| 63 return ProbingResult(); |
| 64 |
| 65 int send_interval_ms = cluster->last_send_ms - cluster->first_send_ms; |
| 66 int receive_interval_ms = |
| 67 cluster->last_receive_ms - cluster->first_receive_ms; |
| 68 |
| 69 if (send_interval_ms == 0 || receive_interval_ms == 0) { |
| 70 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" |
| 71 << " [cluster id: " << packet_info.probe_cluster_id |
| 72 << "] [send interval: " << send_interval_ms << " ms]" |
| 73 << " [receive interval: " << receive_interval_ms << " ms]"; |
| 74 |
| 75 return ProbingResult(); |
| 76 } |
| 77 |
| 78 float send_bps = static_cast<float>(cluster->size) / send_interval_ms * 1000; |
| 79 float receive_bps = |
| 80 static_cast<float>(cluster->size) / receive_interval_ms * 1000; |
| 81 float ratio = receive_bps / send_bps; |
| 82 if (ratio > kValidRatio) { |
| 83 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" |
| 84 << " [cluster id: " << packet_info.probe_cluster_id |
| 85 << "] [send: " << cluster->size << " bytes / " |
| 86 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" |
| 87 << " [receive: " << cluster->size << " bytes / " |
| 88 << receive_interval_ms << " ms = " << receive_bps / 1000 |
| 89 << " kb/s]" |
| 90 << " [ratio: " << receive_bps / 1000 << " / " |
| 91 << send_bps / 1000 << " = " << ratio << " > kValidRatio (" |
| 92 << kValidRatio << ")]"; |
| 93 |
| 94 return ProbingResult(); |
| 95 } |
| 96 // We have a valid estimate. |
| 97 int result_bps = std::min(send_bps, receive_bps); |
| 98 last_valid_cluster_id_ = packet_info.probe_cluster_id; |
| 99 LOG(LS_INFO) << "Probing successful" |
| 100 << " [cluster id: " << packet_info.probe_cluster_id |
| 101 << "] [send: " << cluster->size << " bytes / " |
| 102 << send_interval_ms << " ms = " << send_bps / 1000 << " kb/s]" |
| 103 << " [receive: " << cluster->size << " bytes / " |
| 104 << receive_interval_ms << " ms = " << receive_bps / 1000 |
| 105 << " kb/s]"; |
| 106 |
| 107 return ProbingResult(result_bps, packet_info.arrival_time_ms); |
| 108 } |
| 109 } // namespace webrtc |
| OLD | NEW |