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"
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;
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
28 * All methods take "now" as a parameter so the user of this class can
29 * control the timeline used.
31 class MediaChannelStatistics
{
33 MediaChannelStatistics() = default;
34 MediaChannelStatistics(const MediaChannelStatistics
&) = default;
35 MediaChannelStatistics
& operator=(const MediaChannelStatistics
&) = default;
38 mLastStartTime
= TimeStamp();
39 mAccumulatedTime
= TimeDuration(0);
40 mAccumulatedBytes
= 0;
44 if (mIsStarted
) return;
45 mLastStartTime
= TimeStamp::Now();
49 if (!mIsStarted
) return;
50 mAccumulatedTime
+= TimeStamp::Now() - mLastStartTime
;
53 void AddBytes(int64_t aBytes
) {
55 // ignore this data, it may be related to seeking or some other
56 // operation we don't care about
59 mAccumulatedBytes
+= aBytes
;
61 double GetRateAtLastStop(bool* aReliable
) const {
62 double seconds
= mAccumulatedTime
.ToSeconds();
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
;
71 time
+= TimeStamp::Now() - mLastStartTime
;
73 double seconds
= time
.ToSeconds();
75 (seconds
>= 3.0) || (mAccumulatedBytes
>= RELIABLE_DATA_THRESHOLD
);
76 if (seconds
<= 0.0) return 0.0;
77 return static_cast<double>(mAccumulatedBytes
) / seconds
;
81 int64_t mAccumulatedBytes
= 0;
82 TimeDuration mAccumulatedTime
;
83 TimeStamp mLastStartTime
;
84 bool mIsStarted
= false;
87 } // namespace mozilla
89 #endif // MediaChannelStatistics_h_