owncast/web/pages/components/config/video-latency.tsx

126 lines
3.3 KiB
TypeScript
Raw Normal View History

import React, { useContext, useState, useEffect } from 'react';
2021-01-31 10:38:20 +01:00
import { Typography, Slider } from 'antd';
2021-01-17 09:40:46 +01:00
import { ServerStatusContext } from '../../../utils/server-status-context';
2021-01-31 10:38:20 +01:00
import {
API_VIDEO_SEGMENTS,
SUCCESS_STATES,
RESET_TIMEOUT,
postConfigUpdateToAPI,
} from './constants';
2021-01-17 09:40:46 +01:00
const { Title } = Typography;
const SLIDER_MARKS = {
1: 'low',
2: '',
3: '',
4: '',
5: '',
6: 'high',
2021-01-17 09:40:46 +01:00
};
const SLIDER_COMMENTS = {
1: 'Lowest latency, but least reliability',
2: 'Low latency, some reliability',
3: 'Lower latency, some reliability',
4: 'Optimal latency and reliability (Default)',
5: 'High latency, better reliability',
6: 'Highest latency, higher reliability',
2021-01-17 09:40:46 +01:00
};
interface SegmentToolTipProps {
value: string;
}
function SegmentToolTip({ value }: SegmentToolTipProps) {
2021-01-31 10:38:20 +01:00
return <span className="segment-tip">{value}</span>;
2021-01-17 09:40:46 +01:00
}
export default function VideoLatency() {
2021-01-17 09:40:46 +01:00
const [submitStatus, setSubmitStatus] = useState(null);
const [submitStatusMessage, setSubmitStatusMessage] = useState('');
const [selectedOption, setSelectedOption] = useState(null);
2021-01-17 09:40:46 +01:00
const serverStatusData = useContext(ServerStatusContext);
const { serverConfig, setFieldInConfigState } = serverStatusData || {};
const { videoSettings } = serverConfig || {};
let resetTimer = null;
if (!videoSettings) {
return null;
}
useEffect(() => {
setSelectedOption(videoSettings.latencyLevel);
}, [videoSettings]);
2021-01-17 09:40:46 +01:00
const resetStates = () => {
setSubmitStatus(null);
setSubmitStatusMessage('');
resetTimer = null;
clearTimeout(resetTimer);
2021-01-31 10:38:20 +01:00
};
2021-01-17 09:40:46 +01:00
// posts all the variants at once as an array obj
const postUpdateToAPI = async (postValue: any) => {
await postConfigUpdateToAPI({
apiPath: API_VIDEO_SEGMENTS,
data: { value: postValue },
onSuccess: () => {
setFieldInConfigState({
2021-01-31 10:38:20 +01:00
fieldName: 'latencyLevel',
value: postValue,
path: 'videoSettings',
2021-01-17 09:40:46 +01:00
});
setSubmitStatus('success');
setSubmitStatusMessage('Variants updated.');
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
},
onError: (message: string) => {
setSubmitStatus('error');
setSubmitStatusMessage(message);
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
},
});
};
2021-01-31 10:38:20 +01:00
const { icon: newStatusIcon = null, message: newStatusMessage = '' } =
SUCCESS_STATES[submitStatus] || {};
2021-01-17 09:40:46 +01:00
const statusMessage = (
<div className={`status-message ${submitStatus || ''}`}>
{newStatusIcon} {newStatusMessage} {submitStatusMessage}
</div>
);
2021-01-31 10:38:20 +01:00
const handleChange = value => {
postUpdateToAPI(value);
2021-01-17 09:40:46 +01:00
};
return (
<div className="module-container config-video-segements-conatiner">
2021-01-31 07:53:00 +01:00
<Title level={3}>Latency Buffer</Title>
2021-01-17 09:40:46 +01:00
<p>
2021-01-31 10:38:20 +01:00
There are trade-offs when cosidering video latency and reliability. Blah blah .. better
wording here needed.
2021-01-17 09:40:46 +01:00
</p>
2021-01-31 10:38:20 +01:00
<br />
<br />
2021-01-17 09:40:46 +01:00
<div className="segment-slider">
2021-01-31 10:38:20 +01:00
<Slider
2021-01-17 09:40:46 +01:00
tipFormatter={value => <SegmentToolTip value={SLIDER_COMMENTS[value]} />}
onChange={handleChange}
min={1}
max={6}
2021-01-17 09:40:46 +01:00
marks={SLIDER_MARKS}
2021-01-17 11:01:40 +01:00
defaultValue={selectedOption}
2021-01-17 09:40:46 +01:00
value={selectedOption}
/>
</div>
{statusMessage}
</div>
);
2021-01-31 10:38:20 +01:00
}