From e32cbb64d0a70c874e44a38983ed57ba2da65f18 Mon Sep 17 00:00:00 2001 From: shijing xian Date: Thu, 19 Nov 2026 15:43:55 -0800 Subject: [PATCH 1/2] Own response waiters throughout fast track publication --- .changes/fast-publication-waiters | 1 + lib/src/core/engine.dart | 55 +++++-- test/mock/e2e_container.dart | 12 +- test/mock/websocket_mock.dart | 7 +- test/participant/fast_publish_test.dart | 182 ++++++++++++++++++++++++ 5 files changed, 244 insertions(+), 13 deletions(-) create mode 100644 .changes/fast-publication-waiters create mode 100644 test/participant/fast_publish_test.dart diff --git a/.changes/fast-publication-waiters b/.changes/fast-publication-waiters new file mode 100644 index 000000000..da768424e --- /dev/null +++ b/.changes/fast-publication-waiters @@ -0,0 +1 @@ +patch type="fixed" "Registers publication response waiters before sending AddTrack, fails pending publications promptly on rejection or disconnect, and resets cached fast-publication codecs during cleanup" diff --git a/lib/src/core/engine.dart b/lib/src/core/engine.dart index 48c9d227f..3dd66c237 100644 --- a/lib/src/core/engine.dart +++ b/lib/src/core/engine.dart @@ -188,6 +188,9 @@ class Engine extends Disposable with EventsEmittable { _sendStateChanged.complete(); } + // Owned by the engine and completed on acceptance, rejection, timeout, or teardown. + final _pendingTrackPublishes = >{}; + List? _enabledPublishCodecs; List? get enabledPublishCodecs => _enabledPublishCodecs; @@ -373,6 +376,8 @@ class Engine extends Disposable with EventsEmittable { // resets internal state to a re-usable state Future cleanUp() async { logger.fine('[$objectId] cleanUp()'); + _failPendingTrackPublishes(); + _enabledPublishCodecs = null; await publisher?.dispose(); publisher = null; @@ -397,17 +402,33 @@ class Engine extends Disposable with EventsEmittable { @internal Future addTrack(lk_rtc.AddTrackRequest req) async { - // send request to add track - signalClient.sendAddTrack(req); - - // wait for response, or timeout - final event = await _signalListener.waitFor( - filter: (event) => event.cid == req.cid, - duration: connectOptions.timeouts.publish, - onTimeout: () => throw TrackPublishException(), - ); + if (_isClosed) throw TrackPublishException('engine is closed'); + if (_pendingTrackPublishes.containsKey(req.cid)) { + throw TrackPublishException('track publication is already pending'); + } + final completer = Completer(); + _pendingTrackPublishes[req.cid] = completer; + try { + signalClient.sendAddTrack(req); + return await completer.future.timeout( + connectOptions.timeouts.publish, + onTimeout: () => throw TrackPublishException('timed out waiting for track acceptance'), + ); + } finally { + if (identical(_pendingTrackPublishes[req.cid], completer)) { + _pendingTrackPublishes.remove(req.cid); + } + } + } - return event.track; + void _failPendingTrackPublishes() { + final pending = _pendingTrackPublishes.values.toList(); + _pendingTrackPublishes.clear(); + for (final completer in pending) { + if (!completer.isCompleted) { + completer.completeError(TrackPublishException('connection closed while publishing')); + } + } } @internal @@ -1554,6 +1575,10 @@ class Engine extends Disposable with EventsEmittable { } void _setUpSignalListeners() => _signalListener + ..on((event) { + final completer = _pendingTrackPublishes[event.cid]; + if (completer != null && !completer.isCompleted) completer.complete(event.track); + }) ..on((event) async { // create peer connections _subscriberPrimary = event.response.subscriberPrimary; @@ -1742,6 +1767,15 @@ class Engine extends Disposable with EventsEmittable { } }) ..on((event) async { + final response = event.response; + if (response.hasAddTrack() && + response.reason != lk_rtc.RequestResponse_Reason.OK && + response.reason != lk_rtc.RequestResponse_Reason.QUEUED) { + final completer = _pendingTrackPublishes[response.addTrack.cid]; + if (completer != null && !completer.isCompleted) { + completer.completeError(TrackPublishException(response.message)); + } + } events.emit(EngineRequestResponseEvent(response: event.response)); }) ..on((event) async { @@ -1758,6 +1792,7 @@ class Engine extends Disposable with EventsEmittable { void markClosed() { if (_isClosed) return; _isClosed = true; + _failPendingTrackPublishes(); _sendStateChanged.complete(); events.emit(EngineClosingEvent()); } diff --git a/test/mock/e2e_container.dart b/test/mock/e2e_container.dart index 5a9965ae5..c74fa4a9b 100644 --- a/test/mock/e2e_container.dart +++ b/test/mock/e2e_container.dart @@ -62,6 +62,7 @@ class E2EContainer { Future connectRoom({ int? localClientProtocol, bool captureOutbound = false, + lk_rtc.SignalResponse? joinResponseOverride, ConnectOptions? connectOptions, @Deprecated('mirrors the deprecated Room.connect parameter') RoomOptions? roomOptions, lk_models.ClientConfiguration? clientConfiguration, @@ -73,7 +74,13 @@ class E2EContainer { // ignore: deprecated_member_use_from_same_package roomOptions: roomOptions, ); - unawaited(answerJoin(localClientProtocol: localClientProtocol, clientConfiguration: clientConfiguration)); + unawaited( + answerJoin( + localClientProtocol: localClientProtocol, + clientConfiguration: clientConfiguration, + joinResponseOverride: joinResponseOverride, + ), + ); await connectFuture; @@ -113,12 +120,13 @@ class E2EContainer { /// for a (re)join: a `JoinResponse` followed by the subscriber offer. Used by /// [connectRoom] and by tests that drive a full reconnect. Future answerJoin({ + lk_rtc.SignalResponse? joinResponseOverride, int? localClientProtocol, lk_models.ClientConfiguration? clientConfiguration, }) async { // Give the SDK a tick to start waiting for the join response. await Future.delayed(const Duration(milliseconds: 1)); - final resp = _buildJoinResponse(localClientProtocol, clientConfiguration); + final resp = joinResponseOverride ?? _buildJoinResponse(localClientProtocol, clientConfiguration); wsConnector.onData(resp.writeToBuffer()); wsConnector.onData(offerResponse.writeToBuffer()); } diff --git a/test/mock/websocket_mock.dart b/test/mock/websocket_mock.dart index 33fbadbf9..862d60843 100644 --- a/test/mock/websocket_mock.dart +++ b/test/mock/websocket_mock.dart @@ -43,6 +43,8 @@ class MockWebSocketConnector { /// Every message the SDK sent over any socket this connector opened. final List> sentData = []; + void Function(List data)? onSend; + WebSocketOnData get onData => handlers!.onData!; WebSocketOnDispose get onDispose => handlers!.onDispose!; @@ -70,6 +72,9 @@ class MockWebSocketConnector { } handlers = options; - return MockWebSocket(sentData.add); + return MockWebSocket((data) { + sentData.add(data); + onSend?.call(data); + }); } } diff --git a/test/participant/fast_publish_test.dart b/test/participant/fast_publish_test.dart new file mode 100644 index 000000000..5826098d6 --- /dev/null +++ b/test/participant/fast_publish_test.dart @@ -0,0 +1,182 @@ +// Copyright 2026 LiveKit, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import 'dart:async'; + +import 'package:flutter/services.dart'; + +import 'package:flutter_test/flutter_test.dart'; + +import 'package:livekit_client/livekit_client.dart'; +import 'package:livekit_client/src/proto/livekit_models.pb.dart' as lk_models; +import 'package:livekit_client/src/proto/livekit_rtc.pb.dart' as lk_rtc; +import 'package:livekit_client/src/support/native.dart'; +import '../core/signal_client_test.dart' show joinResponse; +import '../mock/e2e_container.dart'; +import '../mock/peerconnection_mock.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; + const webrtcChannel = MethodChannel('FlutterWebRTC.Method'); + + late E2EContainer container; + + setUp(() { + messenger.setMockMethodCallHandler(webrtcChannel, (call) async { + if (call.method == 'getUserMedia') { + return { + 'streamId': 'stream-1', + 'audioTracks': [ + {'id': 'mic-1', 'label': 'mic', 'kind': 'audio', 'enabled': true}, + ], + 'videoTracks': [], + }; + } + return null; + }); + messenger.setMockMethodCallHandler(Native.channel, (call) async => true); + resetMockDataChannels(); + container = E2EContainer(); + }); + + tearDown(() async { + await container.dispose(); + messenger.setMockMethodCallHandler(webrtcChannel, null); + messenger.setMockMethodCallHandler(Native.channel, null); + }); + + Future waitUntil(bool Function() condition) async { + final deadline = DateTime.now().add(const Duration(seconds: 2)); + while (!condition() && DateTime.now().isBefore(deadline)) { + await Future.delayed(const Duration(milliseconds: 1)); + } + expect(condition(), isTrue, reason: 'sender was not configured before acknowledgment'); + } + + lk_rtc.SignalResponse accepted(String cid) => lk_rtc.SignalResponse( + trackPublished: lk_rtc.TrackPublishedResponse( + cid: cid, + track: lk_models.TrackInfo(sid: 'TR_accepted', type: lk_models.TrackType.AUDIO), + ), + ); + + for (final fast in [false, true]) { + test('ordinary audio publication overlaps SDP only with codecs ($fast)', () async { + final join = joinResponse.deepCopy(); + join.join.fastPublish = fast; + if (fast) { + join.join.enabledPublishCodecs.add(lk_models.Codec(mime: 'audio/opus')); + } + await container.connectRoom(joinResponseOverride: join); + final track = await LocalAudioTrack.create(); + final requestSeen = Completer(); + container.wsConnector.onSend = (data) { + final request = lk_rtc.SignalRequest.fromBuffer(data); + if (request.hasAddTrack() && !requestSeen.isCompleted) requestSeen.complete(request.addTrack); + }; + var completed = false; + final publication = container.room.localParticipant!.publishAudioTrack(track).then((pub) { + completed = true; + return pub; + }); + final request = await requestSeen.future.timeout(const Duration(seconds: 2)); + if (fast) { + await waitUntil(() => track.transceiver != null); + } + expect(completed, isFalse, reason: 'publication must wait for server acceptance'); + expect(track.transceiver != null, fast); + expect(container.room.localParticipant!.trackPublications, isEmpty); + container.wsConnector.onData(accepted(request.cid).writeToBuffer()); + expect((await publication).sid, 'TR_accepted'); + expect(track.transceiver, isNotNull); + expect(container.room.localParticipant!.trackPublications.length, 1); + }); + } + + test('immediate acknowledgment is retained by the registered waiter', () async { + await container.connectRoom(); + container.wsConnector.onSend = (data) { + final request = lk_rtc.SignalRequest.fromBuffer(data); + if (request.hasAddTrack()) { + container.wsConnector.onData(accepted(request.addTrack.cid).writeToBuffer()); + } + }; + final info = await container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'immediate')); + expect(info.sid, 'TR_accepted'); + }); + + test('rejection fails promptly and allows the CID to be retried', () async { + await container.connectRoom(); + container.wsConnector.onSend = (data) { + final request = lk_rtc.SignalRequest.fromBuffer(data); + if (request.hasAddTrack()) { + container.wsConnector.onData( + lk_rtc.SignalResponse( + requestResponse: lk_rtc.RequestResponse( + addTrack: request.addTrack, + reason: lk_rtc.RequestResponse_Reason.NOT_ALLOWED, + message: 'not allowed', + ), + ).writeToBuffer(), + ); + } + }; + await expectLater( + container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'retry')), + throwsA(isA()), + ); + container.wsConnector.onSend = (data) { + final request = lk_rtc.SignalRequest.fromBuffer(data); + if (request.hasAddTrack()) container.wsConnector.onData(accepted(request.addTrack.cid).writeToBuffer()); + }; + expect((await container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'retry'))).sid, 'TR_accepted'); + }); + + test('duplicate CID cannot replace or cancel the first response waiter', () async { + await container.connectRoom(); + final pending = container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'duplicate')); + await expectLater( + container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'duplicate')), + throwsA(isA()), + ); + container.wsConnector.onData(accepted('duplicate').writeToBuffer()); + expect((await pending).sid, 'TR_accepted'); + }); + + test('cleanup fails pending registration and forgets negotiated fast codecs', () async { + final join = joinResponse.deepCopy(); + join.join.fastPublish = true; + join.join.enabledPublishCodecs.add(lk_models.Codec(mime: 'audio/opus')); + await container.connectRoom(joinResponseOverride: join); + expect(container.engine.enabledPublishCodecs, isNotEmpty); + final pending = container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'cleanup')); + final failed = expectLater(pending, throwsA(isA())); + await container.engine.cleanUp(); + await failed; + expect(container.engine.enabledPublishCodecs, isNull); + }); + + test('disconnect fails pending registration and rejects further publication', () async { + await container.connectRoom(); + final pending = container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'pending')); + final failed = expectLater(pending, throwsA(isA())); + container.engine.markClosed(); + await failed; + await expectLater( + container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'later')), + throwsA(isA()), + ); + }); +} From 0bd2fc34d3175190a6fde91f6c349373dcea2e07 Mon Sep 17 00:00:00 2001 From: shijing xian Date: Thu, 19 Nov 2026 16:03:24 -0800 Subject: [PATCH 2/2] Keep publication codec support independent of publisher warm-up --- .changes/fast-publication-waiters | 2 +- lib/src/core/engine.dart | 4 +- test/participant/fast_publish_test.dart | 62 +++++++++++++------------ 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/.changes/fast-publication-waiters b/.changes/fast-publication-waiters index da768424e..b5102d36f 100644 --- a/.changes/fast-publication-waiters +++ b/.changes/fast-publication-waiters @@ -1 +1 @@ -patch type="fixed" "Registers publication response waiters before sending AddTrack, fails pending publications promptly on rejection or disconnect, and resets cached fast-publication codecs during cleanup" +patch type="fixed" "Registers publication response waiters before sending AddTrack, fails pending publications promptly on rejection or disconnect, resets cached fast-publication codecs during cleanup, and preserves eligible publication overlap when publisher warm-up is disabled" diff --git a/lib/src/core/engine.dart b/lib/src/core/engine.dart index 3dd66c237..9c7e6bfec 100644 --- a/lib/src/core/engine.dart +++ b/lib/src/core/engine.dart @@ -1607,9 +1607,9 @@ class Engine extends Disposable with EventsEmittable { await _createPeerConnections(rtcConfiguration); } + // Codec support permits overlap independently of the publisher warm-up hint. + _enabledPublishCodecs = event.response.enabledPublishCodecs; if (!_subscriberPrimary || event.response.fastPublish) { - _enabledPublishCodecs = event.response.enabledPublishCodecs; - /// for subscriberPrimary, we negotiate when necessary (lazy) /// and if `response.fastPublish == true`, we need to negotiate /// immediately diff --git a/test/participant/fast_publish_test.dart b/test/participant/fast_publish_test.dart index 5826098d6..724e2e54c 100644 --- a/test/participant/fast_publish_test.dart +++ b/test/participant/fast_publish_test.dart @@ -72,37 +72,39 @@ void main() { ), ); - for (final fast in [false, true]) { - test('ordinary audio publication overlaps SDP only with codecs ($fast)', () async { - final join = joinResponse.deepCopy(); - join.join.fastPublish = fast; - if (fast) { - join.join.enabledPublishCodecs.add(lk_models.Codec(mime: 'audio/opus')); - } - await container.connectRoom(joinResponseOverride: join); - final track = await LocalAudioTrack.create(); - final requestSeen = Completer(); - container.wsConnector.onSend = (data) { - final request = lk_rtc.SignalRequest.fromBuffer(data); - if (request.hasAddTrack() && !requestSeen.isCompleted) requestSeen.complete(request.addTrack); - }; - var completed = false; - final publication = container.room.localParticipant!.publishAudioTrack(track).then((pub) { - completed = true; - return pub; + for (final warmUp in [false, true]) { + for (final fast in [false, true]) { + test('publication overlap follows codecs ($fast), independently of warm-up ($warmUp)', () async { + final join = joinResponse.deepCopy(); + join.join.fastPublish = warmUp; + if (fast) { + join.join.enabledPublishCodecs.add(lk_models.Codec(mime: 'audio/opus')); + } + await container.connectRoom(joinResponseOverride: join); + final track = await LocalAudioTrack.create(); + final requestSeen = Completer(); + container.wsConnector.onSend = (data) { + final request = lk_rtc.SignalRequest.fromBuffer(data); + if (request.hasAddTrack() && !requestSeen.isCompleted) requestSeen.complete(request.addTrack); + }; + var completed = false; + final publication = container.room.localParticipant!.publishAudioTrack(track).then((pub) { + completed = true; + return pub; + }); + final request = await requestSeen.future.timeout(const Duration(seconds: 2)); + if (fast) { + await waitUntil(() => track.transceiver != null); + } + expect(completed, isFalse, reason: 'publication must wait for server acceptance'); + expect(track.transceiver != null, fast); + expect(container.room.localParticipant!.trackPublications, isEmpty); + container.wsConnector.onData(accepted(request.cid).writeToBuffer()); + expect((await publication).sid, 'TR_accepted'); + expect(track.transceiver, isNotNull); + expect(container.room.localParticipant!.trackPublications.length, 1); }); - final request = await requestSeen.future.timeout(const Duration(seconds: 2)); - if (fast) { - await waitUntil(() => track.transceiver != null); - } - expect(completed, isFalse, reason: 'publication must wait for server acceptance'); - expect(track.transceiver != null, fast); - expect(container.room.localParticipant!.trackPublications, isEmpty); - container.wsConnector.onData(accepted(request.cid).writeToBuffer()); - expect((await publication).sid, 'TR_accepted'); - expect(track.transceiver, isNotNull); - expect(container.room.localParticipant!.trackPublications.length, 1); - }); + } } test('immediate acknowledgment is retained by the registered waiter', () async {