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_window_size| latest packet statuses will be used for | 27 // * Up to |max_acked_packets| latest packet statuses will be used for |
28 // calculating the packet loss metrics. | 28 // calculating the packet loss metrics. |
29 // * PLR (packet-loss-rate) is reliably computable once the statuses of | 29 // * PLR (packet-loss-rate) is reliably computable once the statuses of |
30 // |plr_min_num_packets| packets are known. | 30 // |plr_min_num_acked_packets| packets are known. |
31 // * RPLR (recoverable-packet-loss-rate) is reliably computable once the | 31 // * RPLR (recoverable-packet-loss-rate) is reliably computable once the |
32 // statuses of |rplr_min_num_pairs| pairs are known. | 32 // statuses of |rplr_min_num_acked_pairs| pairs are known. |
33 TransportFeedbackPacketLossTracker(size_t max_window_size, | 33 TransportFeedbackPacketLossTracker(size_t max_acked_packets, |
34 size_t plr_min_num_packets, | 34 size_t plr_min_num_acked_packets, |
35 size_t rplr_min_num_pairs); | 35 size_t rplr_min_num_acked_pairs); |
| 36 |
| 37 void OnPacketAdded(uint16_t seq_num); |
36 | 38 |
37 void OnReceivedTransportFeedback(const rtcp::TransportFeedback& feedback); | 39 void OnReceivedTransportFeedback(const rtcp::TransportFeedback& feedback); |
38 | 40 |
39 // Returns the packet loss rate, if the window has enough packet statuses to | 41 // Returns the packet loss rate, if the window has enough packet statuses to |
40 // reliably compute it. Otherwise, returns empty. | 42 // reliably compute it. Otherwise, returns empty. |
41 rtc::Optional<float> GetPacketLossRate() const; | 43 rtc::Optional<float> GetPacketLossRate() const; |
42 | 44 |
43 // Returns the first-order-FEC recoverable packet loss rate, if the window has | 45 // Returns the first-order-FEC recoverable packet loss rate, if the window has |
44 // enough status pairs to reliably compute it. Otherwise, returns empty. | 46 // enough status pairs to reliably compute it. Otherwise, returns empty. |
45 rtc::Optional<float> GetRecoverablePacketLossRate() const; | 47 rtc::Optional<float> GetRecoverablePacketLossRate() const; |
46 | 48 |
47 // Verifies that the internal states are correct. Only used for tests. | 49 // Verifies that the internal states are correct. Only used for tests. |
48 void Validate() const; | 50 void Validate() const; |
49 | 51 |
50 private: | 52 private: |
51 // PacketStatus is a map from sequence number to its reception status. The | 53 // When a packet is sent, we memorize its association with the stream by |
52 // status is true if the corresponding packet is received, and false if it is | 54 // marking it as (sent-but-so-far-) unacked. If we ever receive a feedback |
53 // lost. Unknown statuses are not present in the map. | 55 // that reports it as received/lost, we update the state and |
54 typedef std::map<uint16_t, bool> PacketStatus; | 56 // metrics accordingly. |
55 typedef PacketStatus::const_iterator PacketStatusIterator; | 57 enum class PacketStatus { Unacked = 0, Received = 1, Lost = 2 }; |
| 58 typedef std::map<uint16_t, PacketStatus> PacketStatusMap; |
| 59 typedef PacketStatusMap::iterator PacketStatusIterator; |
| 60 typedef PacketStatusMap::const_iterator ConstPacketStatusIterator; |
56 | 61 |
57 void Reset(); | 62 void Reset(); |
58 | 63 |
59 // ReferenceSequenceNumber() provides a sequence number that defines the | 64 // ReferenceSequenceNumber() provides a sequence number that defines the |
60 // order of packet reception info stored in |packet_status_window_|. In | 65 // order of packet reception info stored in |packet_status_window_|. In |
61 // particular, given any sequence number |x|, | 66 // particular, given any sequence number |x|, |
62 // (2^16 + x - ref_seq_num_) % 2^16 defines its actual position in | 67 // (2^16 + x - ref_seq_num_) % 2^16 defines its actual position in |
63 // |packet_status_window_|. | 68 // |packet_status_window_|. |
64 uint16_t ReferenceSequenceNumber() const; | 69 uint16_t ReferenceSequenceNumber() const; |
65 bool IsOldSequenceNumber(uint16_t seq_num) const; | 70 uint16_t NewestSequenceNumber() const; |
66 void InsertPacketStatus(uint16_t seq_num, bool received); | 71 void RecordPacketStatus(PacketStatusIterator it, |
| 72 PacketStatus new_packet_status); |
67 void RemoveOldestPacketStatus(); | 73 void RemoveOldestPacketStatus(); |
68 | 74 |
69 void UpdateMetrics(PacketStatusIterator it, bool apply /* false = undo */); | 75 void UpdateMetrics(ConstPacketStatusIterator it, |
70 void UpdatePlr(PacketStatusIterator it, bool apply /* false = undo */); | 76 bool apply /* false = undo */); |
71 void UpdateRplr(PacketStatusIterator it, bool apply /* false = undo */); | 77 void UpdatePlr(ConstPacketStatusIterator it, bool apply /* false = undo */); |
| 78 void UpdateRplr(ConstPacketStatusIterator it, bool apply /* false = undo */); |
72 | 79 |
73 PacketStatusIterator PreviousPacketStatus(PacketStatusIterator it); | 80 ConstPacketStatusIterator PreviousPacketStatus( |
74 PacketStatusIterator NextPacketStatus(PacketStatusIterator it); | 81 ConstPacketStatusIterator it) const; |
| 82 ConstPacketStatusIterator NextPacketStatus( |
| 83 ConstPacketStatusIterator it) const; |
75 | 84 |
76 const size_t max_window_size_; | 85 const size_t max_acked_packets_; |
| 86 size_t acked_packets_; |
77 | 87 |
78 PacketStatus packet_status_window_; | 88 PacketStatusMap packet_status_window_; |
79 // |ref_packet_status_| points to the oldest item in |packet_status_window_|. | 89 // |ref_packet_status_| points to the oldest item in |packet_status_window_|. |
80 PacketStatusIterator ref_packet_status_; | 90 ConstPacketStatusIterator ref_packet_status_; |
81 | 91 |
82 // Packet-loss-rate calculation (lost / all-known-packets). | 92 // Packet-loss-rate calculation (lost / all-known-packets). |
83 struct PlrState { | 93 struct PlrState { |
84 explicit PlrState(size_t min_num_packets) | 94 explicit PlrState(size_t min_num_acked_packets) |
85 : min_num_packets_(min_num_packets) { | 95 : min_num_acked_packets_(min_num_acked_packets) { |
86 Reset(); | 96 Reset(); |
87 } | 97 } |
88 void Reset() { | 98 void Reset() { |
89 num_received_packets_ = 0; | 99 num_received_packets_ = 0; |
90 num_lost_packets_ = 0; | 100 num_lost_packets_ = 0; |
91 } | 101 } |
92 rtc::Optional<float> GetMetric() const; | 102 rtc::Optional<float> GetMetric() const; |
93 const size_t min_num_packets_; | 103 const size_t min_num_acked_packets_; |
94 size_t num_received_packets_; | 104 size_t num_received_packets_; |
95 size_t num_lost_packets_; | 105 size_t num_lost_packets_; |
96 } plr_state_; | 106 } plr_state_; |
97 | 107 |
98 // Recoverable packet loss calculation (first-order-FEC recoverable). | 108 // Recoverable packet loss calculation (first-order-FEC recoverable). |
99 struct RplrState { | 109 struct RplrState { |
100 explicit RplrState(size_t min_num_pairs) | 110 explicit RplrState(size_t min_num_acked_pairs) |
101 : min_num_pairs_(min_num_pairs) { | 111 : min_num_acked_pairs_(min_num_acked_pairs) { |
102 Reset(); | 112 Reset(); |
103 } | 113 } |
104 void Reset() { | 114 void Reset() { |
105 num_known_pairs_ = 0; | 115 num_acked_pairs_ = 0; |
106 num_recoverable_losses_ = 0; | 116 num_recoverable_losses_ = 0; |
107 } | 117 } |
108 rtc::Optional<float> GetMetric() const; | 118 rtc::Optional<float> GetMetric() const; |
109 // Recoverable packets are those which were lost, but immediately followed | 119 // Recoverable packets are those which were lost, but immediately followed |
110 // by a properly received packet. If that second packet carried FEC, | 120 // by a properly received packet. If that second packet carried FEC, |
111 // the data from the former (lost) packet could be recovered. | 121 // the data from the former (lost) packet could be recovered. |
112 // The RPLR is calculated as the fraction of such pairs (lost-received) out | 122 // The RPLR is calculated as the fraction of such pairs (lost-received) out |
113 // of all pairs of consecutive acked packets. | 123 // of all pairs of consecutive acked packets. |
114 const size_t min_num_pairs_; | 124 const size_t min_num_acked_pairs_; |
115 size_t num_known_pairs_; | 125 size_t num_acked_pairs_; |
116 size_t num_recoverable_losses_; | 126 size_t num_recoverable_losses_; |
117 } rplr_state_; | 127 } rplr_state_; |
118 | |
119 size_t num_consecutive_old_reports_; // TODO(elad.alon): Upcoming CL removes. | |
120 }; | 128 }; |
121 | 129 |
122 } // namespace webrtc | 130 } // namespace webrtc |
123 | 131 |
124 #endif // WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ | 132 #endif // WEBRTC_VOICE_ENGINE_TRANSPORT_FEEDBACK_PACKET_LOSS_TRACKER_H_ |
OLD | NEW |