owncast/web/pages/index.tsx

193 lines
5.8 KiB
TypeScript
Raw Normal View History

2020-10-26 04:57:23 +03:00
/*
Will display an overview with the following datasources:
1. Current broadcaster.
2. Viewer count.
3. Video settings.
2020-10-01 04:47:18 +03:00
2020-10-26 04:57:23 +03:00
TODO: Link each overview value to the sub-page that focuses on it.
*/
import React, { useState, useEffect, useContext } from "react";
2020-11-29 06:26:52 +03:00
import { Skeleton, Card, Statistic } from "antd";
2020-10-26 04:57:23 +03:00
import { UserOutlined, ClockCircleOutlined } from "@ant-design/icons";
import { formatDistanceToNow, formatRelative } from "date-fns";
import { ServerStatusContext } from "../utils/server-status-context";
import StatisticItem from "./components/statistic"
2020-10-30 04:01:38 +03:00
import LogTable from "./components/log-table";
2020-11-08 02:32:51 +03:00
import Offline from './offline-notice';
2020-10-30 04:01:38 +03:00
2020-10-26 04:57:23 +03:00
import {
2020-10-30 04:01:38 +03:00
LOGS_WARN,
2020-10-26 04:57:23 +03:00
fetchData,
FETCH_INTERVAL,
2020-11-01 10:01:37 +03:00
} from "../utils/apis";
import { formatIPAddress, isEmptyObject } from "../utils/format";
2020-10-26 04:57:23 +03:00
2020-11-29 06:26:52 +03:00
function streamDetailsFormatter(streamDetails) {
return (
<ul className="statistics-list">
<li>{streamDetails.videoCodec || 'Unknown'} @ {streamDetails.videoBitrate || 'Unknown'} kbps</li>
<li>{streamDetails.framerate || 'Unknown'} fps</li>
2020-11-29 06:26:52 +03:00
<li>{streamDetails.width} x {streamDetails.height}</li>
</ul>
);
}
2020-11-08 02:32:51 +03:00
export default function Home() {
const serverStatusData = useContext(ServerStatusContext);
const { broadcaster, serverConfig: configData } = serverStatusData || {};
2020-10-26 04:57:23 +03:00
const { remoteAddr, streamDetails } = broadcaster || {};
const encoder = streamDetails?.encoder || "Unknown encoder";
2020-10-26 04:57:23 +03:00
const [logsData, setLogs] = useState([]);
2020-10-30 04:01:38 +03:00
const getLogs = async () => {
try {
const result = await fetchData(LOGS_WARN);
setLogs(result);
} catch (error) {
console.log("==== error", error);
}
};
const getMoreStats = () => {
getLogs();
}
2020-10-26 04:57:23 +03:00
useEffect(() => {
getMoreStats();
let intervalId = null;
intervalId = setInterval(getMoreStats, FETCH_INTERVAL);
return () => {
clearInterval(intervalId);
}
2020-10-26 04:57:23 +03:00
}, []);
if (isEmptyObject(configData) || isEmptyObject(serverStatusData)) {
2020-10-26 04:57:23 +03:00
return (
<>
2020-10-26 04:57:23 +03:00
<Skeleton active />
<Skeleton active />
<Skeleton active />
</>
2020-10-26 04:57:23 +03:00
);
}
2020-10-23 03:16:28 +03:00
2020-10-26 04:57:23 +03:00
if (!broadcaster) {
return <Offline logs={logsData} />;
2020-10-26 04:57:23 +03:00
}
2020-10-29 04:59:17 +03:00
// map out settings
const videoQualitySettings = configData?.videoSettings?.videoQualityVariants?.map((setting, index) => {
const { audioPassthrough, videoPassthrough, audioBitrate, videoBitrate, framerate } = setting;
const audioSetting = audioPassthrough
? `${streamDetails.audioCodec || 'Unknown'}, ${streamDetails.audioBitrate} kbps`
: `${audioBitrate || 'Unknown'} kbps`;
const videoSetting = videoPassthrough
? `${streamDetails.videoBitrate || 'Unknown'} kbps, ${streamDetails.framerate} fps ${streamDetails.width} x ${streamDetails.height}`
: `${videoBitrate || 'Unknown'} kbps, ${framerate} fps`;
2020-10-26 04:57:23 +03:00
let settingTitle = 'Outbound Stream Details';
settingTitle = (videoQualitySettings?.length > 1) ?
`${settingTitle} ${index + 1}` : settingTitle;
2020-10-26 04:57:23 +03:00
return (
<Card title={settingTitle} type="inner" key={`${settingTitle}${index}`}>
<StatisticItem
2020-10-28 10:53:24 +03:00
title="Outbound Video Stream"
value={videoSetting}
2020-10-28 10:53:24 +03:00
prefix={null}
/>
<StatisticItem
2020-10-28 10:53:24 +03:00
title="Outbound Audio Stream"
value={audioSetting}
prefix={null}
/>
</Card>
2020-10-26 04:57:23 +03:00
);
});
2020-11-29 06:26:52 +03:00
// inbound
const { viewerCount, sessionPeakViewerCount } = serverStatusData;
2020-11-29 06:26:52 +03:00
const streamAudioDetailString = `${streamDetails.audioCodec}, ${streamDetails.audioBitrate || 'Unknown'} kbps`;
const broadcastDate = new Date(broadcaster.time);
2020-10-26 04:57:23 +03:00
return (
<div className="home-container">
<div className="sections-container">
<div className="section online-status-section">
<Card title="Stream is online" type="inner">
<Statistic
title={`Stream started ${formatRelative(
broadcastDate,
Date.now()
)}`}
value={formatDistanceToNow(broadcastDate)}
prefix={<ClockCircleOutlined />}
/>
<Statistic
title="Viewers"
value={viewerCount}
prefix={<UserOutlined />}
/>
<Statistic
title="Peak viewer count"
value={sessionPeakViewerCount}
prefix={<UserOutlined />}
/>
</Card>
</div>
2020-10-26 04:57:23 +03:00
<div className="section stream-details-section">
2020-10-26 04:57:23 +03:00
<div className="details outbound-details">
{videoQualitySettings}
</div>
2020-10-29 04:59:17 +03:00
<div className="details other-details">
<Card title="Inbound Stream Details" type="inner">
<StatisticItem
title="Input"
value={`${encoder} ${formatIPAddress(remoteAddr)}`}
prefix={null}
/>
<StatisticItem
title="Inbound Video Stream"
2020-11-29 06:26:52 +03:00
value={streamDetails}
formatter={streamDetailsFormatter}
prefix={null}
/>
<StatisticItem
title="Inbound Audio Stream"
value={streamAudioDetailString}
prefix={null}
/>
</Card>
<div className="server-detail">
<Card title="Server Config" type="inner">
<StatisticItem
title="Stream key"
value={configData.streamKey}
prefix={null}
/>
<StatisticItem
title="Directory registration enabled"
value={configData.yp.enabled.toString()}
prefix={null}
/>
</Card>
</div>
</div>
2020-12-04 09:55:04 +03:00
</div>
</div>
2020-11-13 14:57:57 +03:00
<LogTable logs={logsData} pageSize={5} />
2020-10-26 04:57:23 +03:00
</div>
);
}