Merge remote-tracking branch 'origin/develop' into develop
This commit is contained in:
commit
7553d4e6c8
2 files changed
+145
-44
No files matched your search
@@ -120,15 +120,21 @@
|
||||
</v-row>
|
||||
</v-col>
|
||||
<v-col class="col-12 col-sm-6 text-center" align-self="center">
|
||||
<vue-load-image v-if="['mjpegstreamer', 'mjpegstreamer-adaptive', 'uv4l-mjpeg'].includes(form.service)">
|
||||
<img slot="image" :src="form.service === 'mjpegstreamer-adaptive' ? form.urlSnapshot : form.urlStream" alt="Preview" :style="webcamStyle" class="webcamImage" />
|
||||
<v-progress-circular slot="preloader" indeterminate color="primary"></v-progress-circular>
|
||||
<template slot="error">
|
||||
<v-icon x-large>mdi-webcam-off</v-icon>
|
||||
<div class="subtitle-1 mt-2">{{ $t('Settings.WebcamsTab.UrlNotAvailable') }}</div>
|
||||
</template>
|
||||
</vue-load-image>
|
||||
<video v-if="['ipstream'].includes(form.service)" :src="form.urlStream" autoplay :style="webcamStyle" />
|
||||
<template v-if="form.service === 'mjpegstreamer'">
|
||||
<webcam-mjpegstreamer :cam-settings="form"></webcam-mjpegstreamer>
|
||||
</template>
|
||||
<template v-else-if="form.service === 'mjpegstreamer-adaptive'">
|
||||
<webcam-mjpegstreamer-adaptive :cam-settings="form"></webcam-mjpegstreamer-adaptive>
|
||||
</template>
|
||||
<template v-else-if="form.service === 'uv4l-mjpeg'">
|
||||
<webcam-uv4l-mjpeg :cam-settings="form"></webcam-uv4l-mjpeg>
|
||||
</template>
|
||||
<template v-else-if="form.service === 'ipstream'">
|
||||
<webcam-ipstreamer :cam-settings="form"></webcam-ipstreamer>
|
||||
</template>
|
||||
<template v-else>
|
||||
<p class="text-center py-3 font-italic">{{ $t('Panels.WebcamPanel.UnknownWebcamService') }}</p>
|
||||
</template>
|
||||
</v-col>
|
||||
</v-row>
|
||||
</v-card-text>
|
||||
@@ -158,6 +164,10 @@ import {Component, Mixins} from 'vue-property-decorator'
|
||||
import BaseMixin from '../mixins/base'
|
||||
import SettingsRow from '@/components/settings/SettingsRow.vue'
|
||||
import {GuiWebcamStateWebcam} from '@/store/gui/webcams/types'
|
||||
import Mjpegstreamer from '@/components/webcams/Mjpegstreamer.vue'
|
||||
import MjpegstreamerAdaptive from '@/components/webcams/MjpegstreamerAdaptive.vue'
|
||||
import Uv4lMjpeg from '@/components/webcams/Uv4lMjpeg.vue'
|
||||
import Ipstreamer from '@/components/webcams/Ipstreamer.vue'
|
||||
|
||||
interface webcamForm {
|
||||
bool: boolean
|
||||
@@ -174,7 +184,13 @@ interface webcamForm {
|
||||
}
|
||||
|
||||
@Component({
|
||||
components: {SettingsRow}
|
||||
components: {
|
||||
SettingsRow,
|
||||
'webcam-mjpegstreamer': Mjpegstreamer,
|
||||
'webcam-mjpegstreamer-adaptive': MjpegstreamerAdaptive,
|
||||
'webcam-uv4l-mjpeg': Uv4lMjpeg,
|
||||
'webcam-ipstreamer': Ipstreamer,
|
||||
}
|
||||
})
|
||||
export default class SettingsWebcamsTab extends Mixins(BaseMixin) {
|
||||
|
||||
|
||||
@@ -2,19 +2,23 @@
|
||||
.webcamImage {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.webcamFpsOutput {
|
||||
display: inline-block;
|
||||
position:absolute;
|
||||
bottom: 6px;
|
||||
right: 0;
|
||||
background: rgba(0,0,0,0.8);
|
||||
padding: 3px 10px;
|
||||
border-top-left-radius: 5px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<template>
|
||||
<vue-load-image v-observe-visibility="visibilityChanged" ref="imageLoader" onload="resetImageRatio">
|
||||
<img slot="image" :src="url" alt="Preview" :style="webcamStyle" class="webcamImage" ref="webcamImage" />
|
||||
<div slot="preloader" class="text-center py-5">
|
||||
<v-progress-circular indeterminate color="primary"></v-progress-circular>
|
||||
</div>
|
||||
<div slot="error" class="text-center py-5">
|
||||
<v-icon x-large>mdi-webcam-off</v-icon>
|
||||
<div class="subtitle-1 mt-2">{{ $t('Panels.WebcamPanel.UrlNotAvailable') }}</div>
|
||||
</div>
|
||||
</vue-load-image>
|
||||
<div v-observe-visibility="visibilityChanged" style="position: relative;">
|
||||
<img ref="image" class="webcamImage" />
|
||||
<span class="webcamFpsOutput">{{ $t('Panels.WebcamPanel.FPS')}}: {{ fpsOutput }}</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -22,11 +26,16 @@ import Component from 'vue-class-component'
|
||||
import {Mixins, Prop} from 'vue-property-decorator'
|
||||
import BaseMixin from '@/components/mixins/base'
|
||||
|
||||
const CONTENT_LENGTH = 'content-length'
|
||||
const TYPE_JPEG = 'image/jpeg'
|
||||
|
||||
@Component
|
||||
export default class Mjpegstreamer extends Mixins(BaseMixin) {
|
||||
private isVisible = true
|
||||
private refresh = Math.ceil(Math.random() * Math.pow(10, 12))
|
||||
private imageRatio = 0
|
||||
private currentFPS = 0
|
||||
private timerFPS: number | null = null
|
||||
private stream: ReadableStream | null = null
|
||||
|
||||
@Prop({ required: true })
|
||||
camSettings: any
|
||||
@@ -35,14 +44,13 @@ export default class Mjpegstreamer extends Mixins(BaseMixin) {
|
||||
printerUrl: string | undefined
|
||||
|
||||
$refs!: {
|
||||
imageLoader: any,
|
||||
webcamImage: HTMLImageElement,
|
||||
canvas: HTMLCanvasElement,
|
||||
image: HTMLImageElement,
|
||||
}
|
||||
|
||||
get url() {
|
||||
const baseUrl = this.camSettings.urlStream
|
||||
const url = new URL(baseUrl, this.printerUrl === undefined ? this.hostUrl.toString() : this.printerUrl)
|
||||
url.searchParams.append('bypassCache', this.refresh.toString())
|
||||
|
||||
return decodeURIComponent(url.toString())
|
||||
}
|
||||
@@ -56,35 +64,112 @@ export default class Mjpegstreamer extends Mixins(BaseMixin) {
|
||||
return ''
|
||||
}
|
||||
|
||||
get fpsOutput() {
|
||||
return (this.currentFPS < 10) ? '0'+this.currentFPS.toString() : this.currentFPS
|
||||
}
|
||||
|
||||
startStream() {
|
||||
const SOI = new Uint8Array(2)
|
||||
SOI[0] = 0xFF
|
||||
SOI[1] = 0xD8
|
||||
|
||||
function getLength(headers: any) {
|
||||
let contentLength = -1
|
||||
headers.split('\n').forEach((header: any) => {
|
||||
const pair = header.split(':')
|
||||
if (pair[0].toLowerCase() === CONTENT_LENGTH) { // Fix for issue https://github.com/aruntj/mjpeg-readable-stream/issues/3 suggested by martapanc
|
||||
contentLength = pair[1]
|
||||
}
|
||||
})
|
||||
return contentLength
|
||||
}
|
||||
|
||||
//readable stream credit to from https://github.com/aruntj/mjpeg-readable-stream
|
||||
fetch(this.url)
|
||||
.then(response => response.body)
|
||||
.then(rb => {
|
||||
const reader = rb?.getReader()
|
||||
|
||||
let headers = ''
|
||||
let contentLength = -1
|
||||
let imageBuffer: any = null
|
||||
let bytesRead = 0
|
||||
const img = this.$refs.image
|
||||
|
||||
// calculating fps. This is pretty lame. Should probably implement a floating window function.
|
||||
let frames = 0
|
||||
|
||||
this.timerFPS = setInterval(() => {
|
||||
this.currentFPS = frames
|
||||
frames = 0
|
||||
}, 1000)
|
||||
|
||||
this.stream = new ReadableStream({
|
||||
start(controller) {
|
||||
return pump()
|
||||
|
||||
// The following function handles each data chunk
|
||||
function pump(): any {
|
||||
// "done" is a Boolean and value a "Uint8Array"
|
||||
return reader?.read().then( ({done, value}) => {
|
||||
// If there is no more data to read
|
||||
if (done) {
|
||||
window.console.log('done')
|
||||
controller.close()
|
||||
return
|
||||
}
|
||||
// Get the data and send it to the browser via the controller
|
||||
controller.enqueue(value)
|
||||
|
||||
if (value) {
|
||||
for (let index =0; index < value.length; index++) {
|
||||
|
||||
// we've found start of the frame. Everything we've read till now is the header.
|
||||
if (value[index] === SOI[0] && value[index+1] === SOI[1]) {
|
||||
contentLength = getLength(headers)
|
||||
imageBuffer = new Uint8Array(new ArrayBuffer(contentLength))
|
||||
}
|
||||
// we're still reading the header.
|
||||
if (contentLength <= 0) {
|
||||
headers += String.fromCharCode(value[index])
|
||||
}
|
||||
// we're now reading the jpeg.
|
||||
else if (bytesRead < contentLength){
|
||||
imageBuffer[bytesRead++] = value[index]
|
||||
}
|
||||
// we're done reading the jpeg. Time to render it.
|
||||
else {
|
||||
img.src = URL.createObjectURL(new Blob([imageBuffer], {type: TYPE_JPEG}))
|
||||
frames++
|
||||
contentLength = 0
|
||||
bytesRead = 0
|
||||
headers = ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pump()
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
visibilityChanged(isVisible: boolean) {
|
||||
this.isVisible = isVisible
|
||||
|
||||
if (isVisible) {
|
||||
this.refresh = Math.ceil(Math.random() * Math.pow(10, 12))
|
||||
this.startStream()
|
||||
} else {
|
||||
const width = this.$refs.webcamImage?.width ?? 0
|
||||
const height = this.$refs.webcamImage?.height ?? 0
|
||||
this.imageRatio = (width && height) ? width/height : 0
|
||||
this.resize()
|
||||
if (this.timerFPS) clearTimeout(this.timerFPS)
|
||||
this.stream?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
mounted() {
|
||||
window.addEventListener('resize', this.resize)
|
||||
}
|
||||
|
||||
beforeDestroy() {
|
||||
window.removeEventListener('resize', this.resize)
|
||||
}
|
||||
|
||||
resize() {
|
||||
if (this.imageRatio && this.$refs.imageLoader?.$el) {
|
||||
this.$refs.imageLoader.$el.style.height = Math.round(this.$refs.imageLoader.$el.clientWidth / this.imageRatio)+'px'
|
||||
}
|
||||
}
|
||||
|
||||
resetImageRatio() {
|
||||
this.imageRatio = 0
|
||||
if (this.timerFPS) clearTimeout(this.timerFPS)
|
||||
this.stream?.cancel()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in new issue
Block a user