• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 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 RTC_BASE_NUMERICS_EVENT_RATE_COUNTER_H_
11 #define RTC_BASE_NUMERICS_EVENT_RATE_COUNTER_H_
12 
13 #include "rtc_base/numerics/sample_stats.h"
14 
15 namespace webrtc {
16 
17 // Calculates statistics based on events. For example for computing frame rates.
18 // Note that it doesn't provide any running statistics or reset funcitonality,
19 // so it's mostly useful for end of call statistics.
20 class EventRateCounter {
21  public:
22   // Adds an event based on it's |event_time| for correct updates of the
23   // interval statistics, each event must be added past the previous events.
24   void AddEvent(Timestamp event_time);
25   // Adds the events from |other|. Note that the interval stats won't be
26   // recalculated, only merged, so this is not equivalent to if the events would
27   // have been added to the same counter from the start.
28   void AddEvents(EventRateCounter other);
29   bool IsEmpty() const;
30   // Average number of events per second. Defaults to 0 for no events and NAN
31   // for one event.
32   double Rate() const;
interval()33   SampleStats<TimeDelta>& interval() { return interval_; }
34   TimeDelta TotalDuration() const;
Count()35   int Count() const { return event_count_; }
36 
37  private:
38   Timestamp first_time_ = Timestamp::PlusInfinity();
39   Timestamp last_time_ = Timestamp::MinusInfinity();
40   int64_t event_count_ = 0;
41   SampleStats<TimeDelta> interval_;
42 };
43 }  // namespace webrtc
44 #endif  // RTC_BASE_NUMERICS_EVENT_RATE_COUNTER_H_
45