Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/fast-publication-waiters
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"
59 changes: 47 additions & 12 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
_sendStateChanged.complete();
}

// Owned by the engine and completed on acceptance, rejection, timeout, or teardown.
final _pendingTrackPublishes = <String, Completer<lk_models.TrackInfo>>{};

List<lk_models.Codec>? _enabledPublishCodecs;

List<lk_models.Codec>? get enabledPublishCodecs => _enabledPublishCodecs;
Expand Down Expand Up @@ -373,6 +376,8 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
// resets internal state to a re-usable state
Future<void> cleanUp() async {
logger.fine('[$objectId] cleanUp()');
_failPendingTrackPublishes();
_enabledPublishCodecs = null;

await publisher?.dispose();
publisher = null;
Expand All @@ -397,17 +402,33 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {

@internal
Future<lk_models.TrackInfo> addTrack(lk_rtc.AddTrackRequest req) async {
// send request to add track
signalClient.sendAddTrack(req);

// wait for response, or timeout
final event = await _signalListener.waitFor<SignalLocalTrackPublishedEvent>(
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<lk_models.TrackInfo>();
_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
Expand Down Expand Up @@ -1554,6 +1575,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}

void _setUpSignalListeners() => _signalListener
..on<SignalLocalTrackPublishedEvent>((event) {
final completer = _pendingTrackPublishes[event.cid];
if (completer != null && !completer.isCompleted) completer.complete(event.track);
})
..on<SignalJoinResponseEvent>((event) async {
// create peer connections
_subscriberPrimary = event.response.subscriberPrimary;
Expand Down Expand Up @@ -1582,9 +1607,9 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
await _createPeerConnections(rtcConfiguration);
}

// Codec support permits overlap independently of the publisher warm-up hint.
_enabledPublishCodecs = event.response.enabledPublishCodecs;

Copy link
Copy Markdown

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=true and fastPublish=false, _enabledPublishCodecs activates broken video fallback for audio-first codec lists. Given Opus/VP8 and requested VP9, _publishVideoTrack converts Opus into an empty codec. AddTrackRequest then 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 with VideoPublishOptions(videoCodec: 'vp9'). The fallback selects Opus and produces videoCodec: '', 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.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

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
Expand Down Expand Up @@ -1742,6 +1767,15 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}
})
..on<SignalRequestResponseEvent>((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<SignalRoomMovedEvent>((event) async {
Expand All @@ -1758,6 +1792,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
void markClosed() {
if (_isClosed) return;
_isClosed = true;
_failPendingTrackPublishes();
_sendStateChanged.complete();
events.emit(EngineClosingEvent());
}
Expand Down
12 changes: 10 additions & 2 deletions test/mock/e2e_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class E2EContainer {
Future<void> 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,
Expand All @@ -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;

Expand Down Expand Up @@ -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<void> 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<void>.delayed(const Duration(milliseconds: 1));
final resp = _buildJoinResponse(localClientProtocol, clientConfiguration);
final resp = joinResponseOverride ?? _buildJoinResponse(localClientProtocol, clientConfiguration);
wsConnector.onData(resp.writeToBuffer());
wsConnector.onData(offerResponse.writeToBuffer());
}
Expand Down
7 changes: 6 additions & 1 deletion test/mock/websocket_mock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class MockWebSocketConnector {
/// Every message the SDK sent over any socket this connector opened.
final List<List<int>> sentData = [];

void Function(List<int> data)? onSend;

WebSocketOnData get onData => handlers!.onData!;

WebSocketOnDispose get onDispose => handlers!.onDispose!;
Expand Down Expand Up @@ -70,6 +72,9 @@ class MockWebSocketConnector {
}

handlers = options;
return MockWebSocket(sentData.add);
return MockWebSocket((data) {
sentData.add(data);
onSend?.call(data);
});
}
}
184 changes: 184 additions & 0 deletions test/participant/fast_publish_test.dart
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>()),
);
});
}
Loading