From 7cce8addb975c40139ec9ce9b177f2f6822d5cde Mon Sep 17 00:00:00 2001 From: Baku Hashimoto Date: Fri, 14 Jun 2024 01:04:27 +0900 Subject: [PATCH] Treat a webcam as a single source --- core/src/TethrManager.ts | 29 ++++++++--------------------- core/src/TethrWebcam.ts | 8 +++----- doc/src/App.vue | 2 +- 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/core/src/TethrManager.ts b/core/src/TethrManager.ts index e97017b..eca6005 100644 --- a/core/src/TethrManager.ts +++ b/core/src/TethrManager.ts @@ -9,7 +9,7 @@ type TethrManagerEvents = { } export class TethrManager extends EventEmitter { #ptpusbCameras: Map = new Map() - #webcamCameras: Map = new Map() + #webcam: TethrWebcam | null = null constructor() { super() @@ -70,34 +70,21 @@ export class TethrManager extends EventEmitter { async #refreshPairedWebcam(): Promise { const devices = await this.enumerateWebcamDeviceInfo() - let camera: TethrWebcam | null = null - - const prevCameras = this.#webcamCameras - - this.#webcamCameras = new Map() - - for (const device of devices) { - if (device.kind !== 'videoinput' || device.deviceId === '') { - continue - } - - const prevCamera = prevCameras.get(device.deviceId) + const videoDevices = devices.filter( + device => device.kind === 'videoinput' && device.deviceId !== '' + ) - if (prevCamera) { - this.#webcamCameras.set(device.deviceId, prevCamera) - } else { - camera = new TethrWebcam(device) - this.#webcamCameras.set(device.deviceId, camera) - } + if (!this.#webcam && videoDevices.length > 0) { + this.#webcam = new TethrWebcam() } - return camera ?? [...this.#webcamCameras.values()][0] ?? null + return this.#webcam } #emitPairedCameras() { const cameras = [ ...this.#ptpusbCameras.values(), - ...this.#webcamCameras.values(), + ...(this.#webcam ? [this.#webcam] : []), ] this.emit('pairedCameraChange', cameras) diff --git a/core/src/TethrWebcam.ts b/core/src/TethrWebcam.ts index f8da454..0a58b6a 100644 --- a/core/src/TethrWebcam.ts +++ b/core/src/TethrWebcam.ts @@ -14,20 +14,18 @@ type CaptureHandler = } export class TethrWebcam extends Tethr { - #mediaDeviceInfo: MediaDeviceInfo #media: MediaStream | null = null #captureHandler: CaptureHandler | null = null #facingModeDict = new BiMap() - constructor(mediaDeviceInfo: MediaDeviceInfo) { + constructor() { super() - this.#mediaDeviceInfo = mediaDeviceInfo } async open() { try { this.#media = await navigator.mediaDevices.getUserMedia({ - video: {deviceId: this.#mediaDeviceInfo.deviceId}, + video: true, }) } catch { throw new Error('No available webcam is connected') @@ -88,7 +86,7 @@ export class TethrWebcam extends Tethr { } get name() { - return this.#mediaDeviceInfo.label + return 'Webcam' } // Configs diff --git a/doc/src/App.vue b/doc/src/App.vue index def5cc7..7c46b08 100644 --- a/doc/src/App.vue +++ b/doc/src/App.vue @@ -28,7 +28,7 @@