Skip to content

Commit

Permalink
fix: fix autorestart of webcam camerastreamer (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteyou authored Sep 4, 2023
1 parent 1aa9f34 commit fd293d2
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/components/webcams/WebrtcCameraStreamer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
private remote_pc_id: string | null = null
private aspectRatio: null | number = null
private status: string = 'connecting'
private restartTimer: number | null = null
@Prop({ required: true }) readonly camSettings!: GuiWebcamStateWebcam
@Prop({ default: null }) declare readonly printerUrl: string | null
Expand Down Expand Up @@ -99,11 +100,15 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
)
this.pc.addEventListener('connectionstatechange', () => {
this.status = (this.pc?.connectionState ?? '').toString()
// clear restartTimer if it is set
if (this.restartTimer) window.clearTimeout(this.restartTimer)
if (['failed', 'disconnected'].includes(this.status)) {
setTimeout(async () => {
await this.pc?.close()
this.startStream()
}, 500)
// set restartTimer to restart stream after 5 seconds
this.restartTimer = window.setTimeout(() => {
this.restartStream()
}, 5000)
}
})
this.pc.addEventListener('icecandidate', (e) => {
Expand Down Expand Up @@ -147,8 +152,16 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
if (isFirefox) this.status = 'connected'
return response.json()
})
.catch(function (e) {
.catch((e) => {
window.console.error(e)
// clear restartTimer if it is set
if (this.restartTimer) window.clearTimeout(this.restartTimer)
// set restartTimer to restart stream after 5 seconds
this.restartTimer = window.setTimeout(() => {
this.restartStream()
}, 5000)
})
}
Expand All @@ -160,10 +173,16 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
this.pc?.close()
}
restartStream() {
this.pc?.close()
setTimeout(async () => {
this.startStream()
}, 500)
}
@Watch('url')
async changedUrl() {
await this.pc?.close()
this.startStream()
this.restartStream()
}
}
</script>
Expand Down

0 comments on commit fd293d2

Please sign in to comment.