fix: 修正投屏授权状态与窗口生命周期,避免串流状态错乱

This commit is contained in:
tongtong committed 2026-11-28 02:59:49 +08:00
1 parent a0b77a3e7a
commit 4c9d006d11
6 files changed
+134 -90

No files matched your search

@@ -267,9 +267,11 @@ class FrameStore {
// 独立窗口使用:返回当前帧的共享所有权快照。
// 拿到 shared_ptr 后即使解码线程继续 Publish,绘制期间的帧也不会被回收。
std::shared_ptr<std::vector<uint8_t>> Snapshot(int* width, int* height) {
std::shared_ptr<std::vector<uint8_t>> Snapshot(int* width, int* height,
uint64_t after_frame) {
std::lock_guard<std::mutex> lock(mutex_);
if (!front_ || front_->empty() || width_ <= 0 || height_ <= 0) {
if (frames_ <= after_frame || !front_ || front_->empty() || width_ <= 0 ||
height_ <= 0) {
return nullptr;
}
*width = width_;
@@ -988,8 +990,13 @@ class VideoTexturePlugin : public flutter::Plugin {
hint_width = read_int("width");
hint_height = read_int("height");
}
if (!video_window_.IsOpen()) {
window_start_frame_ = store->frames();
}
const bool opened = video_window_.Open(
[store](int* width, int* height) { return store->Snapshot(width, height); },
[store, start = window_start_frame_](int* width, int* height) {
return store->Snapshot(width, height, start);
},
hint_width, hint_height);
result->Success(flutter::EncodableValue(opened));
return;
@@ -1013,8 +1020,13 @@ class VideoTexturePlugin : public flutter::Plugin {
frames_->size(&width, &height);
map[flutter::EncodableValue("textureId")] =
flutter::EncodableValue(static_cast<int64_t>(texture_id_));
const uint64_t decoded = frames_->frames();
map[flutter::EncodableValue("decoded")] =
flutter::EncodableValue(static_cast<int64_t>(frames_->frames()));
flutter::EncodableValue(static_cast<int64_t>(decoded));
map[flutter::EncodableValue("windowFrames")] = flutter::EncodableValue(
static_cast<int64_t>(video_window_.IsOpen()
? decoded - window_start_frame_
: 0));
map[flutter::EncodableValue("pending")] =
flutter::EncodableValue(static_cast<int64_t>(api_.Pending()));
map[flutter::EncodableValue("received")] =
@@ -1143,6 +1155,7 @@ class VideoTexturePlugin : public flutter::Plugin {
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
FlutterDesktopPixelBuffer descriptor_ = {};
std::shared_ptr<FrameStore> frames_;
uint64_t window_start_frame_ = 0;
// 与主窗口纹理共享同一份 FrameStore 的独立串流窗口。
VideoWindow video_window_;
// 本次关闭是由 Flutter 主动发起的(不需要再回调 windowClosed)。
+41 -18
View File
@@ -227,14 +227,25 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
case WM_SIZE:
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
case WM_LBUTTONDOWN:
case WM_LBUTTONDOWN: {
const POINT point{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
int x = 0;
int y = 0;
if (dragging_ || !input_handler_ || !ClientToFrame(point, &x, &y)) {
return 0;
}
// 画面内点击 = 在手机上点一下:先捕获鼠标,拖动时才能持续收到 WM_MOUSEMOVE。
SetFocus(hwnd);
SetCapture(hwnd);
if (GetCapture() != hwnd) {
return 0;
}
dragging_ = true;
last_move_tick_ = GetTickCount64();
DispatchPointerAt(0, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
last_touch_ = POINT{x, y};
input_handler_(0, x, y);
return 0;
}
case WM_MOUSEMOVE: {
// 只有按住左键才下发移动(悬停不注入),并做 60Hz 限流。
if (!dragging_) {
@@ -253,19 +264,15 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
return 0;
}
// 先清标志再 ReleaseCapture:否则 WM_CAPTURECHANGED 会补发一次取消。
dragging_ = false;
ReleaseCapture();
DispatchPointerAt(1, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
EndDrag(hwnd, 1, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
return 0;
}
case WM_CAPTURECHANGED: {
case WM_CANCELMODE:
case WM_KILLFOCUS:
case WM_CAPTURECHANGED:
// 窗口失焦、被别处抢走鼠标:补一个 cancel,避免手机上残留按下的手指。
if (dragging_) {
dragging_ = false;
DispatchPointerAt(3, last_point_);
}
return 0;
}
EndDrag(hwnd, 3, POINT{});
break;
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MBUTTONDOWN:
@@ -319,6 +326,7 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
DestroyWindow(hwnd);
return 0;
case WM_DESTROY: {
EndDrag(hwnd, 3, POINT{});
hwnd_ = nullptr;
open_.store(false);
repaint_pending_.store(false);
@@ -493,21 +501,36 @@ bool VideoWindow::ClientToFrame(POINT point, int* x, int* y) const {
}
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)) {
// 已开始的拖动可以越过画面边缘。画面暂时不可用时,抬起仍用最后的触点。
if (action != 3 && image_rect_.left < image_rect_.right &&
image_rect_.top < image_rect_.bottom) {
point.x = std::clamp(point.x, image_rect_.left, image_rect_.right - 1);
point.y = std::clamp(point.y, image_rect_.top, image_rect_.bottom - 1);
int x = 0;
int y = 0;
if (ClientToFrame(point, &x, &y)) {
last_touch_ = POINT{x, y};
}
}
handler(action, last_touch_.x, last_touch_.y);
}
void VideoWindow::EndDrag(HWND hwnd, int action, POINT point) {
if (!dragging_) {
return;
}
handler(action, x, y);
dragging_ = false;
if (GetCapture() == hwnd) {
ReleaseCapture();
}
DispatchPointerAt(action, point);
}
void VideoWindow::DispatchMouseButtonAt(int button, int action, POINT point) {
last_point_ = point;
MouseButtonHandler handler = mouse_button_handler_;
if (!handler) {
return;
+2 -1
View File
@@ -79,6 +79,7 @@ class VideoWindow {
// 客户区坐标 -> 画面像素;返回 false 表示点在黑边上或还没有画面。
bool ClientToFrame(POINT point, int* x, int* y) const;
void DispatchPointerAt(int action, POINT point);
void EndDrag(HWND hwnd, int action, POINT point);
void DispatchMouseButtonAt(int button, int action, POINT point);
void DispatchKey(int action, int vk);
// 当前修饰键状态(按协议位定义拼装)。
@@ -115,7 +116,7 @@ class VideoWindow {
RECT image_rect_ = {};
// 左键按下期间才下发移动事件,悬停不注入。
bool dragging_ = false;
POINT last_point_ = {};
POINT last_touch_ = {};
ULONGLONG last_move_tick_ = 0;
};