feat(sender): 串流窗口支持点击与拖拽控制
- video_window 增加 InputHandler 回调,WM_LBUTTONDOWN/MOUSEMOVE/LBUTTONUP/CAPTURECHANGED 映射为 0/2/1/3,移动事件按 60Hz 限流 - Paint 记录帧尺寸与实际绘制矩形,ClientToFrame 换算到画面像素并过滤黑边 - video_texture 通过方法通道转发 pointer 事件到 Dart,析构时清空回调 - bridge.dart 新增 onVideoPointer,main.dart 以 msg_type=0 触摸帧下发(压力 1000/0)
This commit is contained in:
1 parent
03a5853da7
commit
a102d10089
5 files changed
+166
-2
No files matched your search
@@ -30,9 +30,24 @@ class SenderBridge {
|
||||
/// 用户关掉独立串流窗口(点关闭按钮 / 按 ESC)时触发,用于停止投屏。
|
||||
void Function()? onVideoWindowClosed;
|
||||
|
||||
/// 用户在串流窗口里点击/拖拽画面时触发:(action, x, y)。
|
||||
///
|
||||
/// action 与协议 TouchEvent.action 一致(0=按下 1=抬起 2=移动 3=取消),
|
||||
/// x/y 是**画面像素坐标**(编码分辨率),由被控端换算成真实屏幕坐标。
|
||||
void Function(int action, int x, int y)? onVideoPointer;
|
||||
|
||||
Future<void> _handleVideoCall(MethodCall call) async {
|
||||
if (call.method == 'windowClosed') {
|
||||
onVideoWindowClosed?.call();
|
||||
switch (call.method) {
|
||||
case 'windowClosed':
|
||||
onVideoWindowClosed?.call();
|
||||
case 'pointer':
|
||||
final args = call.arguments;
|
||||
if (args is! Map) return;
|
||||
final action = (args['action'] as num?)?.toInt();
|
||||
final x = (args['x'] as num?)?.toInt();
|
||||
final y = (args['y'] as num?)?.toInt();
|
||||
if (action == null || x == null || y == null) return;
|
||||
onVideoPointer?.call(action, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +86,8 @@ class _HomePageState extends State<HomePage> {
|
||||
_bridge = SenderBridge(nativeCore: _nativeCore);
|
||||
// 独立窗口被用户关闭 → 停止投屏。
|
||||
_bridge.onVideoWindowClosed = _onStreamWindowClosed;
|
||||
// 串流窗口里的鼠标点击/拖拽 → 触摸控制帧。
|
||||
_bridge.onVideoPointer = _onVideoPointer;
|
||||
_diagnosticLog.start();
|
||||
_diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}');
|
||||
_bridge.watchPhase().listen((phase) {
|
||||
@@ -265,6 +267,22 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 串流窗口里的鼠标事件 → 触摸控制帧。
|
||||
///
|
||||
/// 坐标是画面像素(与编码分辨率一致),被控端会按「画面尺寸 → 真实屏幕尺寸」
|
||||
/// 换算后再注入,因此主控端不需要知道手机的真实分辨率。
|
||||
void _onVideoPointer(int action, int x, int y) {
|
||||
if (!_phase.isConnected) return;
|
||||
_sendControl('touch', {
|
||||
'action': action,
|
||||
'pointerId': 1,
|
||||
'x': x,
|
||||
'y': y,
|
||||
// 按下/拖动保持满压力,抬起与取消归零。
|
||||
'pressure': (action == 0 || action == 2) ? 1000 : 0,
|
||||
});
|
||||
}
|
||||
|
||||
void _snack(String message) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context)
|
||||
|
||||
@@ -838,12 +838,26 @@ class VideoTexturePlugin : public flutter::Plugin {
|
||||
// 用户点窗口关闭按钮或按 ESC:通知 Flutter 停止投屏。
|
||||
video_window_.SetCloseHandler([this]() { OnVideoWindowClosed(); });
|
||||
|
||||
// 画面里的鼠标操作(点击/拖拽)→ Flutter,再由 Dart 转成触摸控制帧下发。
|
||||
video_window_.SetInputHandler([this](int action, int x, int y) {
|
||||
if (channel_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
flutter::EncodableMap map;
|
||||
map[flutter::EncodableValue("action")] = flutter::EncodableValue(action);
|
||||
map[flutter::EncodableValue("x")] = flutter::EncodableValue(x);
|
||||
map[flutter::EncodableValue("y")] = flutter::EncodableValue(y);
|
||||
channel_->InvokeMethod("pointer",
|
||||
std::make_unique<flutter::EncodableValue>(map));
|
||||
});
|
||||
|
||||
worker_ = std::thread([this, shared_frames]() { RunWorker(shared_frames); });
|
||||
}
|
||||
|
||||
~VideoTexturePlugin() override {
|
||||
// 先摘掉回调,避免析构期间 DestroyWindow 触发 InvokeMethod。
|
||||
video_window_.SetCloseHandler(nullptr);
|
||||
video_window_.SetInputHandler(nullptr);
|
||||
video_window_.Close();
|
||||
stop_.store(true);
|
||||
if (worker_.joinable()) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "video_window.h"
|
||||
|
||||
#include <windowsx.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
@@ -12,6 +14,9 @@ constexpr UINT kMsgFrameReady = WM_APP + 0x51;
|
||||
// 窗口线程内部:视频宽高比变化,需要按比例重设窗口大小。
|
||||
constexpr UINT kMsgApplyAspect = WM_APP + 0x52;
|
||||
|
||||
// 拖动时限流下发移动事件(约 60Hz),避免高频 WM_MOUSEMOVE 塞满控制通道。
|
||||
constexpr ULONGLONG kMoveIntervalMs = 16;
|
||||
|
||||
// 最小客户区尺寸,避免窗口被拖成一条缝。
|
||||
constexpr int kMinClientWidth = 320;
|
||||
constexpr int kMinClientHeight = 180;
|
||||
@@ -130,12 +135,20 @@ void VideoWindow::Close() {
|
||||
provider_ = nullptr;
|
||||
aspect_ratio_ = 0.0;
|
||||
applied_aspect_ = 0.0;
|
||||
frame_width_ = 0;
|
||||
frame_height_ = 0;
|
||||
image_rect_ = RECT{};
|
||||
dragging_ = false;
|
||||
}
|
||||
|
||||
void VideoWindow::SetCloseHandler(CloseHandler handler) {
|
||||
close_handler_ = std::move(handler);
|
||||
}
|
||||
|
||||
void VideoWindow::SetInputHandler(InputHandler handler) {
|
||||
input_handler_ = std::move(handler);
|
||||
}
|
||||
|
||||
void VideoWindow::OnFrameAvailable() {
|
||||
if (!open_.load()) {
|
||||
return;
|
||||
@@ -201,6 +214,45 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
|
||||
case WM_SIZE:
|
||||
InvalidateRect(hwnd, nullptr, FALSE);
|
||||
return 0;
|
||||
case WM_LBUTTONDOWN:
|
||||
// 画面内点击 = 在手机上点一下:先捕获鼠标,拖动时才能持续收到 WM_MOUSEMOVE。
|
||||
SetFocus(hwnd);
|
||||
SetCapture(hwnd);
|
||||
dragging_ = true;
|
||||
last_move_tick_ = GetTickCount64();
|
||||
DispatchPointerAt(0, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
|
||||
return 0;
|
||||
case WM_MOUSEMOVE: {
|
||||
// 只有按住左键才下发移动(悬停不注入),并做 60Hz 限流。
|
||||
if (!dragging_) {
|
||||
return 0;
|
||||
}
|
||||
const ULONGLONG now = GetTickCount64();
|
||||
if (now - last_move_tick_ < kMoveIntervalMs) {
|
||||
return 0;
|
||||
}
|
||||
last_move_tick_ = now;
|
||||
DispatchPointerAt(2, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
|
||||
return 0;
|
||||
}
|
||||
case WM_LBUTTONUP: {
|
||||
if (!dragging_) {
|
||||
return 0;
|
||||
}
|
||||
// 先清标志再 ReleaseCapture:否则 WM_CAPTURECHANGED 会补发一次取消。
|
||||
dragging_ = false;
|
||||
ReleaseCapture();
|
||||
DispatchPointerAt(1, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
|
||||
return 0;
|
||||
}
|
||||
case WM_CAPTURECHANGED: {
|
||||
// 窗口失焦、被别处抢走鼠标:补一个 cancel,避免手机上残留按下的手指。
|
||||
if (dragging_) {
|
||||
dragging_ = false;
|
||||
DispatchPointerAt(3, last_point_);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
case WM_KEYDOWN:
|
||||
if (wparam == VK_ESCAPE) {
|
||||
// 关窗即停止投屏(由 close_handler_ 通知 Flutter)。
|
||||
@@ -218,6 +270,10 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
|
||||
provider_ = nullptr;
|
||||
aspect_ratio_ = 0.0;
|
||||
applied_aspect_ = 0.0;
|
||||
frame_width_ = 0;
|
||||
frame_height_ = 0;
|
||||
image_rect_ = RECT{};
|
||||
dragging_ = false;
|
||||
CloseHandler handler = close_handler_;
|
||||
if (handler) {
|
||||
handler();
|
||||
@@ -264,6 +320,12 @@ void VideoWindow::Paint() {
|
||||
const int offset_x = client.left + (client_width - draw_width) / 2;
|
||||
const int offset_y = client.top + (client_height - draw_height) / 2;
|
||||
|
||||
// 记住这一帧的尺寸和实际绘制矩形,鼠标点击时据此换算成画面像素。
|
||||
frame_width_ = width;
|
||||
frame_height_ = height;
|
||||
image_rect_ = RECT{offset_x, offset_y, offset_x + draw_width,
|
||||
offset_y + draw_height};
|
||||
|
||||
BITMAPINFO info = {};
|
||||
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
info.bmiHeader.biWidth = width;
|
||||
@@ -277,6 +339,10 @@ void VideoWindow::Paint() {
|
||||
StretchDIBits(hdc, offset_x, offset_y, draw_width, draw_height, 0, 0, width,
|
||||
height, frame->data(), &info, DIB_RGB_COLORS, SRCCOPY);
|
||||
} else {
|
||||
// 还没有画面:清掉坐标映射,避免拿上一路的尺寸误点。
|
||||
frame_width_ = 0;
|
||||
frame_height_ = 0;
|
||||
image_rect_ = RECT{};
|
||||
// 还没画面时给个提示:窗口是在点「请求投屏」后就弹出的,
|
||||
// 纯黑会让人以为卡死。
|
||||
HGDIOBJ previous = SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
|
||||
@@ -291,6 +357,38 @@ void VideoWindow::Paint() {
|
||||
EndPaint(hwnd_, &paint);
|
||||
}
|
||||
|
||||
bool VideoWindow::ClientToFrame(POINT point, int* x, int* y) const {
|
||||
const int width = image_rect_.right - image_rect_.left;
|
||||
const int height = image_rect_.bottom - image_rect_.top;
|
||||
if (frame_width_ <= 0 || frame_height_ <= 0 || width <= 0 || height <= 0) {
|
||||
return false;
|
||||
}
|
||||
// 黑边区域不算点击,否则会点到画面外的坐标。
|
||||
if (point.x < image_rect_.left || point.x >= image_rect_.right ||
|
||||
point.y < image_rect_.top || point.y >= image_rect_.bottom) {
|
||||
return false;
|
||||
}
|
||||
const int frame_x = (point.x - image_rect_.left) * frame_width_ / width;
|
||||
const int frame_y = (point.y - image_rect_.top) * frame_height_ / height;
|
||||
*x = (std::min)((std::max)(frame_x, 0), frame_width_ - 1);
|
||||
*y = (std::min)((std::max)(frame_y, 0), frame_height_ - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
void VideoWindow::DispatchPointerAt(int action, POINT point) {
|
||||
last_point_ = point;
|
||||
InputHandler handler = input_handler_;
|
||||
if (!handler) {
|
||||
return;
|
||||
}
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
if (!ClientToFrame(point, &x, &y)) {
|
||||
return;
|
||||
}
|
||||
handler(action, x, y);
|
||||
}
|
||||
|
||||
void VideoWindow::ApplyAspectToWindow(double aspect) {
|
||||
if (hwnd_ == nullptr || aspect <= 0.0) {
|
||||
return;
|
||||
|
||||
@@ -24,6 +24,9 @@ class VideoWindow {
|
||||
int* height)>;
|
||||
// 用户主动关闭窗口(点关闭按钮 / 按 ESC)时回调。
|
||||
using CloseHandler = std::function<void()>;
|
||||
// 画面内鼠标操作回调,坐标已换算成画面像素(0..frame_width-1)。
|
||||
// action:0=按下 1=抬起 2=移动 3=取消,语义与协议 TouchEvent.action 一致。
|
||||
using InputHandler = std::function<void(int action, int x, int y)>;
|
||||
|
||||
VideoWindow();
|
||||
~VideoWindow();
|
||||
@@ -42,6 +45,9 @@ class VideoWindow {
|
||||
// 设置/清除用户关闭回调;传 nullptr 清除。
|
||||
void SetCloseHandler(CloseHandler handler);
|
||||
|
||||
// 设置/清除画面鼠标回调;传 nullptr 清除。
|
||||
void SetInputHandler(InputHandler handler);
|
||||
|
||||
bool IsOpen() const { return open_.load(); }
|
||||
|
||||
private:
|
||||
@@ -53,17 +59,30 @@ class VideoWindow {
|
||||
void ApplyAspectToWindow(double aspect);
|
||||
// 把窗口矩形修正为符合当前宽高比(WM_SIZING 用)。
|
||||
void AdjustSizingRect(RECT* rect, WPARAM edge) const;
|
||||
// 客户区坐标 -> 画面像素;返回 false 表示点在黑边上或还没有画面。
|
||||
bool ClientToFrame(POINT point, int* x, int* y) const;
|
||||
void DispatchPointerAt(int action, POINT point);
|
||||
|
||||
HWND hwnd_ = nullptr;
|
||||
std::atomic<bool> open_{false};
|
||||
std::atomic<bool> repaint_pending_{false};
|
||||
FrameProvider provider_;
|
||||
CloseHandler close_handler_;
|
||||
InputHandler input_handler_;
|
||||
|
||||
// 当前视频宽高比(宽/高),0 表示还没有画面。只在窗口线程读写。
|
||||
double aspect_ratio_ = 0.0;
|
||||
// 已经应用过的宽高比,避免每帧都触发 SetWindowPos。
|
||||
double applied_aspect_ = 0.0;
|
||||
|
||||
// 最近一帧的画面尺寸与它在客户区里的实际绘制矩形(含黑边)。只在窗口线程读写。
|
||||
int frame_width_ = 0;
|
||||
int frame_height_ = 0;
|
||||
RECT image_rect_ = {};
|
||||
// 左键按下期间才下发移动事件,悬停不注入。
|
||||
bool dragging_ = false;
|
||||
POINT last_point_ = {};
|
||||
ULONGLONG last_move_tick_ = 0;
|
||||
};
|
||||
|
||||
#endif // RUNNER_VIDEO_WINDOW_H_
|
||||
Reference in new issue
Block a user