2

I have an interesting problem, in short: how to share information between threads in NodeJS (12+).

The tech stack - in short also:

  • A remote/online streaming server, what producing an MP4 live stream
  • A client application what only consumes live view through RTSP over HTTP
  • A small NodeJS based application to get the MP4, transform it and pipe it back to the client.

.

The modules what I use:

  • NodeJS 12+
  • Request/fetch/https module
  • Express module
  • Stream module

The story: I have an application, what has a gateway/relay role between two different system. One provide a live media stream (simple MP4(h264) stream) and another one supposed to consume it as RTSP over HTTP. The weird part is, the consumer client does not behave like any other player (like VLC or a webplayer), sometime - seemingly randomly - resend the request, sometime close the current request and resend it. So direct pipe not really working for this use-case.

I made a worker (from worker_threads), what hold a readable stream object, and when the client hit the request, I start populate the MP4 stream into the readable object in the worker, so even if the stream is does get a close or resend, it will not break the live media stream consuming process. And wherever the client connect, I just would like to pipe the readable object for it.

Originally, I though a simple pipe from like request/fetch/http.get or FFMPEG would be enough, but the client could call the call between 3 seconds and 2 minutes.

.

So, my questions are, what could be the best solution, to pass back the data from the worker to the main and let reach the HTTP routing?

I had some idea like:

  • I know, I can have my own channel between the threads and can pass back-and-forth information, but waiting for message and keep up the process does block the app, as far as I know (worker.on('message', (stuff) => {});).
  • Using Socket.io to pass data back from the worker, populate the readable in the main, and pipe the readable at http level (fake shared object basically)
  • Creating a secondary http server what offer the media stream, then i will just relay this into the response (e.g.: gatewaying/proxying)
  • Looking up some proxy solution where I can just simply redirect and reshape thing, like the input mp4 transforms into RTSP stream and pipe it to the consumer response
  • Should I just "remember" to the active stream, and if its streamed by the remote server, always just using the same url, passing to FFMPEG and continue piping to the res?

Note:

  • I setted up all the headers to keep alive the connection, but seems the client software act as-is.
  • By default its using RTSP and RTP/TCP to consume video stream, but has option for RTSP over http.
  • Probably I overlook some trivial task for serving RTSP video from a remote live MP4, but I did not found any good example or source anywhere (everywhere the same 3 article re-shared basically)
  • I did not found any similar question, nor article anywhere (but checked out the nodejs ffmpeg play video at specific time and stream it to client).
huncyrus
  • 648
  • 8
  • 18

0 Answers0