|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by a BSD-style license |
| 5 | + * that can be found in the LICENSE file in the root of the source |
| 6 | + * tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/* More information about these options at jshint.com/docs/options */ |
| 10 | +'use strict'; |
| 11 | +function VideoFrameChecker(videoElement) { |
| 12 | + this.frameStats = { |
| 13 | + numFrozenFrames: 0, |
| 14 | + numBlackFrames: 0, |
| 15 | + numFrames: 0 |
| 16 | + }; |
| 17 | + |
| 18 | + this.running_ = true; |
| 19 | + |
| 20 | + this.nonBlackPixelLumaThreshold = 20; |
| 21 | + this.previousFrame_ = []; |
| 22 | + this.identicalFrameSsimThreshold = 0.985; |
| 23 | + this.frameComparator = new Ssim(); |
| 24 | + |
| 25 | + this.canvas_ = document.createElement('canvas'); |
| 26 | + this.videoElement_ = videoElement; |
| 27 | + this.listener_ = this.checkVideoFrame_.bind(this); |
| 28 | + this.videoElement_.addEventListener('play', this.listener_, false); |
| 29 | +} |
| 30 | + |
| 31 | +VideoFrameChecker.prototype = { |
| 32 | + stop: function() { |
| 33 | + this.videoElement_.removeEventListener('play' , this.listener_); |
| 34 | + this.running_ = false; |
| 35 | + }, |
| 36 | + |
| 37 | + getCurrentImageData_: function() { |
| 38 | + this.canvas_.width = this.videoElement_.width; |
| 39 | + this.canvas_.height = this.videoElement_.height; |
| 40 | + |
| 41 | + var context = this.canvas_.getContext('2d'); |
| 42 | + context.drawImage(this.videoElement_, 0, 0, this.canvas_.width, |
| 43 | + this.canvas_.height); |
| 44 | + return context.getImageData(0, 0, this.canvas_.width, this.canvas_.height); |
| 45 | + }, |
| 46 | + |
| 47 | + checkVideoFrame_: function() { |
| 48 | + if (!this.running_) { |
| 49 | + return; |
| 50 | + } |
| 51 | + if (this.videoElement_.ended) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + var imageData = this.getCurrentImageData_(); |
| 56 | + |
| 57 | + if (this.isBlackFrame_(imageData.data, imageData.data.length)) { |
| 58 | + this.frameStats.numBlackFrames++; |
| 59 | + } |
| 60 | + |
| 61 | + if (this.frameComparator.calculate(this.previousFrame_, imageData.data) > |
| 62 | + this.identicalFrameSsimThreshold) { |
| 63 | + this.frameStats.numFrozenFrames++; |
| 64 | + } |
| 65 | + this.previousFrame_ = imageData.data; |
| 66 | + |
| 67 | + this.frameStats.numFrames++; |
| 68 | + setTimeout(this.checkVideoFrame_.bind(this), 20); |
| 69 | + }, |
| 70 | + |
| 71 | + isBlackFrame_: function(data, length) { |
| 72 | + // TODO: Use a statistical, histogram-based detection. |
| 73 | + var thresh = this.nonBlackPixelLumaThreshold; |
| 74 | + var accuLuma = 0; |
| 75 | + for (var i = 4; i < length; i += 4) { |
| 76 | + // Use Luma as in Rec. 709: Y′709 = 0.21R + 0.72G + 0.07B; |
| 77 | + accuLuma += 0.21 * data[i] + 0.72 * data[i + 1] + 0.07 * data[i + 2]; |
| 78 | + // Early termination if the average Luma so far is bright enough. |
| 79 | + if (accuLuma > (thresh * i / 4)) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +if (typeof exports === 'object') { |
| 88 | + module.exports = VideoFrameChecker; |
| 89 | +} |
0 commit comments