Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/MediaChannelStatistics.h
Line
Count
Source (jump to first uncovered line)
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/. */
5
6
#if !defined(MediaChannelStatistics_h_)
7
#define MediaChannelStatistics_h_
8
9
#include "mozilla/TimeStamp.h"
10
11
namespace mozilla {
12
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;
17
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.
27
 *
28
 * All methods take "now" as a parameter so the user of this class can
29
 * control the timeline used.
30
 */
31
class MediaChannelStatistics {
32
public:
33
0
  MediaChannelStatistics() = default;
34
  MediaChannelStatistics(const MediaChannelStatistics&) = default;
35
  MediaChannelStatistics& operator=(const MediaChannelStatistics&) = default;
36
37
0
  void Reset() {
38
0
    mLastStartTime = TimeStamp();
39
0
    mAccumulatedTime = TimeDuration(0);
40
0
    mAccumulatedBytes = 0;
41
0
    mIsStarted = false;
42
0
  }
43
0
  void Start() {
44
0
    if (mIsStarted)
45
0
      return;
46
0
    mLastStartTime = TimeStamp::Now();
47
0
    mIsStarted = true;
48
0
  }
49
0
  void Stop() {
50
0
    if (!mIsStarted)
51
0
      return;
52
0
    mAccumulatedTime += TimeStamp::Now() - mLastStartTime;
53
0
    mIsStarted = false;
54
0
  }
55
0
  void AddBytes(int64_t aBytes) {
56
0
    if (!mIsStarted) {
57
0
      // ignore this data, it may be related to seeking or some other
58
0
      // operation we don't care about
59
0
      return;
60
0
    }
61
0
    mAccumulatedBytes += aBytes;
62
0
  }
63
  double GetRateAtLastStop(bool* aReliable) const
64
0
  {
65
0
    double seconds = mAccumulatedTime.ToSeconds();
66
0
    *aReliable = (seconds >= 1.0) ||
67
0
                 (mAccumulatedBytes >= RELIABLE_DATA_THRESHOLD);
68
0
    if (seconds <= 0.0)
69
0
      return 0.0;
70
0
    return static_cast<double>(mAccumulatedBytes)/seconds;
71
0
  }
72
  double GetRate(bool* aReliable) const
73
0
  {
74
0
    TimeDuration time = mAccumulatedTime;
75
0
    if (mIsStarted) {
76
0
      time += TimeStamp::Now() - mLastStartTime;
77
0
    }
78
0
    double seconds = time.ToSeconds();
79
0
    *aReliable = (seconds >= 3.0) ||
80
0
                 (mAccumulatedBytes >= RELIABLE_DATA_THRESHOLD);
81
0
    if (seconds <= 0.0)
82
0
      return 0.0;
83
0
    return static_cast<double>(mAccumulatedBytes)/seconds;
84
0
  }
85
private:
86
  int64_t mAccumulatedBytes = 0;
87
  TimeDuration mAccumulatedTime;
88
  TimeStamp mLastStartTime;
89
  bool mIsStarted = false;
90
};
91
92
} // namespace mozilla
93
94
#endif // MediaChannelStatistics_h_