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#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_OVERUSE_DETECTOR_H_
11#define WEBRTC_MODULES_RTP_RTCP_SOURCE_OVERUSE_DETECTOR_H_
12
13#include <list>
14
15#include "webrtc/modules/interface/module_common_types.h"
16#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
17#include "webrtc/typedefs.h"
18
19namespace webrtc {
20enum RateControlRegion;
21
22// This class is assumed to be protected by the owner if used by multiple
23// threads.
24class OveruseDetector {
25 public:
26  explicit OveruseDetector(const OverUseDetectorOptions& options);
27  ~OveruseDetector();
28  void Update(uint16_t packet_size,
29              int64_t timestamp_ms,
30              uint32_t rtp_timestamp,
31              int64_t arrival_time_ms);
32  BandwidthUsage State() const;
33  double NoiseVar() const;
34  void SetRateControlRegion(RateControlRegion region);
35
36 private:
37  struct FrameSample {
38    FrameSample()
39        : size(0),
40          complete_time_ms(-1),
41          timestamp(-1),
42          timestamp_ms(-1) {}
43
44    uint32_t size;
45    int64_t complete_time_ms;
46    int64_t timestamp;
47    int64_t timestamp_ms;
48  };
49
50  // Returns true if |timestamp| represent a time which is later than
51  // |prev_timestamp|.
52  static bool InOrderTimestamp(uint32_t timestamp, uint32_t prev_timestamp);
53
54  bool PacketInOrder(uint32_t timestamp, int64_t timestamp_ms);
55
56  // Prepares the overuse detector to start using timestamps in milliseconds
57  // instead of 90 kHz timestamps.
58  void SwitchTimeBase();
59
60  void TimeDeltas(const FrameSample& current_frame,
61                  const FrameSample& prev_frame,
62                  int64_t* t_delta,
63                  double* ts_delta);
64  void UpdateKalman(int64_t t_delta,
65                    double ts_elta,
66                    uint32_t frame_size,
67                    uint32_t prev_frame_size);
68  double UpdateMinFramePeriod(double ts_delta);
69  void UpdateNoiseEstimate(double residual, double ts_delta, bool stable_state);
70  BandwidthUsage Detect(double ts_delta);
71  double CurrentDrift();
72
73  OverUseDetectorOptions options_;  // Must be first member
74                                    // variable. Cannot be const
75                                    // because we need to be copyable.
76  FrameSample current_frame_;
77  FrameSample prev_frame_;
78  uint16_t num_of_deltas_;
79  double slope_;
80  double offset_;
81  double E_[2][2];
82  double process_noise_[2];
83  double avg_noise_;
84  double var_noise_;
85  double threshold_;
86  std::list<double> ts_delta_hist_;
87  double prev_offset_;
88  double time_over_using_;
89  uint16_t over_use_counter_;
90  BandwidthUsage hypothesis_;
91};
92}  // namespace webrtc
93
94#endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_OVERUSE_DETECTOR_H_
95