owncast/web/pages/index.tsx

165 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-10-26 02:57:23 +01:00
/*
Will display an overview with the following datasources:
1. Current broadcaster.
2. Viewer count.
3. Video settings.
2020-10-01 03:47:18 +02:00
2020-10-26 02:57:23 +01:00
TODO: Link each overview value to the sub-page that focuses on it.
*/
import React, { useState, useEffect, useContext } from "react";
import { Row, Skeleton, Empty, Typography } from "antd";
2020-10-26 02:57:23 +01:00
import { UserOutlined, ClockCircleOutlined } from "@ant-design/icons";
import { formatDistanceToNow, formatRelative } from "date-fns";
import { BroadcastStatusContext } from "./utils/broadcast-status-context";
import StatisticItem from "./components/statistic"
2020-10-26 02:57:23 +01:00
import {
STREAM_STATUS,
SERVER_CONFIG,
fetchData,
FETCH_INTERVAL,
} from "./utils/apis";
2020-10-28 08:53:24 +01:00
import { formatIPAddress, isEmptyObject } from "./utils/format";
2020-10-26 02:57:23 +01:00
2020-10-28 08:53:24 +01:00
const { Title } = Typography;
2020-10-26 02:57:23 +01:00
2020-10-28 08:53:24 +01:00
function Offline() {
return (
<div>
<Empty
image={Empty.PRESENTED_IMAGE_SIMPLE}
description={
<span>There is no stream currently active. Start one.</span>
}
/>
</div>
);
}
2020-10-26 02:57:23 +01:00
export default function Stats() {
const context = useContext(BroadcastStatusContext);
const { broadcaster } = context || {};
const { remoteAddr, streamDetails } = broadcaster || {};
// Pull in the server status so we can show server overview.
const [stats, setStats] = useState(null);
const getStats = async () => {
try {
const result = await fetchData(STREAM_STATUS);
setStats(result);
} catch (error) {
console.log(error);
}
};
// Pull in the server config so we can show config overview.
const [videoSettings, setVideoSettings] = useState([]);
const getConfig = async () => {
try {
const result = await fetchData(SERVER_CONFIG);
2020-10-28 08:53:24 +01:00
const variants = result && result.videoSettings && result.videoSettings.videoQualityVariants;
setVideoSettings(variants);
2020-10-26 02:57:23 +01:00
} catch (error) {
console.log(error);
}
};
useEffect(() => {
setInterval(getStats, FETCH_INTERVAL);
getStats();
getConfig();
}, []);
2020-10-28 08:53:24 +01:00
if (!stats || isEmptyObject(stats)) {
2020-10-26 02:57:23 +01:00
return (
2020-10-23 02:16:28 +02:00
<div>
2020-10-26 02:57:23 +01:00
<Skeleton active />
<Skeleton active />
<Skeleton active />
2020-10-23 02:16:28 +02:00
</div>
2020-10-26 02:57:23 +01:00
);
}
2020-10-23 02:16:28 +02:00
2020-10-26 02:57:23 +01:00
if (!broadcaster) {
2020-10-28 08:53:24 +01:00
return <Offline />;
2020-10-26 02:57:23 +01:00
}
2020-10-28 08:53:24 +01:00
const videoQualitySettings = videoSettings.map((setting, index) => {
2020-10-26 02:57:23 +01:00
const audioSetting =
setting.audioPassthrough || setting.audioBitrate === 0
? `${streamDetails.audioBitrate} kpbs (passthrough)`
: `${setting.audioBitrate} kbps`;
return (
<Row gutter={[16, 16]} key={index}>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Output"
value={`Video variant ${index}`}
prefix={null}
/>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Outbound Video Stream"
value={`${setting.videoBitrate} kbps ${setting.framerate} fps`}
prefix={null}
/>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Outbound Audio Stream"
value={audioSetting}
prefix={null}
/>
2020-10-26 02:57:23 +01:00
</Row>
);
});
const { viewerCount, sessionMaxViewerCount, lastConnectTime } = stats;
const streamVideoDetailString = `${streamDetails.width}x${streamDetails.height} ${streamDetails.videoBitrate} kbps ${streamDetails.framerate} fps `;
const streamAudioDetailString = `${streamDetails.audioCodec} ${streamDetails.audioBitrate} kpbs`;
return (
<div>
<Title>Server Overview</Title>
<Row gutter={[16, 16]}>
<StatisticItem
2020-10-28 08:53:24 +01:00
title={`Stream started ${formatRelative(
2020-10-26 02:57:23 +01:00
new Date(lastConnectTime),
new Date()
2020-10-28 08:53:24 +01:00
)}`}
value={formatDistanceToNow(new Date(lastConnectTime))}
prefix={<ClockCircleOutlined />}
/>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Viewers"
value={viewerCount}
prefix={<UserOutlined />}
/>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Peak viewer count"
value={sessionMaxViewerCount}
prefix={<UserOutlined />}
/>
2020-10-26 02:57:23 +01:00
</Row>
<Row gutter={[16, 16]}>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Input"
value={formatIPAddress(remoteAddr)}
prefix={null}
/>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Inbound Video Stream"
value={streamVideoDetailString}
prefix={null}
/>
<StatisticItem
2020-10-28 08:53:24 +01:00
title="Inbound Audio Stream"
value={streamAudioDetailString}
prefix={null}
/>
2020-10-01 00:12:10 +02:00
</Row>
2020-10-26 02:57:23 +01:00
{videoQualitySettings}
</div>
);
}