fix(sender): 修复断开后重连一直超时,串流窗口按被控端比例开局

- rust_core: stop 先关闭控制连接再 join 监听线程,保证端口真正释放
- rust_core: start 前回收残留线程,按 AddrInUse 重试绑定,失败不再静默
- rust_core: 监听套接字异常时把 running 置回 false;半开握手读超时缩到 1.2s
- rust_core: 新增 demo_reconnect 回归测试(stop 后立刻 start 仍可接入)
- flutter_sender: connect 检查本机监听结果,失败直接提示端口被占用
- flutter_sender: 断开时一并关闭独立串流窗口,重连不再残留旧帧
- flutter_sender: openWindow 传入被控端分辨率,窗口开局即按竖屏比例居中
This commit is contained in:
tongtong committed 2026-11-27 12:06:57 +08:00
1 parent 77ce882397
commit 32e7ca26d8
7 files changed
+269 -33

No files matched your search

+12 -3
View File
@@ -118,7 +118,10 @@ class SenderBridge {
final native = _native;
if (native != null) {
// 先确保本机控制/媒体端口在监听,再让被控端立刻拨号(不必等它下次广播)。
native.startDemoServer();
// 监听失败必须显式报错:否则被控端拨号会没人接,界面只能笼统地等到超时。
if (!native.startDemoServer()) {
throw Exception('本机监听启动失败(TCP 41001 / UDP 41000、41002 可能被占用)');
}
if (device.ip.isNotEmpty) {
native.discoveryConnect(device.ip);
}
@@ -281,9 +284,15 @@ class SenderBridge {
}
/// 把串流画面弹出为独立原生窗口(Windows);返回是否成功。
Future<bool> openVideoWindow() async {
///
/// [width] / [height] 是被控端屏幕尺寸提示:传了就按该比例开局,
/// 不再先弹出一个 16:9 的大窗口等首帧再缩回去。
Future<bool> openVideoWindow({int? width, int? height}) async {
try {
final opened = await _video.invokeMethod<bool>('openWindow');
final arguments = (width != null && height != null && width > 0 && height > 0)
? <String, dynamic>{'width': width, 'height': height}
: null;
final opened = await _video.invokeMethod<bool>('openWindow', arguments);
return opened ?? false;
} on MissingPluginException {
return false;
+23 -1
View File
@@ -215,7 +215,11 @@ class _HomePageState extends State<HomePage> {
});
try {
if (!_videoWindowOpen) {
final opened = await _bridge.openVideoWindow();
final size = _deviceScreenSize();
final opened = await _bridge.openVideoWindow(
width: size?.$1,
height: size?.$2,
);
if (!mounted) return;
if (!opened) {
_snack('当前平台不支持独立串流窗口');
@@ -230,6 +234,17 @@ class _HomePageState extends State<HomePage> {
}
}
/// 被控端屏幕尺寸(发现信标里的 `res`,如 "1080x2400");拿不到时返回 null。
(int, int)? _deviceScreenSize() {
final text = (_selected?.resolution ?? '').trim();
final match = RegExp(r'^(\d+)\s*[xX*]\s*(\d+)$').firstMatch(text);
if (match == null) return null;
final width = int.tryParse(match.group(1)!);
final height = int.tryParse(match.group(2)!);
if (width == null || height == null || width <= 0 || height <= 0) return null;
return (width, height);
}
/// 用户关掉独立串流窗口(点关闭按钮 / 按 ESC)→ 直接停止投屏。
///
/// 两个入口都会走到这里:原生的 `windowClosed` 回调,以及轮询 `stats.windowOpen`
@@ -428,6 +443,13 @@ class _HomePageState extends State<HomePage> {
Future<void> _disconnect() async {
_diagnosticLog.write('disconnect requested');
_connectWatchdog?.cancel();
// 断开时连串流窗口一起关掉,否则重连后窗口还挂在那儿显示上一路的旧帧。
if (_videoWindowOpen) {
_stopSentForWindowClose = true;
await _bridge.closeVideoWindow();
_videoWindowOpen = false;
_lastFrameAt = null;
}
await _bridge.disconnect();
if (!mounted) return;
setState(() => _phase = ConnectionPhase.idle);
@@ -895,8 +895,30 @@ class VideoTexturePlugin : public flutter::Plugin {
}
if (call.method_name() == "openWindow") {
auto store = frames_;
// Flutter 侧带上被控端屏幕尺寸,窗口首帧之前就能按正确比例开好。
int hint_width = 0;
int hint_height = 0;
if (const auto* arguments =
std::get_if<flutter::EncodableMap>(call.arguments())) {
auto read_int = [arguments](const char* key) -> int {
const auto entry = arguments->find(flutter::EncodableValue(key));
if (entry == arguments->end()) {
return 0;
}
if (const auto* value = std::get_if<int32_t>(&entry->second)) {
return *value;
}
if (const auto* value = std::get_if<int64_t>(&entry->second)) {
return static_cast<int>(*value);
}
return 0;
};
hint_width = read_int("width");
hint_height = read_int("height");
}
const bool opened = video_window_.Open(
[store](int* width, int* height) { return store->Snapshot(width, height); });
[store](int* width, int* height) { return store->Snapshot(width, height); },
hint_width, hint_height);
result->Success(flutter::EncodableValue(opened));
return;
}
+60 -17
View File
@@ -16,6 +16,33 @@ constexpr UINT kMsgApplyAspect = WM_APP + 0x52;
constexpr int kMinClientWidth = 320;
constexpr int kMinClientHeight = 180;
// 窗口最多占工作区的这个比例,竖屏串流也不会铺满整个屏幕。
constexpr double kMaxWorkAreaFraction = 0.8;
// 未知被控端尺寸时的兜底比例(常见手机竖屏 9:16)。
constexpr double kDefaultAspect = 9.0 / 16.0;
// 按比例算出「不超过工作区 80%、不小于最小值」的客户区尺寸。
void FitClientSize(double aspect, const RECT& work, int* client_width,
int* client_height) {
const int work_width = (std::max)(1, static_cast<int>(work.right - work.left));
const int work_height =
(std::max)(1, static_cast<int>(work.bottom - work.top));
const int max_width = static_cast<int>(work_width * kMaxWorkAreaFraction);
const int max_height = static_cast<int>(work_height * kMaxWorkAreaFraction);
int width = max_height > 0
? static_cast<int>(static_cast<double>(max_height) * aspect)
: max_height;
int height = max_height;
if (width > max_width) {
width = max_width;
height = static_cast<int>(static_cast<double>(width) / aspect);
}
*client_width = (std::max)(width, kMinClientWidth);
*client_height = (std::max)(height, kMinClientHeight);
}
} // namespace
VideoWindow::VideoWindow() = default;
@@ -24,7 +51,7 @@ VideoWindow::~VideoWindow() {
Close();
}
bool VideoWindow::Open(FrameProvider provider) {
bool VideoWindow::Open(FrameProvider provider, int hint_width, int hint_height) {
provider_ = std::move(provider);
if (hwnd_ != nullptr) {
ShowWindow(hwnd_, SW_SHOWNORMAL);
@@ -51,20 +78,42 @@ bool VideoWindow::Open(FrameProvider provider) {
// 不提供最大化按钮:画面窗口只按视频比例缩放。
const DWORD style = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX;
RECT rect = {0, 0, 1280, 720};
// 被控端屏幕尺寸已知时直接按它的比例开局,竖屏手机不会被开成横向大窗。
double aspect = kDefaultAspect;
if (hint_width > 0 && hint_height > 0) {
aspect = static_cast<double>(hint_width) / static_cast<double>(hint_height);
}
RECT work = {0, 0, 1280, 720};
if (!SystemParametersInfoW(SPI_GETWORKAREA, 0, &work, 0)) {
work = RECT{0, 0, GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN)};
}
int client_width = 0;
int client_height = 0;
FitClientSize(aspect, work, &client_width, &client_height);
RECT rect = {0, 0, client_width, client_height};
AdjustWindowRect(&rect, style, FALSE);
HWND hwnd = CreateWindowExW(
0, kWindowClass, kWindowTitle, style, CW_USEDEFAULT, CW_USEDEFAULT,
rect.right - rect.left, rect.bottom - rect.top, nullptr, nullptr, instance,
this);
const int window_width = rect.right - rect.left;
const int window_height = rect.bottom - rect.top;
const int work_width = work.right - work.left;
const int work_height = work.bottom - work.top;
const int x = work.left + (std::max)(0, (work_width - window_width) / 2);
const int y = work.top + (std::max)(0, (work_height - window_height) / 2);
HWND hwnd = CreateWindowExW(0, kWindowClass, kWindowTitle, style, x, y,
window_width, window_height, nullptr, nullptr,
instance, this);
if (hwnd == nullptr) {
provider_ = nullptr;
return false;
}
hwnd_ = hwnd;
aspect_ratio_ = 0.0;
applied_aspect_ = 0.0;
aspect_ratio_ = aspect;
// 预先记下已应用的宽高比:首帧与预期一致时就不会再跳一次尺寸。
applied_aspect_ = aspect;
open_.store(true);
ShowWindow(hwnd_, SW_SHOWNORMAL);
UpdateWindow(hwnd_);
@@ -258,15 +307,9 @@ void VideoWindow::ApplyAspectToWindow(double aspect) {
const int work_width = work.right - work.left;
const int work_height = work.bottom - work.top;
int client_width = work_width * 4 / 5;
int client_height = static_cast<int>(client_width / aspect);
const int max_client_height = work_height * 4 / 5;
if (client_height > max_client_height) {
client_height = max_client_height;
client_width = static_cast<int>(client_height * aspect);
}
client_width = (std::max)(client_width, kMinClientWidth);
client_height = (std::max)(client_height, kMinClientHeight);
int client_width = 0;
int client_height = 0;
FitClientSize(aspect, work, &client_width, &client_height);
RECT rect = {0, 0, client_width, client_height};
AdjustWindowRectEx(&rect, GetWindowLongW(hwnd_, GWL_STYLE), FALSE,
+3 -1
View File
@@ -29,7 +29,9 @@ class VideoWindow {
~VideoWindow();
// 创建并显示窗口(已存在则只前置)。必须在平台线程(消息循环所在线程)调用。
bool Open(FrameProvider provider);
// hint_width / hint_height 是被控端屏幕尺寸(可为 0):已知时窗口直接按该比例
// 开成合适大小,不会先弹出一个 16:9 的大窗口再等首帧缩回去。
bool Open(FrameProvider provider, int hint_width, int hint_height);
// 关闭并销毁窗口。必须在平台线程调用。
void Close();