-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·334 lines (282 loc) · 9.51 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>RTSP Metadata Viewer</title>
<style>
<style>
.container {
display: flex;
margin: 20px;
font-family: Arial, sans-serif;
}
.metadata-panel {
flex: 1;
padding: 20px;
background: #f5f5f5;
border-radius: 8px;
max-width: 500px;
}
.metadata-item {
background: white;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.metadata-timestamp {
color: #666;
font-size: 12px;
margin-bottom: 5px;
}
.metadata-content {
font-family: monospace;
white-space: pre-wrap;
}
.status {
position: fixed;
top: 20px;
right: 20px;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
font-weight: bold;
}
.status.connected {
background: #4CAF50;
color: white;
}
.status.disconnected {
background: #f44336;
color: white;
}
.controls {
margin-bottom: 20px;
}
.controls button {
padding: 8px 16px;
margin-right: 10px;
border: none;
border-radius: 4px;
background: #2196F3;
color: white;
cursor: pointer;
transition: background 0.3s;
}
.controls button:hover {
background: #1976D2;
}
.settings {
margin-bottom: 20px;
}
.settings input {
padding: 8px;
margin-right: 10px;
border: 1px solid #ddd;
border-radius: 4px;
width: 300px;
}
</style>
</style>
</head>
<body>
<div class="container">
<div class="metadata-panel">
<div class="settings">
<input type="text" id="rtspUrl" placeholder="RTSP URL" value="rtsp://localhost:8554/stream" />
<input type="text" id="wsUrl" placeholder="WebSocket URL" value="ws://localhost:5001/ws" />
<button onclick="connectToStream()">Connect</button>
</div>
<div class="controls">
<button onclick="startMetadataStream()">Start Stream</button>
<button onclick="stopMetadataStream()">Stop Stream</button>
<button onclick="clearMetadata()">Clear</button>
</div>
<div id="metadataContainer"></div>
</div>
</div>
<div id="status" class="status disconnected">Disconnected</div>
<script>
class RTSPMetadataClient {
constructor(config) {
this.config = {
maxMetadataItems: 50,
updateInterval: 1000,
...config
};
this.metadataContainer = document.getElementById('metadataContainer');
this.statusElement = document.getElementById('status');
this.ws = null;
this.connected = false;
this.streaming = false;
this.metadataBuffer = [];
this.reconnectAttempts = 0;
this.maxReconnectAttempts = 5;
}
connect(rtspUrl, wsUrl) {
if (this.ws) {
this.ws.close();
}
console.log('Connecting to WebSocket:', wsUrl);
console.log('RTSP URL:', rtspUrl);
try {
this.ws = new WebSocket(wsUrl);
this.ws.onopen = () => {
this.connected = true;
this.reconnectAttempts = 0;
this.updateStatus('connected');
console.log('WebSocket connected');
// Wysłanie konfiguracji RTSP
const setupMessage = {
type: 'SETUP',
rtspUrl: rtspUrl,
parameters: {
metadata_only: true,
keep_alive: true
}
};
console.log('Sending setup message:', setupMessage);
this.ws.send(JSON.stringify(setupMessage));
};
this.ws.onmessage = (event) => {
try {
console.log('Received message:', event.data);
const data = JSON.parse(event.data);
this.handleMetadata(data);
} catch (e) {
console.error('Error parsing metadata:', e, event.data);
}
};
this.ws.onerror = (error) => {
console.error('WebSocket error:', error);
this.updateStatus('error');
};
this.ws.onclose = (event) => {
console.log('WebSocket closed:', event);
this.connected = false;
this.updateStatus('disconnected');
// Próba ponownego połączenia
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++;
console.log(`Reconnection attempt ${this.reconnectAttempts} of ${this.maxReconnectAttempts}`);
setTimeout(() => {
if (!this.connected) {
this.connect(rtspUrl, wsUrl);
}
}, 5000);
}
};
} catch (e) {
console.error('Connection error:', e);
this.updateStatus('error');
}
}
handleMetadata(metadata) {
console.log('Processing metadata:', metadata);
// Dodanie timestampu
metadata.receivedAt = Date.now();
// Dodanie do bufora
this.metadataBuffer.unshift(metadata);
// Ograniczenie rozmiaru bufora
if (this.metadataBuffer.length > this.config.maxMetadataItems) {
this.metadataBuffer.pop();
}
// Aktualizacja UI
this.updateMetadataUI();
}
updateMetadataUI() {
// Wyczyszczenie kontenera
this.metadataContainer.innerHTML = '';
// Dodanie nowych elementów
this.metadataBuffer.forEach(metadata => {
const item = document.createElement('div');
item.className = 'metadata-item';
const timestamp = document.createElement('div');
timestamp.className = 'metadata-timestamp';
timestamp.textContent = new Date(metadata.receivedAt).toISOString();
const content = document.createElement('div');
content.className = 'metadata-content';
content.textContent = JSON.stringify(metadata, null, 2);
item.appendChild(timestamp);
item.appendChild(content);
this.metadataContainer.appendChild(item);
});
}
updateStatus(status) {
this.statusElement.className = `status ${status}`;
this.statusElement.textContent = status.charAt(0).toUpperCase() + status.slice(1);
console.log('Status updated:', status);
}
startStream() {
if (!this.connected) {
console.log('Cannot start stream: not connected');
return;
}
console.log('Starting stream');
this.streaming = true;
this.ws.send(JSON.stringify({
type: 'PLAY',
parameters: {
metadata_only: true
}
}));
}
stopStream() {
if (!this.connected) {
console.log('Cannot stop stream: not connected');
return;
}
console.log('Stopping stream');
this.streaming = false;
this.ws.send(JSON.stringify({
type: 'PAUSE'
}));
}
clearMetadata() {
console.log('Clearing metadata');
this.metadataBuffer = [];
this.updateMetadataUI();
}
disconnect() {
console.log('Disconnecting');
if (this.ws) {
this.ws.close();
this.ws = null;
}
this.connected = false;
this.streaming = false;
}
}
// Globalne instancje i funkcje
let rtspClient;
function connectToStream() {
const rtspUrl = document.getElementById('rtspUrl').value;
const wsUrl = document.getElementById('wsUrl').value;
if (rtspClient) {
rtspClient.disconnect();
}
rtspClient = new RTSPMetadataClient({
maxMetadataItems: 50,
updateInterval: 1000
});
rtspClient.connect(rtspUrl, wsUrl);
}
function startMetadataStream() {
if (rtspClient) {
rtspClient.startStream();
}
}
function stopMetadataStream() {
if (rtspClient) {
rtspClient.stopStream();
}
}
function clearMetadata() {
if (rtspClient) {
rtspClient.clearMetadata();
}
}
// Automatyczne połączenie przy starcie
document.addEventListener('DOMContentLoaded', connectToStream);
</script>
</body>
</html>