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

+187 -47
View File
@@ -1,6 +1,7 @@
#include "video_window.h"
#include <algorithm>
#include <cmath>
namespace {
@@ -8,6 +9,12 @@ constexpr wchar_t kWindowClass[] = L"TTSTDVideoWindowClass";
constexpr wchar_t kWindowTitle[] = L"TTSTD 串流";
// 解码线程 -> 窗口线程:有新帧,触发一次重绘。
constexpr UINT kMsgFrameReady = WM_APP + 0x51;
// 窗口线程内部:视频宽高比变化,需要按比例重设窗口大小。
constexpr UINT kMsgApplyAspect = WM_APP + 0x52;
// 最小客户区尺寸,避免窗口被拖成一条缝。
constexpr int kMinClientWidth = 320;
constexpr int kMinClientHeight = 180;
} // namespace
@@ -28,7 +35,7 @@ bool VideoWindow::Open(FrameProvider provider) {
HINSTANCE instance = GetModuleHandleW(nullptr);
WNDCLASSEXW window_class = {};
window_class.cbSize = sizeof(WNDCLASSEXW);
window_class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpfnWndProc = &VideoWindow::StaticWndProc;
window_class.hInstance = instance;
window_class.hCursor = LoadCursorW(nullptr, IDC_ARROW);
@@ -41,18 +48,23 @@ bool VideoWindow::Open(FrameProvider provider) {
return false;
}
// 不提供最大化按钮:画面窗口只按视频比例缩放。
const DWORD style = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX;
RECT rect = {0, 0, 1280, 720};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
AdjustWindowRect(&rect, style, FALSE);
HWND hwnd = CreateWindowExW(
0, kWindowClass, kWindowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, nullptr,
nullptr, instance, this);
0, kWindowClass, kWindowTitle, style, CW_USEDEFAULT, CW_USEDEFAULT,
rect.right - rect.left, rect.bottom - rect.top, nullptr, nullptr, instance,
this);
if (hwnd == nullptr) {
provider_ = nullptr;
return false;
}
hwnd_ = hwnd;
aspect_ratio_ = 0.0;
applied_aspect_ = 0.0;
open_.store(true);
ShowWindow(hwnd_, SW_SHOWNORMAL);
UpdateWindow(hwnd_);
@@ -67,7 +79,12 @@ void VideoWindow::Close() {
open_.store(false);
repaint_pending_.store(false);
provider_ = nullptr;
full_screen_ = false;
aspect_ratio_ = 0.0;
applied_aspect_ = 0.0;
}
void VideoWindow::SetCloseHandler(CloseHandler handler) {
close_handler_ = std::move(handler);
}
void VideoWindow::OnFrameAvailable() {
@@ -115,36 +132,49 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
repaint_pending_.store(false);
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
case kMsgApplyAspect:
ApplyAspectToWindow(aspect_ratio_);
return 0;
case WM_GETMINMAXINFO: {
auto* info = reinterpret_cast<MINMAXINFO*>(lparam);
RECT rect = {0, 0, kMinClientWidth, kMinClientHeight};
AdjustWindowRectEx(&rect, GetWindowLongW(hwnd, GWL_STYLE), FALSE,
GetWindowLongW(hwnd, GWL_EXSTYLE));
info->ptMinTrackSize.x = rect.right - rect.left;
info->ptMinTrackSize.y = rect.bottom - rect.top;
return 0;
}
case WM_SIZING: {
auto* rect = reinterpret_cast<RECT*>(lparam);
AdjustSizingRect(rect, wparam);
return TRUE;
}
case WM_SIZE:
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
case WM_LBUTTONDBLCLK:
ToggleFullScreen();
return 0;
case WM_KEYDOWN:
if (wparam == VK_ESCAPE) {
if (full_screen_) {
ToggleFullScreen();
} else {
SendMessageW(hwnd, WM_CLOSE, 0, 0);
}
return 0;
}
if (wparam == VK_F11 || wparam == 'F') {
ToggleFullScreen();
// 关窗即停止投屏(由 close_handler_ 通知 Flutter)。
SendMessageW(hwnd, WM_CLOSE, 0, 0);
return 0;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
case WM_DESTROY: {
hwnd_ = nullptr;
open_.store(false);
repaint_pending_.store(false);
provider_ = nullptr;
full_screen_ = false;
aspect_ratio_ = 0.0;
applied_aspect_ = 0.0;
CloseHandler handler = close_handler_;
if (handler) {
handler();
}
return 0;
}
default:
break;
}
@@ -168,6 +198,14 @@ void VideoWindow::Paint() {
const int client_height = client.bottom - client.top;
if (frame && !frame->empty() && width > 0 && height > 0 && client_width > 0 &&
client_height > 0) {
const double aspect =
static_cast<double>(width) / static_cast<double>(height);
aspect_ratio_ = aspect;
// 首帧(或分辨率/方向变化)时把窗口调成同比例的尺寸。
if (std::fabs(aspect - applied_aspect_) > 0.01) {
PostMessageW(hwnd_, kMsgApplyAspect, 0, 0);
}
// 保持宽高比,四周留黑边(竖屏串流不会被拉伸)。
const double scale = std::min(
static_cast<double>(client_width) / static_cast<double>(width),
@@ -196,8 +234,7 @@ void VideoWindow::Paint() {
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(170, 170, 170));
RECT text_rect = client;
DrawTextW(hdc,
L"等待被控端画面…\r\n请在手机弹窗中点击「允许」", -1, &text_rect,
DrawTextW(hdc, L"等待被控端画面…\r\n请在手机弹窗中点击「允许」", -1, &text_rect,
DT_CENTER | DT_VCENTER | DT_WORDBREAK);
SelectObject(hdc, previous);
}
@@ -205,32 +242,135 @@ void VideoWindow::Paint() {
EndPaint(hwnd_, &paint);
}
void VideoWindow::ToggleFullScreen() {
if (hwnd_ == nullptr) {
void VideoWindow::ApplyAspectToWindow(double aspect) {
if (hwnd_ == nullptr || aspect <= 0.0) {
return;
}
if (!full_screen_) {
saved_style_ = GetWindowLongW(hwnd_, GWL_STYLE);
saved_placement_.length = sizeof(saved_placement_);
GetWindowPlacement(hwnd_, &saved_placement_);
MONITORINFO monitor = {};
monitor.cbSize = sizeof(monitor);
GetMonitorInfoW(MonitorFromWindow(hwnd_, MONITOR_DEFAULTTONEAREST),
&monitor);
SetWindowLongW(hwnd_, GWL_STYLE, saved_style_ & ~WS_OVERLAPPEDWINDOW);
SetWindowPos(hwnd_, nullptr, monitor.rcMonitor.left, monitor.rcMonitor.top,
monitor.rcMonitor.right - monitor.rcMonitor.left,
monitor.rcMonitor.bottom - monitor.rcMonitor.top,
SWP_NOZORDER | SWP_FRAMECHANGED);
full_screen_ = true;
} else {
SetWindowLongW(hwnd_, GWL_STYLE, saved_style_);
SetWindowPlacement(hwnd_, &saved_placement_);
SetWindowPos(hwnd_, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
full_screen_ = false;
MONITORINFO monitor = {};
monitor.cbSize = sizeof(monitor);
const HMONITOR target = MonitorFromWindow(hwnd_, MONITOR_DEFAULTTONEAREST);
if (target == nullptr || !GetMonitorInfoW(target, &monitor)) {
return;
}
applied_aspect_ = aspect;
const RECT work = monitor.rcWork;
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);
RECT rect = {0, 0, client_width, client_height};
AdjustWindowRectEx(&rect, GetWindowLongW(hwnd_, GWL_STYLE), FALSE,
GetWindowLongW(hwnd_, GWL_EXSTYLE));
const int window_width = rect.right - rect.left;
const int window_height = rect.bottom - rect.top;
const int x = work.left + (work_width - window_width) / 2;
const int y = work.top + (work_height - window_height) / 2;
SetWindowPos(hwnd_, nullptr, x, y, window_width, window_height,
SWP_NOZORDER | SWP_NOACTIVATE);
}
void VideoWindow::AdjustSizingRect(RECT* rect, WPARAM edge) const {
if (rect == nullptr || aspect_ratio_ <= 0.0) {
return;
}
RECT window_rect = {};
RECT client_rect = {};
GetWindowRect(hwnd_, &window_rect);
GetClientRect(hwnd_, &client_rect);
const int frame_width =
(window_rect.right - window_rect.left) - client_rect.right;
const int frame_height =
(window_rect.bottom - window_rect.top) - client_rect.bottom;
int client_width = (rect->right - rect->left) - frame_width;
int client_height = (rect->bottom - rect->top) - frame_height;
client_width = (std::max)(client_width, kMinClientWidth);
client_height = (std::max)(client_height, kMinClientHeight);
switch (edge) {
case WMSZ_TOP:
case WMSZ_BOTTOM:
// 上下拖动:以高度为准算宽度,水平居中。
client_width = static_cast<int>(client_height * aspect_ratio_);
break;
default:
// 左右边与四角:以宽度为准算高度。
client_height = static_cast<int>(client_width / aspect_ratio_);
break;
}
client_height = (std::max)(client_height, kMinClientHeight);
client_width = (std::max)(client_width, kMinClientWidth);
const int width = client_width + frame_width;
const int height = client_height + frame_height;
switch (edge) {
case WMSZ_TOPLEFT:
// 右下角固定。
rect->left = rect->right - width;
rect->top = rect->bottom - height;
break;
case WMSZ_TOPRIGHT:
// 左下角固定。
rect->right = rect->left + width;
rect->top = rect->bottom - height;
break;
case WMSZ_BOTTOMLEFT:
// 右上角固定。
rect->left = rect->right - width;
rect->bottom = rect->top + height;
break;
case WMSZ_BOTTOMRIGHT:
// 左上角固定。
rect->right = rect->left + width;
rect->bottom = rect->top + height;
break;
case WMSZ_LEFT:
// 右边固定,垂直居中。
rect->left = rect->right - width;
{
const int center_y = (rect->top + rect->bottom) / 2;
rect->top = center_y - height / 2;
rect->bottom = rect->top + height;
}
break;
case WMSZ_RIGHT:
// 左边固定,垂直居中。
rect->right = rect->left + width;
{
const int center_y = (rect->top + rect->bottom) / 2;
rect->top = center_y - height / 2;
rect->bottom = rect->top + height;
}
break;
case WMSZ_TOP:
// 底边固定,水平居中。
rect->top = rect->bottom - height;
{
const int center_x = (rect->left + rect->right) / 2;
rect->left = center_x - width / 2;
rect->right = rect->left + width;
}
break;
default:
// WMSZ_BOTTOM:顶边固定,水平居中。
rect->bottom = rect->top + height;
{
const int center_x = (rect->left + rect->right) / 2;
rect->left = center_x - width / 2;
rect->right = rect->left + width;
}
break;
}
InvalidateRect(hwnd_, nullptr, FALSE);
}