| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 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 #ifndef WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ | 11 #ifndef WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ |
| 12 #define WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ | 12 #define WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ |
| 13 | 13 |
| 14 #include <map> | 14 #include <map> |
| 15 | 15 |
| 16 #include "webrtc/base/optional.h" | 16 #include "webrtc/base/optional.h" |
| 17 #include "webrtc/modules/include/module_common_types.h" | 17 #include "webrtc/modules/include/module_common_types.h" |
| 18 | 18 |
| 19 namespace webrtc { | 19 namespace webrtc { |
| 20 | 20 |
| 21 namespace rtcp { | 21 namespace rtcp { |
| 22 class TransportFeedback; | 22 class TransportFeedback; |
| 23 } | 23 } |
| 24 | 24 |
| 25 class TransportFeedbackPacketLossTracker final { | 25 class TransportFeedbackPacketLossTracker final { |
| 26 public: | 26 public: |
| 27 // * Up to |max_acked_packets| latest packet statuses will be used for | 27 // * |max_window_size_ms| is the time-span of the window's acked part. |
| 28 // calculating the packet loss metrics. | 28 // We make sure the TWCC sequence number difference between newest and |
| 29 // oldest is less than 0x8000, regardless of ack-state, but make sure |
| 30 // that we only consider up to |max_window_size_ms| of acked data for |
| 31 // the calculation of the metrics. |
| 29 // * PLR (packet-loss-rate) is reliably computable once the statuses of | 32 // * PLR (packet-loss-rate) is reliably computable once the statuses of |
| 30 // |plr_min_num_acked_packets| packets are known. | 33 // |plr_min_num_acked_packets| packets are known. |
| 31 // * RPLR (recoverable-packet-loss-rate) is reliably computable once the | 34 // * RPLR (recoverable-packet-loss-rate) is reliably computable once the |
| 32 // statuses of |rplr_min_num_acked_pairs| pairs are known. | 35 // statuses of |rplr_min_num_acked_pairs| pairs are known. |
| 33 TransportFeedbackPacketLossTracker(size_t max_acked_packets, | 36 TransportFeedbackPacketLossTracker(int64_t max_window_size_ms, |
| 34 size_t plr_min_num_acked_packets, | 37 size_t plr_min_num_acked_packets, |
| 35 size_t rplr_min_num_acked_pairs); | 38 size_t rplr_min_num_acked_pairs); |
| 36 | 39 |
| 37 void OnPacketAdded(uint16_t seq_num); | 40 void OnPacketAdded(uint16_t seq_num, int64_t send_time_ms); |
| 38 | 41 |
| 39 void OnReceivedTransportFeedback(const rtcp::TransportFeedback& feedback); | 42 void OnReceivedTransportFeedback(const rtcp::TransportFeedback& feedback); |
| 40 | 43 |
| 41 // Returns the packet loss rate, if the window has enough packet statuses to | 44 // Returns the packet loss rate, if the window has enough packet statuses to |
| 42 // reliably compute it. Otherwise, returns empty. | 45 // reliably compute it. Otherwise, returns empty. |
| 43 rtc::Optional<float> GetPacketLossRate() const; | 46 rtc::Optional<float> GetPacketLossRate() const; |
| 44 | 47 |
| 45 // Returns the first-order-FEC recoverable packet loss rate, if the window has | 48 // Returns the first-order-FEC recoverable packet loss rate, if the window has |
| 46 // enough status pairs to reliably compute it. Otherwise, returns empty. | 49 // enough status pairs to reliably compute it. Otherwise, returns empty. |
| 47 rtc::Optional<float> GetRecoverablePacketLossRate() const; | 50 rtc::Optional<float> GetRecoverablePacketLossRate() const; |
| 48 | 51 |
| 49 // Verifies that the internal states are correct. Only used for tests. | 52 // Verifies that the internal states are correct. Only used for tests. |
| 50 void Validate() const; | 53 void Validate() const; |
| 51 | 54 |
| 52 private: | 55 private: |
| 53 // When a packet is sent, we memorize its association with the stream by | 56 // When a packet is sent, we memorize its association with the stream by |
| 54 // marking it as (sent-but-so-far-) unacked. If we ever receive a feedback | 57 // marking it as (sent-but-so-far-) unacked. If we ever receive a feedback |
| 55 // that reports it as received/lost, we update the state and | 58 // that reports it as received/lost, we update the state and |
| 56 // metrics accordingly. | 59 // metrics accordingly. |
| 57 | 60 |
| 58 enum class PacketStatus { Unacked = 0, Received = 1, Lost = 2 }; | 61 enum class PacketStatus { Unacked = 0, Received = 1, Lost = 2 }; |
| 59 typedef std::map<uint16_t, PacketStatus> PacketStatusMap; | 62 struct SentPacket { |
| 60 typedef PacketStatusMap::const_iterator ConstPacketStatusIterator; | 63 SentPacket(int64_t send_time_ms, PacketStatus status) |
| 64 : send_time_ms(send_time_ms), status(status) {} |
| 65 int64_t send_time_ms; |
| 66 PacketStatus status; |
| 67 }; |
| 68 typedef std::map<uint16_t, SentPacket> SentPacketStatusMap; |
| 69 typedef SentPacketStatusMap::const_iterator ConstPacketStatusIterator; |
| 61 | 70 |
| 62 void Reset(); | 71 void Reset(); |
| 63 | 72 |
| 64 // ReferenceSequenceNumber() provides a sequence number that defines the | 73 // ReferenceSequenceNumber() provides a sequence number that defines the |
| 65 // order of packet reception info stored in |packet_status_window_|. In | 74 // order of packet reception info stored in |packet_status_window_|. In |
| 66 // particular, given any sequence number |x|, | 75 // particular, given any sequence number |x|, |
| 67 // (2^16 + x - ref_seq_num_) % 2^16 defines its actual position in | 76 // (2^16 + x - ref_seq_num_) % 2^16 defines its actual position in |
| 68 // |packet_status_window_|. | 77 // |packet_status_window_|. |
| 69 uint16_t ReferenceSequenceNumber() const; | 78 uint16_t ReferenceSequenceNumber() const; |
| 70 uint16_t NewestSequenceNumber() const; | 79 uint16_t NewestSequenceNumber() const; |
| 71 void RecordFeedback(PacketStatusMap::iterator it, bool received); | 80 void RecordPacketStatus(SentPacketStatusMap::iterator it, |
| 81 PacketStatus new_packet_status); |
| 72 void RemoveOldestPacketStatus(); | 82 void RemoveOldestPacketStatus(); |
| 73 | 83 |
| 74 void UpdateMetrics(ConstPacketStatusIterator it, | 84 void UpdateMetrics(ConstPacketStatusIterator it, |
| 75 bool apply /* false = undo */); | 85 bool apply /* false = undo */); |
| 76 void UpdatePlr(ConstPacketStatusIterator it, bool apply /* false = undo */); | 86 void UpdatePlr(ConstPacketStatusIterator it, bool apply /* false = undo */); |
| 77 void UpdateRplr(ConstPacketStatusIterator it, bool apply /* false = undo */); | 87 void UpdateRplr(ConstPacketStatusIterator it, bool apply /* false = undo */); |
| 78 | 88 |
| 79 ConstPacketStatusIterator PreviousPacketStatus( | 89 ConstPacketStatusIterator PreviousPacketStatus( |
| 80 ConstPacketStatusIterator it) const; | 90 ConstPacketStatusIterator it) const; |
| 81 ConstPacketStatusIterator NextPacketStatus( | 91 ConstPacketStatusIterator NextPacketStatus( |
| 82 ConstPacketStatusIterator it) const; | 92 ConstPacketStatusIterator it) const; |
| 83 | 93 |
| 84 const size_t max_acked_packets_; | 94 const int64_t max_window_size_ms_; |
| 85 size_t acked_packets_; | 95 size_t acked_packets_; |
| 86 | 96 |
| 87 PacketStatusMap packet_status_window_; | 97 SentPacketStatusMap packet_status_window_; |
| 88 // |ref_packet_status_| points to the oldest item in |packet_status_window_|. | 98 // |ref_packet_status_| points to the oldest item in |packet_status_window_|. |
| 89 ConstPacketStatusIterator ref_packet_status_; | 99 ConstPacketStatusIterator ref_packet_status_; |
| 90 | 100 |
| 91 // Packet-loss-rate calculation (lost / all-known-packets). | 101 // Packet-loss-rate calculation (lost / all-known-packets). |
| 92 struct PlrState { | 102 struct PlrState { |
| 93 explicit PlrState(size_t min_num_acked_packets) | 103 explicit PlrState(size_t min_num_acked_packets) |
| 94 : min_num_acked_packets_(min_num_acked_packets) { | 104 : min_num_acked_packets_(min_num_acked_packets) { |
| 95 Reset(); | 105 Reset(); |
| 96 } | 106 } |
| 97 void Reset() { | 107 void Reset() { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 122 // of all pairs of consecutive acked packets. | 132 // of all pairs of consecutive acked packets. |
| 123 const size_t min_num_acked_pairs_; | 133 const size_t min_num_acked_pairs_; |
| 124 size_t num_acked_pairs_; | 134 size_t num_acked_pairs_; |
| 125 size_t num_recoverable_losses_; | 135 size_t num_recoverable_losses_; |
| 126 } rplr_state_; | 136 } rplr_state_; |
| 127 }; | 137 }; |
| 128 | 138 |
| 129 } // namespace webrtc | 139 } // namespace webrtc |
| 130 | 140 |
| 131 #endif // WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ | 141 #endif // WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ |
| OLD | NEW |