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 <map>
15 #include <vector>
16 
17 #include "api/transport/network_control.h"
18 #include "api/transport/webrtc_key_value_config.h"
19 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
20 #include "rtc_base/experiments/field_trial_parser.h"
21 #include "rtc_base/numerics/sequence_number_util.h"
22 #include "rtc_base/synchronization/mutex.h"
23 
24 namespace webrtc {
25 
26 class Clock;
27 class PacketRouter;
28 namespace rtcp {
29 class TransportFeedback;
30 }
31 
32 // Class used when send-side BWE is enabled: This proxy is instantiated on the
33 // receive side. It buffers a number of receive timestamps and then sends
34 // transport feedback messages back too the send side.
35 
36 class RemoteEstimatorProxy : public RemoteBitrateEstimator {
37  public:
38   RemoteEstimatorProxy(Clock* clock,
39                        TransportFeedbackSenderInterface* feedback_sender,
40                        const WebRtcKeyValueConfig* key_value_config,
41                        NetworkStateEstimator* network_state_estimator);
42   ~RemoteEstimatorProxy() override;
43 
44   void IncomingPacket(int64_t arrival_time_ms,
45                       size_t payload_size,
46                       const RTPHeader& header) override;
RemoveStream(uint32_t ssrc)47   void RemoveStream(uint32_t ssrc) override {}
48   bool LatestEstimate(std::vector<unsigned int>* ssrcs,
49                       unsigned int* bitrate_bps) const override;
OnRttUpdate(int64_t avg_rtt_ms,int64_t max_rtt_ms)50   void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override {}
SetMinBitrate(int min_bitrate_bps)51   void SetMinBitrate(int min_bitrate_bps) override {}
52   int64_t TimeUntilNextProcess() override;
53   void Process() override;
54   void OnBitrateChanged(int bitrate);
55   void SetSendPeriodicFeedback(bool send_periodic_feedback);
56 
57  private:
58   struct TransportWideFeedbackConfig {
59     FieldTrialParameter<TimeDelta> back_window{"wind", TimeDelta::Millis(500)};
60     FieldTrialParameter<TimeDelta> min_interval{"min", TimeDelta::Millis(50)};
61     FieldTrialParameter<TimeDelta> max_interval{"max", TimeDelta::Millis(250)};
62     FieldTrialParameter<TimeDelta> default_interval{"def",
63                                                     TimeDelta::Millis(100)};
64     FieldTrialParameter<double> bandwidth_fraction{"frac", 0.05};
TransportWideFeedbackConfigTransportWideFeedbackConfig65     explicit TransportWideFeedbackConfig(
66         const WebRtcKeyValueConfig* key_value_config) {
67       ParseFieldTrial({&back_window, &min_interval, &max_interval,
68                        &default_interval, &bandwidth_fraction},
69                       key_value_config->Lookup(
70                           "WebRTC-Bwe-TransportWideFeedbackIntervals"));
71     }
72   };
73 
74   static const int kMaxNumberOfPackets;
75 
76   void SendPeriodicFeedbacks() RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
77   void SendFeedbackOnRequest(int64_t sequence_number,
78                              const FeedbackRequest& feedback_request)
79       RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
80   static int64_t BuildFeedbackPacket(
81       uint8_t feedback_packet_count,
82       uint32_t media_ssrc,
83       int64_t base_sequence_number,
84       std::map<int64_t, int64_t>::const_iterator
85           begin_iterator,  // |begin_iterator| is inclusive.
86       std::map<int64_t, int64_t>::const_iterator
87           end_iterator,  // |end_iterator| is exclusive.
88       rtcp::TransportFeedback* feedback_packet);
89 
90   Clock* const clock_;
91   TransportFeedbackSenderInterface* const feedback_sender_;
92   const TransportWideFeedbackConfig send_config_;
93   int64_t last_process_time_ms_;
94 
95   Mutex lock_;
96   //  |network_state_estimator_| may be null.
97   NetworkStateEstimator* const network_state_estimator_
98       RTC_PT_GUARDED_BY(&lock_);
99   uint32_t media_ssrc_ RTC_GUARDED_BY(&lock_);
100   uint8_t feedback_packet_count_ RTC_GUARDED_BY(&lock_);
101   SeqNumUnwrapper<uint16_t> unwrapper_ RTC_GUARDED_BY(&lock_);
102   absl::optional<int64_t> periodic_window_start_seq_ RTC_GUARDED_BY(&lock_);
103   // Map unwrapped seq -> time.
104   std::map<int64_t, int64_t> packet_arrival_times_ RTC_GUARDED_BY(&lock_);
105   int64_t send_interval_ms_ RTC_GUARDED_BY(&lock_);
106   bool send_periodic_feedback_ RTC_GUARDED_BY(&lock_);
107 
108   // Unwraps absolute send times.
109   uint32_t previous_abs_send_time_ RTC_GUARDED_BY(&lock_);
110   Timestamp abs_send_timestamp_ RTC_GUARDED_BY(&lock_);
111 };
112 
113 }  // namespace webrtc
114 
115 #endif  //  MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_ESTIMATOR_PROXY_H_
116