libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ratetracker.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <promeki/namespace.h>
14#include <promeki/atomic.h>
15#include <promeki/mutex.h>
16#include <promeki/timestamp.h>
17
18PROMEKI_NAMESPACE_BEGIN
19
58class RateTracker {
59 public:
61 static constexpr int64_t kDefaultWindowMs = 5000;
62
67 explicit RateTracker(int64_t windowMs = kDefaultWindowMs);
68
69 RateTracker(const RateTracker &) = delete;
70 RateTracker &operator=(const RateTracker &) = delete;
71 RateTracker(RateTracker &&) = delete;
72 RateTracker &operator=(RateTracker &&) = delete;
73
83 void record(int64_t bytes);
84
94 double bytesPerSecond() const;
95
104 double framesPerSecond() const;
105
112 void reset();
113
115 int64_t windowMs() const { return _windowMs; }
116
117 private:
127 void rotateIfStale(int64_t nowNs) const;
128
129 int64_t _windowMs;
130 mutable Atomic<int64_t> _bytes;
131 mutable Atomic<int64_t> _frames;
132 mutable Mutex _mutex;
133 mutable int64_t _windowStartNs = 0;
134 mutable int64_t _lastWindowBytes = 0;
135 mutable int64_t _lastWindowFrames = 0;
136 mutable int64_t _lastWindowElapsedNs = 0;
137 mutable bool _haveLastWindow = false;
138};
139
140PROMEKI_NAMESPACE_END
141
142#endif // PROMEKI_ENABLE_CORE