- 新增 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
226 lines
6.8 KiB
C++
226 lines
6.8 KiB
C++
#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);
|
|
}
|