mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2026-04-22 23:57:20 +08:00
299 lines
10 KiB
HTML
299 lines
10 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>go2rtc</title>
|
|
<style>
|
|
.controls {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 10px;
|
|
align-items: center;
|
|
}
|
|
|
|
.info {
|
|
color: #888;
|
|
white-space: pre;
|
|
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<script src="main.js"></script>
|
|
|
|
<main>
|
|
<div class="controls">
|
|
<button>stream</button>
|
|
modes
|
|
<label><input type="checkbox" name="webrtc" checked>webrtc</label>
|
|
<label><input type="checkbox" name="mse" checked>mse</label>
|
|
<label><input type="checkbox" name="hls" checked>hls</label>
|
|
<label><input type="checkbox" name="mjpeg" checked>mjpeg</label>
|
|
</div>
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th><label><input id="selectall" type="checkbox">name</label></th>
|
|
<th>online</th>
|
|
<th>commands</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="streams">
|
|
</tbody>
|
|
</table>
|
|
<div class="info"></div>
|
|
</main>
|
|
|
|
<script>
|
|
const templates = [
|
|
'<a href="stream.html?src={name}">stream</a>',
|
|
'<a href="links.html?src={name}">links</a>',
|
|
'<a href="#" data-name="{name}">delete</a>',
|
|
];
|
|
|
|
document.querySelector('.controls > button')
|
|
.addEventListener('click', () => {
|
|
const url = new URL('stream.html', location.href);
|
|
|
|
const streams = document.querySelectorAll('#streams input');
|
|
streams.forEach(i => {
|
|
if (i.checked) url.searchParams.append('src', i.name);
|
|
});
|
|
|
|
if (!url.searchParams.has('src')) return;
|
|
|
|
let mode = document.querySelectorAll('.controls input');
|
|
mode = Array.from(mode).filter(i => i.checked).map(i => i.name).join(',');
|
|
|
|
window.location.href = `${url}&mode=${mode}`;
|
|
});
|
|
|
|
const tbody = document.getElementById('streams');
|
|
tbody.addEventListener('click', async ev => {
|
|
if (ev.target.innerText !== 'delete') return;
|
|
|
|
ev.preventDefault();
|
|
|
|
const src = decodeURIComponent(ev.target.dataset.name);
|
|
|
|
const message = `Please type the name of the stream "${src}" to confirm its deletion from the configuration. This action is irreversible.`;
|
|
if (prompt(message) !== src) {
|
|
alert('Stream name does not match. Deletion cancelled.');
|
|
return;
|
|
}
|
|
|
|
const url = new URL('api/streams', location.href);
|
|
url.searchParams.set('src', src);
|
|
|
|
try {
|
|
await fetch(url, {method: 'DELETE'});
|
|
reload();
|
|
} catch (error) {
|
|
console.error('Failed to delete the stream:', error);
|
|
}
|
|
});
|
|
|
|
document.getElementById('selectall').addEventListener('change', ev => {
|
|
document.querySelectorAll('#streams input').forEach(el => {
|
|
el.checked = ev.target.checked;
|
|
});
|
|
});
|
|
|
|
function reload() {
|
|
const url = new URL('api/streams', location.href);
|
|
const checkboxStates = {};
|
|
tbody.querySelectorAll('input[type="checkbox"][name]').forEach(checkbox => {
|
|
checkboxStates[checkbox.name] = checkbox.checked;
|
|
});
|
|
fetch(url, {cache: 'no-cache'}).then(r => r.json()).then(data => {
|
|
const existingIds = Array.from(tbody.querySelectorAll('tr')).map(tr => tr.dataset['id']);
|
|
const fetchedIds = [];
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
const name = key.replace(/[<">]/g, ''); // sanitize
|
|
fetchedIds.push(name);
|
|
|
|
let tr = tbody.querySelector(`tr[data-id="${name}"]`);
|
|
const online = value && value.consumers ? value.consumers.length : 0;
|
|
const src = encodeURIComponent(name);
|
|
const links = templates.map(link => link.replace('{name}', src)).join(' ');
|
|
|
|
if (!tr) {
|
|
tr = document.createElement('tr');
|
|
tr.dataset['id'] = name;
|
|
tbody.appendChild(tr);
|
|
}
|
|
|
|
const isChecked = checkboxStates[name] ? 'checked' : '';
|
|
tr.innerHTML =
|
|
`<td><label><input type="checkbox" name="${name}" ${isChecked}>${name}</label></td>` +
|
|
`<td><a href="api/streams?src=${src}">${online} / info</a> / <a href="api/streams?src=${src}&video=all&audio=allµphone">probe</a> / <a href="net.html?src=${src}">net</a></td>` +
|
|
`<td>${links}</td>`;
|
|
}
|
|
|
|
// Remove old rows
|
|
existingIds.forEach(id => {
|
|
if (!fetchedIds.includes(id)) {
|
|
const trToRemove = tbody.querySelector(`tr[data-id="${id}"]`);
|
|
tbody.removeChild(trToRemove);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// Auto-reload
|
|
setInterval(reload, 1000);
|
|
|
|
const info = document.querySelector('.info');
|
|
const infoURL = new URL('api', location.href);
|
|
const cpuHistory = [];
|
|
const memHistory = [];
|
|
const timeHistory = [];
|
|
const graphWidth = 36;
|
|
const graphHeight = 8;
|
|
const infoUpdateInterval = window.SYSTEM_INFO_UPDATE_INTERVAL_MS ?? 2000;
|
|
let infoUpdateTimer = null;
|
|
|
|
function toNumber(value) {
|
|
const number = Number(value);
|
|
return Number.isFinite(number) ? number : 0;
|
|
}
|
|
|
|
function clampPercent(value) {
|
|
return Math.max(0, Math.min(100, toNumber(value)));
|
|
}
|
|
|
|
function pushHistory(history, value) {
|
|
history.push(value);
|
|
while (history.length > graphWidth) history.shift();
|
|
}
|
|
|
|
function formatBytes(bytes) {
|
|
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
let value = Math.max(0, toNumber(bytes));
|
|
let index = 0;
|
|
while (value >= 1024 && index < units.length - 1) {
|
|
value /= 1024;
|
|
index++;
|
|
}
|
|
return `${value.toFixed(value >= 100 || index === 0 ? 0 : 1)} ${units[index]}`;
|
|
}
|
|
|
|
function formatClock(timeMs) {
|
|
return new Date(timeMs).toLocaleTimeString(undefined, {
|
|
hour: '2-digit', minute: '2-digit', second: '2-digit'
|
|
});
|
|
}
|
|
|
|
function renderXAxisLabels(width) {
|
|
if (!timeHistory.length) return ' '.repeat(width);
|
|
|
|
const start = formatClock(timeHistory[0]);
|
|
const end = formatClock(timeHistory[timeHistory.length - 1]);
|
|
if (start.length + end.length >= width) return `${start} ${end}`;
|
|
|
|
return `${start}${' '.repeat(width - start.length - end.length)}${end}`;
|
|
}
|
|
|
|
function renderAsciiGraphLines(history) {
|
|
const bars = Array.from({length: graphWidth}, (_, index) => {
|
|
const value = history[index] ?? 0;
|
|
return Math.round((value / 100) * graphHeight);
|
|
});
|
|
const lines = [];
|
|
for (let row = graphHeight; row >= 1; row--) {
|
|
let line = '|';
|
|
for (const bar of bars) {
|
|
line += bar >= row ? '█' : ' ';
|
|
}
|
|
line += '|';
|
|
lines.push(line);
|
|
}
|
|
lines.push('+' + '-'.repeat(graphWidth) + '+');
|
|
return lines;
|
|
}
|
|
|
|
function padRight(text, width) {
|
|
return text + ' '.repeat(Math.max(0, width - text.length));
|
|
}
|
|
|
|
function renderInfo(data, cpuUsage, memUsage, memUsed, memTotal, isUsageUnsupported) {
|
|
const detailsLines = [];
|
|
if (isUsageUnsupported) {
|
|
detailsLines.push('System metrics are not supported on this platform (CPU and MEM usage are 0).');
|
|
} else {
|
|
const cpuLines = renderAsciiGraphLines(cpuHistory);
|
|
const memLines = renderAsciiGraphLines(memHistory);
|
|
const graphBlockWidth = graphWidth + 2; // borders
|
|
const cpuTitle = `CPU ${cpuUsage.toFixed(1)}%`;
|
|
const memTitle = `MEM ${memUsage.toFixed(1)}% (${formatBytes(memUsed)} / ${formatBytes(memTotal)})`;
|
|
|
|
detailsLines.push(
|
|
`${padRight(cpuTitle, graphBlockWidth)} ${padRight(memTitle, graphBlockWidth)}`
|
|
);
|
|
for (let i = 0; i < cpuLines.length; i++) {
|
|
detailsLines.push(`${cpuLines[i]} ${memLines[i]}`);
|
|
}
|
|
const timeLabels = renderXAxisLabels(graphBlockWidth);
|
|
detailsLines.push(`${timeLabels} ${timeLabels}`);
|
|
}
|
|
|
|
const lines = [
|
|
`version: ${data.version ?? '-'}`,
|
|
`pid: ${data.pid ?? '-'}`,
|
|
`config: ${data.config_path ?? '-'}`,
|
|
'',
|
|
...detailsLines,
|
|
];
|
|
info.textContent = lines.join('\n');
|
|
}
|
|
|
|
function startInfoUpdates() {
|
|
if (infoUpdateTimer !== null) return;
|
|
infoUpdateTimer = setInterval(updateInfo, infoUpdateInterval);
|
|
}
|
|
|
|
function stopInfoUpdates() {
|
|
if (infoUpdateTimer === null) return;
|
|
clearInterval(infoUpdateTimer);
|
|
infoUpdateTimer = null;
|
|
}
|
|
|
|
function updateInfo() {
|
|
return fetch(infoURL, {cache: 'no-cache'}).then(r => r.json()).then(data => {
|
|
const cpuUsage = clampPercent(data.system?.cpu_usage);
|
|
const memUsed = toNumber(data.system?.mem_used);
|
|
const memTotal = toNumber(data.system?.mem_total);
|
|
const memUsage = memTotal > 0 ? clampPercent((memUsed * 100) / memTotal) : 0;
|
|
const isUsageUnsupported = cpuUsage === 0 && memUsage === 0;
|
|
|
|
if (!isUsageUnsupported) {
|
|
const now = Date.now();
|
|
pushHistory(cpuHistory, cpuUsage);
|
|
pushHistory(memHistory, memUsage);
|
|
pushHistory(timeHistory, now);
|
|
} else {
|
|
stopInfoUpdates();
|
|
}
|
|
renderInfo(data, cpuUsage, memUsage, memUsed, memTotal, isUsageUnsupported);
|
|
return !isUsageUnsupported;
|
|
}).catch(error => {
|
|
if (!info.textContent) {
|
|
info.textContent = `Can't load system info: ${error.message}`;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
updateInfo().then(isSupported => {
|
|
if (isSupported) startInfoUpdates();
|
|
});
|
|
|
|
reload();
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|