3

I need to decode h264 data at browser side for that I am using openh264 library build in web Assembly using emscripten. I have build it successfully and tried to use it in java script to decode the h264 data. But I am getting one error for following line,

var open_decoder = Module.cwrap('open_decoder', 'number', null);

Error is: Uncaught TypeError: Module.cwrap is not a function

If anyone has has build openh264 with emscripten please help me to figure out issue.

Following steps I have used to build openh264 with emscripten.

  1. $ source emsdk_env.sh
  2. $./emsdk activate latest
  3. cd openh264-js-master
  4. make

Note : The code for openh264 has been downloaded from github( ttyridal) and already has make file with emscripten competent.

Kuldeep More
  • 138
  • 3
  • 14

2 Answers2

6
-s EXTRA_EXPORTED_RUNTIME_METHODS=["cwrap"]

Include above in command line while compiling your source

emcc source.c -s EXPORTED_FUNCTIONS=['_my_add'] -s EXTRA_EXPORTED_RUNTIME_METHODS=["cwrap"]
Anil8753
  • 2,663
  • 4
  • 29
  • 40
3

Probably you are trying to use Module before the Emscripten runtime has been initialized, so Module.cwrap is undefined.

To make sure the runtime is ready, place your code inside of Module.onRuntimeInitialized, as in the following example:

<!doctype html>
<html>
<body>
    <script>
        var Module = {
          onRuntimeInitialized: function() {
            my_add = Module.cwrap('my_add', 'number', ['number', 'number'])
            alert('1 + 2 = ' + my_add(1, 2));
          },
        };
    </script>
    <script async type="text/javascript" src="index.js"></script>
</body>
</html>

See full example in this github repo

Adi Levin
  • 5,165
  • 1
  • 17
  • 26
  • 1
    I done some R&D on this issue and I think that the issue is not at the java script side. I think the make file command for emscripten having some issue. The make file command is like "em++ -O1 $(TOTALMEMORY) -o $@ $^ -s EXPORTED_FUNCTIONS=$(h264symbols) -s -Iopenh264/codec/api/svc/ ". So after some R&D on this I found out that you need to add "EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' option. I have edited cmd with this option and now am getting error like TypeError: Module.asm._open_decoder is undefined. – Kuldeep More May 09 '19 at 07:58
  • From the emscripten docs: cwrap does not actually call compiled code (only calling the wrapper it returns does that). That means that it is safe to call cwrap early, before the runtime is fully initialized (but calling the returned wrapped function must wait for the runtime, of course, like calling compiled code in general). https://emscripten.org/docs/api_reference/preamble.js.html?highlight=cwrap#cwrap – Antonis Christofides Feb 03 '21 at 17:10