Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/client/src/components/video.vue

627 lines
15 KiB
Vue
Raw Normal View History

2020-01-23 06:16:40 +13:00
<template>
<div ref="component" class="video">
<div ref="player" class="player">
<div ref="container" class="player-container">
2021-02-13 10:48:31 +13:00
<video ref="video" playsinline />
2020-01-24 04:23:26 +13:00
<div class="emotes">
<template v-for="(emote, index) in emotes">
<neko-emote :id="index" :key="index" />
</template>
</div>
2020-01-23 06:16:40 +13:00
<div
ref="overlay"
class="overlay"
tabindex="0"
@click.stop.prevent
@contextmenu.stop.prevent
@wheel.stop.prevent="onWheel"
@mousemove.stop.prevent="onMouseMove"
@mousedown.stop.prevent="onMouseDown"
@mouseup.stop.prevent="onMouseUp"
@mouseenter.stop.prevent="onMouseEnter"
@mouseleave.stop.prevent="onMouseLeave"
/>
<div v-if="!playing" class="player-overlay">
<i @click.stop.prevent="toggle" v-if="playable" class="fas fa-play-circle" />
</div>
<div ref="aspect" class="player-aspect" />
</div>
2020-06-20 01:03:49 +12:00
<ul v-if="!fullscreen" class="video-menu top">
2020-02-11 18:15:59 +13:00
<li><i @click.stop.prevent="requestFullscreen" class="fas fa-expand"></i></li>
2020-02-12 07:43:31 +13:00
<li v-if="admin"><i @click.stop.prevent="onResolution" class="fas fa-desktop"></i></li>
<li class="request-control">
<i
:class="[hosted && !hosting ? 'disabled' : '', !hosted && !hosting ? 'faded' : '', 'fas', 'fa-keyboard']"
@click.stop.prevent="toggleControl"
/>
</li>
2020-02-11 18:15:59 +13:00
</ul>
2020-06-20 01:03:49 +12:00
<ul v-if="!fullscreen" class="video-menu bottom">
2021-02-14 00:05:59 +13:00
<li v-if="hosting && (!clipboard_read_available || !clipboard_write_available)">
<i @click.stop.prevent="onClipboard" class="fas fa-clipboard"></i>
</li>
2020-12-19 07:12:41 +13:00
<li>
<i
2021-02-19 06:02:23 +13:00
v-if="pip_available"
2020-12-19 07:12:41 +13:00
@click.stop.prevent="requestPictureInPicture"
v-tooltip="{ content: 'Picture-in-Picture', placement: 'left', offset: 5, boundariesElement: 'body' }"
class="fas fa-external-link-alt"
/>
</li>
2020-06-20 01:03:49 +12:00
</ul>
<neko-resolution ref="resolution" v-if="admin" />
<neko-clipboard ref="clipboard" v-if="hosting && (!clipboard_read_available || !clipboard_write_available)" />
2020-01-23 06:16:40 +13:00
</div>
</div>
</template>
<style lang="scss" scoped>
.video {
width: 100%;
height: 100%;
.player {
position: absolute;
display: flex;
justify-content: center;
align-items: center;
2020-02-11 18:15:59 +13:00
.video-menu {
2020-01-23 06:16:40 +13:00
position: absolute;
right: 20px;
2020-06-20 01:03:49 +12:00
&.top {
top: 15px;
}
&.bottom {
bottom: 15px;
}
2020-02-11 18:15:59 +13:00
li {
margin: 0 0 10px 0;
i {
width: 30px;
height: 30px;
background: rgba($color: #fff, $alpha: 0.2);
border-radius: 5px;
line-height: 30px;
font-size: 16px;
text-align: center;
color: rgba($color: #fff, $alpha: 0.6);
cursor: pointer;
&.faded {
color: rgba($color: $text-normal, $alpha: 0.4);
}
&.disabled {
color: rgba($color: $style-error, $alpha: 0.4);
}
2020-02-11 18:15:59 +13:00
}
&.request-control {
display: none;
}
@media (max-width: 768px) {
&.request-control {
display: inline-block;
}
2020-02-11 18:15:59 +13:00
}
2020-06-20 01:03:49 +12:00
&:last-child {
margin: 0;
}
2020-02-11 18:15:59 +13:00
}
2020-01-23 06:16:40 +13:00
}
.player-container {
position: relative;
width: 100%;
max-width: 16 / 9 * 100vh;
video {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
display: flex;
background: #000;
&::-webkit-media-controls {
display: none !important;
}
}
2020-01-24 04:23:26 +13:00
.player-overlay,
.emotes {
2020-01-23 06:16:40 +13:00
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
2020-01-24 04:23:26 +13:00
overflow: hidden;
}
.player-overlay {
2020-01-23 06:16:40 +13:00
background: rgba($color: #000, $alpha: 0.2);
display: flex;
justify-content: center;
align-items: center;
i {
cursor: pointer;
&::before {
font-size: 120px;
text-align: center;
}
}
&.hidden {
display: none;
}
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
}
.player-aspect {
display: block;
padding-bottom: 56.25%;
}
}
}
}
</style>
<script lang="ts">
import { Component, Ref, Watch, Vue } from 'vue-property-decorator'
import ResizeObserver from 'resize-observer-polyfill'
2020-01-24 04:23:26 +13:00
import Emote from './emote.vue'
2020-02-11 18:15:59 +13:00
import Resolution from './resolution.vue'
2020-06-20 01:03:49 +12:00
import Clipboard from './clipboard.vue'
2020-01-24 04:23:26 +13:00
2020-06-16 06:45:32 +12:00
import GuacamoleKeyboard from '~/utils/guacamole-keyboard.ts'
2020-01-24 04:23:26 +13:00
@Component({
name: 'neko-video',
components: {
'neko-emote': Emote,
2020-02-11 18:15:59 +13:00
'neko-resolution': Resolution,
2020-06-20 01:03:49 +12:00
'neko-clipboard': Clipboard,
2020-01-24 04:23:26 +13:00
},
})
2020-01-23 06:16:40 +13:00
export default class extends Vue {
@Ref('component') readonly _component!: HTMLElement
@Ref('container') readonly _container!: HTMLElement
@Ref('overlay') readonly _overlay!: HTMLElement
@Ref('aspect') readonly _aspect!: HTMLElement
@Ref('player') readonly _player!: HTMLElement
@Ref('video') readonly _video!: HTMLVideoElement
2020-02-11 18:15:59 +13:00
@Ref('resolution') readonly _resolution!: any
2020-06-20 01:03:49 +12:00
@Ref('clipboard') readonly _clipboard!: any
2020-01-23 06:16:40 +13:00
2020-06-16 06:45:32 +12:00
private keyboard = GuacamoleKeyboard()
2020-01-23 06:16:40 +13:00
private observer = new ResizeObserver(this.onResise.bind(this))
private focused = false
private fullscreen = false
2020-02-11 18:15:59 +13:00
get admin() {
return this.$accessor.user.admin
}
2020-01-23 06:16:40 +13:00
get connected() {
return this.$accessor.connected
}
get connecting() {
return this.$accessor.connecting
}
get hosting() {
return this.$accessor.remote.hosting
}
get hosted() {
return this.$accessor.remote.hosted
}
2020-01-23 06:16:40 +13:00
get volume() {
return this.$accessor.video.volume
}
get muted() {
return this.$accessor.video.muted
}
get stream() {
return this.$accessor.video.stream
}
get playing() {
return this.$accessor.video.playing
}
get playable() {
return this.$accessor.video.playable
}
2020-01-24 04:23:26 +13:00
get emotes() {
return this.$accessor.chat.emotes
}
get autoplay() {
return this.$accessor.settings.autoplay
}
2020-02-12 12:51:57 +13:00
get locked() {
return this.$accessor.remote.locked
}
2020-01-24 04:23:26 +13:00
get scroll() {
return this.$accessor.settings.scroll
}
get scroll_invert() {
return this.$accessor.settings.scroll_invert
}
2021-02-19 06:02:23 +13:00
get pip_available() {
//@ts-ignore
return typeof document.createElement('video').requestPictureInPicture === 'function'
}
get clipboard_read_available() {
return 'clipboard' in navigator && typeof navigator.clipboard.readText === 'function'
}
get clipboard_write_available() {
return 'clipboard' in navigator && typeof navigator.clipboard.writeText === 'function'
2020-07-13 07:34:44 +12:00
}
2020-01-26 03:29:52 +13:00
get clipboard() {
return this.$accessor.remote.clipboard
}
2020-02-11 18:15:59 +13:00
get width() {
return this.$accessor.video.width
}
get height() {
return this.$accessor.video.height
}
get rate() {
return this.$accessor.video.rate
}
get vertical() {
return this.$accessor.video.vertical
}
get horizontal() {
return this.$accessor.video.horizontal
}
@Watch('width')
onWidthChanged(width: number) {
this.onResise()
}
@Watch('height')
onHeightChanged(height: number) {
this.onResise()
}
2020-01-23 06:16:40 +13:00
@Watch('volume')
onVolumeChanged(volume: number) {
if (this._video) {
this._video.volume = this.volume / 100
}
}
@Watch('muted')
onMutedChanged(muted: boolean) {
if (this._video) {
this._video.muted = muted
}
}
@Watch('stream')
onStreamChanged(stream?: MediaStream) {
if (!this._video || !stream) {
return
}
if ('srcObject' in this._video) {
this._video.srcObject = stream
} else {
// @ts-ignore
this._video.src = window.URL.createObjectURL(this.stream) // for older browsers
}
}
@Watch('playing')
onPlayingChanged(playing: boolean) {
if (playing) {
this.play()
} else {
this.pause()
}
}
2020-01-26 03:29:52 +13:00
@Watch('clipboard')
onClipboardChanged(clipboard: string) {
if (this.clipboard_write_available) {
2020-01-26 04:14:46 +13:00
navigator.clipboard.writeText(clipboard).catch(console.error)
}
2020-01-26 03:29:52 +13:00
}
2020-01-23 06:16:40 +13:00
mounted() {
this._container.addEventListener('resize', this.onResise)
this.onVolumeChanged(this.volume)
this.onStreamChanged(this.stream)
this.onResise()
this.observer.observe(this._component)
this._player.addEventListener('fullscreenchange', () => {
this.fullscreen = document.fullscreenElement !== null
this.onResise()
})
this._video.addEventListener('canplaythrough', () => {
this.$accessor.video.setPlayable(true)
2020-01-24 04:23:26 +13:00
if (this.autoplay) {
2020-02-13 11:41:52 +13:00
if (!document.hasFocus() || !this.$accessor.active) {
2020-02-12 17:16:49 +13:00
this.$accessor.video.setMuted(true)
2020-02-13 11:41:52 +13:00
this._video.muted = true
2020-02-12 17:16:49 +13:00
}
2020-01-26 03:29:52 +13:00
this.$nextTick(() => {
this.$accessor.video.play()
})
2020-01-24 04:23:26 +13:00
}
2020-01-23 06:16:40 +13:00
})
this._video.addEventListener('ended', () => {
this.$accessor.video.setPlayable(false)
})
this._video.addEventListener('error', (event) => {
2020-02-12 12:51:57 +13:00
this.$log.error(event.error)
2020-01-23 06:16:40 +13:00
this.$accessor.video.setPlayable(false)
})
2020-01-26 03:29:52 +13:00
document.addEventListener('focusin', this.onFocus.bind(this))
2020-06-16 01:55:31 +12:00
2020-06-16 06:45:32 +12:00
/* Initialize Guacamole Keyboard */
this.keyboard.onkeydown = (key: number) => {
2020-06-16 01:55:31 +12:00
if (!this.focused || !this.hosting || this.locked) {
2020-06-16 23:12:36 +12:00
return true
2020-06-16 01:55:31 +12:00
}
this.$client.sendData('keydown', { key })
2020-06-16 23:12:36 +12:00
return false
2020-06-16 06:45:32 +12:00
}
this.keyboard.onkeyup = (key: number) => {
2020-06-16 01:55:31 +12:00
if (!this.focused || !this.hosting || this.locked) {
return
}
this.$client.sendData('keyup', { key })
2020-06-16 03:28:05 +12:00
}
2020-06-16 06:45:32 +12:00
this.keyboard.listenTo(this._overlay)
2020-01-23 06:16:40 +13:00
}
beforeDestroy() {
this.observer.disconnect()
this.$accessor.video.setPlayable(false)
2020-01-26 03:29:52 +13:00
document.removeEventListener('focusin', this.onFocus.bind(this))
2020-06-16 06:45:32 +12:00
/* Guacamole Keyboard does not provide destroy functions */
2020-01-23 06:16:40 +13:00
}
play() {
if (!this._video.paused || !this.playable) {
return
}
2020-02-13 11:41:52 +13:00
try {
this._video
.play()
.then(() => {
this.onResise()
})
.catch((err) => this.$log.error)
2020-02-13 11:41:52 +13:00
} catch (err) {
this.$log.error(err)
}
2020-01-23 06:16:40 +13:00
}
pause() {
if (this._video.paused || !this.playable) {
return
}
this._video.pause()
}
toggle() {
if (!this.playable) {
return
}
if (!this.playing) {
this.$accessor.video.play()
} else {
this.$accessor.video.pause()
}
}
toggleControl() {
if (!this.playable) {
return
}
this.$accessor.remote.toggle()
}
2020-01-23 06:16:40 +13:00
requestFullscreen() {
2021-02-19 08:07:06 +13:00
if (typeof this._player.requestFullscreen === 'function') {
this._player.requestFullscreen()
//@ts-ignore
} else if (typeof this._player.webkitRequestFullscreen === 'function') {
//@ts-ignore
this._player.webkitRequestFullscreen()
//@ts-ignore
} else if (typeof this._player.webkitEnterFullscreen === 'function') {
//@ts-ignore
this._player.webkitEnterFullscreen()
//@ts-ignore
} else if (typeof this._player.msRequestFullScreen === 'function') {
//@ts-ignore
this._player.msRequestFullScreen()
}
2020-01-23 06:16:40 +13:00
this.onResise()
}
2020-12-19 07:12:41 +13:00
requestPictureInPicture() {
//@ts-ignore
this._video.requestPictureInPicture()
this.onResise()
}
2020-01-26 03:29:52 +13:00
onFocus() {
2020-02-13 11:41:52 +13:00
if (!document.hasFocus() || !this.$accessor.active) {
2020-01-26 03:29:52 +13:00
return
}
if (this.hosting && this.clipboard_read_available) {
2020-01-26 03:29:52 +13:00
navigator.clipboard
.readText()
.then((text) => {
2020-01-26 03:29:52 +13:00
if (this.clipboard !== text) {
2020-02-13 11:41:52 +13:00
this.$accessor.remote.setClipboard(text)
2020-01-26 03:29:52 +13:00
this.$accessor.remote.sendClipboard(text)
}
})
2020-02-12 12:51:57 +13:00
.catch(this.$log.error)
2020-01-26 03:29:52 +13:00
}
}
2020-01-23 06:16:40 +13:00
onMousePos(e: MouseEvent) {
const { w, h } = this.$accessor.video.resolution
const rect = this._overlay.getBoundingClientRect()
this.$client.sendData('mousemove', {
x: Math.round((w / rect.width) * (e.clientX - rect.left)),
y: Math.round((h / rect.height) * (e.clientY - rect.top)),
})
}
onWheel(e: WheelEvent) {
2020-02-12 12:51:57 +13:00
if (!this.hosting || this.locked) {
2020-01-23 06:16:40 +13:00
return
}
this.onMousePos(e)
2020-01-24 04:23:26 +13:00
let x = e.deltaX
let y = e.deltaY
if (this.scroll_invert) {
x = x * -1
y = y * -1
}
x = Math.min(Math.max(x, -this.scroll), this.scroll)
y = Math.min(Math.max(y, -this.scroll), this.scroll)
this.$client.sendData('wheel', { x, y })
2020-01-23 06:16:40 +13:00
}
onMouseDown(e: MouseEvent) {
2020-02-12 12:51:57 +13:00
if (!this.hosting || this.locked) {
2020-01-23 06:16:40 +13:00
return
}
this.onMousePos(e)
2020-06-14 02:21:11 +12:00
this.$client.sendData('mousedown', { key: e.button + 1 })
2020-01-23 06:16:40 +13:00
}
onMouseUp(e: MouseEvent) {
2020-02-12 12:51:57 +13:00
if (!this.hosting || this.locked) {
2020-01-23 06:16:40 +13:00
return
}
this.onMousePos(e)
2020-06-14 02:21:11 +12:00
this.$client.sendData('mouseup', { key: e.button + 1 })
2020-01-23 06:16:40 +13:00
}
onMouseMove(e: MouseEvent) {
2020-02-12 12:51:57 +13:00
if (!this.hosting || this.locked) {
2020-01-23 06:16:40 +13:00
return
}
2020-01-23 06:16:40 +13:00
this.onMousePos(e)
}
onMouseEnter(e: MouseEvent) {
2021-02-14 00:05:59 +13:00
if (this.hosting) {
this.$accessor.remote.syncKeyboardModifierState({
2021-02-14 00:05:59 +13:00
capsLock: e.getModifierState('CapsLock'),
numLock: e.getModifierState('NumLock'),
scrollLock: e.getModifierState('ScrollLock'),
})
}
2020-01-23 06:16:40 +13:00
this._overlay.focus()
2020-01-26 03:29:52 +13:00
this.onFocus()
2020-01-23 06:16:40 +13:00
this.focused = true
}
onMouseLeave(e: MouseEvent) {
2021-02-14 00:05:59 +13:00
if (this.hosting) {
this.$accessor.remote.setKeyboardModifierState({
2021-02-14 00:05:59 +13:00
capsLock: e.getModifierState('CapsLock'),
numLock: e.getModifierState('NumLock'),
scrollLock: e.getModifierState('ScrollLock'),
})
}
2020-06-22 00:58:52 +12:00
this.keyboard.reset()
2020-01-23 06:16:40 +13:00
this.focused = false
}
onResise() {
let height = 0
if (!this.fullscreen) {
const { offsetWidth, offsetHeight } = this._component
this._player.style.width = `${offsetWidth}px`
this._player.style.height = `${offsetHeight}px`
height = offsetHeight
} else {
const { offsetWidth, offsetHeight } = this._player
height = offsetHeight
}
2020-02-11 18:15:59 +13:00
this._container.style.maxWidth = `${(this.horizontal / this.vertical) * height}px`
this._aspect.style.paddingBottom = `${(this.vertical / this.horizontal) * 100}%`
}
onResolution(event: MouseEvent) {
this._resolution.open(event)
2020-01-23 06:16:40 +13:00
}
2020-06-20 01:03:49 +12:00
onClipboard(event: MouseEvent) {
this._clipboard.open(event)
}
2020-01-23 06:16:40 +13:00
}
</script>