## Problem and outcome / 问题与结果 This phase adds lightweight room interaction without making media depend on chat, and adds Windows source-audio exclusion and optional SFU-only hosting at their existing owners. It also corrects confirmed encoder/capture ownership and native color-conversion defects. Host microphone commentary remains on the shared media path; room voice and 4K are outside this phase. ## Release notes ### 中文 这是一次功能更新:Piik 加入了文字聊天与表情互动,并完善了 Windows 声音采集、画面颜色和自部署选项。 #### 新功能 - **边看边聊。** 在可移动、可缩放的聊天窗口中发送消息,或开启弹幕。停止分享后,房间中的朋友仍可继续聊天。 - **表情互动。** 点击沙发上的人物,给自己发表情,或把番茄等表情送给朋友;各端按同一事件显示动画。 - **排除应用声音。** Windows App 分享整个屏幕时,可排除一个应用及其子进程的声音,适合同时使用语音软件。所选应用退出后,来源声音停止,避免恢复为未过滤的系统声音。 - **可选的纯 SFU 模式。** 自部署管理员可让共享画面和声音统一通过媒体服务器转发。默认仍为优先 P2P;App 公网邀请保持纯 P2P。 - **Windows HDR 转 SDR。** 原生采集与缩略图自动将 HDR 来源转换为 SDR,沿用现有观看链路。 #### 修复与改进 - 修复原生颜色转换的范围与矩阵处理,避免已复现的暗部丢失、高光裁切和颜色偏差。 - 修复共享编码器切换时使用过期需求或过期帧信息的问题,保护正在观看的画面;修复换源后旧采集事件可能影响新来源的问题。 - 统一新控件、浮窗与来源选择文案,补充中英文使用说明。 #### 使用与升级 聊天与表情需要新版站点页面和 Server;声音排除需要新版页面与支持此能力的 Windows App。聊天只保留本页最近收到的消息,刷新后清空,不补发历史,且不提供端到端加密。 HDR 转换输出为 SDR,不代表端到端 HDR 或多声道支持。既有房主麦克风与立体声能力保留。 ### English This feature update brings text chat and reactions to Piik, alongside improvements to Windows audio capture, picture colors and self-hosting options. #### New features - **Chat while watching.** Send messages in a movable, resizable window, or enable on-screen chat. Friends can keep chatting after the host stops sharing. - **Reactions.** Select a person on the sofa to react above yourself or send a tomato and other reactions to a friend. Participants animate the same shared event. - **Exclude application audio.** When sharing a whole screen with Windows App, exclude one application and its child processes, useful when using a separate voice app. Source audio stops if the selected app exits, preventing a return to unfiltered system sound. - **Optional SFU-only hosting.** Site operators can require shared video and audio to pass through the media server. P2P-first remains the default; App Public invite remains P2P-only. - **Windows HDR-to-SDR conversion.** Native capture and source thumbnails automatically convert HDR sources to SDR for the existing viewing path. #### Fixes and improvements - Correct native color-conversion ranges and matrices, addressing reproduced crushed shadows, clipped highlights and color shifts. - Correct stale demand and frame evidence during shared-encoder handoffs to protect the current picture. Prevent events from a replaced capture from affecting its successor. - Align new controls, floating panels and source-picker copy, with updated Chinese and English guides. #### Using this update Chat and reactions require updated site pages and Server. Audio exclusion requires updated pages and a Windows App that supports the capability. Chat retains only recent messages received by the current page, clears on refresh and does not replay history. It is not end-to-end encrypted. HDR conversion produces SDR; it does not add end-to-end HDR or surround sound. Existing host microphone commentary and stereo support remain available. ## Verification / 验证 - Web type checks, 78 test files / 1275 tests and production build: PASS. - Go core tests, formatting/vet and target builds: PASS; Darwin cgo remains platform-limited on this Windows runner. - Native build/check/color probes and synthetic HDR-to-SDR H.264/VP8 browser reception: PASS. - Access/privacy gate, Native H.264 SFU-only media gate and local Auto/H.264 Host gate: PASS. - Three real production pages: chat/reactions, stop/resume, idle-room join, host departure/return, offline draft recovery and invitation revocation: PASS. - Synthetic parent/child process-audio exclusion and target lifetime: PASS; no raw audio retained. - Windows candidate archive extraction and packaged Local startup/shutdown: PASS. - Documentation and website build: PASS. - [Candidate CI](https://github.com/TNTcraftHIM/Piik/actions/runs/36873766646) at `f26cd208d03bc0dcbe353f8cac217f993216b0a3`: Go core and race checks, Web tests, Server/container checks and Windows/macOS/Linux App packaging all PASS. Publication was skipped as intended. The owner accepted the UI/model and bounded local walkthrough for phase closure. Physical HDR/mixed displays, mobile keyboard, real voice applications across output devices and macOS/Linux capture remain unverified. Existing field reports without matched evidence are not claimed resolved. The owner authorized v1.7.0 publication on 2026-10-02. The final documentation-only commit records that authorization; product code matches the verified candidate above. ## Impact / 影响 Authorized version: **v1.7.0**, matching the local release planner. Current wire contracts remain Browser/server 23, Native control 9 and capture 7. Optional capabilities gate new functionality; existing room/media authority remains owned by the existing services. No new ports, message persistence, room voice or automatic room creation on page visit. Updated README, guides, standards, research evidence and TODO stay with the candidate phase.
633 lines
19 KiB
Go
633 lines
19 KiB
Go
package nativecapture
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
|
|
)
|
|
|
|
const (
|
|
captureStopTimeout = time.Second
|
|
maxPreviewBytes = 192 * 1024
|
|
maxOutputs = 6
|
|
minOutputBitrate = 1_000 // Codec rate APIs use whole kbps.
|
|
maxOutputBitrate = 12_000_000
|
|
)
|
|
|
|
var (
|
|
captureStageCode = regexp.MustCompile(`^[a-z][a-z0-9-]{0,63}$`)
|
|
captureHRESULT = regexp.MustCompile(`^0x[0-9a-fA-F]{8}$`)
|
|
)
|
|
|
|
type audioCaptureState struct {
|
|
State string `json:"state"`
|
|
Audio bool `json:"audio"`
|
|
}
|
|
|
|
type CaptureTarget struct {
|
|
Kind string `json:"kind"`
|
|
SourceID string `json:"sourceId"`
|
|
PID uint32 `json:"pid,omitempty"`
|
|
CreationTime string `json:"creationTime,omitempty"`
|
|
Title string `json:"title"`
|
|
}
|
|
|
|
type VideoOptions struct {
|
|
Target CaptureTarget
|
|
Codec string
|
|
AdapterIndex uint32
|
|
EncoderIndex uint32
|
|
Profile VideoProfile
|
|
RestoreToken string
|
|
OutputGroups int
|
|
ShowCaptureBorder bool
|
|
}
|
|
|
|
type EncodedVideoOptions struct {
|
|
Codec string
|
|
AdapterIndex uint32
|
|
EncoderIndex uint32
|
|
Preference string
|
|
Outputs []OutputProfile
|
|
}
|
|
|
|
type VideoProfile struct {
|
|
Width uint32
|
|
Height uint32
|
|
Framerate uint32
|
|
Bitrate uint32
|
|
Preference string
|
|
}
|
|
|
|
// OutputProfile describes one codec output, not a selectable product preset.
|
|
type OutputProfile struct {
|
|
Width uint32 `json:"width"`
|
|
Height uint32 `json:"height"`
|
|
Framerate uint32 `json:"fps"`
|
|
Bitrate uint32 `json:"bitrate"`
|
|
}
|
|
|
|
func (profile OutputProfile) Valid() bool {
|
|
return profile.Width >= 2 && profile.Width <= 2560 && profile.Width%2 == 0 &&
|
|
profile.Height >= 2 && profile.Height <= 1440 && profile.Height%2 == 0 &&
|
|
profile.Framerate >= 1 && profile.Framerate <= 60 &&
|
|
profile.Bitrate >= minOutputBitrate && profile.Bitrate <= maxOutputBitrate
|
|
}
|
|
|
|
// ScreenShareOutputs follows LiveKit 2.22.1's screen-share simulcast construction,
|
|
// with even derived dimensions for NV12. The source profile has already passed Valid.
|
|
func ScreenShareOutputs(profile VideoProfile) []OutputProfile {
|
|
return []OutputProfile{
|
|
{
|
|
Width: profile.Width / 4 * 2, Height: profile.Height / 4 * 2,
|
|
Framerate: profile.Framerate, Bitrate: max(150_000, profile.Bitrate/4),
|
|
},
|
|
{
|
|
Width: profile.Width, Height: profile.Height,
|
|
Framerate: profile.Framerate, Bitrate: profile.Bitrate,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (profile VideoProfile) Valid() bool {
|
|
validResolution :=
|
|
(profile.Width == 854 && profile.Height == 480) ||
|
|
(profile.Width == 1280 && profile.Height == 720) ||
|
|
(profile.Width == 1920 && profile.Height == 1080) ||
|
|
(profile.Width == 2560 && profile.Height == 1440)
|
|
return validResolution && profile.Framerate >= 15 && profile.Framerate <= 60 &&
|
|
profile.Bitrate >= 2_000_000 && profile.Bitrate <= 12_000_000 &&
|
|
validVideoPreference(profile.Preference)
|
|
}
|
|
|
|
func validVideoPreference(preference string) bool {
|
|
return preference == "maintain-resolution" || preference == "balanced" || preference == "maintain-framerate"
|
|
}
|
|
|
|
type Stream struct {
|
|
cancel context.CancelFunc
|
|
input io.ReadCloser
|
|
key io.WriteCloser
|
|
done chan error
|
|
|
|
outputs []OutputProfile
|
|
active [maxOutputs]bool
|
|
bitrates [maxOutputs]uint32
|
|
controlMu sync.Mutex
|
|
closed bool
|
|
closeOnce sync.Once
|
|
readMu sync.Mutex
|
|
readEnd sync.Once
|
|
logger *slog.Logger
|
|
ctx context.Context
|
|
}
|
|
|
|
func ListSources(parent context.Context, executable string) ([]CaptureTarget, error) {
|
|
ctx, cancel := context.WithTimeout(parent, probeTimeout)
|
|
defer cancel()
|
|
stdout := &boundedBuffer{limit: maxProbeOutputBytes}
|
|
command := exec.CommandContext(ctx, executable, "--list")
|
|
command.Stdout = stdout
|
|
stderr := diagnostics.Writer("native-source-list")
|
|
defer stderr.Close()
|
|
command.Stderr = stderr
|
|
hideWindow(command)
|
|
if err := command.Run(); err != nil {
|
|
slog.DebugContext(ctx, "piik-client", "event", "capture-source-list-failed", diagnostics.Error(err), "canceled", ctx.Err() != nil)
|
|
return nil, errors.New("native capture source list is unavailable")
|
|
}
|
|
var targets []CaptureTarget
|
|
if err := decodeStrictJSON(stdout.Bytes(), &targets); err != nil || len(targets) > 1024 {
|
|
return nil, errors.New("native capture source list is invalid")
|
|
}
|
|
for _, target := range targets {
|
|
if !target.Valid() {
|
|
return nil, errors.New("native capture target is invalid")
|
|
}
|
|
}
|
|
return targets, nil
|
|
}
|
|
|
|
func PreviewSource(parent context.Context, executable string, target CaptureTarget) ([]byte, error) {
|
|
if !target.Valid() {
|
|
return nil, errors.New("native preview target is invalid")
|
|
}
|
|
ctx, cancel := context.WithTimeout(parent, probeTimeout)
|
|
defer cancel()
|
|
stdout := &boundedBuffer{limit: maxPreviewBytes}
|
|
command := exec.CommandContext(ctx, executable,
|
|
"--preview",
|
|
target.Kind,
|
|
target.SourceID,
|
|
strconv.FormatUint(uint64(target.PID), 10),
|
|
zeroWhenEmpty(target.CreationTime),
|
|
)
|
|
command.Stdout = stdout
|
|
stderr := diagnostics.Writer("native-source-preview")
|
|
defer stderr.Close()
|
|
command.Stderr = stderr
|
|
hideWindow(command)
|
|
if err := command.Run(); err != nil {
|
|
slog.DebugContext(ctx, "piik-client", "event", "capture-source-preview-failed", diagnostics.Error(err), "canceled", ctx.Err() != nil)
|
|
return nil, errors.New("native capture preview is unavailable")
|
|
}
|
|
preview := stdout.Bytes()
|
|
if len(preview) < 54 || preview[0] != 'B' || preview[1] != 'M' {
|
|
return nil, errors.New("native capture preview is invalid")
|
|
}
|
|
return append([]byte(nil), preview...), nil
|
|
}
|
|
|
|
func StartVideo(parent context.Context, executable string, options VideoOptions) (*Stream, error) {
|
|
if !options.Target.Valid() || !options.Profile.Valid() ||
|
|
(options.Codec != "auto" && options.Codec != "h264" && options.Codec != "vp8") ||
|
|
options.OutputGroups < 0 || options.OutputGroups > maxOutputs-2 ||
|
|
len(options.RestoreToken) > 4096 || !utf8.ValidString(options.RestoreToken) ||
|
|
strings.ContainsRune(options.RestoreToken, 0) {
|
|
return nil, errors.New("native video target is invalid")
|
|
}
|
|
environment := []string(nil)
|
|
if options.RestoreToken != "" {
|
|
environment = []string{"PIIK_XDP_RESTORE_TOKEN=" + options.RestoreToken}
|
|
}
|
|
arguments := []string{
|
|
"--capture-video",
|
|
options.Target.Kind,
|
|
options.Target.SourceID,
|
|
strconv.FormatUint(uint64(options.Target.PID), 10),
|
|
zeroWhenEmpty(options.Target.CreationTime),
|
|
"--adapter-index",
|
|
strconv.FormatUint(uint64(options.AdapterIndex), 10),
|
|
"--mft-index",
|
|
strconv.FormatUint(uint64(options.EncoderIndex), 10),
|
|
"--width",
|
|
strconv.FormatUint(uint64(options.Profile.Width), 10),
|
|
"--height",
|
|
strconv.FormatUint(uint64(options.Profile.Height), 10),
|
|
"--fps",
|
|
strconv.FormatUint(uint64(options.Profile.Framerate), 10),
|
|
"--bitrate",
|
|
strconv.FormatUint(uint64(options.Profile.Bitrate), 10),
|
|
"--preference",
|
|
options.Profile.Preference,
|
|
"--codec",
|
|
options.Codec,
|
|
"--protocol-v7",
|
|
}
|
|
outputs := ScreenShareOutputs(options.Profile)
|
|
for range options.OutputGroups {
|
|
outputs = append(outputs, outputs[0])
|
|
}
|
|
arguments, err := appendOutputArguments(arguments, outputs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if options.ShowCaptureBorder {
|
|
arguments = append(arguments, "--show-capture-border")
|
|
}
|
|
stream, err := startStreamWithEnvironment(parent, executable, arguments, environment)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stream.outputs = outputs
|
|
stream.active[0], stream.active[1] = true, true
|
|
return stream, nil
|
|
}
|
|
|
|
func StartEncodedVideo(parent context.Context, executable string, options EncodedVideoOptions) (*Stream, error) {
|
|
if (options.Codec != "h264" && options.Codec != "vp8") || !validVideoPreference(options.Preference) {
|
|
return nil, errors.New("native encoded video options are invalid")
|
|
}
|
|
arguments, err := appendOutputArguments([]string{
|
|
"--encoded-video", "--codec", options.Codec,
|
|
"--adapter-index", strconv.FormatUint(uint64(options.AdapterIndex), 10),
|
|
"--mft-index", strconv.FormatUint(uint64(options.EncoderIndex), 10),
|
|
"--preference", options.Preference, "--protocol-v7",
|
|
}, options.Outputs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stream, err := startStream(parent, executable, arguments)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stream.outputs = append([]OutputProfile(nil), options.Outputs...)
|
|
return stream, nil
|
|
}
|
|
|
|
func appendOutputArguments(arguments []string, outputs []OutputProfile) ([]string, error) {
|
|
if len(outputs) < 1 || len(outputs) > maxOutputs {
|
|
return nil, errors.New("native output count is invalid")
|
|
}
|
|
for _, output := range outputs {
|
|
if !output.Valid() {
|
|
return nil, errors.New("native output profile is invalid")
|
|
}
|
|
arguments = append(arguments, "--output", strconv.FormatUint(uint64(output.Width), 10),
|
|
strconv.FormatUint(uint64(output.Height), 10), strconv.FormatUint(uint64(output.Framerate), 10), strconv.FormatUint(uint64(output.Bitrate), 10))
|
|
}
|
|
return arguments, nil
|
|
}
|
|
|
|
func StartAudio(parent context.Context, executable string, target CaptureTarget, exclude bool) (*Stream, error) {
|
|
if !target.Valid() || (exclude && target.Kind != "window") {
|
|
return nil, errors.New("native audio target is invalid")
|
|
}
|
|
kind := target.Kind
|
|
if exclude {
|
|
kind = "exclude"
|
|
}
|
|
return startAudioStream(parent, executable, []string{
|
|
"--capture-audio",
|
|
kind,
|
|
strconv.FormatUint(uint64(target.PID), 10),
|
|
zeroWhenEmpty(target.CreationTime),
|
|
})
|
|
}
|
|
|
|
func StartSystemAudio(parent context.Context, executable string) (*Stream, error) {
|
|
return startAudioStream(parent, executable, []string{
|
|
"--capture-audio", "display", "0", "0",
|
|
})
|
|
}
|
|
|
|
func StartMicrophone(parent context.Context, executable, deviceID string) (*Stream, error) {
|
|
if !ValidDeviceID(deviceID) {
|
|
return nil, errors.New("microphone device is invalid")
|
|
}
|
|
arguments := []string{"--capture-microphone"}
|
|
if deviceID != "" {
|
|
arguments = append(arguments, "--device", deviceID)
|
|
}
|
|
return startAudioStreamWithTimeout(parent, executable, arguments, time.Minute)
|
|
}
|
|
|
|
func startAudioStream(parent context.Context, executable string, arguments []string) (*Stream, error) {
|
|
return startAudioStreamWithTimeout(parent, executable, arguments, probeTimeout)
|
|
}
|
|
|
|
func startAudioStreamWithTimeout(parent context.Context, executable string, arguments []string, timeout time.Duration) (*Stream, error) {
|
|
if parent == nil {
|
|
parent = context.Background()
|
|
}
|
|
stream, err := startStream(parent, executable, arguments)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ready := make(chan error, 1)
|
|
go func() {
|
|
frame, readErr := stream.Read()
|
|
if readErr != nil {
|
|
ready <- errors.New("native audio capture did not become ready")
|
|
return
|
|
}
|
|
ready <- validateAudioReadyFrame(frame)
|
|
}()
|
|
timer := time.NewTimer(timeout)
|
|
defer timer.Stop()
|
|
select {
|
|
case readyErr := <-ready:
|
|
if readyErr == nil {
|
|
return stream, nil
|
|
}
|
|
_ = stream.Close()
|
|
return nil, readyErr
|
|
case <-timer.C:
|
|
_ = stream.Close()
|
|
return nil, errors.New("native audio capture timed out during startup")
|
|
case <-parent.Done():
|
|
_ = stream.Close()
|
|
return nil, parent.Err()
|
|
}
|
|
}
|
|
|
|
func (target CaptureTarget) Valid() bool {
|
|
if !positiveDecimal(target.SourceID) || len(target.Title) == 0 ||
|
|
len(target.Title) > 4096 {
|
|
return false
|
|
}
|
|
switch target.Kind {
|
|
case "window":
|
|
return target.PID > 0 && positiveDecimal(target.CreationTime)
|
|
case "display":
|
|
return target.PID == 0 && target.CreationTime == ""
|
|
case "picker":
|
|
return target.PID == 0 && target.CreationTime == ""
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func zeroWhenEmpty(value string) string {
|
|
if value == "" {
|
|
return "0"
|
|
}
|
|
return value
|
|
}
|
|
|
|
func validateAudioReadyFrame(frame Frame) error {
|
|
var state audioCaptureState
|
|
if frame.Kind != FrameStatus || decodeStrictJSON(frame.Data, &state) != nil ||
|
|
state.State != "active" || !state.Audio {
|
|
return errors.New("native audio capture returned an invalid ready state")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func positiveDecimal(value string) bool {
|
|
if value == "" || len(value) > 20 || value[0] == '0' {
|
|
return false
|
|
}
|
|
for _, character := range value {
|
|
if character < '0' || character > '9' {
|
|
return false
|
|
}
|
|
}
|
|
_, err := strconv.ParseUint(value, 10, 64)
|
|
return err == nil
|
|
}
|
|
|
|
func startStream(parent context.Context, executable string, arguments []string) (*Stream, error) {
|
|
return startStreamWithEnvironment(parent, executable, arguments, nil)
|
|
}
|
|
|
|
func startStreamWithEnvironment(
|
|
parent context.Context,
|
|
executable string,
|
|
arguments []string,
|
|
environment []string,
|
|
) (*Stream, error) {
|
|
if parent == nil {
|
|
parent = context.Background()
|
|
}
|
|
if executable == "" {
|
|
return nil, errors.New("native capture process is unavailable")
|
|
}
|
|
if err := parent.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
// Parent cancellation requests the same bounded Q/Close sequence as an
|
|
// explicit stop. Killing first can leave platform capture state behind.
|
|
ctx, cancel := context.WithCancel(context.WithoutCancel(parent))
|
|
started := time.Now()
|
|
// A diagnostic-only process label correlates stderr while old/new captures overlap.
|
|
captureID := strconv.FormatInt(started.UnixNano(), 36)
|
|
logger := slog.Default().With("capture", captureID)
|
|
command := exec.CommandContext(ctx, executable, arguments...)
|
|
if logger.Enabled(ctx, slog.LevelDebug) {
|
|
environment = append(environment, "PIIK_CAPTURE_DEBUG=1")
|
|
}
|
|
if len(environment) > 0 {
|
|
command.Env = append(os.Environ(), environment...)
|
|
}
|
|
stdout, err := command.StdoutPipe()
|
|
if err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
stdin, err := command.StdinPipe()
|
|
if err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
stderr := &boundedBuffer{limit: maxProbeErrorBytes, discardOverflow: true}
|
|
trace := diagnostics.Writer("native-capture/" + captureID)
|
|
command.Stderr = io.MultiWriter(stderr, trace)
|
|
hideWindow(command)
|
|
if err = command.Start(); err != nil {
|
|
_ = trace.Close()
|
|
cancel()
|
|
logger.DebugContext(ctx, "piik-client", "event", "capture-process-start-failed", diagnostics.Error(err))
|
|
return nil, fmt.Errorf("start native capture process: %w", err)
|
|
}
|
|
logger = logger.With("pid", command.Process.Pid)
|
|
mode := "capture"
|
|
if len(arguments) > 0 {
|
|
switch arguments[0] {
|
|
case "--capture-video", "--capture-audio", "--encoded-video":
|
|
mode = strings.TrimPrefix(arguments[0], "--")
|
|
}
|
|
}
|
|
logger.DebugContext(ctx, "piik-client", "event", "capture-process-started", "mode", mode)
|
|
stream := &Stream{
|
|
cancel: cancel,
|
|
input: stdout,
|
|
key: stdin,
|
|
done: make(chan error, 1),
|
|
logger: logger,
|
|
ctx: ctx,
|
|
}
|
|
stopParent := context.AfterFunc(parent, func() { _ = stream.Close() })
|
|
go func() {
|
|
waitErr := command.Wait()
|
|
stopParent()
|
|
_ = trace.Close()
|
|
logCaptureFailure(ctx, waitErr, command.ProcessState.ExitCode(), stderr.Bytes())
|
|
logger.DebugContext(ctx, "piik-client", "event", "capture-process-ended", "mode", mode,
|
|
"durationMs", time.Since(started).Milliseconds(), "exitCode", command.ProcessState.ExitCode(),
|
|
"canceled", ctx.Err() != nil, "cancelReason", diagnostics.SafeText(fmt.Sprint(context.Cause(ctx))), diagnostics.Error(waitErr))
|
|
stream.done <- waitErr
|
|
close(stream.done)
|
|
}()
|
|
return stream, nil
|
|
}
|
|
|
|
func logCaptureFailure(ctx context.Context, err error, exitCode int, stderr []byte) {
|
|
if err == nil || !slog.Default().Enabled(ctx, slog.LevelDebug) {
|
|
return
|
|
}
|
|
stage, hresult := "", ""
|
|
for _, line := range strings.Split(string(stderr), "\n") {
|
|
key, value, _ := strings.Cut(strings.TrimSuffix(line, "\r"), "=")
|
|
switch {
|
|
case key == "stage" && stage == "" && captureStageCode.MatchString(value):
|
|
stage = value
|
|
case key == "hresult" && hresult == "" && captureHRESULT.MatchString(value):
|
|
hresult = strings.ToLower(value)
|
|
}
|
|
}
|
|
canceled := ctx.Err() != nil
|
|
// A real child failure may race owner cancellation. Keep its fixed codes,
|
|
// while ordinary cancellation without a reported failure stays silent.
|
|
if canceled && stage == "" && hresult == "" {
|
|
return
|
|
}
|
|
slog.DebugContext(ctx, "piik-client", "event", "capture-process-failed",
|
|
"exitCode", exitCode, "stage", stage, "hresult", hresult, "canceled", canceled)
|
|
}
|
|
|
|
func (stream *Stream) Read() (Frame, error) {
|
|
stream.readMu.Lock()
|
|
frame, err := readFrame(stream.input)
|
|
stream.readMu.Unlock()
|
|
if err != nil && stream.logger != nil {
|
|
stream.readEnd.Do(func() {
|
|
stream.logger.DebugContext(stream.ctx, "piik-client", "event", "capture-stream-ended",
|
|
"eof", errors.Is(err, io.EOF), "canceled", stream.ctx.Err() != nil, diagnostics.Error(err))
|
|
})
|
|
}
|
|
return frame, err
|
|
}
|
|
|
|
// WriteFrame shares the existing stdin lock with control. Only encoded video
|
|
// crosses this input boundary; capture status and PCM remain producer outputs.
|
|
func (stream *Stream) WriteFrame(frame Frame) error {
|
|
if frame.Kind != FrameH264 && frame.Kind != FrameVP8 {
|
|
return errors.New("native encoded input is not video")
|
|
}
|
|
stream.controlMu.Lock()
|
|
defer stream.controlMu.Unlock()
|
|
if stream.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
return writeFrame(stream.key, frame)
|
|
}
|
|
|
|
func (stream *Stream) Outputs() []OutputProfile {
|
|
return append([]OutputProfile(nil), stream.outputs...)
|
|
}
|
|
|
|
func (stream *Stream) RequestKeyFrame(layer int) error {
|
|
if layer < -1 || layer >= len(stream.outputs) || len(stream.outputs) == 0 {
|
|
return errors.New("native recovery output is invalid")
|
|
}
|
|
return stream.writeControl("K " + strconv.Itoa(layer))
|
|
}
|
|
|
|
func (stream *Stream) SetOutputActive(layer int, active bool) error {
|
|
if layer < 0 || layer >= len(stream.outputs) {
|
|
return errors.New("native active output is invalid")
|
|
}
|
|
stream.controlMu.Lock()
|
|
defer stream.controlMu.Unlock()
|
|
if stream.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
if stream.active[layer] == active {
|
|
return nil
|
|
}
|
|
value := "0"
|
|
if active {
|
|
value = "1"
|
|
}
|
|
if err := stream.writeControlLocked("A " + strconv.Itoa(layer) + " " + value); err != nil {
|
|
return err
|
|
}
|
|
stream.active[layer] = active
|
|
return nil
|
|
}
|
|
|
|
func (stream *Stream) SetOutputBitrate(layer int, bitrate uint32) error {
|
|
if layer < 0 || layer >= len(stream.outputs) || bitrate < minOutputBitrate || bitrate > stream.outputs[layer].Bitrate {
|
|
return errors.New("native output bitrate is invalid")
|
|
}
|
|
stream.controlMu.Lock()
|
|
defer stream.controlMu.Unlock()
|
|
if stream.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
if stream.bitrates[layer] == bitrate {
|
|
return nil
|
|
}
|
|
if err := stream.writeControlLocked("B " + strconv.Itoa(layer) + " " + strconv.FormatUint(uint64(bitrate), 10)); err != nil {
|
|
return err
|
|
}
|
|
stream.bitrates[layer] = bitrate
|
|
return nil
|
|
}
|
|
|
|
func (stream *Stream) writeControl(command string) error {
|
|
stream.controlMu.Lock()
|
|
defer stream.controlMu.Unlock()
|
|
return stream.writeControlLocked(command)
|
|
}
|
|
|
|
func (stream *Stream) writeControlLocked(command string) error {
|
|
if stream.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
return writeFrame(stream.key, Frame{Kind: FrameControl, Data: []byte(command)})
|
|
}
|
|
|
|
func (stream *Stream) Done() <-chan error {
|
|
return stream.done
|
|
}
|
|
|
|
func (stream *Stream) Close() error {
|
|
stream.closeOnce.Do(func() {
|
|
// Start the stop budget before waiting for a possibly blocked control write.
|
|
timer := time.AfterFunc(captureStopTimeout, stream.cancel)
|
|
defer timer.Stop()
|
|
// A retired consumer must not leave the producer blocked on stdout before
|
|
// it can read Q. Finish any in-flight frame before discarding the rest.
|
|
drained := make(chan struct{})
|
|
go func() {
|
|
stream.readMu.Lock()
|
|
_, _ = io.Copy(io.Discard, stream.input)
|
|
stream.readMu.Unlock()
|
|
close(drained)
|
|
}()
|
|
stream.controlMu.Lock()
|
|
stream.closed = true
|
|
_ = writeFrame(stream.key, Frame{Kind: FrameControl, Data: []byte("Q")})
|
|
_ = stream.key.Close()
|
|
stream.controlMu.Unlock()
|
|
<-stream.done
|
|
stream.cancel()
|
|
<-drained
|
|
})
|
|
return nil
|
|
}
|