Skip to content

Commit c461a0c

Browse files
committed
feat: add drop/wait options to sendLossyBytes to control whether bytes are buffered when the dc is in low status
This was encountered when implementing the e2e tests - a large data track frame could be split up into potentially n packets, and if one packet is dropped the whole frame is considered dropped. So the behavior that is really wanted here is to buffer packets and send them once the data channel can accept more data, even though it is technically a lossy data channel.
1 parent 2f30575 commit c461a0c

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/room/RTCEngine.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,22 +1413,34 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
14131413
async sendLossyBytes(
14141414
bytes: Uint8Array,
14151415
kind: Exclude<DataChannelKind, DataChannelKind.RELIABLE>,
1416+
bufferStatusLowBehavior: 'drop' | 'wait' = 'drop',
14161417
) {
14171418
// make sure we do have a data connection
14181419
await this.ensurePublisherConnected(kind);
14191420

14201421
const dc = this.dataChannelForKind(kind);
14211422
if (dc) {
1422-
// lossy channel, drop messages to reduce latency
14231423
if (!this.isBufferStatusLow(kind)) {
1424-
this.lossyDataDropCount += 1;
1425-
if (this.lossyDataDropCount % 100 === 0) {
1426-
this.log.warn(
1427-
`dropping lossy data channel messages, total dropped: ${this.lossyDataDropCount}`,
1428-
this.logContext,
1429-
);
1424+
// Depending on the exact circumstance that data is being sent, either drop or wait for the
1425+
// buffer status to not be low before continuing.
1426+
switch (bufferStatusLowBehavior) {
1427+
case 'wait':
1428+
this.log.warn(`waiting for data channel buffer status to go low, ${dc.bufferedAmount} > ${dc.bufferedAmountLowThreshold}`, bytes, this.logContext);
1429+
await this.waitForBufferStatusLow(kind);
1430+
this.log.warn(`data channel buffer status no longer low, ${dc.bufferedAmount} <= ${dc.bufferedAmountLowThreshold}`, this.logContext);
1431+
break;
1432+
case 'drop':
1433+
// this.log.warn(`dropping lossy data channel message`, this.logContext);
1434+
// Drop messages to reduce latency
1435+
this.lossyDataDropCount += 1;
1436+
if (this.lossyDataDropCount % 100 === 0) {
1437+
this.log.warn(
1438+
`dropping lossy data channel messages, total dropped: ${this.lossyDataDropCount}`,
1439+
this.logContext,
1440+
);
1441+
}
1442+
return;
14301443
}
1431-
return;
14321444
}
14331445
this.lossyDataStatCurrentBytes += bytes.byteLength;
14341446

src/room/Room.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
289289
this.emit(RoomEvent.LocalDataTrackUnpublished, event.sid);
290290
})
291291
.on('packetAvailable', ({ bytes }) => {
292-
this.engine.sendLossyBytes(bytes, DataChannelKind.DATA_TRACK_LOSSY);
292+
this.engine.sendLossyBytes(bytes, DataChannelKind.DATA_TRACK_LOSSY, 'wait');
293293
});
294294

295295
this.disconnectLock = new Mutex();

0 commit comments

Comments
 (0)