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

126 lines
2.6 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-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[],
width: 1280,
height: 720,
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
},
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-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 = []
state.width = 1280
state.height = 720
state.horizontal = 16
state.vertical = 9
state.playing = false
state.playable = false
2020-01-21 03:36:18 +13:00
},
})