Backed out changeset 06f41c22f3a6 (bug 1888460) for causing linux xpcshell failures...
[gecko.git] / dom / media / MediaChannelStatistics.h
blob9a3636cef250afc7b5a2a069fe61267c401e26a1
1 /* vim:set ts=2 sw=2 sts=2 et cindent: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #if !defined(MediaChannelStatistics_h_)
7 # define MediaChannelStatistics_h_
9 # include "mozilla/TimeStamp.h"
11 namespace mozilla {
13 // Number of bytes we have accumulated before we assume the connection download
14 // rate can be reliably calculated. 57 Segments at IW=3 allows slow start to
15 // reach a CWND of 30 (See bug 831998)
16 static const int64_t RELIABLE_DATA_THRESHOLD = 57 * 1460;
18 /**
19 * This class is useful for estimating rates of data passing through
20 * some channel. The idea is that activity on the channel "starts"
21 * and "stops" over time. At certain times data passes through the
22 * channel (usually while the channel is active; data passing through
23 * an inactive channel is ignored). The GetRate() function computes
24 * an estimate of the "current rate" of the channel, which is some
25 * kind of average of the data passing through over the time the
26 * channel is active.
28 * All methods take "now" as a parameter so the user of this class can
29 * control the timeline used.
31 class MediaChannelStatistics {
32 public:
33 MediaChannelStatistics() = default;
34 MediaChannelStatistics(const MediaChannelStatistics&) = default;
35 MediaChannelStatistics& operator=(const MediaChannelStatistics&) = default;
37 void Reset() {
38 mLastStartTime = TimeStamp();
39 mAccumulatedTime = TimeDuration(0);
40 mAccumulatedBytes = 0;
41 mIsStarted = false;
43 void Start() {
44 if (mIsStarted) return;
45 mLastStartTime = TimeStamp::Now();
46 mIsStarted = true;
48 void Stop() {
49 if (!mIsStarted) return;
50 mAccumulatedTime += TimeStamp::Now() - mLastStartTime;
51 mIsStarted = false;
53 void AddBytes(int64_t aBytes) {
54 if (!mIsStarted) {
55 // ignore this data, it may be related to seeking or some other
56 // operation we don't care about
57 return;
59 mAccumulatedBytes += aBytes;
61 double GetRateAtLastStop(bool* aReliable) const {
62 double seconds = mAccumulatedTime.ToSeconds();
63 *aReliable =
64 (seconds >= 1.0) || (mAccumulatedBytes >= RELIABLE_DATA_THRESHOLD);
65 if (seconds <= 0.0) return 0.0;
66 return static_cast<double>(mAccumulatedBytes) / seconds;
68 double GetRate(bool* aReliable) const {
69 TimeDuration time = mAccumulatedTime;
70 if (mIsStarted) {
71 time += TimeStamp::Now() - mLastStartTime;
73 double seconds = time.ToSeconds();
74 *aReliable =
75 (seconds >= 3.0) || (mAccumulatedBytes >= RELIABLE_DATA_THRESHOLD);
76 if (seconds <= 0.0) return 0.0;
77 return static_cast<double>(mAccumulatedBytes) / seconds;
80 private:
81 int64_t mAccumulatedBytes = 0;
82 TimeDuration mAccumulatedTime;
83 TimeStamp mLastStartTime;
84 bool mIsStarted = false;
87 } // namespace mozilla
89 #endif // MediaChannelStatistics_h_