diff --git a/flutter_sender/lib/main.dart b/flutter_sender/lib/main.dart index 4873d3b..05adfe7 100644 --- a/flutter_sender/lib/main.dart +++ b/flutter_sender/lib/main.dart @@ -65,6 +65,14 @@ class _HomePageState extends State { bool _videoWindowOpen = false; /// 正在拉起窗口并下发投屏请求。 bool _mirrorRequesting = false; + /// 最近一次统计到的累计解码帧数,用于判断是否还在收画面。 + int _lastDecoded = 0; + /// 最近一次收到新帧的时间;超过 [streamingIdleTimeout] 视为已停止推流。 + DateTime? _lastFrameAt; + /// 因关窗而停流只下发一次 stopMirror。 + bool _stopSentForWindowClose = false; + + static const Duration streamingIdleTimeout = Duration(seconds: 2); String? _nativeError; @override @@ -77,7 +85,7 @@ class _HomePageState extends State { } _bridge = SenderBridge(nativeCore: _nativeCore); // 独立窗口被用户关闭 → 停止投屏。 - _bridge.onVideoWindowClosed = _handleVideoWindowClosed; + _bridge.onVideoWindowClosed = _onStreamWindowClosed; _diagnosticLog.start(); _diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}'); _bridge.watchPhase().listen((phase) { @@ -161,6 +169,14 @@ class _HomePageState extends State { if (!mounted || stats.isEmpty) return; final id = stats['textureId']; final windowOpen = stats['windowOpen']; + final decoded = stats['decoded']; + final decodedCount = decoded is int ? decoded : 0; + + // 帧计数变化说明还在收画面;累计值不变就说明推流已停。 + final framesAdvanced = decodedCount != _lastDecoded; + // 原生窗口被用户关掉(方法通道回调丢失时的兜底)。 + final closedByUser = windowOpen is bool && !windowOpen && _videoWindowOpen; + setState(() { _videoStats = stats; if (_textureId == null && id is int && id > 0) { @@ -169,7 +185,18 @@ class _HomePageState extends State { if (windowOpen is bool) { _videoWindowOpen = windowOpen; } + if (framesAdvanced) { + _lastDecoded = decodedCount; + _lastFrameAt = DateTime.now(); + } + if (closedByUser) { + _lastFrameAt = null; + } }); + + if (closedByUser) { + await _onStreamWindowClosed(); + } } /// 请求投屏:先把独立串流窗口打开,再向被控端下发投屏请求。 @@ -180,7 +207,12 @@ class _HomePageState extends State { _snack('请先连接被控端'); return; } - setState(() => _mirrorRequesting = true); + setState(() { + _mirrorRequesting = true; + // 新一轮请求:清掉上一轮「关窗已停流」的标记与旧帧时间。 + _stopSentForWindowClose = false; + _lastFrameAt = null; + }); try { if (!_videoWindowOpen) { final opened = await _bridge.openVideoWindow(); @@ -199,14 +231,23 @@ class _HomePageState extends State { } /// 用户关掉独立串流窗口(点关闭按钮 / 按 ESC)→ 直接停止投屏。 - Future _handleVideoWindowClosed() async { + /// + /// 两个入口都会走到这里:原生的 `windowClosed` 回调,以及轮询 `stats.windowOpen` + /// 发现窗口已关闭的兜底。用 [_stopSentForWindowClose] 去重,避免重复下发。 + Future _onStreamWindowClosed() async { if (!mounted) return; - setState(() => _videoWindowOpen = false); - if (!_phase.isConnected) return; - // 先通知被控端收流,再确保窗口已经关掉(用户已手动关了就无需重复动作)。 - _bridge.sendControl('stopMirror', const {}); - _diagnosticLog.write('video window closed -> stopMirror'); - _snack('串流窗口已关闭,已停止投屏'); + if (_stopSentForWindowClose) return; + _stopSentForWindowClose = true; + + setState(() { + _videoWindowOpen = false; + _lastFrameAt = null; + }); + if (_phase.isConnected) { + _bridge.sendControl('stopMirror', const {}); + _diagnosticLog.write('video window closed -> stopMirror'); + _snack('串流窗口已关闭,已停止投屏'); + } } void _snack(String message) { @@ -654,8 +695,16 @@ class _HomePageState extends State { return decoded is int ? decoded : 0; } - /// 是否已经在接收画面(首页不显示画面,只看状态)。 - bool get _videoStreaming => _videoFrames > 0 && _videoError.isEmpty; + /// 是否正在接收画面。 + /// + /// 三个条件同时满足才算:独立窗口还开着、没有解码错误、最近 [streamingIdleTimeout] + /// 内还有新帧。累计帧数只增不减,单看它会导致关窗后永远显示「接收画面中」。 + bool get _videoStreaming { + if (!_videoWindowOpen || _videoError.isNotEmpty) return false; + final last = _lastFrameAt; + if (last == null) return false; + return DateTime.now().difference(last) < streamingIdleTimeout; + } Widget _buildStage() { return Padding(