emscripten-core / emscripten-core/emscripten

Taking images from the camera in Thread(worker)

Open
#21,162 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

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](https://web.dev/articles/workers-basics#toc-enviornment-features), 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
#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"<

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.