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/store/video.ts

192 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-01-21 03:36:18 +13:00
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
2020-01-24 04:23:26 +13:00
import { get, set } from '~/utils/localstorage'
2020-02-11 18:15:59 +13:00
import { EVENT } from '~/neko/events'
2020-02-12 07:43:31 +13:00
import { ScreenConfigurations, ScreenResolution } from '~/neko/types'
2020-02-11 18:15:59 +13:00
import { accessor } from '~/store'
2020-01-21 03:36:18 +13:00
export const namespaced = true
2020-01-24 04:23:26 +13:00
export const state = () => ({
index: -1,
tracks: [] as MediaStreamTrack[],
streams: [] as MediaStream[],
2020-02-12 07:43:31 +13:00
configurations: [] as ScreenResolution[],
2020-01-24 04:23:26 +13:00
width: 1280,
height: 720,
2020-02-11 18:15:59 +13:00
rate: 30,
2020-01-24 04:23:26 +13:00
horizontal: 16,
vertical: 9,
volume: get<number>('volume', 100),
muted: get<boolean>('muted', false),
playing: false,
playable: false,
})
2020-01-21 03:36:18 +13:00
export const getters = getterTree(state, {
stream: state => state.streams[state.index],
2020-01-23 06:16:40 +13:00
track: state => state.tracks[state.index],
2020-01-21 03:36:18 +13:00
resolution: state => ({ w: state.width, h: state.height }),
2020-01-23 06:16:40 +13:00
})
export const mutations = mutationTree(state, {
play(state) {
if (state.playable) {
state.playing = true
}
},
pause(state) {
if (state.playable) {
state.playing = false
}
},
togglePlay(state) {
if (state.playable) {
state.playing = !state.playing
}
},
toggleMute(state) {
state.muted = !state.muted
2020-01-24 04:23:26 +13:00
set('mute', state.muted)
2020-01-23 06:16:40 +13:00
},
setPlayable(state, playable: boolean) {
if (!playable && state.playing) {
state.playing = false
}
state.playable = playable
},
2020-02-11 18:15:59 +13:00
setResolution(state, { width, height, rate }: { width: number; height: number; rate: number }) {
2020-01-23 06:16:40 +13:00
state.width = width
state.height = height
2020-02-11 18:15:59 +13:00
state.rate = rate
2020-01-21 03:36:18 +13:00
if ((height == 0 && width == 0) || (height == 0 && width != 0) || (height != 0 && width == 0)) {
2020-01-23 06:16:40 +13:00
return
2020-01-21 03:36:18 +13:00
}
if (height == width) {
return {
horizontal: 1,
vertical: 1,
}
}
let dividend = width
let divisor = height
let gcd = -1
if (height > width) {
dividend = height
divisor = width
}
while (gcd == -1) {
const remainder = dividend % divisor
if (remainder == 0) {
gcd = divisor
} else {
dividend = divisor
divisor = remainder
}
}
2020-01-23 06:16:40 +13:00
state.horizontal = width / gcd
state.vertical = height / gcd
2020-01-21 03:36:18 +13:00
},
2020-02-11 18:15:59 +13:00
setConfigurations(state, configurations: ScreenConfigurations) {
2020-02-12 07:43:31 +13:00
const data: ScreenResolution[] = []
for (const i of Object.keys(configurations)) {
const { width, height, rates } = configurations[i]
if (width >= 600 && height >= 300) {
for (const j of Object.keys(rates)) {
const rate = rates[j]
2020-02-12 08:48:30 +13:00
if (rate === 30 || rate === 60) {
2020-02-12 07:43:31 +13:00
data.push({
width,
height,
rate,
})
}
}
}
}
2020-02-12 08:48:30 +13:00
state.configurations = data.sort((a, b) => {
if (b.width === a.width && b.height == a.height) {
return b.rate - a.rate
} else if (b.width === a.width) {
return b.height - a.height
}
return b.width - a.width
})
2020-02-11 18:15:59 +13:00
},
2020-01-21 03:36:18 +13:00
setVolume(state, volume: number) {
state.volume = volume
2020-01-24 04:23:26 +13:00
set('volume', volume)
2020-01-21 03:36:18 +13:00
},
setStream(state, index: number) {
state.index = index
},
2020-01-23 06:16:40 +13:00
addTrack(state, [track, stream]: [MediaStreamTrack, MediaStream]) {
state.tracks = state.tracks.concat([track])
2020-01-21 03:36:18 +13:00
state.streams = state.streams.concat([stream])
},
2020-01-23 06:16:40 +13:00
delTrack(state, index: number) {
2020-01-21 03:36:18 +13:00
state.streams = state.streams.filter((_, i) => i !== index)
2020-01-23 06:16:40 +13:00
state.tracks = state.tracks.filter((_, i) => i !== index)
2020-01-21 03:36:18 +13:00
},
reset(state) {
2020-01-23 06:16:40 +13:00
state.index = -1
state.tracks = []
2020-01-21 03:36:18 +13:00
state.streams = []
2020-02-11 18:15:59 +13:00
state.configurations = []
state.width = 1280
state.height = 720
2020-02-11 18:15:59 +13:00
state.rate = 30
state.horizontal = 16
state.vertical = 9
state.playing = false
state.playable = false
2020-01-21 03:36:18 +13:00
},
})
2020-02-11 18:15:59 +13:00
export const actions = actionTree(
{ state, getters, mutations },
{
screenConfiguations({ state }) {
if (!accessor.connected || !accessor.user.admin) {
return
}
$client.sendMessage(EVENT.SCREEN.CONFIGURATIONS)
},
screenGet({ state }) {
if (!accessor.connected) {
return
}
$client.sendMessage(EVENT.SCREEN.RESOLUTION)
},
2020-02-12 07:43:31 +13:00
screenSet({ state }, resolution: ScreenResolution) {
2020-02-11 18:15:59 +13:00
if (!accessor.connected || !accessor.user.admin) {
return
}
2020-02-12 07:43:31 +13:00
$client.sendMessage(EVENT.SCREEN.SET, resolution)
2020-02-11 18:15:59 +13:00
},
},
)