neko/src/component/screencast.vue

76 lines
1.4 KiB
Vue
Raw Normal View History

2021-07-15 01:37:23 +12:00
<template>
<img ref="image" />
</template>
<script lang="ts">
import { Vue, Component, Ref, Watch, Prop } from 'vue-property-decorator'
import { RoomApi } from './api'
const REFRESH_RATE = 1e3
@Component({
name: 'neko-screencast',
})
export default class extends Vue {
@Ref('image') readonly _image!: HTMLImageElement
active = false
2021-07-18 02:36:56 +12:00
@Prop()
private readonly enabled!: boolean
2021-07-15 01:37:23 +12:00
@Prop()
private readonly api!: RoomApi
async loop() {
while (this.active) {
const lastLoad = Date.now()
if (this._image.src) {
URL.revokeObjectURL(this._image.src)
}
const res = await this.api.screenCastImage({ responseType: 'blob' })
this._image.src = URL.createObjectURL(res.data)
const delay = lastLoad - Date.now() + REFRESH_RATE
if (delay > 0) {
await new Promise((res) => setTimeout(res, delay))
}
}
}
2021-07-18 02:36:56 +12:00
mounted() {
if (this.enabled) {
this.start()
}
}
beforeDestroy() {
this.stop()
}
start() {
2021-07-15 01:37:23 +12:00
this.active = true
setTimeout(this.loop, 0)
}
2021-07-18 02:36:56 +12:00
stop() {
2021-07-15 01:37:23 +12:00
this.active = false
2021-07-18 02:36:56 +12:00
if (this._image && this._image.src) {
2021-07-15 01:37:23 +12:00
URL.revokeObjectURL(this._image.src)
}
}
2021-07-18 02:36:56 +12:00
@Watch('enabled')
onEnabledChanged(enabled: boolean) {
if (enabled) {
this.start()
} else {
this.stop()
}
}
2021-07-15 01:37:23 +12:00
}
</script>