feat(sender): 支持把串流画面弹出为独立原生窗口
- 新增 windows/runner/video_window.{h,cpp}:Win32 顶层窗口,GDI 直接绘制解码后的 BGRA 帧,支持拖到任意显示器/最大化/双击或 F11 全屏/ESC 退出
- FrameStore 增加 Snapshot()(shared_ptr 保活),独立窗口与主窗口共享同一份帧,不重复解码、不抢 Rust 单消费者帧队列
- 独立窗口在播时跳过 MarkTextureFrameAvailable,省一次 BGRA 拷贝;主界面显示占位提示
- ttstd/video 通道新增 openWindow/closeWindow/windowOpen,stats 带 windowOpen
- VideoView 按真实码流宽高算显示比例,竖屏投屏不再被压成 16:9
This commit is contained in:
1 parent
f87ef59d98
commit
0cff2b011a
8 files changed
+419
-4
No files matched your search
@@ -11,6 +11,7 @@ add_executable(${BINARY_NAME} WIN32
|
||||
"main.cpp"
|
||||
"utils.cpp"
|
||||
"video_texture.cpp"
|
||||
"video_window.cpp"
|
||||
"win32_window.cpp"
|
||||
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
|
||||
"Runner.rc"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "video_texture.h"
|
||||
|
||||
#include "video_window.h"
|
||||
|
||||
#include <flutter/encodable_value.h>
|
||||
#include <flutter/method_channel.h>
|
||||
#include <flutter/plugin_registrar_windows.h>
|
||||
@@ -262,6 +264,18 @@ class FrameStore {
|
||||
return frames_;
|
||||
}
|
||||
|
||||
// 独立窗口使用:返回当前帧的共享所有权快照。
|
||||
// 拿到 shared_ptr 后即使解码线程继续 Publish,绘制期间的帧也不会被回收。
|
||||
std::shared_ptr<std::vector<uint8_t>> Snapshot(int* width, int* height) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!front_ || front_->empty() || width_ <= 0 || height_ <= 0) {
|
||||
return nullptr;
|
||||
}
|
||||
*width = width_;
|
||||
*height = height_;
|
||||
return front_;
|
||||
}
|
||||
|
||||
uint64_t pts() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return pts_;
|
||||
@@ -860,6 +874,22 @@ class VideoTexturePlugin : public flutter::Plugin {
|
||||
static_cast<int64_t>(texture_id_)));
|
||||
return;
|
||||
}
|
||||
if (call.method_name() == "openWindow") {
|
||||
auto store = frames_;
|
||||
const bool opened = video_window_.Open(
|
||||
[store](int* width, int* height) { return store->Snapshot(width, height); });
|
||||
result->Success(flutter::EncodableValue(opened));
|
||||
return;
|
||||
}
|
||||
if (call.method_name() == "closeWindow") {
|
||||
video_window_.Close();
|
||||
result->Success(flutter::EncodableValue(true));
|
||||
return;
|
||||
}
|
||||
if (call.method_name() == "windowOpen") {
|
||||
result->Success(flutter::EncodableValue(video_window_.IsOpen()));
|
||||
return;
|
||||
}
|
||||
if (call.method_name() == "stats") {
|
||||
flutter::EncodableMap map;
|
||||
int width = 0;
|
||||
@@ -886,6 +916,8 @@ class VideoTexturePlugin : public flutter::Plugin {
|
||||
// 这里显式指出,避免和「还没等到关键帧」或「没收到码流」混为一谈。
|
||||
error = "被控端码流缺少 SPS/PPS,请更新被控端 APK 后重连";
|
||||
}
|
||||
map[flutter::EncodableValue("windowOpen")] =
|
||||
flutter::EncodableValue(video_window_.IsOpen());
|
||||
map[flutter::EncodableValue("error")] = flutter::EncodableValue(error);
|
||||
map[flutter::EncodableValue("format")] =
|
||||
flutter::EncodableValue(static_cast<int64_t>(last_format_));
|
||||
@@ -978,6 +1010,11 @@ class VideoTexturePlugin : public flutter::Plugin {
|
||||
return [this, store](std::shared_ptr<std::vector<uint8_t>> frame, int width,
|
||||
int height, uint64_t pts) {
|
||||
store->Publish(std::move(frame), width, height, pts);
|
||||
if (video_window_.IsOpen()) {
|
||||
// 画面已经在独立窗口里,没必要再驱动 Flutter 纹理(省一次 BGRA 拷贝)。
|
||||
video_window_.OnFrameAvailable();
|
||||
return;
|
||||
}
|
||||
if (texture_registrar_ != nullptr && texture_id_ != 0) {
|
||||
texture_registrar_->MarkTextureFrameAvailable(texture_id_);
|
||||
}
|
||||
@@ -990,6 +1027,8 @@ class VideoTexturePlugin : public flutter::Plugin {
|
||||
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
|
||||
FlutterDesktopPixelBuffer descriptor_ = {};
|
||||
std::shared_ptr<FrameStore> frames_;
|
||||
// 与主窗口纹理共享同一份 FrameStore 的独立串流窗口。
|
||||
VideoWindow video_window_;
|
||||
RustVideoApi api_;
|
||||
VideoDecoder decoder_;
|
||||
std::atomic<bool> stop_{false};
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
#include "video_window.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr wchar_t kWindowClass[] = L"TTSTDVideoWindowClass";
|
||||
constexpr wchar_t kWindowTitle[] = L"TTSTD 串流";
|
||||
// 解码线程 -> 窗口线程:有新帧,触发一次重绘。
|
||||
constexpr UINT kMsgFrameReady = WM_APP + 0x51;
|
||||
|
||||
} // namespace
|
||||
|
||||
VideoWindow::VideoWindow() = default;
|
||||
|
||||
VideoWindow::~VideoWindow() {
|
||||
Close();
|
||||
}
|
||||
|
||||
bool VideoWindow::Open(FrameProvider provider) {
|
||||
provider_ = std::move(provider);
|
||||
if (hwnd_ != nullptr) {
|
||||
ShowWindow(hwnd_, SW_SHOWNORMAL);
|
||||
SetForegroundWindow(hwnd_);
|
||||
return true;
|
||||
}
|
||||
|
||||
HINSTANCE instance = GetModuleHandleW(nullptr);
|
||||
WNDCLASSEXW window_class = {};
|
||||
window_class.cbSize = sizeof(WNDCLASSEXW);
|
||||
window_class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
|
||||
window_class.lpfnWndProc = &VideoWindow::StaticWndProc;
|
||||
window_class.hInstance = instance;
|
||||
window_class.hCursor = LoadCursorW(nullptr, IDC_ARROW);
|
||||
// 背景由 WM_PAINT 自己填黑,避免无画面时闪白。
|
||||
window_class.hbrBackground = nullptr;
|
||||
window_class.lpszClassName = kWindowClass;
|
||||
if (RegisterClassExW(&window_class) == 0 &&
|
||||
GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
|
||||
provider_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
RECT rect = {0, 0, 1280, 720};
|
||||
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 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);
|
||||
if (hwnd == nullptr) {
|
||||
provider_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
hwnd_ = hwnd;
|
||||
open_.store(true);
|
||||
ShowWindow(hwnd_, SW_SHOWNORMAL);
|
||||
UpdateWindow(hwnd_);
|
||||
return true;
|
||||
}
|
||||
|
||||
void VideoWindow::Close() {
|
||||
if (hwnd_ != nullptr) {
|
||||
DestroyWindow(hwnd_);
|
||||
hwnd_ = nullptr;
|
||||
}
|
||||
open_.store(false);
|
||||
repaint_pending_.store(false);
|
||||
provider_ = nullptr;
|
||||
full_screen_ = false;
|
||||
}
|
||||
|
||||
void VideoWindow::OnFrameAvailable() {
|
||||
if (!open_.load()) {
|
||||
return;
|
||||
}
|
||||
HWND hwnd = hwnd_;
|
||||
if (hwnd == nullptr) {
|
||||
return;
|
||||
}
|
||||
// 上一帧的重绘还没处理完就不再投递,避免高频视频把消息队列塞满。
|
||||
if (repaint_pending_.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
if (!PostMessageW(hwnd, kMsgFrameReady, 0, 0)) {
|
||||
repaint_pending_.store(false);
|
||||
}
|
||||
}
|
||||
|
||||
LRESULT CALLBACK VideoWindow::StaticWndProc(HWND hwnd, UINT message,
|
||||
WPARAM wparam, LPARAM lparam) {
|
||||
if (message == WM_NCCREATE) {
|
||||
auto* create = reinterpret_cast<CREATESTRUCTW*>(lparam);
|
||||
SetWindowLongPtrW(hwnd, GWLP_USERDATA,
|
||||
reinterpret_cast<LONG_PTR>(create->lpCreateParams));
|
||||
}
|
||||
auto* self =
|
||||
reinterpret_cast<VideoWindow*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
|
||||
if (self != nullptr) {
|
||||
return self->WndProc(hwnd, message, wparam, lparam);
|
||||
}
|
||||
return DefWindowProcW(hwnd, message, wparam, lparam);
|
||||
}
|
||||
|
||||
LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
|
||||
LPARAM lparam) {
|
||||
switch (message) {
|
||||
case WM_PAINT:
|
||||
Paint();
|
||||
return 0;
|
||||
case WM_ERASEBKGND:
|
||||
// Paint() 内部整块填黑,这里跳过默认擦除以减少闪烁。
|
||||
return 1;
|
||||
case kMsgFrameReady:
|
||||
repaint_pending_.store(false);
|
||||
InvalidateRect(hwnd, nullptr, FALSE);
|
||||
return 0;
|
||||
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();
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case WM_CLOSE:
|
||||
DestroyWindow(hwnd);
|
||||
return 0;
|
||||
case WM_DESTROY:
|
||||
hwnd_ = nullptr;
|
||||
open_.store(false);
|
||||
repaint_pending_.store(false);
|
||||
provider_ = nullptr;
|
||||
full_screen_ = false;
|
||||
return 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return DefWindowProcW(hwnd, message, wparam, lparam);
|
||||
}
|
||||
|
||||
void VideoWindow::Paint() {
|
||||
PAINTSTRUCT paint = {};
|
||||
HDC hdc = BeginPaint(hwnd_, &paint);
|
||||
|
||||
RECT client = {};
|
||||
GetClientRect(hwnd_, &client);
|
||||
FillRect(hdc, &client, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
std::shared_ptr<const std::vector<uint8_t>> frame =
|
||||
provider_ ? provider_(&width, &height) : nullptr;
|
||||
|
||||
const int client_width = client.right - client.left;
|
||||
const int client_height = client.bottom - client.top;
|
||||
if (frame && !frame->empty() && width > 0 && height > 0 && client_width > 0 &&
|
||||
client_height > 0) {
|
||||
// 保持宽高比,四周留黑边(竖屏串流不会被拉伸)。
|
||||
const double scale = std::min(
|
||||
static_cast<double>(client_width) / static_cast<double>(width),
|
||||
static_cast<double>(client_height) / static_cast<double>(height));
|
||||
const int draw_width = (std::max)(1, static_cast<int>(width * scale));
|
||||
const int draw_height = (std::max)(1, static_cast<int>(height * scale));
|
||||
const int offset_x = client.left + (client_width - draw_width) / 2;
|
||||
const int offset_y = client.top + (client_height - draw_height) / 2;
|
||||
|
||||
BITMAPINFO info = {};
|
||||
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
info.bmiHeader.biWidth = width;
|
||||
// 负高度 = 顶向下 DIB,与解码输出的内存布局一致。
|
||||
info.bmiHeader.biHeight = -height;
|
||||
info.bmiHeader.biPlanes = 1;
|
||||
info.bmiHeader.biBitCount = 32;
|
||||
info.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
SetStretchBltMode(hdc, COLORONCOLOR);
|
||||
StretchDIBits(hdc, offset_x, offset_y, draw_width, draw_height, 0, 0, width,
|
||||
height, frame->data(), &info, DIB_RGB_COLORS, SRCCOPY);
|
||||
}
|
||||
|
||||
EndPaint(hwnd_, &paint);
|
||||
}
|
||||
|
||||
void VideoWindow::ToggleFullScreen() {
|
||||
if (hwnd_ == nullptr) {
|
||||
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;
|
||||
}
|
||||
InvalidateRect(hwnd_, nullptr, FALSE);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef RUNNER_VIDEO_WINDOW_H_
|
||||
#define RUNNER_VIDEO_WINDOW_H_
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
// 独立串流窗口:一个原生 Win32 顶层窗口,直接用 GDI 绘制解码后的 BGRA 帧。
|
||||
//
|
||||
// 它不依赖 Flutter 引擎(不新建引擎、不跨引擎共享纹理),因此可以随意拖动到
|
||||
// 第二块显示器、单独最大化 / 全屏,主窗口最小化也不影响画面。
|
||||
class VideoWindow {
|
||||
public:
|
||||
// 取当前帧;返回空表示暂无画面。返回的 shared_ptr 保证绘制期间帧不会被回收。
|
||||
using FrameProvider =
|
||||
std::function<std::shared_ptr<const std::vector<uint8_t>>(int* width,
|
||||
int* height)>;
|
||||
|
||||
VideoWindow();
|
||||
~VideoWindow();
|
||||
|
||||
// 创建并显示窗口(已存在则只前置)。必须在平台线程(消息循环所在线程)调用。
|
||||
bool Open(FrameProvider provider);
|
||||
|
||||
// 关闭并销毁窗口。必须在平台线程调用。
|
||||
void Close();
|
||||
|
||||
// 解码线程收到新帧时调用,线程安全。
|
||||
void OnFrameAvailable();
|
||||
|
||||
bool IsOpen() const { return open_.load(); }
|
||||
|
||||
private:
|
||||
static LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT message, WPARAM wparam,
|
||||
LPARAM lparam);
|
||||
LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
|
||||
void Paint();
|
||||
void ToggleFullScreen();
|
||||
|
||||
HWND hwnd_ = nullptr;
|
||||
std::atomic<bool> open_{false};
|
||||
std::atomic<bool> repaint_pending_{false};
|
||||
FrameProvider provider_;
|
||||
bool full_screen_ = false;
|
||||
WINDOWPLACEMENT saved_placement_ = {};
|
||||
LONG saved_style_ = 0;
|
||||
};
|
||||
|
||||
#endif // RUNNER_VIDEO_WINDOW_H_
|
||||
Reference in new issue
Block a user