Bug 1856663 - Add more chunks for Android mochitest-plain. r=jmaher,taskgraph-reviewe...
[gecko.git] / dom / media / MediaStatistics.h
blob08b516b25a6013e616f067eba319ff2fdabccd5b
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/. */
7 #ifndef MediaStatistics_h_
8 #define MediaStatistics_h_
10 namespace mozilla {
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;
34 bool CanPlayThrough() {
35 // Number of estimated seconds worth of data we need to have buffered
36 // ahead of the current playback position before we allow the media decoder
37 // to report that it can play through the entire media without the decode
38 // catching up with the download. Having this margin make the
39 // CanPlayThrough() calculation more stable in the case of
40 // fluctuating bitrates.
41 static const int64_t CAN_PLAY_THROUGH_MARGIN = 1;
43 if ((mTotalBytes < 0 && mDownloadRateReliable) ||
44 (mTotalBytes >= 0 && mTotalBytes == mDownloadPosition)) {
45 return true;
48 if (!mDownloadRateReliable || !mPlaybackRateReliable) {
49 return false;
52 int64_t bytesToDownload = mTotalBytes - mDownloadPosition;
53 int64_t bytesToPlayback = mTotalBytes - mPlaybackPosition;
54 double timeToDownload = bytesToDownload / mDownloadRate;
55 double timeToPlay = bytesToPlayback / mPlaybackRate;
57 if (timeToDownload > timeToPlay) {
58 // Estimated time to download is greater than the estimated time to play.
59 // We probably can't play through without having to stop to buffer.
60 return false;
63 // Estimated time to download is less than the estimated time to play.
64 // We can probably play through without having to buffer, but ensure that
65 // we've got a reasonable amount of data buffered after the current
66 // playback position, so that if the bitrate of the media fluctuates, or if
67 // our download rate or decode rate estimation is otherwise inaccurate,
68 // we don't suddenly discover that we need to buffer. This is particularly
69 // required near the start of the media, when not much data is downloaded.
70 int64_t readAheadMargin =
71 static_cast<int64_t>(mPlaybackRate * CAN_PLAY_THROUGH_MARGIN);
72 return mDownloadPosition > mPlaybackPosition + readAheadMargin;
76 } // namespace mozilla
78 #endif // MediaStatistics_h_