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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebRTC vs WebSocket Benchmark</title>
    <script src="https://cdn.jsdelivr.net/npm/alawmulaw"></script>
    <style>
        body {
            font-family: system-ui, -apple-system, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            max-width: 1400px;
            margin: 0 auto;
        }

        .panel {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .chat-container {
            height: 400px;
            overflow-y: auto;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
        }

        .message {
            margin-bottom: 10px;
            padding: 8px 12px;
            border-radius: 8px;
            max-width: 80%;
        }

        .message.user {
            background-color: #e3f2fd;
            margin-left: auto;
        }

        .message.assistant {
            background-color: #f5f5f5;
        }

        .metrics {
            margin-top: 15px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 8px;
        }

        .metric {
            margin: 5px 0;
            font-size: 14px;
        }

        button {
            background-color: #1976d2;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.2s;
        }

        button:hover {
            background-color: #1565c0;
        }

        button:disabled {
            background-color: #bdbdbd;
            cursor: not-allowed;
        }

        h2 {
            margin-top: 0;
            color: #1976d2;
        }

        .visualizer {
            width: 100%;
            height: 100px;
            margin: 10px 0;
            background: #fafafa;
            border-radius: 8px;
        }

        /* Add styles for disclaimer */
        .disclaimer {
            background-color: #fff3e0;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
            line-height: 1.5;
            max-width: 1400px;
            margin: 0 auto 20px auto;
        }

        /* Update nav bar styles */
        .nav-bar {
            background-color: #f5f5f5;
            padding: 10px 20px;
            margin-bottom: 20px;
        }

        .nav-container {
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            gap: 10px;
        }

        .nav-button {
            background-color: #1976d2;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            font-size: 14px;
            transition: background-color 0.2s;
        }

        .nav-button:hover {
            background-color: #1565c0;
        }

        /* Add styles for toast notifications */
        .toast {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            padding: 16px 24px;
            border-radius: 4px;
            font-size: 14px;
            z-index: 1000;
            display: none;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        }

        .toast.error {
            background-color: #f44336;
            color: white;
        }

        .toast.warning {
            background-color: #ffd700;
            color: black;
        }
    </style>
</head>

<body>
    <nav class="nav-bar">
        <div class="nav-container">
            <a href="./webrtc/docs" class="nav-button">WebRTC Docs</a>
            <a href="./websocket/docs" class="nav-button">WebSocket Docs</a>
            <a href="./telephone/docs" class="nav-button">Telephone Docs</a>
            <a href="./ui" class="nav-button">UI</a>
        </div>
    </nav>

    <div class="disclaimer">
        This page compares the WebRTC Round-Trip-Time calculated from <code>getStats()</code> to the time taken to
        process a ping/pong response pattern over websockets. It may not be a gold standard benchmark. Both WebRTC and
        Websockets have their merits/advantages which is why FastRTC supports both. Artifacts in the WebSocket playback
        audio are due to gaps in my frontend processing code and not FastRTC web server.
    </div>

    <div class="container">
        <div class="panel">
            <h2>WebRTC Connection</h2>
            <div id="webrtc-chat" class="chat-container"></div>
            <div id="webrtc-metrics" class="metrics">
                <div class="metric">RTT (Round Trip Time): <span id="webrtc-rtt">-</span></div>
            </div>
            <button id="webrtc-button">Connect WebRTC</button>
        </div>

        <div class="panel">
            <h2>WebSocket Connection</h2>
            <div id="ws-chat" class="chat-container"></div>
            <div id="ws-metrics" class="metrics">
                <div class="metric">RTT (Round Trip Time): <span id="ws-rtt">0</span></div>
            </div>
            <button id="ws-button">Connect WebSocket</button>
        </div>
    </div>

    <audio id="webrtc-audio" style="display: none;"></audio>
    <audio id="ws-audio" style="display: none;"></audio>

    <div id="error-toast" class="toast"></div>

    <script>
        // Shared utilities
        function generateId() {
            return Math.random().toString(36).substring(7);
        }

        function sendInput(id) {

            return function handleMessage(event) {
                const eventJson = JSON.parse(event.data);
                if (eventJson.type === "send_input") {
                    fetch('/input_hook', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({
                            webrtc_id: id,
                            chatbot: chatHistoryWebRTC
                        })
                    });
                }
            }
        }

        let chatHistoryWebRTC = [];
        let chatHistoryWebSocket = [];

        function addMessage(containerId, role, content) {
            const container = document.getElementById(containerId);
            const messageDiv = document.createElement('div');
            messageDiv.classList.add('message', role);
            messageDiv.textContent = content;
            container.appendChild(messageDiv);
            container.scrollTop = container.scrollHeight;
            if (containerId === 'webrtc-chat') {
                chatHistoryWebRTC.push({ role, content });
            } else {
                chatHistoryWebSocket.push({ role, content });
            }
        }

        // WebRTC Implementation
        let webrtcPeerConnection;

        // Add this function to collect RTT stats
        async function updateWebRTCStats() {
            if (!webrtcPeerConnection) return;

            const stats = await webrtcPeerConnection.getStats();
            stats.forEach(report => {
                if (report.type === 'candidate-pair' && report.state === 'succeeded') {
                    const rtt = report.currentRoundTripTime * 1000; // Convert to ms
                    document.getElementById('webrtc-rtt').textContent = `${rtt.toFixed(2)}ms`;
                }
            });
        }

        async function setupWebRTC() {
            const button = document.getElementById('webrtc-button');
            button.textContent = "Stop";

            const config = __RTC_CONFIGURATION__;
            webrtcPeerConnection = new RTCPeerConnection(config);
            const webrtcId = generateId();

            const timeoutId = setTimeout(() => {
                const toast = document.getElementById('error-toast');
                toast.textContent = "Connection is taking longer than usual. Are you on a VPN?";
                toast.className = 'toast warning';
                toast.style.display = 'block';

                // Hide warning after 5 seconds
                setTimeout(() => {
                    toast.style.display = 'none';
                }, 5000);
            }, 5000);

            try {
                const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                stream.getTracks().forEach(track => {
                    webrtcPeerConnection.addTrack(track, stream);
                });

                webrtcPeerConnection.addEventListener('track', (evt) => {
                    const audio = document.getElementById('webrtc-audio');
                    if (audio.srcObject !== evt.streams[0]) {
                        audio.srcObject = evt.streams[0];
                        audio.play();
                    }
                });

                const dataChannel = webrtcPeerConnection.createDataChannel('text');
                dataChannel.onmessage = sendInput(webrtcId);

                const offer = await webrtcPeerConnection.createOffer();
                await webrtcPeerConnection.setLocalDescription(offer);

                await new Promise((resolve) => {
                    if (webrtcPeerConnection.iceGatheringState === "complete") {
                        resolve();
                    } else {
                        const checkState = () => {
                            if (webrtcPeerConnection.iceGatheringState === "complete") {
                                webrtcPeerConnection.removeEventListener("icegatheringstatechange", checkState);
                                resolve();
                            }
                        };
                        webrtcPeerConnection.addEventListener("icegatheringstatechange", checkState);
                    }
                });

                const response = await fetch('/webrtc/offer', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        sdp: webrtcPeerConnection.localDescription.sdp,
                        type: webrtcPeerConnection.localDescription.type,
                        webrtc_id: webrtcId
                    })
                });

                const serverResponse = await response.json();
                await webrtcPeerConnection.setRemoteDescription(serverResponse);

                // Setup event source for messages
                const eventSource = new EventSource('/outputs?webrtc_id=' + webrtcId);
                eventSource.addEventListener("output", (event) => {
                    const eventJson = JSON.parse(event.data);
                    addMessage('webrtc-chat', eventJson.role, eventJson.content);
                });

                // Add periodic stats collection
                const statsInterval = setInterval(updateWebRTCStats, 1000);

                // Store the interval ID on the connection
                webrtcPeerConnection.statsInterval = statsInterval;

                webrtcPeerConnection.addEventListener('connectionstatechange', () => {
                    if (webrtcPeerConnection.connectionState === 'connected') {
                        clearTimeout(timeoutId);
                        const toast = document.getElementById('error-toast');
                        toast.style.display = 'none';
                    }
                });

            } catch (err) {
                clearTimeout(timeoutId);
                console.error('WebRTC setup error:', err);
            }
        }

        function webrtc_stop() {
            if (webrtcPeerConnection) {
                // Clear the stats interval
                if (webrtcPeerConnection.statsInterval) {
                    clearInterval(webrtcPeerConnection.statsInterval);
                }

                // Close all tracks
                webrtcPeerConnection.getSenders().forEach(sender => {
                    if (sender.track) {
                        sender.track.stop();
                    }
                });

                webrtcPeerConnection.close();
                webrtcPeerConnection = null;

                // Reset metrics display
                document.getElementById('webrtc-rtt').textContent = '-';
            }
        }

        // WebSocket Implementation
        let webSocket;
        let wsMetrics = {
            pingStartTime: 0,
            rttValues: []
        };

        // Load mu-law library

        // Add load promise to track when the script is ready


        function resample(audioData, fromSampleRate, toSampleRate) {
            const ratio = fromSampleRate / toSampleRate;
            const newLength = Math.round(audioData.length / ratio);
            const result = new Float32Array(newLength);

            for (let i = 0; i < newLength; i++) {
                const position = i * ratio;
                const index = Math.floor(position);
                const fraction = position - index;

                if (index + 1 < audioData.length) {
                    result[i] = audioData[index] * (1 - fraction) + audioData[index + 1] * fraction;
                } else {
                    result[i] = audioData[index];
                }
            }
            return result;
        }

        function convertToMulaw(audioData, sampleRate) {
            // Resample to 8000 Hz if needed
            if (sampleRate !== 8000) {
                audioData = resample(audioData, sampleRate, 8000);
            }

            // Convert float32 [-1,1] to int16 [-32768,32767]
            const int16Data = new Int16Array(audioData.length);
            for (let i = 0; i < audioData.length; i++) {
                int16Data[i] = Math.floor(audioData[i] * 32768);
            }

            // Convert to mu-law using the library
            return alawmulaw.mulaw.encode(int16Data);
        }

        async function setupWebSocket() {
            const button = document.getElementById('ws-button');
            button.textContent = "Stop";

            try {
                const stream = await navigator.mediaDevices.getUserMedia({
                    audio: {
                        "echoCancellation": true,
                        "noiseSuppression": { "exact": true },
                        "autoGainControl": { "exact": true },
                        "sampleRate": { "ideal": 24000 },
                        "sampleSize": { "ideal": 16 },
                        "channelCount": { "exact": 1 },
                    }
                });
                const wsId = generateId();
                wsMetrics.startTime = performance.now();

                // Create audio context and analyser for visualization
                const audioContext = new AudioContext();
                const analyser = audioContext.createAnalyser();
                const source = audioContext.createMediaStreamSource(stream);
                source.connect(analyser);

                // Connect to websocket endpoint
                webSocket = new WebSocket(`${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/websocket/offer`);

                webSocket.onopen = () => {
                    // Send initial start message
                    webSocket.send(JSON.stringify({
                        event: "start",
                        websocket_id: wsId
                    }));

                    // Setup audio processing
                    const processor = audioContext.createScriptProcessor(2048, 1, 1);
                    source.connect(processor);
                    processor.connect(audioContext.destination);

                    processor.onaudioprocess = (e) => {
                        const inputData = e.inputBuffer.getChannelData(0);
                        const mulawData = convertToMulaw(inputData, audioContext.sampleRate);
                        const base64Audio = btoa(String.fromCharCode.apply(null, mulawData));
                        if (webSocket.readyState === WebSocket.OPEN) {
                            webSocket.send(JSON.stringify({
                                event: "media",
                                media: {
                                    payload: base64Audio
                                }
                            }));
                        }
                    };

                    // Add ping interval
                    webSocket.pingInterval = setInterval(() => {
                        wsMetrics.pingStartTime = performance.now();
                        webSocket.send(JSON.stringify({
                            event: "ping"
                        }));
                    }, 500);
                };

                // Setup audio output context
                const outputContext = new AudioContext({ sampleRate: 24000 });
                const sampleRate = 24000; // Updated to match server sample rate
                let audioQueue = [];
                let isPlaying = false;

                webSocket.onmessage = (event) => {
                    const data = JSON.parse(event.data);
                    if (data?.type === "send_input") {
                        console.log("sending input")
                        fetch('/input_hook', {
                            method: 'POST',
                            headers: { 'Content-Type': 'application/json' },
                            body: JSON.stringify({ webrtc_id: wsId, chatbot: chatHistoryWebSocket })
                        });
                    }
                    if (data.event === "media") {
                        // Process received audio
                        const audioData = atob(data.media.payload);
                        const mulawData = new Uint8Array(audioData.length);
                        for (let i = 0; i < audioData.length; i++) {
                            mulawData[i] = audioData.charCodeAt(i);
                        }

                        // Convert mu-law to linear PCM
                        const linearData = alawmulaw.mulaw.decode(mulawData);

                        // Create an AudioBuffer
                        const audioBuffer = outputContext.createBuffer(1, linearData.length, sampleRate);
                        const channelData = audioBuffer.getChannelData(0);

                        // Fill the buffer with the decoded data
                        for (let i = 0; i < linearData.length; i++) {
                            channelData[i] = linearData[i] / 32768.0;
                        }

                        // Queue the audio buffer
                        audioQueue.push(audioBuffer);

                        // Start playing if not already playing
                        if (!isPlaying) {
                            playNextBuffer();
                        }
                    }

                    // Add pong handler
                    if (data.event === "pong") {
                        const rtt = performance.now() - wsMetrics.pingStartTime;
                        wsMetrics.rttValues.push(rtt);
                        // Keep only last 20 values for running mean
                        if (wsMetrics.rttValues.length > 20) {
                            wsMetrics.rttValues.shift();
                        }
                        const avgRtt = wsMetrics.rttValues.reduce((a, b) => a + b, 0) / wsMetrics.rttValues.length;
                        document.getElementById('ws-rtt').textContent = `${avgRtt.toFixed(2)}ms`;
                        return;
                    }
                };

                function playNextBuffer() {
                    if (audioQueue.length === 0) {
                        isPlaying = false;
                        return;
                    }

                    isPlaying = true;
                    const bufferSource = outputContext.createBufferSource();
                    bufferSource.buffer = audioQueue.shift();
                    bufferSource.connect(outputContext.destination);

                    bufferSource.onended = playNextBuffer;
                    bufferSource.start();
                }

                const eventSource = new EventSource('/outputs?webrtc_id=' + wsId);
                eventSource.addEventListener("output", (event) => {
                    console.log("ws output", event);
                    const eventJson = JSON.parse(event.data);
                    addMessage('ws-chat', eventJson.role, eventJson.content);
                });

            } catch (err) {
                console.error('WebSocket setup error:', err);
                button.disabled = false;
            }
        }

        function ws_stop() {
            if (webSocket) {
                webSocket.send(JSON.stringify({
                    event: "stop"
                }));
                // Clear ping interval
                if (webSocket.pingInterval) {
                    clearInterval(webSocket.pingInterval);
                }
                // Reset RTT display
                document.getElementById('ws-rtt').textContent = '-';
                wsMetrics.rttValues = [];

                // Clear the stats interval
                if (webSocket.statsInterval) {
                    clearInterval(webSocket.statsInterval);
                }
                webSocket.close();
            }
        }

        // Event Listeners
        document.getElementById('webrtc-button').addEventListener('click', () => {
            const button = document.getElementById('webrtc-button');
            if (button.textContent === 'Connect WebRTC') {
                setupWebRTC();
            } else {
                webrtc_stop();
                button.textContent = 'Connect WebRTC';
            }
        });
        const ws_start_button = document.getElementById('ws-button')
        ws_start_button.addEventListener('click', () => {
            if (ws_start_button.textContent === 'Connect WebSocket') {
                setupWebSocket();
                ws_start_button.textContent = 'Stop';
            } else {
                ws_stop();
                ws_start_button.textContent = 'Connect WebSocket';
            }
        });
        document.addEventListener("beforeunload", () => {
            ws_stop();
            webrtc_stop();
        });
    </script>
</body>

</html>