-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcvr.js
287 lines (247 loc) · 7.9 KB
/
cvr.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"use strict";
!function(Object, getPropertyDescriptor) {
// (C) WebReflection - Mit Style License
if (!(getPropertyDescriptor in Object)) {
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
Object[getPropertyDescriptor] = function getPropertyDescriptor(o, name) {
var proto = o,
descriptor;
while (proto && !(
descriptor = getOwnPropertyDescriptor(proto, name))) proto = proto.__proto__;
return descriptor;
};
}
}(Object, "getPropertyDescriptor");
//this has been implemented in chrome, but not yet shipped
HTMLCanvasElement.prototype.toBlob = HTMLCanvasElement.prototype.toBlob || HTMLCanvasElement.prototype.msToBlob;
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function(callback, type, quality) {
var bin = atob(this.toDataURL(type, quality).split(',')[1]),
len = bin.length,
len32 = len >> 2,
a8 = new Uint8Array(len),
a32 = new Uint32Array(a8.buffer, 0, len32);
for (var i = 0, j = 0; i < len32; i++) {
a32[i] = bin.charCodeAt(j++) |
bin.charCodeAt(j++) << 8 |
bin.charCodeAt(j++) << 16 |
bin.charCodeAt(j++) << 24;
}
var tailLength = len & 3;
while (tailLength--) {
a8[j] = bin.charCodeAt(j++);
}
setTimeout(function() {
callback(new Blob([a8], {
'type': type || 'image/png'
}));
}, 0);
}
});
}
var CVRCameraNotReadyyyyyyyyyException = function() {};
var CVR = function(canvas, name, fps, opts) {
opts = opts || {};
this.canvas = canvas;
this.port = opts.port || 22633;
this.ready = true;
this.fps = fps || 30;
this.seqNo = 0;
this.name = name;
this.chunkSize = opts.chunkSize || 0;
this.chunkNo = 0;
this.XHRpool = opts.XHRpool || 8;
this.pendingRequests = 0;
this.callback = null;
this.__sizeDescriptors = [];
this.spoofWindowSize = !!opts.spoofWindowSize;
this.onBlobReceived = this.onBlobReceived.bind(this);
this.lockSpoofedSize = this.lockSpoofedSize.bind(this);
if (opts.spoofWindowSize)
this.spoofSize(opts.spoofWindowSize[0], opts.spoofWindowSize[1]);
};
CVR.prototype.onFinish = function() {
this.ready = false;
if (this.spoofWindowSize)
this.stopSpoofingSize();
};
CVR.prototype.snap = function(cb, update) {
if (!this.ready) throw new CVRCameraNotReadyyyyyyyyyException();
this.callback = cb;
var _this = this;
if (this.chunkSize && this.seqNo === this.chunkSize) {
if (this.pendingRequests) return this.onXHRend = function() {
_this.snap(cb, update);
}
return this.finish(function() {
_this.chunkNo++;
_this.seqNo = 0;
_this.snap(cb, update);
}, true);
}
this.ready = false;
if (update) update();
canvas.toBlob(this.onBlobReceived, 'image/png');
};
CVR.prototype.onBlobReceived = function(blob) {
var xhr,
_this = this;
if (this.pendingRequests < this.XHRpool) {
this.pendingRequests++;
xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:' + this.port + '/?name=' + encodeURIComponent(this.name) + '&seq=' + this.seqNo, true);
xhr.onload = function(res) {
_this.pendingRequests--;
if (_this.onXHRend) {
var cb = _this.onXHRend;
_this.onXHRend = null;
cb();
}
};
xhr.send(blob);
_this.ready = true;
_this.seqNo++;
_this.callback && _this.callback();
} else this.onXHRend = function() {
_this.onBlobReceived(blob);
}
};
CVR.prototype.finish = function(cb, chunk) {
this.ready = false;
var xhr = new XMLHttpRequest(),
_this = this;
xhr.open('GET', 'http://localhost:' + this.port + '/?name=' + encodeURIComponent(this.name) + '&fps=' + this.fps + '&finish=1' + (this.chunkSize ? '&chunk=' + this.chunkNo : ''), true);
xhr.onload = function(res) {
_this.ready = true;
if (!chunk && _this.chunkSize)
return _this._join(cb);
if (!chunk)
_this.onFinish();
if (cb) cb();
};
xhr.send(null);
};
CVR.prototype._join = function(cb) {
this.ready = false;
var xhr = new XMLHttpRequest(),
_this = this;
xhr.open('GET', 'http://localhost:' + this.port + '/?name=' + encodeURIComponent(this.name) + '&join=1', true);
xhr.onload = function(res) {
_this.onFinish();
if (cb) cb();
};
xhr.send(null);
};
CVR.prototype.snapFixed = function(tick, frames, finishCb, dontSpoofTime) {
var snapped = 0,
_this = this;
var elapsedTime = 0,
timeStart;
if (!dontSpoofTime) timeStart = MTC.toggleSpoofing(true);
this.stopRecording = function() {
frames = 0;
};
var func = function() {
if (snapped++ < frames) {
elapsedTime += 1000 / _this.fps;
if (!dontSpoofTime)
MTC.setTime(timeStart + elapsedTime);
tick();
_this.snap(func, tick);
} else _this.finish(onFinish);
};
func();
var onFinish = function()
{
if (!dontSpoofTime) MTC.toggleSpoofing(false);
finishCb && finishCb();
};
};
CVR.prototype.recordFrames = function(opt) {
if (!opt) opt = {};
var snapped = 0;
var elapsedTime = 0,
timeStart;
var oldRAF = requestAnimationFrame;
var self = this;
this.stopRecording = function() {
opt.frames = -1;
};
if (!opt.dontSpoofTime) timeStart = MTC.toggleSpoofing(true);
var called = false,
nfCalled = false;
window.requestAnimationFrame = function(x) {
if (!called && (!opt.func || opt.func === x)) {
if (!opt.func) opt.func = x;
called = true;
oldRAF(onNextFrame);
}
};
var onNextFrame = function(first) {
if ((opt.frames && snapped++ >= opt.frames) || (opt.time && elapsedTime / 1000 >= opt.time))
self.finish(cleanUp);
else {
var wasFirst = !nfCalled;
nfCalled = true;
elapsedTime += 1000 / self.fps;
if (!opt.dontSpoofTime)
MTC.setTime(timeStart + elapsedTime);
self.snap(onNextFrame, !wasFirst ? opt.func : null);
}
};
var cleanUp = function() {
window.requestAnimationFrame = oldRAF;
MTC.toggleSpoofing(false);
if (called)
requestAnimationFrame(opt.func);
if (opt.onFinish)
opt.onFinish();
}
};
CVR.prototype.spoofSize = function(x, y) {
for (var ok of[[window, 'inner'], [document.documentElement, 'client'], [document.body, 'client'], [document.body, 'offset']]) {
this.__sizeDescriptors.push(ok[0], ok[1] + 'Width', Object.getPropertyDescriptor(ok[0], ok[1] + 'Width'));
this.__sizeDescriptors.push(ok[0], ok[1] + 'Height', Object.getPropertyDescriptor(ok[0], ok[1] + 'Height'));
Object.defineProperty(ok[0], ok[1] + 'Width', {
value: x,
enumerable: true,
configurable: true
});
Object.defineProperty(ok[0], ok[1] + 'Height', {
value: y,
enumerable: true,
configurable: true
});
}
window.addEventListener('resize', this.lockSpoofedSize, false);
var evt = document.createEvent('UIEvents');
evt.initUIEvent('resize', true, false, window, 0);
window.dispatchEvent(evt);
};
CVR.__stopImmediatePropagation = function(e) {
e.stopImmediatePropagation();
};
CVR.prototype.lockSpoofedSize = function() {
window.removeEventListener('resize', this.lockSpoofedSize, false);
window.addEventListener('resize', CVR.__stopImmediatePropagation, true);
};
CVR.prototype.stopSpoofingSize = function() {
window.removeEventListener('resize', CVR.__stopImmediatePropagation, true);
for (var t = 0; t < this.__sizeDescriptors.length; t += 3)
Object.defineProperty(this.__sizeDescriptors[t], this.__sizeDescriptors[t + 1], this.__sizeDescriptors[t + 2]);
for (var type of['Width', 'Height']) {
Object.defineProperty(window, 'inner' + type, {
get: (function(type) {
return function() {
return document.documentElement['client' + type];
}
})(type),
enumerable: true,
configurable: true
});
}
var evt = document.createEvent('UIEvents');
evt.initUIEvent('resize', true, false, window, 0);
window.dispatchEvent(evt);
};