@@ -76,13 +76,12 @@ Ssim.prototype = {
76
76
}
77
77
} ;
78
78
79
- function VideoFrameChecker ( videoElement ) {
79
+ function VideoFrameChecker ( stream , canvasElement ) {
80
80
this . frameStats = {
81
81
numFrozenFrames : 0 ,
82
82
numBlackFrames : 0 ,
83
83
numFrames : 0
84
84
} ;
85
- console . log ( "VideoFrameChecker videoElements is ," , videoElement ) ;
86
85
this . running_ = true ;
87
86
88
87
this . nonBlackPixelLumaThreshold = 20 ;
@@ -94,65 +93,67 @@ function VideoFrameChecker(videoElement) {
94
93
this . differenceThreshold = 0 ;
95
94
this . frameComparator = new Ssim ( ) ;
96
95
97
- this . canvas_ = document . createElement ( 'canvas' ) ;
98
- this . videoElement_ = videoElement ;
99
- console . log ( "VideoFrameChecker videoElements videoWith is ," , this . videoElement_ . videoWidth ) ;
100
- console . log ( "VideoFrameChecker videoElements videoHeightis ," , this . videoElement_ . videoHeight ) ;
96
+ this . canvas_ = canvasElement ;
97
+ this . stream_ = stream ;
101
98
this . listener_ = this . checkVideoFrame_ . bind ( this ) ;
102
- this . videoElement_ . addEventListener ( 'play' , this . listener_ , false ) ;
103
99
}
104
100
105
101
VideoFrameChecker . prototype = {
106
- stop : function ( ) {
107
- this . videoElement_ . removeEventListener ( 'play' , this . listener_ ) ;
102
+ stop : function ( ) {
108
103
this . running_ = false ;
109
104
} ,
110
105
111
106
getCurrentImageData_ : function ( ) {
112
- console . log ( "GetCurrentImageData_ videoElements videoWith is ," , this . videoElement_ ) ;
113
- console . log ( "GetCurrentImageData_ videoElements videoHeightis ," , this . videoElement_ ) ;
114
- this . canvas_ . width = this . videoElement_ . videoWidth ;
115
- this . canvas_ . height = this . videoElement_ . videoHeight ;
116
-
117
- if ( this . canvas_ . width == 0 || this . canvas_ . height == 0 ) {
118
- return ;
119
- }
120
-
121
- var context = this . canvas_ . getContext ( '2d' ) ;
122
- context . drawImage ( this . videoElement_ , 0 , 0 , this . canvas_ . width ,
123
- this . canvas_ . height ) ;
124
- return context . getImageData ( 0 , 0 , this . canvas_ . width , this . canvas_ . height ) ;
107
+ return new Promise ( ( resolve , reject ) => {
108
+ const track = this . stream_ . mediaStream . getVideoTracks ( ) [ 0 ] ;
109
+ let imageCapture = new ImageCapture ( track ) ;
110
+ var context = this . canvas_ . getContext ( '2d' ) ;
111
+ setTimeout ( ( ) => {
112
+ imageCapture . grabFrame ( ) . then ( ( imageBitmap ) => {
113
+ context . drawImage ( imageBitmap , 0 , 0 , 320 , 240 ) ;
114
+ resolve ( context )
115
+ } ) . catch ( ( err ) => {
116
+ console . log ( "err.name:" , err . name )
117
+ reject ( err )
118
+ } )
119
+ } , 200 )
120
+ } )
125
121
} ,
126
122
127
123
checkVideoFrame_ : function ( ) {
128
124
if ( ! this . running_ ) {
129
125
return ;
130
126
}
131
- if ( this . videoElement_ . ended ) {
132
- return ;
133
- }
134
- var imageData = this . getCurrentImageData_ ( ) ;
135
- if ( ! imageData ) {
136
- console . log ( "CureentImageData is " , imageData . data ) ;
137
- return ;
138
- }
139
- if ( this . isBlackFrame_ ( imageData . data , imageData . data . length ) ) {
140
- this . frameStats . numBlackFrames ++ ;
141
- }
142
- console . log ( "This.frameComparator.calculate(this.previousFrame_, imageData.data)" , this . frameComparator . calculate ( this . previousFrame_ , imageData . data ) ) ;
143
- console . log ( "This.differenceThreshold is " , this . differenceThreshold ) ;
144
- if ( this . frameComparator . calculate ( this . previousFrame_ , imageData . data ) >
145
- this . identicalFrameSsimThreshold ) {
146
- this . frameStats . numFrozenFrames ++ ;
147
- } else if ( ( this . frameComparator . calculate ( this . previousFrame_ , imageData . data ) == this . differenceThreshold ) && ( this . frameComparator . calculate ( this . previousFrame_ , imageData . data ) != 0 ) ) {
148
- this . frameStats . numFrozenFrames ++ ;
149
-
150
- }
151
- this . differenceThreshold = this . frameComparator . calculate ( this . previousFrame_ , imageData . data ) ;
152
- this . previousFrame_ = imageData . data ;
153
-
154
- this . frameStats . numFrames ++ ;
155
- setTimeout ( this . checkVideoFrame_ . bind ( this ) , 20 ) ;
127
+
128
+ return new Promise ( ( resolve , reject ) => {
129
+ this . getCurrentImageData_ ( ) . then ( ( context ) => {
130
+ var imageData = context . getImageData ( 0 , 0 , 320 , 240 ) ;
131
+ if ( ! imageData ) {
132
+ console . log ( "CureentImageData is " , imageData . data ) ;
133
+ } else {
134
+ if ( this . isBlackFrame_ ( imageData . data , imageData . data . length ) ) {
135
+ this . frameStats . numBlackFrames ++ ;
136
+ }
137
+ let currentCalculate = this . frameComparator . calculate ( this . previousFrame_ , imageData . data )
138
+ console . log ( "This.frameComparator.calculate(this.previousFrame_, imageData.data)" , currentCalculate ) ;
139
+ if ( currentCalculate > this . identicalFrameSsimThreshold ) {
140
+ this . frameStats . numFrozenFrames ++ ;
141
+ } else if ( ( currentCalculate == this . differenceThreshold ) && ( currentCalculate != 0 ) ) {
142
+ this . frameStats . numFrozenFrames ++ ;
143
+ }
144
+ this . differenceThreshold = currentCalculate ;
145
+ this . previousFrame_ = imageData . data ;
146
+
147
+ this . frameStats . numFrames ++ ;
148
+ }
149
+
150
+ setTimeout ( this . checkVideoFrame_ . bind ( this ) , 500 ) ;
151
+ resolve ( )
152
+ } ) . catch ( ( err ) => {
153
+ setTimeout ( this . checkVideoFrame_ . bind ( this ) , 500 ) ;
154
+ resolve ( )
155
+ } )
156
+ } )
156
157
} ,
157
158
158
159
isBlackFrame_ : function ( data , length ) {
0 commit comments