-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstream.js
More file actions
60 lines (54 loc) · 1.94 KB
/
stream.js
File metadata and controls
60 lines (54 loc) · 1.94 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
var fs = require('fs');
var util = require('util');
var http = require('http');
var path = require('path');
if (process.argv.length != 6) {
console.log('Require the following command line arguments:' +
' external_host external_port internal_port webm_file');
console.log(' e.g. myserver.mydomain.com 50001 10000 record.webm');
process.exit();
}
var external_host = process.argv[2];
var external_port = process.argv[3];
var internal_port = process.argv[4];
var fileName = process.argv[5];
http.createServer(function (req, res) {
if (req.url != "/movie.webm") {
res.writeHead(200, { "Content-Type": "text/html" });
res.end('<video src="http://'+external_host+':'+external_port+'/movie.webm" controls></video>');
} else {
var file = path.resolve(__dirname, fileName);
fs.stat(file, function(err, stats) {
if (err) {
if (err.code === 'ENOENT') {
// 404 Error if file not found
res.writeHead(404, {'Content-Type': 'text/plain'});
//return res.end(err);
}
return res.end(JSON.stringify(err));
}
var range = req.headers.range;
if (!range) {
// 416 Wrong range
return res.sendStatus(416);
}
var positions = range.replace(/bytes=/, "").split("-");
var start = parseInt(positions[0], 10);
var total = stats.size;
var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
var chunksize = (end - start) + 1;
res.writeHead(206, {
"Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "video/mp4"
});
var stream = fs.createReadStream(file, { start: start, end: end })
.on("open", function() {
stream.pipe(res);
}).on("error", function(err) {
res.end(err);
});
});
}
}).listen(internal_port);