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

140 lines
4.1 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';
import { ServerStatusContext } from '../../utils/server-status-context';
import { AlertMessageContext } from '../../utils/alert-message-context';
import {
API_VIDEO_SEGMENTS,
RESET_TIMEOUT,
postConfigUpdateToAPI,
} from '../../utils/config-constants';
2021-01-31 10:38:20 +01:00
import {
createInputStatus,
StatusState,
STATUS_ERROR,
STATUS_PROCESSING,
STATUS_SUCCESS,
} from '../../utils/input-statuses';
import FormStatusIndicator from './form-status-indicator';
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, lowest error tolerance',
2: 'Low latency, low error tolerance',
3: 'Lower latency, lower error tolerance',
4: 'Medium latency, medium error tolerance (Default)',
5: 'Higher latency, higher error tolerance',
6: 'Highest latency, highest error tolerance',
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() {
const [submitStatus, setSubmitStatus] = useState<StatusState>(null);
// 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 { setMessage } = useContext(AlertMessageContext);
2021-01-17 09:40:46 +01:00
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('');
2021-01-17 09:40:46 +01:00
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) => {
setSubmitStatus(createInputStatus(STATUS_PROCESSING));
2021-01-17 09:40:46 +01:00
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(createInputStatus(STATUS_SUCCESS, 'Latency buffer level updated.'));
2021-01-17 09:40:46 +01:00
// setSubmitStatus('success');
// setSubmitStatusMessage('Variants updated.');
2021-01-17 09:40:46 +01:00
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
if (serverStatusData.online) {
setMessage(
'Your latency buffer setting will take effect the next time you begin a live stream.',
);
}
2021-01-17 09:40:46 +01:00
},
onError: (message: string) => {
setSubmitStatus(createInputStatus(STATUS_ERROR, message));
// setSubmitStatus('error');
// setSubmitStatusMessage(message);
2021-01-17 09:40:46 +01:00
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
},
});
};
const handleChange = value => {
postUpdateToAPI(value);
2021-01-17 09:40:46 +01:00
};
return (
<div className="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>
While it's natural to want to keep your latency as low as possible, you may experience
reduced error tolerance and stability in some environments the lower you go.
2021-01-17 09:40:46 +01:00
</p>
For interactive live streams you may want to experiment with a lower latency, for
non-interactive broadcasts you may want to increase it.{' '}
<a href="https://owncast.online/docs/encoding#latency-buffer">Read to learn more.</a>
<p></p>
<div className="segment-slider-container">
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>
<FormStatusIndicator status={submitStatus} />
2021-01-17 09:40:46 +01:00
</div>
);
2021-01-31 10:38:20 +01:00
}