From 2495d31d95f7e2672da012b3e28a48db851312d7 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 29 Sep 2021 14:12:56 -0700 Subject: [PATCH] Add ability to capture from webcam video source This allows for direct video capture from cheap capture cards that present as webcams to the OS (like mirabox) --- web/index.html | 17 ++++++++++++++++- web/index.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/web/index.html b/web/index.html index 37f9a6f..8145b63 100644 --- a/web/index.html +++ b/web/index.html @@ -275,7 +275,22 @@

Settings

- + +
diff --git a/web/index.js b/web/index.js index 597d710..4862a8c 100644 --- a/web/index.js +++ b/web/index.js @@ -23,6 +23,8 @@ const displayMediaOptions = { }, audio: false } + + let dialogWindow, autoModeTimer, croppedVideoTimer, currentText; let audioSources, audioDeviceIndex; @@ -73,6 +75,7 @@ const croppedVideoCanvas = document.getElementById("croppedVideoCanvas"); const croppedVideoCtx = croppedVideoCanvas.getContext("2d"); const settingsDialog = document.getElementById("settingsDialog"); const dialogCloseButton = document.getElementById("dialogCloseButton"); +const captureSelect = document.getElementById("captureSource"); init(); @@ -80,6 +83,7 @@ function init() { (async() => { loadProfiles(); loadAnki(); + getCaptureDevices(); })(); } @@ -87,6 +91,30 @@ function selectApplication() { startCapture(); } +async function getCaptureDevices() { + if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) { + console.log("enumerateDevices() not supported."); + return; + } + + captureSelect.options.length = 0; + navigator.mediaDevices.enumerateDevices().then(function(devices) { + devices.forEach(function(device) { + if (device.kind == "videoinput") { + const option = document.createElement('option'); + option.value = device.deviceId; + option.text = device.label || `source ${captureSelect.length +1}`; + captureSelect.appendChild(option) + } + }) + }); + +} + +function selectCaptureDevice() { + startDeviceCapture(); +} + async function startCapture(){ try { videoElement.srcObject = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions); @@ -98,6 +126,23 @@ async function startCapture(){ } } +async function startDeviceCapture(){ + const videoSource = captureSelect.value; + const userMediaOptions = { + video: {width: 1920, height: 1080, deviceId: videoSource ? {exact: videoSource} : undefined}, + audio: false + } + try { + videoElement.srcObject = await navigator.mediaDevices.getUserMedia(userMediaOptions); + getCaptureDevices(); + if (settingsDialog.open) { + closeSettings(); + } + }catch(err) { + console.error("Error" + err) + } +} + function videoOnLoad(element) { videoLoaded = true; resizeCanvas(element);