Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2017 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 #ifndef WEBRTC_P2P_BASE_PACKETLOSSESTIMATOR_H_ | |
| 12 #define WEBRTC_P2P_BASE_PACKETLOSSESTIMATOR_H_ | |
| 13 | |
| 14 #include <stdint.h> | |
| 15 #include <string> | |
| 16 #include <unordered_map> | |
| 17 | |
| 18 namespace cricket { | |
| 19 | |
| 20 // Estimates the response rate for a series of messages expecting responses. | |
| 21 // Messages and their corresponding responses are identified by a string id. | |
| 22 // | |
| 23 // TODO(zstein): The above comment describes a PacketLossEstimatorInterface. | |
|
Taylor Brandstetter
2017/03/08 17:48:21
I don't think an interface is necessary until we h
Zach Stein
2017/03/08 23:00:08
That's why I just put this in a comment :)
I don'
| |
| 24 // Everything below is specific to this implementation, which counts the number | |
| 25 // of responses received in a trailing window, stores its state in an | |
| 26 // unordered_map, and computes the response_rate on demand. Another | |
| 27 // implementation could store state in counts and a sorted vector, or use a | |
| 28 // different policy to decide if a message should be considered lost. | |
| 29 // | |
| 30 // Responses are considered lost if they are not received within | |
| 31 // |consider_lost_after_ms|. If a response is received after | |
| 32 // |consider_lost_after_ms|, it is still considered as a response. | |
| 33 // Messages sent more than |forget_after_ms| ago are not considered | |
| 34 // anymore. The response rate is initially assumed to be 1.0. | |
| 35 // | |
| 36 // If the current time is 7, |forget_after_ms| is 6, and | |
| 37 // |consider_lost_after_ms| is 2, the response rate considers messages sent | |
| 38 // between times 1 and 5, so only messages in the following window can be | |
| 39 // considered lost: | |
| 40 // | |
| 41 // Time: 0 1 2 3 4 5 6 7 | |
| 42 // Wind: <-------> | | |
| 43 // | |
| 44 // Responses received to the right of the window are still counted. | |
| 45 class PacketLossEstimator { | |
| 46 public: | |
| 47 explicit PacketLossEstimator(int64_t consider_lost_after_ms, | |
| 48 int64_t forget_after_ms); | |
| 49 | |
| 50 // Registers that a message with the given |id| was sent at |sent_time|. | |
| 51 void ExpectResponse(std::string id, int64_t sent_time); | |
| 52 | |
| 53 // Registers a response with the given |id| was received at |received_time|. | |
| 54 void ReceivedResponse(std::string id, int64_t received_time); | |
| 55 | |
| 56 // Calculates the current response rate based on the expected and received | |
| 57 // messages. Messages sent more than |forget_after| ms ago will be erased. | |
| 58 // This method is called periodically by ExpectResponse and ReceivedResponse | |
| 59 // to bound memory usage. | |
| 60 void UpdateResponseRate(int64_t now); | |
| 61 | |
| 62 // Gets the current response rate as updated by UpdateResponseRate. | |
| 63 double get_response_rate() const { return response_rate_; } | |
| 64 | |
| 65 private: | |
| 66 struct PacketInfo { | |
| 67 int64_t sent_time; | |
| 68 bool response_received; | |
| 69 }; | |
| 70 | |
| 71 void MaybeForgetOldRequests(int64_t now); | |
| 72 | |
| 73 bool ConsiderLost(const PacketInfo&, int64_t now) const; | |
| 74 bool Forget(const PacketInfo&, int64_t now) const; | |
| 75 | |
| 76 int64_t consider_lost_after_ms_; | |
| 77 int64_t forget_after_ms_; | |
| 78 | |
| 79 int64_t last_forgot_at_ = 0; | |
| 80 | |
| 81 std::unordered_map<std::string, PacketInfo> tracked_packets_; | |
| 82 | |
| 83 double response_rate_ = 1.0; | |
| 84 }; | |
| 85 | |
| 86 } // namespace cricket | |
| 87 | |
| 88 #endif // WEBRTC_P2P_BASE_PACKETLOSSESTIMATOR_H_ | |
| OLD | NEW |