Skip to content

Commit 7cce8ad

Browse files
committed
Treat a webcam as a single source
1 parent e4c89e8 commit 7cce8ad

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

core/src/TethrManager.ts

+8-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type TethrManagerEvents = {
99
}
1010
export class TethrManager extends EventEmitter<TethrManagerEvents> {
1111
#ptpusbCameras: Map<USBDevice, TethrPTPUSB> = new Map()
12-
#webcamCameras: Map<string, TethrWebcam> = new Map()
12+
#webcam: TethrWebcam | null = null
1313

1414
constructor() {
1515
super()
@@ -70,34 +70,21 @@ export class TethrManager extends EventEmitter<TethrManagerEvents> {
7070
async #refreshPairedWebcam(): Promise<TethrWebcam | null> {
7171
const devices = await this.enumerateWebcamDeviceInfo()
7272

73-
let camera: TethrWebcam | null = null
74-
75-
const prevCameras = this.#webcamCameras
76-
77-
this.#webcamCameras = new Map()
78-
79-
for (const device of devices) {
80-
if (device.kind !== 'videoinput' || device.deviceId === '') {
81-
continue
82-
}
83-
84-
const prevCamera = prevCameras.get(device.deviceId)
73+
const videoDevices = devices.filter(
74+
device => device.kind === 'videoinput' && device.deviceId !== ''
75+
)
8576

86-
if (prevCamera) {
87-
this.#webcamCameras.set(device.deviceId, prevCamera)
88-
} else {
89-
camera = new TethrWebcam(device)
90-
this.#webcamCameras.set(device.deviceId, camera)
91-
}
77+
if (!this.#webcam && videoDevices.length > 0) {
78+
this.#webcam = new TethrWebcam()
9279
}
9380

94-
return camera ?? [...this.#webcamCameras.values()][0] ?? null
81+
return this.#webcam
9582
}
9683

9784
#emitPairedCameras() {
9885
const cameras = [
9986
...this.#ptpusbCameras.values(),
100-
...this.#webcamCameras.values(),
87+
...(this.#webcam ? [this.#webcam] : []),
10188
]
10289

10390
this.emit('pairedCameraChange', cameras)

core/src/TethrWebcam.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@ type CaptureHandler =
1414
}
1515

1616
export class TethrWebcam extends Tethr {
17-
#mediaDeviceInfo: MediaDeviceInfo
1817
#media: MediaStream | null = null
1918
#captureHandler: CaptureHandler | null = null
2019
#facingModeDict = new BiMap<string, string>()
2120

22-
constructor(mediaDeviceInfo: MediaDeviceInfo) {
21+
constructor() {
2322
super()
24-
this.#mediaDeviceInfo = mediaDeviceInfo
2523
}
2624

2725
async open() {
2826
try {
2927
this.#media = await navigator.mediaDevices.getUserMedia({
30-
video: {deviceId: this.#mediaDeviceInfo.deviceId},
28+
video: true,
3129
})
3230
} catch {
3331
throw new Error('No available webcam is connected')
@@ -88,7 +86,7 @@ export class TethrWebcam extends Tethr {
8886
}
8987

9088
get name() {
91-
return this.#mediaDeviceInfo.label
89+
return 'Webcam'
9290
}
9391

9492
// Configs

doc/src/App.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<ul class="webcam-list">
2929
<li v-for="(cam, i) in pairedCameras" :key="i">
3030
<button @click="onClickPairedCamera(cam)">
31-
[{{ cam.type }}] {{ cam.name }}
31+
{{ cam.name }}
3232
</button>
3333
</li>
3434
</ul>

0 commit comments

Comments
 (0)