owncast/web/pages/index.tsx

250 lines
6.2 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";
2020-11-04 02:09:11 +01:00
import { Row, Skeleton, Result, List, Typography, Card } from "antd";
2020-10-26 02:57:23 +01:00
import { UserOutlined, ClockCircleOutlined } from "@ant-design/icons";
import { formatDistanceToNow, formatRelative } from "date-fns";
2020-11-01 08:01:37 +01:00
import { BroadcastStatusContext } from "../utils/broadcast-status-context";
import StatisticItem from "./components/statistic"
2020-10-30 02:01:38 +01:00
import LogTable from "./components/log-table";
2020-10-26 02:57:23 +01:00
import {
STREAM_STATUS,
SERVER_CONFIG,
2020-10-30 02:01:38 +01:00
LOGS_WARN,
2020-10-26 02:57:23 +01:00
fetchData,
FETCH_INTERVAL,
2020-11-01 08:01:37 +01:00
} from "../utils/apis";
import { formatIPAddress, isEmptyObject } from "../utils/format";
2020-11-03 09:03:40 +01:00
import OwncastLogo from "./components/logo"
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() {
2020-11-04 02:09:11 +01:00
const data = [
{
title: "Send some test content",
content: (
<div>
With any video you have around you can pass it to the test script and start streaming it.
<blockquote>
<em>./test/ocTestStream.sh yourVideo.mp4</em>
</blockquote>
</div>
),
},
{
title: "Use your broadcasting software",
content: (
<div>
<a href="https://owncast.online/docs/broadcasting/">Learn how to point your existing software to your new server and start streaming your content.</a>
</div>
)
},
{
title: "Something else",
},
];
2020-10-28 08:53:24 +01:00
return (
<div>
2020-11-04 02:09:11 +01:00
<Result
icon={<OwncastLogo />}
title="No stream is active."
subTitle="You should start one."
/>
<List
grid={{
gutter: 16,
xs: 1,
sm: 2,
md: 4,
lg: 4,
xl: 6,
xxl: 3,
2020-11-03 09:03:40 +01:00
}}
2020-11-04 02:09:11 +01:00
dataSource={data}
renderItem={(item) => (
<List.Item>
<Card title={item.title}>{item.content}</Card>
</List.Item>
)}
2020-10-28 08:53:24 +01:00
/>
</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);
}
2020-10-30 02:01:38 +01:00
getConfig();
getLogs();
2020-10-26 02:57:23 +01:00
};
// Pull in the server config so we can show config overview.
2020-11-01 08:01:37 +01:00
const [config, setConfig] = useState({
streamKey: "",
yp: {
enabled: false,
},
videoSettings: {
videoQualityVariants: [
{
audioPassthrough: false,
videoBitrate: 0,
audioBitrate: 0,
framerate: 0,
},
],
},
});
2020-10-30 02:01:38 +01:00
const [logs, setLogs] = useState([]);
2020-10-26 02:57:23 +01:00
const getConfig = async () => {
try {
const result = await fetchData(SERVER_CONFIG);
2020-10-29 02:59:17 +01:00
setConfig(result);
2020-10-26 02:57:23 +01:00
} catch (error) {
console.log(error);
}
};
2020-10-30 02:01:38 +01:00
const getLogs = async () => {
try {
const result = await fetchData(LOGS_WARN);
setLogs(result);
} catch (error) {
console.log("==== error", error);
}
};
2020-10-26 02:57:23 +01:00
useEffect(() => {
setInterval(getStats, FETCH_INTERVAL);
getStats();
}, []);
2020-10-29 02:59:17 +01:00
if (isEmptyObject(config) || 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-29 02:59:17 +01:00
const videoSettings = config.videoSettings.videoQualityVariants;
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 (
2020-11-01 08:01:37 +01:00
// eslint-disable-next-line react/no-array-index-key
2020-10-26 02:57:23 +01:00
<Row gutter={[16, 16]} key={index}>
<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>
);
});
2020-10-30 02:01:38 +01:00
const logTable = logs.length > 0 ? <LogTable logs={logs} pageSize={5} /> : null
2020-10-26 02:57:23 +01:00
const { viewerCount, sessionMaxViewerCount, lastConnectTime } = stats;
2020-10-30 02:01:38 +01:00
const streamVideoDetailString = `${streamDetails.videoCodec} ${streamDetails.videoBitrate} kbps ${streamDetails.width}x${streamDetails.height}`;
2020-10-26 02:57:23 +01:00
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}
2020-10-29 02:59:17 +01:00
<Row gutter={[16, 16]}>
<StatisticItem
title="Stream key"
value={config.streamKey}
prefix={null}
/>
<StatisticItem
title="Directory registration enabled"
value={config.yp.enabled.toString()}
prefix={null}
/>
</Row>
2020-10-30 02:01:38 +01:00
{logTable}
2020-10-26 02:57:23 +01:00
</div>
);
}