diff --git a/.changes/room-restart-ice b/.changes/room-restart-ice new file mode 100644 index 000000000..efd964375 --- /dev/null +++ b/.changes/room-restart-ice @@ -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" diff --git a/lib/src/core/room.dart b/lib/src/core/room.dart index 5731309e1..a0227be55 100644 --- a/lib/src/core/room.dart +++ b/lib/src/core/room.dart @@ -50,6 +50,7 @@ import '../track/local/video.dart'; import '../track/track.dart'; import '../track/web/_audio_api.dart' if (dart.library.js_interop) '../track/web/_audio_html.dart' as audio; import '../types/data_stream.dart'; +import '../types/internal.dart'; import '../types/other.dart'; import '../types/rpc.dart'; import '../types/transcription_segment.dart'; @@ -823,6 +824,16 @@ class Room extends DisposableChangeNotifier with EventsEmittable { } }); + /// Resumes the session on purpose, restarting ICE on both peer connections. + /// + /// Once UDP is blocked, media falls back to a TURN relay over TCP/TLS and + /// stays there after UDP comes back. An app that sees from the stats that a + /// better path is available again can call this to let ICE find it. + Future restartIce() => engine.handleReconnect( + ClientDisconnectReason.reconnect, + reconnectReason: lk_models.ReconnectReason.RR_SWITCH_CANDIDATE, + ); + /// Disconnects from the room, notifying server of disconnection. Future disconnect() async { final bool isPendingReconnect = engine.isPendingReconnect; diff --git a/test/core/restart_ice_test.dart b/test/core/restart_ice_test.dart new file mode 100644 index 000000000..e05807b15 --- /dev/null +++ b/test/core/restart_ice_test.dart @@ -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 until(bool Function() condition) async { + for (var i = 0; i < 200 && !condition(); i++) { + await Future.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(duration: const Duration(seconds: 5)); + expect(publisher.restartingIce, isTrue); + }); +} diff --git a/test/mock/peerconnection_mock.dart b/test/mock/peerconnection_mock.dart index 5e31ad29e..2cc1615f1 100644 --- a/test/mock/peerconnection_mock.dart +++ b/test/mock/peerconnection_mock.dart @@ -303,8 +303,7 @@ a=rtpmap:32 MPV/90000 ]) async => MockPeerConnection(); @override - // TODO: implement restartIce - Future restartIce() => throw UnimplementedError(); + Future restartIce() async {} } class MockRtpSender implements RTCRtpSender {