Set a callback function in val::object

16 views
Skip to first unread message

fidel espanto

unread,
Jul 2, 2024, 10:25:22 AM (18 hours ago) Jul 2
to emscripten-discuss
I'm quite new to emscripten, so please be patient with me :)

My goal is to create a wrapper class with an instance of a VideoDecoder object from WebCodecs API and pass some configurations inside it from C++.

 I'm trying to pass an object called 'init' on the global "VideoDecoder" object, which accepts a callback function on its members: output and error:

LVideoDecoderWebCodecs::LVideoDecoderWebCodecs()
{
    val VideoDecoder = val::global("VideoDecoder");

    if (!VideoDecoder.as<bool>()) {
        EM_ASM(
            console.error("WebCodecs API not available");
        );
    }

    val config = val::object();
    config.set("codec", val("vp8"));
    config.set("codedWidth", val(640));
    config.set("codedHeight", val(480));

    val init = val::object();
    init.set("output", [](val frame) {
        EM_ASM(
            console.log("Got frame: ", frame);
        );
    });

    init.set("error", [](val e) {
        EM_ASM(
            console.error("Error initializing decoder: ", e);
        );
    });

    val decoder = VideoDecoder.new_(init);
    decoder.call<void>("configure", config);
}

The module compiles successfully but when I run it on the browser it says,
wasmapi.js:4543 Uncaught BindingError: _emval_take_value has unknown type

It seems that I can't set a callback function on the init object.

Is there a solution to this? Or what can you suggest I handle this one.


Brendan Dahl

unread,
Jul 2, 2024, 2:47:41 PM (14 hours ago) Jul 2
to emscripten-discuss
Lambdas don't currently work with val. You could do something like this:

struct Init {
void output(val frame) {
EM_ASM(
console.log("Got frame: ", frame);
);
}
void error(val e) {
EM_ASM(
console.error("Error initializing decoder: ", e);
);
}
};

EMSCRIPTEN_BINDINGS(xxx) {
class_<Init>("Init")
.function("output", &Init::output)
.function("error", &Init::error)
;
}
....
// This needs to be deleted at some pointed...
Init* init = new Init();

val decoder = VideoDecoder.new_(init);

Reply all
Reply to author
Forward
0 new messages