Files
piik/native/capture/windows/frame_converter.probe.cpp
T
AAA da7218bf44 feat(voice): add room voice and secure local invitations
## Problem and outcome / 问题与结果

Planned release: **v2.3.0**, preserving the published 2.x sharing contracts.

Friends can now talk in a Piik room before, during or after screen sharing. Room
voice, media forwarding and the picture retain separate lifetimes under the
existing room authority and route controller. This phase also improves direct LAN
entry, native capture startup, interaction controls and first-use documentation.

Related: #484, #437, #486, #488, #485 and #489. These references do not declare
all reported hardware or distribution problems fixed.

## Release notes

**Less typing. More talking.**

Piik now supports room voice, with or without screen sharing.

**聊不够?开麦吧!**

Piik 现已支持房间语音,不开播也能聊。

### English

This update adds room voice alongside sharing, chat and reactions, with clearer
controls and direct HTTPS invitations for local and virtual networks.

#### Highlights

- Create a room before sharing. The Host opens or closes room voice; friends
  can join, control their own microphone and listening, or mute someone locally.
  Joining voice keeps the microphone off until you enable it.
- Choose your microphone, adjust microphone, master and individual listening
  volumes from 0% to 200%, and enable voice processing in the interaction window.
  Audio icons offer quick volume sliders. The microphone icon shows live input
  level. Voice continues when the picture stops.
- Use a local HTTPS invitation for microphone and remote-control features on a
  LAN or virtual LAN. A temporary local certificate requires a browser warning
  confirmation; a self-hosted Server can also use supplied certificate files.
- Rotate your local picture, optionally volunteer more relay capacity, and
  download a native Windows x64 Server package.

#### Fixes and polish

- Align Windows automatic encoder probes with the surfaces used during actual
  capture; keep video capture progressing while optional source audio starts.
- Correct Linux desktop pixel-aspect negotiation and accept fitted native
  source dimensions without forcing a different aspect ratio.
- Avoid retaining false media queue congestion when producer and consumer
  clocks disagree.
- Distinguish chat authors with identical names, clarify remote permissions,
  and keep floating tools, contextual hints and microphone indicators aligned.
- Separate first-use guides, room tools and troubleshooting so help is easier
  to find.

Use updated Server/Web pages for room voice and update the App for the native
capture and local HTTPS improvements. Established 2.x sharing remains supported.
macOS builds do not yet carry Developer ID signing or Apple notarization; see the first-launch instructions.
The macOS audio-startup changes still need confirmation on an affected Mac.

### 中文

本次更新为分享、聊天和表情互动加入房间语音,同时打磨操作控件,并为局域网和虚拟局域网提供直接 HTTPS 邀请。

#### 主要变化

- 不开播也能创建房间。房主开启或关闭房间语音;朋友可以加入、自行开关麦克风和收听,或仅在自己这边静音某人。加入语音时默认不开麦。
- 在互动窗口选择麦克风、分别调整 0%–200% 的麦克风、总收听与个人音量,并设置语音处理。音频图标可展开快捷音量条,麦克风图标显示实时输入音量。停止画面分享后,语音仍可继续。
- 局域网和虚拟局域网可使用本地 HTTPS 邀请,启用麦克风和远程控制;临时本地证书需要在浏览器确认警告。自部署服务端也可使用自行提供的证书文件。
- 可旋转本地观看画面、自愿增加转发容量,并提供 Windows x64 服务端下载包。

#### 修复与打磨

- Windows 自动编码探测使用与实际采集一致的输入;可选来源音频启动期间,画面采集继续运行。
- 修正 Linux 桌面采集的像素比例协商;原生来源保留实际画幅,不再要求固定比例的尺寸。
- 避免生产者和消费者时钟不一致时,错误保留媒体队列拥塞状态。
- 区分同名聊天作者,明确远控授权,并统一浮窗、上下文提示与麦克风指示器的细节。
- 分开首次使用、房间工具和故障排查文档,让帮助更容易查找。

房间语音需使用更新后的服务端和网页;原生采集与本地 HTTPS 改进需更新 App。既有 2.x 分享仍保持兼容。
macOS 构建仍未正式签名和公证,首次启动请参照文档;音频启动调整仍需在受影响的 Mac 上确认。

## Verification / 验证

Full Web checks, Go tests/vet/build/native probes, reachable vulnerability checks,
normal-page P2P/SFU voice and compatible sharing, LAN HTTPS, detached interaction
tools, responsive controls and tooltip/pure-visual checks provide acceptance
evidence. Physical macOS/Linux capture and acoustic behavior on arbitrary devices
are not established by these controls.

