1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
media / test / data / eme_player_js / fps_observer.js [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// FPSObserver observes a <video> and reports decoded FPS, dropped FPS, and
// total dropped frames during the video playback.
var FPSObserver = new function() {
this.video_ = null;
this.decodedFrames_ = 0;
this.droppedFrames_ = 0;
this.startTime_ = 0;
this.intID_ = null;
}
FPSObserver.observe = function(video) {
this.video_ = video;
var observer = this;
this.video_.addEventListener('playing', function() {
observer.onVideoPlaying();
});
this.video_.addEventListener('error', function() {
observer.endTest();
});
this.video_.addEventListener('ended', function() {
observer.endTest();
});
};
FPSObserver.onVideoPlaying = function() {
this.decodedFrames_ = 0;
this.droppedFrames_ = 0;
this.startTime_ = window.performance.now();
this.endTest(true);
var observer = this;
this.intID_ = window.setInterval(function() {
observer.calculateStats();}, 1000);
};
FPSObserver.calculateStats = function() {
if (this.video_.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
this.video_.paused || this.video_.ended)
return;
var currentTime = window.performance.now();
var deltaTime = (currentTime - this.startTime_) / 1000;
this.startTime_ = currentTime;
// Calculate decoded frames per sec.
var fps = (this.video_.webkitDecodedFrameCount - this.decodedFrames_) /
deltaTime;
this.decodedFrames_ = this.video_.webkitDecodedFrameCount;
fps = fps.toFixed(2);
decodedFPSElement.innerHTML = fps;
// Calculate dropped frames per sec.
fps = (this.video_.webkitDroppedFrameCount - this.droppedFrames_) / deltaTime;
this.droppedFrames_ = this.video_.webkitDroppedFrameCount;
fps = fps.toFixed(2);
droppedFPSElement.innerHTML = fps;
droppedFramesElement.innerHTML = this.droppedFrames_;
};
FPSObserver.endTest = function() {
window.clearInterval(this.intID_);
};