Repository navigation
feat: Room.restartIce() to move media off a degraded candidate pair #1241
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
4lxprime
wants to merge
2
commits into
livekit:main
Choose a base branch
from
citadellefr:feat/room-restart-ice
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 @@ | ||
| minor type="added" "`Room.restartIce()` resumes the session on purpose to restart ICE on both peer connections, so media stuck on a TURN relay over TCP/TLS can move back to UDP" |
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,74 @@ | ||
| // 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. | ||
|
|
||
| @Timeout(Duration(seconds: 10)) | ||
| library; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc; | ||
|
|
||
| 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 '../mock/e2e_container.dart'; | ||
| import '../mock/peerconnection_mock.dart'; | ||
| import '../mock/websocket_mock.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| late E2EContainer container; | ||
| late MockWebSocketConnector ws; | ||
|
|
||
| setUp(() async { | ||
| resetMockDataChannels(); | ||
| container = E2EContainer(); | ||
| ws = container.wsConnector; | ||
| await container.connectRoom(); | ||
| }); | ||
|
|
||
| tearDown(() async { | ||
| await container.dispose(); | ||
| }); | ||
|
|
||
| Future<void> until(bool Function() condition) async { | ||
| for (var i = 0; i < 200 && !condition(); i++) { | ||
| await Future<void>.delayed(const Duration(milliseconds: 10)); | ||
| } | ||
| expect(condition(), isTrue); | ||
| } | ||
|
|
||
| test('restartIce() resumes to switch candidates, restarting publisher ICE', () async { | ||
| final publisher = container.engine.publisher!; | ||
| await container.engine.negotiate(); | ||
| await until(() => publisher.pc.signalingState == rtc.RTCSignalingState.RTCSignalingStateHaveLocalOffer); | ||
| ws.onData( | ||
| lk_rtc.SignalResponse( | ||
| answer: lk_rtc.SessionDescription(type: 'answer', sdp: 'remote_answer'), | ||
| ).writeToBuffer(), | ||
| ); | ||
| await until(() => publisher.pc.connectionState == rtc.RTCPeerConnectionState.RTCPeerConnectionStateConnected); | ||
|
|
||
| final previousHandlers = ws.handlers; | ||
| await container.room.restartIce(); | ||
| await until(() => !identical(ws.handlers, previousHandlers)); | ||
|
|
||
| expect(ws.uri!.queryParameters['reconnect'], '1'); | ||
| expect(ws.uri!.queryParameters['reconnect_reason'], '${lk_models.ReconnectReason.RR_SWITCH_CANDIDATE.value}'); | ||
|
|
||
| ws.onData(lk_rtc.SignalResponse(reconnect: lk_rtc.ReconnectResponse()).writeToBuffer()); | ||
| await container.room.events.waitFor<RoomReconnectedEvent>(duration: const Duration(seconds: 5)); | ||
| expect(publisher.restartingIce, isTrue); | ||
| }); | ||
| } |
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
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.
🔴 ICE restart future completes too early
When a connected app awaits
restartIce(), it completes before ICE restarts.handleReconnectonly schedules a timer, so later failures never reach the caller.Learn more
The public method returns the future from handleReconnect, which completes after scheduling a retry timer. The timer later invokes attemptReconnect; that method catches reconnect failures and starts further retries without completing the original future. A caller cannot await ICE restart completion or distinguish success from eventual disconnect.
Example: An app calls
await room.restartIce()and immediately reads the candidate pair. The await completes before the zero-delay timer even opens a new signaling socket, so the old TCP pair is still selected.Recommended fix: Give the requested reconnect a completion future tied to the final successful resume or failure, and return that future from
Room.restartIce(). Preserve the engine's retry behavior and settle the future on disconnect or disposal.Was this helpful? React with 👍 or 👎 to provide feedback.