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

+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,