-
Notifications
You must be signed in to change notification settings - Fork 22
/
input-worker.js
59 lines (55 loc) · 1.67 KB
/
input-worker.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
const { sendFrame, canSendFrame, addToQueue } = require('./send-data');
const uuid = require('uuid');
const spawn = require('threads').spawn;
const config = require('./config');
var stop = '';
var canStop = true;
var currentGUID;
const sendData = function (guid, resizedFrames, delay, index = 0) {
if (index >= resizedFrames.length) {
if (guid == currentGUID && !canStop) {
canStop = true;
}
index = 0;
}
sendFrame(guid, resizedFrames[index], delay, () => {
if (stop == guid) {
stop = '';
return;
}
sendData(guid, resizedFrames, delay, index + 1);
}, () => {
stop = guid;
return true;
}, () => canStop);
}
spawnThread = (input, data) => {
const thread = spawn('./inputs/' + input)
.send(data)
.on('progress', progress => {
console.log(`Processing ${input == 'gif-worker' ? 'GIF' : 'video'} [${data}]: ${progress}%`);
})
.on('message', message => {
if (message.err) {
canStop = true;
console.log('Failed to render GIF', err);
return;
}
// Send resized frames recursively
currentGUID = uuid.v1();
sendData(currentGUID, message.frames, message.delay);
})
.on('done', () => {
thread.kill();
})
}
module.exports = (input, data) => {
if (config.queueing && !canSendFrame()) {
addToQueue(input == 'gif-worker' ? 'GIF' : 'video', () => spawnThread(input, data));
return;
}
// Send URL to worker thread
canStop = false;
currentGUID = '';
spawnThread(input, data);
};