From 5ea740ea24eca719f82bbc5c0bdee35854cacd4c Mon Sep 17 00:00:00 2001 From: David Westgate Date: Fri, 25 Apr 2025 16:52:28 -0700 Subject: [PATCH] update zap; add ice restart --- src/static/js/doc.ts | 12 ------------ src/static/js/video.ts | 22 ++++++++++++++++++---- src/ws.ts | 17 ++++++++++------- src/zap.ts | 6 +++--- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/static/js/doc.ts b/src/static/js/doc.ts index aeb66bc..348e0a8 100644 --- a/src/static/js/doc.ts +++ b/src/static/js/doc.ts @@ -46,16 +46,4 @@ const tune = (adapter = 0) => { } -const getSignal = (adapter = 0) => { - const signalElement = document.getElementById(`signal-${adapter}`); - if (!signalElement) { - return; - } - fetch(`/api/signal?adapter=${adapter}`).then(res => - res.json() - ).then((strength: any) => { - signalElement.innerHTML = `Signal: ${strength.signal} C/N: ${strength.cn}` - }) -} - populateChannels(PLAYERS); \ No newline at end of file diff --git a/src/static/js/video.ts b/src/static/js/video.ts index 6abd54f..1ca1ff1 100644 --- a/src/static/js/video.ts +++ b/src/static/js/video.ts @@ -4,10 +4,10 @@ const ws0builder = isSecure ? `wss://${host}/ws0` : `ws://${host}:3001`; const ws1builder = isSecure ? `wss://${host}/ws1` : `ws://${host}:3002`; const ws0 = new WebSocket(ws0builder); const ws1 = new WebSocket(ws1builder); -const config = { +const config = { iceServers: [ { - urls: ['stun:dwestgate.us:3478','turn:dwestgate.us:3478?transport=udp'], + urls: ['stun:dwestgate.us:3478', 'turn:dwestgate.us:3478?transport=udp'], username: 'webrtcuser', credential: 'webrtccred' } @@ -21,8 +21,8 @@ interface IData { cn: number } -const pc0 = new RTCPeerConnection(config); -const pc1 = new RTCPeerConnection(config); +const pc0 = new RTCPeerConnection(isSecure ? config : {}); +const pc1 = new RTCPeerConnection(isSecure ? config : {}); const video0 = document.getElementById('video-0') as HTMLVideoElement; const video1 = document.getElementById('video-1') as HTMLVideoElement; const station0 = document.getElementById('station-0') as HTMLHeadElement; @@ -60,12 +60,23 @@ pc0.onicecandidate = ({ candidate }) => { } }; +const restartIce = (pc, ws) => { + setInterval(async () => { + const offer = await pc.createOffer({ iceRestart: true }); + await pc.setLocalDescription(offer); + ws.send(JSON.stringify({ type: 'offer', data: offer })); + }, 50000) +} + ws0.onopen = async () => { pc0.addTransceiver('video', { direction: 'recvonly' }); pc0.addTransceiver('audio', { direction: 'recvonly' }) const offer = await pc0.createOffer(); await pc0.setLocalDescription(offer); ws0.send(JSON.stringify({ type: 'offer', data: offer })); + if (isSecure) { + restartIce(pc0,ws0); + } } ws0.onmessage = async (message) => { @@ -114,6 +125,9 @@ ws1.onopen = async () => { const offer = await pc1.createOffer(); await pc1.setLocalDescription(offer); ws1.send(JSON.stringify({ type: 'offer', data: offer })); + if (isSecure) { + restartIce(pc1,ws1); + } } ws1.onmessage = async (message) => { diff --git a/src/ws.ts b/src/ws.ts index db3d4b7..5946087 100644 --- a/src/ws.ts +++ b/src/ws.ts @@ -4,7 +4,7 @@ import * as ws from 'ws'; // Constants const WIDTH = 640; // Video width -const HEIGHT = 480; // Video height +const HEIGHT = 360; // Video height const FRAME_SIZE = WIDTH * HEIGHT * 1.5; // YUV420p frame size (460800 bytes) export default class TVWebSocket { @@ -88,23 +88,26 @@ export default class TVWebSocket { // Function to start FFmpeg and capture raw video startFFmpeg = (): ChildProcessWithoutNullStreams => { const p = spawn('ffmpeg', [ - '-loglevel', 'debug', + // '-loglevel', 'debug', '-i', this.videoDevice, - + '-framerate' ,'59.94', // Video '-map', '0:v:0', - '-framerate', '24', + '-r', '30', '-vf', `scale=${WIDTH}:${HEIGHT}:flags=fast_bilinear`, '-vcodec', 'rawvideo', + '-preset', 'ultrafast', + '-b:v' ,'1M', '-pix_fmt', 'yuv420p', '-f', 'rawvideo', - '-threads', '1', + // '-threads', '1', //quality + '-fflags', '+discardcorrupt', '-err_detect', 'ignore_err', - '-analyzeduration', '100M', - '-probesize', '100M', + '-analyzeduration', '500k', + '-probesize', '500k', 'pipe:3', diff --git a/src/zap.ts b/src/zap.ts index dc9f251..c098223 100644 --- a/src/zap.ts +++ b/src/zap.ts @@ -125,14 +125,14 @@ export default class Zap { zap.process.stderr.on("data", (data) => { - const output = data.toString(); + const output: string = data.toString(); if (/Lock/.test(output)) { clearTimeout(lockTimer); const match = output.match(this.regex); zap.channel = verifiedChannel; - zap.signal = match[1], - zap.cn = match[3] + zap.signal = match?.[1], + zap.cn = match?.[3] resolve(zap); }