0

I'm working on a C code which interacts with hardware (an external USB device) and outputs video frames in realtime, which is eventually built as a DLL file:

int get_frame() {
    // interacts with hardware and
    // generates video frames
}

Are there specific functions to export in the DLL such that it can be used in Chrome as a video source, and if so, how to load this DLL into Chrome?

How to get this code recognized as a video source / webcam in Chrome, such that I can integrate it in a HTML page containing a <video> element?

navigator.mediaDevices.getUserMedia({ video: true }).then(stream => { $("#cam").srcObject = stream; });

enter image description here

Linked topics:

Writing a virtual webcam?, Virtual webcam input as byte stream.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • It's rather a question of how to make it recognizable by the operating system. – Konrad Sep 30 '22 at 16:40
  • @KonradLinkowski What is the condition for the browser to recognize a device as a video source device? – Basj Sep 30 '22 at 16:50
  • 1
    You need to create a webcam driver. See this. https://stackoverflow.com/questions/33693131/how-to-create-virtual-webcam-in-windows-10 Or install obs; it has virtual webcams. – O. Jones Oct 04 '22 at 12:56
  • @david-fong Do you know an SDK which can give a basic example? – Basj Oct 06 '22 at 09:34
  • @Basj It's not obvious what video format your `get_frame()` encodes into? Is it possible to set it to output H.264? If yes, that way you could just send the WASM output (h264 bytes) directly back into JS (to play as video via MSE). – VC.One Oct 09 '22 at 23:31
  • @VC.One Are you sure WASM can access to USB hardware devices (sensors)? I don't think so but maybe I'm wrong. – Basj Oct 10 '22 at 07:16
  • @Basj Why are you asking me that? Is that what the mysterious `// interacts with hardware` part is doing? It depends on how that part works. Or to put it another way... If this was a C-compiled EXE you were coding, what special codes does it have that you think cannot be processed by the C compiler of WASM? At the very least you just `include` your `.h` files and start running functions, right? Or is there something bigger going on that other C compilers (_eg:_ WASM) cannot handle?... Anyways fastest way to know is to make a simple minimum app in WASM that tests your sensor (gives a beep?) – VC.One Oct 10 '22 at 15:04
  • @Basj PS: Are you using Chrome? After connecting the USB device go to `chrome://usb-internals/` and choose `devices` then `inspect` (on your device) then find the yellow box icon with some arrow to expand for more details... expand any sub-boxes until you see `Class Code:` and if it **doesn't** say `blocked by web USB` you might get it to work as [USB directly within the browser](https://web.dev/usb/) (skipping the need for a DLL). The link shows how to communicate with an Arduino board. Don't know if that's same category/class as your USB sensor but it should be useful to you for ideas. – VC.One Oct 10 '22 at 15:23

2 Answers2

1

The standard instruction set / binary format for native-like code on browsers is WASM. Currently, one of the most well established tools for compiling C/C++ code to WASM is Emscripten. You can read more about how to compile OpenGL programs to WebGL using Emscripten on their dedicated doc page. I believe WebGL programs can be drawn to <canvas> elements (not <video> elements).

starball
  • 20,030
  • 7
  • 43
  • 238
  • Can WASM interact directly with hardware? (My C code interacts with hardware components) – Basj Oct 06 '22 at 09:52
  • You're going to need to be more specific than that. A CPU and GPU are both hardware and WASM allows instructing both. What hardware components do you want to interact with, and how are you doing it in C? If you want to know this, put it _inside_ your question body. – starball Oct 06 '22 at 16:12
  • You're right, WASM can access CPU and GPU, so my question was not precise enough ;) I mean an external USB device for example, for example a sensor. – Basj Oct 06 '22 at 16:22
  • 1
    I don't know off the top of my head, although I suspect the answer for WASM is no. There is the [experimental web JS API for USB](https://developer.mozilla.org/en-US/docs/Web/API/USB). Again, please edit this information into your question body and not into a comment section. Don't put your questions in comments, and don't try to ask too many questions in the same post. Your post will get closed for lacking focus. If you have many questions, make multiple question posts. – starball Oct 06 '22 at 16:25
0

If you have c code then you can canvert it to .wasm(web assembly) module and then that file can be used in web browsers as a cdn. After that you can see what other permissions require to access windows resources becasue brawsers cannot get windows resources.

Imran Afzal
  • 134
  • 1
  • 9