Skip to content

Commit

Permalink
Treat a webcam as a single source
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Jun 13, 2024
1 parent e4c89e8 commit 7cce8ad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
29 changes: 8 additions & 21 deletions core/src/TethrManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type TethrManagerEvents = {
}
export class TethrManager extends EventEmitter<TethrManagerEvents> {
#ptpusbCameras: Map<USBDevice, TethrPTPUSB> = new Map()
#webcamCameras: Map<string, TethrWebcam> = new Map()
#webcam: TethrWebcam | null = null

constructor() {
super()
Expand Down Expand Up @@ -70,34 +70,21 @@ export class TethrManager extends EventEmitter<TethrManagerEvents> {
async #refreshPairedWebcam(): Promise<TethrWebcam | null> {
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)
Expand Down
8 changes: 3 additions & 5 deletions core/src/TethrWebcam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@ type CaptureHandler =
}

export class TethrWebcam extends Tethr {
#mediaDeviceInfo: MediaDeviceInfo
#media: MediaStream | null = null
#captureHandler: CaptureHandler | null = null
#facingModeDict = new BiMap<string, string>()

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')
Expand Down Expand Up @@ -88,7 +86,7 @@ export class TethrWebcam extends Tethr {
}

get name() {
return this.#mediaDeviceInfo.label
return 'Webcam'
}

// Configs
Expand Down
2 changes: 1 addition & 1 deletion doc/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<ul class="webcam-list">
<li v-for="(cam, i) in pairedCameras" :key="i">
<button @click="onClickPairedCamera(cam)">
[{{ cam.type }}] {{ cam.name }}
{{ cam.name }}
</button>
</li>
</ul>
Expand Down

0 comments on commit 7cce8ad

Please sign in to comment.