Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/MediaStatistics.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef MediaStatistics_h_
8
#define MediaStatistics_h_
9
10
namespace mozilla {
11
12
struct MediaStatistics {
13
  // Estimate of the current playback rate (bytes/second).
14
  double mPlaybackRate;
15
  // Estimate of the current download rate (bytes/second). This
16
  // ignores time that the channel was paused by Gecko.
17
  double mDownloadRate;
18
  // Total length of media stream in bytes; -1 if not known
19
  int64_t mTotalBytes;
20
  // Current position of the download, in bytes. This is the offset of
21
  // the first uncached byte after the decoder position.
22
  int64_t mDownloadPosition;
23
  // Current position of playback, in bytes
24
  int64_t mPlaybackPosition;
25
  // If false, then mDownloadRate cannot be considered a reliable
26
  // estimate (probably because the download has only been running
27
  // a short time).
28
  bool mDownloadRateReliable;
29
  // If false, then mPlaybackRate cannot be considered a reliable
30
  // estimate (probably because playback has only been running
31
  // a short time).
32
  bool mPlaybackRateReliable;
33
34
  bool CanPlayThrough()
35
0
  {
36
0
    // Number of estimated seconds worth of data we need to have buffered
37
0
    // ahead of the current playback position before we allow the media decoder
38
0
    // to report that it can play through the entire media without the decode
39
0
    // catching up with the download. Having this margin make the
40
0
    // CanPlayThrough() calculation more stable in the case of
41
0
    // fluctuating bitrates.
42
0
    static const int64_t CAN_PLAY_THROUGH_MARGIN = 1;
43
0
44
0
    if ((mTotalBytes < 0 && mDownloadRateReliable) ||
45
0
        (mTotalBytes >= 0 && mTotalBytes == mDownloadPosition)) {
46
0
      return true;
47
0
    }
48
0
49
0
    if (!mDownloadRateReliable || !mPlaybackRateReliable) {
50
0
      return false;
51
0
    }
52
0
53
0
    int64_t bytesToDownload = mTotalBytes - mDownloadPosition;
54
0
    int64_t bytesToPlayback = mTotalBytes - mPlaybackPosition;
55
0
    double timeToDownload = bytesToDownload / mDownloadRate;
56
0
    double timeToPlay = bytesToPlayback / mPlaybackRate;
57
0
58
0
    if (timeToDownload > timeToPlay) {
59
0
      // Estimated time to download is greater than the estimated time to play.
60
0
      // We probably can't play through without having to stop to buffer.
61
0
      return false;
62
0
    }
63
0
64
0
    // Estimated time to download is less than the estimated time to play.
65
0
    // We can probably play through without having to buffer, but ensure that
66
0
    // we've got a reasonable amount of data buffered after the current
67
0
    // playback position, so that if the bitrate of the media fluctuates, or if
68
0
    // our download rate or decode rate estimation is otherwise inaccurate,
69
0
    // we don't suddenly discover that we need to buffer. This is particularly
70
0
    // required near the start of the media, when not much data is downloaded.
71
0
    int64_t readAheadMargin =
72
0
      static_cast<int64_t>(mPlaybackRate * CAN_PLAY_THROUGH_MARGIN);
73
0
    return mDownloadPosition > mPlaybackPosition + readAheadMargin;
74
0
  }
75
};
76
77
} // namespace mozilla
78
79
#endif // MediaStatistics_h_