Skip to content

Commit 5e490e3

Browse files
authored
Send the offer requested while an answer is being applied (#1284)
## Problem A track published right after another one can stay unpublished, with no error. The common case is `setCameraEnabled(true)` followed by `setMicrophoneEnabled(true)` after connecting: the camera is published, the microphone never reaches the server, and remote participants never see it. ## Cause A publisher offer requested while the previous offer is still pending is deferred. `createAndSendOffer` sets `renegotiate`, and `setRemoteDescription` sends the deferred offer once the answer is applied. `createAndSendOffer` read the signaling state, then awaited `getRemoteDescription()`, and only then set the flag. When the answer finished applying inside that gap, `setRemoteDescription` had already checked the flag. The flag was then set with nobody left to act on it, so the offer was never sent. The window was always there, but it was one platform call wide. The duplicate answer guard from #1257 reads the signaling state before it applies an answer, which gives a request that arrives just after the answer the time to see the old state. Starting audio capture can delay the camera offer by more than a second on macOS, which lines the microphone request up with the camera answer. #1257 is not in a published release. Trace from an instrumented build: ``` 41.639120 answer arrived reads the signaling state first 41.639429 createAndSendOffer enter the microphone request 41.641056 createAndSendOffer saw have-local-offer then awaits getRemoteDescription 41.642264 after setRemoteDescription(answer) renegotiate=false, checked once 41.642659 renegotiate flag set too late ``` The server log of the same run shows an answer with one video section and no audio section, and no later offer from the publisher. ## Fix Set the flag right after the state is read, with nothing awaited in between. The remote description is now read only for an ICE restart, the only path that uses it. ## Testing - New unit test in `test/core/transport_negotiate_test.dart`. It fails on `main` (one offer instead of two) and passes here. - e2e-flutter `NodeFailureTest` on macOS, which publishes camera then microphone: 6 failures in 11 runs before, 11 passes in 11 runs after. With the fix the same interleaving still occurs, the answer handler now sees the flag and sends the deferred offer. - e2e-flutter full suite on macOS with the fix: 43 pass, 0 fail, 3 expected failures, 8 skips. ## Notes - #1262 (offer ids) keeps the same await between the state read and the flag, so it still needs this. The two merge cleanly, one test line needs adapting to the new `onOffer` signature. - This should be part of 2.14.0.
1 parent f38055a commit 5e490e3

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎.changes/deferred-offer-lost‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="fixed" "A track published right after another one is no longer left unpublished. Its offer was dropped when the request arrived while the answer to the previous offer was being applied, for example a microphone enabled right after the camera"

‎lib/src/core/transport.dart‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,21 @@ class Transport extends Disposable {
379379
if (await pc.getSignalingState() == rtc.RTCSignalingState.RTCSignalingStateHaveLocalOffer) {
380380
// we're waiting for the peer to accept our offer, so we'll just wait
381381
// the only exception to this is when ICE restart is needed
382+
if (!(options?.iceRestart ?? false)) {
383+
// Nothing may be awaited between reading the state and setting the flag. The answer
384+
// can finish applying in that gap, and setRemoteDescription checks the flag only once,
385+
// so a flag set after that check is never picked up and this offer is never sent.
386+
renegotiate = true;
387+
return;
388+
}
382389
final currentSD = await getRemoteDescription();
383-
if ((options?.iceRestart ?? false) && currentSD != null) {
390+
if (currentSD == null) {
384391
// TODO: handle when ICE restart is needed but we don't have a remote description
385392
// the best thing to do is to recreate the peerconnection
386-
await pc.setRemoteDescription(currentSD);
387-
} else {
388393
renegotiate = true;
389394
return;
390395
}
396+
await pc.setRemoteDescription(currentSD);
391397
}
392398

393399
if (restartingIce && !lkPlatformIs(PlatformType.web)) {

‎test/core/transport_negotiate_test.dart‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,43 @@ class _RejectAfterAnswerPeerConnection extends MockPeerConnection {
4949
}
5050
}
5151

52+
/// A peer connection whose remote description takes a while to read, the way
53+
/// a platform call does when the native side is busy.
54+
class _SlowRemoteDescriptionPeerConnection extends MockPeerConnection {
55+
@override
56+
Future<rtc.RTCSessionDescription?> getRemoteDescription() async {
57+
await Future<void>.delayed(const Duration(milliseconds: 20));
58+
return super.getRemoteDescription();
59+
}
60+
}
61+
5262
void main() {
5363
group('Transport.negotiate', () {
64+
test('sends an offer that was requested while the answer to the previous one is applied', () async {
65+
final pc = _SlowRemoteDescriptionPeerConnection();
66+
final transport = await Transport.create(
67+
(Map<String, dynamic> configuration, [Map<String, dynamic>? constraints]) async => pc,
68+
connectOptions: const ConnectOptions(),
69+
);
70+
addTearDown(transport.dispose);
71+
final offers = <rtc.RTCSessionDescription>[];
72+
transport.onOffer = offers.add;
73+
74+
// The first track is offered and waits for its answer.
75+
await transport.createAndSendOffer();
76+
expect(offers, hasLength(1));
77+
78+
// A second track asks for an offer while the first one is still pending,
79+
// and the answer lands before that request has finished deciding to wait.
80+
final second = transport.createAndSendOffer();
81+
await transport.setRemoteDescription(rtc.RTCSessionDescription('v=0', 'answer'));
82+
await second;
83+
await Future<void>.delayed(const Duration(milliseconds: 50));
84+
85+
expect(offers, hasLength(2), reason: 'the second track was never offered');
86+
expect(transport.renegotiate, isFalse);
87+
});
88+
5489
test('reports a failed offer to onNegotiationError instead of leaking an unhandled error', () async {
5590
final uncaught = <Object>[];
5691
final reported = Completer<Object>();

0 commit comments

Comments
 (0)