Scoped contributions from #488 (Linux pixel aspect) and #485 (Windows Server
packaging) are retained as their original authors' patch commits in this PR.
The remaining implementation and integration belong to the maintainer.

## Impact / 影响

Preserve the published 2.x sharing contract. New room-voice behavior is opt-in;
Host voice activation, capture and picture lifetimes remain distinct. Local
certificate material remains in memory; no system trust installation is performed.
2026-10-21 06:51:22 -04:00

252 lines
12 KiB
C++

// Physical D3D11 check; opt in with build.ps1 -CheckColor. No desktop capture.
#define wmain capture_main
#include "main.cpp"
#undef wmain
#include <DirectXPackedVector.h>
namespace {
constexpr std::array<UINT32, 8> kRgb{
0xff000000, 0xff202020, 0xff808080, 0xffebebeb,
0xffffffff, 0xffff0000, 0xff00ff00, 0xff0000ff};
constexpr std::array<std::array<int, 3>, 8> kYuv{{
{16, 128, 128}, {44, 128, 128}, {126, 128, 128}, {218, 128, 128},
{235, 128, 128}, {82, 90, 240}, {145, 54, 34}, {41, 240, 110}}};
constexpr std::array<std::array<int, 3>, 8> kYuv709{{
{16, 128, 128}, {44, 128, 128}, {126, 128, 128}, {218, 128, 128},
{235, 128, 128}, {63, 102, 240}, {173, 42, 26}, {32, 240, 118}}};
constexpr std::array<std::array<int, 3>, 8> kYuvFull{{
{0, 128, 128}, {32, 128, 128}, {128, 128, 128}, {235, 128, 128},
{255, 128, 128}, {76, 85, 255}, {150, 44, 21}, {29, 255, 107}}};
constexpr std::array<std::array<int, 3>, 8> kYuv709Full{{
{0, 128, 128}, {32, 128, 128}, {128, 128, 128}, {235, 128, 128},
{255, 128, 128}, {54, 99, 255}, {182, 30, 12}, {18, 255, 116}}};
ComPtr<ID3D11Texture2D> ColorBars(const DeviceContext& device, UINT width,
UINT height, DXGI_COLOR_SPACE_TYPE color) {
const bool rgb = color == kDesktopColor;
const auto& yuv = color == DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709 ? kYuv709
: color == DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P709 ? kYuv709Full
: color == DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P601 ? kYuvFull : kYuv;
std::vector<UINT8> pixels(static_cast<size_t>(width) * height * (rgb ? 4 : 2));
for (UINT row = 0; row < height; ++row) {
for (UINT column = 0; column < width; ++column) {
const size_t bar = column * kRgb.size() / width;
const size_t index = static_cast<size_t>(row) * width + column;
if (rgb) {
for (UINT channel = 0; channel < 4; ++channel)
pixels[index * 4 + channel] = static_cast<UINT8>(kRgb[bar] >> (channel * 8));
} else {
pixels[index] = static_cast<UINT8>(yuv[bar][0]);
if (row < height / 2)
pixels[static_cast<size_t>(width) * height + index] =
static_cast<UINT8>(yuv[bar][1 + column % 2]);
}
}
}
D3D11_TEXTURE2D_DESC description{};
description.Width = width;
description.Height = height;
description.MipLevels = description.ArraySize = description.SampleDesc.Count = 1;
description.Format = rgb ? DXGI_FORMAT_B8G8R8A8_UNORM : DXGI_FORMAT_NV12;
description.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
D3D11_SUBRESOURCE_DATA data{};
data.pSysMem = pixels.data();
data.SysMemPitch = width * (rgb ? 4 : 1);
ComPtr<ID3D11Texture2D> texture;
Check(device.device->CreateTexture2D(&description, &data, &texture), "color-source");
return texture;
}
void CheckColors(const DeviceContext& device, ID3D11Texture2D* texture) {
D3D11_TEXTURE2D_DESC description{};
texture->GetDesc(&description);
description.Usage = D3D11_USAGE_STAGING;
description.BindFlags = 0;
description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
ComPtr<ID3D11Texture2D> staging;
Check(device.device->CreateTexture2D(&description, nullptr, &staging), "color-readback");
device.context->CopyResource(staging.Get(), texture);
D3D11_MAPPED_SUBRESOURCE mapped{};
Check(device.context->Map(staging.Get(), 0, D3D11_MAP_READ, 0, &mapped), "color-map");
const auto* bytes = static_cast<const UINT8*>(mapped.pData);
bool correct = true;
for (size_t bar = 0; bar < kYuv.size(); ++bar) {
const auto x = description.Width * (2 * bar + 1) / (2 * kYuv.size());
const size_t y = description.Height / 2;
const size_t uv = (description.Height + y / 2) * mapped.RowPitch + (x & ~size_t{1});
const std::array<int, 3> actual{bytes[y * mapped.RowPitch + x], bytes[uv], bytes[uv + 1]};
for (size_t channel = 0; channel < 3; ++channel) {
if (std::abs(actual[channel] - kYuv[bar][channel]) > 2) {
std::cerr << "bar=" << bar << " channel=" << channel
<< " expected=" << kYuv[bar][channel] << " actual=" << actual[channel] << '\n';
correct = false;
}
}
}
device.context->Unmap(staging.Get(), 0);
if (!correct) throw std::runtime_error("SDR range or matrix changed");
}
void CheckEncoderProbeTextures(const DeviceContext& device) {
const VideoProfile profile{256, 128, 30, 3'000'000};
const auto textures = PrepareEncoderProbeTextures(device.device.Get(), profile);
std::array<UINT64, 8> fingerprints{};
for (size_t index = 0; index < textures.size(); ++index) {
D3D11_TEXTURE2D_DESC description{};
textures[index]->GetDesc(&description);
if (description.Width != profile.width || description.Height != profile.height ||
description.Format != DXGI_FORMAT_NV12 ||
description.BindFlags != D3D11_BIND_RENDER_TARGET)
throw std::runtime_error("Encoder probe does not use live converter surfaces");
description.Usage = D3D11_USAGE_STAGING;
description.BindFlags = 0;
description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
ComPtr<ID3D11Texture2D> staging;
Check(device.device->CreateTexture2D(&description, nullptr, &staging), "probe-readback");
device.context->CopyResource(staging.Get(), textures[index].Get());
D3D11_MAPPED_SUBRESOURCE mapped{};
Check(device.context->Map(staging.Get(), 0, D3D11_MAP_READ, 0, &mapped), "probe-map");
UINT64 fingerprint = 14695981039346656037ull;
const auto* bytes = static_cast<const UINT8*>(mapped.pData);
for (UINT32 row = 0; row < profile.height * 3 / 2; ++row)
for (UINT32 column = 0; column < profile.width; ++column)
fingerprint = (fingerprint ^ bytes[row * mapped.RowPitch + column]) * 1099511628211ull;
device.context->Unmap(staging.Get(), 0);
for (size_t earlier = 0; earlier < index; ++earlier)
if (textures[index].Get() == textures[earlier].Get() || fingerprints[earlier] == fingerprint)
throw std::runtime_error("Encoder probe frames lost their distinct retained contents");
fingerprints[index] = fingerprint;
}
}
ComPtr<ID3D11Texture2D> LinearBars(const DeviceContext& device, UINT width,
UINT height, const std::array<float, 8>& nits) {
std::vector<UINT16> pixels(static_cast<size_t>(width) * height * 4);
for (size_t pixel = 0; pixel < static_cast<size_t>(width) * height; ++pixel) {
const auto value = DirectX::PackedVector::XMConvertFloatToHalf(nits[(pixel % width) * 8 / width] / 80.0f);
for (size_t channel = 0; channel < 3; ++channel) pixels[pixel * 4 + channel] = value;
pixels[pixel * 4 + 3] = DirectX::PackedVector::XMConvertFloatToHalf(1.0f);
}
D3D11_TEXTURE2D_DESC description{};
description.Width = width;
description.Height = height;
description.MipLevels = description.ArraySize = description.SampleDesc.Count = 1;
description.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
description.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
D3D11_SUBRESOURCE_DATA data{};
data.pSysMem = pixels.data();
data.SysMemPitch = width * 8;
ComPtr<ID3D11Texture2D> texture;
Check(device.device->CreateTexture2D(&description, &data, &texture), "hdr-source");
return texture;
}
std::array<int, 8> ReadGrayBars(const DeviceContext& device, ID3D11Texture2D* texture) {
D3D11_TEXTURE2D_DESC description{};
texture->GetDesc(&description);
description.Usage = D3D11_USAGE_STAGING;
description.BindFlags = 0;
description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
ComPtr<ID3D11Texture2D> staging;
Check(device.device->CreateTexture2D(&description, nullptr, &staging), "hdr-readback");
device.context->CopyResource(staging.Get(), texture);
D3D11_MAPPED_SUBRESOURCE mapped{};
Check(device.context->Map(staging.Get(), 0, D3D11_MAP_READ, 0, &mapped), "hdr-map");
const auto* bytes = static_cast<const UINT8*>(mapped.pData);
std::array<int, 8> result{};
bool neutral = true;
for (size_t bar = 0; bar < result.size(); ++bar) {
const size_t pixel = description.Height / 2 * mapped.RowPitch +
description.Width * (2 * bar + 1) / 16 * 4;
result[bar] = bytes[pixel];
neutral = neutral && std::abs(result[bar] - bytes[pixel + 1]) <= 1 &&
std::abs(result[bar] - bytes[pixel + 2]) <= 1;
}
device.context->Unmap(staging.Get(), 0);
if (!neutral) throw std::runtime_error("HDR gray acquired a color cast");
return result;
}
void CheckHdr(const DeviceContext& device) {
CaptureSdrConverter capture(device.device.Get());
VideoProfile profile;
profile.width = 256;
profile.height = 128;
FrameConverter converter(device.device.Get(), profile);
for (const UINT width : {256u, 512u, 256u}) {
// SDR must retain the old pixels after an HDR frame, with the same owner.
auto sdr = ColorBars(device, width, width / 2, kDesktopColor);
auto owned = capture.Convert(sdr.Get(), {});
CheckColors(device, converter.Convert(owned.Get(), width, width / 2,
{static_cast<LONG>(width), static_cast<LONG>(width / 2)}, kDesktopColor).Get());
auto hdr = LinearBars(device, width, width / 2, {0, 5, 20, 80, 200, 400, 700, 1000});
for (const float white : {80.0f, 240.0f, 80.0f}) {
auto mapped = capture.Convert(hdr.Get(), {true, white, 1000.0f});
const auto levels = ReadGrayBars(device, mapped.Get());
std::cout << "HDR white=" << white << " levels=";
for (const auto level : levels) std::cout << level << ',';
std::cout << '\n';
if (levels.front() > 1 || levels.back() < 245)
throw std::runtime_error("HDR black/peak mapping is incorrect");
for (size_t index = 1; index < levels.size(); ++index)
if (levels[index] <= levels[index - 1])
throw std::runtime_error("HDR luminance detail was clipped before tone mapping");
auto next = capture.Convert(sdr.Get(), {});
if (ReadGrayBars(device, mapped.Get()) != levels)
throw std::runtime_error("Capture replacement modified a retained frame");
CheckColors(device, converter.Convert(next.Get(), width, width / 2,
{static_cast<LONG>(width), static_cast<LONG>(width / 2)}, kDesktopColor).Get());
}
}
}
} // namespace
int main() {
try {
Runtime runtime;
const auto adapters = EnumerateAdapters();
if (adapters.empty()) throw std::runtime_error("A physical D3D11 adapter is required");
for (const auto& adapter : adapters) {
auto device = CreateDevice(adapter);
CheckEncoderProbeTextures(device);
for (const UINT output_width : {256u, 128u}) {
VideoProfile profile;
profile.width = output_width;
profile.height = output_width / 2;
FrameConverter converter(device.device.Get(), profile);
// Reuse the same converter across RGB/NV12 and size replacements.
for (const UINT input_width : {256u, 512u, 256u}) {
for (const auto color : {kDesktopColor, kSdrVideoColor,
DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709,
DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P601,
DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P709, kDesktopColor}) {
const UINT input_height = input_width / 2;
auto source = ColorBars(device, input_width, input_height, color);
auto output = converter.Convert(source.Get(), input_width, input_height,
{static_cast<LONG>(input_width), static_cast<LONG>(input_height)},
color);
try {
CheckColors(device, output.Get());
} catch (...) {
std::cerr << "adapter=" << adapter.index << " color=" << color
<< " input-width=" << input_width << " output-width=" << output_width << '\n';
throw;
}
}
}
}
std::cout << "SDR conversion passed: " << NarrowAscii(adapter.description.Description) << '\n';
CheckHdr(device);
std::cout << "HDR conversion passed: " << NarrowAscii(adapter.description.Description) << '\n';
}
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
if (const auto* failure = dynamic_cast<const GateFailure*>(&error))
std::cerr << "stage=" << failure->stage() << " result=" << std::hex << failure->result() << '\n';
return 1;
}
}