Created
July 16, 2017 23:03
-
-
Save AttilaSATAN/fe0c60a53e9cbf498ded081fac886702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const http = require('http'); | |
const WebSocket = require('ws'); | |
const child_process = require('child_process'); | |
const WSPORT = 8082; | |
const CAM1URI = 'rtsp://78.186.127.222:554/11'; | |
const CAM2URI = 'rtsp://78.186.127.222:555/11'; | |
const CAM3URI = 'rtsp://78.186.127.222:556/11'; | |
const ARGS = ['-rtsp_transport', 'tcp', '-i', 'rtsp://78.186.127.222:555/11', '-f', | |
'mpegts', '-codec:v', 'mpeg1video', '-s', '640x480', '-b:v', '1000k', '-bf', '0', '-' | |
]; | |
// http://localhost:8081/asd | |
var stream = child_process.spawn("ffmpeg", ARGS, { | |
detached: false | |
}); | |
stream.stdout.on('data', function (data) { | |
return broadcast(data); | |
}); | |
stream.stderr.on('data', function (data) { | |
//return emit('ffmpegError', data); | |
}); | |
var socketServer = new WebSocket.Server({ | |
port: WSPORT, | |
perMessageDeflate: false | |
}); | |
socketServer.connectionCount = 0; | |
socketServer.on('connection', function (socket, upgradeReq) { | |
socketServer.connectionCount++; | |
console.log( | |
'New WebSocket Connection: ', | |
(upgradeReq || socket.upgradeReq).socket.remoteAddress, | |
(upgradeReq || socket.upgradeReq).headers['user-agent'], | |
'(' + socketServer.connectionCount + ' total)' | |
); | |
socket.on('close', function (code, message) { | |
socketServer.connectionCount--; | |
console.log( | |
'Disconnected WebSocket (' + socketServer.connectionCount + ' total)' | |
); | |
}); | |
}); | |
var broadcast = function (data) { | |
socketServer.clients.forEach(function each(client) { | |
if (client.readyState === WebSocket.OPEN) { | |
try { | |
client.send(data); | |
} catch (e) { | |
console.error('Send fail.') | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment