Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const webrtcModal = document.getElementById('webrtcModal');
+ const pushBtn = document.getElementById('pushBtn');
+ const pullBtn = document.getElementById('pullBtn');
+ const localVideo = document.getElementById('localVideo');
+ let isPushing = false;
+ let isPulling = false;
+ let stream = null;
+
+ // WebRTC Modal Toggle
+ document.addEventListener('click', (e) => {
+ if (e.target.matches('[href="/blog"]')) {
+ const webrtcBtn = document.createElement('button');
+ webrtcBtn.textContent = 'Experience WebRTC';
+ webrtcBtn.className = 'webrtc-btn';
+ webrtcBtn.onclick = () => webrtcModal.style.display = 'block';
+ document.body.appendChild(webrtcBtn);
+ }
+ });
+
+ // Close modal when clicking outside
+ webrtcModal.addEventListener('click', (e) => {
+ if (e.target === webrtcModal) {
+ webrtcModal.style.display = 'none';
+ stopStream();
+ }
+ });
+
+ // Handle push stream
+ pushBtn.addEventListener('click', async () => {
+ if (!isPushing) {
+ try {
+ stream = await navigator.mediaDevices.getUserMedia({
+ video: true,
+ audio: document.getElementById('audioToggle').checked
+ });
+ localVideo.srcObject = stream;
+ isPushing = true;
+ pushBtn.textContent = 'Stop Pushing';
+ updateStats();
+ } catch (err) {
+ console.error('Error accessing media devices:', err);
+ }
+ } else {
+ stopStream();
+ }
+ });
+
+ // Handle pull stream
+ pullBtn.addEventListener('click', () => {
+ isPulling = !isPulling;
+ pullBtn.textContent = isPulling ? 'Stop Pulling' : 'Start Pulling';
+ if (isPulling) {
+ updateStats();
+ }
+ });
+
+ function stopStream() {
+ if (stream) {
+ stream.getTracks().forEach(track => track.stop());
+ localVideo.srcObject = null;
+ }
+ isPushing = false;
+ pushBtn.textContent = 'Start Pushing';
+ }
+
+ function updateStats() {
+ if (isPushing || isPulling) {
+ const upstreamStats = document.getElementById('upstreamStats');
+ const downstreamStats = document.getElementById('downstreamStats');
+
+ setInterval(() => {
+ if (isPushing) {
+ const packetLoss = Math.floor(Math.random() * 20);
+ const rtt = Math.floor(Math.random() * 100);
+ const jitter = Math.floor(Math.random() * 100);
+ const delay = Math.floor(Math.random() * 200);
+
+ upstreamStats.textContent =
+ `Upstream quality: packet loss: ${packetLoss}, RTT: ${rtt}ms, jitter: ${jitter}ms, delay: ${delay}ms`;
+ }
+
+ if (isPulling) {
+ const packetLoss = Math.floor(Math.random() * 20);
+ const rtt = Math.floor(Math.random() * 100);
+ const jitter = Math.floor(Math.random() * 100);
+ const delay = Math.floor(Math.random() * 200);
+
+ downstreamStats.textContent =
+ `Downstream quality: packet loss: ${packetLoss}, RTT: ${rtt}ms, jitter: ${jitter}ms, delay: ${delay}ms`;
+ }
+ }, 1000);
+ }
+ }
+ });