Skip to content

Commit 553b869

Browse files
committed
fix backpressure acks, they need to be on a tight timer
1 parent 9daee7d commit 553b869

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

addons/gst-web-core/selkies-core.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ let micWorkletNode = null;
4141
let preferredInputDeviceId = null;
4242
let preferredOutputDeviceId = null;
4343
let metricsIntervalId = null;
44+
let backpressureIntervalId = null;
4445
const METRICS_INTERVAL_MS = 500;
46+
const BACKPRESSURE_INTERVAL_MS = 50;
4547
const UPLOAD_CHUNK_SIZE = (1024 * 1024) - 1;
4648
const FILE_UPLOAD_THROTTLE_MS = 200;
4749
let fileUploadProgressLastSent = {};
@@ -2508,6 +2510,18 @@ function handleDecodedFrame(frame) { // frame.codedWidth/Height are physical pix
25082510
websocket = new WebSocket(websocketEndpointURL.href);
25092511
websocket.binaryType = 'arraybuffer';
25102512

2513+
const sendBackpressureAck = () => {
2514+
if (websocket && websocket.readyState === WebSocket.OPEN) {
2515+
try {
2516+
if (lastReceivedVideoFrameId !== -1) {
2517+
websocket.send(`CLIENT_FRAME_ACK ${lastReceivedVideoFrameId}`);
2518+
}
2519+
} catch (error) {
2520+
console.error('[Backpressure] Error sending frame ACK:', error);
2521+
}
2522+
}
2523+
};
2524+
25112525
const sendClientMetrics = () => {
25122526
if (isSharedMode) return; // Shared mode does not have client-side FPS display in this context
25132527

@@ -2548,21 +2562,6 @@ function handleDecodedFrame(frame) { // frame.codedWidth/Height are physical pix
25482562
});
25492563
}
25502564
}
2551-
2552-
if (websocket && websocket.readyState === WebSocket.OPEN) {
2553-
if (audioWorkletProcessorPort) {
2554-
audioWorkletProcessorPort.postMessage({
2555-
type: 'getBufferSize'
2556-
});
2557-
}
2558-
try {
2559-
if (lastReceivedVideoFrameId !== -1) {
2560-
websocket.send(`CLIENT_FRAME_ACK ${lastReceivedVideoFrameId}`);
2561-
}
2562-
} catch (error) {
2563-
console.error('[websockets] Error sending client metrics (ACK):', error);
2564-
}
2565-
}
25662565
};
25672566

25682567
websocket.onopen = () => {
@@ -2702,6 +2701,10 @@ function handleDecodedFrame(frame) { // frame.codedWidth/Height are physical pix
27022701
metricsIntervalId = setInterval(sendClientMetrics, METRICS_INTERVAL_MS);
27032702
console.log(`[websockets] Started sending client metrics every ${METRICS_INTERVAL_MS}ms.`);
27042703
}
2704+
if (backpressureIntervalId === null) {
2705+
backpressureIntervalId = setInterval(sendBackpressureAck, BACKPRESSURE_INTERVAL_MS);
2706+
console.log(`[websockets] Started sending backpressure ACKs every ${BACKPRESSURE_INTERVAL_MS}ms.`);
2707+
}
27052708
}
27062709
};
27072710

@@ -3258,6 +3261,10 @@ function handleDecodedFrame(frame) { // frame.codedWidth/Height are physical pix
32583261
clearInterval(metricsIntervalId);
32593262
metricsIntervalId = null;
32603263
}
3264+
if (backpressureIntervalId) {
3265+
clearInterval(backpressureIntervalId);
3266+
backpressureIntervalId = null;
3267+
}
32613268
releaseWakeLock();
32623269
if (isSharedMode) {
32633270
console.error("Shared mode: WebSocket error. Resetting shared state to 'error'.");
@@ -3276,6 +3283,10 @@ function handleDecodedFrame(frame) { // frame.codedWidth/Height are physical pix
32763283
clearInterval(metricsIntervalId);
32773284
metricsIntervalId = null;
32783285
}
3286+
if (backpressureIntervalId) {
3287+
clearInterval(backpressureIntervalId);
3288+
backpressureIntervalId = null;
3289+
}
32793290
releaseWakeLock();
32803291
cleanupVideoBuffer();
32813292
cleanupJpegStripeQueue();
@@ -3591,6 +3602,10 @@ function cleanup() {
35913602
clearInterval(metricsIntervalId);
35923603
metricsIntervalId = null;
35933604
}
3605+
if (backpressureIntervalId) {
3606+
clearInterval(backpressureIntervalId);
3607+
backpressureIntervalId = null;
3608+
}
35943609
releaseWakeLock();
35953610
if (window.isCleaningUp) return;
35963611
window.isCleaningUp = true;

0 commit comments

Comments
 (0)