kyriesent / node-rtsp-stream

Stream any RTSP stream and output to websocket for consumption by jsmpeg (https://github.com/phoboslab/jsmpeg). HTML5 streaming video! Requires ffmpeg.
MIT License
451 stars 166 forks source link

How to use node-rtsp-stream with https connections? Since this package uses websockets internally, where do we mention configutations for "wss"? #37

Closed xabberd closed 4 years ago

cdian commented 5 years ago

Look in videoStream.js and do the following.

const server = https.createServer({ cert: fs.readFileSync('./cert/backend.foobar.com.public.pem'), key: fs.readFileSync('./cert/backend.foobar.com.private.pem'), }, function (req, res) { console.log(new Date() + ' ' + req.connection.remoteAddress + ' ' + req.method + ' ' + req.url); res.writeHead(200); res.end("hello foobarbackend\n"); });

this.wsServer = new ws.Server({ server })

You also have to adapt the VideoStream.prototype.stop Add i line where you stop the http server. this.wsServer.close() this.httpsserver.close(function () { console.log('HTTPS Server closed!'); }); this.stream.kill()

engelhardteric commented 5 years ago

my wss server working but i dont get frames with jsmpeg and html

kyriesent commented 5 years ago

Unfortunately this module isn't designed to use wss right now. If this grows as a request I will look into adding it. It would be great to have, but for now I'm not sure I can dig into it. Thanks for the suggestion though!

engelhardteric commented 5 years ago

Sorry for my late answer. I have use your code for wss and now its work with jsmpeg. Thanks for support. :)

alphabin commented 4 years ago

So does this mean this will only still work on http sites? Sorry for the confusion but I would love to learn how to get it going on https.

alphabin commented 4 years ago

Figured it out hopefully it can help another! The issue was solved using a wss with a slight modification.

Inspired by this

Wss implemetation

Sample index.js for node


var key = fs.readFileSync('../yoursite_com.key');
var cert = fs.readFileSync( '../yoursite_com.crt' );
var ca = fs.readFileSync( '../yoursite_com.csr' );

global.options = {
  key: key,
  cert: cert,
  ca: ca
  };

const app = express();
//Some routes here yada yada

var serverH = https.createServer(options, app);

//On a secure websocket request from the front end, this gets hit
serverH.on('upgrade', function upgrade(request, socket, head) {
  const pathname = url.parse(request.url).pathname;
//Check to make sure it is infact a correct wss request
if (pathname.includes('/socketSecure/')) 
{
    console.log("path :" + pathname)
    var splitPath = pathname.split('/');

    //Here I am parsing the request path to a UID that is going to help me get the ws connection
    //I have a dictionary of these active ws connections
    var reqKey = splitPath[2]+ splitPath[3];

    for (var key in  ClientSteams) {
      // check if the property/key is defined 
      if (ClientSteams.hasOwnProperty(key) && key == reqKey ) {                
            //This is the key, it finds the wsServer and then emits it to my current connection
            ClientSteams[key].wsServer.handleUpgrade(request, socket, head, function done(ws) {
                ClientSteams[key].wsServer.emit('connection', ws, request);
            });
      }}
  } else {
    socket.destroy();
  }
});

Also changed it up a little on the videoStream.js as well, what this is doing is making a Websocket Server without a port


VideoStream.prototype.pipeStreamToSocketServer = function() {
  this.wsServer = new ws.Server({
     noServer: true 
  })

With this approach, I can have many SECURE ws connections without getting any server errors.

On express route I have an express route that creates this ClientStreams in this way

async function StartRtspConnection(data, res){
  //Some code here
  //Yada Yada
    var stream = new Stream({
        name: 'camera-' + data.customerID + data.locationID,
        streamUrl: url
    });

    ClientSteams[data.customerID + data.locationID] = stream;

    res.status(200).end();
}
kyriesent commented 4 years ago

@alphabin Thanks so much! This is great! Closing issue for now.

mmeyers-solartech commented 4 years ago

I'm still having some major issues getting this to work. The provided instructions detailed aren't really clear on where things are supposed to go. I've generated certs but cannot get the first reply to function, the server just instantly closes.

Mathivanan1803 commented 2 years ago

I am facing error when I try to change the RTSP URL dynamically in node-rtsp-stream, can anyone help me to fix this.

my error is look like this:


TypeError: stream is not a constructor
    at D:\Ayonix_Dev_New\ayonix-access-control-nodejs\routes\RTSPLinks.js:48:14
    at Layer.handle [as handle_request] (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\layer.js:95:5)
    at D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:174:3)
    at router (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:47:12)

and my post API is :


RTSPRouter.post('/getPreview', (req, res) => {
    console.log("Inside stream>>>>", stream.JSON);
    // stream.mpeg1Muxer.kill();
    stream = new stream({
        name: 'name',
        streamUrl: req.body.RTSPURL,
        wsPort: 9999,
        ffmpegOptions: {
            '-r': 30
        }
    })
    // stream.mpeg1Muxer.kill()
    `res.send(stream)`
})