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 WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_ABS_SEND_TIME_H_
12#define WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_ABS_SEND_TIME_H_
13
14#include <list>
15#include <map>
16#include <vector>
17
18#include "webrtc/base/checks.h"
19#include "webrtc/base/scoped_ptr.h"
20#include "webrtc/modules/remote_bitrate_estimator/aimd_rate_control.h"
21#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
22#include "webrtc/modules/remote_bitrate_estimator/inter_arrival.h"
23#include "webrtc/modules/remote_bitrate_estimator/overuse_detector.h"
24#include "webrtc/modules/remote_bitrate_estimator/overuse_estimator.h"
25#include "webrtc/modules/remote_bitrate_estimator/rate_statistics.h"
26#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
27
28namespace webrtc {
29
30struct Probe {
31  Probe(int64_t send_time_ms, int64_t recv_time_ms, size_t payload_size)
32      : send_time_ms(send_time_ms),
33        recv_time_ms(recv_time_ms),
34        payload_size(payload_size) {}
35  int64_t send_time_ms;
36  int64_t recv_time_ms;
37  size_t payload_size;
38};
39
40struct Cluster {
41  Cluster()
42      : send_mean_ms(0.0f),
43        recv_mean_ms(0.0f),
44        mean_size(0),
45        count(0),
46        num_above_min_delta(0) {}
47
48  int GetSendBitrateBps() const {
49    RTC_CHECK_GT(send_mean_ms, 0.0f);
50    return mean_size * 8 * 1000 / send_mean_ms;
51  }
52
53  int GetRecvBitrateBps() const {
54    RTC_CHECK_GT(recv_mean_ms, 0.0f);
55    return mean_size * 8 * 1000 / recv_mean_ms;
56  }
57
58  float send_mean_ms;
59  float recv_mean_ms;
60  // TODO(holmer): Add some variance metric as well?
61  size_t mean_size;
62  int count;
63  int num_above_min_delta;
64};
65
66class RemoteBitrateEstimatorAbsSendTime : public RemoteBitrateEstimator {
67 public:
68  RemoteBitrateEstimatorAbsSendTime(RemoteBitrateObserver* observer,
69                                    Clock* clock);
70  virtual ~RemoteBitrateEstimatorAbsSendTime() {}
71
72  void IncomingPacketFeedbackVector(
73      const std::vector<PacketInfo>& packet_feedback_vector) override;
74
75  void IncomingPacket(int64_t arrival_time_ms,
76                      size_t payload_size,
77                      const RTPHeader& header,
78                      bool was_paced) override;
79  // This class relies on Process() being called periodically (at least once
80  // every other second) for streams to be timed out properly. Therefore it
81  // shouldn't be detached from the ProcessThread except if it's about to be
82  // deleted.
83  int32_t Process() override;
84  int64_t TimeUntilNextProcess() override;
85  void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
86  void RemoveStream(unsigned int ssrc) override;
87  bool LatestEstimate(std::vector<unsigned int>* ssrcs,
88                      unsigned int* bitrate_bps) const override;
89  bool GetStats(ReceiveBandwidthEstimatorStats* output) const override;
90  void SetMinBitrate(int min_bitrate_bps) override;
91
92 private:
93  typedef std::map<unsigned int, int64_t> Ssrcs;
94
95  static bool IsWithinClusterBounds(int send_delta_ms,
96                                    const Cluster& cluster_aggregate);
97
98  static void AddCluster(std::list<Cluster>* clusters, Cluster* cluster);
99
100  int Id() const;
101
102  void IncomingPacketInfo(int64_t arrival_time_ms,
103                          uint32_t send_time_24bits,
104                          size_t payload_size,
105                          uint32_t ssrc,
106                          bool was_paced);
107
108  bool IsProbe(int64_t send_time_ms, int payload_size) const
109      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get());
110
111  // Triggers a new estimate calculation.
112  void UpdateEstimate(int64_t now_ms)
113      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get());
114
115  void UpdateStats(int propagation_delta_ms, int64_t now_ms)
116      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get());
117
118  void ComputeClusters(std::list<Cluster>* clusters) const;
119
120  std::list<Cluster>::const_iterator FindBestProbe(
121      const std::list<Cluster>& clusters) const
122      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get());
123
124  void ProcessClusters(int64_t now_ms)
125      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get());
126
127  bool IsBitrateImproving(int probe_bitrate_bps) const
128      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get());
129
130  rtc::scoped_ptr<CriticalSectionWrapper> crit_sect_;
131  RemoteBitrateObserver* observer_ GUARDED_BY(crit_sect_.get());
132  Clock* clock_;
133  Ssrcs ssrcs_ GUARDED_BY(crit_sect_.get());
134  rtc::scoped_ptr<InterArrival> inter_arrival_ GUARDED_BY(crit_sect_.get());
135  OveruseEstimator estimator_ GUARDED_BY(crit_sect_.get());
136  OveruseDetector detector_ GUARDED_BY(crit_sect_.get());
137  RateStatistics incoming_bitrate_ GUARDED_BY(crit_sect_.get());
138  AimdRateControl remote_rate_ GUARDED_BY(crit_sect_.get());
139  int64_t last_process_time_;
140  std::vector<int> recent_propagation_delta_ms_ GUARDED_BY(crit_sect_.get());
141  std::vector<int64_t> recent_update_time_ms_ GUARDED_BY(crit_sect_.get());
142  int64_t process_interval_ms_ GUARDED_BY(crit_sect_.get());
143  int total_propagation_delta_ms_ GUARDED_BY(crit_sect_.get());
144
145  std::list<Probe> probes_;
146  size_t total_probes_received_;
147  int64_t first_packet_time_ms_;
148
149  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RemoteBitrateEstimatorAbsSendTime);
150};
151
152}  // namespace webrtc
153
154#endif  // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_ABS_SEND_TIME_H_
155