Envek / obs-studio-node-example

Learn how to use OBS Studio from your Electron app for screen video recording
GNU General Public License v2.0
105 stars 19 forks source link

Camera results in infinite loop when physical camera is off #10

Closed hrueger closed 4 years ago

hrueger commented 4 years ago

I have a webcam, which is actually a DSLR with some kind of capture card. When the DSLR is off, the capture card is shown in Windows as a Webcam, but in OBS its with and height are 0.

The example app detects it as a camera input, but then gets stuck in an infinite loop waiting for the camera width to be other than 0, which does never happen.

Maybe we need another way to check for the initialization of the camera?

Envek commented 4 years ago

Ouch. My laptop webcam is reports its width and height as 0 for a fraction of a second, while it is being initialized, I suppose.

I wasn't able to quickly find a proper way to wait for its readiness.

Envek commented 4 years ago

For now I've just limited waiting time to 1 second. If camera is still reporting its width as zero, then just skip it and don't use.

If you will find more proper way to handle this, feel free to open pull request.

Thanks for reporting!

yles9056 commented 3 years ago

In case anyone need to get the size of a webcam without waiting: The original size of a webcam is equal to its current resolution. When a webcam input source is created, it uses its default resolution.

You can use cameraSource.properties.get("resolution").value to read the resolution value. However, if cameraSource.properties.get("res_type").value is 0, cameraSource.properties.get("resolution").value will be empty. Set res_type to 1 to read the resolution.

// Create a webcam input
let cameraSource = osn.InputFactory.create("dshow_input", "video", { video_device_id: "[device id of the webcam]" });

// Set res_type to 1
let settings = cameraSource.settings;
settings ["res_type"] = 1;
cameraSource.update(settings);
// res_type = 0 : Device Default
// res_type = 1 : Custom

// resolutionStr should be "[width]x[height]". For example: "1280x720"
let resolutionStr = cameraSource.properties.get("resolution").value;
let resolution = resolutionStr.split("x");
let cameraWidth = Number(resolution[0]);
let cameraHeight = Number(resolution[1]);