diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /dom/media/MediaChannelStatistics.h | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/media/MediaChannelStatistics.h')
-rw-r--r-- | dom/media/MediaChannelStatistics.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/dom/media/MediaChannelStatistics.h b/dom/media/MediaChannelStatistics.h new file mode 100644 index 0000000000..9a3636cef2 --- /dev/null +++ b/dom/media/MediaChannelStatistics.h @@ -0,0 +1,89 @@ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#if !defined(MediaChannelStatistics_h_) +# define MediaChannelStatistics_h_ + +# include "mozilla/TimeStamp.h" + +namespace mozilla { + +// Number of bytes we have accumulated before we assume the connection download +// rate can be reliably calculated. 57 Segments at IW=3 allows slow start to +// reach a CWND of 30 (See bug 831998) +static const int64_t RELIABLE_DATA_THRESHOLD = 57 * 1460; + +/** + * This class is useful for estimating rates of data passing through + * some channel. The idea is that activity on the channel "starts" + * and "stops" over time. At certain times data passes through the + * channel (usually while the channel is active; data passing through + * an inactive channel is ignored). The GetRate() function computes + * an estimate of the "current rate" of the channel, which is some + * kind of average of the data passing through over the time the + * channel is active. + * + * All methods take "now" as a parameter so the user of this class can + * control the timeline used. + */ +class MediaChannelStatistics { + public: + MediaChannelStatistics() = default; + MediaChannelStatistics(const MediaChannelStatistics&) = default; + MediaChannelStatistics& operator=(const MediaChannelStatistics&) = default; + + void Reset() { + mLastStartTime = TimeStamp(); + mAccumulatedTime = TimeDuration(0); + mAccumulatedBytes = 0; + mIsStarted = false; + } + void Start() { + if (mIsStarted) return; + mLastStartTime = TimeStamp::Now(); + mIsStarted = true; + } + void Stop() { + if (!mIsStarted) return; + mAccumulatedTime += TimeStamp::Now() - mLastStartTime; + mIsStarted = false; + } + void AddBytes(int64_t aBytes) { + if (!mIsStarted) { + // ignore this data, it may be related to seeking or some other + // operation we don't care about + return; + } + mAccumulatedBytes += aBytes; + } + double GetRateAtLastStop(bool* aReliable) const { + double seconds = mAccumulatedTime.ToSeconds(); + *aReliable = + (seconds >= 1.0) || (mAccumulatedBytes >= RELIABLE_DATA_THRESHOLD); + if (seconds <= 0.0) return 0.0; + return static_cast<double>(mAccumulatedBytes) / seconds; + } + double GetRate(bool* aReliable) const { + TimeDuration time = mAccumulatedTime; + if (mIsStarted) { + time += TimeStamp::Now() - mLastStartTime; + } + double seconds = time.ToSeconds(); + *aReliable = + (seconds >= 3.0) || (mAccumulatedBytes >= RELIABLE_DATA_THRESHOLD); + if (seconds <= 0.0) return 0.0; + return static_cast<double>(mAccumulatedBytes) / seconds; + } + + private: + int64_t mAccumulatedBytes = 0; + TimeDuration mAccumulatedTime; + TimeStamp mLastStartTime; + bool mIsStarted = false; +}; + +} // namespace mozilla + +#endif // MediaChannelStatistics_h_ |