-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathframe-buffer.js
44 lines (40 loc) · 1.18 KB
/
frame-buffer.js
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
/*
* Authored by Runpeng Liu,
* Brain Power (2018)
*/
var FrameBuffer = function(params) {
var that = Object.create(FrameBuffer.prototype);
var MIN_BUFFER_SIZE = 20;
var frameBuffer = [];
var frameTimestamps = [];
var bufferSize = Math.max(MIN_BUFFER_SIZE, params.size);
that.bufferSize = bufferSize;
var startTimestamp;
var lastTimestamp;
that.addFrame = function(imgData) {
frameBuffer.push(imgData);
startTimestamp = startTimestamp || new Date().getTime();
lastTimestamp = new Date().getTime();
frameTimestamps.push(lastTimestamp);
};
that.clear = function() {
frameBuffer = [];
frameTimestamps = [];
startTimestamp = null;
};
that.shouldClear = function() {
return frameBuffer.length >= bufferSize;
};
that.getData = function() {
return {
frames: frameBuffer.slice(),
framerate: Math.round(1000 * frameBuffer.length / (lastTimestamp - startTimestamp)),
timestamps: frameTimestamps.slice()
}
};
that.getSize = function() {
return frameBuffer.length;
};
Object.freeze(that);
return that;
};