feat: ice断开返回配置页面
This commit is contained in:
@@ -27,6 +27,9 @@ class RemoteController {
|
||||
/// 连接断开。
|
||||
void Function()? onDisconnected;
|
||||
|
||||
/// ICE 连接断开(用于提示用户并返回连接设置)。
|
||||
void Function(String message)? onIceDisconnected;
|
||||
|
||||
/// 远端视频渲染器就绪。
|
||||
void Function(RTCVideoRenderer renderer)? onRemoteStream;
|
||||
|
||||
@@ -72,6 +75,7 @@ class RemoteController {
|
||||
_webRtc!.onDisconnected = () {
|
||||
onDisconnected?.call();
|
||||
};
|
||||
_webRtc!.onIceDisconnected = (message) => onIceDisconnected?.call(message);
|
||||
_webRtc!.onRemoteStream = (renderer) => onRemoteStream?.call(renderer);
|
||||
_webRtc!.initialize().catchError((e) {
|
||||
onStatusChanged?.call('状态: 连接失败 - $e');
|
||||
|
||||
@@ -43,6 +43,9 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
RemoteController? _controller;
|
||||
RTCVideoRenderer? _renderer;
|
||||
|
||||
/// 用于在任意位置(含回调中)弹出提示。
|
||||
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||
|
||||
bool _connected = false;
|
||||
String _status = '状态: 已停止';
|
||||
String _stats = '';
|
||||
@@ -93,6 +96,9 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
setState(() => _connected = false);
|
||||
_setStatus('状态: 远端已断开');
|
||||
};
|
||||
_controller!.onIceDisconnected = (message) {
|
||||
_onIceDisconnected(message);
|
||||
};
|
||||
_controller!.onRemoteStream = (renderer) {
|
||||
setState(() => _renderer = renderer);
|
||||
renderer.addListener(_onRendererUpdate);
|
||||
@@ -110,6 +116,20 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
}
|
||||
}
|
||||
|
||||
/// ICE 断开:提示用户并自动返回连接设置面板。
|
||||
Future<void> _onIceDisconnected(String message) async {
|
||||
final scaffoldCtx = _scaffoldKey.currentState?.context;
|
||||
if (mounted && scaffoldCtx != null) {
|
||||
ScaffoldMessenger.of(scaffoldCtx).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
}
|
||||
await _disconnect();
|
||||
}
|
||||
|
||||
Future<void> _disconnect() async {
|
||||
await _controller?.disconnect();
|
||||
_controller = null;
|
||||
@@ -125,6 +145,7 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
key: _scaffoldKey,
|
||||
appBar: AppBar(title: const Text('WebRTC 控制端')),
|
||||
body: _connected ? _buildControlPanel() : _buildSetupPanel(),
|
||||
);
|
||||
|
||||
@@ -29,6 +29,9 @@ class WebRtcController {
|
||||
void Function(String error)? onConnectionFailed;
|
||||
void Function()? onDisconnected;
|
||||
|
||||
/// ICE 连接断开(如网络中断、被控端退出),用于提示用户并返回连接设置。
|
||||
void Function(String message)? onIceDisconnected;
|
||||
|
||||
/// 远端视频流就绪(renderer 已绑定视频轨道)。
|
||||
void Function(RTCVideoRenderer renderer)? onRemoteStream;
|
||||
|
||||
@@ -124,7 +127,10 @@ class WebRtcController {
|
||||
print('ICE connection state: $state');
|
||||
if (state == RTCIceConnectionState.RTCIceConnectionStateConnected) {
|
||||
onConnectionEstablished?.call();
|
||||
} else if (state == RTCIceConnectionState.RTCIceConnectionStateDisconnected) {
|
||||
onIceDisconnected?.call('ICE 连接已断开');
|
||||
} else if (state == RTCIceConnectionState.RTCIceConnectionStateFailed) {
|
||||
onConnectionFailed?.call('ICE 连接失败');
|
||||
onDisconnected?.call();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user