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

145 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-01-21 03:36:18 +13:00
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
2020-01-23 06:16:40 +13:00
import { accessor } from '~/store'
2020-01-21 03:36:18 +13:00
export const namespaced = true
2020-01-23 06:16:40 +13:00
export const state = () => {
let volume = 100
let _volume = localStorage.getItem('volume')
if (_volume) {
volume = parseInt(_volume)
}
let muted = false
let _muted = localStorage.getItem('muted')
if (_muted) {
muted = _muted === '1'
}
return {
index: -1,
tracks: [] as MediaStreamTrack[],
streams: [] as MediaStream[],
width: 1280,
height: 720,
horizontal: 16,
vertical: 9,
volume,
muted,
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
},
setPlayable(state, playable: boolean) {
if (!playable && state.playing) {
state.playing = false
}
state.playable = playable
},
setResolution(state, { width, height }: { width: number; height: number }) {
state.width = width
state.height = height
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
},
setVolume(state, volume: number) {
state.volume = volume
2020-01-23 06:16:40 +13:00
localStorage.setItem('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
},
2020-01-23 06:16:40 +13:00
clear(state) {
state.index = -1
state.tracks = []
2020-01-21 03:36:18 +13:00
state.streams = []
},
})
export const actions = actionTree(
{ state, getters, mutations },
{
2020-01-23 06:16:40 +13:00
initialise({ commit }) {
const volume = localStorage.getItem('volume')
if (volume) {
accessor.video.setVolume(parseInt(volume))
}
},
2020-01-21 03:36:18 +13:00
},
)