webRTC-to-stream/server.js

147 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-05-28 18:33:58 +00:00
const path = require('path');
const url = require('url');
const express = require('express');
const fs = require('fs');
const http = require('http');
const io = require('socket.io');
const app = express();
2018-05-29 12:24:14 +00:00
const agent = require('agentkeepalive');
const request = require('request');
2018-05-28 18:33:58 +00:00
const port = 8443;
const server = http.createServer(app).listen(port, function() {
console.log('Open http://localhost:' + port + '/ with a browser');
});
2018-05-29 12:24:14 +00:00
// TEST PROMISE
const readFile = (path, opts = 'utf8') =>
new Promise((res, rej) => {
fs.readFile(path, opts, (err, data) => {
if (err) rej(err)
else res(data)
})
})
2018-05-28 18:33:58 +00:00
// CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
app.use(express.static(path.join(__dirname, 'static')));
const socket = io(server);
2018-05-29 12:24:14 +00:00
let streamerId = '';
let streamOnline = false;
let req;
2018-05-28 18:33:58 +00:00
socket.on('connection', (socket) => {
console.log('Connected ' + socket.id);
socket.on('error', (error) => {
console.error('Connection ' + socket.id + ' error', error);
});
2018-05-29 12:24:14 +00:00
socket.on('disconnect',(data) => {
if(streamerId == socket.id){
console.log('Stream offline! ' + streamerId+ ' closed');
} else {
console.log('Connection ' + socket.id + ' closed');
}
2018-05-28 18:33:58 +00:00
});
2018-05-29 12:24:14 +00:00
socket.on('stream', (data) => {
streamerId = socket.id;
streamOnline = true;
console.log('Stream active! Session ' + streamerId);
initConnection();
2018-05-28 18:33:58 +00:00
});
2018-05-29 12:24:14 +00:00
const onNewChunk = () => {
new Promise((res, rej) => {
socket.on('blob', (data) => {
if(data){
res(data)
}
else {
rej(data);
}
});
)}
}
try {
while(true){
req.write(await readFile('new.webm'));
}
} catch(ex){
req.end();
}
2018-05-28 18:33:58 +00:00
});
2018-05-29 12:24:14 +00:00
const initConnection = async () => {
const options = {
method: 'PUT',
forever: true,
uri: 'http://ws.kurento.fishrungames.com:8000/'+ streamerId + '.webm',
headers: {
'Connection': 'keep-alive',
'content-type': 'video/webm',
'ice-Public': '1',
'ice-Name': 'Stream' + streamerId,
'ice-Description': 'Steam' + streamerId
},
auth: {
user: 'source',
password: 'frg$$frg'
},
};
req = request(options, (err, message, data) => {
if(err)
console.log(err)
//console.log(message);
//console.log(data);
});
}
2018-05-28 18:33:58 +00:00
app.get('/video', function(req, res) {
const path = 'new.webm'
const stat = fs.statSync(path)
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1]
? parseInt(parts[1], 10)
: fileSize-1
const chunksize = (end-start)+1;
const file = fs.createReadStream(path, {start, end});
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/webm',
}
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
'Accept-Ranges': 'bytes',
'Content-Length': fileSize,
'Content-Type': 'video/webm',
}
res.writeHead(200, head)
fs.createReadStream(path).pipe(res)
}
})