node.jsrtspnodejs-stream

Nodejs - RTSP live camera feed - distributed architecture


We have a requirement where we pass the camera details and need to show the live stream to the end-user in a web browser. As this is distributed architecture, so we can do it using the Rest API only or socket communication.

Technology stack

  1. Nodejs - at Backend
  2. Angular - at frontend

As part of the solution, I found npm - node-rtsp-stream - https://www.npmjs.com/package/node-rtsp-stream but problem is that we have to create the API and pass the stream in api, have at least 20-30 camera to see the live stream. Pass the port from the frontend and use it at the backend.

Problem -

I tried the below solution, but no luck

const Stream = require('node-rtsp-stream');
app.get('/camera/feed/:camera/:port', (req, res) => {
    console.log(req.params.port);
    if (req.params.camera == 1) {
        var ip_address2 = "192.168.1.12"
        var username2 = "admin";
        var password2 = "admin";
    } else if (req.params.camera == 2) {
        var ip_address2 = "192.168.1.10"
        var username2 = "admin";
        var password2 = "admin";
    }
    stream = new Stream({
        streamUrl: 'rtsp://' + username2 + ':' + password2 + '@' + ip_address2 + ':554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif',
        wsPort: req.params.port
    });
    res.send('OK');
});
// Stop the stream that produce using ffmpeg
app.get('/camera/feed/stop/:port', (req, res) => {
    stream.stop() // This is not make ffmpeg to stop the processing stream 
    res.send('OK');
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

index.html

var canvas3 = document.getElementById('canvas3');
var websocket = new WebSocket("ws://127.0.0.1:9999");
var player3 = new jsmpeg(websocket, { canvas: canvas3, autoplay: true, loop: true })

Thank you.


Solution

  • I found the solution. Passing the stream object in API response and this contains the PID res.send(stream); For the stop/kill the FFmpeg process we can use the PID from the frontend kill(req.params.pid);