fix(sender): 串流窗口锁定视频比例,关窗即停流,修正文件传输取 IP

- 独立窗口首帧到达后按视频宽高比自动调整并居中;WM_SIZING 锁定比例,最小 320x180

- 去掉 WS_MAXIMIZEBOX 与双击/F11 全屏,只保留关闭与最小化

- 移除停止投屏按钮;WM_DESTROY 通过 ttstd/video 通道回调 windowClosed,Dart 侧下发 stopMirror

- 文件传输地址优先取设备局域网 IP,取不到时回退控制通道 peerIp,并在文件页提供可编辑 IP 输入
This commit is contained in:
tongtong committed 2026-11-27 10:42:48 +08:00
1 parent d3ff293877
commit d8357a8491
8 files changed
+321 -127

No files matched your search

+12 -1
View File
@@ -15,7 +15,9 @@ class SenderBridge {
NativeCore? nativeCore,
}) : _method = method ?? const MethodChannel('ttstd/sender'),
_events = events ?? const EventChannel('ttstd/sender/events'),
_native = nativeCore;
_native = nativeCore {
_video.setMethodCallHandler(_handleVideoCall);
}
final MethodChannel _method;
final EventChannel _events;
@@ -25,6 +27,15 @@ class SenderBridge {
/// 原生视频解码纹理通道(Windows runner 内嵌插件)。
static const MethodChannel _video = MethodChannel('ttstd/video');
/// 用户关掉独立串流窗口(点关闭按钮 / 按 ESC)时触发,用于停止投屏。
void Function()? onVideoWindowClosed;
Future<void> _handleVideoCall(MethodCall call) async {
if (call.method == 'windowClosed') {
onVideoWindowClosed?.call();
}
}
NativeCore? get nativeCore => _native;
bool get isLocalSignalingRunning => _localSignaling.isRunning;
@@ -20,19 +20,22 @@ class FileBrowserPage extends StatefulWidget {
final int initialPort;
final String initialToken;
/// 用户改了端口/令牌并保存时通知主界面写回设置。
final void Function(int port, String token)? onConfigChanged;
/// 用户改了地址/端口/令牌并保存时通知主界面写回设置。
final void Function(String host, int port, String token)? onConfigChanged;
@override
State<FileBrowserPage> createState() => _FileBrowserPageState();
}
class _FileBrowserPageState extends State<FileBrowserPage> {
late final TextEditingController _hostController =
TextEditingController(text: widget.host);
late final TextEditingController _portController =
TextEditingController(text: widget.initialPort.toString());
late final TextEditingController _tokenController =
TextEditingController(text: widget.initialToken);
late String _host = widget.host;
late int _port = widget.initialPort;
late String _token = widget.initialToken;
@@ -58,15 +61,15 @@ class _FileBrowserPageState extends State<FileBrowserPage> {
}
Future<void> _connect() async {
if (widget.host.isEmpty) {
setState(() => _error = '还没有选中设备,请先连接被控端');
if (_host.isEmpty || _host == '127.0.0.1') {
setState(() => _error = '还没有拿到被控端的局域网 IP,请在上方「IP」里手动填写(例如 192.168.1.23)');
return;
}
setState(() {
_loading = true;
_error = null;
});
final client = AndroidFileClient(host: widget.host, port: _port, token: _token);
final client = AndroidFileClient(host: _host, port: _port, token: _token);
try {
final info = await client.info();
final directory = await client.browse('');
@@ -224,13 +227,15 @@ class _FileBrowserPageState extends State<FileBrowserPage> {
}
void _saveConfig() {
final host = _hostController.text.trim();
final port = int.tryParse(_portController.text.trim()) ?? _port;
final token = _tokenController.text.trim();
setState(() {
_host = host;
_port = port;
_token = token;
});
widget.onConfigChanged?.call(port, token);
widget.onConfigChanged?.call(host, port, token);
_toast('参数已保存');
_connect();
}
@@ -256,7 +261,7 @@ class _FileBrowserPageState extends State<FileBrowserPage> {
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.host.isEmpty ? '文件传输' : '${widget.host}:$_port'),
Text(_host.isEmpty ? '文件传输' : '$_host:$_port'),
if (_alias.isNotEmpty)
Text(_alias, style: theme.textTheme.bodySmall),
],
@@ -306,6 +311,18 @@ class _FileBrowserPageState extends State<FileBrowserPage> {
const SizedBox(height: 8),
Row(
children: [
Expanded(
flex: 2,
child: TextField(
controller: _hostController,
decoration: const InputDecoration(
labelText: 'IP',
hintText: '192.168.1.23',
isDense: true,
),
),
),
const SizedBox(width: 10),
Expanded(
child: TextField(
controller: _portController,
@@ -316,9 +333,12 @@ class _FileBrowserPageState extends State<FileBrowserPage> {
),
),
),
const SizedBox(width: 12),
],
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
flex: 2,
child: TextField(
controller: _tokenController,
decoration: const InputDecoration(
@@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
/// 串流控制卡片。
///
/// 主界面不渲染投屏画面,画面统一显示在独立原生窗口里;这里只负责
/// 「请求投屏 / 停止投屏」与窗口显隐、状态展示。
/// 主界面不渲染投屏画面,画面统一显示在独立原生窗口里;窗口尺寸按视频比例自动适配,
/// 关闭窗口即停止投屏,所以这里只保留「请求投屏」一个动作。
class MirrorBar extends StatelessWidget {
const MirrorBar({
super.key,
@@ -15,8 +15,6 @@ class MirrorBar extends StatelessWidget {
required this.frames,
required this.error,
required this.onRequest,
required this.onStop,
required this.onToggleWindow,
});
final bool connected;
@@ -28,13 +26,10 @@ class MirrorBar extends StatelessWidget {
final String error;
final VoidCallback onRequest;
final VoidCallback onStop;
final VoidCallback onToggleWindow;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final status = _statusText();
final statusColor = _statusColor(theme);
return Card(
@@ -57,17 +52,13 @@ class MirrorBar extends StatelessWidget {
borderRadius: BorderRadius.circular(10),
),
child: Text(
status,
_statusText(),
style: theme.textTheme.labelSmall?.copyWith(color: statusColor),
),
),
const Spacer(),
if (windowOpen)
IconButton(
tooltip: '显示/隐藏串流窗口',
onPressed: onToggleWindow,
icon: const Icon(Icons.picture_in_picture_alt),
),
Text('串流窗口已打开', style: theme.textTheme.labelSmall),
],
),
const SizedBox(height: 4),
@@ -78,24 +69,15 @@ class MirrorBar extends StatelessWidget {
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 12),
Row(
children: [
Expanded(
child: FilledButton.icon(
onPressed: connected && !busy ? onRequest : null,
icon: const Icon(Icons.play_circle_outline),
label: const Text('请求投屏'),
),
SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: connected && !busy && !streaming ? onRequest : null,
icon: Icon(
streaming ? Icons.cast_connected : Icons.play_circle_outline,
),
const SizedBox(width: 10),
Expanded(
child: OutlinedButton.icon(
onPressed: onStop,
icon: const Icon(Icons.stop_circle_outlined),
label: const Text('停止投屏'),
),
),
],
label: Text(streaming ? '投屏中 · 关闭窗口即停止' : '请求投屏'),
),
),
],
),
@@ -124,9 +106,9 @@ class MirrorBar extends StatelessWidget {
if (!connected) return '先在上方连接被控端,再请求投屏';
if (streaming) {
final size = resolution.isEmpty ? '未知分辨率' : resolution;
return '$size · 已接收 $frames 帧 · 画面在独立窗口显示';
return '$size · 已接收 $frames 帧 · 关闭独立窗口会立即停止投屏';
}
if (windowOpen) return '已向被控端发送投屏请求,请在其弹窗中点击「允许」';
return '点击「请求投屏」后会弹出独立窗口播放画面';
return '点击「请求投屏」会按视频比例弹出独立窗口播放画面';
}
}
+34 -31
View File
@@ -76,6 +76,8 @@ class _HomePageState extends State<HomePage> {
_nativeError = error.toString();
}
_bridge = SenderBridge(nativeCore: _nativeCore);
// 独立窗口被用户关闭 → 停止投屏。
_bridge.onVideoWindowClosed = _handleVideoWindowClosed;
_diagnosticLog.start();
_diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}');
_bridge.watchPhase().listen((phase) {
@@ -196,32 +198,15 @@ class _HomePageState extends State<HomePage> {
}
}
/// 停止投屏:通知被控端结束推流,并关闭独立画面窗口。
Future<void> _stopMirror() async {
if (_phase.isConnected) {
_sendControl('stopMirror', const {});
_diagnosticLog.write('mirror stop sent');
}
await _bridge.closeVideoWindow();
/// 用户关掉独立串流窗口(点关闭按钮 / 按 ESC)→ 直接停止投屏。
Future<void> _handleVideoWindowClosed() async {
if (!mounted) return;
setState(() => _videoWindowOpen = false);
}
/// 独立窗口显隐(不停流)。
Future<void> _toggleVideoWindow() async {
if (_videoWindowOpen) {
await _bridge.closeVideoWindow();
if (!mounted) return;
setState(() => _videoWindowOpen = false);
return;
}
final opened = await _bridge.openVideoWindow();
if (!mounted) return;
if (!opened) {
_snack('当前平台不支持独立串流窗口');
return;
}
setState(() => _videoWindowOpen = true);
if (!_phase.isConnected) return;
// 先通知被控端收流,再确保窗口已经关掉(用户已手动关了就无需重复动作)。
_bridge.sendControl('stopMirror', const {});
_diagnosticLog.write('video window closed -> stopMirror');
_snack('串流窗口已关闭,已停止投屏');
}
void _snack(String message) {
@@ -248,7 +233,7 @@ class _HomePageState extends State<HomePage> {
Text('文件传输', style: Theme.of(context).textTheme.titleMedium),
const Spacer(),
Text(
host.isEmpty ? '未选设备' : '$host:${_settings.filePort}',
host.isEmpty ? '未拿到被控端 IP' : '$host:${_settings.filePort}',
style: Theme.of(context).textTheme.bodySmall,
),
],
@@ -271,19 +256,39 @@ class _HomePageState extends State<HomePage> {
);
}
/// 文件服务要连的被控端地址。
///
/// 优先用设备列表里的局域网 IP;设备信息没带 IP 时退回到控制通道的对端 IP
/// (`peerIp`),避免出现「连 127.0.0.1」这种必然失败的情况。
String get _fileHost {
final candidate = _selected?.ip.trim() ?? '';
if (_isUsableHost(candidate)) return candidate;
final peer = (_demoStatus['peerIp'] as String? ?? '').trim();
if (_isUsableHost(peer)) return peer;
return candidate;
}
static bool _isUsableHost(String host) {
if (host.isEmpty) return false;
return host != '127.0.0.1' && host != '0.0.0.0' && host != '::1' && host != 'localhost';
}
Future<void> _openFileBrowser() async {
final device = _selected;
if (device == null) {
if (_selected == null) {
_snack('请先选中被控端设备');
return;
}
final host = _fileHost;
if (!_isUsableHost(host)) {
_snack('还没拿到被控端 IP:请先连接设备,或在文件页手动填写 IP');
}
await Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => FileBrowserPage(
host: device.ip,
host: host,
initialPort: _settings.filePort,
initialToken: _settings.fileToken,
onConfigChanged: (port, token) {
onConfigChanged: (host, port, token) {
final updated = _settings.copyWith(filePort: port, fileToken: token);
setState(() => _settings = updated);
_bridge.saveSettings(updated);
@@ -674,8 +679,6 @@ class _HomePageState extends State<HomePage> {
frames: _videoFrames,
error: _videoError,
onRequest: _requestMirror,
onStop: _stopMirror,
onToggleWindow: _toggleVideoWindow,
),
const SizedBox(height: 12),
ControlToolbar(