2020-11-07 08:49:05 +13:00
|
|
|
<template>
|
|
|
|
<div ref="component" class="component">
|
2020-11-09 10:44:37 +13:00
|
|
|
<div ref="container" class="player-container">
|
2020-11-07 08:49:05 +13:00
|
|
|
<video ref="video" />
|
|
|
|
<neko-overlay
|
|
|
|
:webrtc="webrtc"
|
2020-11-09 07:38:14 +13:00
|
|
|
:screenWidth="state.screen.size.width"
|
|
|
|
:screenHeight="state.screen.size.height"
|
|
|
|
:isControling="state.member.is_controlling"
|
|
|
|
:scrollSensitivity="state.control.scroll.sensitivity"
|
2020-11-09 09:19:46 +13:00
|
|
|
:scrollInvert="state.control.scroll.inverse"
|
2020-11-07 08:49:05 +13:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.component {
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.player-container {
|
|
|
|
position: relative;
|
|
|
|
|
|
|
|
video {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
bottom: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
display: flex;
|
|
|
|
background: #000;
|
|
|
|
|
|
|
|
&::-webkit-media-controls {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { Vue, Component, Ref, Watch, Prop } from 'vue-property-decorator'
|
|
|
|
import ResizeObserver from 'resize-observer-polyfill'
|
2020-11-08 01:26:07 +13:00
|
|
|
import EventEmitter from 'eventemitter3'
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-29 09:47:16 +13:00
|
|
|
import { NekoWebSocket } from './internal/websocket'
|
|
|
|
import { NekoWebRTC } from './internal/webrtc'
|
|
|
|
import { NekoMessages } from './internal/messages'
|
|
|
|
import { register as VideoRegister } from './internal/video'
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-29 09:47:16 +13:00
|
|
|
import NekoState from './types/state'
|
2020-11-07 08:49:05 +13:00
|
|
|
import Overlay from './overlay.vue'
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
name: 'neko-canvas',
|
|
|
|
components: {
|
|
|
|
'neko-overlay': Overlay,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
export default class extends Vue {
|
|
|
|
@Ref('component') readonly _component!: HTMLElement
|
|
|
|
@Ref('container') readonly _container!: HTMLElement
|
2020-11-11 07:57:52 +13:00
|
|
|
@Ref('video') readonly _video!: HTMLVideoElement
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
websocket = new NekoWebSocket()
|
|
|
|
webrtc = new NekoWebRTC()
|
|
|
|
observer = new ResizeObserver(this.onResize.bind(this))
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
/////////////////////////////
|
|
|
|
// Public state
|
|
|
|
/////////////////////////////
|
2020-11-08 08:12:13 +13:00
|
|
|
public state = {
|
2020-11-09 07:38:14 +13:00
|
|
|
connection: {
|
|
|
|
websocket: 'disconnected',
|
|
|
|
webrtc: 'disconnected',
|
|
|
|
type: 'none',
|
|
|
|
can_watch: false,
|
|
|
|
can_control: false,
|
|
|
|
clipboard_access: false,
|
2020-11-07 08:49:05 +13:00
|
|
|
},
|
2020-11-09 07:38:14 +13:00
|
|
|
video: {
|
|
|
|
playable: false,
|
|
|
|
playing: false,
|
|
|
|
volume: 0,
|
2020-11-30 00:06:01 +13:00
|
|
|
muted: false,
|
2020-11-11 07:57:52 +13:00
|
|
|
fullscreen: false,
|
2020-11-08 06:47:02 +13:00
|
|
|
},
|
2020-11-09 07:38:14 +13:00
|
|
|
control: {
|
|
|
|
scroll: {
|
|
|
|
inverse: true,
|
2020-11-09 09:19:46 +13:00
|
|
|
sensitivity: 1,
|
2020-11-09 07:38:14 +13:00
|
|
|
},
|
2020-11-11 07:57:52 +13:00
|
|
|
clipboard: {
|
|
|
|
data: null,
|
|
|
|
},
|
2020-11-09 07:38:14 +13:00
|
|
|
host: null,
|
|
|
|
},
|
|
|
|
screen: {
|
|
|
|
size: {
|
|
|
|
width: 1280,
|
|
|
|
height: 720,
|
|
|
|
rate: 30,
|
|
|
|
},
|
|
|
|
configurations: [],
|
|
|
|
},
|
|
|
|
member: {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
is_admin: false,
|
|
|
|
is_watching: false,
|
|
|
|
is_controlling: false,
|
|
|
|
can_watch: false,
|
|
|
|
can_control: false,
|
|
|
|
clipboard_access: false,
|
|
|
|
},
|
|
|
|
members: [],
|
2020-11-07 10:19:41 +13:00
|
|
|
} as NekoState
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-08 08:12:13 +13:00
|
|
|
public get connected() {
|
2020-11-09 07:38:14 +13:00
|
|
|
return this.state.connection.websocket == 'connected' && this.state.connection.webrtc == 'connected'
|
2020-11-08 08:12:13 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:46:22 +13:00
|
|
|
public get controlling() {
|
|
|
|
return this.state.member.is_controlling
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
/////////////////////////////
|
|
|
|
// Public events
|
|
|
|
/////////////////////////////
|
|
|
|
public events = new NekoMessages(this.websocket, this.state)
|
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
// Public methods
|
|
|
|
/////////////////////////////
|
2020-11-29 03:28:11 +13:00
|
|
|
public connect(url: string, id: string, secret: string) {
|
2020-11-11 07:57:52 +13:00
|
|
|
if (this.connected) {
|
|
|
|
throw new Error('client already connected')
|
2020-11-09 10:02:00 +13:00
|
|
|
}
|
|
|
|
|
2020-11-29 03:28:11 +13:00
|
|
|
this.websocket.connect(url, id, secret)
|
2020-11-11 07:57:52 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public disconnect() {
|
|
|
|
if (!this.connected) {
|
|
|
|
throw new Error('client not connected')
|
2020-11-09 10:02:00 +13:00
|
|
|
}
|
2020-11-09 10:48:04 +13:00
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
this.websocket.disconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
public play() {
|
|
|
|
this._video.play()
|
2020-11-09 10:02:00 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public pause() {
|
|
|
|
this._video.pause()
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:06:01 +13:00
|
|
|
public mute() {
|
|
|
|
this._video.muted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
public unmute() {
|
|
|
|
this._video.muted = false
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public setVolume(value: number) {
|
2020-11-09 10:02:00 +13:00
|
|
|
if (value < 0 || value > 1) {
|
|
|
|
throw new Error('Out of range. Value must be between 0 and 1.')
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
this._video.volume = value
|
2020-11-09 10:02:00 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public requestFullscreen() {
|
|
|
|
this._component.requestFullscreen()
|
2020-11-09 09:19:46 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public exitFullscreen() {
|
|
|
|
document.exitFullscreen()
|
2020-11-09 10:16:47 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public setScrollInverse(value: boolean = true) {
|
|
|
|
Vue.set(this.state.control.scroll, 'inverse', value)
|
2020-11-07 08:49:05 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public setScrollSensitivity(value: number) {
|
|
|
|
Vue.set(this.state.control.scroll, 'sensitivity', value)
|
2020-11-08 06:47:02 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public setClipboardData(value: number) {
|
|
|
|
// TODO: Via REST API.
|
|
|
|
}
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-11 08:17:46 +13:00
|
|
|
public requestControl() {
|
2020-11-11 07:57:52 +13:00
|
|
|
// TODO: Via REST API.
|
|
|
|
this.websocket.send('control/request')
|
2020-11-07 08:49:05 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 08:17:46 +13:00
|
|
|
public releaseControl() {
|
2020-11-11 07:57:52 +13:00
|
|
|
// TODO: Via REST API.
|
|
|
|
this.websocket.send('control/release')
|
|
|
|
}
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-11 08:17:46 +13:00
|
|
|
public takeControl() {
|
2020-11-11 07:57:52 +13:00
|
|
|
// TODO: Via REST API.
|
|
|
|
}
|
|
|
|
|
2020-11-11 08:17:46 +13:00
|
|
|
public giveControl(id: string) {
|
2020-11-11 07:57:52 +13:00
|
|
|
// TODO: Via REST API.
|
|
|
|
}
|
|
|
|
|
2020-11-11 08:17:46 +13:00
|
|
|
public resetControl() {
|
2020-11-11 07:57:52 +13:00
|
|
|
// TODO: Via REST API.
|
|
|
|
}
|
2020-11-09 10:44:37 +13:00
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
public setScreenSize(width: number, height: number, rate: number) {
|
|
|
|
// TODO: Via REST API.
|
|
|
|
this.websocket.send('screen/set', { width, height, rate })
|
2020-11-07 08:49:05 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
/////////////////////////////
|
|
|
|
// Component lifecycle
|
|
|
|
/////////////////////////////
|
|
|
|
mounted() {
|
|
|
|
// component size change
|
2020-11-07 08:49:05 +13:00
|
|
|
this.observer.observe(this._component)
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
// host change
|
2020-11-08 06:47:02 +13:00
|
|
|
this.events.on('control.host', (id: string | null) => {
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.member, 'is_controlling', id != null && id === this.state.member.id)
|
2020-11-08 06:47:02 +13:00
|
|
|
})
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
// fullscreen change
|
2020-11-09 10:16:47 +13:00
|
|
|
this._component.addEventListener('fullscreenchange', () => {
|
2020-11-11 07:57:52 +13:00
|
|
|
Vue.set(this.state.video, 'fullscreen', document.fullscreenElement !== null)
|
2020-11-09 10:16:47 +13:00
|
|
|
this.onResize()
|
|
|
|
})
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
// video events
|
|
|
|
VideoRegister(this._video, this.state.video)
|
2020-11-09 09:19:46 +13:00
|
|
|
|
2020-11-09 10:44:37 +13:00
|
|
|
// websocket
|
2020-11-07 08:49:05 +13:00
|
|
|
this.websocket.on('message', async (event: string, payload: any) => {
|
|
|
|
switch (event) {
|
|
|
|
case 'signal/provide':
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.member, 'id', payload.id)
|
2020-11-07 10:19:41 +13:00
|
|
|
|
2020-11-07 08:49:05 +13:00
|
|
|
try {
|
|
|
|
let sdp = await this.webrtc.connect(payload.sdp, payload.lite, payload.ice)
|
2020-11-29 03:28:11 +13:00
|
|
|
this.websocket.send('signal/answer', { sdp })
|
2020-11-07 08:49:05 +13:00
|
|
|
} catch (e) {}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.websocket.on('connecting', () => {
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.connection, 'websocket', 'connecting')
|
2020-11-08 06:47:02 +13:00
|
|
|
this.events.emit('system.websocket', 'connecting')
|
2020-11-07 08:49:05 +13:00
|
|
|
})
|
|
|
|
this.websocket.on('connected', () => {
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.connection, 'websocket', 'connected')
|
2020-11-08 06:47:02 +13:00
|
|
|
this.events.emit('system.websocket', 'connected')
|
2020-11-07 08:49:05 +13:00
|
|
|
})
|
|
|
|
this.websocket.on('disconnected', () => {
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.connection, 'websocket', 'disconnected')
|
2020-11-08 06:47:02 +13:00
|
|
|
this.events.emit('system.websocket', 'disconnected')
|
2020-11-07 08:49:05 +13:00
|
|
|
this.webrtc.disconnect()
|
2020-11-11 07:57:52 +13:00
|
|
|
|
|
|
|
// TODO: reset state
|
|
|
|
Vue.set(this.state, 'member', {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
is_admin: false,
|
|
|
|
is_watching: false,
|
|
|
|
is_controlling: false,
|
|
|
|
can_watch: false,
|
|
|
|
can_control: false,
|
|
|
|
clipboard_access: false,
|
|
|
|
})
|
2020-11-07 08:49:05 +13:00
|
|
|
})
|
|
|
|
|
2020-11-09 10:44:37 +13:00
|
|
|
// webrtc
|
2020-11-07 08:49:05 +13:00
|
|
|
this.webrtc.on('track', (event: RTCTrackEvent) => {
|
|
|
|
const { track, streams } = event
|
2020-11-08 06:47:02 +13:00
|
|
|
if (track.kind === 'audio') return
|
2020-11-07 08:49:05 +13:00
|
|
|
|
2020-11-09 10:44:37 +13:00
|
|
|
// create stream
|
2020-11-11 07:57:52 +13:00
|
|
|
if ('srcObject' in this._video) {
|
|
|
|
this._video.srcObject = streams[0]
|
2020-11-07 08:49:05 +13:00
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
2020-11-11 07:57:52 +13:00
|
|
|
this._video.src = window.URL.createObjectURL(streams[0]) // for older browsers
|
2020-11-07 08:49:05 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
this._video.play()
|
2020-11-07 08:49:05 +13:00
|
|
|
})
|
|
|
|
this.webrtc.on('connecting', () => {
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.connection, 'webrtc', 'connecting')
|
2020-11-08 06:47:02 +13:00
|
|
|
this.events.emit('system.webrtc', 'connecting')
|
2020-11-07 08:49:05 +13:00
|
|
|
})
|
|
|
|
this.webrtc.on('connected', () => {
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.connection, 'webrtc', 'connected')
|
2020-11-08 06:47:02 +13:00
|
|
|
this.events.emit('system.webrtc', 'connected')
|
2020-11-07 08:49:05 +13:00
|
|
|
})
|
|
|
|
this.webrtc.on('disconnected', () => {
|
2020-11-09 07:38:14 +13:00
|
|
|
Vue.set(this.state.connection, 'webrtc', 'disconnected')
|
2020-11-08 06:47:02 +13:00
|
|
|
this.events.emit('system.webrtc', 'disconnected')
|
2020-11-09 09:19:46 +13:00
|
|
|
// @ts-ignore
|
2020-11-11 07:57:52 +13:00
|
|
|
this._video.src = null
|
2020-11-07 08:49:05 +13:00
|
|
|
})
|
2020-11-11 07:57:52 +13:00
|
|
|
|
|
|
|
// hardcoded webrtc for now
|
|
|
|
Vue.set(this.state.connection, 'type', 'webrtc')
|
|
|
|
Vue.set(this.state.connection, 'can_watch', this.webrtc.supported)
|
|
|
|
Vue.set(this.state.connection, 'can_control', this.webrtc.supported)
|
2020-11-07 08:49:05 +13:00
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
beforeDestroy() {
|
2020-11-08 08:12:13 +13:00
|
|
|
this.observer.disconnect()
|
2020-11-07 08:49:05 +13:00
|
|
|
this.webrtc.disconnect()
|
|
|
|
this.websocket.disconnect()
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:57:52 +13:00
|
|
|
@Watch('state.video.playing')
|
|
|
|
onVideoPlayingChanged(play: boolean) {
|
|
|
|
// TODO: check if user has tab focused and send via websocket
|
|
|
|
Vue.set(this.state.member, 'is_watching', play)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Watch('state.screen.size')
|
|
|
|
onResize() {
|
2020-11-09 07:38:14 +13:00
|
|
|
const { width, height } = this.state.screen.size
|
2020-11-07 08:49:05 +13:00
|
|
|
const screen_ratio = width / height
|
|
|
|
|
|
|
|
const { offsetWidth, offsetHeight } = this._component
|
|
|
|
const canvas_ratio = offsetWidth / offsetHeight
|
|
|
|
|
2020-11-09 10:44:37 +13:00
|
|
|
// vertical centering
|
2020-11-07 10:19:41 +13:00
|
|
|
if (screen_ratio > canvas_ratio) {
|
2020-11-07 08:49:05 +13:00
|
|
|
const vertical = offsetWidth / screen_ratio
|
|
|
|
this._container.style.width = `${offsetWidth}px`
|
|
|
|
this._container.style.height = `${vertical}px`
|
|
|
|
this._container.style.marginTop = `${(offsetHeight - vertical) / 2}px`
|
|
|
|
this._container.style.marginLeft = `0px`
|
|
|
|
}
|
2020-11-09 10:44:37 +13:00
|
|
|
// horizontal centering
|
2020-11-07 10:19:41 +13:00
|
|
|
else if (screen_ratio < canvas_ratio) {
|
2020-11-07 08:49:05 +13:00
|
|
|
const horizontal = screen_ratio * offsetHeight
|
|
|
|
this._container.style.width = `${horizontal}px`
|
|
|
|
this._container.style.height = `${offsetHeight}px`
|
|
|
|
this._container.style.marginTop = `0px`
|
|
|
|
this._container.style.marginLeft = `${(offsetWidth - horizontal) / 2}px`
|
|
|
|
}
|
2020-11-09 10:44:37 +13:00
|
|
|
// no centering
|
2020-11-07 08:49:05 +13:00
|
|
|
else {
|
|
|
|
this._container.style.width = `${offsetWidth}px`
|
|
|
|
this._container.style.height = `${offsetHeight}px`
|
|
|
|
this._container.style.marginTop = `0px`
|
|
|
|
this._container.style.marginLeft = `0px`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|