Repository navigation
Fix fast publication eligibility and response handling #1300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xianshijing-lk
wants to merge
2
commits into
main
Choose a base branch
from
sxian/ordered-fast-publication
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| // 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 <String, dynamic>{ | ||
| 'streamId': 'stream-1', | ||
| 'audioTracks': [ | ||
| <String, dynamic>{'id': 'mic-1', 'label': 'mic', 'kind': 'audio', 'enabled': true}, | ||
| ], | ||
| 'videoTracks': <dynamic>[], | ||
| }; | ||
| } | ||
| 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<void> waitUntil(bool Function() condition) async { | ||
| final deadline = DateTime.now().add(const Duration(seconds: 2)); | ||
| while (!condition() && DateTime.now().isBefore(deadline)) { | ||
| await Future<void>.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 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<lk_rtc.AddTrackRequest>(); | ||
| 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<TrackPublishException>()), | ||
| ); | ||
| 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<TrackPublishException>()), | ||
| ); | ||
| 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<TrackPublishException>())); | ||
| 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<TrackPublishException>())); | ||
| container.engine.markClosed(); | ||
| await failed; | ||
| await expectLater( | ||
| container.engine.addTrack(lk_rtc.AddTrackRequest(cid: 'later')), | ||
| throwsA(isA<TrackPublishException>()), | ||
| ); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Empty video codec during fallback
With
subscriberPrimary=trueandfastPublish=false,_enabledPublishCodecsactivates broken video fallback for audio-first codec lists. Given Opus/VP8 and requested VP9,_publishVideoTrackconverts Opus into an empty codec.AddTrackRequestthen carries an empty primary video codec instead of VP8.Learn more
Caching codecs for subscriber-primary connections without warm-up also enables the existing video codec fallback, not just overlapping negotiation. The fallback in _publishVideoTrack checks supported video codecs but selects the first entry from the unfiltered list. mimeTypeToVideoCodecString returns an empty string for audio/opus. That empty string becomes the primary SimulcastCodec and the track's codec rather than a supported video codec.
Example: Join with
subscriberPrimary=true,fastPublish=false, and codecs[audio/opus, video/vp8]. Publish a camera track withVideoPublishOptions(videoCodec: 'vp9'). The fallback selects Opus and producesvideoCodec: '', rather than selecting VP8.Recommended fix: Filter the advertised codecs to supported video codecs before selecting a fallback in
_publishVideoTrack. If no supported video codec remains, handle that case explicitly rather than converting an audio codec. Add a video publication regression test with warm-up disabled, an audio-first codec list, and an unavailable requested video codec.Was this helpful? React with 👍 or 👎 to provide feedback.