Claude: WebRTC
This commit is contained in:
@@ -5,66 +5,103 @@ const ICE_SERVERS = [
|
||||
{ urls: 'stun:stun1.l.google.com:19302' },
|
||||
];
|
||||
|
||||
// Wartet bis ICE-Gathering fertig ist (max timeoutMs)
|
||||
function log(camId, msg) {
|
||||
console.log(`[${camId}] ${msg}`);
|
||||
}
|
||||
|
||||
function waitIceComplete(pc, timeoutMs = 5000) {
|
||||
return new Promise(resolve => {
|
||||
if (pc.iceGatheringState === 'complete') { resolve(); return; }
|
||||
const check = () => { if (pc.iceGatheringState === 'complete') resolve(); };
|
||||
pc.addEventListener('icegatheringstatechange', check);
|
||||
setTimeout(() => { pc.removeEventListener('icegatheringstatechange', check); resolve(); }, timeoutMs);
|
||||
setTimeout(() => {
|
||||
pc.removeEventListener('icegatheringstatechange', check);
|
||||
log('ice', `gathering timeout nach ${timeoutMs}ms – sende trotzdem`);
|
||||
resolve();
|
||||
}, timeoutMs);
|
||||
});
|
||||
}
|
||||
|
||||
async function startWebRTC(camId, videoEl, statusEl) {
|
||||
statusEl.textContent = 'Verbinde...';
|
||||
setStatus(statusEl, 'Verbinde...', '#888');
|
||||
log(camId, `WebRTC start → /api/webrtc?src=${camId}`);
|
||||
|
||||
let pc;
|
||||
try {
|
||||
pc = new RTCPeerConnection({ iceServers: ICE_SERVERS });
|
||||
|
||||
// Nur Video empfangen, kein Audio
|
||||
pc.addTransceiver('video', { direction: 'recvonly' });
|
||||
|
||||
pc.ontrack = ({ streams }) => {
|
||||
if (!streams[0]) return;
|
||||
videoEl.srcObject = streams[0];
|
||||
videoEl.play().catch(() => {});
|
||||
pc.onicecandidate = ({ candidate }) => {
|
||||
if (candidate) log(camId, `ICE candidate: ${candidate.type} ${candidate.address ?? '?'}`);
|
||||
};
|
||||
|
||||
pc.onicegatheringstatechange = () =>
|
||||
log(camId, `ICE gathering: ${pc.iceGatheringState}`);
|
||||
|
||||
pc.oniceconnectionstatechange = () => {
|
||||
const s = pc.iceConnectionState;
|
||||
statusEl.textContent = { connected: 'Live ✓', completed: 'Live ✓' }[s] ?? s;
|
||||
log(camId, `ICE connection: ${s}`);
|
||||
const colors = { connected: '#4c4', completed: '#4c4', checking: '#fa0', failed: '#c44', disconnected: '#c44' };
|
||||
setStatus(statusEl, s, colors[s] ?? '#888');
|
||||
if (s === 'failed' || s === 'closed') {
|
||||
pc.close();
|
||||
setTimeout(() => startWebRTC(camId, videoEl, statusEl), 4000);
|
||||
}
|
||||
};
|
||||
|
||||
// SDP Offer erstellen und warten bis alle ICE-Kandidaten gesammelt sind
|
||||
pc.onconnectionstatechange = () =>
|
||||
log(camId, `connection: ${pc.connectionState}`);
|
||||
|
||||
pc.ontrack = ({ streams }) => {
|
||||
log(camId, `Track erhalten: ${streams.length} stream(s)`);
|
||||
if (streams[0]) {
|
||||
videoEl.srcObject = streams[0];
|
||||
videoEl.play().catch(e => log(camId, `play() Fehler: ${e.message}`));
|
||||
setStatus(statusEl, 'Live ✓', '#4c4');
|
||||
}
|
||||
};
|
||||
|
||||
log(camId, 'Erstelle SDP Offer...');
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
log(camId, `ICE gathering wartet (max 5s)...`);
|
||||
await waitIceComplete(pc);
|
||||
|
||||
// Signaling über Node.js-Proxy (kein separater go2rtc-Port nach aussen nötig)
|
||||
log(camId, `Sende Offer (${pc.localDescription.sdp.length} Bytes) an Server...`);
|
||||
const resp = await fetch(`/api/webrtc?src=${encodeURIComponent(camId)}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/sdp' },
|
||||
body: pc.localDescription.sdp,
|
||||
});
|
||||
|
||||
if (!resp.ok) throw new Error(`Signaling HTTP ${resp.status}: ${await resp.text()}`);
|
||||
if (!resp.ok) {
|
||||
const body = await resp.text();
|
||||
throw new Error(`Signaling HTTP ${resp.status}: ${body}`);
|
||||
}
|
||||
|
||||
await pc.setRemoteDescription({ type: 'answer', sdp: await resp.text() });
|
||||
const sdpAnswer = await resp.text();
|
||||
log(camId, `Answer erhalten (${sdpAnswer.length} Bytes)`);
|
||||
await pc.setRemoteDescription({ type: 'answer', sdp: sdpAnswer });
|
||||
log(camId, 'Remote description gesetzt – warte auf ICE...');
|
||||
|
||||
} catch (err) {
|
||||
statusEl.textContent = `Fehler: ${err.message}`;
|
||||
console.error(`[${camId}]`, err);
|
||||
console.error(`[${camId}] Fehler:`, err);
|
||||
setStatus(statusEl, `${err.message}`, '#c44');
|
||||
pc?.close();
|
||||
setTimeout(() => startWebRTC(camId, videoEl, statusEl), 5000);
|
||||
}
|
||||
}
|
||||
|
||||
function setStatus(el, text, color) {
|
||||
el.textContent = text;
|
||||
el.style.color = color ?? '#999';
|
||||
}
|
||||
|
||||
function createCameraView(camId, container) {
|
||||
log(camId, 'View erstellt');
|
||||
|
||||
const box = document.createElement('div');
|
||||
box.className = 'cam-box';
|
||||
|
||||
@@ -72,7 +109,7 @@ function createCameraView(camId, container) {
|
||||
video.autoplay = true;
|
||||
video.playsInline = true;
|
||||
video.muted = true;
|
||||
video.style.cssText = 'display:block;width:640px;height:480px;background:#000';
|
||||
video.style.cssText = 'display:block;width:640px;height:480px;background:#111';
|
||||
box.appendChild(video);
|
||||
|
||||
const label = document.createElement('div');
|
||||
@@ -101,23 +138,29 @@ function createCameraView(camId, container) {
|
||||
startWebRTC(camId, video, status);
|
||||
}
|
||||
|
||||
// Kamera-Liste von go2rtc (via Node.js-Proxy), dann Views aufbauen
|
||||
// Kamera-Liste via /api/snapshot (proxied go2rtc /api/streams)
|
||||
log('init', 'Frage Kamera-Liste ab...');
|
||||
fetch('/api/snapshot')
|
||||
.then(r => r.json())
|
||||
.then(r => {
|
||||
log('init', `/api/snapshot → HTTP ${r.status}`);
|
||||
return r.json();
|
||||
})
|
||||
.then(data => {
|
||||
log('init', `Kameras: ${JSON.stringify(data.cameras)}`);
|
||||
const container = document.getElementById('cameras');
|
||||
const cams = data.cameras ?? [];
|
||||
if (cams.length === 0) {
|
||||
document.getElementById('statusText').textContent = 'Keine Kameras in go2rtc';
|
||||
document.getElementById('statusText').textContent = 'Keine Kameras (go2rtc läuft?)';
|
||||
console.warn('go2rtc meldet keine Streams. Prüfe http://server:1984');
|
||||
return;
|
||||
}
|
||||
cams.forEach(c => createCameraView(c.id, container));
|
||||
document.getElementById('statusText').textContent =
|
||||
`${cams.length} Kamera${cams.length !== 1 ? 's' : ''} · WebRTC`;
|
||||
})
|
||||
.catch(() => {
|
||||
// Fallback wenn go2rtc noch nicht läuft
|
||||
.catch(err => {
|
||||
console.error('[init] /api/snapshot Fehler:', err);
|
||||
document.getElementById('statusText').textContent = 'API-Fehler – Fallback';
|
||||
const container = document.getElementById('cameras');
|
||||
['cam0', 'cam1'].forEach(id => createCameraView(id, container));
|
||||
document.getElementById('statusText').textContent = 'go2rtc nicht erreichbar – versuche trotzdem';
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user