1 /*
2  *  Copyright (c) 2015 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 MODULES_CONGESTION_CONTROLLER_RTP_TRANSPORT_FEEDBACK_ADAPTER_H_
12 #define MODULES_CONGESTION_CONTROLLER_RTP_TRANSPORT_FEEDBACK_ADAPTER_H_
13 
14 #include <deque>
15 #include <map>
16 #include <utility>
17 #include <vector>
18 
19 #include "api/transport/network_types.h"
20 #include "modules/include/module_common_types_public.h"
21 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
22 #include "rtc_base/network/sent_packet.h"
23 #include "rtc_base/network_route.h"
24 #include "rtc_base/thread_annotations.h"
25 #include "rtc_base/thread_checker.h"
26 
27 namespace webrtc {
28 
29 struct PacketFeedback {
30   PacketFeedback() = default;
31   // Time corresponding to when this object was created.
32   Timestamp creation_time = Timestamp::MinusInfinity();
33   SentPacket sent;
34   // Time corresponding to when the packet was received. Timestamped with the
35   // receiver's clock. For unreceived packet, Timestamp::PlusInfinity() is
36   // used.
37   Timestamp receive_time = Timestamp::PlusInfinity();
38 
39   // The network route that this packet is associated with.
40   rtc::NetworkRoute network_route;
41 };
42 
43 class InFlightBytesTracker {
44  public:
45   void AddInFlightPacketBytes(const PacketFeedback& packet);
46   void RemoveInFlightPacketBytes(const PacketFeedback& packet);
47   DataSize GetOutstandingData(const rtc::NetworkRoute& network_route) const;
48 
49  private:
50   struct NetworkRouteComparator {
51     bool operator()(const rtc::NetworkRoute& a,
52                     const rtc::NetworkRoute& b) const;
53   };
54   std::map<rtc::NetworkRoute, DataSize, NetworkRouteComparator> in_flight_data_;
55 };
56 
57 class TransportFeedbackAdapter {
58  public:
59   TransportFeedbackAdapter();
60 
61   void AddPacket(const RtpPacketSendInfo& packet_info,
62                  size_t overhead_bytes,
63                  Timestamp creation_time);
64   absl::optional<SentPacket> ProcessSentPacket(
65       const rtc::SentPacket& sent_packet);
66 
67   absl::optional<TransportPacketsFeedback> ProcessTransportFeedback(
68       const rtcp::TransportFeedback& feedback,
69       Timestamp feedback_receive_time);
70 
71   void SetNetworkRoute(const rtc::NetworkRoute& network_route);
72 
73   DataSize GetOutstandingData() const;
74 
75  private:
76   enum class SendTimeHistoryStatus { kNotAdded, kOk, kDuplicate };
77 
78   std::vector<PacketResult> ProcessTransportFeedbackInner(
79       const rtcp::TransportFeedback& feedback,
80       Timestamp feedback_receive_time);
81 
82   DataSize pending_untracked_size_ = DataSize::Zero();
83   Timestamp last_send_time_ = Timestamp::MinusInfinity();
84   Timestamp last_untracked_send_time_ = Timestamp::MinusInfinity();
85   SequenceNumberUnwrapper seq_num_unwrapper_;
86   std::map<int64_t, PacketFeedback> history_;
87 
88   // Sequence numbers are never negative, using -1 as it always < a real
89   // sequence number.
90   int64_t last_ack_seq_num_ = -1;
91   InFlightBytesTracker in_flight_;
92 
93   Timestamp current_offset_ = Timestamp::MinusInfinity();
94   TimeDelta last_timestamp_ = TimeDelta::MinusInfinity();
95 
96   rtc::NetworkRoute network_route_;
97 };
98 
99 }  // namespace webrtc
100 
101 #endif  // MODULES_CONGESTION_CONTROLLER_RTP_TRANSPORT_FEEDBACK_ADAPTER_H_
102