k-yle / rtsp-relay

📽 View an RTSP stream in your web browser using an express.js server
https://npm.im/rtsp-relay
MIT License
312 stars 58 forks source link

Web socket does not work in build for electron #101

Open na-613c opened 2 years ago

na-613c commented 2 years ago

I have a project on electron + react js + local node js server (use rtsp-relay to convert RTSP stream to web socket). In the dev version it works, in the prod version the web socket does not work (the server is working, it was processing a get request)

I'm sorry for the bad english ☺

k-yle commented 2 years ago

Hi, do you have a example of this bug, perhaps a basic repository where I can test this? It's difficult to help without seeing any code.

na-613c commented 2 years ago

Hi. https://github.com/na-613c/web-rtsp-relay-electron - repo. In electron you can write mainWindow.webContents.openDevTools() (35 line) for open dev tools.

na-613c commented 2 years ago

There is a memory leak, if you run a lot of streams in good quality, then you can see in the task dispatcher how RAM is spent over time.

k-yle commented 2 years ago

Is there any way of opening the dev tools in production? Pretty difficult to debug the issue otherwise

na-613c commented 2 years ago

I just started learning electron. I don't know how to get the logs from the local server. :disappointed: p.s. added devtools (similar to browser) in prod.

k-yle commented 2 years ago

for some reason mainWindow.webContents.openDevTools() seems to have no effect in production.

Perhaps the issue is that in production, you're serving the react app from a file:// url, rather than from localhost...

na-613c commented 2 years ago

You can delete process.env.NODE_ENV === 'development' in process.env.NODE_ENV === 'development' && mainWindow.webContents.openDevTools();
or download last version https://github.com/na-613c/web-rtsp-relay-electron/tree/2cb2e67f3beaa6121deb22328cd5ab07beadcab0.

Or something I didn't understand

evalor commented 1 year ago

This may be caused by '@ffmpeg-installer/ffmpeg' package, It's a known issue that Asar breaks native paths. As a workaround, if you use Asar, you can do something like this:

const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path.replace('app.asar', 'app.asar.unpacked');

Since the relevant code is in this package, I cannot change it. Look here, I can only use translation tools to reply this issues. Forgive me for my poor English.

k-yle commented 1 year ago

Thanks for the suggestion @evalor, are you experiencing the same issue? I just tried the reproduction example from @na-613c (https://github.com/na-613c/web-rtsp-relay-electron), and it seems to work fine in production. Not sure why it didn't work 10 months ago...

evalor commented 1 year ago

I use vite+vue3 to build electron application and encounter the same problem.

I try to change the following code( in index.js line 42 ):

    this.stream = spawn(
      ffmpegPath.replace('app.asar', 'app.asar.unpacked'),  // [!!!] It's changed here
      [
        ...(transport ? ['-rtsp_transport', transport] : []), // this must come before `-i [url]`, see #82
        '-i',
        url,
        '-f', // force format
        'mpegts',
        '-codec:v', // specify video codec (MPEG1 required for jsmpeg)
        'mpeg1video',
        '-r',
        '30', // 30 fps. any lower and the client can't decode it
        ...additionalFlags,
        '-',
      ],
      { detached: false, windowsHide },
    );

Now my application can work after it is built. Due to my busy work, I can't follow up this problem immediately. Later, I will try to provide a "minimal" problem recurrence code to help solve this problem.

By the way, you can enable the electron menu to open the developer tools. Please do not hide the menu bar. After starting the application, click 'View - > toggle developer tools' in the menu bar, the developer tool will open.

evalor commented 1 year ago

Potential memory leaks may come from jsmpeg, on large (4K) RSTP streams, memory stack growth can be significantly observed, and applications will soon crash due to memory exhaustion. When I destroy instances of jsmpeg and recreate them every 10 minutes, this situation is suppressed. I'm trying to trace the source of memory leaks.