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_BITRATE_ESTIMATOR_SINGLE_STREAM_H_
12 #define MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_SINGLE_STREAM_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <map>
18 #include <memory>
19 #include <vector>
20 
21 #include "api/transport/field_trial_based_config.h"
22 #include "modules/remote_bitrate_estimator/aimd_rate_control.h"
23 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
24 #include "rtc_base/constructor_magic.h"
25 #include "rtc_base/rate_statistics.h"
26 #include "rtc_base/synchronization/mutex.h"
27 #include "rtc_base/thread_annotations.h"
28 
29 namespace webrtc {
30 
31 class Clock;
32 struct RTPHeader;
33 
34 class RemoteBitrateEstimatorSingleStream : public RemoteBitrateEstimator {
35  public:
36   RemoteBitrateEstimatorSingleStream(RemoteBitrateObserver* observer,
37                                      Clock* clock);
38   ~RemoteBitrateEstimatorSingleStream() override;
39 
40   void IncomingPacket(int64_t arrival_time_ms,
41                       size_t payload_size,
42                       const RTPHeader& header) override;
43   void Process() override;
44   int64_t TimeUntilNextProcess() override;
45   void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
46   void RemoveStream(uint32_t ssrc) override;
47   bool LatestEstimate(std::vector<uint32_t>* ssrcs,
48                       uint32_t* bitrate_bps) const override;
49   void SetMinBitrate(int min_bitrate_bps) override;
50 
51  private:
52   struct Detector;
53 
54   typedef std::map<uint32_t, Detector*> SsrcOveruseEstimatorMap;
55 
56   // Triggers a new estimate calculation.
57   void UpdateEstimate(int64_t time_now) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
58 
59   void GetSsrcs(std::vector<uint32_t>* ssrcs) const
60       RTC_SHARED_LOCKS_REQUIRED(mutex_);
61 
62   // Returns |remote_rate_| if the pointed to object exists,
63   // otherwise creates it.
64   AimdRateControl* GetRemoteRate() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
65 
66   Clock* const clock_;
67   const FieldTrialBasedConfig field_trials_;
68   SsrcOveruseEstimatorMap overuse_detectors_ RTC_GUARDED_BY(mutex_);
69   RateStatistics incoming_bitrate_ RTC_GUARDED_BY(mutex_);
70   uint32_t last_valid_incoming_bitrate_ RTC_GUARDED_BY(mutex_);
71   std::unique_ptr<AimdRateControl> remote_rate_ RTC_GUARDED_BY(mutex_);
72   RemoteBitrateObserver* const observer_ RTC_GUARDED_BY(mutex_);
73   mutable Mutex mutex_;
74   int64_t last_process_time_;
75   int64_t process_interval_ms_ RTC_GUARDED_BY(mutex_);
76   bool uma_recorded_;
77 
78   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RemoteBitrateEstimatorSingleStream);
79 };
80 
81 }  // namespace webrtc
82 
83 #endif  // MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_SINGLE_STREAM_H_
84