xref: /aosp_15_r20/external/webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_REMOTE_BITRATE_ESTIMATOR_REMOTE_ESTIMATOR_PROXY_H_
12 #define MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_ESTIMATOR_PROXY_H_
13 
14 #include <deque>
15 #include <functional>
16 #include <memory>
17 #include <vector>
18 
19 #include "absl/types/optional.h"
20 #include "api/field_trials_view.h"
21 #include "api/rtp_headers.h"
22 #include "api/transport/network_control.h"
23 #include "api/units/data_size.h"
24 #include "api/units/time_delta.h"
25 #include "api/units/timestamp.h"
26 #include "modules/remote_bitrate_estimator/packet_arrival_map.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
29 #include "rtc_base/numerics/sequence_number_util.h"
30 #include "rtc_base/synchronization/mutex.h"
31 
32 namespace webrtc {
33 
34 // Class used when send-side BWE is enabled: This proxy is instantiated on the
35 // receive side. It buffers a number of receive timestamps and then sends
36 // transport feedback messages back too the send side.
37 class RemoteEstimatorProxy {
38  public:
39   // Used for sending transport feedback messages when send side
40   // BWE is used.
41   using TransportFeedbackSender = std::function<void(
42       std::vector<std::unique_ptr<rtcp::RtcpPacket>> packets)>;
43   RemoteEstimatorProxy(TransportFeedbackSender feedback_sender,
44                        NetworkStateEstimator* network_state_estimator);
45   ~RemoteEstimatorProxy();
46 
47   struct Packet {
48     Timestamp arrival_time;
49     DataSize size;
50     uint32_t ssrc;
51     absl::optional<uint32_t> absolute_send_time_24bits;
52     absl::optional<uint16_t> transport_sequence_number;
53     absl::optional<FeedbackRequest> feedback_request;
54   };
55   void IncomingPacket(Packet packet);
56 
57   void IncomingPacket(int64_t arrival_time_ms,
58                       size_t payload_size,
59                       const RTPHeader& header);
60 
61   // Sends periodic feedback if it is time to send it.
62   // Returns time until next call to Process should be made.
63   TimeDelta Process(Timestamp now);
64 
65   void OnBitrateChanged(int bitrate);
66   void SetSendPeriodicFeedback(bool send_periodic_feedback);
67   void SetTransportOverhead(DataSize overhead_per_packet);
68 
69  private:
70   void MaybeCullOldPackets(int64_t sequence_number, Timestamp arrival_time)
71       RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
72   void SendPeriodicFeedbacks() RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
73   void SendFeedbackOnRequest(int64_t sequence_number,
74                              const FeedbackRequest& feedback_request)
75       RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
76 
77   // Returns a Transport Feedback packet with information about as many packets
78   // that has been received between [`begin_sequence_number_incl`,
79   // `end_sequence_number_excl`) that can fit in it. If `is_periodic_update`,
80   // this represents sending a periodic feedback message, which will make it
81   // update the `periodic_window_start_seq_` variable with the first packet that
82   // was not included in the feedback packet, so that the next update can
83   // continue from that sequence number.
84   //
85   // If no incoming packets were added, nullptr is returned.
86   //
87   // `include_timestamps` decide if the returned TransportFeedback should
88   // include timestamps.
89   std::unique_ptr<rtcp::TransportFeedback> MaybeBuildFeedbackPacket(
90       bool include_timestamps,
91       int64_t begin_sequence_number_inclusive,
92       int64_t end_sequence_number_exclusive,
93       bool is_periodic_update) RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
94 
95   const TransportFeedbackSender feedback_sender_;
96   Timestamp last_process_time_;
97 
98   Mutex lock_;
99   //  `network_state_estimator_` may be null.
100   NetworkStateEstimator* const network_state_estimator_
101       RTC_PT_GUARDED_BY(&lock_);
102   uint32_t media_ssrc_ RTC_GUARDED_BY(&lock_);
103   uint8_t feedback_packet_count_ RTC_GUARDED_BY(&lock_);
104   SeqNumUnwrapper<uint16_t> unwrapper_ RTC_GUARDED_BY(&lock_);
105   DataSize packet_overhead_ RTC_GUARDED_BY(&lock_);
106 
107   // The next sequence number that should be the start sequence number during
108   // periodic reporting. Will be absl::nullopt before the first seen packet.
109   absl::optional<int64_t> periodic_window_start_seq_ RTC_GUARDED_BY(&lock_);
110 
111   // Packet arrival times, by sequence number.
112   PacketArrivalTimeMap packet_arrival_times_ RTC_GUARDED_BY(&lock_);
113 
114   TimeDelta send_interval_ RTC_GUARDED_BY(&lock_);
115   bool send_periodic_feedback_ RTC_GUARDED_BY(&lock_);
116 
117   // Unwraps absolute send times.
118   uint32_t previous_abs_send_time_ RTC_GUARDED_BY(&lock_);
119   Timestamp abs_send_timestamp_ RTC_GUARDED_BY(&lock_);
120 };
121 
122 }  // namespace webrtc
123 
124 #endif  //  MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_ESTIMATOR_PROXY_H_
125