Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix autorestart of webcam camerastreamer #1546

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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