-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebrtc_test.html
More file actions
425 lines (381 loc) · 15.1 KB
/
webrtc_test.html
File metadata and controls
425 lines (381 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebRTC Live Stream Test</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: monospace; background: #1a1a2e; color: #e0e0e0; padding: 20px; }
h1 { color: #00d4ff; margin-bottom: 20px; }
.controls { display: flex; gap: 10px; align-items: center; margin-bottom: 20px; flex-wrap: wrap; }
label { color: #aaa; }
input, select { background: #16213e; color: #e0e0e0; border: 1px solid #0f3460; padding: 6px 10px; border-radius: 4px; font-family: monospace; }
button { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; font-family: monospace; font-weight: bold; }
.btn-start { background: #00b894; color: #fff; }
.btn-start:hover { background: #00a885; }
.btn-stop { background: #e74c3c; color: #fff; }
.btn-stop:hover { background: #c0392b; }
.btn-clear { background: #636e72; color: #fff; }
.btn-clear:hover { background: #535c60; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.main { display: flex; gap: 20px; }
.video-container { flex: 0 0 auto; }
video { background: #000; border: 2px solid #0f3460; border-radius: 4px; display: block; }
.log-container { flex: 1; min-width: 300px; }
#log { background: #0a0a1a; border: 1px solid #0f3460; border-radius: 4px; padding: 10px; height: 500px; overflow-y: auto; font-size: 12px; line-height: 1.5; white-space: pre-wrap; word-break: break-all; }
.log-info { color: #74b9ff; }
.log-success { color: #00b894; }
.log-warn { color: #fdcb6e; }
.log-error { color: #e74c3c; }
.log-data { color: #a29bfe; }
.log-ws { color: #55efc4; }
.status { padding: 4px 10px; border-radius: 12px; font-size: 12px; font-weight: bold; }
.status-disconnected { background: #636e72; }
.status-connecting { background: #0984e3; }
.status-connected { background: #00b894; }
.status-streaming { background: #6c5ce7; }
.status-error { background: #e74c3c; }
.stats { margin-top: 10px; font-size: 12px; color: #aaa; }
.stats span { color: #00d4ff; }
</style>
</head>
<body>
<h1>WebRTC Live Stream Test</h1>
<div class="controls">
<label>Host:</label>
<input type="text" id="host" value="localhost:8080" size="20">
<label>Monitor:</label>
<input type="number" id="monitorId" value="2" min="1" style="width:60px">
<button class="btn-start" id="btnStart" onclick="startStream()">Start Stream</button>
<button class="btn-stop" id="btnStop" onclick="stopStream()" disabled>Stop</button>
<button class="btn-clear" onclick="clearLog()">Clear Log</button>
<span class="status status-disconnected" id="status">Disconnected</span>
</div>
<div class="main">
<div class="video-container">
<video id="video" width="640" height="480" autoplay muted playsinline></video>
<div class="stats">
Resolution: <span id="statRes">-</span> |
Frames: <span id="statFrames">-</span> |
Bitrate: <span id="statBitrate">-</span> |
Codec: <span id="statCodec">-</span>
</div>
</div>
<div class="log-container">
<div id="log"></div>
</div>
</div>
<script>
let ws = null;
let pc = null;
let sessionId = null;
let statsInterval = null;
let prevBytesReceived = 0;
let prevTimestamp = 0;
function log(msg, cls = 'log-info') {
const el = document.getElementById('log');
const time = new Date().toLocaleTimeString('en-GB', { hour12: false, fractionalSecondDigits: 3 });
const line = document.createElement('div');
line.className = cls;
line.textContent = `[${time}] ${msg}`;
el.appendChild(line);
el.scrollTop = el.scrollHeight;
}
function clearLog() {
document.getElementById('log').innerHTML = '';
}
function setStatus(text, cls) {
const el = document.getElementById('status');
el.textContent = text;
el.className = `status status-${cls}`;
}
async function startStream() {
const host = document.getElementById('host').value;
const monitorId = document.getElementById('monitorId').value;
document.getElementById('btnStart').disabled = true;
document.getElementById('btnStop').disabled = false;
setStatus('Starting...', 'connecting');
// Step 1: Start the live stream with WebRTC enabled
log(`Starting live stream for monitor ${monitorId}...`);
try {
const resp = await fetch(`http://${host}/api/v3/live/${monitorId}/start`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enable_hls: false, enable_webrtc: true, enable_mse: false })
});
const data = await resp.json();
if (!resp.ok) {
// 409 = already running, that's fine
if (resp.status === 409) {
log(`Session already active for monitor ${monitorId}, connecting...`, 'log-warn');
} else {
throw new Error(`HTTP ${resp.status}: ${JSON.stringify(data)}`);
}
} else {
log(`Stream started: ${JSON.stringify(data)}`, 'log-success');
}
} catch (e) {
log(`Failed to start stream: ${e.message}`, 'log-error');
// Try connecting to WebSocket anyway (session might already exist)
log('Attempting WebSocket connection anyway...', 'log-warn');
}
// Step 2: Connect WebSocket for signaling
const wsUrl = `ws://${host}/api/v3/live/${monitorId}/webrtc/ws`;
log(`Connecting WebSocket: ${wsUrl}`);
setStatus('Connecting WS...', 'connecting');
try {
ws = new WebSocket(wsUrl);
} catch (e) {
log(`WebSocket creation failed: ${e.message}`, 'log-error');
setStatus('Error', 'error');
resetButtons();
return;
}
ws.onopen = () => {
log('WebSocket connected', 'log-success');
setStatus('WS Connected', 'connecting');
};
ws.onclose = (e) => {
log(`WebSocket closed: code=${e.code} reason=${e.reason}`, 'log-warn');
if (!document.getElementById('btnStart').disabled) return; // already stopped
setStatus('Disconnected', 'disconnected');
};
ws.onerror = (e) => {
log('WebSocket error', 'log-error');
setStatus('WS Error', 'error');
};
ws.onmessage = async (event) => {
let msg;
try {
msg = JSON.parse(event.data);
} catch (e) {
log(`Non-JSON message: ${event.data}`, 'log-warn');
return;
}
log(`WS recv: ${msg.type} ${msg.session_id ? '(session=' + msg.session_id.substring(0,8) + '...)' : ''}`, 'log-ws');
switch (msg.type) {
case 'offer':
sessionId = msg.session_id;
await handleOffer(msg);
break;
case 'ready':
log(`Stream READY for monitor ${msg.monitor_id}`, 'log-success');
setStatus('Streaming', 'streaming');
startStatsPolling();
break;
case 'icecandidate':
await handleRemoteIceCandidate(msg);
break;
case 'error':
log(`Server error: ${msg.message}`, 'log-error');
setStatus('Error', 'error');
break;
case 'pong':
break;
default:
log(`Unknown message type: ${msg.type}`, 'log-warn');
}
};
}
async function handleOffer(msg) {
log('Received SDP offer, creating peer connection...', 'log-info');
// Log SDP details
const sdpLines = msg.sdp.split('\r\n');
const codecLines = sdpLines.filter(l => l.startsWith('a=rtpmap:') || l.startsWith('a=fmtp:'));
codecLines.forEach(l => log(` SDP: ${l}`, 'log-data'));
// Create RTCPeerConnection
pc = new RTCPeerConnection({
iceServers: [], // No STUN/TURN needed for LAN
bundlePolicy: 'max-bundle',
rtcpMuxPolicy: 'require'
});
// Track ICE state changes
pc.oniceconnectionstatechange = () => {
log(`ICE state: ${pc.iceConnectionState}`, 'log-info');
if (pc.iceConnectionState === 'connected') {
setStatus('ICE Connected', 'connected');
} else if (pc.iceConnectionState === 'failed') {
log('ICE connection failed!', 'log-error');
setStatus('ICE Failed', 'error');
} else if (pc.iceConnectionState === 'disconnected') {
log('ICE disconnected', 'log-warn');
setStatus('Disconnected', 'disconnected');
}
};
pc.onconnectionstatechange = () => {
log(`Connection state: ${pc.connectionState}`, 'log-info');
};
pc.onsignalingstatechange = () => {
log(`Signaling state: ${pc.signalingState}`, 'log-data');
};
// Handle ICE candidates
pc.onicecandidate = (event) => {
if (event.candidate) {
log(`Local ICE candidate: ${event.candidate.candidate.substring(0, 60)}...`, 'log-data');
ws.send(JSON.stringify({
type: 'icecandidate',
session_id: sessionId,
candidate: event.candidate.candidate,
sdpMid: event.candidate.sdpMid,
sdpMLineIndex: event.candidate.sdpMLineIndex
}));
} else {
log('ICE gathering complete', 'log-success');
}
};
pc.onicegatheringstatechange = () => {
log(`ICE gathering state: ${pc.iceGatheringState}`, 'log-data');
};
// Handle remote tracks
pc.ontrack = (event) => {
log(`Remote track received: kind=${event.track.kind} id=${event.track.id}`, 'log-success');
const video = document.getElementById('video');
if (event.streams && event.streams[0]) {
video.srcObject = event.streams[0];
log('Attached stream to video element', 'log-success');
} else {
// Create a new MediaStream if not provided
let stream = video.srcObject;
if (!stream) {
stream = new MediaStream();
video.srcObject = stream;
}
stream.addTrack(event.track);
log('Added track to video element', 'log-success');
}
event.track.onunmute = () => log(`Track unmuted: ${event.track.kind}`, 'log-success');
event.track.onmute = () => log(`Track muted: ${event.track.kind}`, 'log-warn');
event.track.onended = () => log(`Track ended: ${event.track.kind}`, 'log-warn');
};
// Set remote description (offer)
try {
await pc.setRemoteDescription(new RTCSessionDescription({
type: 'offer',
sdp: msg.sdp
}));
log('Remote description set (offer)', 'log-success');
} catch (e) {
log(`Failed to set remote description: ${e.message}`, 'log-error');
return;
}
// Create and send answer
try {
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
log('Local description set (answer)', 'log-success');
ws.send(JSON.stringify({
type: 'answer',
session_id: sessionId,
sdp: answer.sdp
}));
log('Sent SDP answer to server', 'log-success');
// Log answer SDP codec info
const answerLines = answer.sdp.split('\r\n');
const answerCodecs = answerLines.filter(l => l.startsWith('a=rtpmap:') || l.startsWith('a=fmtp:'));
answerCodecs.forEach(l => log(` Answer SDP: ${l}`, 'log-data'));
} catch (e) {
log(`Failed to create answer: ${e.message}`, 'log-error');
}
}
async function handleRemoteIceCandidate(msg) {
if (!pc) return;
try {
await pc.addIceCandidate(new RTCIceCandidate({
candidate: msg.candidate,
sdpMid: msg.sdpMid,
sdpMLineIndex: msg.sdpMLineIndex
}));
log(`Added remote ICE candidate`, 'log-data');
} catch (e) {
log(`Failed to add ICE candidate: ${e.message}`, 'log-error');
}
}
function startStatsPolling() {
if (statsInterval) clearInterval(statsInterval);
prevBytesReceived = 0;
prevTimestamp = 0;
statsInterval = setInterval(async () => {
if (!pc) return;
const stats = await pc.getStats();
stats.forEach(report => {
if (report.type === 'inbound-rtp' && report.kind === 'video') {
// Resolution
if (report.frameWidth && report.frameHeight) {
document.getElementById('statRes').textContent = `${report.frameWidth}x${report.frameHeight}`;
}
// Frames
if (report.framesReceived !== undefined) {
document.getElementById('statFrames').textContent = report.framesReceived;
}
// Bitrate
if (report.bytesReceived !== undefined && report.timestamp) {
if (prevTimestamp > 0) {
const dt = (report.timestamp - prevTimestamp) / 1000;
const dBytes = report.bytesReceived - prevBytesReceived;
const kbps = Math.round((dBytes * 8) / dt / 1000);
document.getElementById('statBitrate').textContent = `${kbps} kbps`;
}
prevBytesReceived = report.bytesReceived;
prevTimestamp = report.timestamp;
}
// Codec
if (report.codecId) {
stats.forEach(r => {
if (r.id === report.codecId && r.mimeType) {
document.getElementById('statCodec').textContent = r.mimeType;
}
});
}
}
});
}, 1000);
}
async function stopStream() {
const host = document.getElementById('host').value;
const monitorId = document.getElementById('monitorId').value;
log('Stopping stream...', 'log-warn');
if (statsInterval) {
clearInterval(statsInterval);
statsInterval = null;
}
if (pc) {
pc.close();
pc = null;
log('Peer connection closed', 'log-info');
}
if (ws) {
ws.close();
ws = null;
log('WebSocket closed', 'log-info');
}
// Stop server-side session
try {
await fetch(`http://${host}/api/v3/live/${monitorId}/stop`, { method: 'DELETE' });
log('Server session stopped', 'log-success');
} catch (e) {
log(`Failed to stop server session: ${e.message}`, 'log-warn');
}
document.getElementById('video').srcObject = null;
sessionId = null;
setStatus('Disconnected', 'disconnected');
resetButtons();
// Reset stats
document.getElementById('statRes').textContent = '-';
document.getElementById('statFrames').textContent = '-';
document.getElementById('statBitrate').textContent = '-';
document.getElementById('statCodec').textContent = '-';
}
function resetButtons() {
document.getElementById('btnStart').disabled = false;
document.getElementById('btnStop').disabled = true;
}
// Keepalive ping
setInterval(() => {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: 'ping' }));
}
}, 15000);
</script>
</body>
</html>