cduss Claude Opus 4.5 commited on
Commit
908a587
·
1 Parent(s): 8661c74

Add latency monitor to prevent buffer accumulation

Browse files

- Monitor video buffer every 2 seconds
- If more than 0.5s behind live edge, skip to catch up
- Prevents gradual latency increase over time

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Files changed (1) hide show
  1. index.html +31 -1
index.html CHANGED
@@ -717,6 +717,7 @@
717
  let micEnabled = false;
718
  let robotMuted = true;
719
  let audioSender = null;
 
720
 
721
  // Export functions
722
  window.loginToHuggingFace = loginToHuggingFace;
@@ -916,7 +917,14 @@
916
 
917
  peerConnection.ontrack = (e) => {
918
  if (e.track.kind === 'video') {
919
- document.getElementById('remoteVideo').srcObject = e.streams[0];
 
 
 
 
 
 
 
920
  }
921
  if (e.track.kind === 'audio') {
922
  const audioEl = document.getElementById('remoteAudio');
@@ -981,7 +989,29 @@
981
  }
982
  }
983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
984
  function stopStream() {
 
985
  if (stateRefreshInterval) clearInterval(stateRefreshInterval);
986
  if (peerConnection) peerConnection.close();
987
  if (dataChannel) dataChannel.close();
 
717
  let micEnabled = false;
718
  let robotMuted = true;
719
  let audioSender = null;
720
+ let latencyMonitorId = null;
721
 
722
  // Export functions
723
  window.loginToHuggingFace = loginToHuggingFace;
 
917
 
918
  peerConnection.ontrack = (e) => {
919
  if (e.track.kind === 'video') {
920
+ const video = document.getElementById('remoteVideo');
921
+ video.srcObject = e.streams[0];
922
+ // Low-latency optimizations
923
+ video.playsInline = true;
924
+ if ('requestVideoFrameCallback' in video) {
925
+ // Start latency monitoring
926
+ startLatencyMonitor(video);
927
+ }
928
  }
929
  if (e.track.kind === 'audio') {
930
  const audioEl = document.getElementById('remoteAudio');
 
989
  }
990
  }
991
 
992
+ // Latency monitor - catch up to live if buffer grows too large
993
+ function startLatencyMonitor(video) {
994
+ if (latencyMonitorId) clearInterval(latencyMonitorId);
995
+
996
+ latencyMonitorId = setInterval(() => {
997
+ if (!video.srcObject || video.paused) return;
998
+
999
+ const buffered = video.buffered;
1000
+ if (buffered.length > 0) {
1001
+ const bufferedEnd = buffered.end(buffered.length - 1);
1002
+ const lag = bufferedEnd - video.currentTime;
1003
+
1004
+ // If more than 0.5s behind live edge, catch up
1005
+ if (lag > 0.5) {
1006
+ console.log(`Latency correction: was ${lag.toFixed(2)}s behind`);
1007
+ video.currentTime = bufferedEnd - 0.1;
1008
+ }
1009
+ }
1010
+ }, 2000); // Check every 2 seconds
1011
+ }
1012
+
1013
  function stopStream() {
1014
+ if (latencyMonitorId) clearInterval(latencyMonitorId);
1015
  if (stateRefreshInterval) clearInterval(stateRefreshInterval);
1016
  if (peerConnection) peerConnection.close();
1017
  if (dataChannel) dataChannel.close();