1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // FPSObserver observes a <video> and reports decoded FPS, dropped FPS, and
6 // total dropped frames during the video playback.
7 var FPSObserver
= new function() {
9 this.decodedFrames_
= 0;
10 this.droppedFrames_
= 0;
15 FPSObserver
.observe = function(video
) {
18 this.video_
.addEventListener('playing', function() {
19 observer
.onVideoPlaying();
22 this.video_
.addEventListener('error', function() {
26 this.video_
.addEventListener('ended', function() {
31 FPSObserver
.onVideoPlaying = function() {
32 this.decodedFrames_
= 0;
33 this.droppedFrames_
= 0;
34 this.startTime_
= window
.performance
.now();
37 this.intID_
= window
.setInterval(function() {
38 observer
.calculateStats();}, 1000);
41 FPSObserver
.calculateStats = function() {
42 if (this.video_
.readyState
<= HTMLMediaElement
.HAVE_CURRENT_DATA
||
43 this.video_
.paused
|| this.video_
.ended
)
45 var currentTime
= window
.performance
.now();
46 var deltaTime
= (currentTime
- this.startTime_
) / 1000;
47 this.startTime_
= currentTime
;
49 // Calculate decoded frames per sec.
50 var fps
= (this.video_
.webkitDecodedFrameCount
- this.decodedFrames_
) /
52 this.decodedFrames_
= this.video_
.webkitDecodedFrameCount
;
54 decodedFPSElement
.innerHTML
= fps
;
56 // Calculate dropped frames per sec.
57 fps
= (this.video_
.webkitDroppedFrameCount
- this.droppedFrames_
) / deltaTime
;
58 this.droppedFrames_
= this.video_
.webkitDroppedFrameCount
;
60 droppedFPSElement
.innerHTML
= fps
;
62 droppedFramesElement
.innerHTML
= this.droppedFrames_
;
65 FPSObserver
.endTest = function() {
66 window
.clearInterval(this.intID_
);