fix: 修正投屏授权状态与窗口生命周期,避免串流状态错乱
This commit is contained in:
1 parent
a0b77a3e7a
commit
4c9d006d11
6 files changed
+134
-90
No files matched your search
@@ -10,6 +10,7 @@ class MirrorBar extends StatelessWidget {
|
||||
required this.connected,
|
||||
required this.streaming,
|
||||
required this.windowOpen,
|
||||
required this.granted,
|
||||
required this.busy,
|
||||
required this.resolution,
|
||||
required this.frames,
|
||||
@@ -20,6 +21,7 @@ class MirrorBar extends StatelessWidget {
|
||||
final bool connected;
|
||||
final bool streaming;
|
||||
final bool windowOpen;
|
||||
final bool granted;
|
||||
final bool busy;
|
||||
final String resolution;
|
||||
final int frames;
|
||||
@@ -72,7 +74,7 @@ class MirrorBar extends StatelessWidget {
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: FilledButton.icon(
|
||||
onPressed: connected && !busy && !streaming ? onRequest : null,
|
||||
onPressed: connected && !busy && !windowOpen ? onRequest : null,
|
||||
icon: Icon(
|
||||
streaming ? Icons.cast_connected : Icons.play_circle_outline,
|
||||
),
|
||||
@@ -86,28 +88,30 @@ class MirrorBar extends StatelessWidget {
|
||||
}
|
||||
|
||||
String _statusText() {
|
||||
if (error.isNotEmpty) return '异常';
|
||||
if (streaming) return '接收画面中';
|
||||
if (error.isNotEmpty) return '异常';
|
||||
if (windowOpen && granted) return '已允许,等待画面';
|
||||
if (windowOpen) return '等待被控端确认';
|
||||
if (connected) return '已连接,未投屏';
|
||||
return '未连接';
|
||||
}
|
||||
|
||||
Color _statusColor(ThemeData theme) {
|
||||
if (error.isNotEmpty) return theme.colorScheme.error;
|
||||
if (streaming) return Colors.green;
|
||||
if (error.isNotEmpty) return theme.colorScheme.error;
|
||||
if (windowOpen) return Colors.orange;
|
||||
if (connected) return theme.colorScheme.primary;
|
||||
return theme.colorScheme.outline;
|
||||
}
|
||||
|
||||
String _detailText() {
|
||||
if (error.isNotEmpty) return error;
|
||||
if (!connected) return '先在上方连接被控端,再请求投屏';
|
||||
if (streaming) {
|
||||
final size = resolution.isEmpty ? '未知分辨率' : resolution;
|
||||
return '$size · 已接收 $frames 帧 · 关闭独立窗口会立即停止投屏';
|
||||
}
|
||||
if (error.isNotEmpty) return error;
|
||||
if (windowOpen && granted) return '被控端已允许投屏,正在等待首帧';
|
||||
if (windowOpen) return '已向被控端发送投屏请求,请在其弹窗中点击「允许」';
|
||||
return '点击「请求投屏」会按视频比例弹出独立窗口播放画面';
|
||||
}
|
||||
|
||||
@@ -85,16 +85,14 @@ class _HomePageState extends State<HomePage> {
|
||||
bool _videoWindowOpen = false;
|
||||
/// 正在拉起窗口并下发投屏请求。
|
||||
bool _mirrorRequesting = false;
|
||||
/// 最近一次统计到的累计解码帧数,用于判断是否还在收画面。
|
||||
int _lastDecoded = 0;
|
||||
/// 最近一次收到新帧的时间;超过 [streamingIdleTimeout] 视为已停止推流。
|
||||
DateTime? _lastFrameAt;
|
||||
bool _mirrorGranted = false;
|
||||
bool _videoStreaming = false;
|
||||
int _videoWindowGeneration = 0;
|
||||
/// 因关窗而停流只下发一次 stopMirror。
|
||||
bool _stopSentForWindowClose = false;
|
||||
/// 最近一次同步过的剪贴板文本(收/发都记),用来避免两端来回回送同一条内容。
|
||||
String? _lastClipboardText;
|
||||
|
||||
static const Duration streamingIdleTimeout = Duration(seconds: 2);
|
||||
String? _nativeError;
|
||||
|
||||
@override
|
||||
@@ -199,16 +197,22 @@ class _HomePageState extends State<HomePage> {
|
||||
if (connected && keyEstablished) {
|
||||
_connectWatchdog?.cancel();
|
||||
_phase = ConnectionPhase.direct;
|
||||
} else if (sessionEnded) {
|
||||
// 中继线程退出后 status 不再有进行中的会话,别把界面卡在「连接中」。
|
||||
_phase = ConnectionPhase.idle;
|
||||
} else if (wasConnected) {
|
||||
// 直连监听仍在运行时继续等设备重连。
|
||||
_phase = running && !sessionEnded
|
||||
? ConnectionPhase.probing
|
||||
: ConnectionPhase.idle;
|
||||
} else if ((running || relay) &&
|
||||
_phase != ConnectionPhase.idle &&
|
||||
_phase != ConnectionPhase.failed) {
|
||||
_phase = ConnectionPhase.probing;
|
||||
}
|
||||
});
|
||||
if (sessionEnded) _snack('中继会话已断开');
|
||||
if (wasConnected && !_phase.isConnected && _videoWindowOpen) {
|
||||
unawaited(_abortMirror('被控端已断开,串流窗口已关闭'));
|
||||
} else if (sessionEnded) {
|
||||
_snack('中继会话已断开');
|
||||
}
|
||||
// 刚连上时主动下发一次会话配置,顺带把权限等级请求发过去;
|
||||
// 被控端会用 msgType=10 回送裁剪后的有效等级。
|
||||
if (!wasConnected && _phase.isConnected) {
|
||||
@@ -256,8 +260,10 @@ class _HomePageState extends State<HomePage> {
|
||||
/// 4=已停止 5=出错。以前没有这条通道,被控端拒绝后主控端只能干等画面。
|
||||
void _handleMirrorEvent(int action, String text) {
|
||||
_diagnosticLog.write('mirror event action=$action text=$text');
|
||||
if (!_videoWindowOpen || _stopSentForWindowClose) return;
|
||||
switch (action) {
|
||||
case 2:
|
||||
setState(() => _mirrorGranted = true);
|
||||
_snack('被控端已允许投屏');
|
||||
case 3:
|
||||
unawaited(_abortMirror(text.isEmpty ? '被控端拒绝了投屏请求' : '被控端拒绝了投屏请求:$text'));
|
||||
@@ -274,15 +280,19 @@ class _HomePageState extends State<HomePage> {
|
||||
/// 不能再给对端补发一条 stopMirror。
|
||||
Future<void> _abortMirror(String message) async {
|
||||
_stopSentForWindowClose = true;
|
||||
if (_videoWindowOpen) {
|
||||
await _bridge.closeVideoWindow();
|
||||
}
|
||||
if (!mounted) return;
|
||||
final close = _bridge.closeVideoWindow();
|
||||
_clearMirrorState();
|
||||
await close;
|
||||
if (mounted && !_videoWindowOpen) _snack(message);
|
||||
}
|
||||
|
||||
void _clearMirrorState() {
|
||||
setState(() {
|
||||
_videoWindowGeneration++;
|
||||
_videoWindowOpen = false;
|
||||
_lastFrameAt = null;
|
||||
_mirrorGranted = false;
|
||||
_videoStreaming = false;
|
||||
});
|
||||
_snack(message);
|
||||
}
|
||||
|
||||
/// 被控端回送的权限等级(msgType=10,payload 是 JSON)。
|
||||
@@ -390,15 +400,14 @@ class _HomePageState extends State<HomePage> {
|
||||
/// 轮询原生解码统计:纹理 ID、分辨率、PTS 与失败原因。
|
||||
Future<void> _refreshVideoStats() async {
|
||||
if (!mounted) return;
|
||||
final generation = _videoWindowGeneration;
|
||||
final stats = await _bridge.videoStats();
|
||||
if (!mounted || stats.isEmpty) return;
|
||||
if (!mounted || generation != _videoWindowGeneration || 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 windowFrames = stats['windowFrames'];
|
||||
// 原生窗口被用户关掉(方法通道回调丢失时的兜底)。
|
||||
final closedByUser = windowOpen is bool && !windowOpen && _videoWindowOpen;
|
||||
|
||||
@@ -407,15 +416,11 @@ class _HomePageState extends State<HomePage> {
|
||||
if (_textureId == null && id is int && id > 0) {
|
||||
_textureId = id;
|
||||
}
|
||||
if (windowOpen is bool) {
|
||||
_videoWindowOpen = windowOpen;
|
||||
}
|
||||
if (framesAdvanced) {
|
||||
_lastDecoded = decodedCount;
|
||||
_lastFrameAt = DateTime.now();
|
||||
}
|
||||
if (closedByUser) {
|
||||
_lastFrameAt = null;
|
||||
if (_videoWindowOpen &&
|
||||
windowOpen == true &&
|
||||
windowFrames is int &&
|
||||
windowFrames > 0) {
|
||||
_videoStreaming = true;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -428,29 +433,34 @@ class _HomePageState extends State<HomePage> {
|
||||
///
|
||||
/// 被控端会弹窗确认,确认后才开始推流;这段时间窗口显示「等待画面」。
|
||||
Future<void> _requestMirror() async {
|
||||
if (_videoWindowOpen || _mirrorRequesting) return;
|
||||
if (!_phase.isConnected) {
|
||||
_snack('请先连接被控端');
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_mirrorRequesting = true;
|
||||
// 新一轮请求:清掉上一轮「关窗已停流」的标记与旧帧时间。
|
||||
_stopSentForWindowClose = false;
|
||||
_lastFrameAt = null;
|
||||
});
|
||||
final generation = _videoWindowGeneration;
|
||||
try {
|
||||
if (!_videoWindowOpen) {
|
||||
final size = _deviceScreenSize();
|
||||
final opened = await _bridge.openVideoWindow(
|
||||
width: size?.$1,
|
||||
height: size?.$2,
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (!opened) {
|
||||
_snack('当前平台不支持独立串流窗口');
|
||||
return;
|
||||
}
|
||||
setState(() => _videoWindowOpen = true);
|
||||
final size = _deviceScreenSize();
|
||||
final opened = await _bridge.openVideoWindow(
|
||||
width: size?.$1,
|
||||
height: size?.$2,
|
||||
);
|
||||
if (!mounted || generation != _videoWindowGeneration) return;
|
||||
if (!opened) {
|
||||
_snack('当前平台不支持独立串流窗口');
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_videoWindowGeneration++;
|
||||
_videoWindowOpen = true;
|
||||
});
|
||||
if (!_phase.isConnected) {
|
||||
await _abortMirror('被控端已断开,串流窗口已关闭');
|
||||
return;
|
||||
}
|
||||
// 先下发会话配置,被控端据此决定编码器参数后再开流。
|
||||
_applySessionConfig();
|
||||
@@ -481,10 +491,7 @@ class _HomePageState extends State<HomePage> {
|
||||
if (_stopSentForWindowClose) return;
|
||||
_stopSentForWindowClose = true;
|
||||
|
||||
setState(() {
|
||||
_videoWindowOpen = false;
|
||||
_lastFrameAt = null;
|
||||
});
|
||||
_clearMirrorState();
|
||||
if (_phase.isConnected) {
|
||||
_bridge.sendControl('stopMirror', const {});
|
||||
_diagnosticLog.write('video window closed -> stopMirror');
|
||||
@@ -966,9 +973,9 @@ class _HomePageState extends State<HomePage> {
|
||||
// 断开时连串流窗口一起关掉,否则重连后窗口还挂在那儿显示上一路的旧帧。
|
||||
if (_videoWindowOpen) {
|
||||
_stopSentForWindowClose = true;
|
||||
await _bridge.closeVideoWindow();
|
||||
_videoWindowOpen = false;
|
||||
_lastFrameAt = null;
|
||||
final close = _bridge.closeVideoWindow();
|
||||
_clearMirrorState();
|
||||
await close;
|
||||
}
|
||||
await _bridge.disconnect();
|
||||
if (!mounted) return;
|
||||
@@ -1252,21 +1259,10 @@ class _HomePageState extends State<HomePage> {
|
||||
String get _videoError => (_videoStats['error'] as String? ?? '').trim();
|
||||
|
||||
int get _videoFrames {
|
||||
final decoded = _videoStats['decoded'];
|
||||
final decoded = _videoStats['windowFrames'];
|
||||
return decoded is int ? decoded : 0;
|
||||
}
|
||||
|
||||
/// 是否正在接收画面。
|
||||
///
|
||||
/// 三个条件同时满足才算:独立窗口还开着、没有解码错误、最近 [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(
|
||||
padding: const EdgeInsets.all(16),
|
||||
@@ -1284,6 +1280,7 @@ class _HomePageState extends State<HomePage> {
|
||||
connected: _phase.isConnected,
|
||||
streaming: _videoStreaming,
|
||||
windowOpen: _videoWindowOpen,
|
||||
granted: _mirrorGranted,
|
||||
busy: _mirrorRequesting,
|
||||
resolution: _videoResolution,
|
||||
frames: _videoFrames,
|
||||
|
||||
Reference in new issue
Block a user