-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
230 lines (202 loc) · 6.62 KB
/
app.js
File metadata and controls
230 lines (202 loc) · 6.62 KB
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
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const fs = require('fs');
const si = require('systeminformation');
const FfmpegCmd = require('./src/ffmpeg/ffmpeg.js');
const Messages = require('./src/messages.js')
// Create http and ws server
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
// load configuration
var config = {};
if(process.argv.length <= 2){
console.log("Using default config");
config.working_dir="./";
config.port=7000;
config.address="0.0.0.0";
}else{
let confPath = process.argv[2];
try{
var confRaw = fs.readFileSync(confPath);
config = JSON.parse(confRaw);
}catch(error){
console.error("Unable to load configuration: ",error);
process.exit();
}
}
var workDir = config.working_dir;
console.log("Using working directory: ",workDir)
// Return all ffmpeg infos on this computer
app.get('/ffmpeg_infos', function (req, res) {
var ffmpegcmd = FfmpegCmd();
ffmpegcmd.getInfos().then( (result) => {
res.send(result);
})
.catch( (err) => {
res.status(500);
res.send(err);
});
});
// Return hw infos on this computer
app.get('/hw_infos', function (req, res) {
Promise.all([si.cpu(),si.graphics(),si.osInfo()]).then( (values) => {
var output = {};
output.cpu = values[0];
output.graphics = values[1].controllers;
output.os = values[2];
res.send(output);
})
.catch( (err) => {
res.status(500);
res.send(err);
});
});
// Return load infos on this computer
app.get('/load_infos', function (req, res) {
si.currentLoad().then( (values) => {
res.send(values);
})
.catch( (err) => {
res.status(500);
res.send(err);
});
});
// Return streams infos of file given in the url
app.get('/ffprobe/*', function (req, res) {
var filePath = req.params[0];
var ffmpegcmd = FfmpegCmd();
var file = "";
if(filePath[0] === '/'){
file = filePath;
}else{
file = workDir+"/"+filePath;
}
ffmpegcmd.ffprobe(file, function(err, metadata) {
if(err){
res.status(500);
res.send(err);
console.error("Failed to run ffprobe on ",file, " error: ",err);
}else{
res.send(metadata);
}
});
});
// Setup websocket to process ffmpeg commands
wss.on('connection', function connection(ws) {
console.log('New ws connection');
var ffmpegProcess = null;
var beforeStartingKillSignal = null; // handle kill signal received before the ffmpeg command
var beforeStartingNiceness = null; // handle priority request received before the ffmpeg command
ws.on('close', function close() {
if(ffmpegProcess != null){
ffmpegProcess.kill();
}
})
ws.on('message', function incoming(message) {
//console.log('Received %s', message);
//Parse command
var jsonContent;
try {
jsonContent = JSON.parse(message);
if(jsonContent.command === "ffmpeg" && !ffmpegProcess){
ffmpegProcess = new FfmpegCmd();
//build option
var options = {};
if(beforeStartingNiceness){
options.niceness = beforeStartingNiceness;
}else if(jsonContent.niceness){
options.niceness = jsonContent.niceness;
}else{
options.niceness = 0;
}
options.cwd = workDir,
options.captureStdout = false;
var minTimeBetweenProgressions = 0;
var lastProgression = null;
if(jsonContent.min_time_btw_progressions){ // in msec
minTimeBetweenProgressions = jsonContent.min_time_btw_progressions;
}
console.log("Launching command: ffmpeg "+jsonContent.args.join(" "));
ffmpegProcess
.on('progress', function(progression){
let currentTime = new Date().getTime()
if(lastProgression == null || (currentTime - lastProgression)>minTimeBetweenProgressions){
if(!progression.percent){
sendAsJson(ws,new Messages.StatusMsg(0,progression));
}else{
sendAsJson(ws,new Messages.StatusMsg(progression.percent,progression));
}
lastProgression = currentTime
}
})
.on('end', function(finalMessage){
sendAsJson(ws,new Messages.FinalMsg(0,finalMessage.message,finalMessage));
})
.on('error', function(finalMessage, stdout, stderr){
console.error("Failed to transcode command ",finalMessage.message, stderr)
sendAsJson(ws,new Messages.FinalMsg(1,finalMessage.message,""));
})
.exec(jsonContent.args, options);
if(beforeStartingKillSignal){
ffmpegProcess.kill(beforeStartingKillSignal);
}
}else if(jsonContent.command === "kill"){
if(ffmpegProcess == null){
beforeStartingKillSignal = jsonContent.signal;
}else{
ffmpegProcess.kill(jsonContent.signal);
}
}else if(jsonContent.command === "set_priority"){
//ffmpegProcess
if(ffmpegProcess == null){
beforeStartingNiceness = jsonContent.niceness;
}else{
ffmpegProcess.renice(jsonContent.niceness);
}
}else{
//Invalid command cancel process
if(ffmpegProcess != null){
ffmpegProcess.kill();
}
console.warn("Bad request invalid message: "+message);
sendAsJson(ws,new Messages.FinalMsg(400,"Bad request invalid message: "+message));
}
} catch (err) {
console.error("Error received",err,command);
sendAsJson(ws,new Messages.FinalMsg(500,"Unexpected error :"+err));
}
});
});
function sendAsJson(ws,msg){
//console.log('sending ',msg);
ws.send(JSON.stringify(msg), function ack(error) {
// If error is not defined, the send has been completed, otherwise the error
// object will indicate what failed.
if(typeof error !== 'undefined'){
//console.log('Socket error',error);
}
});
}
//// Web socket keep alive ////
// Heart beats to detect broken connections
function noop() {}
function heartbeat() {
this.isAlive = true;
}
wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.on('pong', heartbeat);
});
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping(noop);
});
}, 30000);
//Start the server
server.listen(config.port, config.address, function listening() {
console.log('Listening on %s:%d', server.address().address, server.address().port);
});