peerpigeon 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +190 -12
- package/examples/browser/app.js +40 -40
- package/examples/browser/crypto-test.html +427 -0
- package/examples/browser/index.html +161 -0
- package/examples/browser/styles.css +216 -0
- package/examples/browser/ui-old.js +1704 -1708
- package/examples/browser/ui.js +2226 -1692
- package/examples/nativescript/README.md +73 -0
- package/examples/nativescript/app.js +152 -0
- package/examples/nativescript/package.json +27 -0
- package/examples/node/test-crypto-media.js +137 -0
- package/examples/node/test-crypto.js +146 -0
- package/examples/node/test-node.js +114 -115
- package/package.json +16 -13
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +457 -384
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +216 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1100 -721
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +118 -104
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +1123 -948
- package/websocket-server/server.js +360 -362
|
@@ -1,1719 +1,1715 @@
|
|
|
1
1
|
// Updated: 2025-07-04 - Fixed getConnectionStatus method name
|
|
2
2
|
export class PeerPigeonUI {
|
|
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
|
-
|
|
3
|
+
constructor(mesh) {
|
|
4
|
+
this.mesh = mesh;
|
|
5
|
+
this.lastCleanupTime = 0; // Track when we last did cleanup
|
|
6
|
+
this.setupEventListeners();
|
|
7
|
+
this.bindDOMEvents();
|
|
8
|
+
this.initializeMedia();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
setupEventListeners() {
|
|
12
|
+
this.mesh.addEventListener('statusChanged', (data) => {
|
|
13
|
+
this.handleStatusChange(data);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
this.mesh.addEventListener('peerDiscovered', (_data) => {
|
|
17
|
+
this.updateDiscoveredPeers();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
this.mesh.addEventListener('peerConnected', (_data) => {
|
|
21
|
+
this.updateUI();
|
|
22
|
+
this.updateDiscoveredPeers();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
this.mesh.addEventListener('peerDisconnected', (data) => {
|
|
26
|
+
this.addMessage('System', `Peer ${data.peerId.substring(0, 8)}... disconnected (${data.reason})`);
|
|
27
|
+
this.updateUI();
|
|
28
|
+
this.updateDiscoveredPeers();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
this.mesh.addEventListener('messageReceived', (data) => {
|
|
32
|
+
// Only show messages from other peers, not self
|
|
33
|
+
if (data.from !== this.mesh.peerId) {
|
|
34
|
+
if (data.direct) {
|
|
35
|
+
this.addMessage(`${data.from.substring(0, 8)}...`, `(DM) ${data.content}`, 'dm');
|
|
36
|
+
} else {
|
|
37
|
+
this.addMessage(`${data.from.substring(0, 8)}...`, data.content);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
this.mesh.addEventListener('peerEvicted', (_data) => {
|
|
43
|
+
this.updateUI();
|
|
44
|
+
this.updateDiscoveredPeers();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
this.mesh.addEventListener('peersUpdated', () => {
|
|
48
|
+
this.updateDiscoveredPeers();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
this.mesh.addEventListener('connectionStats', (_stats) => {
|
|
52
|
+
// Handle connection stats if needed
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// DHT event listeners
|
|
56
|
+
this.mesh.addEventListener('dhtValueChanged', (data) => {
|
|
57
|
+
const { key, newValue, timestamp } = data;
|
|
58
|
+
this.addDHTLogEntry(`🔔 Value Changed: ${key} = ${JSON.stringify(newValue)} (timestamp: ${timestamp})`);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Media event listeners
|
|
62
|
+
this.mesh.addEventListener('localStreamStarted', (data) => {
|
|
63
|
+
this.handleLocalStreamStarted(data);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
this.mesh.addEventListener('localStreamStopped', () => {
|
|
67
|
+
this.handleLocalStreamStopped();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
this.mesh.addEventListener('mediaError', (data) => {
|
|
71
|
+
this.addMessage('System', `Media error: ${data.error.message}`, 'error');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Listen for remote streams
|
|
75
|
+
this.mesh.addEventListener('remoteStream', (data) => {
|
|
76
|
+
this.handleRemoteStream(data);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
handleStatusChange(data) {
|
|
81
|
+
switch (data.type) {
|
|
82
|
+
case 'initialized':
|
|
83
|
+
this.updateUI();
|
|
84
|
+
break;
|
|
85
|
+
case 'connecting':
|
|
86
|
+
this.addMessage('System', 'Connecting to signaling server...');
|
|
87
|
+
this.updateUI();
|
|
88
|
+
break;
|
|
89
|
+
case 'connected':
|
|
90
|
+
this.addMessage('System', 'Connected to signaling server');
|
|
91
|
+
this.updateUI();
|
|
92
|
+
break;
|
|
93
|
+
case 'disconnected':
|
|
94
|
+
this.addMessage('System', 'Disconnected from signaling server');
|
|
95
|
+
this.updateUI();
|
|
96
|
+
this.updateDiscoveredPeers();
|
|
97
|
+
break;
|
|
98
|
+
case 'error':
|
|
99
|
+
this.addMessage('System', data.message, 'error');
|
|
100
|
+
this.updateUI();
|
|
101
|
+
break;
|
|
102
|
+
case 'warning':
|
|
103
|
+
this.addMessage('System', data.message, 'warning');
|
|
104
|
+
break;
|
|
105
|
+
case 'info':
|
|
106
|
+
this.addMessage('System', data.message);
|
|
107
|
+
break;
|
|
108
|
+
case 'urlLoaded':
|
|
109
|
+
document.getElementById('signaling-url').value = data.signalingUrl;
|
|
110
|
+
this.addMessage('System', `API Gateway URL loaded: ${data.signalingUrl}`);
|
|
111
|
+
break;
|
|
112
|
+
case 'setting': {
|
|
113
|
+
const settingName = data.setting === 'autoDiscovery'
|
|
114
|
+
? 'Auto discovery'
|
|
115
|
+
: data.setting === 'evictionStrategy'
|
|
116
|
+
? 'Smart eviction strategy'
|
|
117
|
+
: data.setting === 'xorRouting'
|
|
118
|
+
? 'XOR-based routing'
|
|
119
|
+
: data.setting === 'connectionType' ? 'Connection type' : data.setting;
|
|
120
|
+
|
|
121
|
+
if (data.setting === 'connectionType') {
|
|
122
|
+
this.addMessage('System', `${settingName} set to: ${data.value}`);
|
|
123
|
+
} else {
|
|
124
|
+
this.addMessage('System', `${settingName} ${data.value ? 'enabled' : 'disabled'}`);
|
|
125
|
+
}
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
bindDOMEvents() {
|
|
132
|
+
// Connection controls
|
|
133
|
+
document.getElementById('connect-btn').addEventListener('click', () => {
|
|
134
|
+
this.handleConnect();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
document.getElementById('disconnect-btn').addEventListener('click', () => {
|
|
138
|
+
this.handleDisconnect();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
document.getElementById('cleanup-btn').addEventListener('click', () => {
|
|
142
|
+
this.handleCleanup();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
document.getElementById('health-check-btn').addEventListener('click', () => {
|
|
146
|
+
this.handleHealthCheck();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
document.getElementById('refresh-btn').addEventListener('click', () => {
|
|
150
|
+
window.location.reload();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Messaging
|
|
154
|
+
document.getElementById('send-message-btn').addEventListener('click', () => {
|
|
155
|
+
this.sendMessage();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
document.getElementById('message-input').addEventListener('keypress', (e) => {
|
|
159
|
+
if (e.key === 'Enter') {
|
|
160
|
+
this.sendMessage();
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Manual connection
|
|
165
|
+
document.getElementById('connect-peer-btn').addEventListener('click', () => {
|
|
166
|
+
this.connectToPeer();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
document.getElementById('target-peer').addEventListener('keypress', (e) => {
|
|
170
|
+
if (e.key === 'Enter') {
|
|
171
|
+
this.connectToPeer();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Settings
|
|
176
|
+
document.getElementById('min-peers').addEventListener('change', (e) => {
|
|
177
|
+
this.mesh.setMinPeers(parseInt(e.target.value));
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
document.getElementById('max-peers').addEventListener('change', (e) => {
|
|
181
|
+
this.mesh.setMaxPeers(parseInt(e.target.value));
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
document.getElementById('auto-discovery-toggle').addEventListener('change', (e) => {
|
|
185
|
+
this.mesh.setAutoDiscovery(e.target.checked);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
document.getElementById('eviction-strategy-toggle').addEventListener('change', (e) => {
|
|
189
|
+
this.mesh.setEvictionStrategy(e.target.checked);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
document.getElementById('xor-routing-toggle').addEventListener('change', (e) => {
|
|
193
|
+
this.mesh.setXorRouting(e.target.checked);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// DHT controls
|
|
197
|
+
this.setupDHTControls();
|
|
198
|
+
|
|
199
|
+
// Collapsible sections
|
|
200
|
+
this.setupCollapsibleSections();
|
|
201
|
+
|
|
202
|
+
// Media controls
|
|
203
|
+
this.setupMediaControls();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
setupDHTControls() {
|
|
207
|
+
// DHT input fields and controls
|
|
208
|
+
const dhtKey = document.getElementById('dht-key');
|
|
209
|
+
const dhtValue = document.getElementById('dht-value');
|
|
210
|
+
const dhtGetKey = document.getElementById('dht-get-key'); // Separate key field for retrieval
|
|
211
|
+
const dhtPutBtn = document.getElementById('dht-put-btn');
|
|
212
|
+
const dhtUpdateBtn = document.getElementById('dht-update-btn');
|
|
213
|
+
const dhtGetBtn = document.getElementById('dht-get-btn');
|
|
214
|
+
const dhtSubscribeBtn = document.getElementById('dht-subscribe-btn');
|
|
215
|
+
const dhtUnsubscribeBtn = document.getElementById('dht-unsubscribe-btn');
|
|
216
|
+
const dhtEnableTtl = document.getElementById('dht-enable-ttl');
|
|
217
|
+
const dhtTtlGroup = document.getElementById('dht-ttl-group');
|
|
218
|
+
const dhtTtl = document.getElementById('dht-ttl');
|
|
219
|
+
|
|
220
|
+
// Debug: Check if elements are found
|
|
221
|
+
console.log('DHT Controls setup - Elements found:', {
|
|
222
|
+
dhtKey: !!dhtKey,
|
|
223
|
+
dhtValue: !!dhtValue,
|
|
224
|
+
dhtGetKey: !!dhtGetKey,
|
|
225
|
+
dhtPutBtn: !!dhtPutBtn,
|
|
226
|
+
dhtUpdateBtn: !!dhtUpdateBtn,
|
|
227
|
+
dhtGetBtn: !!dhtGetBtn,
|
|
228
|
+
dhtSubscribeBtn: !!dhtSubscribeBtn,
|
|
229
|
+
dhtUnsubscribeBtn: !!dhtUnsubscribeBtn
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
if (!dhtKey) {
|
|
233
|
+
console.error('DHT key input element not found!');
|
|
234
|
+
this.addDHTLogEntry('❌ Error: DHT key input field not found in DOM');
|
|
235
|
+
}
|
|
46
236
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
237
|
+
if (!dhtValue) {
|
|
238
|
+
console.error('DHT value input element not found!');
|
|
239
|
+
this.addDHTLogEntry('❌ Error: DHT value input field not found in DOM');
|
|
240
|
+
}
|
|
50
241
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
242
|
+
if (!dhtGetKey) {
|
|
243
|
+
console.error('DHT get-key input element not found!');
|
|
244
|
+
this.addDHTLogEntry('❌ Error: DHT get-key input field not found in DOM');
|
|
245
|
+
}
|
|
54
246
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
247
|
+
// Toggle TTL input visibility
|
|
248
|
+
if (dhtEnableTtl && dhtTtlGroup) {
|
|
249
|
+
dhtEnableTtl.addEventListener('change', (e) => {
|
|
250
|
+
dhtTtlGroup.style.display = e.target.checked ? 'block' : 'none';
|
|
251
|
+
});
|
|
252
|
+
}
|
|
60
253
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
254
|
+
// Store in DHT
|
|
255
|
+
if (dhtPutBtn) {
|
|
256
|
+
dhtPutBtn.addEventListener('click', async () => {
|
|
257
|
+
const key = dhtKey?.value?.trim();
|
|
258
|
+
const value = dhtValue?.value?.trim();
|
|
259
|
+
|
|
260
|
+
// Debug logging
|
|
261
|
+
console.log('DHT PUT - Elements found:', {
|
|
262
|
+
dhtKey: !!dhtKey,
|
|
263
|
+
dhtValue: !!dhtValue,
|
|
264
|
+
keyValue: key,
|
|
265
|
+
valueValue: value
|
|
64
266
|
});
|
|
65
267
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
268
|
+
if (!key || !value) {
|
|
269
|
+
this.addDHTLogEntry('❌ Error: Both key and value are required');
|
|
270
|
+
this.addDHTLogEntry(` Debug: key="${key}", value="${value}"`);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
69
273
|
|
|
70
|
-
this.mesh.
|
|
71
|
-
|
|
72
|
-
|
|
274
|
+
if (!this.mesh.isDHTEnabled()) {
|
|
275
|
+
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
73
278
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
279
|
+
try {
|
|
280
|
+
let parsedValue;
|
|
281
|
+
try {
|
|
282
|
+
parsedValue = JSON.parse(value);
|
|
283
|
+
} catch {
|
|
284
|
+
parsedValue = value; // Use as string if not valid JSON
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const options = {};
|
|
288
|
+
if (dhtEnableTtl?.checked && dhtTtl?.value) {
|
|
289
|
+
options.ttl = parseInt(dhtTtl.value) * 1000; // Convert to milliseconds
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const success = await this.mesh.dhtPut(key, parsedValue, options);
|
|
293
|
+
if (success) {
|
|
294
|
+
this.addDHTLogEntry(`✅ Stored: ${key} = ${JSON.stringify(parsedValue)}`);
|
|
295
|
+
if (options.ttl) {
|
|
296
|
+
this.addDHTLogEntry(` TTL: ${options.ttl / 1000} seconds`);
|
|
297
|
+
}
|
|
298
|
+
this.addDHTLogEntry(` 🏆 This peer is now an ORIGINAL STORING PEER for "${key}"`);
|
|
79
299
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
case 'disconnected':
|
|
94
|
-
this.addMessage('System', 'Disconnected from signaling server');
|
|
95
|
-
this.updateUI();
|
|
96
|
-
this.updateDiscoveredPeers();
|
|
97
|
-
break;
|
|
98
|
-
case 'error':
|
|
99
|
-
this.addMessage('System', data.message, 'error');
|
|
100
|
-
this.updateUI();
|
|
101
|
-
break;
|
|
102
|
-
case 'warning':
|
|
103
|
-
this.addMessage('System', data.message, 'warning');
|
|
104
|
-
break;
|
|
105
|
-
case 'info':
|
|
106
|
-
this.addMessage('System', data.message);
|
|
107
|
-
break;
|
|
108
|
-
case 'urlLoaded':
|
|
109
|
-
document.getElementById('signaling-url').value = data.signalingUrl;
|
|
110
|
-
this.addMessage('System', `API Gateway URL loaded: ${data.signalingUrl}`);
|
|
111
|
-
break;
|
|
112
|
-
case 'setting':
|
|
113
|
-
const settingName = data.setting === 'autoDiscovery' ? 'Auto discovery' :
|
|
114
|
-
data.setting === 'evictionStrategy' ? 'Smart eviction strategy' :
|
|
115
|
-
data.setting === 'xorRouting' ? 'XOR-based routing' :
|
|
116
|
-
data.setting === 'connectionType' ? 'Connection type' : data.setting;
|
|
117
|
-
|
|
118
|
-
if (data.setting === 'connectionType') {
|
|
119
|
-
this.addMessage('System', `${settingName} set to: ${data.value}`);
|
|
120
|
-
} else {
|
|
121
|
-
this.addMessage('System', `${settingName} ${data.value ? 'enabled' : 'disabled'}`);
|
|
122
|
-
}
|
|
123
|
-
break;
|
|
300
|
+
// Auto-subscribe to the key we just stored (essential for original storing peers)
|
|
301
|
+
try {
|
|
302
|
+
await this.mesh.dhtSubscribe(key);
|
|
303
|
+
this.addDHTLogEntry(`🔔 Auto-subscribed as original storing peer: ${key}`);
|
|
304
|
+
this.addDHTLogEntry(' 📡 Will receive and relay all future updates for this key');
|
|
305
|
+
} catch (subscribeError) {
|
|
306
|
+
this.addDHTLogEntry(`⚠️ Failed to auto-subscribe to ${key}: ${subscribeError.message}`);
|
|
307
|
+
}
|
|
308
|
+
} else {
|
|
309
|
+
this.addDHTLogEntry(`❌ Failed to store: ${key}`);
|
|
310
|
+
}
|
|
311
|
+
} catch (error) {
|
|
312
|
+
this.addDHTLogEntry(`❌ Error storing ${key}: ${error.message}`);
|
|
124
313
|
}
|
|
314
|
+
});
|
|
125
315
|
}
|
|
126
316
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
317
|
+
// Update in DHT
|
|
318
|
+
if (dhtUpdateBtn) {
|
|
319
|
+
dhtUpdateBtn.addEventListener('click', async () => {
|
|
320
|
+
const key = dhtKey?.value?.trim();
|
|
321
|
+
const value = dhtValue?.value?.trim();
|
|
322
|
+
|
|
323
|
+
// Debug logging
|
|
324
|
+
console.log('DHT UPDATE - Elements found:', {
|
|
325
|
+
dhtKey: !!dhtKey,
|
|
326
|
+
dhtValue: !!dhtValue,
|
|
327
|
+
keyValue: key,
|
|
328
|
+
valueValue: value
|
|
135
329
|
});
|
|
136
330
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this.handleHealthCheck();
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
document.getElementById('refresh-btn').addEventListener('click', () => {
|
|
146
|
-
window.location.reload();
|
|
147
|
-
});
|
|
331
|
+
if (!key || !value) {
|
|
332
|
+
this.addDHTLogEntry('❌ Error: Both key and value are required');
|
|
333
|
+
this.addDHTLogEntry(` Debug: key="${key}", value="${value}"`);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
148
336
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
337
|
+
if (!this.mesh.isDHTEnabled()) {
|
|
338
|
+
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
153
341
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
342
|
+
try {
|
|
343
|
+
let parsedValue;
|
|
344
|
+
try {
|
|
345
|
+
parsedValue = JSON.parse(value);
|
|
346
|
+
} catch {
|
|
347
|
+
parsedValue = value; // Use as string if not valid JSON
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const options = {};
|
|
351
|
+
if (dhtEnableTtl?.checked && dhtTtl?.value) {
|
|
352
|
+
options.ttl = parseInt(dhtTtl.value) * 1000; // Convert to milliseconds
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const success = await this.mesh.dhtUpdate(key, parsedValue, options);
|
|
356
|
+
if (success) {
|
|
357
|
+
this.addDHTLogEntry(`🔄 Updated: ${key} = ${JSON.stringify(parsedValue)}`);
|
|
358
|
+
if (options.ttl) {
|
|
359
|
+
this.addDHTLogEntry(` TTL: ${options.ttl / 1000} seconds`);
|
|
157
360
|
}
|
|
158
|
-
|
|
361
|
+
this.addDHTLogEntry(` 🏆 This peer is now an ORIGINAL STORING PEER for "${key}"`);
|
|
159
362
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
this.connectToPeer();
|
|
363
|
+
// Auto-subscribe to the key we just updated (essential for original storing peers)
|
|
364
|
+
try {
|
|
365
|
+
await this.mesh.dhtSubscribe(key);
|
|
366
|
+
this.addDHTLogEntry(`🔔 Auto-subscribed as original storing peer: ${key}`);
|
|
367
|
+
this.addDHTLogEntry(' 📡 Will receive and relay all future updates for this key');
|
|
368
|
+
} catch (subscribeError) {
|
|
369
|
+
this.addDHTLogEntry(`⚠️ Failed to auto-subscribe to ${key}: ${subscribeError.message}`);
|
|
168
370
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
this.mesh.setMaxPeers(parseInt(e.target.value));
|
|
178
|
-
});
|
|
371
|
+
} else {
|
|
372
|
+
this.addDHTLogEntry(`❌ Failed to update: ${key}`);
|
|
373
|
+
}
|
|
374
|
+
} catch (error) {
|
|
375
|
+
this.addDHTLogEntry(`❌ Error updating ${key}: ${error.message}`);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
179
379
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
380
|
+
// Get from DHT
|
|
381
|
+
if (dhtGetBtn) {
|
|
382
|
+
dhtGetBtn.addEventListener('click', async () => {
|
|
383
|
+
const key = dhtGetKey?.value?.trim(); // Fixed: Use dhtGetKey instead of dhtKey
|
|
183
384
|
|
|
184
|
-
|
|
185
|
-
|
|
385
|
+
// Debug logging
|
|
386
|
+
console.log('DHT GET - Elements found:', {
|
|
387
|
+
dhtGetKey: !!dhtGetKey,
|
|
388
|
+
keyValue: key
|
|
186
389
|
});
|
|
187
390
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
391
|
+
if (!key) {
|
|
392
|
+
this.addDHTLogEntry('❌ Error: Key is required');
|
|
393
|
+
this.addDHTLogEntry(` Debug: key="${key}"`);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
191
396
|
|
|
192
|
-
|
|
193
|
-
|
|
397
|
+
if (!this.mesh.isDHTEnabled()) {
|
|
398
|
+
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
194
401
|
|
|
195
|
-
|
|
196
|
-
|
|
402
|
+
try {
|
|
403
|
+
this.addDHTLogEntry(`🔍 Fetching from ORIGINAL STORING PEERS: ${key}`);
|
|
404
|
+
this.addDHTLogEntry(' 📡 Bypassing local cache to ensure fresh data');
|
|
405
|
+
|
|
406
|
+
// Force fresh retrieval from original storing peers
|
|
407
|
+
const value = await this.mesh.dhtGet(key, { forceRefresh: true });
|
|
408
|
+
if (value !== null) {
|
|
409
|
+
this.addDHTLogEntry(`📥 Retrieved from original peers: ${key} = ${JSON.stringify(value)}`);
|
|
410
|
+
this.addDHTLogEntry(' ✅ Data is FRESH from authoritative source');
|
|
411
|
+
if (dhtValue) {
|
|
412
|
+
dhtValue.value = typeof value === 'string' ? value : JSON.stringify(value);
|
|
413
|
+
}
|
|
197
414
|
|
|
198
|
-
|
|
199
|
-
|
|
415
|
+
// Auto-subscribe to the key we just retrieved for future updates
|
|
416
|
+
try {
|
|
417
|
+
await this.mesh.dhtSubscribe(key);
|
|
418
|
+
this.addDHTLogEntry(`🔔 Auto-subscribed for future updates: ${key}`);
|
|
419
|
+
this.addDHTLogEntry(' 📡 Will receive notifications when original peers update this key');
|
|
420
|
+
} catch (subscribeError) {
|
|
421
|
+
this.addDHTLogEntry(`⚠️ Failed to auto-subscribe to ${key}: ${subscribeError.message}`);
|
|
422
|
+
}
|
|
423
|
+
} else {
|
|
424
|
+
this.addDHTLogEntry(`❌ Not found on original storing peers: ${key}`);
|
|
425
|
+
this.addDHTLogEntry(' 💡 Key may not exist or all original storing peers are offline');
|
|
426
|
+
}
|
|
427
|
+
} catch (error) {
|
|
428
|
+
this.addDHTLogEntry(`❌ Error retrieving ${key}: ${error.message}`);
|
|
429
|
+
}
|
|
430
|
+
});
|
|
200
431
|
}
|
|
201
432
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const dhtTtlGroup = document.getElementById('dht-ttl-group');
|
|
214
|
-
const dhtTtl = document.getElementById('dht-ttl');
|
|
215
|
-
|
|
216
|
-
// Debug: Check if elements are found
|
|
217
|
-
console.log('DHT Controls setup - Elements found:', {
|
|
218
|
-
dhtKey: !!dhtKey,
|
|
219
|
-
dhtValue: !!dhtValue,
|
|
220
|
-
dhtGetKey: !!dhtGetKey,
|
|
221
|
-
dhtPutBtn: !!dhtPutBtn,
|
|
222
|
-
dhtUpdateBtn: !!dhtUpdateBtn,
|
|
223
|
-
dhtGetBtn: !!dhtGetBtn,
|
|
224
|
-
dhtSubscribeBtn: !!dhtSubscribeBtn,
|
|
225
|
-
dhtUnsubscribeBtn: !!dhtUnsubscribeBtn
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
if (!dhtKey) {
|
|
229
|
-
console.error('DHT key input element not found!');
|
|
230
|
-
this.addDHTLogEntry('❌ Error: DHT key input field not found in DOM');
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (!dhtValue) {
|
|
234
|
-
console.error('DHT value input element not found!');
|
|
235
|
-
this.addDHTLogEntry('❌ Error: DHT value input field not found in DOM');
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (!dhtGetKey) {
|
|
239
|
-
console.error('DHT get-key input element not found!');
|
|
240
|
-
this.addDHTLogEntry('❌ Error: DHT get-key input field not found in DOM');
|
|
433
|
+
// Subscribe to DHT key
|
|
434
|
+
if (dhtSubscribeBtn) {
|
|
435
|
+
dhtSubscribeBtn.addEventListener('click', async () => {
|
|
436
|
+
// Check both key fields - use whichever has a value
|
|
437
|
+
const putKey = dhtKey?.value?.trim();
|
|
438
|
+
const getKey = dhtGetKey?.value?.trim();
|
|
439
|
+
const key = putKey || getKey;
|
|
440
|
+
|
|
441
|
+
if (!key) {
|
|
442
|
+
this.addDHTLogEntry('❌ Error: Key is required (enter key in either Put/Update or Get field)');
|
|
443
|
+
return;
|
|
241
444
|
}
|
|
242
445
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
dhtTtlGroup.style.display = e.target.checked ? 'block' : 'none';
|
|
247
|
-
});
|
|
446
|
+
if (!this.mesh.isDHTEnabled()) {
|
|
447
|
+
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
448
|
+
return;
|
|
248
449
|
}
|
|
249
450
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
this.addDHTLogEntry(` Debug: key="${key}", value="${value}"`);
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
if (!this.mesh.isDHTEnabled()) {
|
|
271
|
-
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
try {
|
|
276
|
-
let parsedValue;
|
|
277
|
-
try {
|
|
278
|
-
parsedValue = JSON.parse(value);
|
|
279
|
-
} catch {
|
|
280
|
-
parsedValue = value; // Use as string if not valid JSON
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const options = {};
|
|
284
|
-
if (dhtEnableTtl?.checked && dhtTtl?.value) {
|
|
285
|
-
options.ttl = parseInt(dhtTtl.value) * 1000; // Convert to milliseconds
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const success = await this.mesh.dhtPut(key, parsedValue, options);
|
|
289
|
-
if (success) {
|
|
290
|
-
this.addDHTLogEntry(`✅ Stored: ${key} = ${JSON.stringify(parsedValue)}`);
|
|
291
|
-
if (options.ttl) {
|
|
292
|
-
this.addDHTLogEntry(` TTL: ${options.ttl / 1000} seconds`);
|
|
293
|
-
}
|
|
294
|
-
this.addDHTLogEntry(` 🏆 This peer is now an ORIGINAL STORING PEER for "${key}"`);
|
|
295
|
-
|
|
296
|
-
// Auto-subscribe to the key we just stored (essential for original storing peers)
|
|
297
|
-
try {
|
|
298
|
-
await this.mesh.dhtSubscribe(key);
|
|
299
|
-
this.addDHTLogEntry(`🔔 Auto-subscribed as original storing peer: ${key}`);
|
|
300
|
-
this.addDHTLogEntry(` 📡 Will receive and relay all future updates for this key`);
|
|
301
|
-
} catch (subscribeError) {
|
|
302
|
-
this.addDHTLogEntry(`⚠️ Failed to auto-subscribe to ${key}: ${subscribeError.message}`);
|
|
303
|
-
}
|
|
304
|
-
} else {
|
|
305
|
-
this.addDHTLogEntry(`❌ Failed to store: ${key}`);
|
|
306
|
-
}
|
|
307
|
-
} catch (error) {
|
|
308
|
-
this.addDHTLogEntry(`❌ Error storing ${key}: ${error.message}`);
|
|
309
|
-
}
|
|
310
|
-
});
|
|
451
|
+
try {
|
|
452
|
+
this.addDHTLogEntry(`🔔 Explicitly subscribing to key: ${key}`);
|
|
453
|
+
this.addDHTLogEntry(' 📡 Will receive notifications when original storing peers update this key');
|
|
454
|
+
|
|
455
|
+
const value = await this.mesh.dhtSubscribe(key);
|
|
456
|
+
this.addDHTLogEntry(`✅ Subscribed to: ${key}`);
|
|
457
|
+
if (value !== null) {
|
|
458
|
+
this.addDHTLogEntry(` Current value from original storing peers: ${JSON.stringify(value)}`);
|
|
459
|
+
if (dhtValue) {
|
|
460
|
+
dhtValue.value = typeof value === 'string' ? value : JSON.stringify(value);
|
|
461
|
+
}
|
|
462
|
+
} else {
|
|
463
|
+
this.addDHTLogEntry(' No current value found on original storing peers');
|
|
464
|
+
}
|
|
465
|
+
} catch (error) {
|
|
466
|
+
this.addDHTLogEntry(`❌ Error subscribing to ${key}: ${error.message}`);
|
|
311
467
|
}
|
|
468
|
+
});
|
|
469
|
+
}
|
|
312
470
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
valueValue: value
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
if (!key || !value) {
|
|
328
|
-
this.addDHTLogEntry('❌ Error: Both key and value are required');
|
|
329
|
-
this.addDHTLogEntry(` Debug: key="${key}", value="${value}"`);
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
if (!this.mesh.isDHTEnabled()) {
|
|
334
|
-
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
try {
|
|
339
|
-
let parsedValue;
|
|
340
|
-
try {
|
|
341
|
-
parsedValue = JSON.parse(value);
|
|
342
|
-
} catch {
|
|
343
|
-
parsedValue = value; // Use as string if not valid JSON
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const options = {};
|
|
347
|
-
if (dhtEnableTtl?.checked && dhtTtl?.value) {
|
|
348
|
-
options.ttl = parseInt(dhtTtl.value) * 1000; // Convert to milliseconds
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
const success = await this.mesh.dhtUpdate(key, parsedValue, options);
|
|
352
|
-
if (success) {
|
|
353
|
-
this.addDHTLogEntry(`🔄 Updated: ${key} = ${JSON.stringify(parsedValue)}`);
|
|
354
|
-
if (options.ttl) {
|
|
355
|
-
this.addDHTLogEntry(` TTL: ${options.ttl / 1000} seconds`);
|
|
356
|
-
}
|
|
357
|
-
this.addDHTLogEntry(` 🏆 This peer is now an ORIGINAL STORING PEER for "${key}"`);
|
|
358
|
-
|
|
359
|
-
// Auto-subscribe to the key we just updated (essential for original storing peers)
|
|
360
|
-
try {
|
|
361
|
-
await this.mesh.dhtSubscribe(key);
|
|
362
|
-
this.addDHTLogEntry(`🔔 Auto-subscribed as original storing peer: ${key}`);
|
|
363
|
-
this.addDHTLogEntry(` 📡 Will receive and relay all future updates for this key`);
|
|
364
|
-
} catch (subscribeError) {
|
|
365
|
-
this.addDHTLogEntry(`⚠️ Failed to auto-subscribe to ${key}: ${subscribeError.message}`);
|
|
366
|
-
}
|
|
367
|
-
} else {
|
|
368
|
-
this.addDHTLogEntry(`❌ Failed to update: ${key}`);
|
|
369
|
-
}
|
|
370
|
-
} catch (error) {
|
|
371
|
-
this.addDHTLogEntry(`❌ Error updating ${key}: ${error.message}`);
|
|
372
|
-
}
|
|
373
|
-
});
|
|
471
|
+
// Unsubscribe from DHT key
|
|
472
|
+
if (dhtUnsubscribeBtn) {
|
|
473
|
+
dhtUnsubscribeBtn.addEventListener('click', async () => {
|
|
474
|
+
// Check both key fields - use whichever has a value
|
|
475
|
+
const putKey = dhtKey?.value?.trim();
|
|
476
|
+
const getKey = dhtGetKey?.value?.trim();
|
|
477
|
+
const key = putKey || getKey;
|
|
478
|
+
|
|
479
|
+
if (!key) {
|
|
480
|
+
this.addDHTLogEntry('❌ Error: Key is required (enter key in either Put/Update or Get field)');
|
|
481
|
+
return;
|
|
374
482
|
}
|
|
375
483
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
const key = dhtGetKey?.value?.trim(); // Fixed: Use dhtGetKey instead of dhtKey
|
|
380
|
-
|
|
381
|
-
// Debug logging
|
|
382
|
-
console.log('DHT GET - Elements found:', {
|
|
383
|
-
dhtGetKey: !!dhtGetKey,
|
|
384
|
-
keyValue: key
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
if (!key) {
|
|
388
|
-
this.addDHTLogEntry('❌ Error: Key is required');
|
|
389
|
-
this.addDHTLogEntry(` Debug: key="${key}"`);
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (!this.mesh.isDHTEnabled()) {
|
|
394
|
-
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
try {
|
|
399
|
-
this.addDHTLogEntry(`🔍 Fetching from ORIGINAL STORING PEERS: ${key}`);
|
|
400
|
-
this.addDHTLogEntry(` 📡 Bypassing local cache to ensure fresh data`);
|
|
401
|
-
|
|
402
|
-
// Force fresh retrieval from original storing peers
|
|
403
|
-
const value = await this.mesh.dhtGet(key, { forceRefresh: true });
|
|
404
|
-
if (value !== null) {
|
|
405
|
-
this.addDHTLogEntry(`📥 Retrieved from original peers: ${key} = ${JSON.stringify(value)}`);
|
|
406
|
-
this.addDHTLogEntry(` ✅ Data is FRESH from authoritative source`);
|
|
407
|
-
if (dhtValue) {
|
|
408
|
-
dhtValue.value = typeof value === 'string' ? value : JSON.stringify(value);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// Auto-subscribe to the key we just retrieved for future updates
|
|
412
|
-
try {
|
|
413
|
-
await this.mesh.dhtSubscribe(key);
|
|
414
|
-
this.addDHTLogEntry(`🔔 Auto-subscribed for future updates: ${key}`);
|
|
415
|
-
this.addDHTLogEntry(` 📡 Will receive notifications when original peers update this key`);
|
|
416
|
-
} catch (subscribeError) {
|
|
417
|
-
this.addDHTLogEntry(`⚠️ Failed to auto-subscribe to ${key}: ${subscribeError.message}`);
|
|
418
|
-
}
|
|
419
|
-
} else {
|
|
420
|
-
this.addDHTLogEntry(`❌ Not found on original storing peers: ${key}`);
|
|
421
|
-
this.addDHTLogEntry(` 💡 Key may not exist or all original storing peers are offline`);
|
|
422
|
-
}
|
|
423
|
-
} catch (error) {
|
|
424
|
-
this.addDHTLogEntry(`❌ Error retrieving ${key}: ${error.message}`);
|
|
425
|
-
}
|
|
426
|
-
});
|
|
484
|
+
if (!this.mesh.isDHTEnabled()) {
|
|
485
|
+
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
486
|
+
return;
|
|
427
487
|
}
|
|
428
488
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
const getKey = dhtGetKey?.value?.trim();
|
|
435
|
-
const key = putKey || getKey;
|
|
436
|
-
|
|
437
|
-
if (!key) {
|
|
438
|
-
this.addDHTLogEntry('❌ Error: Key is required (enter key in either Put/Update or Get field)');
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
if (!this.mesh.isDHTEnabled()) {
|
|
443
|
-
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
try {
|
|
448
|
-
this.addDHTLogEntry(`🔔 Explicitly subscribing to key: ${key}`);
|
|
449
|
-
this.addDHTLogEntry(` 📡 Will receive notifications when original storing peers update this key`);
|
|
450
|
-
|
|
451
|
-
const value = await this.mesh.dhtSubscribe(key);
|
|
452
|
-
this.addDHTLogEntry(`✅ Subscribed to: ${key}`);
|
|
453
|
-
if (value !== null) {
|
|
454
|
-
this.addDHTLogEntry(` Current value from original storing peers: ${JSON.stringify(value)}`);
|
|
455
|
-
if (dhtValue) {
|
|
456
|
-
dhtValue.value = typeof value === 'string' ? value : JSON.stringify(value);
|
|
457
|
-
}
|
|
458
|
-
} else {
|
|
459
|
-
this.addDHTLogEntry(` No current value found on original storing peers`);
|
|
460
|
-
}
|
|
461
|
-
} catch (error) {
|
|
462
|
-
this.addDHTLogEntry(`❌ Error subscribing to ${key}: ${error.message}`);
|
|
463
|
-
}
|
|
464
|
-
});
|
|
489
|
+
try {
|
|
490
|
+
await this.mesh.dhtUnsubscribe(key);
|
|
491
|
+
this.addDHTLogEntry(`🔕 Unsubscribed from: ${key}`);
|
|
492
|
+
} catch (error) {
|
|
493
|
+
this.addDHTLogEntry(`❌ Error unsubscribing from ${key}: ${error.message}`);
|
|
465
494
|
}
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
setupMediaControls() {
|
|
500
|
+
// Start media button
|
|
501
|
+
document.getElementById('start-media-btn').addEventListener('click', async () => {
|
|
502
|
+
await this.startMedia();
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
// Stop media button
|
|
506
|
+
document.getElementById('stop-media-btn').addEventListener('click', async () => {
|
|
507
|
+
await this.stopMedia();
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
// Toggle video button
|
|
511
|
+
document.getElementById('toggle-video-btn').addEventListener('click', () => {
|
|
512
|
+
this.toggleVideo();
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
// Toggle audio button
|
|
516
|
+
document.getElementById('toggle-audio-btn').addEventListener('click', () => {
|
|
517
|
+
this.toggleAudio();
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
// Audio test button
|
|
521
|
+
document.getElementById('test-audio-btn').addEventListener('click', () => {
|
|
522
|
+
this.testAudio();
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
// Media type checkboxes
|
|
526
|
+
document.getElementById('enable-video').addEventListener('change', () => {
|
|
527
|
+
this.updateMediaButtonStates();
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
document.getElementById('enable-audio').addEventListener('change', () => {
|
|
531
|
+
this.updateMediaButtonStates();
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
// Device selection dropdowns
|
|
535
|
+
document.getElementById('camera-select').addEventListener('change', () => {
|
|
536
|
+
// Auto-restart media if it's currently active
|
|
537
|
+
if (this.mesh.getMediaState().hasLocalStream) {
|
|
538
|
+
this.startMedia();
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
document.getElementById('microphone-select').addEventListener('change', () => {
|
|
543
|
+
// Auto-restart media if it's currently active
|
|
544
|
+
if (this.mesh.getMediaState().hasLocalStream) {
|
|
545
|
+
this.startMedia();
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
setupCollapsibleSections() {
|
|
551
|
+
const sections = [
|
|
552
|
+
'discovered-peers',
|
|
553
|
+
'connected-peers',
|
|
554
|
+
'manual-connection',
|
|
555
|
+
'settings',
|
|
556
|
+
'dht',
|
|
557
|
+
'media'
|
|
558
|
+
];
|
|
559
|
+
|
|
560
|
+
sections.forEach(sectionId => {
|
|
561
|
+
const toggle = document.getElementById(`${sectionId}-toggle`);
|
|
562
|
+
const content = document.getElementById(`${sectionId}-content`);
|
|
563
|
+
|
|
564
|
+
if (toggle && content) {
|
|
565
|
+
toggle.addEventListener('click', () => {
|
|
566
|
+
this.toggleSection(sectionId);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
}
|
|
466
571
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
if (!key) {
|
|
476
|
-
this.addDHTLogEntry('❌ Error: Key is required (enter key in either Put/Update or Get field)');
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (!this.mesh.isDHTEnabled()) {
|
|
481
|
-
this.addDHTLogEntry('❌ Error: WebDHT is disabled');
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
try {
|
|
486
|
-
await this.mesh.dhtUnsubscribe(key);
|
|
487
|
-
this.addDHTLogEntry(`🔕 Unsubscribed from: ${key}`);
|
|
488
|
-
} catch (error) {
|
|
489
|
-
this.addDHTLogEntry(`❌ Error unsubscribing from ${key}: ${error.message}`);
|
|
490
|
-
}
|
|
491
|
-
});
|
|
492
|
-
}
|
|
572
|
+
toggleSection(sectionId) {
|
|
573
|
+
const toggle = document.getElementById(`${sectionId}-toggle`);
|
|
574
|
+
const content = document.getElementById(`${sectionId}-content`);
|
|
575
|
+
|
|
576
|
+
if (!toggle || !content) {
|
|
577
|
+
console.warn(`Could not find elements for section: ${sectionId}`);
|
|
578
|
+
return;
|
|
493
579
|
}
|
|
494
580
|
|
|
495
|
-
|
|
496
|
-
// Start media button
|
|
497
|
-
document.getElementById('start-media-btn').addEventListener('click', async () => {
|
|
498
|
-
await this.startMedia();
|
|
499
|
-
});
|
|
581
|
+
const section = toggle.closest(`.${sectionId}`);
|
|
500
582
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
583
|
+
if (!section) {
|
|
584
|
+
console.warn(`Could not find section container for: ${sectionId}`, {
|
|
585
|
+
toggle,
|
|
586
|
+
toggleParent: toggle.parentElement,
|
|
587
|
+
toggleParentParent: toggle.parentElement?.parentElement,
|
|
588
|
+
classList: toggle.parentElement?.parentElement?.classList
|
|
589
|
+
});
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
505
592
|
|
|
506
|
-
|
|
507
|
-
document.getElementById('toggle-video-btn').addEventListener('click', () => {
|
|
508
|
-
this.toggleVideo();
|
|
509
|
-
});
|
|
593
|
+
const isExpanded = toggle.getAttribute('aria-expanded') === 'true';
|
|
510
594
|
|
|
511
|
-
|
|
512
|
-
document.getElementById('toggle-audio-btn').addEventListener('click', () => {
|
|
513
|
-
this.toggleAudio();
|
|
514
|
-
});
|
|
595
|
+
toggle.setAttribute('aria-expanded', !isExpanded);
|
|
515
596
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
597
|
+
// Add null check before calling setAttribute
|
|
598
|
+
if (section) {
|
|
599
|
+
section.setAttribute('aria-expanded', !isExpanded);
|
|
600
|
+
}
|
|
520
601
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
602
|
+
if (isExpanded) {
|
|
603
|
+
content.style.display = 'none';
|
|
604
|
+
toggle.textContent = toggle.textContent.replace('▼', '▶');
|
|
605
|
+
} else {
|
|
606
|
+
content.style.display = 'block';
|
|
607
|
+
toggle.textContent = toggle.textContent.replace('▶', '▼');
|
|
608
|
+
}
|
|
609
|
+
}
|
|
525
610
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
611
|
+
// Media-related methods
|
|
612
|
+
async initializeMedia() {
|
|
613
|
+
try {
|
|
614
|
+
// Initialize media manager
|
|
615
|
+
await this.mesh.initializeMedia();
|
|
529
616
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
// Auto-restart media if it's currently active
|
|
533
|
-
if (this.mesh.getMediaState().hasLocalStream) {
|
|
534
|
-
this.startMedia();
|
|
535
|
-
}
|
|
536
|
-
});
|
|
617
|
+
// Populate device lists
|
|
618
|
+
await this.updateDeviceLists();
|
|
537
619
|
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
620
|
+
// Update button states
|
|
621
|
+
this.updateMediaButtonStates();
|
|
622
|
+
|
|
623
|
+
console.log('Media initialized successfully');
|
|
624
|
+
} catch (error) {
|
|
625
|
+
console.error('Failed to initialize media:', error);
|
|
626
|
+
this.addMessage('System', `Failed to initialize media: ${error.message}`, 'error');
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
async updateDeviceLists() {
|
|
631
|
+
try {
|
|
632
|
+
const devices = await this.mesh.enumerateMediaDevices();
|
|
633
|
+
|
|
634
|
+
// Update camera list
|
|
635
|
+
const cameraSelect = document.getElementById('camera-select');
|
|
636
|
+
if (cameraSelect) {
|
|
637
|
+
cameraSelect.innerHTML = '<option value="">Select camera...</option>';
|
|
638
|
+
devices.cameras.forEach(device => {
|
|
639
|
+
const option = document.createElement('option');
|
|
640
|
+
option.value = device.deviceId;
|
|
641
|
+
option.textContent = device.label || `Camera ${device.deviceId.substring(0, 8)}...`;
|
|
642
|
+
cameraSelect.appendChild(option);
|
|
643
|
+
});
|
|
644
|
+
cameraSelect.disabled = devices.cameras.length === 0;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// Update microphone list
|
|
648
|
+
const micSelect = document.getElementById('microphone-select');
|
|
649
|
+
if (micSelect) {
|
|
650
|
+
micSelect.innerHTML = '<option value="">Select microphone...</option>';
|
|
651
|
+
devices.microphones.forEach(device => {
|
|
652
|
+
const option = document.createElement('option');
|
|
653
|
+
option.value = device.deviceId;
|
|
654
|
+
option.textContent = device.label || `Microphone ${device.deviceId.substring(0, 8)}...`;
|
|
655
|
+
micSelect.appendChild(option);
|
|
543
656
|
});
|
|
657
|
+
micSelect.disabled = devices.microphones.length === 0;
|
|
658
|
+
}
|
|
659
|
+
} catch (error) {
|
|
660
|
+
console.error('Failed to update device lists:', error);
|
|
544
661
|
}
|
|
662
|
+
}
|
|
545
663
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
sections.forEach(sectionId => {
|
|
557
|
-
const toggle = document.getElementById(`${sectionId}-toggle`);
|
|
558
|
-
const content = document.getElementById(`${sectionId}-content`);
|
|
559
|
-
|
|
560
|
-
if (toggle && content) {
|
|
561
|
-
toggle.addEventListener('click', () => {
|
|
562
|
-
this.toggleSection(sectionId);
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
});
|
|
664
|
+
updateMediaButtonStates() {
|
|
665
|
+
const videoEnabled = document.getElementById('enable-video')?.checked || false;
|
|
666
|
+
const audioEnabled = document.getElementById('enable-audio')?.checked || false;
|
|
667
|
+
const mediaState = this.mesh.getMediaState();
|
|
668
|
+
|
|
669
|
+
// Enable start button if at least one media type is selected
|
|
670
|
+
const startBtn = document.getElementById('start-media-btn');
|
|
671
|
+
if (startBtn) {
|
|
672
|
+
startBtn.disabled = !(videoEnabled || audioEnabled) || mediaState.hasLocalStream;
|
|
566
673
|
}
|
|
567
674
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (!toggle || !content) {
|
|
573
|
-
console.warn(`Could not find elements for section: ${sectionId}`);
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
const section = toggle.closest(`.${sectionId}`);
|
|
578
|
-
|
|
579
|
-
if (!section) {
|
|
580
|
-
console.warn(`Could not find section container for: ${sectionId}`, {
|
|
581
|
-
toggle: toggle,
|
|
582
|
-
toggleParent: toggle.parentElement,
|
|
583
|
-
toggleParentParent: toggle.parentElement?.parentElement,
|
|
584
|
-
classList: toggle.parentElement?.parentElement?.classList
|
|
585
|
-
});
|
|
586
|
-
return;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
const isExpanded = toggle.getAttribute('aria-expanded') === 'true';
|
|
590
|
-
|
|
591
|
-
toggle.setAttribute('aria-expanded', !isExpanded);
|
|
592
|
-
|
|
593
|
-
// Add null check before calling setAttribute
|
|
594
|
-
if (section) {
|
|
595
|
-
section.setAttribute('aria-expanded', !isExpanded);
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
if (isExpanded) {
|
|
599
|
-
content.style.display = 'none';
|
|
600
|
-
toggle.textContent = toggle.textContent.replace('▼', '▶');
|
|
601
|
-
} else {
|
|
602
|
-
content.style.display = 'block';
|
|
603
|
-
toggle.textContent = toggle.textContent.replace('▶', '▼');
|
|
604
|
-
}
|
|
675
|
+
// Enable stop button if media is active
|
|
676
|
+
const stopBtn = document.getElementById('stop-media-btn');
|
|
677
|
+
if (stopBtn) {
|
|
678
|
+
stopBtn.disabled = !mediaState.hasLocalStream;
|
|
605
679
|
}
|
|
606
680
|
|
|
607
|
-
//
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
await this.updateDeviceLists();
|
|
615
|
-
|
|
616
|
-
// Update button states
|
|
617
|
-
this.updateMediaButtonStates();
|
|
618
|
-
|
|
619
|
-
console.log('Media initialized successfully');
|
|
620
|
-
} catch (error) {
|
|
621
|
-
console.error('Failed to initialize media:', error);
|
|
622
|
-
this.addMessage('System', `Failed to initialize media: ${error.message}`, 'error');
|
|
623
|
-
}
|
|
681
|
+
// Enable toggle buttons if media is active
|
|
682
|
+
const toggleVideoBtn = document.getElementById('toggle-video-btn');
|
|
683
|
+
const toggleAudioBtn = document.getElementById('toggle-audio-btn');
|
|
684
|
+
|
|
685
|
+
if (toggleVideoBtn) {
|
|
686
|
+
toggleVideoBtn.disabled = !mediaState.hasLocalStream;
|
|
687
|
+
toggleVideoBtn.textContent = mediaState.videoEnabled ? 'Turn Off Video' : 'Turn On Video';
|
|
624
688
|
}
|
|
625
689
|
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
// Update camera list
|
|
631
|
-
const cameraSelect = document.getElementById('camera-select');
|
|
632
|
-
if (cameraSelect) {
|
|
633
|
-
cameraSelect.innerHTML = '<option value="">Select camera...</option>';
|
|
634
|
-
devices.cameras.forEach(device => {
|
|
635
|
-
const option = document.createElement('option');
|
|
636
|
-
option.value = device.deviceId;
|
|
637
|
-
option.textContent = device.label || `Camera ${device.deviceId.substring(0, 8)}...`;
|
|
638
|
-
cameraSelect.appendChild(option);
|
|
639
|
-
});
|
|
640
|
-
cameraSelect.disabled = devices.cameras.length === 0;
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
// Update microphone list
|
|
644
|
-
const micSelect = document.getElementById('microphone-select');
|
|
645
|
-
if (micSelect) {
|
|
646
|
-
micSelect.innerHTML = '<option value="">Select microphone...</option>';
|
|
647
|
-
devices.microphones.forEach(device => {
|
|
648
|
-
const option = document.createElement('option');
|
|
649
|
-
option.value = device.deviceId;
|
|
650
|
-
option.textContent = device.label || `Microphone ${device.deviceId.substring(0, 8)}...`;
|
|
651
|
-
micSelect.appendChild(option);
|
|
652
|
-
});
|
|
653
|
-
micSelect.disabled = devices.microphones.length === 0;
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
} catch (error) {
|
|
657
|
-
console.error('Failed to update device lists:', error);
|
|
658
|
-
}
|
|
690
|
+
if (toggleAudioBtn) {
|
|
691
|
+
toggleAudioBtn.disabled = !mediaState.hasLocalStream;
|
|
692
|
+
toggleAudioBtn.textContent = mediaState.audioEnabled ? 'Turn Off Audio' : 'Turn On Audio';
|
|
659
693
|
}
|
|
694
|
+
}
|
|
660
695
|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
// Enable toggle buttons if media is active
|
|
679
|
-
const toggleVideoBtn = document.getElementById('toggle-video-btn');
|
|
680
|
-
const toggleAudioBtn = document.getElementById('toggle-audio-btn');
|
|
681
|
-
|
|
682
|
-
if (toggleVideoBtn) {
|
|
683
|
-
toggleVideoBtn.disabled = !mediaState.hasLocalStream;
|
|
684
|
-
toggleVideoBtn.textContent = mediaState.videoEnabled ? 'Turn Off Video' : 'Turn On Video';
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
if (toggleAudioBtn) {
|
|
688
|
-
toggleAudioBtn.disabled = !mediaState.hasLocalStream;
|
|
689
|
-
toggleAudioBtn.textContent = mediaState.audioEnabled ? 'Turn Off Audio' : 'Turn On Audio';
|
|
696
|
+
async startMedia() {
|
|
697
|
+
try {
|
|
698
|
+
const videoEnabled = document.getElementById('enable-video')?.checked || false;
|
|
699
|
+
const audioEnabled = document.getElementById('enable-audio')?.checked || false;
|
|
700
|
+
|
|
701
|
+
// Initialize audio context for better audio support
|
|
702
|
+
if (audioEnabled && (window.AudioContext || window.webkitAudioContext)) {
|
|
703
|
+
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
704
|
+
try {
|
|
705
|
+
const audioContext = new AudioContext();
|
|
706
|
+
if (audioContext.state === 'suspended') {
|
|
707
|
+
await audioContext.resume();
|
|
708
|
+
console.log('Audio context resumed for local media');
|
|
709
|
+
}
|
|
710
|
+
console.log('Audio context state:', audioContext.state);
|
|
711
|
+
} catch (e) {
|
|
712
|
+
console.log('Could not initialize audio context:', e);
|
|
690
713
|
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const deviceIds = {};
|
|
717
|
+
const cameraSelect = document.getElementById('camera-select');
|
|
718
|
+
const micSelect = document.getElementById('microphone-select');
|
|
719
|
+
|
|
720
|
+
if (cameraSelect?.value) {
|
|
721
|
+
deviceIds.camera = cameraSelect.value;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
if (micSelect?.value) {
|
|
725
|
+
deviceIds.microphone = micSelect.value;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
await this.mesh.startMedia({
|
|
729
|
+
video: videoEnabled,
|
|
730
|
+
audio: audioEnabled,
|
|
731
|
+
deviceIds
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
this.addMessage('System', 'Media started successfully');
|
|
735
|
+
this.updateMediaButtonStates();
|
|
736
|
+
} catch (error) {
|
|
737
|
+
console.error('Failed to start media:', error);
|
|
738
|
+
this.addMessage('System', `Failed to start media: ${error.message}`, 'error');
|
|
691
739
|
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
async stopMedia() {
|
|
743
|
+
try {
|
|
744
|
+
await this.mesh.stopMedia();
|
|
745
|
+
this.addMessage('System', 'Media stopped');
|
|
746
|
+
this.updateMediaButtonStates();
|
|
747
|
+
this.clearLocalVideo();
|
|
748
|
+
} catch (error) {
|
|
749
|
+
console.error('Failed to stop media:', error);
|
|
750
|
+
this.addMessage('System', `Failed to stop media: ${error.message}`, 'error');
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
toggleVideo() {
|
|
755
|
+
try {
|
|
756
|
+
const enabled = this.mesh.toggleVideo();
|
|
757
|
+
this.addMessage('System', `Video ${enabled ? 'enabled' : 'disabled'}`);
|
|
758
|
+
this.updateMediaButtonStates();
|
|
759
|
+
} catch (error) {
|
|
760
|
+
console.error('Failed to toggle video:', error);
|
|
761
|
+
this.addMessage('System', `Failed to toggle video: ${error.message}`, 'error');
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
toggleAudio() {
|
|
766
|
+
try {
|
|
767
|
+
const enabled = this.mesh.toggleAudio();
|
|
768
|
+
this.addMessage('System', `Audio ${enabled ? 'enabled' : 'disabled'}`);
|
|
769
|
+
this.updateMediaButtonStates();
|
|
770
|
+
} catch (error) {
|
|
771
|
+
console.error('Failed to toggle audio:', error);
|
|
772
|
+
this.addMessage('System', `Failed to toggle audio: ${error.message}`, 'error');
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
async testAudio() {
|
|
777
|
+
try {
|
|
778
|
+
console.log('🔊 Starting audio test...');
|
|
779
|
+
this.addMessage('System', '🔊 Testing audio system...', 'info');
|
|
780
|
+
|
|
781
|
+
// First, run diagnostics to understand current state
|
|
782
|
+
this.logPeerDiagnostics();
|
|
783
|
+
|
|
784
|
+
// Test 0: Verify we have actual peer connections (not just self)
|
|
785
|
+
const allPeers = this.mesh.getPeers();
|
|
786
|
+
const connectedPeers = allPeers.filter(peer => peer.status === 'connected');
|
|
787
|
+
const peersWithMedia = allPeers.filter(peer => {
|
|
788
|
+
const peerConnection = this.mesh.connectionManager.getPeer(peer.peerId);
|
|
789
|
+
return peerConnection && (peerConnection.remoteStream || peerConnection.connection?.connectionState === 'connected');
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
console.log('🔊 All peers:', allPeers.length);
|
|
793
|
+
console.log('🔊 Connected peers (data channel):', connectedPeers.length);
|
|
794
|
+
console.log('🔊 Peers with media/WebRTC:', peersWithMedia.length);
|
|
795
|
+
|
|
796
|
+
// Log detailed peer status
|
|
797
|
+
allPeers.forEach(peer => {
|
|
798
|
+
const peerConnection = this.mesh.connectionManager.getPeer(peer.peerId);
|
|
799
|
+
console.log(`🔊 Peer ${peer.peerId.substring(0, 8)}:`, {
|
|
800
|
+
status: peer.status,
|
|
801
|
+
webrtcState: peerConnection?.connection?.connectionState,
|
|
802
|
+
dataChannelState: peerConnection?.dataChannel?.readyState,
|
|
803
|
+
hasRemoteStream: !!peerConnection?.remoteStream,
|
|
804
|
+
hasLocalStream: !!peerConnection?.localStream
|
|
805
|
+
});
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
this.addMessage('System', `🔊 Found ${allPeers.length} total peers (${connectedPeers.length} with data channel, ${peersWithMedia.length} with media)`, 'info');
|
|
809
|
+
|
|
810
|
+
if (peersWithMedia.length === 0) {
|
|
811
|
+
this.addMessage('System', '❌ No peers with active media! You need to open this in TWO different browser tabs/windows and connect them.', 'error');
|
|
812
|
+
console.log('❌ NO PEERS WITH MEDIA: Open this URL in two different browser tabs and ensure they connect to each other');
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// Test each peer connection individually
|
|
817
|
+
peersWithMedia.forEach((peer, index) => {
|
|
818
|
+
const connection = this.mesh.connectionManager.peers.get(peer.peerId);
|
|
819
|
+
if (connection) {
|
|
820
|
+
console.log(`🔊 Peer ${index + 1} (${peer.peerId.substring(0, 8)}...):`);
|
|
821
|
+
|
|
822
|
+
const localStream = connection.getLocalStream();
|
|
823
|
+
const remoteStream = connection.getRemoteStream();
|
|
824
|
+
|
|
825
|
+
// Check local stream
|
|
826
|
+
if (localStream) {
|
|
827
|
+
const localAudioTracks = localStream.getAudioTracks();
|
|
828
|
+
console.log(` - Local audio tracks: ${localAudioTracks.length}`);
|
|
829
|
+
this.addMessage('System', ` Peer ${index + 1} - Local audio: ${localAudioTracks.length} tracks`, 'info');
|
|
830
|
+
localAudioTracks.forEach((track, i) => {
|
|
831
|
+
console.log(` Track ${i}: enabled=${track.enabled}, id=${track.id.substring(0, 8)}...`);
|
|
832
|
+
});
|
|
833
|
+
} else {
|
|
834
|
+
console.log(' - No local stream');
|
|
835
|
+
this.addMessage('System', ` Peer ${index + 1} - No local stream`, 'warning');
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
// Check remote stream
|
|
839
|
+
if (remoteStream) {
|
|
840
|
+
const remoteAudioTracks = remoteStream.getAudioTracks();
|
|
841
|
+
console.log(` - Remote audio tracks: ${remoteAudioTracks.length}`);
|
|
842
|
+
this.addMessage('System', ` Peer ${index + 1} - Remote audio: ${remoteAudioTracks.length} tracks`, remoteAudioTracks.length > 0 ? 'success' : 'warning');
|
|
843
|
+
|
|
844
|
+
remoteAudioTracks.forEach((track, i) => {
|
|
845
|
+
console.log(` Track ${i}: enabled=${track.enabled}, id=${track.id.substring(0, 8)}..., readyState=${track.readyState}`);
|
|
846
|
+
});
|
|
692
847
|
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
701
|
-
try {
|
|
702
|
-
const audioContext = new AudioContext();
|
|
703
|
-
if (audioContext.state === 'suspended') {
|
|
704
|
-
await audioContext.resume();
|
|
705
|
-
console.log('Audio context resumed for local media');
|
|
706
|
-
}
|
|
707
|
-
console.log('Audio context state:', audioContext.state);
|
|
708
|
-
} catch (e) {
|
|
709
|
-
console.log('Could not initialize audio context:', e);
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
const deviceIds = {};
|
|
714
|
-
const cameraSelect = document.getElementById('camera-select');
|
|
715
|
-
const micSelect = document.getElementById('microphone-select');
|
|
716
|
-
|
|
717
|
-
if (cameraSelect?.value) {
|
|
718
|
-
deviceIds.camera = cameraSelect.value;
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
if (micSelect?.value) {
|
|
722
|
-
deviceIds.microphone = micSelect.value;
|
|
848
|
+
// CRITICAL: Check if remote stream ID matches local stream ID (loopback detection)
|
|
849
|
+
if (localStream && remoteStream.id === localStream.id) {
|
|
850
|
+
console.error('❌ LOOPBACK DETECTED: Remote stream ID matches local stream ID!');
|
|
851
|
+
this.addMessage('System', `❌ Peer ${index + 1} - LOOPBACK: Getting own audio back!`, 'error');
|
|
852
|
+
} else {
|
|
853
|
+
console.log(`✅ Stream IDs different: local=${localStream?.id.substring(0, 8) || 'none'}, remote=${remoteStream.id.substring(0, 8)}`);
|
|
854
|
+
this.addMessage('System', `✅ Peer ${index + 1} - Stream IDs are different (good!)`, 'success');
|
|
723
855
|
}
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
856
|
+
|
|
857
|
+
// Check video elements playing this stream
|
|
858
|
+
const videoElements = document.querySelectorAll('video');
|
|
859
|
+
let foundVideoElement = false;
|
|
860
|
+
videoElements.forEach(video => {
|
|
861
|
+
if (video.srcObject === remoteStream) {
|
|
862
|
+
foundVideoElement = true;
|
|
863
|
+
console.log(` - Video element: volume=${video.volume}, muted=${video.muted}, paused=${video.paused}, readyState=${video.readyState}`);
|
|
864
|
+
this.addMessage('System', ` Peer ${index + 1} - Video element: volume=${video.volume}, muted=${video.muted}`, video.muted ? 'warning' : 'success');
|
|
865
|
+
}
|
|
729
866
|
});
|
|
730
|
-
|
|
731
|
-
this.addMessage('System', 'Media started successfully');
|
|
732
|
-
this.updateMediaButtonStates();
|
|
733
|
-
|
|
734
|
-
} catch (error) {
|
|
735
|
-
console.error('Failed to start media:', error);
|
|
736
|
-
this.addMessage('System', `Failed to start media: ${error.message}`, 'error');
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
867
|
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
868
|
+
if (!foundVideoElement) {
|
|
869
|
+
console.log(' - ❌ No video element found for this remote stream');
|
|
870
|
+
this.addMessage('System', ` Peer ${index + 1} - ❌ No video element displaying this stream`, 'error');
|
|
871
|
+
}
|
|
872
|
+
} else {
|
|
873
|
+
console.log(' - ❌ No remote stream');
|
|
874
|
+
this.addMessage('System', ` Peer ${index + 1} - ❌ No remote stream received`, 'error');
|
|
875
|
+
}
|
|
876
|
+
} else {
|
|
877
|
+
console.log(`❌ No connection object found for peer ${peer.peerId}`);
|
|
878
|
+
this.addMessage('System', `❌ No connection for peer ${index + 1}`, 'error');
|
|
750
879
|
}
|
|
751
|
-
|
|
880
|
+
});
|
|
752
881
|
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
} catch (error) {
|
|
759
|
-
console.error('Failed to toggle video:', error);
|
|
760
|
-
this.addMessage('System', `Failed to toggle video: ${error.message}`, 'error');
|
|
761
|
-
}
|
|
762
|
-
}
|
|
882
|
+
// Test 1: Audio Context
|
|
883
|
+
if (window.AudioContext || window.webkitAudioContext) {
|
|
884
|
+
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
885
|
+
const audioContext = new AudioContext();
|
|
886
|
+
console.log('Audio context state:', audioContext.state);
|
|
763
887
|
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
this.addMessage('System', `Audio ${enabled ? 'enabled' : 'disabled'}`);
|
|
768
|
-
this.updateMediaButtonStates();
|
|
769
|
-
} catch (error) {
|
|
770
|
-
console.error('Failed to toggle audio:', error);
|
|
771
|
-
this.addMessage('System', `Failed to toggle audio: ${error.message}`, 'error');
|
|
888
|
+
if (audioContext.state === 'suspended') {
|
|
889
|
+
await audioContext.resume();
|
|
890
|
+
console.log('Audio context resumed');
|
|
772
891
|
}
|
|
773
|
-
}
|
|
774
892
|
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
893
|
+
// Generate a test tone
|
|
894
|
+
const oscillator = audioContext.createOscillator();
|
|
895
|
+
const gainNode = audioContext.createGain();
|
|
896
|
+
|
|
897
|
+
oscillator.connect(gainNode);
|
|
898
|
+
gainNode.connect(audioContext.destination);
|
|
899
|
+
|
|
900
|
+
oscillator.frequency.setValueAtTime(440, audioContext.currentTime); // A4 note
|
|
901
|
+
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime); // Low volume
|
|
902
|
+
|
|
903
|
+
oscillator.start();
|
|
904
|
+
oscillator.stop(audioContext.currentTime + 0.5); // Play for 0.5 seconds
|
|
905
|
+
|
|
906
|
+
this.addMessage('System', '🔊 Audio test tone played (440Hz for 0.5s)', 'success');
|
|
907
|
+
} else {
|
|
908
|
+
this.addMessage('System', '❌ Audio context not supported', 'error');
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// Test 2: Check current media streams
|
|
912
|
+
const peers = this.mesh.getPeers();
|
|
913
|
+
console.log('Current peers:', peers.length);
|
|
914
|
+
|
|
915
|
+
peers.forEach(peer => {
|
|
916
|
+
const connection = this.mesh.connectionManager.peers.get(peer.peerId);
|
|
917
|
+
if (connection) {
|
|
918
|
+
const localStream = connection.getLocalStream();
|
|
919
|
+
const remoteStream = connection.getRemoteStream();
|
|
920
|
+
|
|
921
|
+
console.log(`Peer ${peer.peerId.substring(0, 8)}:`);
|
|
922
|
+
if (localStream) {
|
|
923
|
+
const audioTracks = localStream.getAudioTracks();
|
|
924
|
+
console.log(`- Local audio tracks: ${audioTracks.length}`);
|
|
925
|
+
audioTracks.forEach((track, i) => {
|
|
926
|
+
console.log(` Track ${i}: enabled=${track.enabled}, readyState=${track.readyState}, muted=${track.muted}`);
|
|
805
927
|
});
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
// Test each peer connection individually
|
|
816
|
-
peersWithMedia.forEach((peer, index) => {
|
|
817
|
-
const connection = this.mesh.connectionManager.peers.get(peer.peerId);
|
|
818
|
-
if (connection) {
|
|
819
|
-
console.log(`🔊 Peer ${index + 1} (${peer.peerId.substring(0, 8)}...):`);
|
|
820
|
-
|
|
821
|
-
const localStream = connection.getLocalStream();
|
|
822
|
-
const remoteStream = connection.getRemoteStream();
|
|
823
|
-
|
|
824
|
-
// Check local stream
|
|
825
|
-
if (localStream) {
|
|
826
|
-
const localAudioTracks = localStream.getAudioTracks();
|
|
827
|
-
console.log(` - Local audio tracks: ${localAudioTracks.length}`);
|
|
828
|
-
this.addMessage('System', ` Peer ${index + 1} - Local audio: ${localAudioTracks.length} tracks`, 'info');
|
|
829
|
-
localAudioTracks.forEach((track, i) => {
|
|
830
|
-
console.log(` Track ${i}: enabled=${track.enabled}, id=${track.id.substring(0, 8)}...`);
|
|
831
|
-
});
|
|
832
|
-
} else {
|
|
833
|
-
console.log(' - No local stream');
|
|
834
|
-
this.addMessage('System', ` Peer ${index + 1} - No local stream`, 'warning');
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
// Check remote stream
|
|
838
|
-
if (remoteStream) {
|
|
839
|
-
const remoteAudioTracks = remoteStream.getAudioTracks();
|
|
840
|
-
console.log(` - Remote audio tracks: ${remoteAudioTracks.length}`);
|
|
841
|
-
this.addMessage('System', ` Peer ${index + 1} - Remote audio: ${remoteAudioTracks.length} tracks`, remoteAudioTracks.length > 0 ? 'success' : 'warning');
|
|
842
|
-
|
|
843
|
-
remoteAudioTracks.forEach((track, i) => {
|
|
844
|
-
console.log(` Track ${i}: enabled=${track.enabled}, id=${track.id.substring(0, 8)}..., readyState=${track.readyState}`);
|
|
845
|
-
});
|
|
846
|
-
|
|
847
|
-
// CRITICAL: Check if remote stream ID matches local stream ID (loopback detection)
|
|
848
|
-
if (localStream && remoteStream.id === localStream.id) {
|
|
849
|
-
console.error(`❌ LOOPBACK DETECTED: Remote stream ID matches local stream ID!`);
|
|
850
|
-
this.addMessage('System', `❌ Peer ${index + 1} - LOOPBACK: Getting own audio back!`, 'error');
|
|
851
|
-
} else {
|
|
852
|
-
console.log(`✅ Stream IDs different: local=${localStream?.id.substring(0, 8) || 'none'}, remote=${remoteStream.id.substring(0, 8)}`);
|
|
853
|
-
this.addMessage('System', `✅ Peer ${index + 1} - Stream IDs are different (good!)`, 'success');
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
// Check video elements playing this stream
|
|
857
|
-
const videoElements = document.querySelectorAll('video');
|
|
858
|
-
let foundVideoElement = false;
|
|
859
|
-
videoElements.forEach(video => {
|
|
860
|
-
if (video.srcObject === remoteStream) {
|
|
861
|
-
foundVideoElement = true;
|
|
862
|
-
console.log(` - Video element: volume=${video.volume}, muted=${video.muted}, paused=${video.paused}, readyState=${video.readyState}`);
|
|
863
|
-
this.addMessage('System', ` Peer ${index + 1} - Video element: volume=${video.volume}, muted=${video.muted}`, video.muted ? 'warning' : 'success');
|
|
864
|
-
}
|
|
865
|
-
});
|
|
866
|
-
|
|
867
|
-
if (!foundVideoElement) {
|
|
868
|
-
console.log(` - ❌ No video element found for this remote stream`);
|
|
869
|
-
this.addMessage('System', ` Peer ${index + 1} - ❌ No video element displaying this stream`, 'error');
|
|
870
|
-
}
|
|
871
|
-
} else {
|
|
872
|
-
console.log(' - ❌ No remote stream');
|
|
873
|
-
this.addMessage('System', ` Peer ${index + 1} - ❌ No remote stream received`, 'error');
|
|
874
|
-
}
|
|
875
|
-
} else {
|
|
876
|
-
console.log(`❌ No connection object found for peer ${peer.peerId}`);
|
|
877
|
-
this.addMessage('System', `❌ No connection for peer ${index + 1}`, 'error');
|
|
878
|
-
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
if (remoteStream) {
|
|
931
|
+
const audioTracks = remoteStream.getAudioTracks();
|
|
932
|
+
console.log(`- Remote audio tracks: ${audioTracks.length}`);
|
|
933
|
+
audioTracks.forEach((track, i) => {
|
|
934
|
+
console.log(` Track ${i}: enabled=${track.enabled}, readyState=${track.readyState}, muted=${track.muted}`);
|
|
879
935
|
});
|
|
880
|
-
|
|
881
|
-
//
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
console.log(
|
|
886
|
-
|
|
887
|
-
if (audioContext.state === 'suspended') {
|
|
888
|
-
await audioContext.resume();
|
|
889
|
-
console.log('Audio context resumed');
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
// Generate a test tone
|
|
893
|
-
const oscillator = audioContext.createOscillator();
|
|
894
|
-
const gainNode = audioContext.createGain();
|
|
895
|
-
|
|
896
|
-
oscillator.connect(gainNode);
|
|
897
|
-
gainNode.connect(audioContext.destination);
|
|
898
|
-
|
|
899
|
-
oscillator.frequency.setValueAtTime(440, audioContext.currentTime); // A4 note
|
|
900
|
-
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime); // Low volume
|
|
901
|
-
|
|
902
|
-
oscillator.start();
|
|
903
|
-
oscillator.stop(audioContext.currentTime + 0.5); // Play for 0.5 seconds
|
|
904
|
-
|
|
905
|
-
this.addMessage('System', '🔊 Audio test tone played (440Hz for 0.5s)', 'success');
|
|
906
|
-
} else {
|
|
907
|
-
this.addMessage('System', '❌ Audio context not supported', 'error');
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
// Test 2: Check current media streams
|
|
911
|
-
const peers = this.mesh.getPeers();
|
|
912
|
-
console.log('Current peers:', peers.length);
|
|
913
|
-
|
|
914
|
-
peers.forEach(peer => {
|
|
915
|
-
const connection = this.mesh.connectionManager.peers.get(peer.peerId);
|
|
916
|
-
if (connection) {
|
|
917
|
-
const localStream = connection.getLocalStream();
|
|
918
|
-
const remoteStream = connection.getRemoteStream();
|
|
919
|
-
|
|
920
|
-
console.log(`Peer ${peer.peerId.substring(0, 8)}:`);
|
|
921
|
-
if (localStream) {
|
|
922
|
-
const audioTracks = localStream.getAudioTracks();
|
|
923
|
-
console.log(`- Local audio tracks: ${audioTracks.length}`);
|
|
924
|
-
audioTracks.forEach((track, i) => {
|
|
925
|
-
console.log(` Track ${i}: enabled=${track.enabled}, readyState=${track.readyState}, muted=${track.muted}`);
|
|
926
|
-
});
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
if (remoteStream) {
|
|
930
|
-
const audioTracks = remoteStream.getAudioTracks();
|
|
931
|
-
console.log(`- Remote audio tracks: ${audioTracks.length}`);
|
|
932
|
-
audioTracks.forEach((track, i) => {
|
|
933
|
-
console.log(` Track ${i}: enabled=${track.enabled}, readyState=${track.readyState}, muted=${track.muted}`);
|
|
934
|
-
});
|
|
935
|
-
|
|
936
|
-
// Check if there are video elements for this stream
|
|
937
|
-
const videoElements = document.querySelectorAll('video');
|
|
938
|
-
videoElements.forEach(video => {
|
|
939
|
-
if (video.srcObject === remoteStream) {
|
|
940
|
-
console.log(`- Video element: volume=${video.volume}, muted=${video.muted}, paused=${video.paused}`);
|
|
941
|
-
}
|
|
942
|
-
});
|
|
943
|
-
}
|
|
944
|
-
}
|
|
936
|
+
|
|
937
|
+
// Check if there are video elements for this stream
|
|
938
|
+
const videoElements = document.querySelectorAll('video');
|
|
939
|
+
videoElements.forEach(video => {
|
|
940
|
+
if (video.srcObject === remoteStream) {
|
|
941
|
+
console.log(`- Video element: volume=${video.volume}, muted=${video.muted}, paused=${video.paused}`);
|
|
942
|
+
}
|
|
945
943
|
});
|
|
946
|
-
|
|
947
|
-
// Test 3: Check microphone access
|
|
948
|
-
try {
|
|
949
|
-
const testStream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
950
|
-
console.log('✅ Microphone access working');
|
|
951
|
-
this.addMessage('System', '✅ Microphone access: OK', 'success');
|
|
952
|
-
testStream.getTracks().forEach(track => track.stop());
|
|
953
|
-
} catch (error) {
|
|
954
|
-
console.log('❌ Microphone access failed:', error);
|
|
955
|
-
this.addMessage('System', `❌ Microphone access failed: ${error.message}`, 'error');
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
} catch (error) {
|
|
959
|
-
console.error('Audio test failed:', error);
|
|
960
|
-
this.addMessage('System', `❌ Audio test failed: ${error.message}`, 'error');
|
|
944
|
+
}
|
|
961
945
|
}
|
|
946
|
+
});
|
|
947
|
+
|
|
948
|
+
// Test 3: Check microphone access
|
|
949
|
+
try {
|
|
950
|
+
const testStream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
951
|
+
console.log('✅ Microphone access working');
|
|
952
|
+
this.addMessage('System', '✅ Microphone access: OK', 'success');
|
|
953
|
+
testStream.getTracks().forEach(track => track.stop());
|
|
954
|
+
} catch (error) {
|
|
955
|
+
console.log('❌ Microphone access failed:', error);
|
|
956
|
+
this.addMessage('System', `❌ Microphone access failed: ${error.message}`, 'error');
|
|
957
|
+
}
|
|
958
|
+
} catch (error) {
|
|
959
|
+
console.error('Audio test failed:', error);
|
|
960
|
+
this.addMessage('System', `❌ Audio test failed: ${error.message}`, 'error');
|
|
962
961
|
}
|
|
962
|
+
}
|
|
963
963
|
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
const mediaTypes = [];
|
|
973
|
-
if (data.video) mediaTypes.push('video');
|
|
974
|
-
if (data.audio) mediaTypes.push('audio');
|
|
975
|
-
|
|
976
|
-
this.addMessage('System', `Local ${mediaTypes.join(' and ')} started`);
|
|
964
|
+
handleLocalStreamStarted(data) {
|
|
965
|
+
this.updateMediaButtonStates();
|
|
966
|
+
|
|
967
|
+
// Display local video if video is enabled
|
|
968
|
+
if (data.video) {
|
|
969
|
+
this.displayLocalVideo(data.stream);
|
|
977
970
|
}
|
|
978
971
|
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
972
|
+
const mediaTypes = [];
|
|
973
|
+
if (data.video) mediaTypes.push('video');
|
|
974
|
+
if (data.audio) mediaTypes.push('audio');
|
|
975
|
+
|
|
976
|
+
this.addMessage('System', `Local ${mediaTypes.join(' and ')} started`);
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
handleLocalStreamStopped() {
|
|
980
|
+
this.updateMediaButtonStates();
|
|
981
|
+
this.clearLocalVideo();
|
|
982
|
+
this.addMessage('System', 'Local media stopped');
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
displayLocalVideo(stream) {
|
|
986
|
+
const localVideoContainer = document.getElementById('local-video-container');
|
|
987
|
+
if (!localVideoContainer) return;
|
|
988
|
+
|
|
989
|
+
let video = localVideoContainer.querySelector('video');
|
|
990
|
+
if (!video) {
|
|
991
|
+
video = document.createElement('video');
|
|
992
|
+
video.autoplay = true;
|
|
993
|
+
video.muted = true;
|
|
994
|
+
video.playsInline = true;
|
|
995
|
+
video.style.width = '100%';
|
|
996
|
+
video.style.borderRadius = '8px';
|
|
997
|
+
localVideoContainer.appendChild(video);
|
|
983
998
|
}
|
|
984
999
|
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
video.style.borderRadius = '8px';
|
|
997
|
-
localVideoContainer.appendChild(video);
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
video.srcObject = stream;
|
|
1000
|
+
video.srcObject = stream;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
clearLocalVideo() {
|
|
1004
|
+
const localVideoContainer = document.getElementById('local-video-container');
|
|
1005
|
+
if (localVideoContainer) {
|
|
1006
|
+
const video = localVideoContainer.querySelector('video');
|
|
1007
|
+
if (video) {
|
|
1008
|
+
video.srcObject = null;
|
|
1009
|
+
video.remove();
|
|
1010
|
+
}
|
|
1001
1011
|
}
|
|
1012
|
+
}
|
|
1002
1013
|
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1014
|
+
handleRemoteStream(data) {
|
|
1015
|
+
console.log('🎵 Handling remote stream:', data);
|
|
1016
|
+
const { peerId, stream } = data;
|
|
1017
|
+
|
|
1018
|
+
// CRITICAL: Prevent audio loopback - check if this is our own stream
|
|
1019
|
+
if (peerId === this.mesh.peerId) {
|
|
1020
|
+
console.warn('🚨 LOOPBACK DETECTED: Received our own stream as remote! This should not happen.');
|
|
1021
|
+
console.warn('Stream ID:', stream.id);
|
|
1022
|
+
// Still display it but it will be muted by displayRemoteVideo
|
|
1012
1023
|
}
|
|
1013
1024
|
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
audioTracksEnabled: audioTracks.filter(t => t.enabled).length,
|
|
1063
|
-
streamActive: stream.active,
|
|
1064
|
-
streamId: stream.id
|
|
1065
|
-
};
|
|
1066
|
-
|
|
1067
|
-
console.log(`🎵 AUDIO DATA EXPECTATION for peer ${audioSummary.peerIdShort}:`, audioSummary);
|
|
1068
|
-
|
|
1069
|
-
if (audioTracks.length > 0) {
|
|
1070
|
-
console.log(`✅ EXPECTING AUDIO DATA from peer ${audioSummary.peerIdShort} - ${audioSummary.audioTracksEnabled}/${audioSummary.audioTrackCount} tracks enabled`);
|
|
1071
|
-
} else {
|
|
1072
|
-
console.log(`❌ NO AUDIO TRACKS from peer ${audioSummary.peerIdShort} - video only`);
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
|
-
this.addMessage('System', `🎵 Remote stream received from ${peerId.substring(0, 8)}... (${audioTracks.length} audio, ${videoTracks.length} video)`);
|
|
1025
|
+
// Detailed audio analysis
|
|
1026
|
+
const audioTracks = stream.getAudioTracks();
|
|
1027
|
+
const videoTracks = stream.getVideoTracks();
|
|
1028
|
+
|
|
1029
|
+
console.log(`Stream from ${peerId.substring(0, 8)}: ${audioTracks.length} audio, ${videoTracks.length} video tracks`);
|
|
1030
|
+
|
|
1031
|
+
audioTracks.forEach((track, i) => {
|
|
1032
|
+
console.log(`🎵 Audio track ${i}:`, {
|
|
1033
|
+
id: track.id,
|
|
1034
|
+
kind: track.kind,
|
|
1035
|
+
enabled: track.enabled,
|
|
1036
|
+
readyState: track.readyState,
|
|
1037
|
+
muted: track.muted,
|
|
1038
|
+
label: track.label
|
|
1039
|
+
});
|
|
1040
|
+
|
|
1041
|
+
// Add track event listeners for state monitoring
|
|
1042
|
+
track.addEventListener('ended', () => {
|
|
1043
|
+
console.log(`🎵 Audio track ${i} from peer ${peerId.substring(0, 8)} ENDED`);
|
|
1044
|
+
});
|
|
1045
|
+
|
|
1046
|
+
track.addEventListener('mute', () => {
|
|
1047
|
+
console.log(`🎵 Audio track ${i} from peer ${peerId.substring(0, 8)} MUTED`);
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
track.addEventListener('unmute', () => {
|
|
1051
|
+
console.log(`🎵 Audio track ${i} from peer ${peerId.substring(0, 8)} UNMUTED`);
|
|
1052
|
+
});
|
|
1053
|
+
});
|
|
1054
|
+
|
|
1055
|
+
this.displayRemoteVideo(peerId, stream);
|
|
1056
|
+
|
|
1057
|
+
// Audio data summary
|
|
1058
|
+
const audioSummary = {
|
|
1059
|
+
peerIdShort: peerId.substring(0, 8),
|
|
1060
|
+
audioTrackCount: audioTracks.length,
|
|
1061
|
+
videoTrackCount: videoTracks.length,
|
|
1062
|
+
audioTracksEnabled: audioTracks.filter(t => t.enabled).length,
|
|
1063
|
+
streamActive: stream.active,
|
|
1064
|
+
streamId: stream.id
|
|
1065
|
+
};
|
|
1066
|
+
|
|
1067
|
+
console.log(`🎵 AUDIO DATA EXPECTATION for peer ${audioSummary.peerIdShort}:`, audioSummary);
|
|
1068
|
+
|
|
1069
|
+
if (audioTracks.length > 0) {
|
|
1070
|
+
console.log(`✅ EXPECTING AUDIO DATA from peer ${audioSummary.peerIdShort} - ${audioSummary.audioTracksEnabled}/${audioSummary.audioTrackCount} tracks enabled`);
|
|
1071
|
+
} else {
|
|
1072
|
+
console.log(`❌ NO AUDIO TRACKS from peer ${audioSummary.peerIdShort} - video only`);
|
|
1076
1073
|
}
|
|
1077
1074
|
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
if (!remoteVideosContainer) return;
|
|
1090
|
-
|
|
1091
|
-
let remoteVideoItem = remoteVideosContainer.querySelector(`[data-peer-id="${peerId}"]`);
|
|
1092
|
-
|
|
1093
|
-
if (!remoteVideoItem) {
|
|
1094
|
-
remoteVideoItem = document.createElement('div');
|
|
1095
|
-
remoteVideoItem.className = 'remote-video-item';
|
|
1096
|
-
remoteVideoItem.setAttribute('data-peer-id', peerId);
|
|
1097
|
-
|
|
1098
|
-
const title = document.createElement('div');
|
|
1099
|
-
title.className = 'video-title';
|
|
1100
|
-
title.textContent = `Peer ${peerId.substring(0, 8)}...`;
|
|
1101
|
-
|
|
1102
|
-
const video = document.createElement('video');
|
|
1103
|
-
video.autoplay = true;
|
|
1104
|
-
video.playsInline = true;
|
|
1105
|
-
video.controls = true;
|
|
1106
|
-
// CRITICAL: Mute if this is our own stream to prevent audio feedback
|
|
1107
|
-
video.muted = isOwnStream; // Mute our own audio, unmute others
|
|
1108
|
-
video.style.width = '100%';
|
|
1109
|
-
video.style.borderRadius = '8px';
|
|
1110
|
-
|
|
1111
|
-
// Enhanced audio debugging
|
|
1112
|
-
video.addEventListener('loadedmetadata', () => {
|
|
1113
|
-
console.log('Video element loaded metadata:', {
|
|
1114
|
-
duration: video.duration,
|
|
1115
|
-
audioTracks: video.audioTracks?.length || 'N/A',
|
|
1116
|
-
volume: video.volume,
|
|
1117
|
-
muted: video.muted
|
|
1118
|
-
});
|
|
1119
|
-
|
|
1120
|
-
video.play().then(() => {
|
|
1121
|
-
console.log('Video/audio playback started successfully');
|
|
1122
|
-
// Check audio context state
|
|
1123
|
-
if (window.AudioContext || window.webkitAudioContext) {
|
|
1124
|
-
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
1125
|
-
const audioContext = new AudioContext();
|
|
1126
|
-
console.log('Audio context state:', audioContext.state);
|
|
1127
|
-
if (audioContext.state === 'suspended') {
|
|
1128
|
-
console.log('Audio context is suspended - may need user interaction');
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
// Hide any play button if playback started
|
|
1132
|
-
const playButton = remoteVideoItem.querySelector('.manual-play-button');
|
|
1133
|
-
if (playButton) {
|
|
1134
|
-
playButton.style.display = 'none';
|
|
1135
|
-
}
|
|
1136
|
-
}).catch(error => {
|
|
1137
|
-
console.log('Autoplay failed, user interaction required:', error);
|
|
1138
|
-
// Show a manual play button
|
|
1139
|
-
const playButton = document.createElement('button');
|
|
1140
|
-
playButton.className = 'manual-play-button';
|
|
1141
|
-
playButton.textContent = '▶ Click to Play Audio';
|
|
1142
|
-
playButton.style.cssText = 'position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 10; padding: 10px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;';
|
|
1143
|
-
|
|
1144
|
-
playButton.addEventListener('click', async () => {
|
|
1145
|
-
try {
|
|
1146
|
-
await video.play();
|
|
1147
|
-
playButton.style.display = 'none';
|
|
1148
|
-
console.log('Manual play successful');
|
|
1149
|
-
} catch (e) {
|
|
1150
|
-
console.error('Manual play failed:', e);
|
|
1151
|
-
}
|
|
1152
|
-
});
|
|
1153
|
-
|
|
1154
|
-
remoteVideoItem.style.position = 'relative';
|
|
1155
|
-
remoteVideoItem.appendChild(playButton);
|
|
1156
|
-
});
|
|
1157
|
-
});
|
|
1158
|
-
|
|
1159
|
-
// Add event listeners for audio debugging
|
|
1160
|
-
video.addEventListener('play', () => {
|
|
1161
|
-
console.log('Video element started playing');
|
|
1162
|
-
});
|
|
1163
|
-
|
|
1164
|
-
video.addEventListener('pause', () => {
|
|
1165
|
-
console.log('Video element paused');
|
|
1166
|
-
});
|
|
1167
|
-
|
|
1168
|
-
video.addEventListener('volumechange', () => {
|
|
1169
|
-
console.log('Volume changed:', video.volume, 'Muted:', video.muted);
|
|
1170
|
-
});
|
|
1171
|
-
|
|
1172
|
-
const status = document.createElement('div');
|
|
1173
|
-
status.className = 'video-status';
|
|
1174
|
-
|
|
1175
|
-
remoteVideoItem.appendChild(title);
|
|
1176
|
-
remoteVideoItem.appendChild(video);
|
|
1177
|
-
remoteVideoItem.appendChild(status);
|
|
1178
|
-
|
|
1179
|
-
remoteVideosContainer.appendChild(remoteVideoItem);
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
const video = remoteVideoItem.querySelector('video');
|
|
1183
|
-
const status = remoteVideoItem.querySelector('.video-status');
|
|
1184
|
-
|
|
1185
|
-
video.srcObject = stream;
|
|
1186
|
-
|
|
1187
|
-
// EXPLICIT AUDIO DEBUGGING
|
|
1188
|
-
console.log('🎵 Setting srcObject for video element:', {
|
|
1189
|
-
stream: stream,
|
|
1190
|
-
streamId: stream.id,
|
|
1191
|
-
streamActive: stream.active,
|
|
1192
|
-
videoElement: video,
|
|
1193
|
-
videoMuted: video.muted,
|
|
1194
|
-
videoVolume: video.volume,
|
|
1195
|
-
isOwnStream: isOwnStream
|
|
1196
|
-
});
|
|
1197
|
-
|
|
1198
|
-
const audioTracks = stream.getAudioTracks();
|
|
1199
|
-
const videoTracks = stream.getVideoTracks();
|
|
1200
|
-
|
|
1201
|
-
console.log('🎵 Stream tracks assigned:', {
|
|
1202
|
-
audioTracks: audioTracks.map(t => ({
|
|
1203
|
-
id: t.id,
|
|
1204
|
-
enabled: t.enabled,
|
|
1205
|
-
readyState: t.readyState,
|
|
1206
|
-
muted: t.muted
|
|
1207
|
-
})),
|
|
1208
|
-
videoTracks: videoTracks.map(t => ({
|
|
1209
|
-
id: t.id,
|
|
1210
|
-
enabled: t.enabled,
|
|
1211
|
-
readyState: t.readyState,
|
|
1212
|
-
muted: t.muted
|
|
1213
|
-
}))
|
|
1214
|
-
});
|
|
1215
|
-
|
|
1216
|
-
// CRITICAL: Set audio state based on whether this is our own stream
|
|
1217
|
-
if (isOwnStream) {
|
|
1218
|
-
console.warn('🚨 Muting our own stream to prevent audio feedback');
|
|
1219
|
-
video.muted = true;
|
|
1220
|
-
video.volume = 0;
|
|
1221
|
-
} else {
|
|
1222
|
-
// Only unmute if this is NOT our own stream
|
|
1223
|
-
video.muted = false;
|
|
1224
|
-
video.volume = 1.0;
|
|
1225
|
-
|
|
1226
|
-
// Setup audio playback monitoring for remote streams
|
|
1227
|
-
this.setupAudioPlaybackMonitoring(video, peerId, audioTracks);
|
|
1228
|
-
}
|
|
1229
|
-
|
|
1230
|
-
// Update status based on stream tracks
|
|
1231
|
-
console.log(`Video tracks: ${videoTracks.length}, Audio tracks: ${audioTracks.length}`);
|
|
1232
|
-
audioTracks.forEach((track, i) => {
|
|
1233
|
-
console.log(`Audio track ${i}:`, { enabled: track.enabled, muted: track.muted, label: track.label });
|
|
1234
|
-
});
|
|
1235
|
-
|
|
1236
|
-
// For audio-only streams, ensure the video element is properly configured
|
|
1237
|
-
if (audioTracks.length > 0 && videoTracks.length === 0) {
|
|
1238
|
-
console.log('Audio-only stream detected - configuring for audio playback');
|
|
1239
|
-
video.style.height = '60px'; // Smaller height for audio-only
|
|
1240
|
-
video.style.backgroundColor = '#f0f0f0';
|
|
1241
|
-
|
|
1242
|
-
// Force audio to play by trying to resume audio context
|
|
1243
|
-
if (window.AudioContext || window.webkitAudioContext) {
|
|
1244
|
-
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
1245
|
-
try {
|
|
1246
|
-
const audioContext = new AudioContext();
|
|
1247
|
-
if (audioContext.state === 'suspended') {
|
|
1248
|
-
audioContext.resume().then(() => {
|
|
1249
|
-
console.log('Audio context resumed for remote stream');
|
|
1250
|
-
});
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
// Test audio by creating a brief analysis
|
|
1254
|
-
try {
|
|
1255
|
-
const source = audioContext.createMediaStreamSource(stream);
|
|
1256
|
-
const analyser = audioContext.createAnalyser();
|
|
1257
|
-
source.connect(analyser);
|
|
1258
|
-
|
|
1259
|
-
const dataArray = new Uint8Array(analyser.frequencyBinCount);
|
|
1260
|
-
const checkAudio = () => {
|
|
1261
|
-
analyser.getByteFrequencyData(dataArray);
|
|
1262
|
-
const sum = dataArray.reduce((a, b) => a + b, 0);
|
|
1263
|
-
if (sum > 0) {
|
|
1264
|
-
console.log('Remote audio data detected! Sum:', sum);
|
|
1265
|
-
}
|
|
1266
|
-
};
|
|
1267
|
-
|
|
1268
|
-
// Check audio data periodically
|
|
1269
|
-
const audioChecker = setInterval(checkAudio, 1000);
|
|
1270
|
-
setTimeout(() => clearInterval(audioChecker), 10000); // Stop after 10 seconds
|
|
1271
|
-
|
|
1272
|
-
} catch (audioAnalysisError) {
|
|
1273
|
-
console.log('Could not analyze remote audio:', audioAnalysisError);
|
|
1274
|
-
}
|
|
1275
|
-
} catch (e) {
|
|
1276
|
-
console.log('Could not create/resume audio context:', e);
|
|
1277
|
-
}
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
const statusText = [];
|
|
1282
|
-
if (videoTracks.length > 0) statusText.push('Video');
|
|
1283
|
-
if (audioTracks.length > 0) statusText.push('Audio');
|
|
1284
|
-
|
|
1285
|
-
status.textContent = statusText.length > 0 ? statusText.join(' + ') : 'Audio only';
|
|
1286
|
-
status.style.color = '#28a745';
|
|
1287
|
-
|
|
1288
|
-
this.addMessage('System', `Receiving ${statusText.join(' + ')} from ${peerId.substring(0, 8)}...`);
|
|
1075
|
+
this.addMessage('System', `🎵 Remote stream received from ${peerId.substring(0, 8)}... (${audioTracks.length} audio, ${videoTracks.length} video)`);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
displayRemoteVideo(peerId, stream) {
|
|
1079
|
+
console.log(`Displaying remote video for ${peerId}:`, stream);
|
|
1080
|
+
console.log('Stream tracks:', stream.getTracks().map(t => ({ kind: t.kind, enabled: t.enabled })));
|
|
1081
|
+
|
|
1082
|
+
// CRITICAL: Prevent audio loopback - check if this is our own stream
|
|
1083
|
+
const isOwnStream = peerId === this.mesh.peerId;
|
|
1084
|
+
if (isOwnStream) {
|
|
1085
|
+
console.warn('🚨 LOOPBACK DETECTED: Received our own stream! Muting audio to prevent feedback.');
|
|
1289
1086
|
}
|
|
1290
1087
|
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1088
|
+
const remoteVideosContainer = document.getElementById('remote-videos-container');
|
|
1089
|
+
if (!remoteVideosContainer) return;
|
|
1090
|
+
|
|
1091
|
+
let remoteVideoItem = remoteVideosContainer.querySelector(`[data-peer-id="${peerId}"]`);
|
|
1092
|
+
|
|
1093
|
+
if (!remoteVideoItem) {
|
|
1094
|
+
remoteVideoItem = document.createElement('div');
|
|
1095
|
+
remoteVideoItem.className = 'remote-video-item';
|
|
1096
|
+
remoteVideoItem.setAttribute('data-peer-id', peerId);
|
|
1097
|
+
|
|
1098
|
+
const title = document.createElement('div');
|
|
1099
|
+
title.className = 'video-title';
|
|
1100
|
+
title.textContent = `Peer ${peerId.substring(0, 8)}...`;
|
|
1101
|
+
|
|
1102
|
+
const video = document.createElement('video');
|
|
1103
|
+
video.autoplay = true;
|
|
1104
|
+
video.playsInline = true;
|
|
1105
|
+
video.controls = true;
|
|
1106
|
+
// CRITICAL: Mute if this is our own stream to prevent audio feedback
|
|
1107
|
+
video.muted = isOwnStream; // Mute our own audio, unmute others
|
|
1108
|
+
video.style.width = '100%';
|
|
1109
|
+
video.style.borderRadius = '8px';
|
|
1110
|
+
|
|
1111
|
+
// Enhanced audio debugging
|
|
1112
|
+
video.addEventListener('loadedmetadata', () => {
|
|
1113
|
+
console.log('Video element loaded metadata:', {
|
|
1114
|
+
duration: video.duration,
|
|
1115
|
+
audioTracks: video.audioTracks?.length || 'N/A',
|
|
1116
|
+
volume: video.volume,
|
|
1117
|
+
muted: video.muted
|
|
1118
|
+
});
|
|
1119
|
+
|
|
1120
|
+
video.play().then(() => {
|
|
1121
|
+
console.log('Video/audio playback started successfully');
|
|
1122
|
+
// Check audio context state
|
|
1123
|
+
if (window.AudioContext || window.webkitAudioContext) {
|
|
1325
1124
|
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1125
|
+
const audioContext = new AudioContext();
|
|
1126
|
+
console.log('Audio context state:', audioContext.state);
|
|
1127
|
+
if (audioContext.state === 'suspended') {
|
|
1128
|
+
console.log('Audio context is suspended - may need user interaction');
|
|
1329
1129
|
}
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
let maxPlaybackLevel = 0;
|
|
1352
|
-
|
|
1353
|
-
const monitorPlayback = () => {
|
|
1354
|
-
if (videoElement.paused || videoElement.ended) {
|
|
1355
|
-
return; // Stop monitoring if playback stopped
|
|
1356
|
-
}
|
|
1357
|
-
|
|
1358
|
-
analyser.getByteFrequencyData(dataArray);
|
|
1359
|
-
|
|
1360
|
-
// Calculate playback audio level
|
|
1361
|
-
const average = dataArray.reduce((sum, value) => sum + value, 0) / bufferLength;
|
|
1362
|
-
const currentTime = Date.now();
|
|
1363
|
-
|
|
1364
|
-
totalSamples++;
|
|
1365
|
-
if (average > 3) { // Lower threshold for playback detection
|
|
1366
|
-
playbackSamples++;
|
|
1367
|
-
maxPlaybackLevel = Math.max(maxPlaybackLevel, average);
|
|
1368
|
-
}
|
|
1369
|
-
|
|
1370
|
-
// Log every 3 seconds
|
|
1371
|
-
if (currentTime - lastLogTime > 3000) {
|
|
1372
|
-
const playbackActivity = totalSamples > 0 ? (playbackSamples / totalSamples * 100) : 0;
|
|
1373
|
-
console.log(`🔊 Audio PLAYBACK from peer ${peerIdShort}:`, {
|
|
1374
|
-
videoElementMuted: videoElement.muted,
|
|
1375
|
-
videoElementVolume: videoElement.volume,
|
|
1376
|
-
currentPlaybackLevel: Math.round(average),
|
|
1377
|
-
maxPlaybackLevel: Math.round(maxPlaybackLevel),
|
|
1378
|
-
playbackActivityPercent: Math.round(playbackActivity),
|
|
1379
|
-
samplesAnalyzed: totalSamples,
|
|
1380
|
-
audioBeingPlayed: playbackSamples > 0,
|
|
1381
|
-
audioContextState: audioContext.state
|
|
1382
|
-
});
|
|
1383
|
-
|
|
1384
|
-
lastLogTime = currentTime;
|
|
1385
|
-
totalSamples = 0;
|
|
1386
|
-
playbackSamples = 0;
|
|
1387
|
-
maxPlaybackLevel = 0;
|
|
1388
|
-
}
|
|
1389
|
-
|
|
1390
|
-
// Continue monitoring
|
|
1391
|
-
if (!videoElement.paused && !videoElement.ended) {
|
|
1392
|
-
requestAnimationFrame(monitorPlayback);
|
|
1393
|
-
}
|
|
1394
|
-
};
|
|
1395
|
-
|
|
1396
|
-
// Start monitoring when video plays
|
|
1397
|
-
videoElement.addEventListener('play', () => {
|
|
1398
|
-
console.log(`🔊 Starting audio playback monitoring for peer ${peerIdShort}`);
|
|
1399
|
-
if (audioContext.state === 'suspended') {
|
|
1400
|
-
audioContext.resume().then(() => {
|
|
1401
|
-
requestAnimationFrame(monitorPlayback);
|
|
1402
|
-
});
|
|
1403
|
-
} else {
|
|
1404
|
-
requestAnimationFrame(monitorPlayback);
|
|
1405
|
-
}
|
|
1406
|
-
});
|
|
1407
|
-
|
|
1408
|
-
console.log(`🔊 Audio playback analysis setup complete for peer ${peerIdShort}`);
|
|
1409
|
-
|
|
1410
|
-
} catch (error) {
|
|
1411
|
-
console.error(`🔊 Failed to setup audio playback analysis for peer ${peerIdShort}:`, error);
|
|
1412
|
-
}
|
|
1413
|
-
};
|
|
1414
|
-
|
|
1415
|
-
if (videoElement.readyState >= 1) {
|
|
1416
|
-
// Video metadata already loaded
|
|
1417
|
-
setupAudioAnalysis();
|
|
1418
|
-
} else {
|
|
1419
|
-
// Wait for metadata to load
|
|
1420
|
-
videoElement.addEventListener('loadedmetadata', setupAudioAnalysis, { once: true });
|
|
1130
|
+
}
|
|
1131
|
+
// Hide any play button if playback started
|
|
1132
|
+
const playButton = remoteVideoItem.querySelector('.manual-play-button');
|
|
1133
|
+
if (playButton) {
|
|
1134
|
+
playButton.style.display = 'none';
|
|
1135
|
+
}
|
|
1136
|
+
}).catch(error => {
|
|
1137
|
+
console.log('Autoplay failed, user interaction required:', error);
|
|
1138
|
+
// Show a manual play button
|
|
1139
|
+
const playButton = document.createElement('button');
|
|
1140
|
+
playButton.className = 'manual-play-button';
|
|
1141
|
+
playButton.textContent = '▶ Click to Play Audio';
|
|
1142
|
+
playButton.style.cssText = 'position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 10; padding: 10px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;';
|
|
1143
|
+
|
|
1144
|
+
playButton.addEventListener('click', async () => {
|
|
1145
|
+
try {
|
|
1146
|
+
await video.play();
|
|
1147
|
+
playButton.style.display = 'none';
|
|
1148
|
+
console.log('Manual play successful');
|
|
1149
|
+
} catch (e) {
|
|
1150
|
+
console.error('Manual play failed:', e);
|
|
1421
1151
|
}
|
|
1422
|
-
|
|
1423
|
-
} catch (error) {
|
|
1424
|
-
console.error(`🎵 Failed to setup audio playback monitoring for peer ${peerIdShort}:`, error);
|
|
1425
|
-
}
|
|
1426
|
-
}
|
|
1152
|
+
});
|
|
1427
1153
|
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
*/
|
|
1431
|
-
logPeerDiagnostics() {
|
|
1432
|
-
console.log('🔍 PEER DIAGNOSTICS:');
|
|
1433
|
-
const allPeers = this.mesh.getPeers();
|
|
1434
|
-
|
|
1435
|
-
console.log(`📊 Total peers in mesh: ${allPeers.length}`);
|
|
1436
|
-
|
|
1437
|
-
allPeers.forEach((peer, index) => {
|
|
1438
|
-
const peerConnection = this.mesh.connectionManager.getPeer(peer.peerId);
|
|
1439
|
-
|
|
1440
|
-
console.log(`\n🔍 Peer ${index + 1}: ${peer.peerId.substring(0, 8)}...`);
|
|
1441
|
-
console.log(` Status: ${peer.status}`);
|
|
1442
|
-
|
|
1443
|
-
if (peerConnection) {
|
|
1444
|
-
console.log(` WebRTC Connection State: ${peerConnection.connection?.connectionState || 'none'}`);
|
|
1445
|
-
console.log(` ICE Connection State: ${peerConnection.connection?.iceConnectionState || 'none'}`);
|
|
1446
|
-
console.log(` Data Channel State: ${peerConnection.dataChannel?.readyState || 'none'}`);
|
|
1447
|
-
console.log(` Data Channel Ready: ${peerConnection.dataChannelReady}`);
|
|
1448
|
-
console.log(` Has Local Stream: ${!!peerConnection.localStream}`);
|
|
1449
|
-
console.log(` Has Remote Stream: ${!!peerConnection.remoteStream}`);
|
|
1450
|
-
|
|
1451
|
-
if (peerConnection.localStream) {
|
|
1452
|
-
const audioTracks = peerConnection.localStream.getAudioTracks();
|
|
1453
|
-
const videoTracks = peerConnection.localStream.getVideoTracks();
|
|
1454
|
-
console.log(` Local Stream Tracks: ${audioTracks.length} audio, ${videoTracks.length} video`);
|
|
1455
|
-
}
|
|
1456
|
-
|
|
1457
|
-
if (peerConnection.remoteStream) {
|
|
1458
|
-
const audioTracks = peerConnection.remoteStream.getAudioTracks();
|
|
1459
|
-
const videoTracks = peerConnection.remoteStream.getVideoTracks();
|
|
1460
|
-
console.log(` Remote Stream Tracks: ${audioTracks.length} audio, ${videoTracks.length} video`);
|
|
1461
|
-
}
|
|
1462
|
-
} else {
|
|
1463
|
-
console.log(` ❌ No PeerConnection object found`);
|
|
1464
|
-
}
|
|
1154
|
+
remoteVideoItem.style.position = 'relative';
|
|
1155
|
+
remoteVideoItem.appendChild(playButton);
|
|
1465
1156
|
});
|
|
1466
|
-
|
|
1467
|
-
console.log('\n🔍 END PEER DIAGNOSTICS\n');
|
|
1468
|
-
}
|
|
1157
|
+
});
|
|
1469
1158
|
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
this.addMessage('System', 'Please enter a signaling server URL', 'error');
|
|
1475
|
-
return;
|
|
1476
|
-
}
|
|
1159
|
+
// Add event listeners for audio debugging
|
|
1160
|
+
video.addEventListener('play', () => {
|
|
1161
|
+
console.log('Video element started playing');
|
|
1162
|
+
});
|
|
1477
1163
|
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1164
|
+
video.addEventListener('pause', () => {
|
|
1165
|
+
console.log('Video element paused');
|
|
1166
|
+
});
|
|
1167
|
+
|
|
1168
|
+
video.addEventListener('volumechange', () => {
|
|
1169
|
+
console.log('Volume changed:', video.volume, 'Muted:', video.muted);
|
|
1170
|
+
});
|
|
1171
|
+
|
|
1172
|
+
const status = document.createElement('div');
|
|
1173
|
+
status.className = 'video-status';
|
|
1174
|
+
|
|
1175
|
+
remoteVideoItem.appendChild(title);
|
|
1176
|
+
remoteVideoItem.appendChild(video);
|
|
1177
|
+
remoteVideoItem.appendChild(status);
|
|
1178
|
+
|
|
1179
|
+
remoteVideosContainer.appendChild(remoteVideoItem);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
const video = remoteVideoItem.querySelector('video');
|
|
1183
|
+
const status = remoteVideoItem.querySelector('.video-status');
|
|
1184
|
+
|
|
1185
|
+
video.srcObject = stream;
|
|
1186
|
+
|
|
1187
|
+
// EXPLICIT AUDIO DEBUGGING
|
|
1188
|
+
console.log('🎵 Setting srcObject for video element:', {
|
|
1189
|
+
stream,
|
|
1190
|
+
streamId: stream.id,
|
|
1191
|
+
streamActive: stream.active,
|
|
1192
|
+
videoElement: video,
|
|
1193
|
+
videoMuted: video.muted,
|
|
1194
|
+
videoVolume: video.volume,
|
|
1195
|
+
isOwnStream
|
|
1196
|
+
});
|
|
1197
|
+
|
|
1198
|
+
const audioTracks = stream.getAudioTracks();
|
|
1199
|
+
const videoTracks = stream.getVideoTracks();
|
|
1200
|
+
|
|
1201
|
+
console.log('🎵 Stream tracks assigned:', {
|
|
1202
|
+
audioTracks: audioTracks.map(t => ({
|
|
1203
|
+
id: t.id,
|
|
1204
|
+
enabled: t.enabled,
|
|
1205
|
+
readyState: t.readyState,
|
|
1206
|
+
muted: t.muted
|
|
1207
|
+
})),
|
|
1208
|
+
videoTracks: videoTracks.map(t => ({
|
|
1209
|
+
id: t.id,
|
|
1210
|
+
enabled: t.enabled,
|
|
1211
|
+
readyState: t.readyState,
|
|
1212
|
+
muted: t.muted
|
|
1213
|
+
}))
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
// CRITICAL: Set audio state based on whether this is our own stream
|
|
1217
|
+
if (isOwnStream) {
|
|
1218
|
+
console.warn('🚨 Muting our own stream to prevent audio feedback');
|
|
1219
|
+
video.muted = true;
|
|
1220
|
+
video.volume = 0;
|
|
1221
|
+
} else {
|
|
1222
|
+
// Only unmute if this is NOT our own stream
|
|
1223
|
+
video.muted = false;
|
|
1224
|
+
video.volume = 1.0;
|
|
1225
|
+
|
|
1226
|
+
// Setup audio playback monitoring for remote streams
|
|
1227
|
+
this.setupAudioPlaybackMonitoring(video, peerId, audioTracks);
|
|
1486
1228
|
}
|
|
1487
1229
|
|
|
1488
|
-
|
|
1230
|
+
// Update status based on stream tracks
|
|
1231
|
+
console.log(`Video tracks: ${videoTracks.length}, Audio tracks: ${audioTracks.length}`);
|
|
1232
|
+
audioTracks.forEach((track, i) => {
|
|
1233
|
+
console.log(`Audio track ${i}:`, { enabled: track.enabled, muted: track.muted, label: track.label });
|
|
1234
|
+
});
|
|
1235
|
+
|
|
1236
|
+
// For audio-only streams, ensure the video element is properly configured
|
|
1237
|
+
if (audioTracks.length > 0 && videoTracks.length === 0) {
|
|
1238
|
+
console.log('Audio-only stream detected - configuring for audio playback');
|
|
1239
|
+
video.style.height = '60px'; // Smaller height for audio-only
|
|
1240
|
+
video.style.backgroundColor = '#f0f0f0';
|
|
1241
|
+
|
|
1242
|
+
// Force audio to play by trying to resume audio context
|
|
1243
|
+
if (window.AudioContext || window.webkitAudioContext) {
|
|
1244
|
+
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
1489
1245
|
try {
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1246
|
+
const audioContext = new AudioContext();
|
|
1247
|
+
if (audioContext.state === 'suspended') {
|
|
1248
|
+
audioContext.resume().then(() => {
|
|
1249
|
+
console.log('Audio context resumed for remote stream');
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
// Test audio by creating a brief analysis
|
|
1254
|
+
try {
|
|
1255
|
+
const source = audioContext.createMediaStreamSource(stream);
|
|
1256
|
+
const analyser = audioContext.createAnalyser();
|
|
1257
|
+
source.connect(analyser);
|
|
1258
|
+
|
|
1259
|
+
const dataArray = new Uint8Array(analyser.frequencyBinCount);
|
|
1260
|
+
const checkAudio = () => {
|
|
1261
|
+
analyser.getByteFrequencyData(dataArray);
|
|
1262
|
+
const sum = dataArray.reduce((a, b) => a + b, 0);
|
|
1263
|
+
if (sum > 0) {
|
|
1264
|
+
console.log('Remote audio data detected! Sum:', sum);
|
|
1265
|
+
}
|
|
1266
|
+
};
|
|
1267
|
+
|
|
1268
|
+
// Check audio data periodically
|
|
1269
|
+
const audioChecker = setInterval(checkAudio, 1000);
|
|
1270
|
+
setTimeout(() => clearInterval(audioChecker), 10000); // Stop after 10 seconds
|
|
1271
|
+
} catch (audioAnalysisError) {
|
|
1272
|
+
console.log('Could not analyze remote audio:', audioAnalysisError);
|
|
1273
|
+
}
|
|
1274
|
+
} catch (e) {
|
|
1275
|
+
console.log('Could not create/resume audio context:', e);
|
|
1497
1276
|
}
|
|
1277
|
+
}
|
|
1498
1278
|
}
|
|
1499
1279
|
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
if (timeSinceLastCleanup < 5000) {
|
|
1505
|
-
const remaining = Math.ceil((5000 - timeSinceLastCleanup) / 1000);
|
|
1506
|
-
this.addMessage('System', `Cleanup on cooldown (${remaining}s remaining)`, 'warning');
|
|
1507
|
-
return;
|
|
1508
|
-
}
|
|
1280
|
+
const statusText = [];
|
|
1281
|
+
if (videoTracks.length > 0) statusText.push('Video');
|
|
1282
|
+
if (audioTracks.length > 0) statusText.push('Audio');
|
|
1509
1283
|
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1284
|
+
status.textContent = statusText.length > 0 ? statusText.join(' + ') : 'Audio only';
|
|
1285
|
+
status.style.color = '#28a745';
|
|
1286
|
+
|
|
1287
|
+
this.addMessage('System', `Receiving ${statusText.join(' + ')} from ${peerId.substring(0, 8)}...`);
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
/**
|
|
1291
|
+
* Setup audio playback monitoring for video elements playing remote streams
|
|
1292
|
+
*/
|
|
1293
|
+
setupAudioPlaybackMonitoring(videoElement, peerId, audioTracks) {
|
|
1294
|
+
const peerIdShort = peerId.substring(0, 8);
|
|
1295
|
+
console.log(`🎵 Setting up audio playback monitoring for peer ${peerIdShort}`);
|
|
1296
|
+
|
|
1297
|
+
if (audioTracks.length === 0) {
|
|
1298
|
+
console.log(`🎵 No audio tracks to monitor for peer ${peerIdShort}`);
|
|
1299
|
+
return;
|
|
1518
1300
|
}
|
|
1519
1301
|
|
|
1520
|
-
|
|
1302
|
+
try {
|
|
1303
|
+
// Monitor video element audio events
|
|
1304
|
+
videoElement.addEventListener('play', () => {
|
|
1305
|
+
console.log(`🎵 Video element started playing for peer ${peerIdShort}`, {
|
|
1306
|
+
muted: videoElement.muted,
|
|
1307
|
+
volume: videoElement.volume,
|
|
1308
|
+
audioTracks: audioTracks.length
|
|
1309
|
+
});
|
|
1310
|
+
});
|
|
1311
|
+
|
|
1312
|
+
videoElement.addEventListener('pause', () => {
|
|
1313
|
+
console.log(`🎵 Video element paused for peer ${peerIdShort}`);
|
|
1314
|
+
});
|
|
1315
|
+
|
|
1316
|
+
videoElement.addEventListener('volumechange', () => {
|
|
1317
|
+
console.log(`🎵 Volume changed for peer ${peerIdShort}:`, {
|
|
1318
|
+
volume: videoElement.volume,
|
|
1319
|
+
muted: videoElement.muted
|
|
1320
|
+
});
|
|
1321
|
+
});
|
|
1322
|
+
|
|
1323
|
+
// Create audio context for monitoring actual audio data playback
|
|
1324
|
+
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
1325
|
+
if (!AudioContext) {
|
|
1326
|
+
console.warn('🎵 AudioContext not available - basic monitoring only');
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
// Wait for video to load before setting up audio analysis
|
|
1331
|
+
const setupAudioAnalysis = () => {
|
|
1521
1332
|
try {
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1333
|
+
const audioContext = new AudioContext();
|
|
1334
|
+
const source = audioContext.createMediaElementSource(videoElement);
|
|
1335
|
+
const analyser = audioContext.createAnalyser();
|
|
1336
|
+
const gainNode = audioContext.createGain();
|
|
1337
|
+
|
|
1338
|
+
analyser.fftSize = 256;
|
|
1339
|
+
const bufferLength = analyser.frequencyBinCount;
|
|
1340
|
+
const dataArray = new Uint8Array(bufferLength);
|
|
1341
|
+
|
|
1342
|
+
// Connect: source -> analyser -> gain -> destination
|
|
1343
|
+
source.connect(analyser);
|
|
1344
|
+
analyser.connect(gainNode);
|
|
1345
|
+
gainNode.connect(audioContext.destination);
|
|
1346
|
+
|
|
1347
|
+
let lastLogTime = 0;
|
|
1348
|
+
let totalSamples = 0;
|
|
1349
|
+
let playbackSamples = 0;
|
|
1350
|
+
let maxPlaybackLevel = 0;
|
|
1351
|
+
|
|
1352
|
+
const monitorPlayback = () => {
|
|
1353
|
+
if (videoElement.paused || videoElement.ended) {
|
|
1354
|
+
return; // Stop monitoring if playback stopped
|
|
1527
1355
|
}
|
|
1528
|
-
} catch (error) {
|
|
1529
|
-
console.error('Health check failed:', error);
|
|
1530
|
-
this.addMessage('System', `Health check error: ${error.message}`, 'error');
|
|
1531
|
-
}
|
|
1532
|
-
}
|
|
1533
1356
|
|
|
1534
|
-
|
|
1535
|
-
const messageInput = document.getElementById('message-input');
|
|
1536
|
-
const dmTargetInput = document.getElementById('dm-target-input');
|
|
1537
|
-
const message = messageInput.value.trim();
|
|
1538
|
-
const dmTarget = dmTargetInput.value.trim();
|
|
1357
|
+
analyser.getByteFrequencyData(dataArray);
|
|
1539
1358
|
|
|
1540
|
-
|
|
1359
|
+
// Calculate playback audio level
|
|
1360
|
+
const average = dataArray.reduce((sum, value) => sum + value, 0) / bufferLength;
|
|
1361
|
+
const currentTime = Date.now();
|
|
1541
1362
|
|
|
1542
|
-
|
|
1543
|
-
if (
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1363
|
+
totalSamples++;
|
|
1364
|
+
if (average > 3) { // Lower threshold for playback detection
|
|
1365
|
+
playbackSamples++;
|
|
1366
|
+
maxPlaybackLevel = Math.max(maxPlaybackLevel, average);
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
// Log every 3 seconds
|
|
1370
|
+
if (currentTime - lastLogTime > 3000) {
|
|
1371
|
+
const playbackActivity = totalSamples > 0 ? (playbackSamples / totalSamples * 100) : 0;
|
|
1372
|
+
console.log(`🔊 Audio PLAYBACK from peer ${peerIdShort}:`, {
|
|
1373
|
+
videoElementMuted: videoElement.muted,
|
|
1374
|
+
videoElementVolume: videoElement.volume,
|
|
1375
|
+
currentPlaybackLevel: Math.round(average),
|
|
1376
|
+
maxPlaybackLevel: Math.round(maxPlaybackLevel),
|
|
1377
|
+
playbackActivityPercent: Math.round(playbackActivity),
|
|
1378
|
+
samplesAnalyzed: totalSamples,
|
|
1379
|
+
audioBeingPlayed: playbackSamples > 0,
|
|
1380
|
+
audioContextState: audioContext.state
|
|
1381
|
+
});
|
|
1382
|
+
|
|
1383
|
+
lastLogTime = currentTime;
|
|
1384
|
+
totalSamples = 0;
|
|
1385
|
+
playbackSamples = 0;
|
|
1386
|
+
maxPlaybackLevel = 0;
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
// Continue monitoring
|
|
1390
|
+
if (!videoElement.paused && !videoElement.ended) {
|
|
1391
|
+
requestAnimationFrame(monitorPlayback);
|
|
1392
|
+
}
|
|
1393
|
+
};
|
|
1394
|
+
|
|
1395
|
+
// Start monitoring when video plays
|
|
1396
|
+
videoElement.addEventListener('play', () => {
|
|
1397
|
+
console.log(`🔊 Starting audio playback monitoring for peer ${peerIdShort}`);
|
|
1398
|
+
if (audioContext.state === 'suspended') {
|
|
1399
|
+
audioContext.resume().then(() => {
|
|
1400
|
+
requestAnimationFrame(monitorPlayback);
|
|
1401
|
+
});
|
|
1557
1402
|
} else {
|
|
1558
|
-
|
|
1559
|
-
// this.mesh.broadcastMessage(message);
|
|
1560
|
-
this.mesh.sendMessage(message);
|
|
1561
|
-
this.addMessage('You', message, 'own');
|
|
1403
|
+
requestAnimationFrame(monitorPlayback);
|
|
1562
1404
|
}
|
|
1405
|
+
});
|
|
1563
1406
|
|
|
1564
|
-
|
|
1407
|
+
console.log(`🔊 Audio playback analysis setup complete for peer ${peerIdShort}`);
|
|
1565
1408
|
} catch (error) {
|
|
1566
|
-
|
|
1567
|
-
this.addMessage('System', `Failed to send message: ${error.message}`, 'error');
|
|
1409
|
+
console.error(`🔊 Failed to setup audio playback analysis for peer ${peerIdShort}:`, error);
|
|
1568
1410
|
}
|
|
1411
|
+
};
|
|
1412
|
+
|
|
1413
|
+
if (videoElement.readyState >= 1) {
|
|
1414
|
+
// Video metadata already loaded
|
|
1415
|
+
setupAudioAnalysis();
|
|
1416
|
+
} else {
|
|
1417
|
+
// Wait for metadata to load
|
|
1418
|
+
videoElement.addEventListener('loadedmetadata', setupAudioAnalysis, { once: true });
|
|
1419
|
+
}
|
|
1420
|
+
} catch (error) {
|
|
1421
|
+
console.error(`🎵 Failed to setup audio playback monitoring for peer ${peerIdShort}:`, error);
|
|
1569
1422
|
}
|
|
1423
|
+
}
|
|
1570
1424
|
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1425
|
+
/**
|
|
1426
|
+
* Diagnostic method to log complete peer connection state
|
|
1427
|
+
*/
|
|
1428
|
+
logPeerDiagnostics() {
|
|
1429
|
+
console.log('🔍 PEER DIAGNOSTICS:');
|
|
1430
|
+
const allPeers = this.mesh.getPeers();
|
|
1431
|
+
|
|
1432
|
+
console.log(`📊 Total peers in mesh: ${allPeers.length}`);
|
|
1433
|
+
|
|
1434
|
+
allPeers.forEach((peer, index) => {
|
|
1435
|
+
const peerConnection = this.mesh.connectionManager.getPeer(peer.peerId);
|
|
1436
|
+
|
|
1437
|
+
console.log(`\n🔍 Peer ${index + 1}: ${peer.peerId.substring(0, 8)}...`);
|
|
1438
|
+
console.log(` Status: ${peer.status}`);
|
|
1439
|
+
|
|
1440
|
+
if (peerConnection) {
|
|
1441
|
+
console.log(` WebRTC Connection State: ${peerConnection.connection?.connectionState || 'none'}`);
|
|
1442
|
+
console.log(` ICE Connection State: ${peerConnection.connection?.iceConnectionState || 'none'}`);
|
|
1443
|
+
console.log(` Data Channel State: ${peerConnection.dataChannel?.readyState || 'none'}`);
|
|
1444
|
+
console.log(` Data Channel Ready: ${peerConnection.dataChannelReady}`);
|
|
1445
|
+
console.log(` Has Local Stream: ${!!peerConnection.localStream}`);
|
|
1446
|
+
console.log(` Has Remote Stream: ${!!peerConnection.remoteStream}`);
|
|
1447
|
+
|
|
1448
|
+
if (peerConnection.localStream) {
|
|
1449
|
+
const audioTracks = peerConnection.localStream.getAudioTracks();
|
|
1450
|
+
const videoTracks = peerConnection.localStream.getVideoTracks();
|
|
1451
|
+
console.log(` Local Stream Tracks: ${audioTracks.length} audio, ${videoTracks.length} video`);
|
|
1578
1452
|
}
|
|
1579
1453
|
|
|
1580
|
-
if (
|
|
1581
|
-
|
|
1582
|
-
|
|
1454
|
+
if (peerConnection.remoteStream) {
|
|
1455
|
+
const audioTracks = peerConnection.remoteStream.getAudioTracks();
|
|
1456
|
+
const videoTracks = peerConnection.remoteStream.getVideoTracks();
|
|
1457
|
+
console.log(` Remote Stream Tracks: ${audioTracks.length} audio, ${videoTracks.length} video`);
|
|
1583
1458
|
}
|
|
1459
|
+
} else {
|
|
1460
|
+
console.log(' ❌ No PeerConnection object found');
|
|
1461
|
+
}
|
|
1462
|
+
});
|
|
1463
|
+
|
|
1464
|
+
console.log('\n🔍 END PEER DIAGNOSTICS\n');
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
// Existing UI methods (keeping the rest of the original functionality)
|
|
1468
|
+
async handleConnect() {
|
|
1469
|
+
const url = document.getElementById('signaling-url').value.trim();
|
|
1470
|
+
if (!url) {
|
|
1471
|
+
this.addMessage('System', 'Please enter a signaling server URL', 'error');
|
|
1472
|
+
return;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
try {
|
|
1476
|
+
this.updateButtonStates({ connecting: true });
|
|
1477
|
+
await this.mesh.connect(url);
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
console.error('Connection failed:', error);
|
|
1480
|
+
this.addMessage('System', `Connection failed: ${error.message}`, 'error');
|
|
1481
|
+
this.updateButtonStates({ connecting: false });
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
async handleDisconnect() {
|
|
1486
|
+
try {
|
|
1487
|
+
this.updateButtonStates({ disconnecting: true });
|
|
1488
|
+
await this.mesh.disconnect();
|
|
1489
|
+
} catch (error) {
|
|
1490
|
+
console.error('Disconnection failed:', error);
|
|
1491
|
+
this.addMessage('System', `Disconnection failed: ${error.message}`, 'error');
|
|
1492
|
+
} finally {
|
|
1493
|
+
this.updateButtonStates({ disconnecting: false });
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1584
1496
|
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1497
|
+
async handleCleanup() {
|
|
1498
|
+
const now = Date.now();
|
|
1499
|
+
const timeSinceLastCleanup = now - this.lastCleanupTime;
|
|
1500
|
+
|
|
1501
|
+
if (timeSinceLastCleanup < 5000) {
|
|
1502
|
+
const remaining = Math.ceil((5000 - timeSinceLastCleanup) / 1000);
|
|
1503
|
+
this.addMessage('System', `Cleanup on cooldown (${remaining}s remaining)`, 'warning');
|
|
1504
|
+
return;
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
try {
|
|
1508
|
+
this.lastCleanupTime = now;
|
|
1509
|
+
await this.mesh.cleanupSignalingData();
|
|
1510
|
+
this.addMessage('System', 'Signaling data cleanup initiated');
|
|
1511
|
+
} catch (error) {
|
|
1512
|
+
console.error('Cleanup failed:', error);
|
|
1513
|
+
this.addMessage('System', `Cleanup failed: ${error.message}`, 'error');
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
async handleHealthCheck() {
|
|
1518
|
+
try {
|
|
1519
|
+
const result = await this.mesh.performHealthCheck();
|
|
1520
|
+
if (result.healthy) {
|
|
1521
|
+
this.addMessage('System', 'Health check passed - all systems nominal', 'info');
|
|
1522
|
+
} else {
|
|
1523
|
+
this.addMessage('System', `Health check failed: ${result.issues.join(', ')}`, 'warning');
|
|
1524
|
+
}
|
|
1525
|
+
} catch (error) {
|
|
1526
|
+
console.error('Health check failed:', error);
|
|
1527
|
+
this.addMessage('System', `Health check error: ${error.message}`, 'error');
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
sendMessage() {
|
|
1532
|
+
const messageInput = document.getElementById('message-input');
|
|
1533
|
+
const dmTargetInput = document.getElementById('dm-target-input');
|
|
1534
|
+
const message = messageInput.value.trim();
|
|
1535
|
+
const dmTarget = dmTargetInput.value.trim();
|
|
1536
|
+
|
|
1537
|
+
if (!message) return;
|
|
1538
|
+
|
|
1539
|
+
try {
|
|
1540
|
+
if (dmTarget) {
|
|
1541
|
+
// Validate peer ID format
|
|
1542
|
+
if (!window.PeerPigeonMesh.validatePeerId(dmTarget)) {
|
|
1543
|
+
this.addMessage('System', 'Invalid peer ID format. Must be 40-character SHA-1 hash.', 'error');
|
|
1544
|
+
return;
|
|
1588
1545
|
}
|
|
1589
1546
|
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
}
|
|
1595
|
-
|
|
1596
|
-
this.addMessage('System', `Failed to connect: ${error.message}`, 'error');
|
|
1547
|
+
// Send direct message
|
|
1548
|
+
const success = this.mesh.sendDirectMessage(dmTarget, message);
|
|
1549
|
+
if (success) {
|
|
1550
|
+
this.addMessage('You', `(DM to ${dmTarget.substring(0, 8)}...) ${message}`, 'own');
|
|
1551
|
+
} else {
|
|
1552
|
+
this.addMessage('System', `Failed to send direct message to ${dmTarget.substring(0, 8)}...`, 'error');
|
|
1597
1553
|
}
|
|
1554
|
+
} else {
|
|
1555
|
+
// Send broadcast message
|
|
1556
|
+
// this.mesh.broadcastMessage(message);
|
|
1557
|
+
this.mesh.sendMessage(message);
|
|
1558
|
+
this.addMessage('You', message, 'own');
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
messageInput.value = '';
|
|
1562
|
+
} catch (error) {
|
|
1563
|
+
console.error('Failed to send message:', error);
|
|
1564
|
+
this.addMessage('System', `Failed to send message: ${error.message}`, 'error');
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
connectToPeer() {
|
|
1569
|
+
const targetPeerInput = document.getElementById('target-peer');
|
|
1570
|
+
const targetPeerId = targetPeerInput.value.trim();
|
|
1571
|
+
|
|
1572
|
+
if (!targetPeerId) {
|
|
1573
|
+
this.addMessage('System', 'Please enter a peer ID', 'error');
|
|
1574
|
+
return;
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
if (!window.PeerPigeonMesh.validatePeerId(targetPeerId)) {
|
|
1578
|
+
this.addMessage('System', 'Invalid peer ID format. Must be 40-character SHA-1 hash.', 'error');
|
|
1579
|
+
return;
|
|
1598
1580
|
}
|
|
1599
1581
|
|
|
1600
|
-
|
|
1582
|
+
if (targetPeerId === this.mesh.peerId) {
|
|
1583
|
+
this.addMessage('System', 'Cannot connect to yourself', 'error');
|
|
1584
|
+
return;
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
try {
|
|
1588
|
+
this.mesh.connectToPeer(targetPeerId);
|
|
1589
|
+
this.addMessage('System', `Attempting to connect to ${targetPeerId.substring(0, 8)}...`);
|
|
1590
|
+
targetPeerInput.value = '';
|
|
1591
|
+
} catch (error) {
|
|
1592
|
+
console.error('Failed to initiate connection:', error);
|
|
1593
|
+
this.addMessage('System', `Failed to connect: ${error.message}`, 'error');
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
/**
|
|
1601
1598
|
* Connect to peer from button click (for onclick handlers)
|
|
1602
1599
|
*/
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
}
|
|
1609
|
-
|
|
1610
|
-
// Disable button to prevent double-clicks
|
|
1611
|
-
button.disabled = true;
|
|
1612
|
-
button.textContent = 'Connecting...';
|
|
1613
|
-
|
|
1614
|
-
try {
|
|
1615
|
-
this.mesh.connectToPeer(peerId);
|
|
1616
|
-
this.addMessage('System', `Attempting to connect to ${peerId.substring(0, 8)}...`);
|
|
1617
|
-
|
|
1618
|
-
// Re-enable button after a delay
|
|
1619
|
-
setTimeout(() => {
|
|
1620
|
-
button.disabled = false;
|
|
1621
|
-
button.textContent = 'Connect';
|
|
1622
|
-
}, 3000);
|
|
1623
|
-
|
|
1624
|
-
} catch (error) {
|
|
1625
|
-
console.error('Failed to initiate connection:', error);
|
|
1626
|
-
this.addMessage('System', `Failed to connect: ${error.message}`, 'error');
|
|
1627
|
-
|
|
1628
|
-
// Re-enable button immediately on error
|
|
1629
|
-
button.disabled = false;
|
|
1630
|
-
button.textContent = 'Connect';
|
|
1631
|
-
}
|
|
1600
|
+
connectToPeerFromButton(button) {
|
|
1601
|
+
const peerId = button.getAttribute('data-peer-id');
|
|
1602
|
+
if (!peerId) {
|
|
1603
|
+
this.addMessage('System', 'Invalid peer ID in button', 'error');
|
|
1604
|
+
return;
|
|
1632
1605
|
}
|
|
1633
1606
|
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1607
|
+
// Disable button to prevent double-clicks
|
|
1608
|
+
button.disabled = true;
|
|
1609
|
+
button.textContent = 'Connecting...';
|
|
1610
|
+
|
|
1611
|
+
try {
|
|
1612
|
+
this.mesh.connectToPeer(peerId);
|
|
1613
|
+
this.addMessage('System', `Attempting to connect to ${peerId.substring(0, 8)}...`);
|
|
1614
|
+
|
|
1615
|
+
// Re-enable button after a delay
|
|
1616
|
+
setTimeout(() => {
|
|
1617
|
+
button.disabled = false;
|
|
1618
|
+
button.textContent = 'Connect';
|
|
1619
|
+
}, 3000);
|
|
1620
|
+
} catch (error) {
|
|
1621
|
+
console.error('Failed to initiate connection:', error);
|
|
1622
|
+
this.addMessage('System', `Failed to connect: ${error.message}`, 'error');
|
|
1623
|
+
|
|
1624
|
+
// Re-enable button immediately on error
|
|
1625
|
+
button.disabled = false;
|
|
1626
|
+
button.textContent = 'Connect';
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1640
1629
|
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
// Update peer lists
|
|
1648
|
-
this.updateConnectedPeers();
|
|
1649
|
-
this.updateDiscoveredPeers();
|
|
1650
|
-
|
|
1651
|
-
// Update settings from mesh state
|
|
1652
|
-
this.updateSettings();
|
|
1653
|
-
|
|
1654
|
-
// Update DHT status
|
|
1655
|
-
this.updateDHTStatus();
|
|
1630
|
+
updateUI() {
|
|
1631
|
+
// Update peer ID display
|
|
1632
|
+
const peerIdElement = document.getElementById('peer-id');
|
|
1633
|
+
if (peerIdElement && this.mesh.peerId) {
|
|
1634
|
+
peerIdElement.textContent = this.mesh.peerId;
|
|
1656
1635
|
}
|
|
1657
1636
|
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
if (!statusElement) return;
|
|
1637
|
+
// Update status
|
|
1638
|
+
this.updateStatus();
|
|
1661
1639
|
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
statusElement.textContent = connectionStatus.charAt(0).toUpperCase() + connectionStatus.slice(1);
|
|
1665
|
-
statusElement.className = `status ${connectionStatus.toLowerCase()}`;
|
|
1666
|
-
}
|
|
1640
|
+
// Update button states
|
|
1641
|
+
this.updateButtonStates();
|
|
1667
1642
|
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
const cleanupBtn = document.getElementById('cleanup-btn');
|
|
1672
|
-
const healthCheckBtn = document.getElementById('health-check-btn');
|
|
1643
|
+
// Update peer lists
|
|
1644
|
+
this.updateConnectedPeers();
|
|
1645
|
+
this.updateDiscoveredPeers();
|
|
1673
1646
|
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
const isDisconnecting = options.disconnecting || false;
|
|
1647
|
+
// Update settings from mesh state
|
|
1648
|
+
this.updateSettings();
|
|
1677
1649
|
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
}
|
|
1650
|
+
// Update DHT status
|
|
1651
|
+
this.updateDHTStatus();
|
|
1652
|
+
}
|
|
1682
1653
|
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
}
|
|
1654
|
+
updateStatus() {
|
|
1655
|
+
const statusElement = document.getElementById('status');
|
|
1656
|
+
if (!statusElement) return;
|
|
1687
1657
|
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1658
|
+
const status = this.mesh.getStatus();
|
|
1659
|
+
const connectionStatus = status.connected ? 'connected' : 'disconnected';
|
|
1660
|
+
statusElement.textContent = connectionStatus.charAt(0).toUpperCase() + connectionStatus.slice(1);
|
|
1661
|
+
statusElement.className = `status ${connectionStatus.toLowerCase()}`;
|
|
1662
|
+
}
|
|
1691
1663
|
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1664
|
+
updateButtonStates(options = {}) {
|
|
1665
|
+
const connectBtn = document.getElementById('connect-btn');
|
|
1666
|
+
const disconnectBtn = document.getElementById('disconnect-btn');
|
|
1667
|
+
const cleanupBtn = document.getElementById('cleanup-btn');
|
|
1668
|
+
const healthCheckBtn = document.getElementById('health-check-btn');
|
|
1669
|
+
|
|
1670
|
+
const isConnected = this.mesh.connected;
|
|
1671
|
+
const isConnecting = options.connecting || false;
|
|
1672
|
+
const isDisconnecting = options.disconnecting || false;
|
|
1673
|
+
|
|
1674
|
+
if (connectBtn) {
|
|
1675
|
+
connectBtn.disabled = isConnected || isConnecting;
|
|
1676
|
+
connectBtn.textContent = isConnecting ? 'Connecting...' : 'Connect';
|
|
1695
1677
|
}
|
|
1696
1678
|
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
if (!peersList || !connectedPeersCount) return;
|
|
1679
|
+
if (disconnectBtn) {
|
|
1680
|
+
disconnectBtn.disabled = !isConnected || isDisconnecting;
|
|
1681
|
+
disconnectBtn.textContent = isDisconnecting ? 'Disconnecting...' : 'Disconnect';
|
|
1682
|
+
}
|
|
1702
1683
|
|
|
1703
|
-
|
|
1704
|
-
|
|
1684
|
+
if (cleanupBtn) {
|
|
1685
|
+
cleanupBtn.disabled = !isConnected;
|
|
1686
|
+
}
|
|
1705
1687
|
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1688
|
+
if (healthCheckBtn) {
|
|
1689
|
+
healthCheckBtn.disabled = !isConnected;
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
updateConnectedPeers() {
|
|
1694
|
+
const peersList = document.getElementById('peers-list');
|
|
1695
|
+
const connectedPeersCount = document.getElementById('connected-peers-count');
|
|
1696
|
+
|
|
1697
|
+
if (!peersList || !connectedPeersCount) return;
|
|
1710
1698
|
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1699
|
+
const peers = this.mesh.getPeers();
|
|
1700
|
+
connectedPeersCount.textContent = peers.length;
|
|
1701
|
+
|
|
1702
|
+
if (peers.length === 0) {
|
|
1703
|
+
peersList.innerHTML = '<p class="empty-state">No peers connected</p>';
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
peersList.innerHTML = peers.map(peer => {
|
|
1708
|
+
// peer is a plain object: { peerId, status, isInitiator, connectionStartTime }
|
|
1709
|
+
const statusClass = peer.status === 'connected' ? 'connected' : 'connecting';
|
|
1710
|
+
const connectionTime = ((Date.now() - peer.connectionStartTime) / 1000).toFixed(1);
|
|
1711
|
+
|
|
1712
|
+
return `
|
|
1717
1713
|
<div class="peer-item">
|
|
1718
1714
|
<div class="peer-info">
|
|
1719
1715
|
<span class="peer-id">${peer.peerId.substring(0, 16)}...</span>
|
|
@@ -1725,36 +1721,36 @@ export class PeerPigeonUI {
|
|
|
1725
1721
|
</div>
|
|
1726
1722
|
</div>
|
|
1727
1723
|
`;
|
|
1728
|
-
|
|
1724
|
+
}).join('');
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
updateDiscoveredPeers() {
|
|
1728
|
+
const discoveredPeersList = document.getElementById('discovered-peers-list');
|
|
1729
|
+
const discoveredPeersCount = document.getElementById('discovered-peers-count');
|
|
1730
|
+
|
|
1731
|
+
if (!discoveredPeersList || !discoveredPeersCount) return;
|
|
1732
|
+
|
|
1733
|
+
const discoveredPeers = this.mesh.getDiscoveredPeers();
|
|
1734
|
+
const connectedPeerIds = this.mesh.getPeers().map(p => p.peerId);
|
|
1735
|
+
|
|
1736
|
+
// Filter out already connected peers
|
|
1737
|
+
const availablePeers = discoveredPeers.filter(peer =>
|
|
1738
|
+
!connectedPeerIds.includes(peer.peerId) && peer.peerId !== this.mesh.peerId
|
|
1739
|
+
);
|
|
1740
|
+
|
|
1741
|
+
discoveredPeersCount.textContent = availablePeers.length;
|
|
1742
|
+
|
|
1743
|
+
if (availablePeers.length === 0) {
|
|
1744
|
+
discoveredPeersList.innerHTML = '<p class="empty-state">No peers discovered</p>';
|
|
1745
|
+
return;
|
|
1729
1746
|
}
|
|
1730
1747
|
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
if (!discoveredPeersList || !discoveredPeersCount) return;
|
|
1736
|
-
|
|
1737
|
-
const discoveredPeers = this.mesh.getDiscoveredPeers();
|
|
1738
|
-
const connectedPeerIds = this.mesh.getPeers().map(p => p.peerId);
|
|
1739
|
-
|
|
1740
|
-
// Filter out already connected peers
|
|
1741
|
-
const availablePeers = discoveredPeers.filter(peer =>
|
|
1742
|
-
!connectedPeerIds.includes(peer.peerId) && peer.peerId !== this.mesh.peerId
|
|
1743
|
-
);
|
|
1744
|
-
|
|
1745
|
-
discoveredPeersCount.textContent = availablePeers.length;
|
|
1746
|
-
|
|
1747
|
-
if (availablePeers.length === 0) {
|
|
1748
|
-
discoveredPeersList.innerHTML = '<p class="empty-state">No peers discovered</p>';
|
|
1749
|
-
return;
|
|
1750
|
-
}
|
|
1748
|
+
discoveredPeersList.innerHTML = availablePeers.map(peer => {
|
|
1749
|
+
const lastSeen = peer.lastSeen
|
|
1750
|
+
? `${Math.round((Date.now() - peer.lastSeen) / 1000)}s ago`
|
|
1751
|
+
: 'Unknown';
|
|
1751
1752
|
|
|
1752
|
-
|
|
1753
|
-
const lastSeen = peer.lastSeen ?
|
|
1754
|
-
`${Math.round((Date.now() - peer.lastSeen) / 1000)}s ago` :
|
|
1755
|
-
'Unknown';
|
|
1756
|
-
|
|
1757
|
-
return `
|
|
1753
|
+
return `
|
|
1758
1754
|
<div class="peer-item discovered">
|
|
1759
1755
|
<div class="peer-info">
|
|
1760
1756
|
<span class="peer-id">${peer.peerId.substring(0, 16)}...</span>
|
|
@@ -1768,108 +1764,108 @@ export class PeerPigeonUI {
|
|
|
1768
1764
|
</div>
|
|
1769
1765
|
</div>
|
|
1770
1766
|
`;
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
}
|
|
1804
|
-
}
|
|
1767
|
+
}).join('');
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
updateSettings() {
|
|
1771
|
+
// Update form values to match mesh state
|
|
1772
|
+
const minPeersInput = document.getElementById('min-peers');
|
|
1773
|
+
const maxPeersInput = document.getElementById('max-peers');
|
|
1774
|
+
const autoDiscoveryToggle = document.getElementById('auto-discovery-toggle');
|
|
1775
|
+
const evictionStrategyToggle = document.getElementById('eviction-strategy-toggle');
|
|
1776
|
+
const xorRoutingToggle = document.getElementById('xor-routing-toggle');
|
|
1777
|
+
const webdhtToggle = document.getElementById('webdht-toggle');
|
|
1778
|
+
|
|
1779
|
+
if (minPeersInput) minPeersInput.value = this.mesh.minPeers;
|
|
1780
|
+
if (maxPeersInput) maxPeersInput.value = this.mesh.maxPeers;
|
|
1781
|
+
if (autoDiscoveryToggle) autoDiscoveryToggle.checked = this.mesh.autoDiscovery;
|
|
1782
|
+
if (evictionStrategyToggle) evictionStrategyToggle.checked = this.mesh.evictionStrategy;
|
|
1783
|
+
if (xorRoutingToggle) xorRoutingToggle.checked = this.mesh.xorRouting;
|
|
1784
|
+
|
|
1785
|
+
// Update WebDHT status (read-only)
|
|
1786
|
+
if (webdhtToggle) {
|
|
1787
|
+
const isDHTEnabled = this.mesh && this.mesh.isDHTEnabled && this.mesh.isDHTEnabled();
|
|
1788
|
+
webdhtToggle.checked = isDHTEnabled;
|
|
1789
|
+
|
|
1790
|
+
// Update the visual styling based on status
|
|
1791
|
+
const parent = webdhtToggle.closest('.auto-discovery-control');
|
|
1792
|
+
if (parent) {
|
|
1793
|
+
if (isDHTEnabled) {
|
|
1794
|
+
parent.style.opacity = '1';
|
|
1795
|
+
parent.title = 'WebDHT is enabled and running';
|
|
1796
|
+
} else {
|
|
1797
|
+
parent.style.opacity = '0.6';
|
|
1798
|
+
parent.title = 'WebDHT is disabled (restart required to enable)';
|
|
1805
1799
|
}
|
|
1800
|
+
}
|
|
1806
1801
|
}
|
|
1802
|
+
}
|
|
1807
1803
|
|
|
1808
|
-
|
|
1804
|
+
/**
|
|
1809
1805
|
* Add a message to the UI message log
|
|
1810
1806
|
* @param {string} sender - Message sender identifier
|
|
1811
1807
|
* @param {string} content - Message content
|
|
1812
1808
|
* @param {string} type - Message type ('info', 'error', 'warning', 'own', 'dm')
|
|
1813
1809
|
*/
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
}
|
|
1810
|
+
addMessage(sender, content, type = 'info') {
|
|
1811
|
+
// System messages go to the system-messages container (below connection buttons)
|
|
1812
|
+
// Chat messages go to the messages-log container (at the bottom)
|
|
1813
|
+
if (sender === 'System') {
|
|
1814
|
+
this.addSystemMessage(content, type);
|
|
1815
|
+
} else {
|
|
1816
|
+
this.addChatMessage(sender, content, type);
|
|
1822
1817
|
}
|
|
1818
|
+
}
|
|
1823
1819
|
|
|
1824
|
-
|
|
1820
|
+
/**
|
|
1825
1821
|
* Add a system message to the system messages container below connection buttons
|
|
1826
1822
|
* @param {string} content - Message content
|
|
1827
1823
|
* @param {string} type - Message type ('info', 'error', 'warning')
|
|
1828
1824
|
*/
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1825
|
+
addSystemMessage(content, type = 'info') {
|
|
1826
|
+
const systemMessages = document.getElementById('system-messages');
|
|
1827
|
+
if (!systemMessages) return;
|
|
1828
|
+
|
|
1829
|
+
const messageElement = document.createElement('div');
|
|
1830
|
+
messageElement.className = `system-message ${type}`;
|
|
1831
|
+
|
|
1832
|
+
const timestamp = new Date().toLocaleTimeString();
|
|
1833
|
+
|
|
1834
|
+
messageElement.innerHTML = `
|
|
1839
1835
|
<div class="system-message-header">
|
|
1840
1836
|
<span class="system-message-time">${timestamp}</span>
|
|
1841
1837
|
<span class="system-message-type">${type.toUpperCase()}</span>
|
|
1842
1838
|
</div>
|
|
1843
1839
|
<div class="system-message-content">${content}</div>
|
|
1844
1840
|
`;
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
}
|
|
1841
|
+
|
|
1842
|
+
systemMessages.appendChild(messageElement);
|
|
1843
|
+
systemMessages.scrollTop = systemMessages.scrollHeight;
|
|
1844
|
+
|
|
1845
|
+
// Limit message history to prevent memory issues
|
|
1846
|
+
const messages = systemMessages.children;
|
|
1847
|
+
if (messages.length > 50) {
|
|
1848
|
+
systemMessages.removeChild(messages[0]);
|
|
1854
1849
|
}
|
|
1850
|
+
}
|
|
1855
1851
|
|
|
1856
|
-
|
|
1852
|
+
/**
|
|
1857
1853
|
* Add a chat message to the messages log at the bottom
|
|
1858
1854
|
* @param {string} sender - Message sender identifier
|
|
1859
1855
|
* @param {string} content - Message content
|
|
1860
1856
|
* @param {string} type - Message type ('own', 'dm')
|
|
1861
1857
|
*/
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1858
|
+
addChatMessage(sender, content, type = 'info') {
|
|
1859
|
+
const messagesLog = document.getElementById('messages-log');
|
|
1860
|
+
if (!messagesLog) return;
|
|
1861
|
+
|
|
1862
|
+
const messageElement = document.createElement('div');
|
|
1863
|
+
messageElement.className = `message-item ${type}`;
|
|
1864
|
+
|
|
1865
|
+
const timestamp = new Date().toLocaleTimeString();
|
|
1866
|
+
const senderDisplay = sender === 'You' ? 'You' : sender.substring(0, 3);
|
|
1867
|
+
|
|
1868
|
+
messageElement.innerHTML = `
|
|
1873
1869
|
<div class="avatar">${senderDisplay}</div>
|
|
1874
1870
|
<div class="message-body">
|
|
1875
1871
|
<div class="message-header">
|
|
@@ -1878,53 +1874,53 @@ export class PeerPigeonUI {
|
|
|
1878
1874
|
<div class="message-content">${content}</div>
|
|
1879
1875
|
</div>
|
|
1880
1876
|
`;
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
}
|
|
1877
|
+
|
|
1878
|
+
messagesLog.appendChild(messageElement);
|
|
1879
|
+
messagesLog.scrollTop = messagesLog.scrollHeight;
|
|
1880
|
+
|
|
1881
|
+
// Limit message history to prevent memory issues
|
|
1882
|
+
const messages = messagesLog.children;
|
|
1883
|
+
if (messages.length > 100) {
|
|
1884
|
+
messagesLog.removeChild(messages[0]);
|
|
1890
1885
|
}
|
|
1886
|
+
}
|
|
1891
1887
|
|
|
1892
|
-
|
|
1888
|
+
/**
|
|
1893
1889
|
* Add an entry to the DHT log
|
|
1894
1890
|
* @param {string} message - The log message
|
|
1895
1891
|
*/
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1892
|
+
addDHTLogEntry(message) {
|
|
1893
|
+
const dhtLog = document.getElementById('dht-log');
|
|
1894
|
+
if (!dhtLog) return;
|
|
1895
|
+
|
|
1896
|
+
const logEntry = document.createElement('div');
|
|
1897
|
+
logEntry.className = 'dht-log-entry';
|
|
1898
|
+
|
|
1899
|
+
const timestamp = new Date().toLocaleTimeString();
|
|
1900
|
+
logEntry.innerHTML = `
|
|
1905
1901
|
<span class="dht-log-time">${timestamp}</span>
|
|
1906
1902
|
<span class="dht-log-message">${message}</span>
|
|
1907
1903
|
`;
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
}
|
|
1904
|
+
|
|
1905
|
+
dhtLog.appendChild(logEntry);
|
|
1906
|
+
dhtLog.scrollTop = dhtLog.scrollHeight;
|
|
1907
|
+
|
|
1908
|
+
// Limit log entries to prevent memory issues
|
|
1909
|
+
const entries = dhtLog.children;
|
|
1910
|
+
if (entries.length > 100) {
|
|
1911
|
+
dhtLog.removeChild(entries[0]);
|
|
1917
1912
|
}
|
|
1913
|
+
}
|
|
1918
1914
|
|
|
1919
|
-
|
|
1915
|
+
/**
|
|
1920
1916
|
* Update the DHT status display
|
|
1921
1917
|
*/
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
}
|
|
1918
|
+
updateDHTStatus() {
|
|
1919
|
+
const dhtStatusElement = document.getElementById('dht-status');
|
|
1920
|
+
if (!dhtStatusElement) return;
|
|
1921
|
+
|
|
1922
|
+
const isDHTEnabled = this.mesh && this.mesh.isDHTEnabled && this.mesh.isDHTEnabled();
|
|
1923
|
+
dhtStatusElement.textContent = isDHTEnabled ? 'Enabled' : 'Disabled';
|
|
1924
|
+
dhtStatusElement.className = `status ${isDHTEnabled ? 'connected' : 'disconnected'}`;
|
|
1925
|
+
}
|
|
1926
|
+
}
|