Skip to content

Commit

Permalink
fix: only start stream, when the panel is open
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Dej <[email protected]>
  • Loading branch information
meteyou committed Aug 24, 2024
1 parent 94f2fd7 commit bbb33b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/webcams/WebcamWrapperItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<j-muxer-stream-async :cam-settings="webcam" :printer-url="printerUrl" />
</template>
<template v-else-if="service === 'webrtc-camerastreamer'">
<webrtc-camera-streamer-async :cam-settings="webcam" :printer-url="printerUrl" />
<webrtc-camera-streamer-async :cam-settings="webcam" :printer-url="printerUrl" :page="page" />
</template>
<template v-else-if="service === 'webrtc-janus'">
<janus-streamer-async :cam-settings="webcam" :printer-url="printerUrl" />
Expand Down
35 changes: 27 additions & 8 deletions src/components/webcams/streamers/WebrtcCameraStreamer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
@Prop({ required: true }) readonly camSettings!: GuiWebcamStateWebcam
@Prop({ default: null }) declare readonly printerUrl: string | null
@Prop({ type: String, default: null }) readonly page!: string | null
@Ref() declare stream: HTMLVideoElement
get url() {
Expand All @@ -59,8 +60,25 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
return output
}
get expanded(): boolean {
if (this.page !== 'dashboard') return true
return this.$store.getters['gui/getPanelExpand']('webcam-panel', this.viewport) ?? false
}
// start or stop the video when the expanded state changes
@Watch('expanded', { immediate: true })
expandChanged(newExpanded: boolean): void {
if (!newExpanded) {
this.terminate()
return
}
this.start()
}
// This WebRTC signaling pattern is designed for camera-streamer, a common webcam server the supports WebRTC.
async startStream() {
async start() {
if (this.restartTimer) {
this.log('Clearing restart timer before starting stream')
window.clearTimeout(this.restartTimer)
Expand Down Expand Up @@ -163,7 +181,7 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
onConnectionStateChange() {
this.status = this.pc?.connectionState ?? 'connecting'
this.log(`ConnectionState: ${this.status}`)
this.log(`State: ${this.status}`)
if (['failed', 'disconnected'].includes(this.status)) {
this.restartStream(5000)
Expand All @@ -186,23 +204,24 @@ export default class WebrtcCameraStreamer extends Mixins(BaseMixin, WebcamMixin)
window.console.log(message)
}
mounted() {
this.startStream()
}
beforeDestroy() {
this.pc?.close()
if (this.restartTimer) window.clearTimeout(this.restartTimer)
}
restartStream(delay = 500) {
terminate() {
this.log('Terminating stream')
this.pc?.close()
}
restartStream(delay = 500) {
this.terminate()
if (this.restartTimer) return
this.restartTimer = window.setTimeout(async () => {
this.restartTimer = null
await this.startStream()
await this.start()
}, delay)
}
Expand Down

0 comments on commit bbb33b8

Please sign in to comment.