1 /*
2  *  Copyright (c) 2012 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  *  FEC and NACK added bitrate is handled outside class
11  */
12 
13 #ifndef WEBRTC_MODULES_BITRATE_CONTROLLER_SEND_SIDE_BANDWIDTH_ESTIMATION_H_
14 #define WEBRTC_MODULES_BITRATE_CONTROLLER_SEND_SIDE_BANDWIDTH_ESTIMATION_H_
15 
16 #include <deque>
17 #include <utility>
18 #include <vector>
19 
20 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
22 
23 namespace webrtc {
24 
25 class RtcEventLog;
26 
27 class SendSideBandwidthEstimation {
28  public:
29   SendSideBandwidthEstimation() = delete;
30   explicit SendSideBandwidthEstimation(RtcEventLog* event_log);
31   virtual ~SendSideBandwidthEstimation();
32 
33   void CurrentEstimate(int* bitrate, uint8_t* loss, int64_t* rtt) const;
34 
35   // Call periodically to update estimate.
36   void UpdateEstimate(int64_t now_ms);
37 
38   // Call when we receive a RTCP message with TMMBR or REMB.
39   void UpdateReceiverEstimate(int64_t now_ms, uint32_t bandwidth);
40 
41   // Call when a new delay-based estimate is available.
42   void UpdateDelayBasedEstimate(int64_t now_ms, uint32_t bitrate_bps);
43 
44   // Call when we receive a RTCP message with a ReceiveBlock.
45   void UpdateReceiverBlock(uint8_t fraction_loss,
46                            int64_t rtt,
47                            int number_of_packets,
48                            int64_t now_ms);
49 
50   void SetBitrates(int send_bitrate,
51                    int min_bitrate,
52                    int max_bitrate);
53   void SetSendBitrate(int bitrate);
54   void SetMinMaxBitrate(int min_bitrate, int max_bitrate);
55   int GetMinBitrate() const;
56 
57  private:
58   enum UmaState { kNoUpdate, kFirstDone, kDone };
59 
60   bool IsInStartPhase(int64_t now_ms) const;
61 
62   void UpdateUmaStats(int64_t now_ms, int64_t rtt, int lost_packets);
63 
64   // Returns the input bitrate capped to the thresholds defined by the max,
65   // min and incoming bandwidth.
66   uint32_t CapBitrateToThresholds(int64_t now_ms, uint32_t bitrate);
67 
68   // Updates history of min bitrates.
69   // After this method returns min_bitrate_history_.front().second contains the
70   // min bitrate used during last kBweIncreaseIntervalMs.
71   void UpdateMinHistory(int64_t now_ms);
72 
73   std::deque<std::pair<int64_t, uint32_t> > min_bitrate_history_;
74 
75   // incoming filters
76   int lost_packets_since_last_loss_update_Q8_;
77   int expected_packets_since_last_loss_update_;
78 
79   uint32_t bitrate_;
80   uint32_t min_bitrate_configured_;
81   uint32_t max_bitrate_configured_;
82   int64_t last_low_bitrate_log_ms_;
83 
84   bool has_decreased_since_last_fraction_loss_;
85   int64_t last_feedback_ms_;
86   int64_t last_packet_report_ms_;
87   int64_t last_timeout_ms_;
88   uint8_t last_fraction_loss_;
89   uint8_t last_logged_fraction_loss_;
90   int64_t last_round_trip_time_ms_;
91 
92   uint32_t bwe_incoming_;
93   uint32_t delay_based_bitrate_bps_;
94   int64_t time_last_decrease_ms_;
95   int64_t first_report_time_ms_;
96   int initially_lost_packets_;
97   int bitrate_at_2_seconds_kbps_;
98   UmaState uma_update_state_;
99   std::vector<bool> rampup_uma_stats_updated_;
100   RtcEventLog* event_log_;
101   int64_t last_rtc_event_log_ms_;
102   bool in_timeout_experiment_;
103 };
104 }  // namespace webrtc
105 #endif  // WEBRTC_MODULES_BITRATE_CONTROLLER_SEND_SIDE_BANDWIDTH_ESTIMATION_H_
106