-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclient.html
More file actions
403 lines (342 loc) · 12.6 KB
/
client.html
File metadata and controls
403 lines (342 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sidekick AI Character - WebRTC Video Chat</title>
<link href="https://fonts.googleapis.com/css2?family=Geist:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Geist', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: #171717;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
max-width: 530px;
width: 100%;
padding: 25px;
display: flex;
flex-direction: column;
align-items: center;
position: relative;
}
.header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
h1 {
color: #ffffff;
margin: 0;
text-align: center;
font-size: 1.6em;
font-weight: 400;
}
.status-indicator {
width: 12px;
height: 12px;
border-radius: 50%;
transition: all 0.3s ease;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.status-indicator.disconnected {
background-color: #ff7675;
}
.status-indicator.connecting {
background-color: #fdcb6e;
animation: blink 1s infinite;
}
.status-indicator.connected {
background-color: #55efc4;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.controls {
display: flex;
flex-direction: row;
gap: 10px;
margin-top: 0;
justify-content: center;
width: 100%;
max-width: 450px;
}
button {
border: none;
cursor: pointer;
transition: all 0.3s ease;
}
#connectBtn {
background: #4F69DE;
color: white;
padding: 12px 30px;
border-radius: 8px;
font-size: 16px;
font-weight: 400;
letter-spacing: 0.5px;
width: 180px;
}
#connectBtn:hover:not(:disabled) {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
#connectBtn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
#muteBtn {
position: absolute;
bottom: 15px;
right: 15px;
width: 40px;
height: 40px;
border-radius: 50%;
background: rgba(0, 0, 0, 0.5);
color: white;
display: none;
align-items: center;
justify-content: center;
font-size: 18px;
padding: 0;
}
#muteBtn.visible {
display: flex;
}
#muteBtn.muted {
background: rgba(255, 118, 117, 0.8);
}
#muteBtn:hover {
background: rgba(0, 0, 0, 0.7);
}
#muteBtn.muted:hover {
background: rgba(255, 118, 117, 1);
}
#videoContainer {
display: none;
width: 450px;
height: 800px;
border-radius: 14px;
overflow: hidden;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
background: #4F69DE;
padding: 3px;
position: relative;
margin-top: 8px;
margin-bottom: 20px;
}
#videoContainer.active {
display: block;
}
#remoteVideo {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
border-radius: 11px;
background: #212121;
}
.video-label {
position: absolute;
top: 10px;
right: 10px;
background: rgba(102, 126, 234, 0.9);
color: white;
padding: 5px 10px;
border-radius: 5px;
font-size: 12px;
font-weight: 600;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<div id="statusIndicator" class="status-indicator disconnected"></div>
<h1>Talk to Sidekick</h1>
</div>
<div id="videoContainer" class="active">
<video id="remoteVideo" autoplay playsinline></video>
<button id="muteBtn" onclick="toggleMute()" title="Toggle Microphone">
<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
<path class="mic-on" d="M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3z"/>
<path class="mic-on" d="M17 11c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z"/>
<path class="mic-off" style="display: none;" d="M19 11c0 1.19-.34 2.3-.9 3.28l-1.23-1.23c.27-.62.43-1.31.43-2.05H19zm-4 .16L9 5.18V5c0-1.66 1.34-3 3-3s3 1.34 3 3v6.16zM4.27 3L21 19.73 19.73 21l-4.19-4.19c-.77.46-1.63.77-2.54.91V21h-2v-3.08c-3.02-.46-5.35-2.87-5.78-5.92H7c.45 2.11 2.2 3.69 4.31 3.93l-1.84-1.84C8.65 13.46 8 12.48 8 11.35v-.15L3 6.27 4.27 3z"/>
</svg>
</button>
</div>
<div class="controls">
<button id="connectBtn" onclick="toggleConnection()">
Connect
</button>
</div>
</div>
<script>
let ws = null;
let pc = null;
let localStream = null;
let isConnected = false;
let isMuted = false;
const WS_URL = 'ws://127.0.0.1:8080';
const statusIndicator = document.getElementById('statusIndicator');
const connectBtn = document.getElementById('connectBtn');
const muteBtn = document.getElementById('muteBtn');
const remoteVideo = document.getElementById('remoteVideo');
const videoContainer = document.getElementById('videoContainer');
function updateStatus(status, className) {
statusIndicator.className = `status-indicator ${className}`;
statusIndicator.title = status;
}
async function connect() {
try {
updateStatus('Connecting...', 'connecting');
// Connect to WebSocket
ws = new WebSocket(WS_URL);
ws.onopen = async () => {
console.log('WebSocket connected');
updateStatus('Initializing WebRTC...', 'connecting');
// Get user media
localStream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true
},
video: false
});
// Create peer connection
pc = new RTCPeerConnection({
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' }
]
});
// Add local stream
localStream.getTracks().forEach(track => {
pc.addTrack(track, localStream);
});
// Add video transceiver to receive video from server
pc.addTransceiver('video', { direction: 'recvonly' });
console.log('Added video transceiver for receiving video from server');
// Handle remote stream
pc.ontrack = (event) => {
console.log('Received remote track:', event.track.kind);
// For video + audio stream
if (event.streams && event.streams[0]) {
if (!remoteVideo.srcObject) {
remoteVideo.srcObject = event.streams[0];
console.log('Set remote stream to video element');
}
}
if (event.track.kind === 'video') {
videoContainer.style.display = 'block';
console.log('Video track received - showing video container');
} else if (event.track.kind === 'audio') {
console.log('Audio track received');
}
};
// Create offer
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
// Send offer
ws.send(JSON.stringify({
type: offer.type,
sdp: offer.sdp
}));
};
ws.onmessage = async (event) => {
const data = JSON.parse(event.data);
if (data.type === 'answer') {
// Set remote description
await pc.setRemoteDescription(new RTCSessionDescription({
type: data.type,
sdp: data.sdp
}));
isConnected = true;
updateStatus('Connected', 'connected');
connectBtn.textContent = 'Disconnect';
muteBtn.classList.add('visible');
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
updateStatus('Connection error', 'disconnected');
};
ws.onclose = () => {
console.log('WebSocket disconnected');
disconnect();
};
} catch (error) {
console.error('Connection error:', error);
updateStatus('Failed to connect', 'disconnected');
disconnect();
}
}
function disconnect() {
isConnected = false;
if (localStream) {
localStream.getTracks().forEach(track => track.stop());
localStream = null;
}
if (pc) {
pc.close();
pc = null;
}
if (ws) {
ws.close();
ws = null;
}
updateStatus('Disconnected', 'disconnected');
connectBtn.textContent = 'Connect';
muteBtn.classList.remove('visible');
// Keep video container visible but clear the video stream
remoteVideo.srcObject = null;
}
function toggleConnection() {
if (isConnected) {
disconnect();
} else {
connect();
}
}
function toggleMute() {
if (!localStream) return;
const audioTrack = localStream.getAudioTracks()[0];
if (audioTrack) {
isMuted = !isMuted;
audioTrack.enabled = !isMuted;
const micOn = muteBtn.querySelectorAll('.mic-on');
const micOff = muteBtn.querySelectorAll('.mic-off');
if (isMuted) {
micOn.forEach(el => el.style.display = 'none');
micOff.forEach(el => el.style.display = 'block');
muteBtn.title = 'Unmute Microphone';
} else {
micOn.forEach(el => el.style.display = 'block');
micOff.forEach(el => el.style.display = 'none');
muteBtn.title = 'Mute Microphone';
}
muteBtn.classList.toggle('muted', isMuted);
}
}
// Request microphone permission on page load
window.addEventListener('load', async () => {
try {
await navigator.mediaDevices.getUserMedia({ audio: true });
console.log('Microphone permission granted');
} catch (error) {
console.log('Microphone permission denied or not available');
}
});
</script>
</body>
</html>