Skip to content

Commit

Permalink
fix: fix multiple streames with Mjpegstreamer
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Dej <[email protected]>
  • Loading branch information
meteyou committed Sep 3, 2024
1 parent b6db556 commit ff5cb05
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/components/webcams/streamers/Mjpegstreamer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const SOI = new Uint8Array(2)
SOI[0] = 0xff
SOI[1] = 0xd8
let reader: ReadableStreamDefaultReader<Uint8Array> | null = null
@Component
export default class Mjpegstreamer extends Mixins(BaseMixin, WebcamMixin) {
// current read stream frames counter
Expand All @@ -48,6 +46,7 @@ export default class Mjpegstreamer extends Mixins(BaseMixin, WebcamMixin) {
aspectRatio: null | number = null
timerFPS: number | null = null
timerRestart: number | null = null
reader: ReadableStreamDefaultReader<Uint8Array> | null = null
@Prop({ required: true }) readonly camSettings!: GuiWebcamStateWebcam
@Prop({ default: null }) readonly printerUrl!: string | null
Expand Down Expand Up @@ -149,12 +148,12 @@ export default class Mjpegstreamer extends Mixins(BaseMixin, WebcamMixin) {
this.restartStream()
}, 10000)
reader = response.body?.getReader()
this.reader = response.body?.getReader()
await this.readStream()
// cleanup
reader = null
this.reader = null
response = null
} catch (error: any) {
this.log(error.message)
Expand All @@ -169,7 +168,7 @@ export default class Mjpegstreamer extends Mixins(BaseMixin, WebcamMixin) {
async readStream() {
// stop if the stream is not ready
if (!reader) return
if (!this.reader) return
try {
// variables to read the stream
Expand All @@ -182,7 +181,7 @@ export default class Mjpegstreamer extends Mixins(BaseMixin, WebcamMixin) {
let value
do {
;({ done, value } = await reader.read())
;({ done, value } = await this.reader.read())
if (done || !value) continue
Expand Down Expand Up @@ -218,7 +217,7 @@ export default class Mjpegstreamer extends Mixins(BaseMixin, WebcamMixin) {
} catch (error: any) {
this.log(`readStream error: ${error.message ?? ''}`, error)
} finally {
reader?.releaseLock()
this.reader?.releaseLock()
}
}
Expand Down Expand Up @@ -251,9 +250,9 @@ export default class Mjpegstreamer extends Mixins(BaseMixin, WebcamMixin) {
this.clearTimeouts()
try {
await reader?.cancel()
reader?.releaseLock()
reader = null
await this.reader?.cancel()
this.reader?.releaseLock()
this.reader = null
} catch (error) {
this.log('Error cancelling reader:', error)
}
Expand Down

0 comments on commit ff5cb05

Please sign in to comment.