Skip to content

Taking images from the camera in Thread(worker) #21162

@moondayyy

Description

@moondayyy

I can access the document object with javascript codes in em_asm without a thread, but I cannot access it when I open a thread (i.e. worker).
In this link, it says that I cannot access the "window, document, parent" objects.

What I want to do is to open an infinite loop in the thread and use setInterval?
The video element is taking images from the camera.
Is there an alternative solution?
How can I get an image from the camera in a thread?

CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(emscripten_global_variable_test)
set(CMAKE_CXX_STANDARD 17)
add_executable(emscripten_global_variable_test main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "--bind -sUSE_PTHREADS -sPTHREAD_POOL_SIZE=5 -s TOTAL_MEMORY=256MB -s ALLOW_TABLE_GROWTH=1 -s NO_EXIT_RUNTIME=1 -s ALLOW_MEMORY_GROWTH=1 -O0 -s EXPORT_ALL=1 -s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap']")

main.cpp:

#include <iostream>
#include "emscripten.h"
#include "emscripten/val.h"
#include "thread"

extern "C" EMSCRIPTEN_KEEPALIVE void Test(){
    // directly document not working!
   auto k= emscripten::val::global("document");
    std::thread([&k](){
        EM_ASM({
                   var video= Module.emval_get_global().document.createElement("video");
                   video.setAttribute("id", "videoInput");
                   Module.emval_get_global().document.body.appendChild(video);
               });

        EM_ASM({
                   console.log(Module.emval_get_global().document.querySelector("#videoInput"));
               });

    }).detach();
    std::cout<<"Test"<<std::endl;
}

Calling Module._Test()

This error message:
worker.js onmessage() captured an uncaught exception: TypeError: Cannot read properties of undefined (reading 'createElement')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions