Files
SimpleRemoter/client/X264Encoder.h
T
yuanyuanxiang ca4e85826f Feature: remote desktop tuning flags, on by default with per-item opt-out
Adds "Advanced -> Remote desktop tuning" with three independently
checkable items, all enabled by default:

  x264 bitrate cap (VBV)       TUNING_X264_VBV        0x1
  GDI capture: no slice delay  TUNING_GDI_NO_SLEEP    0x2
  Send pacing FPS floor        TUNING_PACE_FPS_FLOOR  0x4

Unticking an item clears its bit and restores the pre-commit behaviour
exactly, so the escape hatch is one click per measure -- no rebuild, no
config edit. Each bit is independent; a problem with one does not force
turning all three off.

Plumbing
--------
Menu state -> [settings] Tuning* -> CMy2015RemoteDlg::GetScreenTuningFlags()
  -> CScreenSpyDlg::SendScreenTuning()  (right after TOKEN_BITMAPINFO)
  -> CMD_SCREEN_TUNING = 178 [cmd:1][flags:4 LE]
  -> CScreenManager::OnReceive -> m_nTuningFlags -> ScreenCapture::SetTuningFlags

Why it is safe to send to old clients: CScreenManager::OnReceive has no
default branch in its switch, so an unknown command falls straight out
(verified). 178 was unassigned (177 = COMMAND_SET_PROTO, 199 =
TOKEN_DECRYPT), and the enum re-anchors explicitly so nothing auto-
increments onto it.

Flags are sent even when all-zero on purpose: the client's flag lives in
CScreenManager, which survives closing and reopening the remote desktop
window on the same connection. Without the zero send, unticking the menu
would leave the previous flags in effect.

Per-measure notes
-----------------
VBV: capped CRF (peak = target, 0.5s buffer, init 0.9). The hardware
paths already cap via rc_max_rate (1.5x, 1s buffer); this is tighter and
only affects x264. After an IDR the buffer drains and takes up to 0.5s
to refill, so a brief quality dip follows every keyframe -- if that shows
up, relaxing to 1.5x/1s would match NVENC/QSV.

GDI: keeps the 50-line BitBlt slicing (large single BitBlts hit driver
limits) and only drops the 5ms sleep between slices -- 1080p is 22 slices
= a forced 110ms per frame against a 50ms budget at 20fps.

Pacing: clamps s0 to 2x the base interval instead of 4x (2fps -> 4fps),
taken as max(2x base, 1000/MaxFPS) so it never exceeds the configured
MaxFPS. The growth/decay code itself is untouched. This one works against
the loop's own congestion signal, so it is the first to untick if a bad
link feels laggier rather than smoother.

SetTuningFlags only rebuilds the encoder when TUNING_ENCODER_BITS
(currently just VBV) changes -- the GDI and pacing bits are read live by
the capture and send threads, and rebuilding for them would drop an IDR
and visibly flash the picture.

Defaults: the on-by-default value lives in the third argument of
THIS_CFG.GetInt(), which only applies when the key is absent. A machine
that already has an explicit 0 keeps it, so the flip is not retroactive
and a test machine can end up with a mixed state.

Verified on the Debug build: with no Tuning* keys all three items start
checked; unticking VBV alone leaves the other two checked and survives a
restart; en_US/de_DE/zh_TW translations all resolve. Not yet measured end
to end on a real remote desktop session.
2026-11-04 14:47:40 +01:00

88 lines
2.8 KiB
C++

#pragma once
#include "VideoEncoderBase.h"
// 必须在下面的条件编译之前:DISABLE_X264_FOR_TEST 定义在这里。
// 原来它排在 x264 头文件之后,导致开关无论怎么设,x264.h 都已经被拉进来了。
#include "common/config.h"
#if DISABLE_X264_FOR_TEST
// 合规构建(DISABLE_X264_FOR_TEST=1):不包含 x264 / libyuv 头文件,不声明任何
// x264 类型,整棵源码树不含 GPL 头文件,也不链接 libx264。
//
// 类仍保留基类接口(EncoderFactory::CreateEncoder 通过 VideoEncoderBase 指针创建
// 并调用),但 open() 恒返回 false,由 CreateEncoder 自然回退到硬编路径;
// 若硬编也不可用则返回 nullptr,调用方按“无编码器”处理。
class CX264Encoder : public VideoEncoderBase
{
private:
bool m_forceIDR; // 保留成员以对齐两套实现的行为,桩里不使用
public:
// 旧签名保留:与非合规构建保持同一份对外接口
bool open(int width, int height, int fps, int crf, int vbv_kbps = 0);
// VideoEncoderBase
bool open(const EncoderParams& params) override;
void close() override;
int encode(
uint8_t * rgb,
uint8_t bpp,
uint32_t stride,
uint32_t width,
uint32_t height,
uint8_t ** lppData,
uint32_t * lpSize,
int direction = 1
) override;
void forceIDR() override { m_forceIDR = true; }
VideoCodec codec() const override { return VideoCodec::H264; }
const char* backendName() const override { return "x264"; }
CX264Encoder();
~CX264Encoder() override;
};
#else
extern "C" {
#include <libyuv\libyuv.h>
#include <x264\x264.h>
}
class CX264Encoder : public VideoEncoderBase
{
private:
x264_t* m_pCodec; //编码器实例
x264_picture_t *m_pPicIn;
x264_picture_t *m_pPicOut;
x264_param_t m_Param;
bool m_forceIDR; // 下一次 encode 强制 IDR
public:
// 旧签名保留:被 ScreenCapture 临时直接调;新增 EncoderParams overload 走接口路径
// vbv_kbps > 0 时给 CRF 加 VBV 峰值上限(capped CRF),0 = 不加(历史行为)
bool open(int width, int height, int fps, int crf, int vbv_kbps = 0);
bool open(x264_param_t * param);
// VideoEncoderBase
bool open(const EncoderParams& params) override;
void close() override;
int encode(
uint8_t * rgb,
uint8_t bpp,
uint32_t stride,
uint32_t width,
uint32_t height,
uint8_t ** lppData,
uint32_t * lpSize,
int direction = 1
) override;
void forceIDR() override { m_forceIDR = true; }
VideoCodec codec() const override { return VideoCodec::H264; }
const char* backendName() const override { return "x264"; }
CX264Encoder();
~CX264Encoder() override;
};
#endif // DISABLE_X264_FOR_TEST