node.jselectronnode-gypnode-addon-apin-api

Node addon don't work in electron, but worked in nodejs


I wrote a nodejs addon, compiled with node-gyp. It won't work on electron, but nodejs worked. The nodejs and electron node has the same version.

The addon do these things:

  1. Load ffmpeg static library and open a rtsp or local file.
  2. Convert the frame to rgba color to arraybuffer and call to electron's main process.
  3. The renderer process handle the data event and render the data to the canvas element.

In electron, the follow codes always return Protol not found

    int status = avformat_open_input(&pFormatContext, url, NULL, NULL);
    if (0 != status) {
        av_log(NULL, AV_LOG_ERROR, "ffmpeg open error: %s\n", av_err2str(status));
        return status;
    }

The node-gyp configurations:

{
    "targets": [{
        "target_name": "ffmpeg",
        "sources": ["src/ffmpeg/api/addon.c", "src/ffmpeg/api/ffmpeg.c"],
        "include_dirs": [
            "/home/my/ffmpeg_build/include"
        ],
        "libraries": [
            "-L$$PWD/../lib/ffmpeg-kylinux-aarch64",
            "-lavformat",
            "-lavcodec",
            "-lavutil",
            "-lswscale",
            "-lswresample",
            "-lx264",
            "-lx265"
        ]
    }]
}

Solution

  • Electron already includes ffmpeg (unlike stock Node.js) leaving you no other choice but to link with the included version - otherwise you will get symbol clashes and weird behavior - which is your case - because some symbols will be resolved to your version, others to the built-in version.

    Maybe there is a possible workaround which is to build ffmpeg statically into your addon.