Skip to content

Support adb's delayed_ack in the TcpUsb bridge - #552

Merged
koral-- merged 2 commits into
DeviceFarmer:masterfrom
jozenstar:tcpusb-delayed-ack
Oct 5, 2026
Merged

koral-- merged 2 commits into
DeviceFarmer:masterfrom
jozenstar:tcpusb-delayed-ack

Conversation

@jozenstar

Copy link
Copy Markdown

Without adb's delayed_ack feature, a stream through the TcpUsb bridge has one A_WRTE in flight until the peer's A_OKAY. Data reaches the client once per round trip regardless of bandwidth, so over a WAN, screen mirroring stutters: frames arrive in bunches, one bunch per round trip. Android Studio's Device Mirroring and scrcpy are both affected.

Change

The bridge now implements delayed_ack the way adb's sockets.cpp and adb.cpp do:

  • Negotiation: the bridge lists features=delayed_ack; in its A_CNXN banner when the client's banner lists it. adb hosts list it when the server runs with ADB_BURST_MODE=1.
  • Credits: A_OPEN's arg1 and a 4-byte A_OKAY payload carry byte credits. The bridge sends while it has credit, and credits the client only once its bytes have been written, so a slow device or reader still pushes back.
  • Fallback: a stream opened with arg1 = 0 keeps one packet in flight. Without the feature, behaviour is unchanged.
  • Where: a new SendWindow class holds the per-stream state, used by both Service and ReverseStream.
  • Banner terminator: features= is ;-terminated. Otherwise adb reads the trailing NUL as part of the value and silently doesn't enable the feature.

Measurements

Through an STF 3.8.0 provider 182 ms away, scrcpy 4.1's video stream over adb reverse (720×1600, the device produces a frame every 18 ms):

Client fps Arrival gap p50 / p90 / max Frames arriving < 5 ms after the previous one
default 49 0 / 6 / 587 ms 505 of 567
ADB_BURST_MODE=1 52 18 / 29 / 110 ms 9 of 602

exec-out throughput: 34 → 90 Mbit/s. Android Studio's Device Mirroring went from a few fps to usable.

Testing

  • Unit tests: new tests cover negotiation, several packets in flight, stopping at the granted credit, the legacy single packet in flight, and the banner. npm test: 209 passing.

Without delayed_ack a stream has one A_WRTE in flight until the peer's
A_OKAY, so data arrives once per round trip regardless of bandwidth. Over
a 186 ms link that is 5.5 deliveries per second, which caps screen
mirroring (e.g. Android Studio's Device Mirroring) at a few fps.

With delayed_ack, A_OPEN's arg1 and a 4-byte A_OKAY payload carry byte
credits, as in adb's sockets.cpp. The bridge lists the feature in its
banner when the client's banner does (adb hosts do with
ADB_BURST_MODE=1). It then sends while it has credit and credits the
client once bytes are written. A stream opened with arg1 = 0 keeps one
packet in flight. Without the feature, nothing changes.

Measured through an STF provider 186 ms away, 60 Hz writes over adb
reverse: 5.5 bursts/s, 183 ms apart, without burst mode; 295 of 300
writes delivered as sent, 44 ms apart (the device's own pace), with it.
exec-out 34 -> 90 Mbit/s.

Signed-off-by: Arthur Yazhevich <[email protected]>
Comment thread src/adb/tcpusb/service.ts Outdated
Comment on lines +166 to +171
this.transport.write(data, () => {
if (!this.ended) {
debug('O:A_OKAY');
this.socket.write(Packet.assemble(Packet.A_OKAY, this.localId, this.remoteId, this.window.ack(data.length)));
}
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The write callback ignores its error argument, so a failed write is still acked as success and the client gets credit for bytes that never reached the device. Suggest closing the stream on error. (The !this.ended guard is fine: the stream is already closed by then.)

Suggested change
this.transport.write(data, () => {
if (!this.ended) {
debug('O:A_OKAY');
this.socket.write(Packet.assemble(Packet.A_OKAY, this.localId, this.remoteId, this.window.ack(data.length)));
}
});
this.transport.write(data, (err?: Error | null) => {
if (err) {
debug('transport write failed', err);
this.end();
return;
}
if (!this.ended) {
debug('O:A_OKAY');
this.socket.write(Packet.assemble(Packet.A_OKAY, this.localId, this.remoteId, this.window.ack(data.length)));
}
});

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, applied in 98b14ca: the stream ends on a write error instead of crediting the client.

Comment thread src/adb/tcpusb/reversestream.ts Outdated
Comment on lines +89 to +91
this.conn.write(data, () => {
if (!this.ended) ack();
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in service.ts: the write error is ignored, so a failed write is acked as success. Suggest ending the stream on error.

Suggested change
this.conn.write(data, () => {
if (!this.ended) ack();
});
this.conn.write(data, (err?: Error | null) => {
if (err) {
debug('conn write failed', err);
this.end();
return;
}
if (!this.ended) ack();
});

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, applied in 98b14ca: the stream ends on a write error instead of crediting the client.

With delayed_ack the bridge credits the client from the write callback. A
failed write was still credited, as if the bytes had reached the device or
the reverse connection. End the stream instead.

Signed-off-by: Arthur Yazhevich <[email protected]>
@koral--
koral-- merged commit 29a21cc into DeviceFarmer:master Oct 5, 2026
2 checks passed
@jozenstar
jozenstar deleted the tcpusb-delayed-ack branch October 6, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants