Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

feat(app): bring Android sharing and touch control into the shared runtime (#504)

AAA499789671c75
diff --git a/.agents/skills/ponytail/SKILL.md b/.agents/skills/ponytail/SKILL.md
index 2f6a3ce4..75072639 100644
--- a/.agents/skills/ponytail/SKILL.md
+++ b/.agents/skills/ponytail/SKILL.md
@@ -42,16 +42,10 @@ count. A larger correction is justified when it removes competing writers or
finishes the affected flow. Keep readable code and clear boundaries rather than
compressing logic into a one-liner or one oversized file.
-Add an abstraction, setting or extension point only for a current responsibility,
-real consumer or accepted contract. Do not build scaffolding for hypothetical
-features. Preserve necessary platform adapters; a single implementation alone
-does not prove an interface is wasteful. Inspect existing protections before
-removing them, and preserve authorization, data integrity, accessibility and
-published compatibility.
-
-Comment non-obvious constraints and reasons at their owner. Avoid slogans,
-restated code or speculative upgrade plans in comments. Remove replaced private
-paths within scope; keep useful provenance and required license notices.
+Apply the shared [implementation rules](../../../docs/standards/engineering.md#implementation-rules)
+for helper boundaries, typed validation, comments and generated sources rather
+than maintaining separate coding rules in this skill. Preserve useful provenance
+and required license notices when removing replaced paths.
## Verify And Finish
diff --git a/.agents/skills/stop-that-shit/SKILL.md b/.agents/skills/stop-that-shit/SKILL.md
index fef94f86..abc0d4b4 100644
--- a/.agents/skills/stop-that-shit/SKILL.md
+++ b/.agents/skills/stop-that-shit/SKILL.md
@@ -25,11 +25,6 @@ Piik 是供朋友分享的小项目。优先简单、好用、漂亮和实际效
- **让研究产生实现。** 参考相关的一手实现,选出简单、可逆的方案就做对照。
一旦信息足够做决定,停止搜集同类材料。上游也有问题不等于只能接受;检查现有配置、
接口和小范围补丁。研究或审查任务仍按其授权范围交付,不自动变成实现。
-- **防御放在真实边界。** 保留鉴权、外部输入校验和必要的资源释放;内部已验证事实直接使用。
- 不逐层重复校验,不用空值、假成功或静默兜底掩盖错误。现有保护的用途不明时先追踪,
- 不能凭“没出过事”删除。新增重试、状态、开关或限制要解决实际问题或已承诺的边界,
- 不必等到事故发生,也不为假想规模或未来消费者铺路。兼容承诺由
- [版本规范](../../../docs/standards/versioning.md) 决定。
- **新机制要验完整。** 沿实际调用链验证预期效果及受影响的主路径,覆盖相关的失败、恢复
和生命周期行为;不能用一个 happy path 或孤立单测宣称完成。复用现有检查和复现,缺口
补有效回归。小文案、布局和可逆低影响改动不默认新增单元测试;不为每个分支、私有函数
@@ -46,16 +41,5 @@ Piik 是供朋友分享的小项目。优先简单、好用、漂亮和实际效
不把未知说成已解决,也不把所有未知都变成发布门槛。汇报结果和影响决定的限制即可,
不用测试数量、长免责声明、反复道歉或未来待办替代成果。
-## 用在 Piik 上
-
-| 遇到的问题 | 应采取的动作 |
-| --- | --- |
-| 画质恢复慢 | 对比现有适配策略的恢复时间与持续弱网表现;有明确收益就采用。不要仅因上游也慢而停手,也不要另造周期重置器。 |
-| 小控件错位 | 修布局,查看受影响的尺寸与状态;不启动全仓库网络验收。 |
-| 同一事实到处加判断 | 修正它的生产者和生命周期,让消费者使用可靠结果;删掉重复分支。 |
-| 检查已经通过 | 完成当前交付;只有新变化、失败或尚未回答的实际问题才扩大验证。 |
-
-这些是做判断的尺度,不要求为它们生成清单、报告或新工具。
-
参考来源与取舍保留在[上下文治理研究](../../../docs/research/agent-context-governance.md#instructions-and-skills),
不作为每次任务必读材料。
diff --git a/.env.example b/.env.example
index b6997ab1..ced88a56 100644
--- a/.env.example
+++ b/.env.example
@@ -44,7 +44,8 @@ ENDPOINT_MEDIA_COPY_CAPACITY=2
SFU_UDP_PORT=
# IPv4 bind address, independent of HTTP LISTEN_HOST and advertised media IP.
SFU_LISTEN_HOST=0.0.0.0
-# Optional advertised IPv4 override when the listener's address is behind NAT.
+# Optional advertised IPv4 or DNS hostname when the listener is behind NAT.
+# Hostnames resolve at startup; restart Piik after the public IPv4 changes.
SFU_PUBLIC_IP=
# Require server media for every room instead of P2P-first. Requires SFU_UDP_PORT.
SFU_ONLY=false
diff --git a/.github/workflows/repository-hygiene.yml b/.github/workflows/repository-hygiene.yml
index 334d673a..05a80778 100644
--- a/.github/workflows/repository-hygiene.yml
+++ b/.github/workflows/repository-hygiene.yml
@@ -280,8 +280,10 @@ jobs:
runner: macos-15
- target: darwin-amd64
runner: macos-15-intel
+ - target: android-universal
+ runner: ubuntu-24.04
runs-on: ${{ matrix.runner }}
- timeout-minutes: 20
+ timeout-minutes: ${{ matrix.target == 'android-universal' && 45 || 20 }}
env:
PIIK_BUILD_VERSION: ${{ needs.validate.outputs.version }}
steps:
@@ -315,16 +317,70 @@ jobs:
gstreamer1.0-plugins-bad
gstreamer1.0-pulseaudio
- name: Download Server release
+ if: matrix.target != 'android-universal'
uses: actions/download-artifact@v8
with:
name: piik-server-${{ github.sha }}
path: ${{ runner.temp }}/server-release
- name: Build and verify App candidate
+ if: matrix.target != 'android-universal'
run: >-
node scripts/package-app-candidate.mjs
"${{ runner.temp }}/server-release"
"${{ matrix.target }}"
"${{ runner.temp }}/app-release"
+ - name: Set up Android Java
+ if: matrix.target == 'android-universal'
+ uses: actions/setup-java@v5
+ with:
+ distribution: temurin
+ java-version: '17'
+ - name: Set up Android Gradle
+ if: matrix.target == 'android-universal'
+ uses: gradle/actions/setup-gradle@v6
+ with:
+ gradle-version: '8.11.1'
+ cache-provider: basic
+ - name: Install pinned Android build tools
+ if: matrix.target == 'android-universal'
+ run: |
+ "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" 'platforms;android-35' 'build-tools;35.0.0' 'ndk;30.0.16248370'
+ echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/30.0.16248370" >> "$GITHUB_ENV"
+ echo "GRADLE_HOME=$(dirname "$(dirname "$(readlink -f "$(command -v gradle)")")")" >> "$GITHUB_ENV"
+ go install golang.org/x/mobile/cmd/[email protected]
+ go install golang.org/x/mobile/cmd/[email protected]
+ - name: Prepare Android signing
+ if: matrix.target == 'android-universal'
+ env:
+ PIIK_PUBLISH: ${{ needs.validate.outputs.publish }}
+ PIIK_ANDROID_KEYSTORE_BASE64: ${{ secrets.PIIK_ANDROID_KEYSTORE_BASE64 }}
+ PIIK_ANDROID_STORE_PASSWORD: ${{ secrets.PIIK_ANDROID_STORE_PASSWORD }}
+ PIIK_ANDROID_KEY_ALIAS: ${{ secrets.PIIK_ANDROID_KEY_ALIAS }}
+ PIIK_ANDROID_KEY_PASSWORD: ${{ secrets.PIIK_ANDROID_KEY_PASSWORD }}
+ run: |
+ if [ "$PIIK_PUBLISH" = true ]; then
+ for name in PIIK_ANDROID_KEYSTORE_BASE64 PIIK_ANDROID_STORE_PASSWORD PIIK_ANDROID_KEY_ALIAS PIIK_ANDROID_KEY_PASSWORD; do
+ if [ -z "${!name}" ]; then echo "Android publication requires $name" >&2; exit 1; fi
+ done
+ umask 077
+ printf '%s' "$PIIK_ANDROID_KEYSTORE_BASE64" | base64 --decode > "$RUNNER_TEMP/piik-android.keystore"
+ else
+ # Manual candidates exercise the same release variant with a disposable key.
+ keytool -genkeypair -keystore "$RUNNER_TEMP/piik-android.keystore" -storetype PKCS12 \
+ -storepass candidate-password -keypass candidate-password -alias piik-candidate \
+ -keyalg RSA -keysize 3072 -validity 2 -dname 'CN=Piik CI candidate' -noprompt
+ fi
+ - name: Build and verify Android release APK
+ if: matrix.target == 'android-universal'
+ env:
+ PIIK_ANDROID_KEYSTORE: ${{ runner.temp }}/piik-android.keystore
+ PIIK_ANDROID_STORE_PASSWORD: ${{ needs.validate.outputs.publish == 'true' && secrets.PIIK_ANDROID_STORE_PASSWORD || 'candidate-password' }}
+ PIIK_ANDROID_KEY_ALIAS: ${{ needs.validate.outputs.publish == 'true' && secrets.PIIK_ANDROID_KEY_ALIAS || 'piik-candidate' }}
+ PIIK_ANDROID_KEY_PASSWORD: ${{ needs.validate.outputs.publish == 'true' && secrets.PIIK_ANDROID_KEY_PASSWORD || 'candidate-password' }}
+ run: node scripts/build-android.mjs --release --output "$RUNNER_TEMP/app-release"
+ - name: Remove temporary Android signing material
+ if: always() && matrix.target == 'android-universal'
+ run: rm -f -- "$RUNNER_TEMP/piik-android.keystore"
- name: Upload App release candidate
uses: actions/upload-artifact@v7
with:
diff --git a/AGENTS.md b/AGENTS.md
index b3d0aa74..9243cf54 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -26,6 +26,11 @@ and [engineering](./docs/standards/engineering.md).
- This file owns project-wide constraints; linked standards own domain rules,
CONTRIBUTING owns delivery, and skills supply task methods. A skill, reference
example or audit suggestion cannot override those owners or authorize extra work.
+- Do not expand scope for speculative races or security edge cases. Before
+ adding such extra mechanisms, explain the concrete failure, evidence for its
+ likelihood, and added code and maintenance cost; obtain user agreement. Do not
+ invent probabilities. Reachable defects and required authority, lifecycle and
+ resource correctness within authorized work need no extra approval.
- Model recovery from the smallest underlying authority and connection state.
Restart, crash, timeout, and network loss do not justify parallel states or
case-specific fallbacks when their required action is the same.
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 4d3d13cd..b9e15cce 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -49,7 +49,10 @@ do not maintain a separate thanks list or manufacture empty credit commits.
This is the default for an ordinary scoped implementation or bug fix.
1. Start a new phase from clean, current canonical `main`; continue scoped work in that phase's existing worktree. Preserve unrelated user work.
-2. Inspect the affected code, current owner, and reachable evidence. Research primary sources only when the issue is non-trivial or component behavior is uncertain.
+2. Inspect the affected code, current owner, and reachable evidence. Use the
+ repository's existing tools, commands and workflow; introduce another only
+ when the requested result needs it. Research primary sources when the issue
+ is non-trivial or component behavior is uncertain.
3. Make the smallest coherent change under the repo-tracked [`ponytail` skill](./.agents/skills/ponytail/SKILL.md).
4. Run the focused tests or checks that exercise the changed behavior, inspect the pending diff, stage requested files, and report the result and evidence.
@@ -95,6 +98,11 @@ require a wire bump.
Clean up worktrees and branches only after semantic review and integration are complete and the normal merged-head, open-reference, clean-tree, and non-following link checks pass. Never merge an old branch wholesale after a newer truth checkpoint.
+For owner-managed manual deletion, collect permitted retired artifacts in
+`Desktop/Piik-cleanup` with their original paths. Keep worktrees under Git's cleanup
+workflow. Targets of a policy-rejected cleanup stay in place and go in that path
+index; do not substitute a move for the rejected deletion.
+
## Research Standard
The [engineering reference](./docs/standards/engineering.md) owns module and
diff --git a/README.md b/README.md
index 2e3babd2..9f0fbace 100644
--- a/README.md
+++ b/README.md
@@ -59,7 +59,7 @@ you can also create a room in the online version.
| Start here | What you need to do |
| --- | --- |
| [Use Piik online](https://demo.piik.tv) | Share from a desktop browser, or choose Camera on a phone with browser camera support. |
-| [Download Piik App](https://piik.tv/#download) | Extract and open the App, choose **Public invite**, then **Open Piik** to create a temporary room. |
+| [Download Piik App](https://piik.tv/#download) | Extract the desktop package or install the mobile app, open Piik, choose **Public invite**, then **Open Piik**. |
<p align="center"><img src="./docs/assets/quickstart.svg" width="640" alt="Choose a screen, send an invitation, watch together."></p>
@@ -90,7 +90,7 @@ Deploy Piik Server to run a site for your group.
<p><a href="./docs/operations/self-hosting.md#docker-compose"><img src="https://img.shields.io/badge/Deploy_with-Docker_Compose-2764c4?style=for-the-badge&amp;logo=docker&amp;logoColor=white" alt="Deploy Piik with Docker Compose"></a></p>
-Piik Server is a standalone binary with the web interface built in.
+Piik Server runs on Windows x64 and Linux x64/ARM64, with the web interface built in.
Download the Server package for Linux x64 (`piik-<revision>-runtime.tar.gz`)
or ARM64 (`piik-<revision>-linux-arm64-runtime.tar.gz`),
extract it, and run:
diff --git a/README.zh-CN.md b/README.zh-CN.md
index 06ee9bb8..b8f93247 100644
--- a/README.zh-CN.md
+++ b/README.zh-CN.md
@@ -55,7 +55,7 @@ Piik 是一个免费开源的私密屏幕共享工具,适合游戏直播、一
| 从这里开始 | 需要做什么 |
| --- | --- |
| [使用在线版](https://demo.piik.tv) | 用电脑浏览器分享画面;手机浏览器支持摄像头访问时,也可选择摄像头分享。 |
-| [下载 Piik App](https://piik.tv/?lang=zh-CN#download) | 解压并启动 Piik App,选择**公网邀请**,点击**进入 Piik**,创建临时房间。 |
+| [下载 Piik App](https://piik.tv/?lang=zh-CN#download) | 电脑上解压程序包,手机或平板安装 App。打开 Piik,选择**公网邀请**,点击**进入 Piik**。 |
<p align="center"><img src="./docs/assets/quickstart.svg" width="640" alt="选择画面,发送邀请,一起观看。"></p>
@@ -82,7 +82,7 @@ Piik App 可创建本地房间或临时公网邀请,也能在后台为已授
<p><a href="./docs/operations/self-hosting.zh-CN.md#使用-docker-compose"><img src="https://img.shields.io/badge/Deploy_with-Docker_Compose-2764c4?style=for-the-badge&amp;logo=docker&amp;logoColor=white" alt="使用 Docker Compose 部署 Piik"></a></p>
-Piik Server 是一个内置网页界面的独立程序。
+Piik Server 是内置网页界面的独立程序,支持 Windows x64 和 Linux x64/ARM64。
根据服务器架构下载 Linux x64 的 `piik-<revision>-runtime.tar.gz` 或 ARM64 的
`piik-<revision>-linux-arm64-runtime.tar.gz`,解压后运行:
diff --git a/cmd/piik-app/README.md b/cmd/piik-app/README.md
index 2b8c9e31..e345c0dd 100644
--- a/cmd/piik-app/README.md
+++ b/cmd/piik-app/README.md
@@ -7,6 +7,10 @@ interface in your system Browser.
## Run A Package
+For Android APK installation and the shared browser workflow, follow
+[Getting started](../../docs/guide/getting-started.md#share-with-piik-app).
+The package, terminal and command-line instructions below apply to desktop App.
+
Extract the matching platform bundle in full. On Windows and Linux, keep
`runtime` beside the App executable and run `piik-app.exe` or `./piik-app`.
On macOS, open `Piik App.app`; you can move the whole `.app` to Applications.
diff --git a/cmd/piik-app/README.zh-CN.md b/cmd/piik-app/README.zh-CN.md
index 2d858758..855f618b 100644
--- a/cmd/piik-app/README.zh-CN.md
+++ b/cmd/piik-app/README.zh-CN.md
@@ -6,6 +6,9 @@ Piik App 通过系统浏览器中的 Piik 网页界面,提供本地房间与
## 运行程序包
+安卓 APK 安装与共用的浏览器操作流程见[上手指南](../../docs/guide/getting-started.zh-CN.md#用-piik-app-分享)。
+下方程序包、终端与命令行说明适用于桌面 App。
+
完整解压对应平台的程序包。Windows 和 Linux 保留 App 程序旁的 `runtime` 目录,
分别运行 `piik-app.exe` 或 `./piik-app`。
macOS 打开 `Piik App.app`,也可以将整个 `.app` 移到「应用程序」。
diff --git a/cmd/piik-server/main.go b/cmd/piik-server/main.go
index 0ec44e0e..02df506b 100644
--- a/cmd/piik-server/main.go
+++ b/cmd/piik-server/main.go
@@ -158,10 +158,14 @@ func serve(debug bool) (returnedErr error) {
stop()
<-releaseChecked
}()
- <-ctx.Done()
+ var serveErr error
+ select {
+ case <-ctx.Done():
+ case serveErr = <-server.Done():
+ }
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
- return server.Close(shutdownCtx)
+ return errors.Join(serveErr, server.Close(shutdownCtx))
}
func serverDebugEnabled(value string) bool {
diff --git a/docs/assets/guide/audio-paths.en.svg b/docs/assets/guide/audio-paths.en.svg
index 525473d2..1b160145 100644
--- a/docs/assets/guide/audio-paths.en.svg
+++ b/docs/assets/guide/audio-paths.en.svg
@@ -4,10 +4,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -23,16 +26,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -43,8 +56,11 @@
@media (prefers-color-scheme: dark) { :root {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -56,10 +72,11 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
} }
text { fill: var(--ink); font: 600 26px system-ui, -apple-system, 'Segoe UI', 'Microsoft YaHei', sans-serif; text-anchor: middle; }
diff --git a/docs/assets/guide/audio-paths.zh-CN.svg b/docs/assets/guide/audio-paths.zh-CN.svg
index 0e52dfd8..e76083d7 100644
--- a/docs/assets/guide/audio-paths.zh-CN.svg
+++ b/docs/assets/guide/audio-paths.zh-CN.svg
@@ -4,10 +4,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -23,16 +26,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -43,8 +56,11 @@
@media (prefers-color-scheme: dark) { :root {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -56,10 +72,11 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
} }
text { fill: var(--ink); font: 600 26px system-ui, -apple-system, 'Segoe UI', 'Microsoft YaHei', sans-serif; text-anchor: middle; }
diff --git a/docs/assets/guide/network-paths.en.svg b/docs/assets/guide/network-paths.en.svg
index 1b479ead..e706e643 100644
--- a/docs/assets/guide/network-paths.en.svg
+++ b/docs/assets/guide/network-paths.en.svg
@@ -4,10 +4,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -23,16 +26,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -43,8 +56,11 @@
@media (prefers-color-scheme: dark) { :root {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -56,10 +72,11 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
} }
text { fill: var(--ink); font: 600 26px system-ui, -apple-system, 'Segoe UI', 'Microsoft YaHei', sans-serif; text-anchor: middle; }
diff --git a/docs/assets/guide/network-paths.zh-CN.svg b/docs/assets/guide/network-paths.zh-CN.svg
index db42ada7..a41d1096 100644
--- a/docs/assets/guide/network-paths.zh-CN.svg
+++ b/docs/assets/guide/network-paths.zh-CN.svg
@@ -4,10 +4,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -23,16 +26,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -43,8 +56,11 @@
@media (prefers-color-scheme: dark) { :root {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -56,10 +72,11 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
} }
text { fill: var(--ink); font: 600 26px system-ui, -apple-system, 'Segoe UI', 'Microsoft YaHei', sans-serif; text-anchor: middle; }
diff --git a/docs/assets/guide/virtual-lan.en.svg b/docs/assets/guide/virtual-lan.en.svg
index 33526bd1..8a63035c 100644
--- a/docs/assets/guide/virtual-lan.en.svg
+++ b/docs/assets/guide/virtual-lan.en.svg
@@ -4,10 +4,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -23,16 +26,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -43,8 +56,11 @@
@media (prefers-color-scheme: dark) { :root {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -56,10 +72,11 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
} }
text { fill: var(--ink); font: 600 26px system-ui, -apple-system, 'Segoe UI', 'Microsoft YaHei', sans-serif; text-anchor: middle; }
diff --git a/docs/assets/guide/virtual-lan.zh-CN.svg b/docs/assets/guide/virtual-lan.zh-CN.svg
index c9aa7410..aa61df95 100644
--- a/docs/assets/guide/virtual-lan.zh-CN.svg
+++ b/docs/assets/guide/virtual-lan.zh-CN.svg
@@ -4,10 +4,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -23,16 +26,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -43,8 +56,11 @@
@media (prefers-color-scheme: dark) { :root {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -56,10 +72,11 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
} }
text { fill: var(--ink); font: 600 26px system-ui, -apple-system, 'Segoe UI', 'Microsoft YaHei', sans-serif; text-anchor: middle; }
diff --git a/docs/assets/quickstart.svg b/docs/assets/quickstart.svg
index 49c98436..79fb60e9 100644
--- a/docs/assets/quickstart.svg
+++ b/docs/assets/quickstart.svg
@@ -8,10 +8,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -27,16 +30,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -47,8 +60,11 @@
@media (prefers-color-scheme: dark) { :root {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -60,10 +76,11 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
} }
</style>
diff --git a/docs/guide/getting-started.md b/docs/guide/getting-started.md
index a16cd048..318789aa 100644
--- a/docs/guide/getting-started.md
+++ b/docs/guide/getting-started.md
@@ -41,12 +41,12 @@ restrictive networks may load the page but block video.
## Share with Piik App
-1. Download a **`piik-app`** archive for your platform from
+1. Download **Piik App** for your platform from
[GitHub Releases](https://github.com/TNTcraftHIM/Piik/releases) or the
- [Gitee mirror](https://gitee.com/TNTcraftHIM/Piik/releases). Extract the whole
- archive and keep its directory structure intact.
+ [Gitee mirror](https://gitee.com/TNTcraftHIM/Piik/releases). On phones and tablets,
+ install the app. On a computer, extract the whole archive and keep its directory structure intact.
2. Open `piik-app.exe` on Windows, `Piik App.app` on macOS, or `./piik-app` on Linux.
- The launcher opens in your system browser.
+ On phones and tablets, open Piik after installation. The launcher opens in your system browser.
3. Choose **Public invite** for friends outside your local network, then select
**Open Piik**. Other modes are explained below.
4. Choose **Start sharing**, then select **Browser**, **Camera**, **Apps / Windows**, or
@@ -68,6 +68,7 @@ If macOS blocks the first launch, follow the [Mac opening steps](./troubleshooti
| `darwin-amd64` | Intel Mac; native capture requires macOS 13+ |
| `linux-amd64` | Linux x64 |
| `linux-arm64` | Linux ARM64 |
+| `android-universal` | Android 10+; ARM64 and x64 in one APK |
Linux native capture also needs [system components](../../native/capture/linux/README.md).
See the [App guide](../../cmd/piik-app/README.md) for package and runtime details.
diff --git a/docs/guide/getting-started.zh-CN.md b/docs/guide/getting-started.zh-CN.md
index 13993539..5731abbb 100644
--- a/docs/guide/getting-started.zh-CN.md
+++ b/docs/guide/getting-started.zh-CN.md
@@ -40,9 +40,9 @@ Piik 支持一位房主分享屏幕,最多 20 位受邀观众观看。先选
1. 从 [GitHub Releases](https://github.com/TNTcraftHIM/Piik/releases) 或
[Gitee 镜像](https://gitee.com/TNTcraftHIM/Piik/releases)下载以 **`piik-app`** 开头、
- 对应平台的压缩包,完整解压并保留目录结构。
+ 对应平台的程序包。手机或平板安装 App,电脑上完整解压并保留目录结构。
2. Windows 打开 `piik-app.exe`,macOS 打开 `Piik App.app`,Linux 运行 `./piik-app`。
- 启动页会在系统浏览器里打开。
+ 手机或平板安装后打开 Piik。启动页会在系统浏览器里打开。
3. 邀请不在同一局域网的朋友时,选择 **公网邀请**,再点击 **进入 Piik**。
其他模式的用途见下方说明。
4. 点击 **开始分享**,按可用选项选择 **浏览器**、**摄像头**、**程序 / 窗口** 或 **屏幕**,
@@ -64,6 +64,7 @@ macOS 首次打开被拦截时,按 [Mac 打开说明](./troubleshooting.zh-CN.
| `darwin-amd64` | Intel Mac;原生采集需 macOS 13 及以上 |
| `linux-amd64` | Linux x64 |
| `linux-arm64` | Linux ARM64 |
+| `android-universal` | Android 10 及以上;一个 APK 包含 ARM64 和 x64 |
Linux 原生采集还需要[系统组件](../../native/capture/linux/README.md)。
程序包与启动参数的细节见 [App 指南](../../cmd/piik-app/README.zh-CN.md)。
diff --git a/docs/guide/remote-control.md b/docs/guide/remote-control.md
index 1f5344bf..5931c651 100644
--- a/docs/guide/remote-control.md
+++ b/docs/guide/remote-control.md
@@ -104,10 +104,26 @@ choice needs **Allow Viewers** before it applies again. Passwords are not saved.
## Mouse, touch and permissions
-On a phone, swipe to move the pointer, tap to click and hold to drag. Two-finger
-tap is right-click; two-finger swipe scrolls. Pinch to zoom, then use two fingers
-to move around the enlarged picture. **Remote keyboard** provides keys and
-shortcuts. On a computer, **Game mouse** captures continuous mouse movement;
+On a phone, touch the picture directly to click and hold to drag, without a
+cursor. Two-finger tap is right-click; two-finger swipe scrolls. Pinch to zoom in
+or out, then use two fingers to pan, including into the blank space around the
+picture. **Playback settings → Default touch mode → Trackpad** keeps the simpler cursor
+mode available.
+
+Drag the mini mouse to reposition it; tap to expand its left and right buttons
+and combined scroll wheel / middle button. The cursor stays above and to the
+left of the mouse and moves with it. The picture pans at the edges to leave room
+for the tool, and the cursor stops at the source picture's edges. With the mouse
+expanded, use two fingers on the picture to pan or zoom while the mouse stays
+in reach. Use its wheel to scroll the Host. Drag the lower shell to move; hold a
+mouse button while moving to drag an item. Tap the wheel to middle-click, swipe
+it to scroll, or hold then
+move for a middle-button drag. The separate minimize button returns it to the mini
+mouse and your selected touch mode. Leave the mini mouse at an edge to tuck it away
+after the same idle delay as the player controls; tap the visible part to expand
+it again. **Remote keyboard** provides keys,
+shortcuts and text entry.
+On a computer, **Game mouse** captures continuous mouse movement;
press **Esc** to release it.
**Room Viewers** lets authenticated room members take available control without
diff --git a/docs/guide/remote-control.zh-CN.md b/docs/guide/remote-control.zh-CN.md
index 5f1876c9..9d2329ef 100644
--- a/docs/guide/remote-control.zh-CN.md
+++ b/docs/guide/remote-control.zh-CN.md
@@ -80,8 +80,14 @@ Piik 不指定游戏中的玩家编号,请使用游戏自己的加入和分配
## 鼠标、触屏与权限
-手机上滑动可移动鼠标,轻点点击、长按拖动;双指轻点右击、滑动滚动。
-捏合可以放大画面,放大后用双指移动查看位置。**远程键盘** 提供按键和组合键。
+手机上默认直接触摸画面操作,不显示光标;轻点点击、长按拖动,双指轻点右击、滑动滚动。
+捏合可放大或缩小画面,缩放后双指移动用于平移画面,周围允许留黑。在 **播控设置 → 默认触摸方式** 中,也可选择 **光标触控板**。
+
+拖动迷你鼠标可调整工具的位置,轻点即可展开左右键和滚轮。光标位于鼠标左上方,拖动下半部或单指滑动画面时一起移动;到达边缘时,画面会平移留出鼠标的操作空间。
+光标停在所分享画面的边缘,留黑只补足工具所需的空间。鼠标展开时,双指缩放、平移会调整本地画面,鼠标留在手边;要滚动远端页面,请滑动鼠标滚轮。
+按住鼠标键再移动即可拖拽物体。滚轮轻点为中击、滑动为滚动,长按后移动可进行中键拖动。
+独立的缩小按钮可切回迷你鼠标和选定的触摸方式。迷你鼠标贴边闲置后会按播控相同的时长自动收纳,点击露出的部分即可再次展开。
+**远程键盘** 提供按键、组合键和文字输入。
电脑上可开启 **游戏鼠标** 连续转动视角,按 **Esc** 释放鼠标。
**房间观众** 允许已加入房间的观众直接取得可用控制权;**控制密码** 要求输入单独设置的密码。
diff --git a/docs/guide/sharing.md b/docs/guide/sharing.md
index c6535501..02d8e339 100644
--- a/docs/guide/sharing.md
+++ b/docs/guide/sharing.md
@@ -111,5 +111,6 @@ Leave the connection setting on **Auto** for ordinary use. To compare routes,
select one of the alternatives offered by the Host and site.
On supporting sites, **Playback settings → Connection → Help relay to more
-viewers** lets a Viewer volunteer more upload for the current visit. It remains
-an ordinary P2P relay; enable it on a stable connection with upload to spare.
+viewers** lets a Viewer volunteer more upload for the current visit. It starts
+with one extra Viewer; adjust **Maximum relay viewers** to set your limit.
+It remains an ordinary P2P relay; enable it on a stable connection with upload to spare.
diff --git a/docs/guide/sharing.zh-CN.md b/docs/guide/sharing.zh-CN.md
index 781facb7..f673f593 100644
--- a/docs/guide/sharing.zh-CN.md
+++ b/docs/guide/sharing.zh-CN.md
@@ -94,4 +94,5 @@
通常保持 **自动**。需要对照线路时,选择房主与站点提供的其他选项。
支持的站点还提供 **播放设置 → 连接 → 帮助更多观众转发**,
-允许观众为本次访问提供更多上行带宽。它仍是普通 P2P 转发;请在连接稳定、上行有余量时开启。
+允许观众为本次访问提供更多上行带宽。开启后默认多转发一人,可调整 **最多转发人数**。
+它仍是普通 P2P 转发;请在连接稳定、上行有余量时开启。
diff --git a/docs/guide/troubleshooting.md b/docs/guide/troubleshooting.md
index b674a690..69c3aba6 100644
--- a/docs/guide/troubleshooting.md
+++ b/docs/guide/troubleshooting.md
@@ -30,7 +30,7 @@ symptom's simple checks have failed. Change one thing at a time.
## Opening App and joining a room
-[Startup](#app-startup-fails-or-no-page-opens) · [macOS](#macos-blocks-first-launch) ·
+[Startup](#app-startup-fails-or-no-page-opens) · [Android installation](#android-apk-will-not-install) · [macOS](#macos-blocks-first-launch) ·
[Public invite / 1033](#public-invitation-fails-or-shows-1033) · [Local invitation](#local-invitation-will-not-open) ·
[Local certificate](#local-https-certificate-warning) · [Room code](#room-code-does-not-work) · [Room creation 403](#room-creation-returns-403)
@@ -43,6 +43,18 @@ symptom's simple checks have failed. Change one thing at a time.
3. If startup still fails, retry with **Debug launch** enabled and save the complete
error and an [App report](#reporting-a-problem).
+### Android APK will not install
+
+Download the APK from the [App installation steps](./getting-started.md#share-with-piik-app).
+If Android asks, allow this browser or file manager to install the downloaded APK.
+
+If installation reports a conflicting package or signature and you previously
+installed a test build, uninstall that test App before installing the official
+APK. The test build and official release use different signing keys. Uninstalling
+clears App settings, including approved sites and the starred startup site;
+browser settings are stored separately. Later official updates can be installed
+over the existing official App.
+
### macOS blocks first launch
Current Mac builds are unsigned and unnotarized. To open one, verify the
@@ -457,8 +469,10 @@ window audio. Piik's microphone mute does not mute that separate source.
URL scheme does not enable HTTPS; `localhost` means the current device.
2. For a microphone, review this site's browser permission and selected input.
3. For remote control, update the Host's App and the site, then refresh the Viewer
- page. The Host must use App window/screen capture and enable control; Browser
- and camera capture cannot receive remote input. Follow the
+ page. The Host must use desktop App window/screen capture and enable control;
+ Browser and camera capture cannot receive remote input. Android can share its
+ screen and control a desktop as a Viewer, but cannot receive remote control;
+ its Host page therefore has no enable-control action. Follow the
[authorization steps](./room-tools.md#let-a-friend-control-your-shared-screen).
If your browser cannot accept the local certificate, use a reachable **Public
@@ -511,7 +525,7 @@ Enable diagnostics before reproducing once, then export before refreshing:
- **Web:** use the page's diagnostics button. A sharing failure's error icon below
the TV also provides a copyable summary; it contains the Web version, message
and code, without invitation links or source names.
-- **App capture:** also enable **Debug launch** on the App's mode selection page,
+- **Desktop App capture or remote control:** also enable **Debug launch** on the Host App's mode selection page,
then press `D` in the terminal to export its report.
- **Connection failure:** collect the affected Viewer and Host reports for the
same attempt. For self-hosted SFU, include the Server report too.
diff --git a/docs/guide/troubleshooting.zh-CN.md b/docs/guide/troubleshooting.zh-CN.md
index 4fb76b8f..ef75ac15 100644
--- a/docs/guide/troubleshooting.zh-CN.md
+++ b/docs/guide/troubleshooting.zh-CN.md
@@ -29,7 +29,7 @@ outline: 2
## 打开 App 与加入房间
-[启动失败](#app-启动失败或没有打开网页) · [macOS 打不开](#macos-提示已损坏或无法验证开发者) ·
+[启动失败](#app-启动失败或没有打开网页) · [安卓安装](#安卓-apk-无法安装) · [macOS 打不开](#macos-提示已损坏或无法验证开发者) ·
[公网邀请 / 1033](#公网邀请创建失败或出现-1033) · [本地邀请](#本地邀请打不开) ·
[本地证书](#本地-https-证书提示) · [房间号](#房间号无法使用) · [创建房间 403](#创建房间返回-403)
@@ -39,6 +39,15 @@ outline: 2
2. 本地模式列出多个地址时,选择朋友能访问的网卡和 IP,见[本地邀请排查](#本地邀请打不开)。
3. 仍然失败时,开启 **诊断启动** 后重试,保留完整错误和 [App 报告](#反馈问题)。
+### 安卓 APK 无法安装
+
+按 [App 安装步骤](./getting-started.zh-CN.md#用-piik-app-分享)下载 APK。
+安卓提示时,允许本次使用的浏览器或文件管理器安装下载的 APK。
+
+如果安装提示软件包冲突或签名不一致,且之前安装过测试版,请先卸载测试版,再安装正式 APK。
+测试版与正式版使用不同的签名密钥。卸载会清除 App 设置,包括已授权站点和收藏的启动站点;
+浏览器设置独立保存。之后的正式版更新可以覆盖安装。
+
### macOS 提示已损坏或无法验证开发者
当前 Mac 版本未进行 Developer ID 签名和 Apple 公证。首次打开时,先校验下载文件,再为这个 App 放行:
@@ -376,8 +385,9 @@ Piik 的麦克风静音不会关闭这条独立来源。
普通 HTTP 内网页面可以观看、聊天和加入语音收听,但不能使用需要安全上下文的麦克风、摄像头采集和远控接口。
仅修改链接里的协议名不会开启 HTTPS;`localhost` 只代表当前设备。
2. 麦克风不可用时,检查当前站点的浏览器权限和所选输入设备。
-3. 远控需更新房主 App 和站点,再刷新观众页面。房主必须使用 App 窗口/屏幕采集并开启远控;
- 浏览器与摄像头采集不能接收远程输入。随后按[授权步骤](./room-tools.zh-CN.md#让朋友操作共享的电脑)操作。
+3. 远控需更新房主 App 和站点,再刷新观众页面。房主必须使用桌面 App 窗口/屏幕采集并开启远控;
+ 浏览器与摄像头采集不能接收远程输入。安卓可以分享屏幕,也可以作为观众控制电脑,但不能作为被控手机,
+ 因此安卓房主页面没有开启远控的入口。随后按[授权步骤](./room-tools.zh-CN.md#让朋友操作共享的电脑)操作。
浏览器不接受本地证书例外时,改用可访问的 **公网邀请** 或具有可信证书的 HTTPS 站点。
@@ -423,7 +433,7 @@ Piik 的麦克风静音不会关闭这条独立来源。
- **网页:** 使用页面的诊断按钮。分享失败时,电视下方的错误图标还可复制失败信息,
包含网页版本、提示和错误编号,不包含邀请链接或来源名称。
-- **App 采集:** 还需在 App 模式选择页开启 **诊断启动**,随后在终端按 `D` 导出 App 报告。
+- **桌面 App 采集或远控:** 还需在房主 App 模式选择页开启 **诊断启动**,随后在终端按 `D` 导出 App 报告。
- **连接失败:** 收集受影响观众和房主同一次尝试的报告;自部署 SFU 还需服务端报告。
报告保存在本机,分享前请检查私人信息;[诊断与导出](../../cmd/piik-app/README.zh-CN.md#诊断)说明报告内容。
diff --git a/docs/operations/github.md b/docs/operations/github.md
index e4ee66a0..f49ffb5c 100644
--- a/docs/operations/github.md
+++ b/docs/operations/github.md
@@ -129,7 +129,7 @@ package set. While the publishing run's artifacts are available, download all pa
into one directory (replace `RUN_ID`, `FULL_SOURCE_SHA` and `VERSION`):
```sh
-for artifact in piik-server piik-server-linux-arm64 piik-server-windows-amd64 piik-app-windows-amd64 piik-app-linux-amd64 piik-app-linux-arm64 piik-app-darwin-arm64 piik-app-darwin-amd64; do
+for artifact in piik-server piik-server-linux-arm64 piik-server-windows-amd64 piik-app-windows-amd64 piik-app-linux-amd64 piik-app-linux-arm64 piik-app-darwin-arm64 piik-app-darwin-amd64 piik-app-android-universal; do
gh run download RUN_ID --repo TNTcraftHIM/Piik \
--name "$artifact-FULL_SOURCE_SHA" --dir /tmp/piik-mirror
done
diff --git a/docs/operations/self-hosting.md b/docs/operations/self-hosting.md
index 7306fb4f..4379dec8 100644
--- a/docs/operations/self-hosting.md
+++ b/docs/operations/self-hosting.md
@@ -188,8 +188,10 @@ configuration uses P2P media, so participants need a usable UDP path.
Add `SFU_UDP_PORT=7882` to `.env`, allow UDP 7882, and restart Piik to enable
automatic SFU fallback. When the server is behind NAT, also set `SFU_PUBLIC_IP`
-to its reachable public IPv4 address and forward the same external and internal
-UDP port as `SFU_UDP_PORT`. The same binary provides the fallback.
+to its reachable public IPv4 address or DDNS hostname and forward the same external
+and internal UDP port as `SFU_UDP_PORT`. Use a hostname pointing directly to that
+IPv4, with the CDN proxy disabled. Piik resolves it at startup; after the public
+IP changes and DDNS updates, restart Piik. The same binary provides the fallback.
Allow server forwarding capacity and outbound bandwidth for Viewers actually
using the SFU.
The Host must turn off **Privacy mode** before sharing to allow this route.
diff --git a/docs/operations/self-hosting.zh-CN.md b/docs/operations/self-hosting.zh-CN.md
index 29f83b3b..aa839316 100644
--- a/docs/operations/self-hosting.zh-CN.md
+++ b/docs/operations/self-hosting.zh-CN.md
@@ -167,7 +167,8 @@ TCP 8787 仅供本机反向代理访问。STUN 域名需要直接解析到服务
## 可选:启用媒体兜底
在 `.env` 中添加 `SFU_UDP_PORT=7882`,放行 UDP 7882,再重启 Piik,即可启用自动 SFU 兜底。
-如果服务器处于 NAT 后方,还需将 `SFU_PUBLIC_IP` 设置为外部可达的公网 IPv4 地址。
+如果服务器处于 NAT 后方,还需将 `SFU_PUBLIC_IP` 设置为外部可达的公网 IPv4 地址或 DDNS 域名。
+域名应直接指向该 IPv4,关闭 CDN 代理。Piik 在启动时解析域名;公网 IP 变化、DDNS 更新后需重启 Piik。
公网和内部映射的 UDP 端口都应与 `SFU_UDP_PORT` 一致。
这项功能由同一个服务端程序提供。
服务器需要为实际经由 SFU 观看的观众承担转发负载和出口带宽。
diff --git a/docs/operations/website.md b/docs/operations/website.md
index 4dd50f02..35fd7ea8 100644
--- a/docs/operations/website.md
+++ b/docs/operations/website.md
@@ -232,8 +232,9 @@ document links. Screenshot refresh and visual review are manual steps.
## Downloads
-The Windows, macOS and Linux App cards use fixed filenames and each provider's
-native latest-attachment route:
+The desktop row contains Windows, macOS and Linux; the mobile row contains
+Android and the iOS online entry. App downloads use fixed ZIP/APK filenames and
+each provider's native latest-attachment route:
- GitHub: `releases/latest/download/asset-name.zip`
([documentation](https://docs.github.com/en/repositories/releasing-projects-on-github/linking-to-releases)).
@@ -242,10 +243,11 @@ native latest-attachment route:
Both links follow provider publication without a website rebuild. Supported App
architecture choices stay visible; one selection updates both providers' links.
-An optional GitHub metadata request supplies the latest version/date and its
+Android uses one universal APK for ARM64 and x64. The iOS entry opens the online
+version without promising an App release date. An optional GitHub metadata request supplies the latest version/date and its
authored editorial title and one-line explanation;
failure does not disable architecture selection. Without JavaScript, the default
-links remain Windows x64, Apple silicon and Linux x64. Publish a newly listed App
+links remain Windows x64, Apple silicon, Linux x64 and Android universal. Publish a newly listed App
package before deploying its website option. ARM64 Server packages alone do not
enable an ARM64 App choice.
Platform buttons say “Download from GitHub” /
diff --git a/docs/project-memory.md b/docs/project-memory.md
index 316a6554..881f230c 100644
--- a/docs/project-memory.md
+++ b/docs/project-memory.md
@@ -1,6 +1,6 @@
# Project Memory
-Last updated: 2026-10-24
+Last updated: 2026-10-26
Piik is private, low-latency screen sharing for one Host and up to 20
authenticated friends. The current product surface is Web Host, Web Viewer, and
@@ -8,9 +8,9 @@ Browser relay; desktop and mobile Browsers are Viewer targets, and camera-capabl
mobile Browsers can also host. It is not a
public broadcast service. Hosted and App run one shared Go core
([ADR-0012](./adr/0012-shared-go-backend-core.md)); Node and Vite build the
-Browser UI and are not a runtime. Source includes a cross-platform App that
-runs that same core in a self-contained Local deployment or supplies native
-capability to explicitly approved Sites through the system Browser. Its single Go
+Browser UI and are not a runtime. Desktop and Android Apps run
+that same core in a self-contained Local deployment or supply native
+capability to explicitly approved Sites through the system Browser and shared Web UI. Each App's Go
process starts the loopback capability service before room selection. The
persistent launcher offers Local and temporary public-link choices and manages
per-Site local approval and one starred startup Site. Site use can remain on
@@ -20,7 +20,7 @@ Browser and Native peers without changing participant, signaling, capacity, or
route identity. Local is LAN-first; explicit one-link mode exposes that same
authority through a temporary public control tunnel while media remains P2P.
Host authority belongs to a tab, with one separate persistent origin resume
-hint. The App admits two independent native control sessions under its one
+hint. The desktop App admits two independent native control sessions under its one
loopback service.
The Hosted Go process embeds Binding-only STUN and optional SFU forwarding.
diff --git a/docs/research/agent-context-governance.md b/docs/research/agent-context-governance.md
index 4b973750..174332c2 100644
--- a/docs/research/agent-context-governance.md
+++ b/docs/research/agent-context-governance.md
@@ -67,7 +67,7 @@ Sources:
### Instructions And Skills
-Reviewed: 2026-10-17. These are source-derived design considerations, not a
+Reviewed: 2026-10-24. These are source-derived design considerations, not a
benchmark of agent reliability or another set of mandatory instructions.
OpenAI's current guidance favors task-scoped references, short skill descriptions
@@ -93,8 +93,22 @@ remains in the skill's [provenance](../../.agents/skills/ponytail/UPSTREAM.md).
New mechanisms still need relevant end-to-end evidence. Required design thinking
does not imply a separate design document, review agent or approval for each edit.
+OpenCode's instructions make several of these decisions concrete: validate at
+the owning boundary, extract helpers for real concepts or reuse, test production
+code, and use existing generation and delivery commands. Piik consolidates these
+in its existing rule owners rather than adding another rule catalog. Its Bun,
+Effect, import and loop preferences remain specific to that repository.
+
+The public X discussion, read through linked mirrors, particularly recommends
+the speculative-complexity rule and its companion about single-use helpers.
+Replies also distinguish retry correctness from speculative hardening and question
+unsupported probability estimates. Piik therefore asks for agreement on extra
+speculative scope, while authorized, reachable defects remain ordinary fixes.
+
Sources:
+- [OpenCode AGENTS.md, reviewed revision](https://github.com/anomalyco/opencode/blob/84578e123ffd31e31a174457e7350961da4fabbc/AGENTS.md).
+- [Dave Kiss's X discussion](https://x.com/davekiss/status/2113363042247512161), [public mirror](https://xitter.cf/davekiss/status/2113363042247512161); [helper-rule reply](https://xitter.cf/gol_tiro/status/2113433555749744872), [retry-correctness reply](https://xitter.cf/sebuzdugan/status/2113569699703517539) and [probability-estimate reply](https://xitter.cf/feng25591/status/2113689613659410615).
- [OpenAI: Rethinking skills and prompts](https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra)
- [OpenAI: Using skills for OSS maintenance](https://developers.openai.com/blog/skills-agents-sdk)
- [Codex instruction discovery](https://learn.chatgpt.com/docs/agent-configuration/agents-md) and [skill loading](https://learn.chatgpt.com/docs/build-skills)
diff --git a/docs/research/cross-platform-client-runtime.md b/docs/research/cross-platform-client-runtime.md
index 4acf03df..60a57e74 100644
--- a/docs/research/cross-platform-client-runtime.md
+++ b/docs/research/cross-platform-client-runtime.md
@@ -328,6 +328,161 @@ link and inline major-upgrade guidance. The accepted behavior belongs to
clients still follow their released behavior; a notice cannot substitute for
actual App/Site compatibility checks.
+## Android Capture Bridge Assessment
+
+The [Android App](../../mobile/README.md) builds an APK for arm64 and
+x86-64 from the repository's single Go dependency graph. The system browser
+presents the shared Web UI at its loopback origin or an explicitly approved
+site. `AppRuntimeService` owns the one Go runtime and pending OS requests;
+the Activity owns visible permission dialogs. A user-started `specialUse`
+foreground service keeps local rooms and the native companion available while
+the system browser is in front, with ordinary open/stop notification actions.
+The shared launcher's add-site action displays the exact origin for explicit
+approval before opening it. Its management popup preserves the opener, so the
+existing approval notification and close return to the original live Host.
+The App management page owns approval, revocation and the starred startup site.
+Local mode starts the Go room and LAN HTTPS invitation only when
+selected; Hosted mode uses that site's room service. Room signaling stays with
+the same Web UI. Public-link mode reuses the desktop supervised cloudflared
+process and the same room server. Its Android PIE executable is installed in
+the APK native-library directory; temporary configuration uses the Android
+cache directory rather than a process-wide temporary-path override. Runtime
+shutdown joins startup work, ends the room server, then joins the sidecar.
+A room-server or sidecar failure retires that local room and returns its launcher
+to retry. The resident capability service and approved external Site sessions
+continue independently; failure of the launcher or capability service ends the runtime.
+
+A `mediaProjection` foreground service owns the approved Projection, VirtualDisplay,
+MediaCodec and AudioRecord. A narrow
+[gomobile AAR](https://pkg.go.dev/golang.org/x/mobile/cmd/gomobile) bridge supplies
+frames and controls through `nativehost.CaptureProvider`; desktop capture keeps
+its supervised process adapter. Both use the same Host, packetization and route
+owners. Android admits one native control session because one Projection owns
+the shared display; desktop admission remains two.
+
+Capture prefers hardware AVC and otherwise uses an available Surface-capable
+MediaCodec VP8 encoder. Device capabilities bound the existing quality controls.
+The APK uses the ordinary responsive UI for phones and tablets; TV availability
+depends on an installed browser, Projection and encoder support.
+
+[MediaProjection](https://developer.android.com/media/grow/media-projection)
+requires consent for each capture session; Android 14 permits one VirtualDisplay
+per consent token. System `onStop` retires the capture resources. Source audio
+uses [playback capture](https://developer.android.com/media/platform/av-capture)
+from Android 10, subject to `RECORD_AUDIO` permission, the same user profile and the
+source application's capture policy.
+
+Native commentary uses AudioRecord VOICE_COMMUNICATION with the same per-share stream/ready
+handshake and existing Go audio mixer. Its input and replacement stay independent
+of playback capture; the Projection service holds microphone eligibility until
+the last commentary reader joins. Mono PCM is adapted to the existing PCM48
+stereo contract. Device preferences use AudioManager input identities and verify
+the actual recording route; the empty choice follows Android's default. This is
+separate from room voice's Browser microphone and does not add a mixer or route.
+Android's [audio-input sharing policy](https://developer.android.com/media/platform/sharing-audio-input)
+limits concurrent microphone input across separate apps. System echo processing
+does not make the two microphone paths share one capture or eliminate that limit.
+
+The earlier prototype API 35 emulator run delivered a live 480-by-800 VP8 picture
+to an independent Windows Browser Viewer. Sharing continued after switching to another
+App. A separate allowed-capture AudioTrack source reached that Viewer as Opus
+with measurable tone energy. The lab Browser used explicit local candidates
+because mDNS does not cross the emulator's isolated network. Physical-device
+acceptance and distribution remain in [TODO](../todo.md#next).
+
+The system browser owns camera, room voice, page permissions and fullscreen.
+Native playback audio requests recording permission separately; pure-video
+Projection does not. The Activity and service retain native capture ownership
+across rotation; runtime detachment precedes asynchronous shutdown so an old
+close cannot retire a newly opened runtime.
+
+Emulator checks cover Local/public startup, screen and browser-camera
+replacement, concurrent room voice, portrait-to-landscape resizing,
+cancellation/retry, background return and new-Viewer admission. The packaged
+public-link helper established its tunnel without extra DNS configuration.
+[Verification status](../verification-status.md#device-evidence-boundary) owns
+the current rate-control and physical-device acceptance boundaries.
+
+### Android Encoder Budget
+
+A 2026-10-26 standalone API 35 emulator probe isolated the software
+`OMX.google.vp8.encoder` from Piik's room and transport. A changing 1080p30
+Surface produced about 2.65 Mbps at either a 1 Mbps or 100 kbps CBR request.
+Extending the keyframe interval reduced keyframes, but not that output rate.
+The [AOSP software VPX implementation](https://android.googlesource.com/platform/frameworks/av/+/ffede69924118c5454484164fd590b3474cc716a/media/libstagefright/codecs/on2/enc/SoftVPXEncoder.cpp)
+disables frame dropping and CBR resizing; reaching its compression limit can
+therefore exceed the budget even when bitrate updates succeed.
+
+The same probe reused the unmodified
+[WebRTC FrameDropper](https://webrtc.googlesource.com/src/+/6f37672d358475cd17544121a12494da454d85fb/modules/video_coding/utility/frame_dropper.cc)
+from the existing native SDK pin, with the
+[VideoStreamEncoder call sequence](https://webrtc.googlesource.com/src/+/6f37672d358475cd17544121a12494da454d85fb/video/video_stream_encoder.cc).
+The 960x540 lower output at 100 kbps averaged 99.1 kbps over the final 20 seconds;
+1080p at 1 Mbps reached about 1.03 Mbps over the final three seconds. Restoring
+3 Mbps restored full frame submission. At 1080p and 100 kbps, output remained
+about 175 kbps: the stock component bounds frame suppression rather than
+promising an arbitrary hard byte ceiling. These are synthetic platform/component
+comparisons, not a full-App capture or network result. Android reuses that same
+component at each encoder's Surface input, with one capture clock and existing
+demand-driven lower outputs.
+
+A full-App API 35 emulator trial of candidate `e933633f` on the same date
+exercised Projection, the shared Go media route and Browser decoding together.
+Android reported concurrent 1920x1080 and 960x540 software encoders. A changing
+source delivered about 4.8–5.6 Mbps at a 5 Mbps live profile; lowering that
+profile to 100 kbps retained the same Viewer connection and produced about
+140–244 kbps in sampled two-second windows, at 1–2 fps. Restoring 5 Mbps
+restored about 5.0–5.6 Mbps. These RTP payload observations retain the stock
+FrameDropper saturation boundary; they are not a strict byte-ceiling guarantee
+or an independent network-shaping measurement.
+
+With a stationary captured source, a fresh Browser subscription decoded the
+960x540 rendition and new keyframes. Viewer departure retired the lower
+encoder while the original preview continued decoding. Pause/resume, stopping
+and consenting to a fresh capture also passed: each final stop removed the
+Projection, capture service and active codec resources, while the independent
+launcher runtime remained available. These are bounded emulator observations;
+physical-device codec capacity and audio remain separate acceptance targets.
+
+### Android Browser Handoff
+
+The Android App uses the system browser with the same Go room, authorization and media code.
+[Chrome Intents](https://developer.chrome.com/docs/android/intents) allow an
+explicit gesture to wake App, then return to the current browser tab. Waking is
+not authorization: the exact site still needs approval and an authorized-health
+recheck. The browser's existing approval popup retains its real opener.
+[Verified App Links](https://developer.android.com/training/app-links/verify-applinks)
+require domain association, so they cannot be the universal entry for arbitrary
+self-hosted sites.
+
+Each new recording uses a user-visible return to App for system consent;
+[background activity restrictions](https://developer.android.com/guide/components/activities/secure-bal)
+prevent the Go callback from simply opening that UI behind the browser.
+Outside recording, the runtime service keeps Local/public rooms alive rather
+than relying on a background Activity's static runtime. Android can suspend or
+kill [cached processes](https://developer.android.com/guide/components/activities/process-lifecycle).
+The external browser owns camera, voice and local-network permission. The
+optional native activation description adds a foreground action without changing native
+commands or the published control version. A normal App open also processes a
+current pending request, independently of whether its site has the shortcut.
+
+### iOS External-Browser Assessment
+
+An explicit native session can use iOS 26's `BGContinuedProcessingTask` before
+opening the local Web UI in Safari. [Apple DTS](https://developer.apple.com/forums/thread/840384)
+describes user-controlled sessions without a fixed duration ceiling; admission,
+progress, cancellation and retirement remain part of that session's lifecycle.
+[Synctrain](https://github.com/pixelspark/sushitrain/blob/main/Sushitrain/BackgroundManager.swift)
+provides an existing Go/gomobile implementation to reference.
+
+The next implementation gate is one background-task owner bound to Piik's Go
+runtime, with explicit Start/Stop and shutdown on expiration. Exercise local
+HTTP/WebSocket, idle room, voice, native capture and return to Safari on a device.
+Hosted HTTPS access to insecure loopback remains a separate
+[WebKit boundary](https://bugs.webkit.org/show_bug.cgi?id=171934).
+`SFSafariViewController` is a system-managed in-App browser alternative; it is
+distinct from the requested standalone-browser workflow.
+
## Current Follow-up
[TODO](../todo.md) owns remaining work; [verification status](../verification-status.md)
diff --git a/docs/research/nat-traversal.md b/docs/research/nat-traversal.md
index 66fb56a9..4795576f 100644
--- a/docs/research/nat-traversal.md
+++ b/docs/research/nat-traversal.md
@@ -1119,6 +1119,23 @@ and the owner confirmed both page and picture worked. No proxy, candidate
substitution or Browser privacy change was used. This closes the direct
App-to-Browser trial without claiming universal campus-network compatibility.
+### Startup Resource Transfer
+
+The later title-only loading report concerns Web startup, before ICE or STUN.
+The entry modules and bootstrap API use the same origin. Static compression
+reduced the main script from about 674 KB to 194 KB and a 64 KiB/s controlled
+page load from 11.0 to 3.2 seconds.
+
+A 2026-10-26 isolated TCP control used the installed virtual adapter's 1470-byte
+MTU at both endpoints, with a hidden 1280-byte link and no ICMP feedback. The
+712-byte HTML arrived, but the compressed 194 KB script received zero bytes in
+18 seconds with repeated retransmissions. Changing only the isolated SYN MSS
+to 1240 restored the complete script in about 8 ms. This reproduces
+[TCP path-MTU black holes](https://www.rfc-editor.org/rfc/rfc2923), including why
+compression or an endpoint MTU below 1500 is insufficient. The field report
+still needs its actual resource transfer and route evidence to distinguish this
+case from tunnel interruption, filtering or Browser execution failure.
+
## Primary Sources
- [RFC 8445: Interactive Connectivity Establishment](https://datatracker.ietf.org/doc/html/rfc8445)
diff --git a/docs/research/native-client-media.md b/docs/research/native-client-media.md
index 339cc271..a0c77f91 100644
--- a/docs/research/native-client-media.md
+++ b/docs/research/native-client-media.md
@@ -151,6 +151,10 @@ The 2026-10-20 pipe reproduction establishes a second startup defect: waiting
for audio readiness before reading video can fill the video pipe and block a
capture service that audio initialization also needs. The session now waits for
video readiness while draining video, then initializes optional source audio.
+The 2026-10-26 gated-helper check found that the `starting` progress status
+still admitted audio before platform video initialization finished. Source audio
+now waits for the existing `active` status. Mixed-audio pages can receive their
+stable output first; source changes and shutdown cancel the pending input.
The full-pipe/silent/no-ready controls and native Host lifecycle tests pass
without longer deadlines. This also removes the overlapping initial source
enumeration described in an [Apple Developer Forums report](https://developer.apple.com/forums/thread/772365)
diff --git a/docs/research/network-lab.md b/docs/research/network-lab.md
index ffb012af..129b6acc 100644
--- a/docs/research/network-lab.md
+++ b/docs/research/network-lab.md
@@ -140,14 +140,15 @@ game quality. Frame cadence, bitrate, loss, repair, freezes and QP remain separa
measurements. These longer cases cap captures at 500,000 packets, retaining only
each packet's first 96 bytes; ordinary cases keep their 100,000-packet bound.
-The [full process acceptance](https://github.com/TNTcraftHIM/Piik/actions/runs/38505151190)
-on 2026-10-14 passed all 37 scenarios and six negative controls on candidate
-`a5e89805`, including resource cleanup and unchanged host networking. Thirty-one
-scenarios produced advancing decoded audio/video. Blocked UDP, the two
-random-mapping profiles and the three Browser-containing sequential-mapping
-cases produced bounded unavailability; there were no observation timeouts.
-A passing failure observation is not a
-successful connection or an Internet success-rate measurement.
+The [full process acceptance](https://github.com/TNTcraftHIM/Piik/actions/runs/40218617697)
+on 2026-10-25 passed all 83 trials across 43 scenarios on candidate `8db79dd0`,
+with paired enhancement on/off trials where applicable and six negative controls.
+Seventy trials produced progressing media; thirteen exhausted their deliberately
+restricted routes with bounded unavailability. There were no observation timeouts.
+All owned resources retired and host networking remained unchanged. The matrix
+includes both voice and picture, STUN fallback, every Browser/App pairing,
+address changes, SFU fallback and constrained/recovered quality. It measures the
+laboratory profiles, not an Internet connection-success rate.
The [paired comparison](https://github.com/TNTcraftHIM/Piik/actions/runs/38301455274)
also passed eight trials across two seeds with alternating enhancement on/off
order. All four ordinary App/App trials delivered media; all four random-mapping
diff --git a/docs/research/room-interactions.md b/docs/research/room-interactions.md
index 5597644e..8007a310 100644
--- a/docs/research/room-interactions.md
+++ b/docs/research/room-interactions.md
@@ -440,7 +440,7 @@ independent, with room administration separate from publication ownership.
The existing one-Host workflow would remain the default.
This is a model change: the current route controller owns one Host and one
-publication. Each new publication would need its own source generation and
+picture publication. Each new publication would need its own source generation and
subscription lifecycle, while endpoint upload limits remain shared across all
publications. Creating a controller per source must not multiply that endpoint's
outbound allowance. Switching pictures should replace the selected subscription,
@@ -448,6 +448,40 @@ preserving room membership and chat. One selected picture is the initial scope;
camera conferencing and room voice are separate features. No implementation or
protocol change is accepted by this assessment.
+### Multi-room And Distributed Hosting Assessment
+
+Reviewed 2026-10-24. The proposed 3.0 direction separates room administration,
+service hosting and media publication. Chat, voice and screen sharing would have
+independent lifetimes; image/file exchange is a separate finite transfer concern.
+
+Distributed hosting has different implementation costs:
+
+| Model | Responsibility and tradeoff |
+| --- | --- |
+| Different instances host different rooms | Each room keeps one authority and route controller; clients join rooms across instances. An App can host a temporary room, while a self-hosted service can keep rooms available. |
+| Cross-instance identity and room admission | Users reach a room's owning service through their own provider. This adds inter-service authentication and admission without requiring replicated room authority. |
+| One room replicated across instances | Membership, permissions and history need conflict resolution, revocation and partition behavior. This is a substantially larger distributed-state design. |
+
+[XMPP multi-user chat](https://xmpp.org/extensions/xep-0045.html) provides a reference
+for service-owned rooms; [Matrix](https://spec.matrix.org/latest/#architecture)
+replicates room events across participating homeservers. These are alternatives,
+not a requirement to adopt either protocol. Direct joins across separately hosted
+rooms do not by themselves constitute federation.
+
+P2P-first media is independent of that hosting choice. Multiple publishers need
+explicit publication/subscription identities and an aggregate endpoint bandwidth
+budget. Reuse of traversal, recovery and forwarding does not make the current
+single-picture route planner a finished multi-source implementation.
+[Selective subscription](https://docs.livekit.io/transport/media/subscribe/#selective-subscription)
+separates available publications from the streams actually delivered. Multiple
+publishers with one selected primary picture is a smaller first UI than a live
+grid; the data model should represent subscriptions rather than hard-code one
+room-wide current publisher.
+
+The proposed starting point is one authority per room, many rooms per instance,
+and existing Go/media components. Automatic room migration, replicated authority
+and a language rewrite are separate decisions, not prerequisites.
+
## Attended Remote-Control Prototype
Reviewed 2026-10-19. Remote control is included in the accepted release scope.
@@ -822,13 +856,20 @@ grant, rather than desktop pointer coordinates.
For window keyboard/mouse control, the captured region must map to the current
native window geometry, including movement, resizing and DPI. The input owner
reads the current geometry and process identity. It suspends input and releases
-held state when the target loses focus, and revokes control if the source
-disappears. Both pages display that same suspension; the Host is asked to focus
-the shared window to resume. This reduces
+held state when the target loses focus or its current geometry cannot be read,
+and revokes control when the source identity is confirmed invalid. Both pages
+display the suspension; restoring the shared window resumes the existing grant. This reduces
accidental input after switching away; it is not an OS window sandbox.
`SendInput` has no target-window parameter, and shortcuts can change focus.
Window selection must never imply that remote input is confined to that window.
+Input execution and authorization have separate lifetimes. A failed system input
+call drops that operation, while the existing grant accepts subsequent input.
+Failed releases remain with the input owner until cleanup succeeds; actions and
+partially inserted text are never replayed. This follows the failure scope used by
+[Sunshine's Windows input sink](https://github.com/LizardByte/Sunshine/blob/v2025.923.33222/src/platform/windows/input.cpp#L435),
+where an unsuccessful system call does not revoke the user's control session.
+
The Windows sink uses physical-key mapping through
[scan codes](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput),
and supports relative mouse movement through browser
diff --git a/docs/standards/configuration.md b/docs/standards/configuration.md
index 43eec207..4acf1b72 100644
--- a/docs/standards/configuration.md
+++ b/docs/standards/configuration.md
@@ -67,7 +67,7 @@ Automatic SFU fallback runs inside the Hosted process when `SFU_UDP_PORT` is set
| --- | --- |
| `SFU_UDP_PORT` | Optional UDP media port `1..65535`; unset or blank disables SFU and fixes Privacy mode on for every room. Set `7882` for the standard public listener and allow the Host to choose Privacy mode. |
| `SFU_LISTEN_HOST` | IPv4 bind address, default `0.0.0.0`; independent of HTTP `LISTEN_HOST`. Read only when SFU is enabled. |
-| `SFU_PUBLIC_IP` | Optional explicit IPv4 advertised-address override for a host behind NAT. It changes the advertised address, not the port: forward the same UDP port as `SFU_UDP_PORT`. Read only when SFU is enabled. |
+| `SFU_PUBLIC_IP` | Optional advertised IPv4 address or DNS hostname for a host behind NAT. A hostname resolves to IPv4 at startup; restart Piik after its address changes. Forward the same UDP port as `SFU_UDP_PORT`. Read only when SFU is enabled. |
| `SFU_ONLY` | `false` by default. `true` requires `SFU_UDP_PORT` and sends every room through SFU, without Peer candidates or background P2P convergence. Applies after Server restart. |
SFU control uses the application's authenticated signaling connection. No
diff --git a/docs/standards/engineering.md b/docs/standards/engineering.md
index 319c04e6..c4d455f5 100644
--- a/docs/standards/engineering.md
+++ b/docs/standards/engineering.md
@@ -24,6 +24,7 @@ checks the Browser, `scripts/tsconfig.json` checks tooling/tests, and root
| `internal/thirdparty/` | Scoped Go dependency repairs with pinned upstream provenance, licenses and tests; removal criteria live beside the source |
| `native/capture`, `native/fixtures`, `native/probes` | Platform capture implementations, synthetic workloads and standalone capability probes |
| `site/` | Independently published static introduction; its assets do not implement product behavior |
+| `mobile/` | Android platform shell and in-process capture bridge over the shared Go core |
| `scripts/` | Development checks, browser/media gates, dependency assembly and release packaging/publishing tools |
| `tests/` | TypeScript/tooling tests and cross-language fixtures; Go tests stay beside their packages |
| `deploy/` | Operator scripts and container, proxy, service and certificate templates |
@@ -33,8 +34,10 @@ checks the Browser, `scripts/tsconfig.json` checks tooling/tests, and root
| `build/`, `node_modules/`, `coverage/` | Ignored local output and dependencies; never current product truth |
`site/` introduces Piik; `src/client/` is Piik's interactive application.
-`internal/app/nativecapture` adapts the capture contract in Go; `native/capture/`
-implements it using platform APIs. `scripts/` prepares and verifies artifacts;
+`internal/app/nativecapture` owns the desktop helper process and frame contract;
+`native/capture/` implements its platform APIs. `nativehost` consumes a capture
+stream; the Android App supplies that stream in-process through `mobile/`.
+`scripts/` prepares and verifies artifacts;
`deploy/` operates them. Move files with their actual consumers, and update paths
and checks together; a smaller root alone does not justify another abstraction.
@@ -49,7 +52,8 @@ and checks together; a smaller root alone does not justify another abstraction.
| `internal/server/route` | Synchronous room graph and single-operation decisions; returns resource effects to `signal`, owns no locks or timers, and calls its optional diagnostic sink synchronously |
| `internal/server/sfu`, `internal/app/mediaedge` | Server forwarding and native peer adapters over the shared `internal/media/forwarding`; Pion/LiveKit own transport and adaptation |
| `internal/app` | Local/public-link/Site composition, loopback service, native sessions and App lifecycle; never a second room backend |
-| `internal/app/nativecapture`, `native/capture/*` | App-side capture contract and minimal platform adapters; platform-specific capture/encoding stays here |
+| `internal/app/nativecapture`, `native/capture/*` | Desktop capture process, frame contract and platform capture/encoding adapters |
+| `internal/app/nativehost/capture.go`, `mobile/bridge`, `mobile/android` | Capture producer contract, shared mobile frame bridge and Android-owned consent/codec resources; reuse room control and media routes |
| `src/client/pages`, `lib`, `media`, `native` | Browser orchestration and endpoint adapters; bind current media and retire owned resources |
| `src/client/media/viewer-presentation.ts`, `ui/media-status.ts` | Playback facts and their derived status projection; UI indicators do not create recovery or quality policy |
| `src/client/components`, `locales`, `ui` | Shared interaction, visual primitives and localized copy; same product interface for Server and App |
@@ -78,6 +82,7 @@ Keep this check outside room startup, authorization and media lifecycles.
| Browser/App discovery and control | `src/client/native/wire.ts`, `internal/app/loopback/protocol.go`, `internal/app/nativecontrol/wire.go` |
| Remote input data channel | [Browser input protocol](../../src/client/remote-control/protocol.ts), [native input validation](../../internal/app/remoteinput/manager.go) and [channel routing](../../internal/media/inputroute); admission remains with the existing room and native control owners |
| App/capture sidecar | `internal/app/nativecapture` and each platform capture adapter; probe/commands and encoded-frame envelope are distinct formats |
+| In-process capture producer | `internal/app/nativehost/capture.go`; the platform supplies frames and retires its own resources, without another control or routing protocol |
| Configuration and persistence | `internal/server/config`, `internal/app/config`, `internal/server/room/database.go`; operator semantics in [configuration](./configuration.md) |
| Release and update metadata | Existing packaging scripts, `src/client/lib/release-update.ts`, `cmd/piik-server/release.go`; identity/compatibility in [versioning](./versioning.md), operations in [deployment](../deployment.md) |
@@ -124,9 +129,9 @@ justify a generic plugin framework or accepting unknown commands.
## Runtime Lifecycles
Read each runtime as acquisition, the authoritative commit that makes its work
-visible, and the retirement that ends it. Revalidate the acquiring identity
-after every await or callback; presentation derives from the committed fact
-instead of mirroring it.
+visible, and the retirement that ends it. Before committing asynchronous results
+or retiring current state or resources, revalidate the acquiring identity.
+Presentation derives from the committed fact instead of mirroring it.
| Runtime | Acquire | Commit | Retire |
| --- | --- | --- | --- |
@@ -138,6 +143,7 @@ instead of mirroring it.
| Room voice | `room-voice/session.ts` binds the admitted room session and its Browser endpoint | authoritative membership and the current publication/connection identity under [room media](./routing-transport.md#room-media) | leave, room retirement or session close releases owned audio; closing the interaction window does not leave voice |
| Floating room tool | `FloatingPanel.tsx` presents the tool; `ui/panel-window.ts` opens a supported detached window | the current window request and document own presentation, not the room session | close returns the tool to the page; opener/session retirement closes even a late open under [presentation lifecycle](./presentation-lifecycle.md) |
| App native session | the loopback service admits native control sessions and the Web UI selects the native path | session identity plus the explicit media-path choice | session end, share end or process exit retires that edge and its capture sidecar |
+| Android runtime | the user starts `AppRuntimeService`, which creates the shared Go core | one service/runtime instance; pending OS results retain their original media request | user Stop or service retirement joins startup and closes its own runtime; Activity navigation does not stop it |
| Native ICE gathering | one gathering observes addresses on its connection's retained socket | candidates retain their source ICE generation; one collector joins Pion, STUN and gateway completion | ICE restart retires previous observations and excludes stale queued output; connection close retires the socket |
| Server room authority | `signal` authenticates one Host or Viewer command | `room` writes durably before the in-memory commit; the signaling lock serializes mutations | explicit replacement or deletion, grant rotation or revocation, or process exit for memory-mode rooms |
| Server route and SFU | `signal` feeds `route` one serial child operation; `sfu/admission.go` accepts one bounded publication | one committed graph per controller and the forwarding publication | superseded assignments retire inside the same operation; deactivate retires the publication |
@@ -159,6 +165,7 @@ in [introduction design](./public-introduction.md) and the [website runbook](../
| SFU endpoints | [src/client/sfu/publisher.ts](../../src/client/sfu/publisher.ts), [src/client/sfu/subscriber.ts](../../src/client/sfu/subscriber.ts), [src/client/sfu/peer.ts](../../src/client/sfu/peer.ts), [internal/server/sfu](../../internal/server/sfu) |
| Localized copy and failure facts | [src/client/locales](../../src/client/locales), [src/client/ui/copy.ts](../../src/client/ui/copy.ts), [src/client/ui/media-failure.ts](../../src/client/ui/media-failure.ts) |
| Native control connection and media adapters | [src/client/native/client-connection.ts](../../src/client/native/client-connection.ts), [src/client/native](../../src/client/native), [internal/app/loopback](../../internal/app/loopback), [internal/app/nativecontrol](../../internal/app/nativecontrol), [internal/app/nativehost](../../internal/app/nativehost), [internal/app/nativeviewer](../../internal/app/nativeviewer), [internal/app/nativecapture](../../internal/app/nativecapture) |
+| Android App and platform lifecycle | [mobile/README.md](../../mobile/README.md), [mobile/bridge](../../mobile/bridge), [mobile/android/app/src/main](../../mobile/android/app/src/main) |
| Room authority, persistence and graph | [internal/server/signal](../../internal/server/signal), [internal/server/room](../../internal/server/room), [internal/server/route](../../internal/server/route) |
| Room signaling intent and interaction delivery | [src/client/lib/signaling.ts](../../src/client/lib/signaling.ts), [src/client/lib/room-interactions.ts](../../src/client/lib/room-interactions.ts); admission and transient delivery follow [rooms and access](./rooms-access.md) |
| Shared wire contract | [src/shared/protocol.ts](../../src/shared/protocol.ts), [internal/server/protocol](../../internal/server/protocol), [tests/fixtures/wire-samples.json](../../tests/fixtures/wire-samples.json) |
@@ -181,9 +188,13 @@ in [introduction design](./public-introduction.md) and the [website runbook](../
resource acquisition, replacement and retirement together. File size alone
does not justify wrappers, a generic manager or a framework.
Remove superseded paths in the same change; retain no unused legacy copy.
- Keep test-only state seeding and setup in test harnesses.
- Do not export mutable resources solely for tests; preserve the production
- construction path when verifying defaults or restoration.
+ A platform adapter can be justified with one implementation; judge its boundary
+ and consumers before removing it.
+- Keep simple logic at its call site. Extract a helper for actual reuse, a
+ substantial boundary, or a distinct concept whose name makes the caller clearer.
+ In longer orchestration, keep the main flow readable and move substantial
+ validation and supporting details into nearby helpers. Prefer meaningful
+ boundaries over many single-use wrappers or arbitrary function-size limits.
- Review dependency direction for source/type imports as well as runtime
imports. Shared types belong to a common owner. A type-only cycle is an
ownership concern; it does not by itself establish a runtime or performance
@@ -229,8 +240,12 @@ in [introduction design](./public-introduction.md) and the [website runbook](../
Injected callbacks still execute in their caller's context. Synchronous
authority hooks must not hide blocking I/O or release the caller's lock;
schedule slow work at an explicit, owned effect boundary.
-- Validate input at the boundary, keep domain error meanings stable, and map
- them into shared localized UI. [Versioning](./versioning.md) determines which
+- Validate and normalize unknown input once at each owning trust boundary, then
+ pass typed values inward. Do not repeat shape checks already guaranteed by a
+ schema, constructor or internal contract. Authorization and mutable state or
+ resource identity remain checked where they can change.
+ Keep domain error meanings stable and map them into shared localized UI.
+ [Versioning](./versioning.md) determines which
extensions are compatible; an optional field is not automatically compatible.
Dispatch separates protocol rejection, per-request operation failure and an
already-satisfied teardown. Late teardown is idempotent and cannot retire a
@@ -253,12 +268,16 @@ in [introduction design](./public-introduction.md) and the [website runbook](../
- Browser/App/Server share [visual language](./visual-language.md) and
[status projection](./media-status.md). A new page, locale or deployment
does not invent a new palette, status model or user-role metaphor.
-- Keep package comments about current responsibilities and invariants. Historical
+- Comment on non-obvious reasons, constraints and surprising behavior, rather
+ than narrating assignments or control flow. Package comments describe current
+ responsibilities and invariants. Historical
migration paths belong in ADR/research unless they explain a surviving constraint.
Support Windows, macOS and Linux; new text files use LF and new filenames use
ASCII, with format exceptions in [`.gitattributes`](../../.gitattributes).
Scripts use stable executable paths; physical confidence stays explicit in
[verification status](../verification-status.md).
+- Update generated files through their source schema, template or generator and
+ the existing regeneration command; do not patch generated output by hand.
- Retained tools declare their direct package dependencies. Documented TypeScript
tools join the normal type-check entry point; a transitive dependency or a
one-off manual check is not an owned toolchain contract.
@@ -308,6 +327,13 @@ domain distinctions.
## Ablation And Review
+Test production implementations through their real construction paths. Replace
+external I/O or platform boundaries only where the focused test needs it; keep
+the behavior under test real instead of copying its algorithm or validator into
+the test. Keep test-only setup and state seeding in the harness, and do not export
+mutable resources solely for tests. Defaults and restoration use the production
+construction path.
+
Judge a change by verified user value against implementation, maintenance,
compatibility, runtime and failure cost. After a material module, remove any
new state, dependency, branch or abstraction that the accepted behavior does
diff --git a/docs/standards/media-quality.md b/docs/standards/media-quality.md
index 426e6ff7..3eb623d6 100644
--- a/docs/standards/media-quality.md
+++ b/docs/standards/media-quality.md
@@ -290,6 +290,13 @@ separate facts. Congestion must not turn a temporary reduced output into a new
requested ceiling. Recovery demand must survive lower-layer selection and reach
the existing encoder or dormant-output owner without recreating the connection.
+Native outputs share one source presentation timeline across encoder groups,
+dormancy and capture replacement. Capture and refresh timestamps express elapsed
+source time; encoder completion and pipe arrival do not establish another clock.
+Internal RTP copies reuse the original ingress's accepted remote-to-local clock
+mapping. Only that ingress estimates network delay; outgoing sender reports
+project the source timeline onto the media core's local clock at emission.
+
Each Browser direct, relay, or SFU video sender owns a separate track. The original
track remains a source and local presentation track; it is never attached directly
to an outbound sender. Display capture with raw-frame stream support forwards a
@@ -376,6 +383,10 @@ directly and does not re-encode that preview through a Browser publisher.
Native parents and the SFU share the media forwarding adapter. Each Native
parent considers only its direct children and its admitted SFU publication.
+A Native source with one rendition shares that encoding across its consumers.
+Its encoder budget follows the largest active remote allocation within the source
+ceiling. Local preview alone can use the ceiling, but cannot mask remote demand.
+One shared rendition cannot provide different encoded qualities to its children.
Recovery-probe traffic is exploration, not a reduction of committed media
capacity. Allocation and encoder consumers retain the same budget through probe
settling. A locally rate-controlled output has unmet demand within its current
@@ -417,6 +428,12 @@ VP8 uses the library encoder. Windows tunes the stock QP history and sampling
for brief interruptions and low-frame-rate recovery; codec thresholds and the
adaptation controller remain WebRTC-owned. Other platform producers retain their
current fixed-output implementation pending equivalent physical acceptance.
+Android shares one Projection and captured texture across demand-driven
+MediaCodec outputs. Each output uses the pinned WebRTC FrameDropper before
+encoder submission, with encoded-byte feedback and the current allocation;
+dropping an output frame never stops the shared capture clock. Unsupported
+optional encoders leave the original usable. Its fixed spatial outputs and
+platform codec still bound extremely low-bandwidth delivery.
Each source groups compatible effective direct-child requests and keeps
incompatible weak budgets independent. Per-group rates change in place; joining
another group's output waits for accepted recovery and keeps the existing
diff --git a/docs/standards/naming.md b/docs/standards/naming.md
index b4cebc4c..6c8b3f5a 100644
--- a/docs/standards/naming.md
+++ b/docs/standards/naming.md
@@ -23,6 +23,11 @@ The browser picker names a capture method, not a browser-tab-only source. Its
short explanation includes screens, application windows and tabs; available
choices remain owned by the browser and operating system.
+Public architecture labels use `x64` and `ARM64`. Keep package and ABI identifiers
+such as `amd64`, `x86_64` and `arm64-v8a` unchanged in filenames and technical
+instructions. Present package format (APK/ZIP) and system requirements separately
+from architecture; a universal APK contains multiple supported architectures.
+
Write each language for its own audience. Chinese headings and welcomes may
sound like friends inviting each other: “叫朋友来一起看。” or “沙发给你留着呢。”
Do not translate English sentence structure or possessive screen metaphors
diff --git a/docs/standards/presentation-lifecycle.md b/docs/standards/presentation-lifecycle.md
index 85d22645..eda7e310 100644
--- a/docs/standards/presentation-lifecycle.md
+++ b/docs/standards/presentation-lifecycle.md
@@ -58,26 +58,34 @@ Closing the window returns it to the page; room retirement or opener teardown
closes it, including a late asynchronous open. Browser window support does not
promise game-overlay transparency, click-through or exclusive-fullscreen coverage.
-Piik App opens this same application in the system Browser. Its startup surface
+Desktop Piik App opens this same application in the system Browser. Its startup surface
offers Local and temporary public invitation; the running App is already
-available to its approved Sites before either mode starts. The desktop App entry
-is omitted on known mobile platforms; Browser and camera actions retain their
-own capability checks. A Site's explicit App action discovers the local approval
+available to its approved Sites before either mode starts. Browser pages share
+one App entry across platforms; discovery and authorized health determine its
+connection state. Android adds an explicit wake action. Browser and camera
+actions retain their own capability checks.
+A Site's explicit App action discovers the local approval
page, where the user confirms its exact origin. The launcher lists approvals and
revokes each independently. Starring one approved Site makes an ordinary App launch open it;
without a star, the launcher opens. This preference grants no access and starts
no room. Revoking that Site clears its star; other approvals leave it intact. Explicit CLI entry keeps
precedence. The ready App button and terminal launcher address return to its
-management page in every mode. Local/public room startup is one selection per
-App run; reopening the launcher does not stop or replace that room. Changing
-between those modes requires restarting App.
+management page in every mode. A pending or healthy Local/public room keeps its
+startup selection; reopening the launcher does not stop or replace that room.
+After a failed attempt has fully retired, the launcher can retry or select the
+other mode in the same App run. Changing a healthy room's mode requires restarting App.
+An App-launched tab selects that process's control endpoint and retains the
+selection through reloads in its Browser session. If that endpoint becomes
+unavailable, discovery stays with it; a new explicit launch selects another App.
+Endpoint selection does not grant Site access or capture permission.
Activating a page supplies
native capability without restarting its room or healthy media session.
-The source selector exposes that same connection action before sharing; a
-successful connection refreshes its current list. An already approved exact
+The shared App entry stays in the header while selecting a source; a successful
+connection refreshes the open selector's list. An already approved exact
origin needs no repeat confirmation, but the page still verifies authorized App
-health. Closing the selector cancels its pending connection action. Existing
-Browser shares retain their capture implementation until sharing ends.
+health. Closing the selector does not cancel the separate site approval. Existing
+Browser shares stay unchanged when App connects; explicit source replacement
+uses the current share's source transaction.
App-created Local/public-invite Host pages open at their automatically approved
loopback origin. The shared App button reflects verified authorized health,
including activation through another entry and return to an activated page;
@@ -87,7 +95,14 @@ Viewer links opened at that activated origin may use the same running App
without changing the Viewer UI. Starting a share offers the
Browser picker and any exact App-owned native windows. It never chooses a
window automatically. A reproduced Browser-window, capture, or background
-failure is required before introducing an embedded Web runtime.
+failure is required before introducing an embedded Web runtime on desktop.
+The Android App uses that same system-browser workflow. Recording consent and
+foreground services remain platform-owned. Its startup page and site management
+reuse the shared launcher. Waking App or navigating to a site
+never grants native permission. A pending system permission stays with its
+current native request while the user enters App and returns to the browser.
+App returns after the requested capture preparation completes, including source
+audio; video readiness alone does not complete that operation.
Opening the system Browser is an asynchronous convenience action. One pending
OS handoff is allowed; a slow or failed handler cannot block readiness or App
@@ -104,9 +119,12 @@ versions of the product UI.
A remembered App activation permits discovery when choosing a source or
receiving media; it does not make App a prerequisite for sharing. Idle Host
pages hold no App control session. The shared source selector orders Browser,
-Camera, Window and Screen, defaulting to Window after App detection. Without an
-App it shows only Browser and Camera, defaulting to Browser. Discovery never
-blocks Browser selection or overrides an explicitly chosen tab. Refresh remains
+Camera, Window and Screen, defaulting to Window after App detection. An App that
+provides only a system picker gets one combined choice in place of Window and
+Screen. Browser and Camera choices require their respective Browser APIs;
+temporary restrictions keep a supported choice disabled. Without an App,
+the selector defaults to the first supported Browser or Camera choice. Discovery
+never blocks Browser selection or overrides an explicitly chosen tab. Refresh remains
reachable after failed App discovery; pending attempts are shared, completed failures
do not suppress later discovery. Cancelling the selector or ending a share
releases its unused control session; an obsolete operation cannot install or
@@ -123,9 +141,10 @@ share ownership and required audio tracks come from the accepted App response.
Browser initialization failure remains distinct from capture permission failure.
A reachable Piik App with an incompatible control protocol is distinct from an
-absent App. Discovery continues looking for a compatible process before reporting
-the mismatch; the source selector retains Browser operation and offers update/
-refresh guidance. Discovery metadata may grow, while known identity and capability
+absent App. Without an explicitly selected endpoint, discovery continues looking
+for a compatible process before reporting the mismatch. The source selector
+retains Browser operation and offers update/refresh guidance. Discovery metadata
+may grow, while known identity and capability
types remain validated. Missing optional capabilities are unavailable; control
commands and responses retain their strict protocol contract.
An empty source list means enumeration succeeded for the selected category.
@@ -200,12 +219,12 @@ input, viewing options, and window actions; on-screen chat belongs with viewing
options. Keep PiP, theater and fullscreen together at the trailing edge.
Frequent viewing actions stay directly
reachable; on-screen chat appearance and connection preferences stay in playback
-settings, while chat settings own records. Picture fit has one direct cycling control.
+settings, while chat settings own records. Picture fit uses one cycling control.
Narrow players keep volume, PiP, theater and fullscreen directly available.
-Keep one capsule at the lower edge. Shorten the volume slider and hide its
-percentage before wrapping: narrow players put playback/sound and remote input
-on the first row, with viewing and window actions on the second. Both rows retain
-one visibility owner and 44px control height; none becomes a distant toolbar.
+Keep one translucent capsule at the lower edge. Narrow players shorten the
+volume slider, hide its percentage and move picture fit/rotation into the
+attached settings, reusing those controls. Playback, sound, remote input and
+window actions stay on one row under the same visibility owner.
Playing video hides the bar
after two idle seconds, following [Media Chrome's default](https://github.com/muxinc/media-chrome/blob/main/docs/src/pages/docs/en/components/media-controller.md#autohide).
Pause or unavailable playback keeps it visible. Control hover, keyboard focus,
@@ -264,9 +283,11 @@ the picture controls and starts muted. Local pause, fit, rotation,
theater and fullscreen affect only that preview; the separately labelled sharing
controls own authoritative pause and stop. Its speaker control adjusts the shared
output for Viewers, as defined by [source audio](./media-quality.md#screen-audio);
-it does not enable local monitoring. A hidden or unfocused Host page still
-suspends local preview, except while its video is in Picture-in-Picture. Returning
-to the page preserves an explicit local pause.
+it does not enable local monitoring. By default, a hidden or unfocused Host page
+suspends local preview, except while its video is in Picture-in-Picture. The Host
+can disable this in playback settings to keep watching on another monitor;
+the choice is remembered at the same Browser origin. Returning to the page or
+changing this preference preserves an explicit local pause.
Autoplay rejection exposes the play action. A connection, track object,
or `playing` event alone is not proof that the current route is visible. The
@@ -384,8 +405,9 @@ ordinary connection details do not collect or expose that report.
Current scope includes Web Host, Web Viewer, Browser relay, and optional Native
Host/Viewer media beneath those same pages. Desktop and mobile Browsers are
-Viewer targets; mobile screen capture, reliable background relay, App-owned SFU,
-and physical non-Windows native acceptance remain later platform work.
+Viewer targets. Android screen capture is implemented in the system-browser App
+candidate; public delivery awaits its platform acceptance. Reliable background
+relay, App-owned SFU and physical non-Windows native acceptance remain later work.
## Primary References
diff --git a/docs/standards/rooms-access.md b/docs/standards/rooms-access.md
index 08194faa..bc96c436 100644
--- a/docs/standards/rooms-access.md
+++ b/docs/standards/rooms-access.md
@@ -202,6 +202,12 @@ separate control password. Viewer identity and requests reuse authenticated room
membership; grants and encrypted input stay bound to the current route and source.
Changing the source, stopping sharing, authorization loss or route retirement
releases the corresponding grants and held input. Reconnection requires a new grant.
+Preparing a source replacement preserves the current source's authorization;
+failed preparation or cancellation does not retire a still-active source.
+An individual input execution failure drops that operation and preserves the
+grant. Failed key/button releases remain owned cleanup; only releases are retried,
+never presses, movement or text. Missing current target geometry suspends input
+until observation recovers; confirmed target identity loss retires its grants.
Remote control defaults to one controller. The Host can enable **Allow
simultaneous control** for the current source so approved, capability-negotiated
diff --git a/docs/standards/routing-transport.md b/docs/standards/routing-transport.md
index 544ec7e7..e7041c28 100644
--- a/docs/standards/routing-transport.md
+++ b/docs/standards/routing-transport.md
@@ -99,9 +99,12 @@ Every non-server endpoint has one server-authoritative steady outbound media-
copy cap `C`, default `2` and configurable only as `1`, `2`, or `3`.
A Viewer can also volunteer to relay to more participants
-for the current visit. That explicit intent raises its permitted fanout only to
-the endpoint's reported capability, at most the room's 20-Viewer limit. Ordinary
-participants keep the configured cap. Withdrawing consent schedules affected
+for the current visit. It starts with one extra child above the ordinary cap;
+the Viewer may select a higher maximum within the endpoint's reported capability,
+at most the room's 20-Viewer limit. The requested maximum survives backend
+rediscovery within that visit; a smaller available capability limits the applied
+value without replacing the choice. Ordinary participants keep the configured
+cap. Withdrawing consent schedules affected
children through the same operation; it does not abruptly drop the subtree.
This remains an ordinary Peer relay, with no separate SFU service or authority.
@@ -217,6 +220,8 @@ first request.
The budget belongs to the current parent and survives physical connection or
Browser/Native backend replacement. Only a connected transport or a new parent
resets it; replacing an unconnected backend cannot renew recovery indefinitely.
+Host signaling status is not transport-failure evidence. Its offline status alone
+does not cancel current-edge recovery or retire a relay's downstream connections.
Capable Native receivers also renegotiate that connection while retaining the
encoded source, local playback and downstream edges when its codec/audio shape
is unchanged. Pion owns ICE restart; each actual gathering owns fresh supplemental
diff --git a/docs/standards/versioning.md b/docs/standards/versioning.md
index c45f4cc8..54e3dffe 100644
--- a/docs/standards/versioning.md
+++ b/docs/standards/versioning.md
@@ -25,8 +25,9 @@ Keep those internal values at the first public release: resetting schema or
authorization counters for visual consistency can invalidate data or stale-work
fences. A room number is a user-facing address, not a software version.
The private tooling-only npm package carries no independent product version.
-The App launcher displays its installed version from the existing launcher state;
-the Web help menu displays its bundled build version without a network lookup.
+The shared App launcher displays its installed version from the existing launcher
+state; development builds also show their short source revision.
+The Web help menu displays its bundled build version without a network lookup.
Server startup logs retain the running version and revision. The website may
display GitHub's latest stable tag and `published_at` as optional metadata, never
a manually maintained version or the installed App's identity. Publication time
@@ -111,13 +112,25 @@ revision. Do not maintain independent hand-edited App/Server/Web version files.
Archive layout and checks stay in the
existing packagers; a new build never replaces an already-published package.
-App archive names are fixed per target: `piik-app-{target}.zip`. Each published
+Android `versionName` uses the product version. Its `versionCode` is generated as
+`major * 1000000 + minor * 1000 + patch`; minor and patch must each be at most
+999, and the result must be in Android's `1..2100000000` range. Development
+builds use code 1 and include the short revision in `versionName`. The shared
+product version remains the only release ordering input; no Android counter is
+maintained separately.
+
+Desktop App archive names are fixed per target: `piik-app-{target}.zip`. Each published
tag owns its archive bytes; the full source SHA remains in `REVISION` and the
release descriptor. Fixed names allow GitHub's native latest-asset links;
[website operations](../operations/website.md#preview) owns download presentation
and verification of the matching Gitee mirror.
-Public Release attachments contain the App ZIPs and Server runtime archives
+Android uses the installable `piik-app-android-universal.apk` for both ARM64 and
+x64. It follows the same release identity, checksum, complete-target and
+mirror rules. [Android build instructions](../../mobile/README.md#signed-release-apk)
+own signing input and installation details.
+
+Public Release attachments contain the App ZIPs/APK and Server runtime archives
for the registered package targets. Package descriptors (`.release.json`), file manifests (`.manifest.tsv`)
and standalone `.sha256` files remain build/deployment inputs in the local output
and CI artifacts; publishers validate them but do not upload them to GitHub or
diff --git a/docs/standards/visual-language.md b/docs/standards/visual-language.md
index b43cc740..95403814 100644
--- a/docs/standards/visual-language.md
+++ b/docs/standards/visual-language.md
@@ -85,7 +85,7 @@ permissions and window chrome are verified in the actual platform, not imitated.
| Choose a short, fixed mode | Shared `.lr-toggle` sliding selection; option chips/cards when individual choices need explanations, richer previews or wrapping |
| Switch related content pages | A named tablist with one selected tab and its associated panel; use the shared sliding treatment for short labels, including capture categories, and `navigateTabs` for Arrow/Home/End navigation |
| Pick a device or an item from a variable list | Existing named selector/menu; use source thumbnails when appearance helps identify the item |
-| Adjust a numeric value | Range slider for quick adjustment, with a visible value; reuse `QualityNumberControl` when exact entry or a wider accepted range is useful |
+| Adjust a numeric value | Range slider for quick adjustment, with a visible value; reuse `NumberControl` when exact entry or a wider accepted range is useful |
| Return within a room tool | Shared `BackButton`: left arrow and **Back**, with the same quiet dense treatment in settings and expanded reactions |
| Apply several settings together | A named preset tile/chip; allow no preset to be selected when the current values are custom |
@@ -201,6 +201,11 @@ without triggering the action on release. Action controls keep their direct
short-tap action. `Tooltip` owns these interactions; pages do not add their own
open state or timers.
+An actionable sharing failure reveals its status guidance once without moving
+focus. Keep troubleshooting on the status indicator's direct action; do not add
+a permanent caption beside the light. The shared tooltip retains dismissal and
+can be reopened through its ordinary hover or touch-hold interaction.
+
Mouse hover opens after 500 ms of staying on the trigger; passing through or
operating a control cancels that pending hint. Keyboard focus and explicit help
activation do not wait. Keep the open panel hoverable for reading, with a short
@@ -316,8 +321,10 @@ Playback follows the [shared playback owner](./presentation-lifecycle.md#playbac
Keep related controls together at the picture's lower edge: local playback and
sound, remote input, viewing options, then window modes with fullscreen last.
Common actions remain directly available; detailed preferences use an anchored
-popover. A narrow player may use two adjacent rows in the same capsule to preserve
-usable controls, not send window actions to a distant corner. One visibility
+popover. A narrow player keeps one compact row, including sound, remote input
+and window actions. Picture fit and rotation move to the same attached settings
+at that width, reusing their controls rather than duplicating visible actions.
+The playback surface is lightly translucent over the picture. One visibility
owner hides the complete surface. Grants, pause and source changes do not move
the bar or resize the picture. Remote input retains its local bezel pull.
Playback settings own on-screen chat appearance and connection preferences;
@@ -328,6 +335,21 @@ change local display only, separate from the Host's picture settings.
Persistent interaction and remote-permission tools may use the shared floating
window; the remote keyboard docks outside the picture and can detach on capable
browsers. One-step actions and brief adjustments do not need movable windows.
+Touch controllers default to direct touch without a cursor. A draggable mini
+mouse expands on tap, with its cursor spaced above and to the left; one pointer
+position drives both. The existing viewport pans the picture to leave room for
+the mouse at its edges. Added blank space is bounded by the tool's footprint,
+and the cursor stops at the source picture's edges. Two-finger pinch and pan
+move the picture beneath the full mouse, reanchoring its local source target
+without remote input. The compact trackpad retains its two-finger scrolling
+at normal scale.
+A separate button minimizes it and restores the selected touch mode; the
+compact trackpad cursor remains an optional local preference in playback settings.
+Only the mini mouse tucks away after resting at a picture edge, using the player
+controls' idle delay. The wheel shares middle-click, scrolling and held middle-drag
+in one target. Expanded buttons, scrolling and pointer movement share
+the existing input grant and pointer geometry; the keyboard keeps its separate
+docked surface.
Visual source choices (windows, screens and cameras) share named thumbnail cards,
including their focus, hover and refresh treatment. Thumbnails fit the whole frame
diff --git a/docs/status.md b/docs/status.md
index 12555e16..a421c923 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -1,6 +1,6 @@
# Current Status
-Last updated: 2026-10-24
+Last updated: 2026-10-26
This is the compact execution/deployment index. Product modules own behavior,
[verification status](./verification-status.md) summarizes material evidence limits,
@@ -42,6 +42,12 @@ ownership. [TODO](./todo.md) is the only unfinished-work ledger; Git and PRs own
completed fixes. [Verification status](./verification-status.md) owns material
acceptance and platform boundaries.
+The Android system-browser App shares the desktop Web UI and Go core,
+including Local/public-link rooms, Site approval, native screen capture and room
+voice. Its universal APK covers ARM64 and x64 through the shared release pipeline;
+[device evidence](./verification-status.md#device-evidence-boundary) and
+[remaining work](./todo.md) retain their existing owners. iOS remains separate.
+
Room voice, shared keyboard/mouse and optional Windows virtual gamepads use the
normal App/Web interfaces. App packages contain no optional input SDK or driver.
Linux uinput has an isolated kernel check; desktop permission and game-recognition
diff --git a/docs/todo.md b/docs/todo.md
index a3dd9023..c63bc484 100644
--- a/docs/todo.md
+++ b/docs/todo.md
@@ -1,28 +1,32 @@
# Current TODO Ledger
-Last reviewed: 2026-10-24
+Last reviewed: 2026-10-26
This ledger contains unfinished work and explicit owner holds. Product modules
own behavior; research and Git/PRs own evidence and completed history.
## Now
+- [ ] **Virtual-LAN page loading.** Investigate the UU report where only the tab
+ title appears and the page never loads. Static compression reduces transfer
+ size; capture the stalled document/script response to locate this failure.
- [ ] **Remote-control end-to-end latency.** Measure input-to-visible-response
on an attended target, covering OS execution, Native capture/encoding, remote
transport and display in the same trial. The
[input assessment](./research/room-interactions.md#window-capture-and-game-input)
owns completed local component measurements.
-- [ ] **Windows window-sharing black screen (#495).** Investigate
- [#495](https://github.com/TNTcraftHIM/Piik/issues/495): Windows 10, Chrome 155,
- Piik v2.3.0; native Chrome/Edge window sharing fails while QQ and Browser
- capture work. The latest report uses VP8: transport connects, but capture
- remains `starting` with no video frames. Obtain the App failure stage or compare
- the existing Intel/NVIDIA adapter choices with VP8 fixed on that device.
-- [ ] **Intel native H.264 startup (#452).** Follow the
- [new v2.4.0 report](https://github.com/TNTcraftHIM/Piik/issues/452): explicit H.264
- fails while VP8 works on Intel Arc B580 / Intel Graphics. The Browser report
- records App startup rejection; an App diagnostic is needed to identify its
- native failure stage.
+- [ ] **Linux capture ends after selection (#499).** Follow
+ [#499](https://github.com/TNTcraftHIM/Piik/issues/499) on niri/Wayland with
+ GStreamer NVENC. The Browser report records capture ending before connection
+ cleanup; obtain the native capture failure to distinguish the encoder and Portal.
+- [ ] **macOS window capture with sound (#500).** Follow
+ [#500](https://github.com/TNTcraftHIM/Piik/issues/500). Compare the same window
+ with source sound on and off in the App that waits for video `active`
+ before starting source audio. Obtain the App diagnostic if the window stays black.
+- [ ] **Control loss when the Host browser is minimized (#502).** Follow
+ [#502](https://github.com/TNTcraftHIM/Piik/issues/502) with the corrected
+ Windows App. If minimizing still prevents input, obtain its Host App
+ diagnostic ZIP to identify the rejected system operation.
- [ ] **Return LiveKit to an upstream pin.** Follow
[upstream #5016](https://github.com/livekit/livekit/pull/5016). Once upstream
supplies the allocator input Piik needs, verify recovery and remove the
@@ -32,16 +36,17 @@ own behavior; research and Git/PRs own evidence and completed history.
## Next
- [ ] **Release headlines in App and Server update checks.** Keep the prepared
- implementation on the feature branch for a later feature release. It consumes
+ implementation on `archive/deferred-prototypes` for a later feature release. It consumes
the existing [headline contract](./standards/versioning.md#release-notes).
-- [ ] **Android App release scope.** Keep the Local-sharing APK prototype on
- the feature branch. Complete phone, tablet and TV acceptance, hosted-site
- integration and release packaging before including Android in a public release.
## Held By Owner
+- [ ] **iOS App publication.** Held on 2026-10-25. Keep the system-browser
+ implementation on `feat/ios-client`, outside the Android release candidate.
+ Apple SDK builds pass; signed-device acceptance still needs continued-session
+ admission/expiry, recording consent, background capture and hosted-site linkage.
- [ ] **QR invitations.** Deferred from the maintenance release on 2026-10-24.
- Keep the local QR implementation on the feature branch for later acceptance.
+ Keep the local QR implementation on `archive/deferred-prototypes` for later acceptance.
- [ ] **Additional collaboration cursors.** Deferred by the owner. Keep the
shared system mouse; do not add multi-cursor or pointer-ownership visualization.
- [ ] **Persistent-loss estimator replacement.** Deferred on 2026-10-18.
@@ -59,6 +64,11 @@ own behavior; research and Git/PRs own evidence and completed history.
## Parked Product Work
+- **Piik 3.0 scope.** Explore independent room, chat, voice and screen-share
+ lifecycles, with optional image/file exchange, retaining the current topology
+ and route controller. Compare continued Go with scoped Rust components before
+ choosing implementation scope. The [hosting and subscription assessment](./research/room-interactions.md#multi-room-and-distributed-hosting-assessment)
+ distinguishes per-room hosting, federation and replicated room authority.
- **Virtual gamepads beyond Windows.** Linux uinput passed its isolated kernel
gate; desktop permissions and game recognition need physical checks. macOS
virtual HID needs an entitled App and macOS SDK/runtime checks. The
diff --git a/docs/verification-status.md b/docs/verification-status.md
index 34500e7c..3a3c2066 100644
--- a/docs/verification-status.md
+++ b/docs/verification-status.md
@@ -1,6 +1,6 @@
# Verification Status
-Last updated: 2026-10-21
+Last updated: 2026-10-26
This file summarizes evidence boundaries that materially affect product claims.
[Status](./status.md) owns candidate and deployment readiness; research owns
@@ -23,6 +23,18 @@ phases, including NAT and Auto codec behavior.
- macOS/Linux builds and bounded native checks are distinct from physical
capture and input. Physical platform coverage remains limited.
+- Android's system-browser App passed emulator screen/camera sharing
+ with concurrent room voice. Browser consent returns to the same tab; source
+ replacement, cancellation and background return preserve the share. Local/public
+ startup and launcher reentry also have emulator evidence. Full-App checks now
+ cover concurrent original/lower encoding, live-budget reduction and recovery,
+ static-source late joins, lower-output retirement, capture stop and restart.
+ The [capture assessment](./research/cross-platform-client-runtime.md#android-encoder-budget)
+ owns measured bitrate bounds and the stock FrameDropper's saturation limit.
+ Phone/tablet/TV hardware coverage and physical audio remain limited.
+- The separate iOS system-browser implementation passes Apple SDK device and
+ simulator builds and its session/encoder checks. Physical capture, background
+ lifetime and hosted-site linkage remain in the [held iOS scope](./todo.md#held-by-owner).
- Windows HDR-to-SDR has synthetic GPU and decoded-video evidence; it does not
establish Browser-capture HDR conversion or end-to-end HDR. The
[fidelity assessment](./research/media-fidelity.md#hdr-to-sdr) owns that boundary.
diff --git a/go.mod b/go.mod
index 62459a5a..c2ee12e0 100644
--- a/go.mod
+++ b/go.mod
@@ -16,6 +16,7 @@ require (
github.com/charmbracelet/x/term v0.2.2
github.com/coder/websocket v1.8.15
github.com/go-logr/logr v1.4.4
+ github.com/klauspost/compress v1.20.1
github.com/livekit/livekit-server v1.13.11-0.20261016133420-cde3991916cf
github.com/livekit/mediatransportutil v0.0.0-20261014192331-dd8e31a0adc0
github.com/livekit/protocol v1.52.2-0.20261012065025-9bb708cdb819
@@ -31,13 +32,17 @@ require (
github.com/pion/transport/v5 v5.1.1
github.com/pion/turn/v5 v5.1.2
github.com/pion/webrtc/v4 v4.2.24
+ github.com/stretchr/testify v1.12.1
github.com/thesyncim/gopus v0.2.2
+ github.com/wlynxg/anet v0.0.5
go.uber.org/zap v1.28.0
- golang.org/x/crypto v0.57.0
- golang.org/x/mod v0.41.0
- golang.org/x/net v0.60.0
- golang.org/x/sync v0.23.0
- golang.org/x/text v0.42.0
+ golang.org/x/crypto v0.58.0
+ golang.org/x/mobile v0.0.0-20261011034528-197770444867
+ golang.org/x/mod v0.42.0
+ golang.org/x/net v0.61.0
+ golang.org/x/sync v0.24.0
+ golang.org/x/sys v0.49.0
+ golang.org/x/text v0.43.0
google.golang.org/protobuf v1.36.12
modernc.org/sqlite v1.58.0
)
@@ -66,7 +71,6 @@ require (
github.com/gammazero/deque v1.2.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
- github.com/google/cel-go v0.30.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/uuid v1.6.0 // indirect
@@ -77,7 +81,6 @@ require (
github.com/jellydator/ttlcache/v3 v3.4.1 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/jxskiss/base62 v1.1.0 // indirect
- github.com/klauspost/compress v1.20.1 // indirect
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
@@ -86,16 +89,12 @@ require (
github.com/livekit/psrpc v0.8.2 // indirect
github.com/lucasb-eyer/go-colorful v1.4.1 // indirect
github.com/mackerelio/go-osstat v0.2.8 // indirect
- github.com/magefile/mage v1.17.2 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/mattn/go-runewidth v0.0.24 // indirect
github.com/mdlayher/netlink v1.11.2 // indirect
github.com/mdlayher/socket v0.6.1 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
- github.com/nats-io/nats.go v1.53.1 // indirect
- github.com/nats-io/nkeys v0.4.16 // indirect
- github.com/nats-io/nuid v1.0.1 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/petermattis/goid v0.0.0-20260918085751-abfca077860b // indirect
github.com/pion/datachannel v1.6.3 // indirect
@@ -110,11 +109,9 @@ require (
github.com/prometheus/common v0.72.0 // indirect
github.com/prometheus/procfs v0.22.0 // indirect
github.com/puzpuzpuz/xsync/v4 v4.5.0 // indirect
- github.com/redis/go-redis/v9 v9.22.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/twitchtv/twirp v8.1.3+incompatible // indirect
- github.com/wlynxg/anet v0.0.5 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/zeebo/xxh3 v1.1.0 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
@@ -127,8 +124,8 @@ require (
go.uber.org/zap/exp v0.3.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/exp v0.0.0-20261005173118-76772065c9b0 // indirect
- golang.org/x/sys v0.48.0 // indirect
golang.org/x/time v0.16.0 // indirect
+ golang.org/x/tools v0.52.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20261005182115-fad411399dd8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20261005182115-fad411399dd8 // indirect
google.golang.org/grpc v1.84.0 // indirect
diff --git a/go.sum b/go.sum
index fbe93f98..a2b63d13 100644
--- a/go.sum
+++ b/go.sum
@@ -1,17 +1,11 @@
-buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.11-20260709200747-435963d16310.1 h1:fXh8CsdNpjRr8R5vFdqtIxPt/Lno2IIJlYOdZBIZn0w=
-buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.11-20260709200747-435963d16310.1/go.mod h1:tvtbpgaVXZX4g6Pn+AnzFycuRK3MOz5HJfEGeEllXYM=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.12-20260825204119-511051f7f437.2 h1:NbnlmV26O7oZ1iM5tsCI+GEx+3ZSdrhvnKQ/eWSrLiY=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.12-20260825204119-511051f7f437.2/go.mod h1:TCt1lluMFnctISJXvkIQ4x3ABrPuUKCWKyjKdkJNBpw=
-buf.build/go/protovalidate v1.2.0 h1:DQVrUWkmGTBij+kOYv/x2LLxwcLaGKMdzShj1/6/3H0=
-buf.build/go/protovalidate v1.2.0/go.mod h1:7rYiQEhqvAipoazpVNBBH2S2f8bjG4huMVy1V2Yofn4=
buf.build/go/protovalidate v1.4.0 h1:UjLrYbt5VX7+TMOs2+pG5FhZhIG1mSfK4EIopbb4LcM=
buf.build/go/protovalidate v1.4.0/go.mod h1:8vJfzNT6NIG2qm3uFsJDXMlRmG+bQJzbcIn1Aa0vPGs=
buf.build/go/protoyaml v0.7.0 h1:z4oVoFicbpPefhT7WAykxUdfp0yEQlhMQ2mCZOY5V38=
buf.build/go/protoyaml v0.7.0/go.mod h1:+a0cavd0uMvirb87xdu2ZMMmjlIQoiH/N2Ich5MGSQ0=
cel.dev/cel-go v0.32.0 h1:irvpFKr5EuGPyxeME03ERh0rii1TX+BDAnB9eL3IvNk=
cel.dev/cel-go v0.32.0/go.mod h1:DnVip7tpJSsgZymwfT+m1tnEVy3ivAjSMXPx12YrMkU=
-cel.dev/expr v0.25.2 h1:K6j46C81hXtZQfuX60cVWQFBJahKSE2gfRbNuvr5bFs=
-cel.dev/expr v0.25.2/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4=
cel.dev/expr v0.25.3 h1:A2jO8jwOugrrovveCWfj0KEZOfqiLgAcwjpHPhzIGw0=
cel.dev/expr v0.25.3/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4=
charm.land/bubbletea/v2 v2.0.9 h1:DpJCMWKgzQK8SJv4zbKKFHAI10ymWy/evClPFk0k0f8=
@@ -20,8 +14,6 @@ charm.land/lipgloss/v2 v2.0.6 h1:EaGKeuA8FvF+v2BT5VmZd2LoYLaMZJXA5n34th8nCIQ=
charm.land/lipgloss/v2 v2.0.6/go.mod h1:ipDDJNSGa1hlwDtSfW1s2/xR8Vdhbut4PXh2zEKZd0Q=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
-github.com/TNTcraftHIM/livekit v1.13.11-0.20261015180525-f10692c6f232 h1:SM6Ly7cYLi1Ap+UMPZYLLi5f4T1xHRF0OrB3N3e0KvU=
-github.com/TNTcraftHIM/livekit v1.13.11-0.20261015180525-f10692c6f232/go.mod h1:j0XqdfycMvylbtSq2SE4UG2aGF0SADNReWrQfbpn2DE=
github.com/TNTcraftHIM/livekit v1.13.11-0.20261018043008-c7f56b389713 h1:RrcmStTuxqbsgEai2S+wN5vBYkrMyx9GZIWKBwJgIBA=
github.com/TNTcraftHIM/livekit v1.13.11-0.20261018043008-c7f56b389713/go.mod h1:VisDyFQKxQr0ncrV3558MBUKjlrTUqXMBQwsxEHMRgA=
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
@@ -36,10 +28,6 @@ github.com/bluenviron/mediacommon/v2 v2.9.5 h1:6r1vq9qFH5AUUjPOPxRd00BhBNAg1EcmY
github.com/bluenviron/mediacommon/v2 v2.9.5/go.mod h1:1J4QckclKIksdI9+7C+PJXVBkpO/JYWn7/h+wOYq+7E=
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
-github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
-github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
-github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
-github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -89,9 +77,8 @@ github.com/elliotchance/orderedmap/v3 v3.1.1 h1:eV7lfZ5fVL8d36b8Wogqi/eqm7R/kZcf
github.com/elliotchance/orderedmap/v3 v3.1.1/go.mod h1:G+Hc2RwaZvJMcS4JpGCOyViCnGeKf0bTYCGTO4uhjSo=
github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w=
github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE=
-github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
-github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
+github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
github.com/florianl/go-tc v0.4.8 h1:hgmakUX1Nm0Ba1I0ZkbUl9CH6HbRwqSiwipnpmYp3Es=
github.com/florianl/go-tc v0.4.8/go.mod h1:B8GeOEnmrbOnxZtaCvsYJcgIzzmM8c/AIhtfCZsDj3Q=
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
@@ -113,8 +100,6 @@ github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63Y
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
-github.com/google/cel-go v0.30.0 h1:ll54AkzKunWkBn9wSoiUXbFZXYZTkdJGNXTBXUoolGo=
-github.com/google/cel-go v0.30.0/go.mod h1:X0bD6iVNR8pkROSOoHVdgTkzmRcosof7WQqCD6wcMc8=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@@ -162,8 +147,6 @@ github.com/jsimonetti/rtnetlink v0.0.0-20211022192332-93da33804786 h1:N527AHMa79
github.com/jsimonetti/rtnetlink v0.0.0-20211022192332-93da33804786/go.mod h1:v4hqbTdfQngbVSZJVWUhGE/lbTFf9jb+ygmNUDQMuOs=
github.com/jxskiss/base62 v1.1.0 h1:A5zbF8v8WXx2xixnAKD2w+abC+sIzYJX+nxmhA6HWFw=
github.com/jxskiss/base62 v1.1.0/go.mod h1:HhWAlUXvxKThfOlZbcuFzsqwtF5TcqS9ru3y5GfjWAc=
-github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
-github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/compress v1.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ=
github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
@@ -181,30 +164,14 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/libp2p/go-netroute v0.2.1 h1:V8kVrpD8GK0Riv15/7VN6RbUQ3URNZVosw7H2v9tksU=
github.com/libp2p/go-netroute v0.2.1/go.mod h1:hraioZr0fhBjG0ZRXJJ6Zj2IVEVNx6tDTFQfSmcq7mQ=
-github.com/lithammer/shortuuid/v4 v4.2.0 h1:LMFOzVB3996a7b8aBuEXxqOBflbfPQAiVzkIcHO0h8c=
-github.com/lithammer/shortuuid/v4 v4.2.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y=
github.com/lithammer/shortuuid/v4 v4.3.0 h1:XRr80OYPOlqxgnCv2/iuElkl/ZtXtPtLKk2AvpXGdWA=
github.com/lithammer/shortuuid/v4 v4.3.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y=
-github.com/livekit/livekit-server v1.13.6 h1:vvLufsmzMG9ueZ6BqSahasC8rUH1toSv4979fOY9uQI=
-github.com/livekit/livekit-server v1.13.6/go.mod h1:Dxd+bHclL+F08/T8TlZkxjSRYXCHObLo+ujPRc7h7MM=
-github.com/livekit/livekit-server v1.13.9 h1:HPKsWMDF+fXWX7mrXD4Cul5L9X8dSZ6KOjXlBm1m4tk=
-github.com/livekit/livekit-server v1.13.9/go.mod h1:yF0PoK9MxarCECGVj/MWKsEbH9t4SQmn1CL0dEXQ3Ks=
-github.com/livekit/livekit-server v1.13.11-0.20261014174133-18df6ad4b694 h1:nq/kk6q8W+Mkx1IAfkxd1YuhNQb6jLryxyQ/6Q/IdYI=
-github.com/livekit/livekit-server v1.13.11-0.20261014174133-18df6ad4b694/go.mod h1:j0XqdfycMvylbtSq2SE4UG2aGF0SADNReWrQfbpn2DE=
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5ATTo469PQPkqzdoU7be46ryiCDO3boc=
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
-github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:BcliKAXoMhl/nWmzQweQ5kmh4Qqagxl4s3Z5pvM/7AY=
-github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU=
github.com/livekit/mediatransportutil v0.0.0-20261014192331-dd8e31a0adc0 h1:BSLyHr3Lsl71bHGHsfiYNmbohugR2yoaf9HjAZeFJnc=
github.com/livekit/mediatransportutil v0.0.0-20261014192331-dd8e31a0adc0/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU=
-github.com/livekit/protocol v1.50.5-0.20260821144838-17c16cf496fd h1:pGWIR9fw1eC699v2njRX/7Y+baEXB7p6k9CvDg2uVrQ=
-github.com/livekit/protocol v1.50.5-0.20260821144838-17c16cf496fd/go.mod h1:h4wP4IBPkT5aew7SZSGViHSmanKAiKWsHij/5YfL4BE=
-github.com/livekit/protocol v1.52.2-0.20261007103317-a935cd67c29a h1:Sa9+0Kr+MVIabmJaBFtX8Yl3NYewkv02/mMDvLEKYiQ=
-github.com/livekit/protocol v1.52.2-0.20261007103317-a935cd67c29a/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4=
github.com/livekit/protocol v1.52.2-0.20261012065025-9bb708cdb819 h1:oZ/dI5RG22t0GalRDU1BJWCMtNAEQVPU+odrRgycKDE=
github.com/livekit/protocol v1.52.2-0.20261012065025-9bb708cdb819/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4=
-github.com/livekit/psrpc v0.7.3 h1:bekuZt/ZQzg8+/M8G6G5jq7bvV9fAKdPHSOZeTwrIIc=
-github.com/livekit/psrpc v0.7.3/go.mod h1:rAI+m2+/cb4x9RXhLRtUx5ZwdfjjXOl4zi46IjEetaw=
github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc=
github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q=
github.com/livekit/server-sdk-go/v2 v2.14.0 h1:yy7LsR1+64DHC2i4DxZe1xX4ScVq+S3CvgpvHRruEec=
@@ -213,8 +180,6 @@ github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBql
github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mackerelio/go-osstat v0.2.8 h1:I2duicTaCGWoM53XwAwA9OIe1inu0xnVs8/pqOWWVr4=
github.com/mackerelio/go-osstat v0.2.8/go.mod h1:SyS3XxKdoSKJnTGTkN5Yrh6VUQVuAURACfE6y+2DN4k=
-github.com/magefile/mage v1.17.2 h1:fyXVu1eadI8Ap1HCCNgEhJ5McIWiYhLR8uol64ZZc40=
-github.com/magefile/mage v1.17.2/go.mod h1:Yj51kqllmsgFpvvSzgrZPK9WtluG3kUhFaBUVLo4feA=
github.com/mattn/go-colorable v0.1.15 h1:+u9SLTRGnXv73cEsnsmoZBom+dMU88B2M0aDcWy0/jY=
github.com/mattn/go-colorable v0.1.15/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
@@ -242,18 +207,14 @@ github.com/mdlayher/socket v0.6.1 h1:M7uj2NtuujUY4mYr1C57NmfNiRHbkKpnBxO856lsc3A
github.com/mdlayher/socket v0.6.1/go.mod h1:+/SGtqc9V+5dAuRgQsU0fGBI+oRDiW7O2Obx10OIWfg=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
-github.com/moby/moby/api v1.55.0 h1:2/sexvQyqIWS8pRSCFddBfpW2qE7vR7FCL+vN8pxwMc=
-github.com/moby/moby/api v1.55.0/go.mod h1:+RQ6wluLwtYaTd1WnPLykIDPekkuyD/ROWQClE83pzs=
github.com/moby/moby/api v1.56.0 h1:GQzua3NA599ASSIICx0iFgiJeO9YkdDARvQsm23ZZuQ=
-github.com/moby/moby/client v0.5.1 h1:tYNaJno4c0HXz12y5BiqEDy0rVTYkWzI26lGvnTMiJw=
-github.com/moby/moby/client v0.5.1/go.mod h1:odLstlZ6uSnfvAgVxMpvgmb8SUdd+siH2T0GBuxVAlM=
+github.com/moby/moby/api v1.56.0/go.mod h1:sZ+THbVWkjOmBPPfbnzdD/G1LuIexWhqlSHHPTDQ1Uk=
github.com/moby/moby/client v0.6.0 h1:AJjEB21QPbXSXjDsZorFBoDZPhMrfbpaPLgSMAW9Bgs=
+github.com/moby/moby/client v0.6.0/go.mod h1:OCo00wNRyA3m4lmJ228W3JbyCN4ZNNYjpOXiJydBdcQ=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
-github.com/nats-io/nats.go v1.52.0 h1:n3avV4VBsCgsdwh71TppsTwtv+QdPs7ntSKM8qJLGsc=
-github.com/nats-io/nats.go v1.52.0/go.mod h1:26HypzazeOkyO3/mqd1zZd53STJN0EjCYF9Uy2ZOBno=
github.com/nats-io/nats.go v1.53.1 h1:Otsq3uLc/kLdjmkNHkXH0jBqwUquwdKFoe3fq6/3/Xo=
github.com/nats-io/nats.go v1.53.1/go.mod h1:26HypzazeOkyO3/mqd1zZd53STJN0EjCYF9Uy2ZOBno=
github.com/nats-io/nkeys v0.4.16 h1:rd5oAuLOb8mnAycB0xleuEBNS1pVVnN0fv/FF34Eypg=
@@ -268,18 +229,12 @@ github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJw
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
github.com/ory/dockertest/v4 v4.0.0 h1:i19aFsO/VXE0VrMk4ifnKW4G/KIJ93PCjLOslxXoPME=
github.com/ory/dockertest/v4 v4.0.0/go.mod h1:b5Ofu8VIxWNhXFvQcLu17pRNQdoUBKtXBW74G4Ygzx8=
-github.com/petermattis/goid v0.0.0-20260725062400-500c67a39b75 h1:VmZ6mKVkxavKEhEy4ZYyV7BwBYBFBP0TwIqmLk84fpU=
-github.com/petermattis/goid v0.0.0-20260725062400-500c67a39b75/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
github.com/petermattis/goid v0.0.0-20260918085751-abfca077860b h1:OzNsuVdSWGwXvWKTtChx9ve89k4cFJ6NxYM4Aw4/f+E=
github.com/petermattis/goid v0.0.0-20260918085751-abfca077860b/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
github.com/pion/datachannel v1.6.3 h1:HZAYLpunVI0xi057ZiANl558d0uSpomcWw4UEPz3qXs=
github.com/pion/datachannel v1.6.3/go.mod h1:oRmkXVVXxkUbqa8MatgdNHW29kvtQY3HUdjJQsfv2GE=
-github.com/pion/dtls/v3 v3.1.10 h1:HWC+QCZitP/ApADS/6+g7UIw2YmLgoK3CsynnjPJgMo=
-github.com/pion/dtls/v3 v3.1.10/go.mod h1:iKFQNYrjsN2TiA2YKKMqB9MOZaFpjFULBI/A4sW0eyc=
github.com/pion/dtls/v3 v3.1.13 h1:oWNjsoF3xUPDhzOuE8U42ql1BtVzWc8X4Asiz9BKwYk=
github.com/pion/dtls/v3 v3.1.13/go.mod h1:iKFQNYrjsN2TiA2YKKMqB9MOZaFpjFULBI/A4sW0eyc=
-github.com/pion/ice/v4 v4.4.7 h1:JiI6VId3XnIztxKE8RjTC0SeXrp7ppkAO0jGez3/sIE=
-github.com/pion/ice/v4 v4.4.7/go.mod h1:zCRRg4Mkpdbx9B+75fd4z2APvIXeIraeUOB7IMPt8mU=
github.com/pion/ice/v4 v4.4.8 h1:NB22Ze5WrgMjcf9L+O+1B29bl6anq+sbyvddImDLdhc=
github.com/pion/ice/v4 v4.4.8/go.mod h1:zCRRg4Mkpdbx9B+75fd4z2APvIXeIraeUOB7IMPt8mU=
github.com/pion/interceptor v0.1.49 h1:iyBsNHRoLNNXZiJA/DdGvJgdyfS8YWDCzxIAlVjt5nY=
@@ -308,24 +263,16 @@ github.com/pion/transport/v5 v5.1.1 h1:Sauq8YJqRopgRfIgnpfKpdpKG14g0N4jWlRgpWsEf
github.com/pion/transport/v5 v5.1.1/go.mod h1:Qxw6fCEjFWQkRDZOhS4Vf+neJBcihauvA3uyEa1J1F0=
github.com/pion/turn/v5 v5.1.2 h1:acEOO+D5txJ52vKfcilmSMItfP23Q9K6pJ1LM5P2VV0=
github.com/pion/turn/v5 v5.1.2/go.mod h1:57Oadc8fHnac15VFyCd6nEaBTVZBxQe2POUMD4lsHRA=
-github.com/pion/webrtc/v4 v4.2.23 h1:AlVixhTtQmxrheSv6lAH0w2sH5qjM7OTos5Lt811nk0=
-github.com/pion/webrtc/v4 v4.2.23/go.mod h1:vzf5RQ0uMBuXAYTK2pabZUtnZyj2t3TF88eL4xzQZvI=
github.com/pion/webrtc/v4 v4.2.24 h1:Ove9nRpDAfK39+QSw3/Ru17VDiECjbiLaM73mRG4Yms=
github.com/pion/webrtc/v4 v4.2.24/go.mod h1:JxD/FgjMDKCWFukjknTdG5a9rR4PUxkg0yBX1/JBInM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/prometheus/client_golang v1.24.1 h1:JnJkREXzWxUdCuPFpIWZiPispT9xVV59uiuyR2bPlnU=
github.com/prometheus/client_golang v1.24.1/go.mod h1:F+oSRECHg4sse5ucfYpYDeIv/hu68Zo0uoHKetWnzcE=
-github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
-github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
github.com/prometheus/client_model v0.6.3 h1:O0jaTVAYNxTHYInEPFJt5I3+sN8zqBtVMPTB1qyxiEo=
github.com/prometheus/client_model v0.6.3/go.mod h1:gpN5P9S7Rr6Yr92PiQ+Ixvhf6JZEkF1dnxsYL2aPBEM=
-github.com/prometheus/common v0.70.1 h1:1HvjP4D5oL3t8RsPlwxA9onvvStjtIHYE5XuuwOi/PY=
-github.com/prometheus/common v0.70.1/go.mod h1:VdFUQDMZK3VLkurFUVhia6uys/0suUp86TJz5qbJRhc=
github.com/prometheus/common v0.72.0 h1:tAYsE+sPJxIncDAobm4H5aQjmox9ZxEIIqPbiffa8G4=
github.com/prometheus/common v0.72.0/go.mod h1:77NWqAQ2tXT7BIK40qjJdw5Acrsrg1TlHAnsQi3i6mk=
-github.com/prometheus/procfs v0.21.1 h1:GljZCt+zSTS+NZq88cyQ1LjZ+RCHp3uVuabBWA5+OJI=
-github.com/prometheus/procfs v0.21.1/go.mod h1:aB55Cww9pdSJVHk0hUf0inxWyyjPogFIjmHKYgMKmtY=
github.com/prometheus/procfs v0.22.0 h1:6q9+/JL9IKAPbCmBrv9n5O5Ty3NKnciV5X7YGw0oics=
github.com/prometheus/procfs v0.22.0/go.mod h1:CvmFr/GVhIjIvWJZW3tgkODBQMRIf0EyWMQLHCHab58=
github.com/puzpuzpuz/xsync/v4 v4.5.0 h1:vOSWu6b57/emh+L/Cw0BeQfvxa/cogFywXHeGUxQxAg=
@@ -359,30 +306,16 @@ github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
-go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0 h1:8tvICD4vSTOOsNrsI4Ljf6C+6UKvpTEH5XY3JMoyPoo=
-go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0/go.mod h1:z9+yiacE0IHRqM4qFfkbt/JYlmYXgss8GY/jXoNuPJI=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0 h1:LMuyCAyfalSjDyjdC65nK6N0zoTT63+E/u95X0JovZI=
-go.opentelemetry.io/otel v1.45.0 h1:pdrWmLHofpubmArBv1LgFSv1Z0Ie/ppdZzu+kUN5EeU=
-go.opentelemetry.io/otel v1.45.0/go.mod h1:XZxIqPapzEYnhNSScF5DIqXhm/rYi0FzCe2XddAwZfQ=
+go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0/go.mod h1:085m8qbm4hgc8rZWGDEa4vmyyo2c3nPxUslYUKUIU04=
go.opentelemetry.io/otel v1.47.0 h1:j7ALJ/zgkS7Z6aeJW09p8VC9804bC+PpeTfCD4XPnOM=
go.opentelemetry.io/otel v1.47.0/go.mod h1:8wS9O2qfXrYrzp6hIF/HOYJJf/wIhFPhR2xLuP+iXQU=
go.opentelemetry.io/otel/log v1.47.0 h1:cOTS1CcLbSQeZKanGJ+0JpF/+t4PELi3O3bbl2lqCcI=
go.opentelemetry.io/otel/log v1.47.0/go.mod h1:9byitSQ5pLC6PpqwGXjqdMKya6ZTswHRZh2vvXT33nw=
-go.opentelemetry.io/otel/metric v1.45.0 h1:7Eg1uH7CJ5cXv9is6tnBe1FI6rj1nwUdbFypRm3br/M=
-go.opentelemetry.io/otel/metric v1.45.0/go.mod h1:HAPbm1nd3p1PmFH7v2dR+6BjXxw+Lq4a2+pndMAm08s=
go.opentelemetry.io/otel/metric v1.47.0 h1:4PptaldXx3Eat1XjMZ68pPJEs5wrhlemctZE9a3UdWY=
go.opentelemetry.io/otel/metric v1.47.0/go.mod h1:ADGSXxRrXM6bjbvLo535EstVFlPpPYZm4LBKixjDHwU=
-go.opentelemetry.io/otel/sdk v1.44.0 h1:nHYwb9lK+fJPU/dnT6s7W7Z8itMWyqrnVfbheVYrZ58=
-go.opentelemetry.io/otel/sdk v1.44.0/go.mod h1:Osuydd3Se74nqjAKxid74N5eC+jfEqfTegHRnq58oK0=
-go.opentelemetry.io/otel/sdk v1.46.0 h1:h5CNQQjEbuQXY/JfZtgt3i7HVFV3aHPO2OAwO2eTYPI=
-go.opentelemetry.io/otel/sdk/metric v1.44.0 h1:3LlKgI+VjbVsjNRFZJZAJ30WjXC5VkNRks6si09iEfI=
-go.opentelemetry.io/otel/sdk/metric v1.44.0/go.mod h1:5B5pMARnXxKhltooO4xUuCBorl65a4EpnTalObqOigA=
-go.opentelemetry.io/otel/trace v1.45.0 h1:l/mP6Uv7oNO7/TblbhpbgMidxhq1uO/rPsikOyVhxag=
-go.opentelemetry.io/otel/trace v1.45.0/go.mod h1:qoJJA2xNMnxRrdISU/kLtfUH2wNeQbiv+jhs/CxI8bc=
go.opentelemetry.io/otel/trace v1.47.0 h1:JOjX/Oci8K94QHddo+bbfya/Ai/nf6/dt9ZfrFNWSrM=
go.opentelemetry.io/otel/trace v1.47.0/go.mod h1:jNaSLa2PZEYFG6fRjJABAu+bw4FS08uDmPg28lTghu0=
-go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
-go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
@@ -400,20 +333,16 @@ go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
-golang.org/x/crypto v0.56.0 h1:GUh5Ii4J5jtcseSMiRqr1jXCNHoxjeV9Fmekc2oLy6Y=
-golang.org/x/crypto v0.56.0/go.mod h1:OMW5y6CY9l38uPLmxU6l6pwcXp1obtLo3e6gT7gQR2I=
-golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
-golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
-golang.org/x/exp v0.0.0-20260727155853-b88d891fe743 h1:ex206bKw+v3K0dm3andkrIF+ijyQKJG1pLgwQ2PYdQM=
-golang.org/x/exp v0.0.0-20260727155853-b88d891fe743/go.mod h1:EdfpwwqSu+0Li0mzskwHU6FWDV3t9Q+RZDo3QMUtL3Q=
+golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
+golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
golang.org/x/exp v0.0.0-20261005173118-76772065c9b0 h1:qUp3i6M39kccSm3OMMIE3tvs7YQWO9rFeCKdk1sSaFk=
golang.org/x/exp v0.0.0-20261005173118-76772065c9b0/go.mod h1:SRjTzy5WcYqRRqquLUvHeeovSwLO0CZ3xcltZNO4Fck=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
+golang.org/x/mobile v0.0.0-20261011034528-197770444867 h1:rav73cdd7OQUPgt1aIFbZQTiPNnjfueKXvfOiLCY0Ss=
+golang.org/x/mobile v0.0.0-20261011034528-197770444867/go.mod h1:HR/iBzuQmVTl/us733QPwrqHp43a8m9ZLgyKXD3ST6U=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
-golang.org/x/mod v0.40.0 h1:hUv+3cXcdRHz08UmSiOob7sadHig73uo5bkXxQ/tvUs=
-golang.org/x/mod v0.40.0/go.mod h1:0/weTWkPWGBikyTWAX3dkjVztMmBA5hM0DH6BElSupE=
-golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c=
-golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o=
+golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg=
+golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -429,17 +358,13 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
-golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To=
-golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU=
-golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
-golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
+golang.org/x/net v0.61.0 h1:RR+6j/BTBCrggOLugxa+66R8FvoN6MHlzDlZIbN9YFw=
+golang.org/x/net v0.61.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
-golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
-golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
-golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
+golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
+golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190411185658-b44545bcd369/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -463,48 +388,37 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
-golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
-golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
-golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
+golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
+golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
-golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
-golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
-golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
-golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
-golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
-golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
+golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
+golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
golang.org/x/time v0.16.0 h1:vMb6ptszcQMkcwiRTAuNNU50gom6++Q/6gY2hDM6VDE=
golang.org/x/time v0.16.0/go.mod h1:rVKOqvZeKvrDKTQiAHJ7wmwP0RzleSphoEA9RcdLA0s=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
-golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI=
-golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo=
-golang.org/x/tools v0.51.0 h1:k4Xc/1Om9jwkBJBo4NVLMSARBoWtK10mx+W5BnXCeAI=
+golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs=
+golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs=
+golang.org/x/tools/go/expect v0.1.1-deprecated h1:jpBZDwmgPhXsKZC6WhL20P4b/wmnpsEAGHaNy0n/rJM=
+golang.org/x/tools/go/expect v0.1.1-deprecated/go.mod h1:eihoPOH+FgIqa3FpoTwguz/bVUSGBlGQU67vpBeOrBY=
+golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated h1:1h2MnaIAIXISqTFKdENegdpAgUXz6NrPEsbIeWaBRvM=
+golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated/go.mod h1:RVAQXBGNv1ib0J382/DPCRS/BPnsGebyM1Gj5VSDpG8=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
-google.golang.org/genproto/googleapis/api v0.0.0-20260803160001-6ac0973c030d h1:FarXi840EJWSHYTN3ERkADbPWjl307+FGrA22KAVjjc=
-google.golang.org/genproto/googleapis/api v0.0.0-20260803160001-6ac0973c030d/go.mod h1:K/+WGbmBY7aNW1HDw1fJnKYo10i0DkAX6pows00dLig=
google.golang.org/genproto/googleapis/api v0.0.0-20261005182115-fad411399dd8 h1:cFbkNigDInw+wVTXc+CyhUsoHGJCw0BR6mlNoRVfre0=
google.golang.org/genproto/googleapis/api v0.0.0-20261005182115-fad411399dd8/go.mod h1:QK6p7IgD7IZEZJBhhSUCib5X2vMOzlrGCGUw2XyVDh0=
-google.golang.org/genproto/googleapis/rpc v0.0.0-20260803160001-6ac0973c030d h1:IL4hdHzcUv2l/gcg98/Rj3FbtE6axwqslOW8SW0C+S0=
-google.golang.org/genproto/googleapis/rpc v0.0.0-20260803160001-6ac0973c030d/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20261005182115-fad411399dd8 h1:0SXBqli5dnuCKMjI/X3FM226zEaEa0GUcSCJtN1vL2o=
google.golang.org/genproto/googleapis/rpc v0.0.0-20261005182115-fad411399dd8/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc=
-google.golang.org/grpc v1.83.2 h1:EManeRomTObA0BU7I8vXgg/78uE5MJ9M8B39EX2WscU=
-google.golang.org/grpc v1.83.2/go.mod h1:YPI1hK3kDked6iHvgX3tR0y+nX/qpMFKhPgFsokw1S8=
google.golang.org/grpc v1.84.0 h1:soMyaPJ8pAak5PIQ0DGBUir0XRo2fRoMqhNWMLlLxO0=
google.golang.org/grpc v1.84.0/go.mod h1:ljCht0DrxQrXBDRTZp52Qxh3Ffk8CdYm2sj4O2QN2C0=
-google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
-google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
diff --git a/internal/app/app.go b/internal/app/app.go
index 7d2ea4d3..e12944f6 100644
--- a/internal/app/app.go
+++ b/internal/app/app.go
@@ -19,6 +19,7 @@ import (
"syscall"
"time"
+ "github.com/TNTcraftHIM/Piik/internal/app/buildinfo"
appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
"github.com/TNTcraftHIM/Piik/internal/app/failure"
"github.com/TNTcraftHIM/Piik/internal/app/lan"
@@ -36,31 +37,16 @@ import (
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
"github.com/TNTcraftHIM/Piik/internal/server/webassets"
"github.com/TNTcraftHIM/Piik/internal/tlsconfig"
- "golang.org/x/mod/semver"
)
const (
- DefaultLocalPort = 8787
- publicSTUNURL = "stun:stun.cloudflare.com:3478"
- publicFallbackSTUNURLA = "stun:stun.miwifi.com:3478"
- publicFallbackSTUNURLB = "stun:stun.chat.bilibili.com:3478"
+ DefaultLocalPort = 8787
// localShutdownTimeout bounds the ordered Local stop. The packaged smoke
// allows 10 s between the stop request and the exit, so it stays well
// inside that budget.
localShutdownTimeout = 5 * time.Second
)
-var BuildVersion = "development"
-var BuildRevision = "development"
-
-func buildVersion() string {
- version := strings.TrimSpace(BuildVersion)
- if version != "" && semver.Canonical(version) == version && semver.Prerelease(version) == "" {
- return version
- }
- return "development"
-}
-
type Options struct {
Site string
SiteSet bool
@@ -101,7 +87,7 @@ func Run(ctx context.Context, options Options) (returnedErr error) {
if err != nil {
return errors.Join(failure.Diagnostics, fmt.Errorf("Piik App diagnostics are unavailable: %w", err))
}
- recorder.Context("build", map[string]any{"version": buildVersion(), "revision": BuildRevision})
+ recorder.Context("build", map[string]any{"version": buildinfo.Version(), "revision": buildinfo.Revision})
previous, previousWriter, previousFlags := slog.Default(), log.Writer(), log.Flags()
var dependencyLog *diagnostics.LineWriter
restoreLogger = func() {
@@ -124,7 +110,7 @@ func Run(ctx context.Context, options Options) (returnedErr error) {
return err
}
}
- slog.Debug("piik-client", "event", "start", "version", buildVersion(), "revision", BuildRevision)
+ slog.Debug("piik-client", "event", "start", "version", buildinfo.Version(), "revision", buildinfo.Revision)
options.console.show(consoleView{state: "starting"})
if err := validateMode(options); err != nil {
return errors.Join(failure.Configuration, err)
@@ -286,7 +272,8 @@ func runLauncher(
SaveStartupSite: func(site string) error {
return launcher.SaveStartupSite(configPath, site)
},
- Sites: config.ApprovedSites(), Version: buildVersion(), Revision: BuildRevision,
+ Sites: config.ApprovedSites(), Version: buildinfo.Version(), Revision: buildinfo.Revision,
+ ControlPort: control.Endpoint().Port,
LocalAccessPassword: config.LocalAccessPassword,
Debug: options.Debug, LANAddress: options.LANAddress,
ChangeSite: func(site string, allowed bool) ([]string, error) {
@@ -342,106 +329,100 @@ func runLauncher(
if !automatic {
site = startupSite
}
- openSite(site, options)
+ openSite(site, options, control.Endpoint().Port)
}
- // An interactive room is a child of the resident App. Its failure must not
- // retire an unrelated Site's native sessions or its approval page. Explicit
- // CLI launches still return their outcome to the caller. Call only after the
- // room has retired, so neither approval nor process exit depends on its work.
- finishRoom := func(roomErr error) error {
- if directRoom || roomErr == nil || ctx.Err() != nil {
- return roomErr
+ // The resident App outlives a failed interactive room. Retry only after the
+ // previous runtime has joined, leaving unrelated Site/native sessions intact.
+ runRoom := func(selection launcher.Selection) error {
+ options := initialOptions
+ if selection.Language != "" {
+ options.console.setLanguage(selection.Language)
}
- launch.SetResult("", roomErr)
- options.console.show(consoleView{state: "setup", entry: launch.URL(), problem: roomErr})
- slog.Debug("piik-client", "event", "local-room-stopped", diagnostics.Error(roomErr))
- select {
- case <-ctx.Done():
- case err := <-control.Done():
- roomErr = errors.Join(roomErr, err)
- case err := <-launch.Done():
- roomErr = errors.Join(roomErr, err)
+ if err := launcher.SaveMode(configPath, selection.Mode); err != nil {
+ slog.Debug("Could not save the launcher mode", diagnostics.Error(err))
}
- return roomErr
- }
-
- var selection launcher.Selection
- if directRoom {
- selection = launcher.Selection{Mode: launcher.ModeLocal, LocalAccessPassword: initialConfig.LocalAccessPassword}
- if options.Link {
- selection.Mode = launcher.ModeLink
- }
- launch.Begin()
- } else {
- select {
- case selection = <-launch.Selection():
- case <-ctx.Done():
- return nil
- case err = <-control.Done():
+ options.Local = selection.Mode == launcher.ModeLocal
+ options.Link = selection.Mode == launcher.ModeLink
+ options.Debug = options.Debug || selection.Debug
+ if selection.LANAddress != nil {
+ options.LANAddress = *selection.LANAddress
+ }
+ selectedConfig, err := updateConfig(func(next *appconfig.Config) {
+ next.LocalAccessPassword = selection.LocalAccessPassword
+ })
+ if err != nil {
+ err = errors.Join(failure.Configuration, fmt.Errorf("Piik App configuration could not be saved: %w", err))
return err
- case err = <-launch.Done():
- if err != nil {
- return errors.Join(failure.Launcher, fmt.Errorf("Piik App launcher stopped unexpectedly: %w", err))
- }
- return nil
}
- }
- if selection.Language != "" {
- options.console.setLanguage(selection.Language)
- }
- if err := launcher.SaveMode(configPath, selection.Mode); err != nil {
- slog.Debug("Could not save the launcher mode", diagnostics.Error(err))
- }
- options.Local = selection.Mode == launcher.ModeLocal
- options.Link = selection.Mode == launcher.ModeLink
- options.Debug = options.Debug || selection.Debug
- if selection.LANAddress != nil {
- options.LANAddress = *selection.LANAddress
- }
- selectedConfig, err := updateConfig(func(next *appconfig.Config) {
- next.LocalAccessPassword = selection.LocalAccessPassword
- })
- if err != nil {
- err = errors.Join(failure.Configuration, fmt.Errorf("Piik App configuration could not be saved: %w", err))
- return finishRoom(err)
- }
- if err = configureDiagnostics(&options, selectedConfig); err != nil {
- return finishRoom(err)
- }
- if !directRoom {
- options.DisableBrowser = true
- }
- ready := make(chan string, 1)
- onReady := options.Ready
- options.Ready = func(target string) {
- if onReady != nil {
- onReady(target)
+ if err = configureDiagnostics(&options, selectedConfig); err != nil {
+ return err
}
- ready <- target
- }
- runtimeContext, cancelRuntime := context.WithCancel(ctx)
- runtimeDone := make(chan struct{})
- var runtimeErr error
- go func() {
- runtimeErr = runLocal(runtimeContext, options, selectedConfig, control)
- close(runtimeDone)
- }()
- var exitErr error
- select {
- case target := <-ready:
- launch.SetResult(target, nil)
+ if !directRoom {
+ options.DisableBrowser = true
+ }
+ ready := make(chan string, 1)
+ onReady := options.Ready
+ options.Ready = func(target string) {
+ if onReady != nil {
+ onReady(target)
+ }
+ ready <- target
+ }
+ runtimeContext, cancelRuntime := context.WithCancel(ctx)
+ runtimeDone := make(chan struct{})
+ var runtimeErr error
+ go func() {
+ runtimeErr = runLocal(runtimeContext, options, selectedConfig, control)
+ close(runtimeDone)
+ }()
+ var exitErr error
select {
+ case target := <-ready:
+ launch.SetResult(target, nil)
+ select {
+ case <-runtimeDone:
+ case exitErr = <-launch.Done():
+ case <-ctx.Done():
+ }
case <-runtimeDone:
- case exitErr = <-launch.Done():
case <-ctx.Done():
+ launch.SetResult("", ctx.Err())
+ }
+ cancelRuntime()
+ <-runtimeDone
+ return errors.Join(exitErr, runtimeErr)
+ }
+ var roomErr error
+ for {
+ var selection launcher.Selection
+ if directRoom {
+ selection = launcher.Selection{Mode: launcher.ModeLocal, LocalAccessPassword: initialConfig.LocalAccessPassword}
+ if initialOptions.Link {
+ selection.Mode = launcher.ModeLink
+ }
+ launch.Begin()
+ } else {
+ select {
+ case selection = <-launch.Selection():
+ case <-ctx.Done():
+ return roomErr
+ case err := <-control.Done():
+ return errors.Join(roomErr, err)
+ case err := <-launch.Done():
+ if err != nil {
+ return errors.Join(roomErr, failure.Launcher, fmt.Errorf("Piik App launcher stopped unexpectedly: %w", err))
+ }
+ return roomErr
+ }
}
- case <-runtimeDone:
- case <-ctx.Done():
- launch.SetResult("", ctx.Err())
+ roomErr = runRoom(selection)
+ if directRoom || roomErr == nil || ctx.Err() != nil {
+ return roomErr
+ }
+ launch.SetResult("", roomErr)
+ initialOptions.console.show(consoleView{state: "setup", entry: launch.URL(), problem: roomErr})
+ slog.Debug("piik-client", "event", "local-room-stopped", diagnostics.Error(roomErr))
}
- cancelRuntime()
- <-runtimeDone
- return finishRoom(errors.Join(exitErr, runtimeErr))
}
func applyMode(config appconfig.Config, options Options) (appconfig.Config, error) {
@@ -480,13 +461,13 @@ func openDiagnostics(directory string) (*diagnostics.Recorder, error) {
directory = strings.TrimSpace(os.Getenv("PIIK_LOG_DIR"))
}
if directory != "" {
- return diagnostics.Open(directory, "client", BuildRevision)
+ return diagnostics.Open(directory, "client", buildinfo.Revision)
}
executable, err := os.Executable()
if err != nil {
return nil, err
}
- recorder, err := diagnostics.Open(filepath.Join(filepath.Dir(executable), "logs"), "client", BuildRevision)
+ recorder, err := diagnostics.Open(filepath.Join(filepath.Dir(executable), "logs"), "client", buildinfo.Revision)
if err == nil || !errors.Is(err, os.ErrPermission) && !errors.Is(err, syscall.EROFS) {
return recorder, err
}
@@ -494,12 +475,12 @@ func openDiagnostics(directory string) (*diagnostics.Recorder, error) {
if cacheErr != nil {
return nil, errors.Join(err, cacheErr)
}
- return diagnostics.Open(filepath.Join(cache, "Piik", "logs"), "client", BuildRevision)
+ return diagnostics.Open(filepath.Join(cache, "Piik", "logs"), "client", buildinfo.Revision)
}
-func openSite(site string, options Options) {
+func openSite(site string, options Options, controlPort int) {
slog.Debug("piik-client", "event", "mode", "mode", "site")
- targetURL := launchURL(site)
+ targetURL := launcher.EntryURL(site, controlPort)
view := consoleView{mode: "site", state: "ready", entry: targetURL}
options.console.show(view)
if options.console.machine {
@@ -571,6 +552,7 @@ func runLocal(ctx context.Context, options Options, config appconfig.Config,
ctx,
tunnelExecutable(options.TunnelProcess),
fmt.Sprintf("http://127.0.0.1:%d", options.Port),
+ "",
)
if err != nil {
// Only the caller's clean cancellation is a normal stopped outcome.
@@ -636,9 +618,10 @@ func runLocal(ctx context.Context, options Options, config appconfig.Config,
fmt.Printf("LAN certificate SHA-256: %s\n", view.certificate)
}
}
- launchURL := launchURLWithLocalAccess(
+ launchURL := launcher.LocalEntryURL(
fmt.Sprintf("http://127.0.0.1:%d/", options.Port),
config.LocalAccessPassword,
+ control.Endpoint().Port,
)
view.state, view.entry, view.invite = "ready", launchURL, publicOrigin
if view.invite == "" {
@@ -659,6 +642,8 @@ func runLocal(ctx context.Context, options Options, config appconfig.Config,
select {
case <-ctx.Done():
return nil
+ case err = <-localServer.Done():
+ return errors.Join(failure.LocalServer, err)
case err = <-control.Done():
if err != nil {
return errors.Join(failure.NativeControl, fmt.Errorf("Piik App native control stopped unexpectedly: %w", err))
@@ -668,42 +653,10 @@ func runLocal(ctx context.Context, options Options, config appconfig.Config,
if ctx.Err() != nil {
return nil
}
- return errors.Join(failure.TunnelStopped, errors.New("public invitation link stopped; reopen Piik to create a new link"))
+ return errors.Join(failure.TunnelStopped, errors.New("public invitation link stopped"))
}
}
-func launchURL(raw string) string {
- parsed, err := url.Parse(raw)
- if err != nil {
- return raw
- }
- fragment, err := url.ParseQuery(parsed.EscapedFragment())
- if err != nil {
- return raw
- }
- fragment.Set("piik-client", "1")
- parsed.Fragment, parsed.RawFragment = "", ""
- return parsed.String() + "#" + fragment.Encode()
-}
-
-func launchURLWithLocalAccess(raw, password string) string {
- parsed, err := url.Parse(raw)
- if err != nil {
- return raw
- }
- fragment, err := url.ParseQuery(parsed.EscapedFragment())
- if err != nil {
- return launchURL(raw)
- }
- fragment.Del("client-access")
- if password != "" {
- fragment.Set("client-access", password)
- }
- fragment.Set("piik-client", "1")
- parsed.Fragment, parsed.RawFragment = "", ""
- return parsed.String() + "#" + fragment.Encode()
-}
-
type nativeRuntime struct {
capabilities loopback.NativeMediaCapabilities
capture nativecapture.Capabilities
@@ -773,20 +726,7 @@ func localSTUNURLs(link bool) (stunURLs []string, natPredictionSTUNURLs []string
if !link {
return nil, nil, nil
}
- stunURLs, err = serverconfig.ParseSTUNURLs(os.Getenv("PIIK_STUN_URLS"), "PIIK_STUN_URLS")
- if err != nil {
- return nil, nil, err
- }
- if len(stunURLs) == 0 {
- stunURLs = []string{publicSTUNURL, publicFallbackSTUNURLA, publicFallbackSTUNURLB}
- }
- if len(stunURLs) >= 3 {
- if len(stunURLs) > protocol.MaxIceServerURLs-protocol.MaxNatPredictionAuxiliaryStunURLs {
- return nil, nil, errors.New("PIIK_STUN_URLS must contain at most 6 URLs")
- }
- natPredictionSTUNURLs = stunURLs[1:3]
- }
- return stunURLs, natPredictionSTUNURLs, nil
+ return appconfig.PublicSTUNURLs(os.Getenv("PIIK_STUN_URLS"))
}
// endLocalServer is step 1 of the ADR-0010 item 12 unwind: every room ends,
diff --git a/internal/app/app_test.go b/internal/app/app_test.go
index 0e474765..3f17301e 100644
--- a/internal/app/app_test.go
+++ b/internal/app/app_test.go
@@ -16,12 +16,14 @@ import (
"path/filepath"
"reflect"
"slices"
+ "strconv"
"strings"
"sync"
"testing"
"time"
tea "charm.land/bubbletea/v2"
+ "github.com/TNTcraftHIM/Piik/internal/app/buildinfo"
appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
"github.com/TNTcraftHIM/Piik/internal/app/lan"
"github.com/TNTcraftHIM/Piik/internal/app/launcher"
@@ -309,6 +311,94 @@ func TestLauncherRetainsItsRuntimeFailureDuringCancellation(t *testing.T) {
}
}
+func TestLauncherRetriesARetiredFailureWithoutReplacingAReadyRoom(t *testing.T) {
+ t.Setenv("PIIK_APP_TUNNEL_FIXTURE", "ready")
+ t.Setenv("PIIK_APP_TUNNEL_STARTED", filepath.Join(t.TempDir(), "started"))
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ occupied, err := net.Listen("tcp4", "0.0.0.0:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer occupied.Close()
+ port := occupied.Addr().(*net.TCPAddr).Port
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ control, err := loopback.Start(ctx, loopback.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer control.Close()
+ configPath := filepath.Join(t.TempDir(), "client.json")
+ config, err := appconfig.LoadOrCreate(configPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ config.SetApprovedSites([]string{"https://site.example"})
+ opened := make(chan string, 1)
+ console := newConsole(cancel, true)
+ console.openURL = func(target string) error { opened <- target; return nil }
+ done := make(chan error, 1)
+ go func() {
+ done <- runLauncher(ctx, Options{Port: port, console: console, TunnelProcess: executable},
+ configPath, config, control, func(*Options, appconfig.Config) error { return nil })
+ }()
+ t.Cleanup(func() {
+ cancel()
+ select {
+ case err := <-done:
+ if err != nil {
+ t.Errorf("retried room exit retained an obsolete failure: %v", err)
+ }
+ case <-time.After(5 * time.Second):
+ t.Error("retried launcher did not retire")
+ }
+ })
+ var target string
+ select {
+ case target = <-opened:
+ case <-time.After(2 * time.Second):
+ t.Fatal("launcher did not open")
+ }
+ client := &http.Client{Timeout: 5 * time.Second}
+ endpoint := strings.TrimSuffix(target, "/client") + "/api/client-launcher/launch"
+ post := func() *http.Response {
+ t.Helper()
+ response, err := postLocalLauncher(client, endpoint, "application/json", strings.NewReader(`{"mode":"link","language":"en"}`))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { response.Body.Close() })
+ return response
+ }
+ if response := post(); response.StatusCode != http.StatusServiceUnavailable {
+ t.Fatalf("occupied port startup = %d", response.StatusCode)
+ }
+ if err := occupied.Close(); err != nil {
+ t.Fatal(err)
+ }
+ response := post()
+ var ready struct{ Target string }
+ if err := json.NewDecoder(response.Body).Decode(&ready); err != nil || response.StatusCode != http.StatusOK || ready.Target == "" {
+ t.Fatalf("retry did not become ready: %d %+v %v", response.StatusCode, ready, err)
+ }
+ response = post()
+ var reopened struct{ Target string }
+ if err := json.NewDecoder(response.Body).Decode(&reopened); err != nil || response.StatusCode != http.StatusOK || reopened.Target != ready.Target {
+ t.Fatalf("reentry replaced the ready room: %d %+v %v", response.StatusCode, reopened, err)
+ }
+ response, err = client.Get(fmt.Sprintf("http://127.0.0.1:%d/healthz", port))
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ if response.StatusCode != http.StatusOK {
+ t.Fatalf("launcher reopening retired the ready room = %d", response.StatusCode)
+ }
+}
+
func TestHungBrowserHandoffDoesNotBlockLauncherOrSiteExit(t *testing.T) {
t.Setenv("TERM", "dumb")
for _, mode := range []string{"launcher", "site", "starred-site", "starred-second-site", "revoked-site"} {
@@ -366,13 +456,19 @@ func TestHungBrowserHandoffDoesNotBlockLauncherOrSiteExit(t *testing.T) {
t.Fatal("manual entry was not available while browser opening hung")
}
if mode == "site" || mode == "starred-site" || mode == "starred-second-site" {
- if model.view.entry != launchURL("https://share.example") {
+ if model.view.entry != launcher.EntryURL("https://share.example", control.Endpoint().Port) {
t.Fatalf("App did not open the selected Site: %s", model.view.entry)
}
- response, err := http.Get(model.launcher)
+ response, err := http.Get(strings.TrimSuffix(model.launcher, "/client") + "/api/client-launcher")
if err != nil {
t.Fatalf("launcher management is unavailable: %v", err)
}
+ var state struct {
+ ControlPort int `json:"controlPort"`
+ }
+ if err := json.NewDecoder(response.Body).Decode(&state); err != nil || state.ControlPort != control.Endpoint().Port {
+ t.Fatalf("launcher selected another App runtime: %+v, %v", state, err)
+ }
response.Body.Close()
if response.StatusCode != http.StatusOK {
t.Fatal("Site entry lost access to the launcher")
@@ -572,10 +668,10 @@ func TestLocalLaunchRevalidatesTheChosenLANAddress(t *testing.T) {
}
func TestAppLaunchURLMarksThePageWithoutChangingOrigin(t *testing.T) {
- value := launchURL("https://share.example/")
+ value := launcher.EntryURL("https://share.example/", loopback.DefaultPortStart)
parsed, err := url.Parse(value)
if err != nil || parsed.Scheme != "https" || parsed.Host != "share.example" ||
- parsed.RawQuery != "" {
+ parsed.Query().Get("piik-client-port") != strconv.Itoa(loopback.DefaultPortStart) {
t.Fatalf("launch URL = %q, %v", value, err)
}
fragment, err := url.ParseQuery(parsed.EscapedFragment())
@@ -621,7 +717,7 @@ func TestAppDiagnosticsUseFilesOnlyWhenEnabled(t *testing.T) {
continue
}
if readErr != nil || !strings.Contains(string(content), `"event":"start"`) ||
- !strings.Contains(string(content), `"version":"`+buildVersion()+`"`) ||
+ !strings.Contains(string(content), `"version":"`+buildinfo.Version()+`"`) ||
!strings.Contains(string(content), `"revision":`) ||
!strings.Contains(string(content), `"event":"stopped","failed":true`) {
t.Fatalf("missing file lifecycle events: %s, %v", content, readErr)
@@ -699,9 +795,9 @@ func TestFailedCaptureProbeRetainsItsCause(t *testing.T) {
}
func TestLaunchURLPreservesLocalAccessInsideThePrivateFragment(t *testing.T) {
- value := launchURL("http://localhost:8787/#client-access=secret")
+ value := launcher.EntryURL("http://localhost:8787/#client-access=secret", loopback.DefaultPortStart)
parsed, err := url.Parse(value)
- if err != nil || parsed.RawQuery != "" {
+ if err != nil || parsed.Query().Get("piik-client-port") != strconv.Itoa(loopback.DefaultPortStart) {
t.Fatalf("local native launch URL = %q, %v", value, err)
}
fragment, err := url.ParseQuery(parsed.EscapedFragment())
@@ -714,13 +810,15 @@ func TestLaunchURLPreservesLocalAccessInsideThePrivateFragment(t *testing.T) {
func TestLaunchURLEncodesAndClearsOptionalLocalAccess(t *testing.T) {
for _, password := range []string{"x", "好", " ", "+", "%", "#&?=", " 中文 a+b&c?d=e% "} {
t.Run(password, func(t *testing.T) {
- value := launchURLWithLocalAccess(
+ value := launcher.LocalEntryURL(
"http://localhost:8787/?retained=query#retained=a%2Bb%25&client-access=old&piik-lang=zh&piik-mode=text&piik-theme=dark",
password,
+ loopback.DefaultPortStart,
)
- for _, target := range []string{value, launchURL(value)} {
+ for _, target := range []string{value, launcher.EntryURL(value, loopback.DefaultPortStart)} {
parsed, err := url.Parse(target)
- if err != nil || parsed.RawQuery != "retained=query" {
+ if err != nil || parsed.Query().Get("retained") != "query" ||
+ parsed.Query().Get("piik-client-port") != strconv.Itoa(loopback.DefaultPortStart) {
t.Fatalf("local launch URL = %q, %v", target, err)
}
// location.hash stays escaped. Parsing Fragment instead would decode
@@ -736,7 +834,7 @@ func TestLaunchURLEncodesAndClearsOptionalLocalAccess(t *testing.T) {
})
}
- open := launchURLWithLocalAccess("http://localhost:8787/#client-access=old", "")
+ open := launcher.LocalEntryURL("http://localhost:8787/#client-access=old", "", loopback.DefaultPortStart)
parsed, err := url.Parse(open)
if err != nil {
t.Fatal(err)
@@ -782,9 +880,9 @@ func TestLocalServerOwnsItsSTUNConfiguration(t *testing.T) {
{name: "local ignores public override", override: "invalid"},
{
name: "link", link: true,
- stunURLs: []string{publicSTUNURL, publicFallbackSTUNURLA, publicFallbackSTUNURLB},
+ stunURLs: []string{"stun:stun.cloudflare.com:3478", "stun:stun.miwifi.com:3478", "stun:stun.chat.bilibili.com:3478"},
natPredictionSTUNURLs: []string{
- publicFallbackSTUNURLA, publicFallbackSTUNURLB,
+ "stun:stun.miwifi.com:3478", "stun:stun.chat.bilibili.com:3478",
},
},
{
diff --git a/internal/app/buildinfo/buildinfo.go b/internal/app/buildinfo/buildinfo.go
new file mode 100644
index 00000000..2ca2c346
--- /dev/null
+++ b/internal/app/buildinfo/buildinfo.go
@@ -0,0 +1,20 @@
+// Package buildinfo owns the build identity shared by App entry points.
+package buildinfo
+
+import (
+ "strings"
+
+ "golang.org/x/mod/semver"
+)
+
+// Packagers inject these values into desktop and mobile builds.
+var Release = "development"
+var Revision = "development"
+
+func Version() string {
+ version := strings.TrimSpace(Release)
+ if version != "" && semver.Canonical(version) == version && semver.Prerelease(version) == "" {
+ return version
+ }
+ return "development"
+}
diff --git a/internal/app/config/public_stun.go b/internal/app/config/public_stun.go
new file mode 100644
index 00000000..770cb24d
--- /dev/null
+++ b/internal/app/config/public_stun.go
@@ -0,0 +1,27 @@
+package config
+
+import (
+ "errors"
+
+ serverconfig "github.com/TNTcraftHIM/Piik/internal/server/config"
+ "github.com/TNTcraftHIM/Piik/internal/server/protocol"
+)
+
+// PublicSTUNURLs supplies the shared App public-link discovery policy. An explicit
+// override replaces every destination; fewer than three disable the survey pair.
+func PublicSTUNURLs(override string) (stunURLs []string, prediction []string, err error) {
+ stunURLs, err = serverconfig.ParseSTUNURLs(override, "PIIK_STUN_URLS")
+ if err != nil {
+ return nil, nil, err
+ }
+ if len(stunURLs) == 0 {
+ stunURLs = []string{"stun:stun.cloudflare.com:3478", "stun:stun.miwifi.com:3478", "stun:stun.chat.bilibili.com:3478"}
+ }
+ if len(stunURLs) >= 3 {
+ if len(stunURLs) > protocol.MaxIceServerURLs-protocol.MaxNatPredictionAuxiliaryStunURLs {
+ return nil, nil, errors.New("PIIK_STUN_URLS must contain at most 6 URLs")
+ }
+ prediction = stunURLs[1:3]
+ }
+ return stunURLs, prediction, nil
+}
diff --git a/internal/app/console.go b/internal/app/console.go
index beb1b857..cf8f5f79 100644
--- a/internal/app/console.go
+++ b/internal/app/console.go
@@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path/filepath"
+ "strconv"
"strings"
"sync"
"time"
@@ -16,7 +17,9 @@ import (
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/TNTcraftHIM/Piik/internal/app/browser"
+ "github.com/TNTcraftHIM/Piik/internal/app/buildinfo"
"github.com/TNTcraftHIM/Piik/internal/app/failure"
+ "github.com/TNTcraftHIM/Piik/internal/app/launcher"
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
"github.com/charmbracelet/colorprofile"
"github.com/charmbracelet/x/ansi"
@@ -365,12 +368,12 @@ func (model consoleModel) content(styled bool) string {
stateStyle = stateStyle.Foreground(lipgloss.Color("9"))
}
}
- revision := BuildRevision
+ revision := buildinfo.Revision
if len(revision) == 40 {
revision = revision[:7]
}
var out strings.Builder
- version := buildVersion()
+ version := buildinfo.Version()
heading := accent.Render("Piik App") + "\n" + muted.Render(version)
if revision != "" && revision != version {
heading += "\n" + muted.Render(revision)
@@ -413,7 +416,11 @@ func (model consoleModel) content(styled bool) string {
}
address := consoleAddress(item[1])
if item[0] == "entry" {
- address = launchURL(address)
+ port := 0
+ if source, err := url.Parse(item[1]); err == nil {
+ port, _ = strconv.Atoi(source.Query().Get("piik-client-port"))
+ }
+ address = launcher.EntryURL(address, port)
}
style := link
if styled {
diff --git a/internal/app/console_test.go b/internal/app/console_test.go
index b7fc8263..0bddf413 100644
--- a/internal/app/console_test.go
+++ b/internal/app/console_test.go
@@ -15,6 +15,7 @@ import (
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
+ "github.com/TNTcraftHIM/Piik/internal/app/buildinfo"
"github.com/TNTcraftHIM/Piik/internal/app/failure"
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
"github.com/charmbracelet/x/ansi"
@@ -224,10 +225,10 @@ func TestBrowserOpenFeedbackBelongsToTheCurrentEntry(t *testing.T) {
func TestConsoleEntryKeepsAppActivationWithoutExposingCredentials(t *testing.T) {
model := consoleModel{width: 80, language: "en", view: consoleView{
mode: "link", state: "ready",
- entry: "http://user:private-secret@localhost:8787/?token=private-secret#client-access=private-secret&piik-client=1",
+ entry: "http://user:private-secret@localhost:8787/?token=private-secret&piik-client-port=39730#client-access=private-secret&piik-client=1",
invite: "https://room.example/?token=private-secret#v=private-secret",
}}
- want := []string{"http://localhost:8787/#piik-client=1", "https://room.example/"}
+ want := []string{"http://localhost:8787/?piik-client-port=39730#piik-client=1", "https://room.example/"}
for _, styled := range []bool{false, true} {
content := model.content(styled)
var addresses []string
@@ -373,9 +374,9 @@ func TestConsolePresentationAndShutdown(t *testing.T) {
}
func TestConsoleBuildVersionAndRevision(t *testing.T) {
- previousVersion, previousRevision := BuildVersion, BuildRevision
- t.Cleanup(func() { BuildVersion, BuildRevision = previousVersion, previousRevision })
- BuildRevision = strings.Repeat("a", 40)
+ previousVersion, previousRevision := buildinfo.Release, buildinfo.Revision
+ t.Cleanup(func() { buildinfo.Release, buildinfo.Revision = previousVersion, previousRevision })
+ buildinfo.Revision = strings.Repeat("a", 40)
for _, test := range []struct{ version, want string }{
{"v1.2.3", "v1.2.3"},
{"development", "development"},
@@ -385,9 +386,9 @@ func TestConsoleBuildVersionAndRevision(t *testing.T) {
{"v1.2.3-preview.1", "development"},
{"v1.2.3+build", "development"},
} {
- BuildVersion = test.version
+ buildinfo.Release = test.version
content := (consoleModel{width: 80, language: "en", view: consoleView{state: "ready"}}).content(false)
- if !strings.Contains(content, test.want) || !strings.Contains(content, "aaaaaaa") || strings.Contains(content, BuildRevision) {
+ if !strings.Contains(content, test.want) || !strings.Contains(content, "aaaaaaa") || strings.Contains(content, buildinfo.Revision) {
t.Fatalf("console identity for %q: %s", test.version, content)
}
}
diff --git a/internal/app/lan/addresses.go b/internal/app/lan/addresses.go
index 7a1e3c0b..ebce6959 100644
--- a/internal/app/lan/addresses.go
+++ b/internal/app/lan/addresses.go
@@ -6,6 +6,8 @@ import (
"net"
"sort"
"strings"
+
+ "github.com/wlynxg/anet"
)
type Address struct {
@@ -16,7 +18,7 @@ type Address struct {
// List describes invitation addresses. It does not choose or constrain the
// media interfaces, which remain owned by ICE and the operating system.
func List() ([]Address, error) {
- interfaces, err := net.Interfaces()
+ interfaces, err := anet.Interfaces()
if err != nil {
return nil, fmt.Errorf("list network interfaces: %w", err)
}
@@ -25,7 +27,7 @@ func List() ([]Address, error) {
if networkInterface.Flags&net.FlagUp == 0 || networkInterface.Flags&net.FlagLoopback != 0 {
continue
}
- addresses, addressErr := networkInterface.Addrs()
+ addresses, addressErr := anet.InterfaceAddrsByInterface(&networkInterface)
if addressErr != nil {
continue
}
diff --git a/internal/app/launcher/entry.go b/internal/app/launcher/entry.go
new file mode 100644
index 00000000..19e8df6e
--- /dev/null
+++ b/internal/app/launcher/entry.go
@@ -0,0 +1,46 @@
+package launcher
+
+import (
+ "net/url"
+ "strconv"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/loopback"
+)
+
+// EntryURL selects the App runtime without carrying authority in the query.
+func EntryURL(raw string, controlPort int) string {
+ parsed, err := url.Parse(raw)
+ if err != nil {
+ return raw
+ }
+ fragment, err := url.ParseQuery(parsed.EscapedFragment())
+ if err != nil {
+ return raw
+ }
+ fragment.Set("piik-client", "1")
+ query := parsed.Query()
+ if controlPort >= loopback.DefaultPortStart && controlPort <= loopback.DefaultPortEnd {
+ query.Set("piik-client-port", strconv.Itoa(controlPort))
+ }
+ parsed.RawQuery = query.Encode()
+ parsed.Fragment, parsed.RawFragment = "", ""
+ return parsed.String() + "#" + fragment.Encode()
+}
+
+// LocalEntryURL passes the local access choice only through the consumed Host fragment.
+func LocalEntryURL(raw, password string, controlPort int) string {
+ parsed, err := url.Parse(raw)
+ if err != nil {
+ return raw
+ }
+ fragment, err := url.ParseQuery(parsed.EscapedFragment())
+ if err != nil {
+ return raw
+ }
+ fragment.Del("client-access")
+ if password != "" {
+ fragment.Set("client-access", password)
+ }
+ parsed.Fragment, parsed.RawFragment = "", ""
+ return EntryURL(parsed.String()+"#"+fragment.Encode(), controlPort)
+}
diff --git a/internal/app/launcher/preference_test.go b/internal/app/launcher/preference_test.go
index f9ef0bc9..d0b340bf 100644
--- a/internal/app/launcher/preference_test.go
+++ b/internal/app/launcher/preference_test.go
@@ -85,7 +85,7 @@ func TestStarredSiteSurvivesOtherApprovalsAndClearsOnRevocation(t *testing.T) {
t.Fatal(err)
}
sites := []string{"https://share.example"}
- server, err := Start(t.Context(), appAssets(), Options{Sites: sites,
+ server, err := Start(t.Context(), appAssets(), Options{Sites: sites, ControlPort: 39730,
SaveStartupSite: func(site string) error { return SaveStartupSite(path, site) },
ChangeSite: func(site string, allowed bool) ([]string, error) {
sites = editSites(sites, site, allowed)
@@ -124,8 +124,15 @@ func TestStarredSiteSurvivesOtherApprovalsAndClearsOnRevocation(t *testing.T) {
if saved, err := LoadStartupSite(path, state.Sites...); err != nil || saved != test.want {
t.Fatalf("saved startup: %q %v", saved, err)
}
+ entry := server.URL()
+ if test.want != "" {
+ entry = test.want + "/?piik-client-port=39730#piik-client=1"
+ }
+ if server.EntryURL() != entry {
+ t.Fatalf("startup entry lost its App endpoint: %q, want %q", server.EntryURL(), entry)
+ }
}
- if server.selected || config.Site != "" {
+ if server.attempt != nil || config.Site != "" {
t.Fatal("startup preference created room or access")
}
}
diff --git a/internal/app/launcher/server.go b/internal/app/launcher/server.go
index 0ff3a8b7..a73dcc55 100644
--- a/internal/app/launcher/server.go
+++ b/internal/app/launcher/server.go
@@ -22,6 +22,7 @@ import (
"github.com/TNTcraftHIM/Piik/internal/app/failure"
"github.com/TNTcraftHIM/Piik/internal/app/lan"
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
+ "github.com/TNTcraftHIM/Piik/internal/httpassets"
)
const (
@@ -48,11 +49,13 @@ type Selection struct {
}
type Options struct {
+ Modes []Mode
DefaultMode Mode
Sites []string
StartupSite string
Version string
Revision string
+ ControlPort int
LocalAccessPassword string
Debug bool
LANAddress string
@@ -70,29 +73,34 @@ type Server struct {
httpServer *http.Server
assets fs.FS
static http.Handler
+ client http.Handler
sites []string
startupSite string
+ modes []Mode
defaultMode Mode
localAccessPassword string
version string
revision string
+ controlPort int
debug bool
lanAddress string
changeSite func(string, bool) ([]string, error)
saveStartupSite func(string) error
enableDebug func() error
selection chan Selection
- resultReady chan struct{}
done chan error
ready chan struct{}
- mu sync.Mutex
- selected bool
- resultSet bool
- target string
- resultErr error
- closeOnce sync.Once
- resultOnce sync.Once
+ mu sync.Mutex
+ attempt *launchAttempt
+ closeOnce sync.Once
+}
+
+type launchAttempt struct {
+ ready chan struct{}
+ completed bool
+ target string
+ err error
}
// Start serves the launcher page and its API on a loopback port. assets is the
@@ -122,23 +130,25 @@ func Start(parent context.Context, assets fs.FS, options Options) (*Server, erro
cancel: cancel,
listener: listener,
assets: assets,
- static: http.FileServer(http.FS(assets)),
+ static: httpassets.Compress(http.FileServer(http.FS(assets))),
sites: normalizedSites,
startupSite: options.StartupSite,
defaultMode: options.DefaultMode,
+ modes: slices.Clone(options.Modes),
localAccessPassword: options.LocalAccessPassword,
version: options.Version,
revision: strings.TrimSpace(options.Revision),
+ controlPort: options.ControlPort,
debug: options.Debug,
lanAddress: options.LANAddress,
changeSite: options.ChangeSite,
saveStartupSite: options.SaveStartupSite,
enableDebug: options.EnableDebug,
selection: make(chan Selection, 1),
- resultReady: make(chan struct{}),
done: make(chan error, 1),
ready: make(chan struct{}),
}
+ server.client = httpassets.Compress(http.HandlerFunc(server.serveClient))
server.httpServer = &http.Server{
Handler: server,
ReadHeaderTimeout: 5 * time.Second,
@@ -157,6 +167,18 @@ func (server *Server) URL() string {
return "http://" + server.listener.Addr().String() + "/client"
}
+// EntryURL follows the current approved startup preference; URL always opens
+// the launcher itself, including when returning from an existing room.
+func (server *Server) EntryURL() string {
+ server.mu.Lock()
+ startup := server.startupSite
+ server.mu.Unlock()
+ if startup != "" {
+ return EntryURL(startup+"/", server.controlPort)
+ }
+ return server.URL()
+}
+
func (server *Server) Selection() <-chan Selection {
return server.selection
}
@@ -166,26 +188,34 @@ func (server *Server) Done() <-chan error {
}
func (server *Server) Begin() bool {
+ _, fresh := server.begin()
+ return fresh
+}
+
+func (server *Server) begin() (*launchAttempt, bool) {
server.mu.Lock()
defer server.mu.Unlock()
- if server.selected {
- return false
+ if server.attempt != nil && server.attempt.err == nil {
+ return server.attempt, false
}
- server.selected = true
- return true
+ server.attempt = &launchAttempt{ready: make(chan struct{})}
+ return server.attempt, true
}
+// SetResult is called by the serial room owner after readiness or full retirement.
func (server *Server) SetResult(target string, resultErr error) {
if resultErr == nil && !validTarget(target) {
resultErr = errors.New("App target is invalid")
}
server.mu.Lock()
- server.resultSet = true
- server.target = target
- server.resultErr = resultErr
- server.mu.Unlock()
- // Readiness completes once; a later room failure invalidates the old target.
- server.resultOnce.Do(func() { close(server.resultReady) })
+ defer server.mu.Unlock()
+ attempt := server.attempt
+ attempt.target = target
+ attempt.err = resultErr
+ if !attempt.completed {
+ attempt.completed = true
+ close(attempt.ready)
+ }
}
func (server *Server) Close() error {
@@ -226,18 +256,22 @@ func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Requ
case "/api/client-launcher/debug":
server.handleDebug(response, request)
case "/client", "/client/":
- if request.Method != http.MethodGet && request.Method != http.MethodHead {
- response.Header().Set("Allow", "GET, HEAD")
- http.Error(response, "method not allowed", http.StatusMethodNotAllowed)
- return
- }
- response.Header().Set("Cache-Control", "no-store")
- http.ServeFileFS(response, request, server.assets, launcherIndex)
+ server.client.ServeHTTP(response, request)
default:
server.static.ServeHTTP(response, request)
}
}
+func (server *Server) serveClient(response http.ResponseWriter, request *http.Request) {
+ if request.Method != http.MethodGet && request.Method != http.MethodHead {
+ response.Header().Set("Allow", "GET, HEAD")
+ http.Error(response, "method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+ response.Header().Set("Cache-Control", "no-store")
+ http.ServeFileFS(response, request, server.assets, launcherIndex)
+}
+
func (server *Server) handleState(response http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodGet {
response.Header().Set("Allow", http.MethodGet)
@@ -257,19 +291,36 @@ func (server *Server) handleState(response http.ResponseWriter, request *http.Re
}
selected, _ := lan.Select(values, server.lanAddress)
server.mu.Lock()
+ var target string
+ var resultErr error
+ starting := false
+ if attempt := server.attempt; attempt != nil {
+ target, resultErr = attempt.target, attempt.err
+ starting = !attempt.completed
+ }
+ packageTarget := runtime.GOOS + "-" + runtime.GOARCH
+ if runtime.GOOS == "android" {
+ packageTarget = "android-universal"
+ }
state := map[string]any{
"sites": slices.Clone(server.sites),
"startupSite": server.startupSite,
"localAccessPassword": server.localAccessPassword,
"version": server.version,
"revision": server.revision,
- "packageTarget": runtime.GOOS + "-" + runtime.GOARCH,
- "debug": server.debug,
- "target": server.target,
- "starting": server.selected && !server.resultSet,
- "detail": errorDetail(server.resultErr),
- "reason": failure.Of(server.resultErr),
+ "packageTarget": packageTarget,
+ "controlPort": server.controlPort,
+ "target": target,
+ "starting": starting,
+ "detail": errorDetail(resultErr),
+ "reason": failure.Of(resultErr),
"lan": map[string]any{"addresses": addresses, "selected": selected},
+ "modes": func() []Mode {
+ if len(server.modes) > 0 {
+ return slices.Clone(server.modes)
+ }
+ return []Mode{ModeLocal, ModeLink}
+ }(),
"defaultMode": func() Mode {
if server.defaultMode == ModeLocal || server.defaultMode == ModeLink {
return server.defaultMode
@@ -277,6 +328,9 @@ func (server *Server) handleState(response http.ResponseWriter, request *http.Re
return ModeLink
}(),
}
+ if server.enableDebug != nil || server.debug {
+ state["debug"] = server.debug
+ }
server.mu.Unlock()
writeJSON(response, http.StatusOK, state)
}
@@ -354,7 +408,7 @@ func (server *Server) handleDebug(response http.ResponseWriter, request *http.Re
}
server.mu.Lock()
if !server.debug {
- if server.selected || server.enableDebug == nil {
+ if (server.attempt != nil && server.attempt.err == nil) || server.enableDebug == nil {
server.mu.Unlock()
http.Error(response, "enable diagnostics before starting a local room", http.StatusConflict)
return
@@ -384,27 +438,29 @@ func (server *Server) handleLaunch(response http.ResponseWriter, request *http.R
})
return
}
- if !server.Begin() {
- writeJSON(response, http.StatusConflict, map[string]string{
- "error": "launch already requested", "detail": "Close and reopen Piik before starting again.",
- })
+ if len(server.modes) > 0 && !slices.Contains(server.modes, selection.Mode) {
+ http.Error(response, "unsupported launch mode", http.StatusBadRequest)
return
}
-
- // Begin commits this one selection to the App, independently of the page.
- // The single-slot queue cannot block: only the accepted request writes it.
- server.selection <- selection
+ attempt, fresh := server.begin()
+ if fresh {
+ // Each accepted attempt outlives its page request and owns its own result.
+ // The single-slot queue cannot block: only the accepted request writes it.
+ server.selection <- selection
+ }
+ // Reentry joins this exact attempt, including an accepted startup whose
+ // original page has gone away. It cannot start another room or join a retry.
select {
- case <-server.resultReady:
+ case <-attempt.ready:
case <-request.Context().Done():
return
case <-server.ctx.Done():
return
}
server.mu.Lock()
- target, resultErr, resultSet := server.target, server.resultErr, server.resultSet
+ target, resultErr := attempt.target, attempt.err
server.mu.Unlock()
- if !resultSet || resultErr != nil {
+ if resultErr != nil {
writeJSON(response, http.StatusServiceUnavailable, map[string]string{
"error": "Piik App could not start", "detail": errorDetail(resultErr), "reason": string(failure.Of(resultErr)),
})
diff --git a/internal/app/launcher/server_test.go b/internal/app/launcher/server_test.go
index b30f20e9..d9d1d2f7 100644
--- a/internal/app/launcher/server_test.go
+++ b/internal/app/launcher/server_test.go
@@ -2,6 +2,7 @@ package launcher
import (
"bytes"
+ "compress/gzip"
"context"
"encoding/json"
"errors"
@@ -27,6 +28,50 @@ func appAssets() fs.FS {
}
}
+func TestLauncherCompressesEntryAndAssetsWithoutChangingAPI(t *testing.T) {
+ body := []byte(strings.Repeat("Piik launcher asset ", 400))
+ assets := fstest.MapFS{
+ "index.html": {Data: body},
+ "assets/app.js": {Data: body},
+ }
+ server, err := Start(t.Context(), assets, Options{Version: "development"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = server.Close() })
+ origin := strings.TrimSuffix(server.URL(), "/client")
+ for _, path := range []string{"/client", "/client/", "/assets/app.js"} {
+ t.Run(path, func(t *testing.T) {
+ request := httptest.NewRequest(http.MethodGet, origin+path, nil)
+ request.Header.Set("Accept-Encoding", "gzip")
+ response := httptest.NewRecorder()
+ server.ServeHTTP(response, request)
+ if response.Code != http.StatusOK || response.Header().Get("Content-Encoding") != "gzip" {
+ t.Fatalf("static response: %d, %v", response.Code, response.Header())
+ }
+ reader, err := gzip.NewReader(response.Body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer reader.Close()
+ decoded, err := io.ReadAll(reader)
+ if err != nil || !bytes.Equal(decoded, body) {
+ t.Fatalf("compressed asset changed: %v", err)
+ }
+ if path != "/assets/app.js" && response.Header().Get("Cache-Control") != "no-store" {
+ t.Fatal("launcher entry lost its cache policy")
+ }
+ })
+ }
+ request := httptest.NewRequest(http.MethodGet, origin+"/api/client-launcher", nil)
+ request.Header.Set("Accept-Encoding", "gzip")
+ response := httptest.NewRecorder()
+ server.ServeHTTP(response, request)
+ if response.Code != http.StatusOK || response.Header().Get("Content-Encoding") != "" || !json.Valid(response.Body.Bytes()) {
+ t.Fatalf("launcher API changed: %d, %v", response.Code, response.Header())
+ }
+}
+
func TestLauncherServesStateAndCompletesOneSelection(t *testing.T) {
server := startFixture(t, "https://share.example")
origin := strings.TrimSuffix(server.URL(), "/client")
@@ -91,6 +136,80 @@ func TestLauncherServesStateAndCompletesOneSelection(t *testing.T) {
}
}
+func TestLauncherReentryReturnsTheExistingRoom(t *testing.T) {
+ server := startFixture(t, "https://share.example")
+ origin := strings.TrimSuffix(server.URL(), "/client")
+ post := func(ctx context.Context) (*http.Response, error) {
+ request, err := http.NewRequestWithContext(ctx, http.MethodPost, origin+"/api/client-launcher/launch",
+ strings.NewReader(`{"mode":"local","language":"en"}`))
+ if err != nil {
+ return nil, err
+ }
+ request.Header.Set("Origin", origin)
+ request.Header.Set("Content-Type", "application/json")
+ return (&http.Client{Timeout: 3 * time.Second}).Do(request)
+ }
+ results := make(chan *http.Response, 2)
+ errors := make(chan error, 2)
+ enter := func(ctx context.Context) {
+ response, err := post(ctx)
+ if err != nil {
+ errors <- err
+ return
+ }
+ results <- response
+ }
+ first, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ go enter(first)
+ select {
+ case <-server.Selection():
+ case <-time.After(time.Second):
+ t.Fatal("first entry did not start the room")
+ }
+ // Leaving the page cancels HTTP, but the accepted startup remains alive.
+ cancel()
+ select {
+ case <-errors:
+ case <-time.After(time.Second):
+ t.Fatal("departed page did not release its request")
+ }
+ // A second page or rapid repeat entry must join the accepted startup.
+ go enter(t.Context())
+ go enter(t.Context())
+ target := "http://localhost:8787/#piik-client=1"
+ server.SetResult(target, nil)
+ check := func(response *http.Response) {
+ t.Helper()
+ defer response.Body.Close()
+ var result struct{ Target string }
+ if json.NewDecoder(response.Body).Decode(&result) != nil || response.StatusCode != http.StatusOK || result.Target != target {
+ t.Fatalf("entry did not open the existing room: status=%d result=%+v", response.StatusCode, result)
+ }
+ }
+ for range 2 {
+ select {
+ case response := <-results:
+ check(response)
+ case err := <-errors:
+ t.Fatal(err)
+ case <-time.After(time.Second):
+ t.Fatal("repeat entry did not finish")
+ }
+ }
+ // Returning to the launcher after sharing uses the same healthy room.
+ response, err := post(t.Context())
+ if err != nil {
+ t.Fatal(err)
+ }
+ check(response)
+ select {
+ case <-server.Selection():
+ t.Fatal("reentry started a second room")
+ default:
+ }
+}
+
func TestRoomFailureInvalidatesReadyTargetButKeepsSiteApprovalAvailable(t *testing.T) {
server := startFixture(t, "https://share.example")
server.changeSite = func(site string, allowed bool) ([]string, error) { return []string{}, nil }
@@ -127,9 +246,8 @@ func TestLauncherRetainsAcceptedSelectionAfterRequestCancellation(t *testing.T)
// Cancellation may race the handoff, but cannot discard an accepted launch.
for range 32 {
server := &Server{
- ctx: t.Context(),
- selection: make(chan Selection, 1),
- resultReady: make(chan struct{}),
+ ctx: t.Context(),
+ selection: make(chan Selection, 1),
}
ctx, cancel := context.WithCancel(t.Context())
request := httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/client-launcher/launch",
@@ -150,6 +268,77 @@ func TestLauncherRetainsAcceptedSelectionAfterRequestCancellation(t *testing.T)
}
}
+type pausedLaunchContext struct {
+ context.Context
+ waiting chan struct{}
+ resume chan struct{}
+}
+
+func (ctx pausedLaunchContext) Done() <-chan struct{} {
+ close(ctx.waiting)
+ <-ctx.resume
+ return ctx.Context.Done()
+}
+
+func TestFailedAttemptCanRetryWithoutReplacingAnOlderRequestResult(t *testing.T) {
+ server := &Server{ctx: t.Context(), selection: make(chan Selection, 1)}
+ // Resume the old HTTP waiter only after the retry has already succeeded.
+ firstContext := pausedLaunchContext{t.Context(), make(chan struct{}), make(chan struct{})}
+ firstResponse := httptest.NewRecorder()
+ firstDone := make(chan struct{})
+ go func() {
+ defer close(firstDone)
+ server.handleLaunch(firstResponse, httptest.NewRequestWithContext(firstContext, "POST", "/api/client-launcher/launch",
+ strings.NewReader(`{"mode":"link","language":"en"}`)))
+ }()
+ select {
+ case <-firstContext.waiting:
+ case <-time.After(time.Second):
+ t.Fatal("first request did not start waiting")
+ }
+ if selection := <-server.Selection(); selection.Mode != ModeLink {
+ t.Fatalf("first selection = %+v", selection)
+ }
+ if server.Begin() {
+ t.Fatal("pending room allowed another start")
+ }
+ server.SetResult("", errors.New("first attempt failed"))
+
+ secondResponse := httptest.NewRecorder()
+ secondDone := make(chan struct{})
+ go func() {
+ defer close(secondDone)
+ server.handleLaunch(secondResponse, httptest.NewRequest("POST", "/api/client-launcher/launch",
+ strings.NewReader(`{"mode":"local","language":"en"}`)))
+ }()
+ select {
+ case selection := <-server.Selection():
+ if selection.Mode != ModeLocal {
+ t.Fatalf("retry selection = %+v", selection)
+ }
+ case <-time.After(time.Second):
+ t.Fatal("failure did not permit retry")
+ }
+ server.SetResult("http://localhost:8787/#piik-client=1", nil)
+ if server.Begin() {
+ t.Fatal("successful room allowed replacement")
+ }
+ close(firstContext.resume)
+ for _, done := range []chan struct{}{firstDone, secondDone} {
+ select {
+ case <-done:
+ case <-time.After(time.Second):
+ t.Fatal("launch request did not complete")
+ }
+ }
+ if firstResponse.Code != http.StatusServiceUnavailable || !strings.Contains(firstResponse.Body.String(), "first attempt failed") {
+ t.Fatalf("old request observed retry result: %d %s", firstResponse.Code, firstResponse.Body.String())
+ }
+ if secondResponse.Code != http.StatusOK || !strings.Contains(secondResponse.Body.String(), "http://localhost:8787/") {
+ t.Fatalf("retry did not return its own success: %d %s", secondResponse.Code, secondResponse.Body.String())
+ }
+}
+
func TestLauncherIncludesTheInjectedBuildVersionAndRevision(t *testing.T) {
const version = "v1.2.3"
const revision = "0123456789abcdef0123456789abcdef01234567"
@@ -166,9 +355,13 @@ func TestLauncherIncludesTheInjectedBuildVersionAndRevision(t *testing.T) {
PackageTarget string `json:"packageTarget"`
DefaultMode Mode `json:"defaultMode"`
}
+ packageTarget := runtime.GOOS + "-" + runtime.GOARCH
+ if runtime.GOOS == "android" {
+ packageTarget = "android-universal"
+ }
if response.StatusCode != http.StatusOK || json.NewDecoder(response.Body).Decode(&state) != nil ||
state.Version != version || state.Revision != revision ||
- state.PackageTarget != runtime.GOOS+"-"+runtime.GOARCH || state.DefaultMode != ModeLink {
+ state.PackageTarget != packageTarget || state.DefaultMode != ModeLink {
t.Fatalf("launcher build = %d, %+v", response.StatusCode, state)
}
}
@@ -227,7 +420,11 @@ func TestLauncherLANChoicesUseCurrentAddressesAndTheExplicitPreference(t *testin
func TestLauncherPassesTheChosenLANAddressToTheApp(t *testing.T) {
server := startFixture(t, "")
- server.SetResult("http://localhost:8787/#piik-client=1", nil)
+ selected := make(chan Selection, 1)
+ go func() {
+ selected <- <-server.Selection()
+ server.SetResult("http://localhost:8787/#piik-client=1", nil)
+ }()
response, err := localPost(strings.TrimSuffix(server.URL(), "/client")+"/api/client-launcher/launch",
"application/json", strings.NewReader(`{"mode":"local","language":"en","lanAddress":"192.168.1.4"}`))
if err != nil {
@@ -237,7 +434,7 @@ func TestLauncherPassesTheChosenLANAddressToTheApp(t *testing.T) {
if response.StatusCode != http.StatusOK {
t.Fatalf("selection status = %d", response.StatusCode)
}
- selection := <-server.Selection()
+ selection := <-selected
if selection.LANAddress == nil || *selection.LANAddress != "192.168.1.4" {
t.Fatalf("selected invitation address was lost: %+v", selection)
}
@@ -376,7 +573,10 @@ func TestLauncherReportsSafeBoundedFailureDetails(t *testing.T) {
}
_ = response.Body.Close()
cause := errors.New("public tunnel failed: token=private-secret " + strings.Repeat("network unavailable ", 200))
- server.SetResult("", errors.Join(appfailure.TunnelExited, cause))
+ go func() {
+ <-server.Selection()
+ server.SetResult("", errors.Join(appfailure.TunnelExited, cause))
+ }()
response, err = localPost(origin+"/api/client-launcher/launch", "application/json",
strings.NewReader(`{"mode":"link","language":"en"}`))
if err != nil {
@@ -400,7 +600,7 @@ func TestLauncherReportsSafeBoundedFailureDetails(t *testing.T) {
if err := json.NewDecoder(response.Body).Decode(&restored); err != nil || restored.Reason != failure.Reason || restored.Detail != failure.Detail {
t.Fatalf("restored failure = %+v, %v", restored, err)
}
- if !errors.Is(server.resultErr, cause) {
+ if !errors.Is(server.attempt.err, cause) {
t.Fatal("launcher lost original cause")
}
}
@@ -462,7 +662,8 @@ func startFixtureWithBuildAndPassword(
site, version, revision, password string,
) *Server {
t.Helper()
- server, err := Start(t.Context(), appAssets(), Options{Sites: siteList(site), Version: version, Revision: revision, LocalAccessPassword: password})
+ server, err := Start(t.Context(), appAssets(), Options{Sites: siteList(site), Version: version, Revision: revision, LocalAccessPassword: password,
+ EnableDebug: func() error { return nil }})
if err != nil {
t.Fatal(err)
}
diff --git a/internal/app/local_https_test.go b/internal/app/local_https_test.go
index 240505ec..38d3d896 100644
--- a/internal/app/local_https_test.go
+++ b/internal/app/local_https_test.go
@@ -33,13 +33,14 @@ func TestLocalHTTPSAndLoopbackShareRoomAuthority(t *testing.T) {
t.Fatal(err)
}
port := reservation.Addr().(*net.TCPAddr).Port
- _ = reservation.Close()
+ defer reservation.Close()
ctx, cancel := context.WithCancel(t.Context())
control, err := loopback.Start(ctx, loopback.Options{})
if err != nil {
cancel()
t.Fatal(err)
}
+ _ = reservation.Close()
ready, done := make(chan string, 1), make(chan error, 1)
go func() {
done <- runLocal(ctx, Options{Local: true, Port: port, LANAddress: addresses[0],
@@ -148,14 +149,28 @@ func TestLocalPortCollisionReleasesTheOtherEntry(t *testing.T) {
{"tcp4", "127.0.0.1"}, {"tcp4", addresses[0]}, {"udp4", addresses[0]},
} {
t.Run(test.network+"/"+test.address, func(t *testing.T) {
+ control, err := loopback.Start(t.Context(), loopback.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer control.Close()
var occupied io.Closer
+ var reservation net.Listener
var port int
if test.network == "udp4" {
- socket, err := net.ListenPacket("udp4", net.JoinHostPort(test.address, "0"))
+ // A UDP allocation alone says nothing about the same TCP port.
+ // Reserve both Local HTTP addresses until the UDP fixture is ready.
+ reservation, err = net.Listen("tcp4", "0.0.0.0:0")
if err != nil {
t.Fatal(err)
}
- occupied, port = socket, socket.LocalAddr().(*net.UDPAddr).Port
+ defer reservation.Close()
+ port = reservation.Addr().(*net.TCPAddr).Port
+ socket, err := net.ListenPacket("udp4", net.JoinHostPort(test.address, fmt.Sprint(port)))
+ if err != nil {
+ t.Fatal(err)
+ }
+ occupied = socket
} else {
socket, err := net.Listen("tcp4", net.JoinHostPort(test.address, "0"))
if err != nil {
@@ -164,11 +179,9 @@ func TestLocalPortCollisionReleasesTheOtherEntry(t *testing.T) {
occupied, port = socket, socket.Addr().(*net.TCPAddr).Port
}
defer occupied.Close()
- control, err := loopback.Start(t.Context(), loopback.Options{})
- if err != nil {
- t.Fatal(err)
+ if reservation != nil {
+ _ = reservation.Close()
}
- defer control.Close()
err = runLocal(t.Context(), Options{Local: true, Port: port, LANAddress: addresses[0],
DisableBrowser: true, console: newConsole(func() {}, true),
Ready: func(string) { t.Error("occupied Local service reported readiness") },
diff --git a/internal/app/loopback/server.go b/internal/app/loopback/server.go
index 27b71f28..b64ebd3c 100644
--- a/internal/app/loopback/server.go
+++ b/internal/app/loopback/server.go
@@ -31,13 +31,16 @@ const (
)
type Options struct {
- PortStart int
- PortEnd int
- AllowedOrigins []string
- NativeMedia NativeMediaCapabilities
- NewControl func() ControlSession
- Presentation func(language string)
- QualityPreference *appconfig.QualityPreference
+ PortStart int
+ PortEnd int
+ // Zero keeps the desktop default. Single-source platforms may admit one.
+ MaxControlSessions int
+ AllowedOrigins []string
+ ActivationURL string
+ NativeMedia NativeMediaCapabilities
+ NewControl func() ControlSession
+ Presentation func(language string)
+ QualityPreference *appconfig.QualityPreference
}
type ControlSession interface {
@@ -73,6 +76,7 @@ type Health struct {
InstanceToken string `json:"instanceToken"`
NativeMedia NativeMediaCapabilities `json:"nativeMedia"`
QualityPreference bool `json:"qualityPreference,omitempty"`
+ ActivationURL string `json:"activationURL,omitempty"`
}
type NativeMediaCapabilities struct {
@@ -90,6 +94,7 @@ type NativeMediaCapabilities struct {
Video bool `json:"video"`
CaptureBorderControl bool `json:"captureBorderControl,omitempty"`
Microphone bool `json:"microphone,omitempty"`
+ SourceCancellation bool `json:"sourceCancellation,omitempty"`
ShareVolume bool `json:"shareVolume,omitempty"`
ProcessAudio bool `json:"processAudio"`
ProcessAudioExclusion bool `json:"processAudioExclusion,omitempty"`
@@ -106,7 +111,9 @@ type Server struct {
endpoint Endpoint
allowedOrigins []string
nativeMedia NativeMediaCapabilities
+ activationURL string
newControl func() ControlSession
+ maxSessions int
presentation func(string)
qualityPreference *appconfig.QualityPreference
connectURL string
@@ -134,6 +141,13 @@ func Start(parent context.Context, options Options) (*Server, error) {
if parent == nil {
parent = context.Background()
}
+ sessionLimit := options.MaxControlSessions
+ if sessionLimit == 0 {
+ sessionLimit = maxControlSessions
+ }
+ if sessionLimit < 1 || sessionLimit > maxControlSessions {
+ return nil, errors.New("native control session limit is invalid")
+ }
portStart, portEnd := normalizePortRange(options.PortStart, options.PortEnd)
listener, err := listenInRange(defaultBindAddress, portStart, portEnd)
if err != nil {
@@ -160,7 +174,9 @@ func Start(parent context.Context, options Options) (*Server, error) {
},
allowedOrigins: normalizedOrigins(options.AllowedOrigins),
nativeMedia: options.NativeMedia,
+ activationURL: options.ActivationURL,
newControl: options.NewControl,
+ maxSessions: sessionLimit,
presentation: options.Presentation,
qualityPreference: options.QualityPreference,
done: make(chan error, 1),
@@ -189,6 +205,26 @@ func (server *Server) Done() <-chan error {
func (server *Server) SetAllowedOrigins(origins []string) error {
server.mu.Lock()
+ retired := server.setAllowedOriginsLocked(origins)
+ server.mu.Unlock()
+ return server.retireConnections(retired)
+}
+
+// RemoveAllowedOrigin retires one authority without replaying other origins.
+func (server *Server) RemoveAllowedOrigin(origin string) error {
+ server.mu.Lock()
+ origins := make([]string, 0, len(server.allowedOrigins))
+ for _, allowed := range server.allowedOrigins {
+ if !sameOrigin(allowed, origin) {
+ origins = append(origins, allowed)
+ }
+ }
+ retired := server.setAllowedOriginsLocked(origins)
+ server.mu.Unlock()
+ return server.retireConnections(retired)
+}
+
+func (server *Server) setAllowedOriginsLocked(origins []string) []*controlConnection {
server.allowedOrigins = normalizedOrigins(origins)
var retired []*controlConnection
for connection := range server.connections {
@@ -197,7 +233,10 @@ func (server *Server) SetAllowedOrigins(origins []string) error {
retired = append(retired, connection)
}
}
- server.mu.Unlock()
+ return retired
+}
+
+func (server *Server) retireConnections(retired []*controlConnection) error {
ctx, cancel := context.WithTimeout(server.ctx, shutdownTimeout)
defer cancel()
for _, connection := range retired {
@@ -383,6 +422,7 @@ func (server *Server) handleHealth(response http.ResponseWriter, request *http.R
InstanceToken: server.endpoint.InstanceToken,
NativeMedia: server.nativeMedia,
QualityPreference: server.qualityPreference != nil,
+ ActivationURL: server.activationURL,
})
}
@@ -631,7 +671,7 @@ func (server *Server) localOrigin(origin string) bool {
func (server *Server) claimConnection(origin string) *controlConnection {
server.mu.Lock()
defer server.mu.Unlock()
- if server.closed || len(server.connections) >= maxControlSessions || !server.originAllowedLocked(origin) {
+ if server.closed || len(server.connections) >= server.maxSessions || !server.originAllowedLocked(origin) {
return nil
}
ctx, cancel := context.WithCancel(server.ctx)
diff --git a/internal/app/loopback/server_test.go b/internal/app/loopback/server_test.go
index b2393c69..c395a1e7 100644
--- a/internal/app/loopback/server_test.go
+++ b/internal/app/loopback/server_test.go
@@ -19,6 +19,38 @@ import (
const testOrigin = "https://share.example.com"
+func TestHealthActivationURLIsOptionalAndOriginBound(t *testing.T) {
+ for _, activation := range []string{"", "piik://media"} {
+ t.Run(activation, func(t *testing.T) {
+ server := startTestServerWithOptions(t, Options{
+ AllowedOrigins: []string{testOrigin}, ActivationURL: activation,
+ })
+ endpoint := server.Endpoint()
+ response := doHealthRequest(t, endpoint, endpoint.Host, testOrigin)
+ defer response.Body.Close()
+ var health map[string]json.RawMessage
+ if response.StatusCode != http.StatusOK {
+ t.Fatalf("health status = %d", response.StatusCode)
+ }
+ if err := json.NewDecoder(response.Body).Decode(&health); err != nil {
+ t.Fatal(err)
+ }
+ field, present := health["activationURL"]
+ if present != (activation != "") {
+ t.Fatalf("activationURL presence = %t, want %t", present, activation != "")
+ }
+ if present && string(field) != `"piik://media"` {
+ t.Fatalf("activationURL = %s", field)
+ }
+ denied := doHealthRequest(t, endpoint, endpoint.Host, testOrigin+".other")
+ defer denied.Body.Close()
+ if denied.StatusCode != http.StatusForbidden {
+ t.Fatalf("unapproved origin health status = %d", denied.StatusCode)
+ }
+ })
+ }
+}
+
func TestStartServesHealthAndStrictControlHandshake(t *testing.T) {
expectedMedia := NativeMediaCapabilities{
Video: true, ProcessAudio: false, SystemAudio: true, HardwareH264: true, MaxRelayChildren: 20,
@@ -169,64 +201,75 @@ func TestPresentationUpdatesOnlyLanguageWithoutClaimingMediaControl(t *testing.T
}
func TestControlSessionsAreBoundedAndRetireIndependently(t *testing.T) {
- created := make(chan *testControlSession, maxControlSessions)
- server := startTestServerWithOptions(t, Options{
- AllowedOrigins: []string{testOrigin},
- NewControl: func() ControlSession {
- session := &testControlSession{events: make(chan any), closed: make(chan struct{})}
- created <- session
- return session
- },
- })
- endpoint := server.Endpoint()
- var connections []*websocket.Conn
- var sessions []*testControlSession
- for index := 0; index < maxControlSessions; index++ {
- connection := dialControl(t, endpoint, endpoint.InstanceToken, testOrigin)
- defer connection.CloseNow()
- writeControl(t, connection, requestJSON("request_hello", "hello"))
- var ready controlMessage
- readControl(t, connection, &ready)
- connections = append(connections, connection)
- select {
- case session := <-created:
- sessions = append(sessions, session)
- case <-time.After(time.Second):
- t.Fatal("accepted control did not acquire its own session")
- }
- }
- extra, response, err := websocket.Dial(context.Background(), websocketURL(endpoint)+"/control", &websocket.DialOptions{
- HTTPHeader: http.Header{"Origin": []string{testOrigin}},
- Subprotocols: []string{ControlSubprotocol + "." + endpoint.InstanceToken},
- })
- if extra != nil {
- extra.CloseNow()
- }
- if err == nil || response == nil || response.StatusCode != http.StatusConflict {
- t.Fatalf("excess control dial: conn=%v response=%v err=%v", extra, response, err)
- }
- _ = response.Body.Close()
- _ = connections[0].CloseNow()
- select {
- case <-sessions[0].closed:
- case <-time.After(time.Second):
- t.Fatal("closed room retained its native session")
- }
- writeControl(t, connections[1], requestJSON("request_ping", "ping"))
- var pong controlMessage
- readControl(t, connections[1], &pong)
- if pong.Type != "pong" {
- t.Fatal("closing one room affected its sibling control")
- }
- if err = server.Close(); err != nil {
- t.Fatal(err)
- }
- for _, session := range sessions {
- select {
- case <-session.closed:
- default:
- t.Fatal("App close returned before native session cleanup")
- }
+ for _, configured := range []int{0, 1} {
+ t.Run(strconv.Itoa(configured), func(t *testing.T) {
+ limit := configured
+ if limit == 0 {
+ limit = maxControlSessions
+ }
+ created := make(chan *testControlSession, limit)
+ server := startTestServerWithOptions(t, Options{
+ MaxControlSessions: configured,
+ AllowedOrigins: []string{testOrigin},
+ NewControl: func() ControlSession {
+ session := &testControlSession{events: make(chan any), closed: make(chan struct{})}
+ created <- session
+ return session
+ },
+ })
+ endpoint := server.Endpoint()
+ var connections []*websocket.Conn
+ var sessions []*testControlSession
+ for index := 0; index < limit; index++ {
+ connection := dialControl(t, endpoint, endpoint.InstanceToken, testOrigin)
+ defer connection.CloseNow()
+ writeControl(t, connection, requestJSON("request_hello", "hello"))
+ var ready controlMessage
+ readControl(t, connection, &ready)
+ connections = append(connections, connection)
+ select {
+ case session := <-created:
+ sessions = append(sessions, session)
+ case <-time.After(time.Second):
+ t.Fatal("accepted control did not acquire its own session")
+ }
+ }
+ extra, response, err := websocket.Dial(context.Background(), websocketURL(endpoint)+"/control", &websocket.DialOptions{
+ HTTPHeader: http.Header{"Origin": []string{testOrigin}},
+ Subprotocols: []string{ControlSubprotocol + "." + endpoint.InstanceToken},
+ })
+ if extra != nil {
+ extra.CloseNow()
+ }
+ if err == nil || response == nil || response.StatusCode != http.StatusConflict {
+ t.Fatalf("excess control dial: conn=%v response=%v err=%v", extra, response, err)
+ }
+ _ = response.Body.Close()
+ _ = connections[0].CloseNow()
+ select {
+ case <-sessions[0].closed:
+ case <-time.After(time.Second):
+ t.Fatal("closed room retained its native session")
+ }
+ if len(connections) > 1 {
+ writeControl(t, connections[1], requestJSON("request_ping", "ping"))
+ var pong controlMessage
+ readControl(t, connections[1], &pong)
+ if pong.Type != "pong" {
+ t.Fatal("closing one room affected its sibling control")
+ }
+ }
+ if err = server.Close(); err != nil {
+ t.Fatal(err)
+ }
+ for _, session := range sessions {
+ select {
+ case <-session.closed:
+ default:
+ t.Fatal("App close returned before native session cleanup")
+ }
+ }
+ })
}
}
diff --git a/internal/app/loopback/site_access_test.go b/internal/app/loopback/site_access_test.go
index 10b7c118..0edd3b84 100644
--- a/internal/app/loopback/site_access_test.go
+++ b/internal/app/loopback/site_access_test.go
@@ -3,6 +3,7 @@ package loopback
import (
"context"
"encoding/json"
+ "fmt"
"net/http"
"testing"
"time"
@@ -62,8 +63,12 @@ func (session *retiringControl) Close() error {
}
func TestSiteRevocationPreservesUnrelatedSessions(t *testing.T) {
- for _, otherOrigin := range []string{"", "https://keep.example"} {
- t.Run(otherOrigin, func(t *testing.T) {
+ for _, test := range []struct {
+ otherOrigin string
+ remove bool
+ }{{"", false}, {"https://keep.example", false}, {"https://keep.example", true}} {
+ t.Run(fmt.Sprintf("%s/remove=%v", test.otherOrigin, test.remove), func(t *testing.T) {
+ otherOrigin := test.otherOrigin
finish := make(chan struct{})
defer close(finish)
created := make(chan *testControlSession, 2)
@@ -89,7 +94,13 @@ func TestSiteRevocationPreservesUnrelatedSessions(t *testing.T) {
localSession := <-created
done := make(chan error, 1)
- go func() { done <- server.SetAllowedOrigins([]string{"https://next.example", "https://keep.example"}) }()
+ go func() {
+ if test.remove {
+ done <- server.RemoveAllowedOrigin(testOrigin)
+ } else {
+ done <- server.SetAllowedOrigins([]string{"https://next.example", "https://keep.example"})
+ }
+ }()
select {
case <-oldSession.closed:
case <-time.After(time.Second):
diff --git a/internal/app/mediaedge/edge_test.go b/internal/app/mediaedge/edge_test.go
index efbbbb4b..3e053bdf 100644
--- a/internal/app/mediaedge/edge_test.go
+++ b/internal/app/mediaedge/edge_test.go
@@ -523,16 +523,23 @@ func TestNewCaptureGenerationKeepsTheRTPClockContinuous(t *testing.T) {
t.Cleanup(func() { _ = receiver.Close() })
accessUnit := sfu.VP8KeyFrame8x8
frameDuration := time.Second / 30
- if err = writeSourceFrame(source, accessUnit, 50*time.Second, frameDuration); err != nil {
+ origin := time.Now()
+ if _, err = source.beginFrame(50*time.Second, origin); err != nil {
+ t.Fatal(err)
+ }
+ if err = source.WriteVideo(0, encoded.Frame{Data: accessUnit, PTS: 50 * time.Second, Duration: frameDuration, Recovery: true}); err != nil {
t.Fatal(err)
}
first := waitPacket(t, packets, edge)
source.BeginGeneration()
- if err = writeSourceFrame(source, accessUnit, 100*time.Millisecond, frameDuration); err != nil {
+ if _, err = source.beginFrame(100*time.Millisecond, origin.Add(200*time.Millisecond)); err != nil {
+ t.Fatal(err)
+ }
+ if err = source.WriteVideo(0, encoded.Frame{Data: accessUnit, PTS: 100 * time.Millisecond, Duration: frameDuration, Recovery: true}); err != nil {
t.Fatal(err)
}
second := waitPacket(t, packets, edge)
- if got := second.Timestamp - first.Timestamp; got < 2_999 || got > 3_001 {
+ if got := second.Timestamp - first.Timestamp; got < 17_999 || got > 18_001 {
t.Fatalf("generation timestamp delta = %d", got)
}
}
diff --git a/internal/app/mediaedge/engine.go b/internal/app/mediaedge/engine.go
index a6ccf231..c404a8b0 100644
--- a/internal/app/mediaedge/engine.go
+++ b/internal/app/mediaedge/engine.go
@@ -211,6 +211,9 @@ func (engine *Engine) newSource(codec string, capacity, layers int, relay *Relay
return nil, err
}
source.media = media
+ if relay != nil {
+ media.UseSourceClock(media.Source, layers-1)
+ }
if requestKeyFrame != nil {
go source.runRecovery()
}
diff --git a/internal/app/mediaedge/group_fixture_test.go b/internal/app/mediaedge/group_fixture_test.go
index 878c5647..a5d31e14 100644
--- a/internal/app/mediaedge/group_fixture_test.go
+++ b/internal/app/mediaedge/group_fixture_test.go
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"io"
+ "math"
"os"
"testing"
"time"
@@ -187,9 +188,11 @@ func TestEncodedGroupVP8Fixture(t *testing.T) {
firstTS = packet.Timestamp
if previous[child] != nil {
delta := packet.Timestamp - previous[child].Timestamp
- want := (row.PTS - previousPTS[child]).Seconds() * videoClockRate
+ // SMED PTS uses 100 ns units; compare whole 90 kHz ticks
+ // before allowing the SR projection's one-tick rounding.
+ want := math.Round((row.PTS - previousPTS[child]).Seconds() * videoClockRate)
if float64(delta) < want-1 || float64(delta) > want+1 {
- t.Fatal("layer switch changed source time mapping")
+ t.Fatalf("frame %d child %d layer %d: source clock advanced %d RTP ticks, expected %.0f", row.FrameIndex, child, row.Layer, delta, want)
}
}
} else if packet.Timestamp != firstTS {
diff --git a/internal/app/mediaedge/groups.go b/internal/app/mediaedge/groups.go
index ce090882..4d5dc689 100644
--- a/internal/app/mediaedge/groups.go
+++ b/internal/app/mediaedge/groups.go
@@ -21,11 +21,12 @@ type groupConsumer interface {
}
type outputGroup struct {
- slot int
- media *forwarding.EncodedSource
- budget uint32
- active bool
- start time.Duration
+ slot int
+ media *forwarding.EncodedSource
+ budget uint32
+ active bool
+ start time.Duration
+ clockPending bool
}
type outputDemand struct {
@@ -221,6 +222,9 @@ func (source *Source) planGroups(demands []outputDemand) (OutputPlan, error) {
}
}
nextActive := active && source.outputBitrates[group.slot] > 0
+ if nextActive && !group.active && source.relay != nil {
+ group.clockPending = true
+ }
if group.active != nextActive {
slog.Debug("encoding-group", "event", "activity", "slot", group.slot, "active", nextActive, "sourceId", diagnostics.ID(fmt.Sprintf("%p", source)))
}
@@ -259,8 +263,11 @@ func (source *Source) newGroup(slot int) (*outputGroup, error) {
if err != nil {
return nil, err
}
+ if source.relay != nil {
+ media.UseSourceClock(source.media.Source, len(source.formats)-1)
+ }
media.SetLowestLayerRateControlled(true)
- group := &outputGroup{slot: slot, media: media, start: source.inputPTS}
+ group := &outputGroup{slot: slot, media: media, start: source.inputPTS, clockPending: source.relay != nil}
if source.hasInput {
if err = media.BeginFrame(source.inputPTS, source.inputAt); err != nil {
media.Close()
@@ -302,6 +309,19 @@ func groupConsumerID(consumer groupConsumer) string {
}
func (source *Source) beginGroupFrame(pts time.Duration, at time.Time) error {
+ if source.hasInput {
+ // New groups inherit the established capture clock. Pipe arrival time
+ // can change independently of the input timestamps.
+ at = source.inputAt.Add(pts - source.inputPTS)
+ } else if source.relay == nil && !source.inputAt.IsZero() && !at.IsZero() {
+ // Capture generations can reset PTS while first-arrival latency changes.
+ // Normalize their common origin beyond the last input by one RTP tick;
+ // relay input already supplies its authoritative upstream clock.
+ earliest := source.inputAt.Add(time.Second/videoClockRate + time.Nanosecond)
+ if at.Before(earliest) {
+ at = earliest
+ }
+ }
if err := source.media.BeginFrame(pts, at); err != nil {
return err
}
diff --git a/internal/app/mediaedge/groups_test.go b/internal/app/mediaedge/groups_test.go
index 32d90048..13fa120a 100644
--- a/internal/app/mediaedge/groups_test.go
+++ b/internal/app/mediaedge/groups_test.go
@@ -1,6 +1,7 @@
package mediaedge
import (
+ "errors"
"fmt"
"testing"
"time"
@@ -8,7 +9,9 @@ import (
"github.com/TNTcraftHIM/Piik/internal/media/encoded"
"github.com/TNTcraftHIM/Piik/internal/media/forwarding"
"github.com/livekit/livekit-server/pkg/sfu"
+ "github.com/livekit/mediatransportutil"
"github.com/pion/rtcp"
+ "github.com/pion/rtp"
)
type groupTestConsumer struct{ source *forwarding.Source }
@@ -441,7 +444,7 @@ func TestOutputGroupGenerationRefreshesMetadataAndClock(t *testing.T) {
t.Fatal(err)
}
report := group.media.GetAllBuffers()[0].GetSenderReportData()
- if report == nil || report.RtpTimestamp-previousTimestamp < 2999 || report.RtpTimestamp-previousTimestamp > 3001 {
+ if report == nil || report.RtpTimestamp-previousTimestamp != 100*90_000 {
t.Fatal("new capture generation broke the group RTP clock")
}
correlated, ok := group.media.ReferenceTime(0, report.RtpTimestamp)
@@ -449,3 +452,200 @@ func TestOutputGroupGenerationRefreshesMetadataAndClock(t *testing.T) {
t.Fatalf("new generation correlation = %v, available=%v", correlated, ok)
}
}
+
+func TestLateOutputGroupKeepsSharedCaptureClock(t *testing.T) {
+ source, _, _, _ := newGroupLifecycleFixture(t)
+ // The capture clock advances one second while input-pipe latency grows.
+ if err := source.beginGroupFrame(11*time.Second, time.Unix(101, 500_000_000)); err != nil {
+ t.Fatal(err)
+ }
+ group, err := source.newGroup(3)
+ if err != nil {
+ t.Fatal(err)
+ }
+ write := func(pts time.Duration, expected time.Time) uint32 {
+ t.Helper()
+ var timestamp uint32
+ for _, media := range []*forwarding.EncodedSource{source.media, group.media} {
+ if err := media.WriteFrame(0, encoded.Frame{Data: sfu.VP8KeyFrame8x8,
+ PTS: pts, Duration: time.Second / 30}); err != nil {
+ t.Fatal(err)
+ }
+ report := media.GetAllBuffers()[0].GetSenderReportData()
+ if report == nil {
+ t.Fatal("output did not establish its capture clock")
+ }
+ at, ok := media.ReferenceTime(0, report.RtpTimestamp)
+ if !ok || at.Sub(expected).Abs() > time.Microsecond {
+ t.Fatalf("output capture clock = %v, expected %v", at, expected)
+ }
+ timestamp = report.RtpTimestamp
+ }
+ return timestamp
+ }
+ previous := write(11*time.Second, time.Unix(101, 0))
+ source.BeginGeneration()
+ if err := source.beginGroupFrame(time.Second, time.Unix(200, 0)); err != nil {
+ t.Fatal(err)
+ }
+ current := write(time.Second, time.Unix(200, 0))
+ if advance := current - previous; advance != 99*90_000 {
+ t.Fatalf("replacement group RTP timestamp advanced %d ticks", advance)
+ }
+}
+
+func TestCaptureGenerationNormalizesOneSharedClock(t *testing.T) {
+ source, err := (&Engine{}).NewSource("vp8", 2, 2, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer source.Close()
+ if err = source.ConfigureOutputs([]uint32{500_000, 2_000_000, 500_000, 500_000}); err != nil {
+ t.Fatal(err)
+ }
+ if err = source.beginGroupFrame(10*time.Second, time.Unix(100, 0)); err != nil {
+ t.Fatal(err)
+ }
+ if _, err = source.planGroups(nil); err != nil {
+ t.Fatal(err)
+ }
+ group, err := source.newGroup(2)
+ if err != nil {
+ t.Fatal(err)
+ }
+ write := func(media *forwarding.EncodedSource, layer int, pts time.Duration, expected time.Time) {
+ t.Helper()
+ if err := media.WriteFrame(layer, encoded.Frame{Data: sfu.VP8KeyFrame8x8,
+ PTS: pts, Duration: time.Second / 30, Recovery: true}); err != nil {
+ t.Fatal(err)
+ }
+ report := media.GetAllBuffers()[layer].GetSenderReportData()
+ at, ok := media.ReferenceTime(layer, report.RtpTimestamp)
+ if !ok || at.Sub(expected).Abs() > time.Microsecond {
+ t.Fatalf("output clock = %v, expected %v", at, expected)
+ }
+ }
+ for _, media := range []*forwarding.EncodedSource{source.media, group.media} {
+ for layer := range 2 {
+ write(media, layer, 10*time.Second, time.Unix(100, 0))
+ }
+ }
+ // Later input completes faster than the first one. Its capture timeline
+ // advances to 101 even though the input arrives at 100.1.
+ if err := source.beginGroupFrame(11*time.Second, time.Unix(100, 100_000_000)); err != nil {
+ t.Fatal(err)
+ }
+ write(source.media, 0, 11*time.Second, time.Unix(101, 0))
+ source.BeginGeneration()
+ if err := source.beginGroupFrame(0, time.Unix(100, 200_000_000)); err != nil {
+ t.Fatal(err)
+ }
+ origin := time.Unix(101, 0).Add(time.Second/videoClockRate + time.Nanosecond)
+ write(source.media, 0, 0, origin)
+ late, err := source.newGroup(3)
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Original and derived outputs resume after different dormant intervals;
+ // the newly created group inherits the same normalized generation origin.
+ if err = source.beginGroupFrame(5*time.Second, time.Unix(100, 300_000_000)); err != nil {
+ t.Fatal(err)
+ }
+ for _, media := range []*forwarding.EncodedSource{source.media, group.media, late.media} {
+ for layer := range 2 {
+ write(media, layer, 5*time.Second, origin.Add(5*time.Second))
+ }
+ }
+ if err = source.beginGroupFrame(4*time.Second, time.Unix(200, 0)); !errors.Is(err, encoded.ErrInvalidTimestamp) {
+ t.Fatalf("same-generation reverse PTS = %v", err)
+ }
+ source.BeginGeneration()
+ if err = source.beginGroupFrame(0, time.Unix(200, 0)); err != nil {
+ t.Fatal(err)
+ }
+ write(source.media, 0, 0, time.Unix(200, 0))
+}
+
+func TestRelayGroupSharesReceivedClockAcrossWrap(t *testing.T) {
+ check := func(err error) {
+ t.Helper()
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ source, err := (&Engine{}).newSource("vp8", 2, 2, &RelayOptions{}, nil)
+ check(err)
+ defer source.Close()
+ check(source.SetFormat(0, 8, 8))
+ check(source.SetFormat(1, 8, 8))
+ check(source.ConfigureOutputs([]uint32{100_000, 1_000_000, 100_000, 100_000}))
+ source.groups = make(map[int]*outputGroup)
+ source.memberships = make(map[groupConsumer]*outputGroup)
+ origin := time.Now()
+ write := func(sequence uint16, timestamp uint32, pts time.Duration) {
+ t.Helper()
+ check(source.beginGroupFrame(pts, origin.Add(pts)))
+ check(source.WriteRTP(&rtp.Packet{Header: rtp.Header{
+ Version: 2, SSRC: 99, PayloadType: 96, SequenceNumber: sequence, Timestamp: timestamp, Marker: true,
+ }, Payload: append([]byte{0x10}, sfu.VP8KeyFrame8x8...)}))
+ if group := source.groups[2]; group != nil && group.active {
+ copied := group.media.GetAllBuffers()[1].GetSenderReportData()
+ accepted := source.media.GetAllBuffers()[1].GetSenderReportData()
+ if copied == nil || copied.RtpTimestamp != accepted.RtpTimestamp || copied.NtpTimestamp != accepted.NtpTimestamp {
+ t.Fatal("first copied RTP waited for another wire report")
+ }
+ }
+ check(source.SenderReport(&rtcp.SenderReport{SSRC: 99, RTPTime: timestamp,
+ NTPTime: uint64(mediatransportutil.ToNtpTime(origin.Add(pts))),
+ PacketCount: uint32(sequence), OctetCount: uint32(sequence) * 20}))
+ }
+ write(1, 0xffff_ff00, 0)
+ write(2, 644, 10*time.Millisecond)
+ // The original already observed the path; a later group first sees a delayed
+ // report. That internal copy must not establish another network clock.
+ time.Sleep(120 * time.Millisecond)
+ group, err := source.newGroup(2)
+ check(err)
+ group.active = true
+ earlier := &rtcp.SenderReport{SSRC: 99, RTPTime: 0xffff_ff00,
+ NTPTime: uint64(mediatransportutil.ToNtpTime(origin)), PacketCount: 1, OctetCount: 20}
+ check(source.SenderReport(earlier))
+ if group.media.GetAllBuffers()[1].GetSenderReportData() != nil {
+ t.Fatal("a group without RTP accepted a sender report")
+ }
+ write(3, 1544, 20*time.Millisecond)
+ check(source.WriteVideo(2, encoded.Frame{Data: sfu.VP8KeyFrame8x8,
+ PTS: 20 * time.Millisecond, Duration: time.Second / 30}))
+ // A delayed older wire report is rejected by the original owner before
+ // either internal copy can adopt it.
+ check(source.SenderReport(earlier))
+ base := source.media.GetAllBuffers()[1].GetSenderReportData()
+ copied := group.media.GetAllBuffers()[1].GetSenderReportData()
+ if base == nil || copied == nil || base.RtpTimestamp != copied.RtpTimestamp ||
+ base.NtpTimestamp != copied.NtpTimestamp || base.Packets != copied.Packets || base.Octets != copied.Octets {
+ t.Fatal("copied original changed the accepted sender report")
+ }
+ if base.RtpTimestampExt-copied.RtpTimestampExt != 1<<32 {
+ t.Fatal("late group did not retain its own RTP wrap epoch")
+ }
+ for _, media := range []*forwarding.EncodedSource{source.media, group.media} {
+ for layer, buffer := range media.GetAllBuffers() {
+ if buffer == nil || buffer.GetSenderReportData() == nil {
+ continue
+ }
+ local, ok := media.LocalTime(layer, origin.Add(20*time.Millisecond))
+ if !ok || local.UnixNano() != base.AtAdjusted {
+ t.Fatalf("layer %d split the received clock: local=%v expected=%v", layer, local, time.Unix(0, base.AtAdjusted))
+ }
+ }
+ }
+ group.active = false
+ write(4, 3344, 40*time.Millisecond)
+ consumer := &groupTestConsumer{group.media.Source}
+ _, err = source.planGroups([]outputDemand{{consumer: consumer, budget: group.budget, active: true, lower: true}})
+ check(err)
+ if !group.active {
+ t.Fatal("fixture did not reactivate the relay group")
+ }
+ write(5, 4244, 50*time.Millisecond)
+}
diff --git a/internal/app/mediaedge/source.go b/internal/app/mediaedge/source.go
index 00efd8d4..6e0b6f0e 100644
--- a/internal/app/mediaedge/source.go
+++ b/internal/app/mediaedge/source.go
@@ -164,17 +164,34 @@ func (source *Source) beginFrame(timestamp time.Duration, at time.Time) (OutputP
}
plan := OutputPlan{Active: make([]bool, len(source.outputBitrates)), Bitrates: append([]uint32(nil), source.outputBitrates...)}
count := 0
+ var sharedBudget int64
+ remoteDemand := false
for _, edge := range edges {
if edge.State() == webrtc.PeerConnectionStateConnected {
- count = max(count, edge.transport.RequiredActiveCount())
+ required := edge.transport.RequiredActiveCount()
+ count = max(count, required)
+ if !edge.local && required > 0 {
+ remoteDemand = true
+ sharedBudget = max(sharedBudget, edge.transport.Output.State().VideoBudget)
+ }
}
}
for _, publication := range publications {
- count = max(count, publication.RequiredActiveCount())
+ required := publication.RequiredActiveCount()
+ count = max(count, required)
+ if required > 0 {
+ remoteDemand = true
+ sharedBudget = max(sharedBudget, publication.LowestLayerBudget())
+ }
}
for slot := 0; slot < min(count, len(plan.Active)); slot++ {
plan.Active[slot] = true
}
+ if len(source.formats) == 1 && len(plan.Bitrates) == 1 && plan.Bitrates[0] > 0 && remoteDemand {
+ // One shared rendition follows the largest child demand. Local preview
+ // does not hold it at the ceiling or reduce a healthy child's quality.
+ plan.Bitrates[0] = uint32(max(1000, min(source.media.CodecBudget(0, sharedBudget), int64(plan.Bitrates[0]))))
+ }
return plan, nil
}
@@ -401,7 +418,12 @@ func (source *Source) WriteRTP(packet *rtp.Packet) error {
err := source.media.Source.WriteRTP(layer, &forwarded)
for slot, group := range source.groups {
if slot != 0 && group.active {
- if groupErr := group.media.Source.WriteRTP(layer, &forwarded); err == nil {
+ groupErr := group.media.Source.WriteRTP(layer, &forwarded)
+ if groupErr == nil && group.clockPending {
+ groupErr = group.media.CopyCorrelation(layer, source.media.Source)
+ group.clockPending = false
+ }
+ if err == nil {
err = groupErr
}
}
@@ -466,7 +488,7 @@ func (source *Source) SenderReport(report *rtcp.SenderReport) error {
err := source.media.SenderReport(layer, &correlated)
for slot, group := range source.groups {
if slot != 0 && group.active {
- if groupErr := group.media.SenderReport(layer, &correlated); err == nil {
+ if groupErr := group.media.CopyCorrelation(layer, source.media.Source); err == nil {
err = groupErr
}
}
diff --git a/internal/app/mediaedge/source_test.go b/internal/app/mediaedge/source_test.go
index 772687e8..972ad77a 100644
--- a/internal/app/mediaedge/source_test.go
+++ b/internal/app/mediaedge/source_test.go
@@ -1,6 +1,7 @@
package mediaedge
import (
+ "context"
"encoding/binary"
"errors"
"fmt"
@@ -14,6 +15,7 @@ import (
"github.com/TNTcraftHIM/Piik/internal/media/forwarding"
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/streamtracker"
+ "github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/webrtc/v4"
"google.golang.org/protobuf/proto"
@@ -59,6 +61,97 @@ func TestSourceRecoveryCoalescesTargetsWithoutWakingUnrelatedOutputs(t *testing.
}
}
+func TestSingleRenditionFollowsSharedRemoteBudget(t *testing.T) {
+ engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true, InitialBitrate: 200_000})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer engine.Close()
+ source, err := engine.NewSource("vp8", 2, 1, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer source.Close()
+ check := func(err error) {
+ t.Helper()
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ check(source.SetFormat(0, 640, 360))
+ check(source.ConfigureOutputs([]uint32{1_000_000}))
+ source.SetLowestLayerRateControlled(true)
+ var pts time.Duration
+ plan := func(label string, active bool, bitrate uint32) {
+ t.Helper()
+ value, err := source.BeginFrame(pts)
+ check(err)
+ pts += time.Second / 30
+ if len(value.Active) != 1 || value.Active[0] != active || len(value.Bitrates) != 1 || value.Bitrates[0] != bitrate {
+ t.Fatalf("%s: plan = %+v, want active=%t bitrate=%d", label, value, active, bitrate)
+ }
+ }
+ plan("no consumers", false, 1_000_000)
+ preview, err := engine.NewEdge(source, EdgeOptions{ConnectionID: "preview", Local: true})
+ check(err)
+ previewReceiver := newReceiver(t)
+ defer previewReceiver.Close()
+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
+ defer cancel()
+ offer, err := preview.CreateLocalOffer(ctx, "preview-offer", false)
+ check(err)
+ check(previewReceiver.SetRemoteDescription(offer))
+ answer, err := previewReceiver.CreateAnswer(nil)
+ check(err)
+ gathered := webrtc.GatheringCompletePromise(previewReceiver)
+ check(previewReceiver.SetLocalDescription(answer))
+ waitSignal(t, gathered, "preview ICE gathering")
+ check(preview.SetAnswer(*previewReceiver.LocalDescription(), "preview-offer"))
+ for preview.State() != webrtc.PeerConnectionStateConnected {
+ select {
+ case <-ctx.Done():
+ t.Fatal("preview did not connect")
+ case <-time.After(10 * time.Millisecond):
+ }
+ }
+ plan("preview alone", true, 1_000_000)
+ weak, weakReceiver, _ := connectedReceiver(t, engine, source, "weak")
+ defer weakReceiver.Close()
+ weak.transport.OnTransportCCFeedback(nil, &rtcp.TransportLayerCC{})
+ weak.transport.Output.SetBudget(100_000)
+ plan("preview does not mask weak child", true, 100_000)
+ healthy, healthyReceiver, _ := connectedReceiver(t, engine, source, "healthy")
+ defer healthyReceiver.Close()
+ healthy.transport.OnTransportCCFeedback(nil, &rtcp.TransportLayerCC{})
+ healthy.transport.Output.SetBudget(700_000)
+ plan("weak child does not lower healthy sibling", true, 700_000)
+ healthy.transport.Output.SetBudget(0)
+ plan("parked sibling", true, 100_000)
+ healthy.transport.Output.SetBudget(900_000)
+ plan("reactivated sibling", true, 900_000)
+ check(healthy.Close())
+ plan("retired sibling", true, 100_000)
+ check(weak.Close())
+ plan("preview remains", true, 1_000_000)
+ check(preview.Close())
+ plan("all consumers retired", false, 1_000_000)
+
+ publication, err := engine.NewPublication(source, EdgeOptions{ConnectionID: "single-publication"})
+ check(err)
+ check(publication.transport.SetConnected())
+ plan("SFU upload budget", true, 200_000)
+ check(publication.SetActiveCount(0))
+ plan("SFU upload parked", false, 1_000_000)
+ check(publication.SetActiveCount(1))
+ plan("SFU upload reactivated", true, 200_000)
+ check(source.ConfigureOutputs([]uint32{150_000}))
+ source.BeginGeneration()
+ pts = 0
+ plan("new generation ceiling", true, 150_000)
+ check(publication.Close())
+ plan("publication retired", false, 150_000)
+}
+
func TestSourceStartupFeedbackRequestsRecovery(t *testing.T) {
for _, relay := range []*RelayOptions{nil, {}} {
t.Run(fmt.Sprintf("relay-%t", relay != nil), func(t *testing.T) {
diff --git a/internal/app/nativecapture/protocol.go b/internal/app/nativecapture/protocol.go
index f9d3bbe6..ad69ab52 100644
--- a/internal/app/nativecapture/protocol.go
+++ b/internal/app/nativecapture/protocol.go
@@ -10,6 +10,9 @@ import (
"github.com/TNTcraftHIM/Piik/internal/media/encoded"
)
+// ErrSourceCanceled means the user dismissed the system source-selection dialog.
+var ErrSourceCanceled = errors.New("capture source selection canceled")
+
const (
envelopeHeaderBytes = 32
maxMediaBytes = encoded.MaxAccessUnitBytes
diff --git a/internal/app/nativecontrol/capture.go b/internal/app/nativecontrol/capture.go
new file mode 100644
index 00000000..c5cf698d
--- /dev/null
+++ b/internal/app/nativecontrol/capture.go
@@ -0,0 +1,47 @@
+package nativecontrol
+
+import (
+ "context"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativehost"
+ "github.com/TNTcraftHIM/Piik/internal/app/remoteinput"
+)
+
+// CaptureProvider supplies the platform capture operations used by the control
+// session. The control protocol and media routing do not depend on its runtime.
+type CaptureProvider interface {
+ nativehost.CaptureProvider
+ sourceProvider
+}
+
+type sourceProvider interface {
+ ListSources(context.Context) ([]nativecapture.CaptureTarget, error)
+ PreviewSource(context.Context, nativecapture.CaptureTarget) ([]byte, error)
+ ListMicrophones(context.Context) ([]nativecapture.Microphone, error)
+}
+
+type processSources struct{ executable string }
+
+func (p processSources) ListSources(ctx context.Context) ([]nativecapture.CaptureTarget, error) {
+ return nativecapture.ListSources(ctx, p.executable)
+}
+
+func (p processSources) PreviewSource(ctx context.Context, target nativecapture.CaptureTarget) ([]byte, error) {
+ return nativecapture.PreviewSource(ctx, p.executable, target)
+}
+
+func (p processSources) ListMicrophones(ctx context.Context) ([]nativecapture.Microphone, error) {
+ return nativecapture.ListMicrophones(ctx, p.executable)
+}
+
+// NewWithCapture uses an in-process platform producer instead of the desktop
+// capture executable. Capabilities still describe actual platform support.
+func NewWithCapture(
+ capabilities nativecapture.Capabilities,
+ portMapping bool,
+ remoteInput *remoteinput.Manager,
+ capture CaptureProvider,
+) *Session {
+ return newSession("", capabilities, portMapping, remoteInput, capture, capture)
+}
diff --git a/internal/app/nativecontrol/session.go b/internal/app/nativecontrol/session.go
index 34e06c31..0b33bf5a 100644
--- a/internal/app/nativecontrol/session.go
+++ b/internal/app/nativecontrol/session.go
@@ -41,6 +41,8 @@ var identityPattern = regexp.MustCompile("^[A-Za-z0-9_-]{8,256}$")
type Session struct {
captureProcess string
+ capture nativehost.CaptureProvider
+ sources sourceProvider
capabilities nativecapture.Capabilities
portMapping bool
remoteInput *remoteinput.Manager
@@ -72,10 +74,23 @@ func New(
capabilities nativecapture.Capabilities,
portMapping bool,
remoteInput *remoteinput.Manager,
+) *Session {
+ return newSession(captureProcess, capabilities, portMapping, remoteInput, nil, processSources{captureProcess})
+}
+
+func newSession(
+ captureProcess string,
+ capabilities nativecapture.Capabilities,
+ portMapping bool,
+ remoteInput *remoteinput.Manager,
+ capture nativehost.CaptureProvider,
+ sources sourceProvider,
) *Session {
ctx, cancel := context.WithCancel(context.Background())
session := &Session{
captureProcess: captureProcess,
+ capture: capture,
+ sources: sources,
capabilities: capabilities,
portMapping: portMapping,
remoteInput: remoteInput,
@@ -139,7 +154,7 @@ func (session *Session) Handle(ctx context.Context, payload []byte) (result any,
if !session.capabilities.Microphone {
return operationFailure(envelope, errors.New("native microphone is unavailable")), nil
}
- devices, err := nativecapture.ListMicrophones(ctx, session.captureProcess)
+ devices, err := session.sources.ListMicrophones(ctx)
if err != nil {
return operationFailure(envelope, err), nil
}
@@ -152,7 +167,7 @@ func (session *Session) Handle(ctx context.Context, payload []byte) (result any,
if err := decodeStrict(payload, &request); err != nil || request.Type != envelope.Type {
return nil, protocolViolation("native list-sources request is invalid")
}
- targets, err := nativecapture.ListSources(ctx, session.captureProcess)
+ targets, err := session.sources.ListSources(ctx)
if err != nil {
return operationFailure(envelope, err), nil
}
@@ -165,11 +180,7 @@ func (session *Session) Handle(ctx context.Context, payload []byte) (result any,
if err := decodeStrict(payload, &request); err != nil || request.Type != envelope.Type {
return nil, protocolViolation("native source-preview request is invalid")
}
- preview, err := nativecapture.PreviewSource(
- ctx,
- session.captureProcess,
- request.Source,
- )
+ preview, err := session.sources.PreviewSource(ctx, request.Source)
data := ""
if err == nil {
data = base64.StdEncoding.EncodeToString(preview)
@@ -204,6 +215,7 @@ func (session *Session) Handle(ctx context.Context, payload []byte) (result any,
slog.Debug("piik-client", "event", "native-profile-requested", "requestId", envelope.ID,
"share", diagnostics.ID(request.ShareID), "profile", nativeQualityProfile(request.Profile), "codec", request.Codec, "audio", request.Audio,
"sourceKind", request.Source.Kind, "adapterIndex", request.AdapterIndex, "encoderIndex", request.EncoderIndex)
+ envelope.reportSourceCancellation = request.ReportSourceCancellation
result = session.beginShare(ctx, envelope, request, complete)
asynchronous = result == nil
return result, nil
@@ -362,6 +374,7 @@ func (session *Session) Handle(ctx context.Context, payload []byte) (result any,
!validIdentities(request.ShareID) || !request.Source.Valid() {
return nil, protocolViolation("native replace-share-source request is invalid")
}
+ envelope.reportSourceCancellation = request.ReportSourceCancellation
if err := session.validateAudioExclusion(request.Source, request.Audio, request.ExcludeAudio); err != nil {
return nil, err
}
@@ -706,6 +719,7 @@ func (session *Session) startShare(
RemoteInput: input,
ShareID: request.ShareID,
CaptureProcess: session.captureProcess,
+ Capture: session.capture,
Video: nativecapture.VideoOptions{
Target: request.Source,
ShowCaptureBorder: request.ShowCaptureBorder && session.capabilities.CaptureBorderControl,
@@ -1219,9 +1233,13 @@ func operationFailure(request requestEnvelope, causes ...error) requestFailedRes
slog.Debug("piik-client", "event", "native-operation-failed", "requestId", request.ID,
"requestType", diagnostics.SafeText(request.Type), diagnostics.Error(causes[0]))
}
+ code := "operation-failed"
+ if request.reportSourceCancellation && len(causes) > 0 && errors.Is(causes[0], nativecapture.ErrSourceCanceled) {
+ code = "source-canceled"
+ }
return requestFailedResponse{
responseEnvelope: response(request, "request-failed"),
- Code: "operation-failed",
+ Code: code,
}
}
diff --git a/internal/app/nativecontrol/session_test.go b/internal/app/nativecontrol/session_test.go
index 1b5d50bb..700724b6 100644
--- a/internal/app/nativecontrol/session_test.go
+++ b/internal/app/nativecontrol/session_test.go
@@ -112,7 +112,7 @@ func TestRemoteStopShortcutRequiresAnOwnedShare(t *testing.T) {
if command == "set-remote-stop-shortcut" {
key = 9
}
- request := remoteStopShortcutRequest{requestEnvelope: requestEnvelope{10, "request_shortcut", command}, ShareID: "retired_share", Key: key}
+ request := remoteStopShortcutRequest{requestEnvelope: requestEnvelope{Version: 10, ID: "request_shortcut", Type: command}, ShareID: "retired_share", Key: key}
payload, _ := json.Marshal(request)
result, err := session.Handle(t.Context(), payload)
if _, ok := result.(requestFailedResponse); !ok || err != nil {
diff --git a/internal/app/nativecontrol/wire.go b/internal/app/nativecontrol/wire.go
index e8986e08..38837878 100644
--- a/internal/app/nativecontrol/wire.go
+++ b/internal/app/nativecontrol/wire.go
@@ -9,9 +9,10 @@ import (
)
type requestEnvelope struct {
- Version int `json:"version"`
- ID string `json:"id"`
- Type string `json:"type"`
+ reportSourceCancellation bool
+ Version int `json:"version"`
+ ID string `json:"id"`
+ Type string `json:"type"`
}
type responseEnvelope struct {
@@ -38,21 +39,22 @@ type sourcePreviewRequest struct {
type qualitySettings = protocol.QualitySettings
type startShareRequest struct {
- Version int `json:"version"`
- ID string `json:"id"`
- Type string `json:"type"`
- ShareID string `json:"shareId"`
- Source nativecapture.CaptureTarget `json:"source"`
- Audio bool `json:"audio"`
- MicrophoneMixing bool `json:"microphoneMixing,omitempty"`
- RemoteInput bool `json:"remoteInput,omitempty"`
- ExcludeAudio *nativecapture.CaptureTarget `json:"excludeAudio,omitempty"`
- ShowCaptureBorder bool `json:"showCaptureBorder,omitempty"`
- AdapterIndex uint32 `json:"adapterIndex"`
- EncoderIndex uint32 `json:"encoderIndex"`
- EdgeCapacity int `json:"edgeCapacity"`
- Profile qualitySettings `json:"profile"`
- Codec string `json:"codec"`
+ ReportSourceCancellation bool `json:"reportSourceCancellation,omitempty"`
+ Version int `json:"version"`
+ ID string `json:"id"`
+ Type string `json:"type"`
+ ShareID string `json:"shareId"`
+ Source nativecapture.CaptureTarget `json:"source"`
+ Audio bool `json:"audio"`
+ MicrophoneMixing bool `json:"microphoneMixing,omitempty"`
+ RemoteInput bool `json:"remoteInput,omitempty"`
+ ExcludeAudio *nativecapture.CaptureTarget `json:"excludeAudio,omitempty"`
+ ShowCaptureBorder bool `json:"showCaptureBorder,omitempty"`
+ AdapterIndex uint32 `json:"adapterIndex"`
+ EncoderIndex uint32 `json:"encoderIndex"`
+ EdgeCapacity int `json:"edgeCapacity"`
+ Profile qualitySettings `json:"profile"`
+ Codec string `json:"codec"`
}
type microphoneRequest struct {
@@ -128,16 +130,17 @@ type updateShareRequest struct {
}
type replaceShareSourceRequest struct {
- Version int `json:"version"`
- ID string `json:"id"`
- Type string `json:"type"`
- ShareID string `json:"shareId"`
- Source nativecapture.CaptureTarget `json:"source"`
- Audio bool `json:"audio"`
- ExcludeAudio *nativecapture.CaptureTarget `json:"excludeAudio,omitempty"`
- ShowCaptureBorder bool `json:"showCaptureBorder,omitempty"`
- AdapterIndex uint32 `json:"adapterIndex"`
- EncoderIndex uint32 `json:"encoderIndex"`
+ ReportSourceCancellation bool `json:"reportSourceCancellation,omitempty"`
+ Version int `json:"version"`
+ ID string `json:"id"`
+ Type string `json:"type"`
+ ShareID string `json:"shareId"`
+ Source nativecapture.CaptureTarget `json:"source"`
+ Audio bool `json:"audio"`
+ ExcludeAudio *nativecapture.CaptureTarget `json:"excludeAudio,omitempty"`
+ ShowCaptureBorder bool `json:"showCaptureBorder,omitempty"`
+ AdapterIndex uint32 `json:"adapterIndex"`
+ EncoderIndex uint32 `json:"encoderIndex"`
}
type stopShareRequest struct {
diff --git a/internal/app/nativehost/audio_mix.go b/internal/app/nativehost/audio_mix.go
index b4707c35..8b453b2a 100644
--- a/internal/app/nativehost/audio_mix.go
+++ b/internal/app/nativehost/audio_mix.go
@@ -21,7 +21,7 @@ const audioPeriod = 20 * time.Millisecond
// absorbs scheduling jitter; it never replays samples or builds a latency tail.
// It does not claim to eliminate long-term drift between hardware clocks.
type audioInput struct {
- stream *nativecapture.Stream
+ stream CaptureStream
frames [][]byte
}
@@ -64,7 +64,7 @@ func (mix *audioMix) stateLocked(failed bool) Event {
SourceAudio: mix.source != nil, Microphone: mix.microphone != nil, Failed: failed}
}
-func (mix *audioMix) setSource(stream *nativecapture.Stream) {
+func (mix *audioMix) setSource(stream CaptureStream) {
mix.eventMu.Lock()
defer mix.eventMu.Unlock()
mix.mu.Lock()
@@ -145,7 +145,7 @@ func (mix *audioMix) inputEnded(input *audioInput, microphone bool) {
mix.emit(state)
}
-func (mix *audioMix) setMicrophone(executable string, enabled *bool, gain *float64, deviceID *string) error {
+func (mix *audioMix) setMicrophone(capture CaptureProvider, enabled *bool, gain *float64, deviceID *string) error {
if deviceID != nil && !nativecapture.ValidDeviceID(*deviceID) {
return errors.New("microphone device is invalid")
}
@@ -169,10 +169,10 @@ func (mix *audioMix) setMicrophone(executable string, enabled *bool, gain *float
if unchanged {
return nil
}
- var stream *nativecapture.Stream
+ var stream CaptureStream
if wanted {
var err error
- stream, err = nativecapture.StartMicrophone(mix.ctx, executable, device)
+ stream, err = capture.Microphone(mix.ctx, device)
if err != nil {
return err
}
diff --git a/internal/app/nativehost/audio_mix_test.go b/internal/app/nativehost/audio_mix_test.go
index 2235e304..c23b6844 100644
--- a/internal/app/nativehost/audio_mix_test.go
+++ b/internal/app/nativehost/audio_mix_test.go
@@ -341,7 +341,7 @@ func TestAudioNotificationBackpressureDoesNotBlockPauseOrGain(t *testing.T) {
go func() {
mix.setPaused(true)
gain := 1.5
- if err := mix.setMicrophone("", nil, &gain, nil); err != nil {
+ if err := mix.setMicrophone(processCapture{}, nil, &gain, nil); err != nil {
t.Error(err)
}
close(controlled)
diff --git a/internal/app/nativehost/audio_test.go b/internal/app/nativehost/audio_test.go
index 8db318ad..40623420 100644
--- a/internal/app/nativehost/audio_test.go
+++ b/internal/app/nativehost/audio_test.go
@@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
+ "strconv"
"strings"
"sync/atomic"
"testing"
@@ -58,6 +59,14 @@ func runAudioRecoveryCapture() {
}
}
audio := microphone || (len(os.Args) > 1 && os.Args[1] == "--capture-audio")
+ startupGate := os.Getenv("PIIK_VIDEO_START_GATE")
+ if audio && !microphone && startupGate != "" {
+ if _, err := os.Stat(startupGate + ".active"); err != nil {
+ _ = os.WriteFile(startupGate+".early-audio", nil, 0600)
+ os.Exit(2)
+ }
+ _ = os.WriteFile(startupGate+".audio", []byte(os.Args[3]+":"+os.Args[4]), 0600)
+ }
if !audio && os.Getenv("PIIK_VIDEO_FAIL") == "1" {
_, _ = io.WriteString(os.Stderr, "stage=mft-set-output-type\nhresult=0xc00d36b4\ndetail=private capture target\n")
os.Exit(2)
@@ -74,15 +83,31 @@ func runAudioRecoveryCapture() {
<-stopped
return
}
- period := time.Second / 30
+ profile := audioRecoveryProfile()
+ if !audio {
+ for index := 1; index+1 < len(os.Args); index++ {
+ value, _ := strconv.ParseUint(os.Args[index+1], 10, 32)
+ switch os.Args[index] {
+ case "--width":
+ profile.Width = uint32(value)
+ case "--height":
+ profile.Height = uint32(value)
+ case "--fps":
+ profile.Framerate = uint32(value)
+ case "--bitrate":
+ profile.Bitrate = uint32(value)
+ }
+ }
+ }
+ period := time.Second / time.Duration(profile.Framerate)
status := []byte(`{"state":"active","audio":true}`)
if audio {
period = 20 * time.Millisecond
} else {
- outputs := nativecapture.ScreenShareOutputs(audioRecoveryProfile())
+ outputs := nativecapture.ScreenShareOutputs(profile)
outputs = append(outputs, outputs[0])
status, _ = json.Marshal(CaptureState{State: "active", Codec: "vp8",
- Width: 1280, Height: 720, FPS: 30, Outputs: outputs})
+ Width: profile.Width, Height: profile.Height, FPS: profile.Framerate, Outputs: outputs})
}
marker := os.Getenv("PIIK_AUDIO_WAIT_FOR_VIDEO")
if audio && marker != "" {
@@ -99,6 +124,36 @@ func runAudioRecoveryCapture() {
}
}
}
+ if !audio && startupGate != "" && os.Args[3] == "1" && profile.Width == audioRecoveryProfile().Width {
+ var starting CaptureState
+ _ = json.Unmarshal(status, &starting)
+ starting.State = "starting"
+ index := uint32(0)
+ starting.AdapterIndex = &index
+ starting.AdapterName, starting.AdapterIdentity = "Fixture", "fixture"
+ starting.EncoderName, starting.EncoderIdentity = "Fixture VP8", "fixture-vp8"
+ initial, _ := json.Marshal(starting)
+ write(nativecapture.Frame{Kind: nativecapture.FrameStatus, Data: initial})
+ ticker := time.NewTicker(5 * time.Millisecond)
+ defer ticker.Stop()
+ for {
+ if _, err := os.Stat(startupGate + ".fail"); err == nil {
+ _, _ = io.WriteString(os.Stderr, "stage=fixture-video-initialization\n")
+ os.Exit(2)
+ }
+ if _, err := os.Stat(startupGate + ".release"); err == nil {
+ break
+ }
+ select {
+ case <-stopped:
+ return
+ case <-ticker.C:
+ }
+ }
+ }
+ if !audio && startupGate != "" {
+ _ = os.WriteFile(startupGate+".active", nil, 0600)
+ }
write(nativecapture.Frame{Kind: nativecapture.FrameStatus, Data: status})
if !audio && marker != "" {
// Exceed the OS pipe buffer before allowing audio initialization to finish.
@@ -134,6 +189,205 @@ func runAudioRecoveryCapture() {
}
}
+func TestMixedSourceAudioWaitsForActiveVideoWithoutBlockingStart(t *testing.T) {
+ t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")
+ gate := filepath.Join(t.TempDir(), "video")
+ t.Setenv("PIIK_VIDEO_START_GATE", gate)
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
+ defer cancel()
+ events := make(chan Event, 32)
+ video := nativecapture.VideoOptions{Codec: "vp8", Profile: audioRecoveryProfile(),
+ Target: nativecapture.CaptureTarget{Kind: "window", SourceID: "1", PID: 42, CreationTime: "1", Title: "Fixture"}}
+ session, err := Start(ctx, Options{ShareID: "staged-audio", CaptureProcess: executable,
+ Video: video, Profile: QualityProfile{Video: video.Profile, AudioBitrate: 64_000},
+ AudioEnabled: true, MicrophoneMixing: true, EdgeCapacity: 1, BindAddress: "127.0.0.1:0",
+ Events: func(_ context.Context, event Event) { events <- event }})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer session.Close()
+ if _, err := os.Stat(gate + ".early-audio"); err == nil {
+ t.Fatal("source audio initialized before video became active")
+ }
+ if !session.HasAudio() || session.SourceAudio() == nil || *session.SourceAudio() {
+ t.Fatal("pending source audio changed the mixed output shape or claimed readiness")
+ }
+ if err := os.WriteFile(gate+".release", nil, 0600); err != nil {
+ t.Fatal(err)
+ }
+ for {
+ select {
+ case event := <-events:
+ if event.Type == "audio-state" && event.SourceAudio {
+ if _, err := os.Stat(gate + ".audio"); err != nil {
+ t.Fatal("source audio did not initialize after active video")
+ }
+ return
+ }
+ case <-ctx.Done():
+ t.Fatal("active video did not resume pending source audio")
+ }
+ }
+}
+
+func TestPendingSourceAudioRetiresWithVideoOrLaterSelection(t *testing.T) {
+ t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, finish := range []string{"stop", "video-failure", "audio-off", "source-replacement", "audio-profile", "video-profile", "audio-on"} {
+ t.Run(finish, func(t *testing.T) {
+ gate := filepath.Join(t.TempDir(), "video")
+ t.Setenv("PIIK_VIDEO_START_GATE", gate)
+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
+ defer cancel()
+ events := make(chan Event, 32)
+ video := nativecapture.VideoOptions{Codec: "vp8", Profile: audioRecoveryProfile(),
+ Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}}
+ session, err := Start(ctx, Options{ShareID: "pending-audio", CaptureProcess: executable,
+ Video: video, Profile: QualityProfile{Video: video.Profile, AudioBitrate: 64_000},
+ AudioEnabled: true, MicrophoneMixing: true, EdgeCapacity: 1, BindAddress: "127.0.0.1:0",
+ Events: func(_ context.Context, event Event) { events <- event }})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer session.Close()
+ completed := make(chan error, 1)
+ switch finish {
+ case "stop":
+ go func() { completed <- session.Close() }()
+ case "video-failure":
+ if err := os.WriteFile(gate+".fail", nil, 0600); err != nil {
+ t.Fatal(err)
+ }
+ go func() { err := <-session.Done(); completed <- err }()
+ case "audio-off", "source-replacement":
+ next := video
+ if finish == "source-replacement" {
+ next.Target = nativecapture.CaptureTarget{Kind: "window", SourceID: "2", PID: 99, CreationTime: "2", Title: "Next fixture"}
+ }
+ go func() { completed <- session.ReplaceSource(next, false, nil) }()
+ case "audio-profile":
+ profile := QualityProfile{Video: video.Profile, AudioBitrate: 32_000}
+ go func() { completed <- session.UpdateProfile(profile) }()
+ case "video-profile":
+ profile := QualityProfile{Video: video.Profile, AudioBitrate: 64_000}
+ profile.Video.Width, profile.Video.Height = 640, 360
+ go func() { completed <- session.UpdateProfile(profile) }()
+ case "audio-on":
+ go func() { completed <- session.ReplaceSource(video, true, nil) }()
+ }
+ select {
+ case err := <-completed:
+ if (err != nil) != (finish == "video-failure" || finish == "audio-on") {
+ t.Fatalf("unexpected %s result: %v", finish, err)
+ }
+ case <-ctx.Done():
+ t.Fatal("pending audio blocked", finish)
+ }
+ if _, err := os.Stat(gate + ".early-audio"); err == nil {
+ t.Fatal("source audio initialized before active video")
+ }
+ if finish == "audio-off" || finish == "source-replacement" {
+ if session.SourceAudio() == nil || *session.SourceAudio() || session.currentAudioStream() != nil {
+ t.Fatal("late initial audio overwrote the newer source-audio selection")
+ }
+ } else if finish != "video-profile" {
+ if _, err := os.Stat(gate + ".audio"); err == nil {
+ t.Fatal("retired video still started source audio")
+ }
+ }
+ if finish == "video-profile" {
+ for {
+ select {
+ case event := <-events:
+ if event.Type == "audio-state" && event.SourceAudio {
+ return
+ }
+ case <-ctx.Done():
+ t.Fatal("quality replacement lost pending source audio")
+ }
+ }
+ }
+ })
+ }
+}
+
+func TestFixedSourceAudioWaitsForActiveVideoAndCanCancel(t *testing.T) {
+ t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, cancelStartup := range []bool{false, true} {
+ t.Run(map[bool]string{false: "active", true: "cancelled"}[cancelStartup], func(t *testing.T) {
+ gate := filepath.Join(t.TempDir(), "video")
+ t.Setenv("PIIK_VIDEO_START_GATE", gate)
+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
+ defer cancel()
+ events := make(chan Event, 16)
+ type result struct {
+ session *Session
+ err error
+ }
+ started := make(chan result, 1)
+ go func() {
+ video := nativecapture.VideoOptions{Codec: "vp8", Profile: audioRecoveryProfile(),
+ Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}}
+ session, err := Start(ctx, Options{ShareID: "fixed-audio", CaptureProcess: executable,
+ Video: video, Profile: QualityProfile{Video: video.Profile, AudioBitrate: 64_000},
+ AudioEnabled: true, EdgeCapacity: 1, BindAddress: "127.0.0.1:0",
+ Events: func(_ context.Context, event Event) { events <- event }})
+ started <- result{session, err}
+ }()
+ select {
+ case event := <-events:
+ if event.Type != "capture-state" || event.State != "starting" {
+ t.Fatal("missing video startup progress", event)
+ }
+ case <-ctx.Done():
+ t.Fatal("video startup progress did not arrive")
+ }
+ select {
+ case result := <-started:
+ if result.session != nil {
+ _ = result.session.Close()
+ }
+ t.Fatal("fixed audio shape committed before video became active", result.err)
+ default:
+ }
+ if cancelStartup {
+ cancel()
+ } else if err := os.WriteFile(gate+".release", nil, 0600); err != nil {
+ t.Fatal(err)
+ }
+ select {
+ case result := <-started:
+ if result.session != nil {
+ defer result.session.Close()
+ }
+ if cancelStartup {
+ if result.err == nil || result.session != nil {
+ t.Fatal("cancelled startup admitted a share")
+ }
+ } else if result.err != nil || result.session == nil || !result.session.HasAudio() {
+ t.Fatal("fixed source audio was not admitted", result.err)
+ }
+ case <-time.After(2 * time.Second):
+ t.Fatal("source audio did not finish startup or cancellation")
+ }
+ if _, err := os.Stat(gate + ".early-audio"); err == nil {
+ t.Fatal("source audio initialized before active video")
+ }
+ })
+ }
+}
+
func TestCaptureStartupReturnsNativeFailureCodesWithoutPrivateDetail(t *testing.T) {
t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")
t.Setenv("PIIK_VIDEO_FAIL", "1")
diff --git a/internal/app/nativehost/capture.go b/internal/app/nativehost/capture.go
new file mode 100644
index 00000000..8c328c0f
--- /dev/null
+++ b/internal/app/nativehost/capture.go
@@ -0,0 +1,48 @@
+package nativehost
+
+import (
+ "context"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+)
+
+// CaptureStream is owned by one installed capture generation. Android supplies
+// frames in-process; desktop capture retains its supervised process adapter.
+type CaptureStream interface {
+ Read() (nativecapture.Frame, error)
+ Close() error
+ Outputs() []nativecapture.OutputProfile
+ RequestKeyFrame(int) error
+ SetOutputActive(int, bool) error
+ SetOutputBitrate(int, uint32) error
+}
+
+type CaptureProvider interface {
+ Video(context.Context, nativecapture.VideoOptions) (CaptureStream, error)
+ Audio(context.Context, nativecapture.CaptureTarget, *nativecapture.CaptureTarget) (CaptureStream, error)
+ Microphone(context.Context, string) (CaptureStream, error)
+}
+
+type processCapture struct{ executable string }
+
+func (p processCapture) Video(ctx context.Context, options nativecapture.VideoOptions) (CaptureStream, error) {
+ stream, err := nativecapture.StartVideo(ctx, p.executable, options)
+ if err != nil {
+ return nil, err
+ }
+ return stream, nil
+}
+func (p processCapture) Audio(ctx context.Context, target nativecapture.CaptureTarget, exclude *nativecapture.CaptureTarget) (CaptureStream, error) {
+ stream, err := startAudioCapture(ctx, p.executable, target, exclude)
+ if err != nil {
+ return nil, err
+ }
+ return stream, nil
+}
+func (p processCapture) Microphone(ctx context.Context, device string) (CaptureStream, error) {
+ stream, err := nativecapture.StartMicrophone(ctx, p.executable, device)
+ if err != nil {
+ return nil, err
+ }
+ return stream, nil
+}
diff --git a/internal/app/nativehost/publication.go b/internal/app/nativehost/publication.go
index 7dee87e8..32a5ce3b 100644
--- a/internal/app/nativehost/publication.go
+++ b/internal/app/nativehost/publication.go
@@ -36,7 +36,7 @@ func (session *Session) PreparePublication(generation, connectionID string, serv
PublicationGeneration: generation, ConnectionID: connectionID, Candidate: candidate})
},
ConnectionState: func(state webrtc.PeerConnectionState, _ *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {
- if state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed {
+ if current() && isCurrentState(state) && (state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed) {
session.remoteInput.RevokeEdge(connectionID)
}
session.emit(Event{Type: "publication-state", ShareID: session.shareID,
diff --git a/internal/app/nativehost/session.go b/internal/app/nativehost/session.go
index 22525206..e7ce8502 100644
--- a/internal/app/nativehost/session.go
+++ b/internal/app/nativehost/session.go
@@ -67,6 +67,7 @@ type Event struct {
type Options struct {
ShareID string
CaptureProcess string
+ Capture CaptureProvider
Video nativecapture.VideoOptions
Profile QualityProfile
AudioEnabled bool
@@ -80,23 +81,24 @@ type Options struct {
}
type Session struct {
- shareID string
- captureProcess string
- videoOptions nativecapture.VideoOptions
- profile QualityProfile
- edgeCapacity int
- stream *nativecapture.Stream
- audioStream *nativecapture.Stream
- engine *mediaedge.Engine
- source *mediaedge.Source
- audioSource *mediaedge.AudioSource
- mixer *audioMix
- events func(context.Context, Event)
+ shareID string
+ capture CaptureProvider
+ videoOptions nativecapture.VideoOptions
+ profile QualityProfile
+ edgeCapacity int
+ stream CaptureStream
+ audioStream CaptureStream
+ engine *mediaedge.Engine
+ source *mediaedge.Source
+ audioSource *mediaedge.AudioSource
+ mixer *audioMix
+ events func(context.Context, Event)
- ctx context.Context
- cancel context.CancelFunc
- done chan error
- ready chan error
+ ctx context.Context
+ cancel context.CancelFunc
+ done chan error
+ ready chan error
+ videoActive chan struct{}
mu sync.Mutex
updateMu sync.Mutex
@@ -135,34 +137,47 @@ func Start(parent context.Context, options Options) (*Session, error) {
return nil, err
}
options.Video.OutputGroups = min(options.EdgeCapacity, nativecapture.MaxOutputGroups)
- stream, err := nativecapture.StartVideo(parent, options.CaptureProcess, options.Video)
+ capture := options.Capture
+ if capture == nil {
+ capture = processCapture{options.CaptureProcess}
+ }
+ stream, err := capture.Video(parent, options.Video)
if err != nil {
_ = engine.Close()
return nil, err
}
+ preparing := stream
+ defer func() { capturePreparationDone(preparing) }()
ctx, cancel := context.WithCancel(parent)
session := &Session{
- shareID: options.ShareID,
- captureProcess: options.CaptureProcess,
- videoOptions: options.Video,
- profile: options.Profile,
- edgeCapacity: options.EdgeCapacity,
- stream: stream,
- engine: engine,
- events: options.Events,
- ctx: ctx,
- cancel: cancel,
- done: make(chan error, 1),
- ready: make(chan error, 1),
- edges: make(map[string]*mediaedge.Edge),
- audioChanged: make(chan struct{}, 1),
- remoteInput: options.RemoteInput.NewSession(),
+ shareID: options.ShareID,
+ capture: capture,
+ videoOptions: options.Video,
+ profile: options.Profile,
+ edgeCapacity: options.EdgeCapacity,
+ stream: stream,
+ engine: engine,
+ events: options.Events,
+ ctx: ctx,
+ cancel: cancel,
+ done: make(chan error, 1),
+ ready: make(chan error, 1),
+ videoActive: make(chan struct{}),
+ edges: make(map[string]*mediaedge.Edge),
+ audioChanged: make(chan struct{}, 1),
+ remoteInput: options.RemoteInput.NewSession(),
}
session.syncRemoteInputSource(nil)
// Drain video while the optional audio process initializes. Otherwise a full
// video pipe can stall the platform capture service that audio also needs.
videoDone := make(chan error, 1)
- go func() { videoDone <- session.runVideo() }()
+ // Source audio may still be awaiting platform permission when video ends.
+ // Its context lives with video, including after a successful start.
+ audioCtx, cancelAudio := context.WithCancel(ctx)
+ go func() {
+ videoDone <- session.runVideo()
+ cancelAudio()
+ }()
var timeout <-chan time.Time
if options.Video.Target.Kind != "picker" {
timer := time.NewTimer(startTimeout)
@@ -183,17 +198,28 @@ func Start(parent context.Context, options Options) (*Session, error) {
session.retire()
return nil, errors.Join(err, captureErr)
}
- // Start the optional audio helper only after video initialization, avoiding
- // concurrent platform source enumeration while continuing to drain video.
- var audioStream *nativecapture.Stream
- if options.AudioEnabled {
+ // Legacy pages choose a fixed audio shape in the start response. Mixed audio
+ // already has a stable output, so its optional input can attach after return.
+ var audioStream CaptureStream
+ if options.AudioEnabled && !options.MicrophoneMixing {
var audioErr error
- audioStream, audioErr = startAudioCapture(ctx, options.CaptureProcess, options.Video.Target, options.ExcludeAudio)
+ audioStream, audioErr = session.startSourceAudio(audioCtx, options.Video.Target, options.ExcludeAudio)
if audioErr != nil {
slog.DebugContext(ctx, "piik-client", "event", "capture-audio-unavailable", "share", diagnostics.ID(options.ShareID), diagnostics.Error(audioErr))
}
session.audioStream = audioStream
}
+ select {
+ case videoErr := <-videoDone:
+ cancel()
+ _ = stream.Close()
+ if audioStream != nil {
+ _ = audioStream.Close()
+ }
+ session.retire()
+ return nil, videoErr
+ default:
+ }
if audioStream != nil || options.MicrophoneMixing {
audioSource, audioErr := engine.NewAudioSource(
mediaedge.PhysicalSourceCapacity(options.EdgeCapacity), options.Profile.AudioBitrate,
@@ -220,7 +246,46 @@ func Start(parent context.Context, options Options) (*Session, error) {
session.mixer = &audioMix{ctx: ctx, output: session.audioSource, emit: session.emit, shareID: session.shareID, gain: 1}
session.mixer.setSource(audioStream)
}
- go session.run(videoDone)
+ var audioStarting chan struct{}
+ if options.AudioEnabled && session.mixer != nil {
+ audioStarting = make(chan struct{})
+ preparing = nil // The input startup now owns the interactive preparation.
+ go func() {
+ defer close(audioStarting)
+ defer capturePreparationDone(stream)
+ // Waiting for the platform must not hold up a source or quality change.
+ // A later source-audio commit supersedes this initial input request.
+ select {
+ case <-session.videoActive:
+ case <-session.audioChanged:
+ return
+ case <-audioCtx.Done():
+ return
+ }
+ session.updateMu.Lock()
+ defer session.updateMu.Unlock()
+ select {
+ case <-session.audioChanged:
+ return
+ default:
+ }
+ input, audioErr := session.startSourceAudio(audioCtx, options.Video.Target, options.ExcludeAudio)
+ if audioErr != nil {
+ slog.DebugContext(ctx, "piik-client", "event", "capture-audio-unavailable", "share", diagnostics.ID(options.ShareID), diagnostics.Error(audioErr))
+ return
+ }
+ session.mu.Lock()
+ if audioCtx.Err() != nil || session.closed {
+ session.mu.Unlock()
+ _ = input.Close()
+ return
+ }
+ session.audioStream = input
+ session.mu.Unlock()
+ session.mixer.setSource(input)
+ }()
+ }
+ go session.run(videoDone, audioStarting)
if err := ctx.Err(); err != nil {
_ = session.Close()
return nil, err
@@ -281,6 +346,12 @@ func (session *Session) prepareEdge(
return webrtc.SessionDescription{}, errors.New("native media edge is unavailable")
}
session.mu.Unlock()
+ var edge *mediaedge.Edge
+ current := func() bool {
+ session.mu.Lock()
+ defer session.mu.Unlock()
+ return !session.closed && edge != nil && session.edges[connectionID] == edge
+ }
created, err := session.engine.NewEdge(session.source, mediaedge.EdgeOptions{
ConnectionID: connectionID,
ICEServers: iceServers,
@@ -290,22 +361,23 @@ func (session *Session) prepareEdge(
Events: mediaedge.EdgeEvents{
LocalCandidate: func(candidate *webrtc.ICECandidateInit) {
session.emit(Event{
- Type: "edge-candidate", ShareID: session.shareID,
+ Current: current,
+ Type: "edge-candidate", ShareID: session.shareID,
ConnectionID: connectionID, Candidate: candidate,
})
},
ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {
- if state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed {
+ if current() && isCurrentState(state) && (state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed) {
session.remoteInput.RevokeEdge(connectionID)
}
session.emit(Event{
- Current: func() bool { return isCurrentState(state) },
+ Current: func() bool { return current() && isCurrentState(state) },
Type: "edge-state", ShareID: session.shareID,
ConnectionID: connectionID, State: state.String(),
})
if pair != nil {
session.emit(Event{
- Current: func() bool { return isCurrentState(state) },
+ Current: func() bool { return current() && isCurrentState(state) },
Type: "edge-path", ShareID: session.shareID,
ConnectionID: connectionID,
LocalType: pair.Local.String(),
@@ -319,13 +391,13 @@ func (session *Session) prepareEdge(
if err != nil {
return webrtc.SessionDescription{}, err
}
- edge := created
session.mu.Lock()
if session.closed || session.edges[connectionID] != nil {
session.mu.Unlock()
- _ = edge.Close()
+ _ = created.Close()
return webrtc.SessionDescription{}, errors.New("native media edge is unavailable")
}
+ edge = created
session.edges[connectionID] = edge
session.mu.Unlock()
var offer webrtc.SessionDescription
@@ -427,6 +499,7 @@ func (session *Session) UpdateProfile(profile QualityProfile) error {
if err != nil {
return err
}
+ defer capturePreparationDone(replacement)
if session.audioSource != nil && profile.AudioBitrate != previousProfile.AudioBitrate {
if err = session.audioSource.SetBitrate(profile.AudioBitrate); err != nil {
_ = replacement.Close()
@@ -472,11 +545,12 @@ func (session *Session) ReplaceSource(
if session.mixer == nil && audioEnabled != hasAudio {
return errors.New("native source audio availability cannot change while sharing")
}
- if !audioOnly {
- // Suspend input while changing capture. On success bind the committed
- // target; on failure the previous picture remains eligible for a new grant.
- session.remoteInput.SetTarget(nativecapture.CaptureTarget{}, nil)
- defer session.syncRemoteInputSource(nil)
+ if audioEnabled && session.videoActive != nil {
+ select {
+ case <-session.videoActive:
+ default:
+ return errors.New("native source audio is unavailable until video becomes active")
+ }
}
if excludeAudio != nil {
if session.mixer == nil {
@@ -495,30 +569,36 @@ func (session *Session) ReplaceSource(
}
options.Profile = profile.Video
options.RestoreToken = ""
- var replacement *nativecapture.Stream
+ var replacement CaptureStream
var state CaptureState
var err error
- if !audioOnly {
- replacement, state, err = session.prepareVideo(session.ctx, options, options.Target.Kind == "picker", false)
- if err != nil {
- return err
- }
- }
- var replacementAudio *nativecapture.Stream
+ // Admit audio before candidate video can fill its unread queue and block a
+ // platform handler that also starts audio. The current picture stays drained.
+ var replacementAudio CaptureStream
if audioEnabled {
- replacementAudio, err = startAudioCapture(
+ replacementAudio, err = session.startSourceAudio(
session.ctx,
- session.captureProcess,
options.Target,
excludeAudio,
)
if err != nil {
- if replacement != nil {
- _ = replacement.Close()
- }
return errors.New("native source audio could not start")
}
}
+ if !audioOnly {
+ replacement, state, err = session.prepareVideo(session.ctx, options, options.Target.Kind == "picker", false)
+ if err != nil {
+ if replacementAudio != nil {
+ _ = replacementAudio.Close()
+ }
+ return err
+ }
+ defer capturePreparationDone(replacement)
+ // Preparation still belongs to the old source. Retire its authorization
+ // only when the replacement is ready to enter the capture transaction.
+ session.remoteInput.SetTarget(nativecapture.CaptureTarget{}, nil)
+ defer session.syncRemoteInputSource(nil)
+ }
return session.commitCapture(
options,
profile,
@@ -564,6 +644,7 @@ func (session *Session) PrepareRemoteInputPolicy(mode, password string, coPlay b
if err != nil {
return err
}
+ defer capturePreparationDone(replacement)
if err := change.Context().Err(); err != nil {
_ = replacement.Close()
return err
@@ -592,7 +673,7 @@ func (session *Session) SetRemoteInputSimultaneous(enabled bool) error {
func (session *Session) syncRemoteInputSource(change *remoteinput.PolicyChange) {
session.mu.Lock()
target := session.videoOptions.Target
- capture := session.stream
+ capture, _ := session.stream.(*nativecapture.Stream)
session.mu.Unlock()
if change != nil {
change.SetTarget(target, capture)
@@ -616,21 +697,48 @@ func startAudioCapture(
return nativecapture.StartAudio(ctx, captureProcess, target, false)
}
+func (session *Session) startSourceAudio(
+ ctx context.Context,
+ target nativecapture.CaptureTarget,
+ excludeAudio *nativecapture.CaptureTarget,
+) (CaptureStream, error) {
+ // A starting status admits control and progress; only active proves that
+ // platform video initialization finished. Keep draining video during this wait.
+ if session.videoActive != nil {
+ select {
+ case <-session.videoActive:
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ }
+ }
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ return session.capture.Audio(ctx, target, excludeAudio)
+}
+
+// Interactive platforms retain their preparation UI until the whole operation,
+// including source audio and capture commit, has finished.
+func capturePreparationDone(stream CaptureStream) {
+ if preparation, ok := stream.(interface{ PreparationDone() }); ok {
+ preparation.PreparationDone()
+ }
+}
+
func (session *Session) prepareVideo(
ctx context.Context,
options nativecapture.VideoOptions,
interactive bool,
allowQuiet bool,
-) (*nativecapture.Stream, CaptureState, error) {
+) (CaptureStream, CaptureState, error) {
if err := ctx.Err(); err != nil {
return nil, CaptureState{}, err
}
options.OutputGroups = min(session.edgeCapacity, nativecapture.MaxOutputGroups)
// Preparation can be cancelled independently. Installed capture always lives
// with the share; cancelling a permission request must not kill its process.
- replacement, err := nativecapture.StartVideo(
+ replacement, err := session.capture.Video(
session.ctx,
- session.captureProcess,
options,
)
if err != nil {
@@ -658,9 +766,9 @@ func (session *Session) prepareVideo(
func (session *Session) commitCapture(
options nativecapture.VideoOptions,
profile QualityProfile,
- replacement *nativecapture.Stream,
+ replacement CaptureStream,
state CaptureState,
- replacementAudio *nativecapture.Stream,
+ replacementAudio CaptureStream,
replaceAudio bool,
inputChange *remoteinput.PolicyChange,
) error {
@@ -734,7 +842,7 @@ func (session *Session) commitCapture(
// Only the video reader installs source metadata, after leaving the old input.
// The update response waits for this exact stream's installation, not its process.
-func (session *Session) installCapture(next *nativecapture.Stream) error {
+func (session *Session) installCapture(next CaptureStream) error {
session.mu.Lock()
unavailable := session.captureUnavailable
session.mu.Unlock()
@@ -760,7 +868,7 @@ func (session *Session) installCapture(next *nativecapture.Stream) error {
return nil
}
-func (session *Session) disableCaptureLayer(stream *nativecapture.Stream, layer int) error {
+func (session *Session) disableCaptureLayer(stream CaptureStream, layer int) error {
session.mu.Lock()
if session.stream != stream || session.closed || session.ctx.Err() != nil {
session.mu.Unlock()
@@ -794,16 +902,19 @@ func (session *Session) Done() <-chan error {
func (session *Session) Close() error {
session.cancel()
session.updateMu.Lock()
- defer session.updateMu.Unlock()
session.mu.Lock()
if session.closed {
session.mu.Unlock()
+ session.updateMu.Unlock()
_, _ = <-session.done
return nil
}
session.closed = true
session.edges = make(map[string]*mediaedge.Edge)
session.mu.Unlock()
+ // Closed/cancelled rejects further updates. Pending startup must be able to
+ // acquire the update lock and finish before run can complete retirement.
+ session.updateMu.Unlock()
session.retire()
_, _ = <-session.done
return nil
@@ -839,7 +950,7 @@ func (session *Session) edge(connectionID string) *mediaedge.Edge {
return session.edges[connectionID]
}
-func (session *Session) run(videoDone <-chan error) {
+func (session *Session) run(videoDone <-chan error, audioStarting <-chan struct{}) {
audioDone := make(chan struct{})
qualityDone := make(chan struct{})
go func() {
@@ -863,10 +974,13 @@ func (session *Session) run(videoDone <-chan error) {
session.mu.Unlock()
if closed || session.ctx.Err() != nil {
// Closing this owner or its parent is a clean end, even if stopping the
- // capture process made the video reader return an error.
+ // capture made the video reader return an error.
result = nil
}
session.cancel()
+ if audioStarting != nil {
+ <-audioStarting
+ }
session.mu.Lock()
audioStream := session.audioStream
session.mu.Unlock()
@@ -876,8 +990,12 @@ func (session *Session) run(videoDone <-chan error) {
<-audioDone
<-qualityDone
session.retire()
- if slog.Default().Enabled(session.ctx, slog.LevelDebug) {
- slog.Debug("piik-client", "event", "share-ended", "share", diagnostics.ID(session.shareID), "failed", result != nil, diagnostics.Error(result))
+ level := slog.LevelDebug
+ if result != nil && !errors.Is(result, nativecapture.ErrSourceCanceled) {
+ level = slog.LevelWarn
+ }
+ if slog.Default().Enabled(session.ctx, level) {
+ slog.Log(session.ctx, level, "piik-client", "event", "share-ended", "share", diagnostics.ID(session.shareID), "failed", result != nil, diagnostics.Error(result))
}
session.done <- result
close(session.done)
@@ -886,6 +1004,12 @@ func (session *Session) run(videoDone <-chan error) {
func (session *Session) runVideo() error {
ready := false
activeSeen := false
+ markActive := func() {
+ if !activeSeen && session.videoActive != nil {
+ close(session.videoActive)
+ }
+ activeSeen = true
+ }
fail := func(err error) error {
if !ready {
session.ready <- err
@@ -893,13 +1017,13 @@ func (session *Session) runVideo() error {
return err
}
current := session.currentStream()
- changeStream := func(next *nativecapture.Stream) error {
+ changeStream := func(next CaptureStream) error {
if err := session.installCapture(next); err != nil {
return err
}
current = next
// prepareVideo consumed and verified this replacement's active status.
- activeSeen = true
+ markActive()
return nil
}
for current != nil {
@@ -914,7 +1038,10 @@ func (session *Session) runVideo() error {
}
slog.DebugContext(session.ctx, "piik-client", "event", "capture-video-read-ended",
"share", diagnostics.ID(session.shareID), "canceled", session.ctx.Err() != nil, "eof", errors.Is(err, io.EOF), diagnostics.Error(err))
- return fail(errors.New("native capture process stopped unexpectedly"))
+ if errors.Is(err, nativecapture.ErrSourceCanceled) {
+ return fail(err)
+ }
+ return fail(errors.New("native capture stopped unexpectedly"))
}
if next := session.currentStream(); next != current {
if next != nil {
@@ -970,7 +1097,7 @@ func (session *Session) runVideo() error {
return fail(errors.New("native capture codec changed within a share"))
}
if status.State == "active" {
- activeSeen = true
+ markActive()
if status.RestoreToken != "" {
session.videoOptions.RestoreToken = status.RestoreToken
}
@@ -1040,7 +1167,7 @@ func (session *Session) runVideo() error {
return fail(errors.New("native video process emitted an unknown frame"))
}
}
- return fail(errors.New("native capture process stopped unexpectedly"))
+ return fail(errors.New("native capture stopped unexpectedly"))
}
func configureCaptureOutputs(source *mediaedge.Source, outputs []nativecapture.OutputProfile) error {
@@ -1065,7 +1192,7 @@ func configureCaptureOutputs(source *mediaedge.Source, outputs []nativecapture.O
return nil
}
-func applyOutputPlan(stream *nativecapture.Stream, plan mediaedge.OutputPlan, activeSeen bool) error {
+func applyOutputPlan(stream CaptureStream, plan mediaedge.OutputPlan, activeSeen bool) error {
for layer, bitrate := range plan.Bitrates {
if bitrate > 0 {
if err := stream.SetOutputBitrate(layer, bitrate); err != nil {
@@ -1090,7 +1217,7 @@ func applyOutputPlan(stream *nativecapture.Stream, plan mediaedge.OutputPlan, ac
return nil
}
-func (session *Session) currentStream() *nativecapture.Stream {
+func (session *Session) currentStream() CaptureStream {
session.mu.Lock()
defer session.mu.Unlock()
if session.closed {
@@ -1107,7 +1234,7 @@ func captureProfileFailure(ctx context.Context, profile nativecapture.VideoProfi
func waitForCaptureProfile(
ctx context.Context,
- stream *nativecapture.Stream,
+ stream CaptureStream,
profile nativecapture.VideoProfile,
codec string,
interactive bool,
@@ -1139,7 +1266,10 @@ func waitForCaptureProfile(
for {
frame, err := stream.Read()
if err != nil {
- send(result{stage: "process-ended", err: errors.New("native capture profile did not become ready")})
+ if !errors.Is(err, nativecapture.ErrSourceCanceled) {
+ err = errors.New("native capture profile did not become ready")
+ }
+ send(result{stage: "process-ended", err: err})
return
}
if frame.Kind == nativecapture.FrameLayerUnavailable {
@@ -1306,7 +1436,7 @@ func (session *Session) runAudio() {
}
}
-func (session *Session) currentAudioStream() *nativecapture.Stream {
+func (session *Session) currentAudioStream() CaptureStream {
session.mu.Lock()
defer session.mu.Unlock()
if session.closed {
@@ -1390,7 +1520,7 @@ func (session *Session) SetMicrophone(enabled *bool, gain *float64, deviceID *st
if session.mixer == nil || session.ctx.Err() != nil {
return errors.New("native microphone is unavailable")
}
- return session.mixer.setMicrophone(session.captureProcess, enabled, gain, deviceID)
+ return session.mixer.setMicrophone(session.capture, enabled, gain, deviceID)
}
func (session *Session) SetVolume(volume float64) error {
diff --git a/internal/app/nativehost/session_test.go b/internal/app/nativehost/session_test.go
index 490ac17b..ce34e6c7 100644
--- a/internal/app/nativehost/session_test.go
+++ b/internal/app/nativehost/session_test.go
@@ -8,6 +8,7 @@ import (
"io"
"log/slog"
"os"
+ "path/filepath"
"slices"
"strconv"
"strings"
@@ -46,6 +47,144 @@ func TestMain(tests *testing.M) {
os.Exit(tests.Run())
}
+type retiringCapture struct {
+ processCapture
+ entered, release chan struct{}
+}
+
+func (capture retiringCapture) Video(ctx context.Context, options nativecapture.VideoOptions) (CaptureStream, error) {
+ stream, err := capture.processCapture.Video(ctx, options)
+ if err != nil {
+ return nil, err
+ }
+ return &retiringCaptureStream{CaptureStream: stream, entered: capture.entered, release: capture.release}, nil
+}
+
+type retiringCaptureStream struct {
+ CaptureStream
+ entered, release chan struct{}
+ once sync.Once
+}
+
+func (stream *retiringCaptureStream) Close() error {
+ stream.once.Do(func() { close(stream.entered) })
+ <-stream.release
+ return stream.CaptureStream.Close()
+}
+
+func TestCloseReleasesUpdateLockBeforeWaitingForCaptureRetirement(t *testing.T) {
+ t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")
+ t.Setenv("PIIK_VIDEO_START_GATE", filepath.Join(t.TempDir(), "video"))
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ entered, release := make(chan struct{}), make(chan struct{})
+ resume := sync.OnceFunc(func() { close(release) })
+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
+ defer cancel()
+ video := nativecapture.VideoOptions{Codec: "vp8", Profile: audioRecoveryProfile(),
+ Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}}
+ session, err := Start(ctx, Options{ShareID: "retiring-audio", Video: video,
+ Capture: retiringCapture{processCapture{executable}, entered, release},
+ Profile: QualityProfile{Video: video.Profile, AudioBitrate: 64_000},
+ AudioEnabled: true, MicrophoneMixing: true, EdgeCapacity: 1, BindAddress: "127.0.0.1:0"})
+ if err != nil {
+ resume()
+ t.Fatal(err)
+ }
+ defer func() { resume(); _ = session.Close() }()
+ closed := make(chan error, 1)
+ go func() { closed <- session.Close() }()
+ select {
+ case <-entered:
+ case <-ctx.Done():
+ t.Fatal("Close did not reach capture retirement")
+ }
+ // A startup worker awakened by active/cancellation may still need this lock
+ // before it can finish. Close cannot retain it while waiting for that worker.
+ if session.updateMu.TryLock() {
+ session.updateMu.Unlock()
+ } else {
+ t.Error("Close retained the update lock across blocking retirement")
+ }
+ resume()
+ select {
+ case err := <-closed:
+ if err != nil {
+ t.Fatal(err)
+ }
+ case <-ctx.Done():
+ t.Fatal("Close did not finish after capture retirement")
+ }
+}
+
+func TestEdgeReplacementFencesNativeControl(t *testing.T) {
+ engine, err := mediaedge.NewEngine(mediaedge.EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer engine.Close()
+ source, err := engine.NewSource("vp8", 1, 1, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer source.Close()
+ if err = source.SetFormat(0, 320, 180); err != nil {
+ t.Fatal(err)
+ }
+ if err = source.ConfigureOutputs([]uint32{300_000}); err != nil {
+ t.Fatal(err)
+ }
+ events := make(chan Event, 64)
+ session := &Session{shareID: "share_12345678", engine: engine, source: source, ctx: t.Context(),
+ edges: make(map[string]*mediaedge.Edge), events: func(ctx context.Context, event Event) {
+ select {
+ case events <- event:
+ case <-ctx.Done():
+ }
+ }}
+ const connectionID = "connection_12345678"
+ if _, err = session.PrepareEdge(connectionID, nil, false); err != nil {
+ t.Fatal(err)
+ }
+ old := session.edge(connectionID)
+ if err = old.Close(); err != nil {
+ t.Fatal(err)
+ }
+ var closed Event
+ var observations []Event
+ deadline := time.NewTimer(3 * time.Second)
+ defer deadline.Stop()
+ for closed.Current == nil {
+ select {
+ case event := <-events:
+ observations = append(observations, event)
+ if event.Type == "edge-state" && event.State == "closed" {
+ if event.Current == nil || !event.Current() {
+ t.Fatal("current transport failure lost its delivery authority")
+ }
+ closed = event
+ }
+ case <-deadline.C:
+ t.Fatal("edge did not emit its terminal state")
+ }
+ }
+ session.CloseEdge(connectionID)
+ if _, err = session.PrepareEdge(connectionID, nil, false); err != nil {
+ t.Fatal(err)
+ }
+ defer session.CloseEdge(connectionID)
+ if session.edge(connectionID) == old {
+ t.Fatal("replacement reused the retired edge")
+ }
+ for _, event := range observations {
+ if event.Current == nil || event.Current() {
+ t.Fatal("same-ID replacement accepted a retired edge observation", event.Type)
+ }
+ }
+}
+
func TestCaptureCommitWaitsForReaderMetadataOrTermination(t *testing.T) {
t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "1")
executable, err := os.Executable()
@@ -156,7 +295,7 @@ func TestCaptureCommitWaitsForReaderMetadataOrTermination(t *testing.T) {
}
videoDone := make(chan error, 1)
go func() { videoDone <- session.runVideo() }()
- go session.run(videoDone)
+ go session.run(videoDone, nil)
closed := make(chan error, 1)
if mode == "closed" {
go func() { closed <- session.Close() }()
@@ -321,7 +460,7 @@ func TestPreparedCaptureOutlivesItsPermissionOperation(t *testing.T) {
defer stopShare()
operationContext, finishOperation := context.WithCancel(shareContext)
defer finishOperation()
- session := &Session{ctx: shareContext, captureProcess: executable, edgeCapacity: 4}
+ session := &Session{ctx: shareContext, capture: processCapture{executable}, edgeCapacity: 4}
stream, _, err := session.prepareVideo(operationContext, nativecapture.VideoOptions{
Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}, Codec: "vp8",
Profile: nativecapture.VideoProfile{Width: 1280, Height: 720, Framerate: 30, Bitrate: 3_000_000, Preference: "balanced"},
@@ -332,13 +471,13 @@ func TestPreparedCaptureOutlivesItsPermissionOperation(t *testing.T) {
defer stream.Close()
finishOperation()
select {
- case <-stream.Done():
+ case <-stream.(*nativecapture.Stream).Done():
t.Fatal("finishing permission preparation stopped its accepted picture")
case <-time.After(250 * time.Millisecond):
}
stopShare()
select {
- case <-stream.Done():
+ case <-stream.(*nativecapture.Stream).Done():
case <-time.After(2 * time.Second):
t.Fatal("capture outlived its owning share")
}
diff --git a/internal/app/nativehost/source_input_windows_test.go b/internal/app/nativehost/source_input_windows_test.go
new file mode 100644
index 00000000..781b625e
--- /dev/null
+++ b/internal/app/nativehost/source_input_windows_test.go
@@ -0,0 +1,120 @@
+//go:build windows
+
+package nativehost
+
+import (
+ "context"
+ "errors"
+ "os"
+ "testing"
+ "time"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "github.com/TNTcraftHIM/Piik/internal/app/remoteinput"
+)
+
+type sourceInputCapture struct {
+ processCapture
+ reject error
+ rejectAudio bool
+}
+
+func (c *sourceInputCapture) Audio(ctx context.Context, target nativecapture.CaptureTarget, excluded *nativecapture.CaptureTarget) (CaptureStream, error) {
+ if c.rejectAudio {
+ return nil, errors.New("candidate audio failed")
+ }
+ return c.processCapture.Audio(ctx, target, excluded)
+}
+
+func (c *sourceInputCapture) Video(ctx context.Context, options nativecapture.VideoOptions) (CaptureStream, error) {
+ if c.reject != nil {
+ return rejectedSourceStream{failure: c.reject}, nil
+ }
+ return c.processCapture.Video(ctx, options)
+}
+
+type rejectedSourceStream struct {
+ CaptureStream
+ failure error
+}
+
+func (s rejectedSourceStream) Read() (nativecapture.Frame, error) {
+ return nativecapture.Frame{}, s.failure
+}
+func (s rejectedSourceStream) Close() error { return nil }
+func (s rejectedSourceStream) Outputs() []nativecapture.OutputProfile { return nil }
+
+func TestSourcePreparationPreservesInputAuthorizationUntilCommit(t *testing.T) {
+ t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
+ defer cancel()
+ input, err := remoteinput.New(ctx, false)
+ if err != nil || input == nil {
+ t.Fatalf("Windows input backend unavailable: %v", err)
+ }
+ defer input.Close()
+ capture := &sourceInputCapture{processCapture: processCapture{executable}}
+ video := nativecapture.VideoOptions{Codec: "vp8", Profile: audioRecoveryProfile(), Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}}
+ session, err := Start(ctx, Options{ShareID: "source-input-transaction", Capture: capture, Video: video, Profile: QualityProfile{Video: video.Profile, AudioBitrate: 64_000}, EdgeCapacity: 1, BindAddress: "127.0.0.1:0", RemoteInput: input, MicrophoneMixing: true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer session.Close()
+ // Enabling ask records policy only. No Viewer is granted and no input is injected.
+ apply, err := session.PrepareRemoteInputPolicy("ask", "", false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := apply(); err != nil {
+ t.Fatal(err)
+ }
+ previous := session.currentStream()
+ next := video
+ next.Target.SourceID = "2"
+ for _, failure := range []error{errors.New("candidate failed"), nativecapture.ErrSourceCanceled} {
+ capture.reject = failure
+ if err := session.ReplaceSource(next, false, nil); err == nil {
+ t.Fatal("rejected preparation succeeded")
+ }
+ if session.currentStream() != previous || session.RemoteInputState().Mode != "ask" {
+ t.Fatal("failed preparation retired the previous source authorization")
+ }
+ select {
+ case <-session.Done():
+ t.Fatal("failed preparation stopped the share")
+ default:
+ }
+ }
+ capture.reject = nil
+ select {
+ case <-session.videoActive:
+ case <-ctx.Done():
+ t.Fatal("initial capture did not become active")
+ }
+ capture.rejectAudio = true
+ if err := session.ReplaceSource(next, true, nil); err == nil {
+ t.Fatal("rejected audio preparation succeeded")
+ }
+ if session.currentStream() != previous || session.RemoteInputState().Mode != "ask" {
+ t.Fatal("audio preparation failure retired previous source authorization")
+ }
+ capture.rejectAudio = false
+ profile := QualityProfile{Video: video.Profile, AudioBitrate: 64_000}
+ profile.Video.Width, profile.Video.Height = 640, 360
+ if err := session.UpdateProfile(profile); err != nil {
+ t.Fatal(err)
+ }
+ if session.RemoteInputState().Mode != "ask" {
+ t.Fatal("same-source profile update retired desktop input authorization")
+ }
+ if err := session.ReplaceSource(next, false, nil); err != nil {
+ t.Fatal(err)
+ }
+ if session.RemoteInputState().Mode != "off" || session.videoOptions.Target.SourceID != "2" {
+ t.Fatal("committed replacement inherited previous source authorization")
+ }
+}
diff --git a/internal/app/publictunnel/process.go b/internal/app/publictunnel/process.go
index c7c70b5b..61e6eee8 100644
--- a/internal/app/publictunnel/process.go
+++ b/internal/app/publictunnel/process.go
@@ -65,6 +65,7 @@ func Start(
parent context.Context,
executable string,
localOrigin string,
+ temporaryDirectory string,
) (*Process, error) {
if parent == nil {
parent = context.Background()
@@ -83,7 +84,7 @@ func Start(
var err error
for attempt := 1; attempt <= startupAttempts; attempt++ {
var process *Process
- process, err = startAttempt(startup, executable, localOrigin)
+ process, err = startAttempt(startup, executable, localOrigin, temporaryDirectory)
if err == nil {
return process, nil
}
@@ -123,11 +124,11 @@ func Start(
return nil, err
}
-func startAttempt(parent context.Context, executable, localOrigin string) (*Process, error) {
+func startAttempt(parent context.Context, executable, localOrigin, temporaryDirectory string) (*Process, error) {
if err := parent.Err(); err != nil {
return nil, err
}
- configPath, err := writeTemporaryConfig()
+ configPath, err := writeTemporaryConfig(temporaryDirectory)
if err != nil {
return nil, err
}
@@ -253,18 +254,12 @@ func startAttempt(parent context.Context, executable, localOrigin string) (*Proc
cancel()
return nil, errors.Join(errStartupExited, process.Err())
case <-parent.Done():
- if err := process.Close(); err != nil {
- return nil, errors.Join(parent.Err(), err)
- }
- return nil, parent.Err()
+ return nil, process.stopStartup(parent.Err())
}
}
select {
case <-parent.Done():
- if err := process.Close(); err != nil {
- return nil, errors.Join(parent.Err(), err)
- }
- return nil, parent.Err()
+ return nil, process.stopStartup(parent.Err())
case <-process.done:
cancel()
return nil, errors.Join(errStartupExited, process.Err())
@@ -273,8 +268,27 @@ func startAttempt(parent context.Context, executable, localOrigin string) (*Proc
return process, nil
}
-func writeTemporaryConfig() (string, error) {
- file, err := os.CreateTemp("", "piik-cloudflared-*.yml")
+func (process *Process) stopStartup(cause error) error {
+ closeErr := process.Close()
+ err := cause
+ if closeErr != nil {
+ err = errors.Join(cause, closeErr)
+ }
+ // Killing an unready child at our deadline is clean process shutdown, but
+ // its last dependency error still explains why startup did not complete.
+ if errors.Is(cause, context.DeadlineExceeded) && closeErr == nil {
+ process.mu.Lock()
+ detail := process.lastDetail
+ process.mu.Unlock()
+ if detail != "" {
+ err = fmt.Errorf("%w\nlast public invitation detail: %s", err, detail)
+ }
+ }
+ return err
+}
+
+func writeTemporaryConfig(directory string) (string, error) {
+ file, err := os.CreateTemp(directory, "piik-cloudflared-*.yml")
if err != nil {
return "", fmt.Errorf("public tunnel temporary file could not be created: %w", err)
}
diff --git a/internal/app/publictunnel/process_test.go b/internal/app/publictunnel/process_test.go
index ce307796..a3db5164 100644
--- a/internal/app/publictunnel/process_test.go
+++ b/internal/app/publictunnel/process_test.go
@@ -21,6 +21,13 @@ import (
func TestMain(tests *testing.M) {
if mode := os.Getenv("PIIK_TUNNEL_FIXTURE"); mode != "" {
+ if mode == "deadline-detail" {
+ fmt.Fprintln(os.Stderr, `{"level":"error","message":"edge discovery failed","error":"DNS lookup failed token=fixture-secret"}`)
+ fmt.Fprintln(os.Stderr, `{"level":"info","message":"retrying"}`)
+ for {
+ time.Sleep(time.Hour)
+ }
+ }
if mode == "plain-exit" {
fmt.Fprintln(os.Stderr, "failed to request quick Tunnel: connect timeout token=fixture-secret")
fmt.Fprintln(os.Stdout, "trailing ordinary output")
@@ -99,7 +106,8 @@ func TestStartupRetainsPlainDependencyFailure(t *testing.T) {
t.Fatal(err)
}
t.Setenv("PIIK_TUNNEL_FIXTURE", "plain-exit")
- process, err := startAttempt(t.Context(), executable, "http://127.0.0.1:8787")
+ directory := t.TempDir()
+ process, err := startAttempt(t.Context(), executable, "http://127.0.0.1:8787", directory)
var failure *exec.ExitError
if process != nil || !errors.Is(err, errStartupExited) || !errors.As(err, &failure) || failure.ExitCode() != 7 {
t.Fatalf("plain dependency failure lost its exit status: %v, %v", process, err)
@@ -108,6 +116,33 @@ func TestStartupRetainsPlainDependencyFailure(t *testing.T) {
strings.Contains(err.Error(), "fixture-secret") || strings.Contains(err.Error(), "ordinary output") {
t.Fatalf("plain dependency failure was omitted, unsafe or overwritten: %v", err)
}
+ entries, readErr := os.ReadDir(directory)
+ if readErr != nil || len(entries) != 0 {
+ t.Fatalf("failed child retained caller config: %v, %v", entries, readErr)
+ }
+}
+
+func TestStartupDeadlineRetainsSanitizedDependencyCause(t *testing.T) {
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("PIIK_TUNNEL_FIXTURE", "deadline-detail")
+ ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
+ defer cancel()
+ directory := t.TempDir()
+ process, err := startAttempt(ctx, executable, "http://127.0.0.1:8787", directory)
+ if process != nil || !errors.Is(err, context.DeadlineExceeded) {
+ t.Fatalf("startup deadline was lost: %v, %v", process, err)
+ }
+ if !strings.Contains(err.Error(), "edge discovery failed\nDNS lookup failed token=[redacted]") ||
+ strings.Contains(err.Error(), "fixture-secret") || strings.Contains(err.Error(), "retrying") {
+ t.Fatalf("startup deadline lost or exposed dependency cause: %v", err)
+ }
+ entries, readErr := os.ReadDir(directory)
+ if readErr != nil || len(entries) != 0 {
+ t.Fatalf("timed-out child retained temporary config: %v, %v", entries, readErr)
+ }
}
func TestStartupRetriesOnlyRetiredPreReadyChildren(t *testing.T) {
@@ -130,7 +165,7 @@ func TestStartupRetriesOnlyRetiredPreReadyChildren(t *testing.T) {
t.Setenv("PIIK_TUNNEL_FIXTURE_ATTEMPTS", record)
t.Setenv("PIIK_TUNNEL_FIXTURE_FAILURES", strconv.Itoa(sample.failures))
t.Setenv("PIIK_TUNNEL_FIXTURE_EXIT_CODE", strconv.Itoa(sample.exitCode))
- process, err := Start(t.Context(), executable, "http://127.0.0.1:8787")
+ process, err := Start(t.Context(), executable, "http://127.0.0.1:8787", "")
if sample.ready {
if err != nil || process == nil {
t.Fatalf("fourth attempt: %v, %v", process, err)
@@ -205,7 +240,7 @@ func TestStartupRetriesShareCancellationAndDeadline(t *testing.T) {
key: sample.key, value: sample.value, cancel: cancel,
}))
defer slog.SetDefault(previous)
- process, err := Start(ctx, executable, "http://127.0.0.1:8787")
+ process, err := Start(ctx, executable, "http://127.0.0.1:8787", "")
if process != nil || err != context.Canceled {
t.Fatalf("startup cancellation: %v, %v", process, err)
}
@@ -222,7 +257,7 @@ func TestStartupRetriesShareCancellationAndDeadline(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), timeout)
defer cancel()
started := time.Now()
- process, err := Start(ctx, executable, "http://127.0.0.1:8787")
+ process, err := Start(ctx, executable, "http://127.0.0.1:8787", "")
if process != nil || !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("startup deadline: %v, %v", process, err)
}
@@ -266,7 +301,7 @@ func TestStartupEnablesCloudflaredProtocolFallback(t *testing.T) {
t.Setenv("PIIK_TUNNEL_FIXTURE", "tcp-only")
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
- process, err := Start(ctx, executable, "http://127.0.0.1:8787")
+ process, err := Start(ctx, executable, "http://127.0.0.1:8787", "")
if err != nil {
t.Fatalf("tunnel with TCP available: %v", err)
}
@@ -311,7 +346,7 @@ func TestCloseKeepsAnObservedFailureWhileLogsDrain(t *testing.T) {
}))
defer slog.SetDefault(previous)
defer resume()
- process, err := Start(t.Context(), executable, "http://127.0.0.1:8787")
+ process, err := Start(t.Context(), executable, "http://127.0.0.1:8787", "")
if err != nil {
t.Fatal(err)
}
@@ -366,7 +401,7 @@ func TestReadyTunnelWaitsForItsExplicitOrderedClose(t *testing.T) {
t.Setenv("PIIK_TUNNEL_FIXTURE", "ready")
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
- process, err := Start(ctx, executable, "http://127.0.0.1:8787")
+ process, err := Start(ctx, executable, "http://127.0.0.1:8787", "")
if err != nil {
t.Fatal(err)
}
@@ -399,7 +434,7 @@ func TestStartupCancellationStillRetiresTheTunnel(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 150*time.Millisecond)
defer cancel()
started := time.Now()
- process, err := Start(ctx, executable, "http://127.0.0.1:8787")
+ process, err := Start(ctx, executable, "http://127.0.0.1:8787", "")
if process != nil || !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("cancelled startup: %v, %v", process, err)
}
@@ -409,7 +444,7 @@ func TestStartupCancellationStillRetiresTheTunnel(t *testing.T) {
}
func TestMissingTunnelExplainsHowToRestoreThePackage(t *testing.T) {
- _, err := Start(t.Context(), filepath.Join(t.TempDir(), "missing-tunnel"), "http://127.0.0.1:8787")
+ _, err := Start(t.Context(), filepath.Join(t.TempDir(), "missing-tunnel"), "http://127.0.0.1:8787", "")
var cause *os.PathError
if !errors.As(err, &cause) || appfailure.Of(err) != appfailure.TunnelUnavailable || !strings.Contains(err.Error(), "extract the complete Piik archive") {
t.Fatalf("missing tunnel lost its cause or recovery instruction: %v", err)
@@ -470,11 +505,15 @@ func TestLocalOriginMustBeLoopbackHTTP(t *testing.T) {
}
func TestTemporaryConfigIsMinimal(t *testing.T) {
- path, err := writeTemporaryConfig()
+ directory := t.TempDir()
+ path, err := writeTemporaryConfig(directory)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Remove(path) })
+ if filepath.Dir(path) != directory {
+ t.Fatalf("temporary config escaped caller directory: %s", path)
+ }
payload, err := os.ReadFile(path)
if err != nil || string(payload) != "metrics: 127.0.0.1:0\n" {
t.Fatalf("temporary config = %q, %v", payload, err)
diff --git a/internal/app/remoteinput/gamepad_darwin.go b/internal/app/remoteinput/gamepad_darwin.go
index 38a3cd1f..a18b870e 100644
--- a/internal/app/remoteinput/gamepad_darwin.go
+++ b/internal/app/remoteinput/gamepad_darwin.go
@@ -1,4 +1,4 @@
-//go:build darwin && cgo
+//go:build darwin && !ios && cgo
package remoteinput
diff --git a/internal/app/remoteinput/gamepad_darwin.m b/internal/app/remoteinput/gamepad_darwin.m
index 6a1db2c4..9ee7d561 100644
--- a/internal/app/remoteinput/gamepad_darwin.m
+++ b/internal/app/remoteinput/gamepad_darwin.m
@@ -1,4 +1,4 @@
-//go:build darwin && cgo
+//go:build darwin && !ios && cgo
#include "gamepad_darwin.h"
#import <Foundation/Foundation.h>
diff --git a/internal/app/remoteinput/gamepad_darwin_test.go b/internal/app/remoteinput/gamepad_darwin_test.go
index fc0429eb..21df6b03 100644
--- a/internal/app/remoteinput/gamepad_darwin_test.go
+++ b/internal/app/remoteinput/gamepad_darwin_test.go
@@ -1,4 +1,4 @@
-//go:build darwin && cgo
+//go:build darwin && !ios && cgo
package remoteinput
diff --git a/internal/app/remoteinput/gamepad_other.go b/internal/app/remoteinput/gamepad_other.go
index 23951292..955550ce 100644
--- a/internal/app/remoteinput/gamepad_other.go
+++ b/internal/app/remoteinput/gamepad_other.go
@@ -1,4 +1,4 @@
-//go:build !linux && (!windows || !amd64) && (!darwin || !cgo)
+//go:build !linux && (!windows || !amd64) && (!darwin || ios || !cgo)
package remoteinput
diff --git a/internal/app/remoteinput/held.go b/internal/app/remoteinput/held.go
new file mode 100644
index 00000000..fcca2283
--- /dev/null
+++ b/internal/app/remoteinput/held.go
@@ -0,0 +1,144 @@
+package remoteinput
+
+import "errors"
+
+type controllerTextSink struct {
+ manager *Manager
+ controller *connection
+}
+
+func (s controllerTextSink) Reset() error { return s.manager.resetHeld(s.controller.owner) }
+func (s controllerTextSink) Apply(b bounds, event inputEvent) error {
+ return s.manager.applyHeld(s.controller, b, event)
+}
+
+// Whole-source cleanup may use the platform's tracked physical state. Keep the
+// logical release debt until Reset actually succeeds, including after retirement.
+func (m *Manager) resetHeld(s *Session) error {
+ if m.pendingKeys == nil {
+ m.pendingKeys = make(map[string]bool)
+ }
+ if m.pendingButtons == nil {
+ m.pendingButtons = make(map[int]bool)
+ }
+ for _, c := range m.connections {
+ if c.owner != s {
+ continue
+ }
+ for key := range c.keys {
+ m.pendingKeys[key] = true
+ }
+ for button := range c.buttons {
+ m.pendingButtons[button] = true
+ }
+ c.keys, c.buttons = nil, nil
+ }
+ err := m.sink.Reset()
+ if err == nil {
+ clear(m.pendingKeys)
+ clear(m.pendingButtons)
+ }
+ return err
+}
+
+// Failed releases outlive their departing controller. Only releases are retried;
+// pointer movement, presses and committed text are never replayed.
+func (m *Manager) releaseKey(code string) error {
+ err := m.sink.Apply(bounds{}, inputEvent{Type: "key", Code: code})
+ if err != nil {
+ if m.pendingKeys == nil {
+ m.pendingKeys = make(map[string]bool)
+ }
+ m.pendingKeys[code] = true
+ } else {
+ delete(m.pendingKeys, code)
+ }
+ return err
+}
+
+func (m *Manager) releaseButton(button int) error {
+ err := m.sink.Apply(bounds{}, inputEvent{Type: "button", Button: button})
+ if err != nil {
+ if m.pendingButtons == nil {
+ m.pendingButtons = make(map[int]bool)
+ }
+ m.pendingButtons[button] = true
+ } else {
+ delete(m.pendingButtons, button)
+ }
+ return err
+}
+
+func (m *Manager) retryReleases() {
+ for code := range m.pendingKeys {
+ _ = m.releaseKey(code)
+ }
+ for button := range m.pendingButtons {
+ _ = m.releaseButton(button)
+ }
+}
+
+func (m *Manager) noteInputResult(c *connection, err error) {
+ failed := err != nil
+ if c.inputFailed != failed {
+ c.inputFailed = failed
+ m.send(c, m.state(c))
+ }
+}
+
+func (m *Manager) applyHeld(c *connection, b bounds, event inputEvent) error {
+ m.retryReleases()
+ releasing := (event.Type == "key" || event.Type == "button") && !event.Down
+ if !releasing && (len(m.pendingKeys) != 0 || len(m.pendingButtons) != 0) {
+ return errors.New("held input cleanup is unavailable")
+ }
+ switch event.Type {
+ case "key":
+ if event.Code == "" || len(event.Code) > 32 || event.Down && len(c.keys) >= 256 && !c.keys[event.Code] {
+ return nil
+ }
+ if event.Down {
+ // A repeat from this holder is input; another holder shares the press.
+ if c.keys[event.Code] || !m.keyHeld(event.Code) {
+ if err := m.sink.Apply(b, event); err != nil {
+ return err
+ }
+ }
+ if c.keys == nil {
+ c.keys = make(map[string]bool)
+ }
+ c.keys[event.Code] = true
+ } else {
+ if !c.keys[event.Code] {
+ return nil
+ }
+ delete(c.keys, event.Code)
+ if !m.keyHeld(event.Code) {
+ return m.releaseKey(event.Code)
+ }
+ }
+ case "button":
+ if event.Button < 0 || event.Button > 4 || c.buttons[event.Button] == event.Down {
+ return nil
+ }
+ if event.Down {
+ if !m.buttonHeld(event.Button) {
+ if err := m.sink.Apply(b, event); err != nil {
+ return err
+ }
+ }
+ if c.buttons == nil {
+ c.buttons = make(map[int]bool)
+ }
+ c.buttons[event.Button] = true
+ } else {
+ delete(c.buttons, event.Button)
+ if !m.buttonHeld(event.Button) {
+ return m.releaseButton(event.Button)
+ }
+ }
+ default:
+ return m.sink.Apply(b, event)
+ }
+ return nil
+}
diff --git a/internal/app/remoteinput/held_test.go b/internal/app/remoteinput/held_test.go
new file mode 100644
index 00000000..81db8c7a
--- /dev/null
+++ b/internal/app/remoteinput/held_test.go
@@ -0,0 +1,142 @@
+package remoteinput
+
+import (
+ "errors"
+ "testing"
+)
+
+type textReleaseSink struct {
+ *recordedInput
+ reject bool
+}
+
+func (s *textReleaseSink) Apply(b bounds, event inputEvent) error {
+ if s.reject && event.Type == "key" && !event.Down {
+ return errors.New("text key release rejected")
+ }
+ return s.recordedInput.Apply(b, event)
+}
+
+func TestPartialTextOnlyRetriesRelease(t *testing.T) {
+ sink := &textReleaseSink{recordedInput: &recordedInput{events: make(chan inputEvent, 32), resets: make(chan struct{}, 16), focused: true}, reject: true}
+ m := &Manager{sink: sink, connections: map[endpointKey]*connection{}}
+ _, peers := coPlaySession(t, m, true)
+ c := peers[0]
+ sendTestInput(m, c, inputEvent{Type: "request"})
+ grant := c.grant
+ sendTestInput(m, c, inputEvent{Type: "text", Text: "once\nnever"})
+ if c.grant != grant || !c.inputFailed || !m.pendingKeys["Enter"] {
+ t.Fatal("text failure changed grant or lost release debt")
+ }
+ if event := <-sink.events; event.Text != "once" {
+ t.Fatal("text prefix was not delivered")
+ }
+ if event := <-sink.events; event.Code != "Enter" || !event.Down {
+ t.Fatal("text did not execute newline press")
+ }
+ sink.reject = false
+ m.retryReleases()
+ if len(sink.events) != 1 {
+ t.Fatal("text failure replayed a text fragment")
+ }
+ if event := <-sink.events; event.Code != "Enter" || event.Down {
+ t.Fatal("text recovery did not restrict itself to release")
+ }
+}
+
+func TestRejectedInputKeepsGrantAndDoesNotReplay(t *testing.T) {
+ for _, shared := range []bool{false, true} {
+ t.Run(map[bool]string{false: "ordinary", true: "shared"}[shared], func(t *testing.T) {
+ sink := &recordedInput{events: make(chan inputEvent, 32), resets: make(chan struct{}, 16), focused: true}
+ m := &Manager{sink: sink, connections: map[endpointKey]*connection{}}
+ s, peers := coPlaySession(t, m, true)
+ s.coPlay = shared
+ c := peers[0]
+ sendTestInput(m, c, inputEvent{Type: "request"})
+ grant := c.grant
+ sink.applyErr = errors.New("temporarily rejected")
+ sendTestInput(m, c, inputEvent{Type: "key", Code: "KeyA", Down: true})
+ if grant == "" || c.grant != grant || !c.inputFailed || len(c.keys) != 0 {
+ t.Fatal("failed press changed grant or became held")
+ }
+ sink.applyErr = nil
+ m.retryReleases()
+ if len(sink.events) != 0 {
+ t.Fatal("failed press was replayed")
+ }
+ sendTestInput(m, c, inputEvent{Type: "key", Code: "KeyB", Down: true})
+ if c.grant != grant || c.inputFailed || !c.keys["KeyB"] {
+ t.Fatal("new input did not recover within grant")
+ }
+ <-sink.events
+ sink.applyErr = errors.New("release rejected")
+ sendTestInput(m, c, inputEvent{Type: "key", Code: "KeyB"})
+ if c.grant != grant || !m.pendingKeys["KeyB"] || c.keys["KeyB"] {
+ t.Fatal("failed release lost cleanup debt or grant")
+ }
+ sendTestInput(m, c, inputEvent{Type: "move", X: .7, Y: .4})
+ if len(sink.events) != 0 {
+ t.Fatal("new action ran before cleanup")
+ }
+ sink.resetErr = errors.New("reset blocked")
+ m.releaseConnection(c)
+ if c.grant != "" || !m.pendingKeys["KeyB"] {
+ t.Fatal("retirement lost pending release")
+ }
+ sink.applyErr = nil
+ sink.resetErr = nil
+ m.retryReleases()
+ if len(m.pendingKeys) != 0 || len(sink.events) != 1 {
+ t.Fatal("retired release did not recover")
+ }
+ if event := <-sink.events; event.Type != "key" || event.Code != "KeyB" || event.Down {
+ t.Fatalf("cleanup replayed action: %+v", event)
+ }
+ })
+ }
+}
+
+func TestPlayerCleanupFailurePreservesSiblingGrant(t *testing.T) {
+ sink := &recordedInput{events: make(chan inputEvent, 32), resets: make(chan struct{}, 16), focused: true}
+ m := &Manager{sink: sink, connections: map[endpointKey]*connection{}}
+ s, peers := coPlaySession(t, m, true, true)
+ a, b := peers[0], peers[1]
+ for _, c := range peers {
+ sendTestInput(m, c, inputEvent{Type: "request"})
+ }
+ grant := b.grant
+ sendTestInput(m, a, inputEvent{Type: "button", Button: 0, Down: true})
+ <-sink.events
+ sink.applyErr = errors.New("release blocked")
+ sendTestInput(m, a, inputEvent{Type: "reset"})
+ if a.grant == "" || b.grant != grant || !a.inputFailed || !m.pendingButtons[0] {
+ t.Fatal("personal reset changed authority or lost release")
+ }
+ m.releaseConnection(a)
+ if b.grant != grant || m.owner != s {
+ t.Fatal("disconnect cleanup revoked sibling")
+ }
+ sink.applyErr = nil
+ m.retryReleases()
+ if event := <-sink.events; event.Type != "button" || event.Down {
+ t.Fatal("cleanup replayed press")
+ }
+ sendTestInput(m, b, inputEvent{Type: "key", Code: "KeyW", Down: true})
+ <-sink.events
+ sink.resetErr = errors.New("focus cleanup blocked")
+ sink.focused = false
+ m.refreshActive()
+ if b.grant != grant || !b.suspended || !m.pendingKeys["KeyW"] {
+ t.Fatal("focus cleanup lost grant or release debt")
+ }
+ m.retryReleases()
+ if event := <-sink.events; event.Code != "KeyW" || event.Down {
+ t.Fatal("focus cleanup replayed press")
+ }
+ sink.resetErr = nil
+ sink.focused = true
+ m.refreshActive()
+ if b.grant != grant || b.suspended || len(sink.events) != 0 {
+ t.Fatal("focus return changed grant or replayed action")
+ }
+}
diff --git a/internal/app/remoteinput/manager.go b/internal/app/remoteinput/manager.go
index 9702d910..6a7de321 100644
--- a/internal/app/remoteinput/manager.go
+++ b/internal/app/remoteinput/manager.go
@@ -11,12 +11,14 @@ import (
"encoding/json"
"errors"
"io"
+ "log/slog"
"runtime"
"sort"
"sync"
"time"
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "github.com/TNTcraftHIM/Piik/internal/diagnostics"
"github.com/TNTcraftHIM/Piik/internal/media/inputroute"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
"github.com/pion/webrtc/v4"
@@ -27,20 +29,21 @@ const ChannelLabel = inputroute.Label
type bounds struct{ left, top, width, height int32 }
type inputEvent struct {
- Type string `json:"type"`
- Grant string `json:"grant,omitempty"`
- Intent string `json:"intent,omitempty"`
- Password string `json:"password,omitempty"`
- X float64 `json:"x,omitempty"`
- Y float64 `json:"y,omitempty"`
- Button int `json:"button,omitempty"`
- Down bool `json:"down,omitempty"`
- Code string `json:"code,omitempty"`
- Text string `json:"text,omitempty"`
- Delta int `json:"delta,omitempty"`
- DX int32 `json:"dx,omitempty"`
- DY int32 `json:"dy,omitempty"`
- Gamepad *gamepadReport `json:"gamepad,omitempty"`
+ Type string `json:"type"`
+ Grant string `json:"grant,omitempty"`
+ Intent string `json:"intent,omitempty"`
+ Password string `json:"password,omitempty"`
+ X float64 `json:"x,omitempty"`
+ Y float64 `json:"y,omitempty"`
+ Button int `json:"button,omitempty"`
+ Down bool `json:"down,omitempty"`
+ Code string `json:"code,omitempty"`
+ Text string `json:"text,omitempty"`
+ Delta int `json:"delta,omitempty"`
+ DX int32 `json:"dx,omitempty"`
+ DY int32 `json:"dy,omitempty"`
+ Gamepad *gamepadReport `json:"gamepad,omitempty"`
+ InputFeedback bool `json:"inputFeedback,omitempty"`
}
type inputSink interface {
@@ -61,6 +64,8 @@ type View struct {
type Manager struct {
mu sync.Mutex
sink inputSink
+ pendingKeys map[string]bool
+ pendingButtons map[int]bool
gamepads gamepadBackend
gamepadOpening bool
connections map[endpointKey]*connection
@@ -90,24 +95,26 @@ type Session struct {
}
type connection struct {
- owner *Session
- edgeID string
- epoch string
- id string
- requested bool
- requestID string
- intent string
- grant string
- suspended bool
- peerID string
- secret string
- cipher *inputCipher
- extended bool
- keys map[string]bool
- buttons map[int]bool
- pad gamepadDevice
- padFailed bool
- padIntent uint64
+ owner *Session
+ edgeID string
+ epoch string
+ id string
+ requested bool
+ requestID string
+ intent string
+ grant string
+ suspended bool
+ inputFailed bool
+ inputFeedback bool
+ peerID string
+ secret string
+ cipher *inputCipher
+ extended bool
+ keys map[string]bool
+ buttons map[int]bool
+ pad gamepadDevice
+ padFailed bool
+ padIntent uint64
}
type endpointKey struct {
@@ -149,6 +156,7 @@ func New(ctx context.Context, captureInput bool) (*Manager, error) {
m.Revoke()
case <-ticker.C:
m.mu.Lock()
+ m.retryReleases()
m.refreshActive()
m.mu.Unlock()
}
@@ -308,6 +316,7 @@ func (s *Session) receivePacket(edgeID string, packet inputroute.Packet) {
}
if c.epoch != packet.Epoch {
c.extended = false
+ c.inputFeedback = false
}
c.edgeID, c.epoch = edgeID, packet.Epoch
m.mu.Unlock()
@@ -335,12 +344,13 @@ func (m *Manager) reply(c *connection, state, reason string) {
Intent string `json:"intent,omitempty"`
Mode string `json:"mode"`
Reason string `json:"reason,omitempty"`
+ InputFailed bool `json:"inputFailed,omitempty"`
Capabilities []string `json:"capabilities,omitempty"`
CoPlay *bool `json:"coPlay,omitempty"`
- }{Platform: runtime.GOOS, State: state, Grant: c.grant, Intent: c.intent, Mode: c.owner.mode, Reason: reason}
+ }{Platform: runtime.GOOS, State: state, Grant: c.grant, Intent: c.intent, Mode: c.owner.mode, Reason: reason, InputFailed: c.inputFailed && c.inputFeedback}
if c.extended {
coPlay := c.owner.coPlay
- value.Capabilities, value.CoPlay = []string{"coPlay"}, &coPlay
+ value.Capabilities, value.CoPlay = []string{"coPlay", "inputFeedback"}, &coPlay
if coPlay && m.singleController(c.owner) == c {
value.Capabilities = append(value.Capabilities, "text")
}
@@ -381,6 +391,8 @@ func (m *Manager) refreshActive() (bounds, bool) {
var failed []*connection
for _, c := range m.connections {
if c.owner == s && c.grant != "" {
+ slog.Debug("remote-input", "event", "target-failed", "sourceKind", s.target.Kind,
+ "connectionId", diagnostics.ID(c.edgeID), "inputGeneration", diagnostics.ID(c.epoch), diagnostics.Error(err))
failed = append(failed, c)
}
}
@@ -401,10 +413,12 @@ func (m *Manager) refreshActive() (bounds, bool) {
}
if changed {
if !focused {
- _ = m.sink.Reset()
+ err := m.resetHeld(s)
for _, c := range m.connections {
if c.owner == s {
- c.keys, c.buttons = nil, nil
+ if err != nil {
+ m.noteInputResult(c, err)
+ }
if c.pad != nil {
if err := c.pad.Update(gamepadReport{}); err != nil {
m.failGamepad(c)
@@ -425,7 +439,7 @@ func (m *Manager) release() {
}
s := m.owner
m.owner = nil
- _ = m.sink.Reset()
+ _ = m.resetHeld(s)
for _, c := range m.connections {
if c.owner == s && c.grant != "" {
if c.pad != nil {
@@ -433,7 +447,7 @@ func (m *Manager) release() {
c.pad = nil
}
c.grant, c.suspended = "", false
- c.keys, c.buttons = nil, nil
+ c.inputFailed = false
m.send(c, "ready")
}
}
@@ -448,14 +462,14 @@ func (m *Manager) clearHeld(c *connection) error {
var err error
for code := range keys {
if !m.keyHeld(code) {
- err = errors.Join(err, m.sink.Apply(bounds{}, inputEvent{Type: "key", Code: code}))
+ err = errors.Join(err, m.releaseKey(code))
}
}
buttons := c.buttons
c.buttons = nil
for button := range buttons {
if !m.buttonHeld(button) {
- err = errors.Join(err, m.sink.Apply(bounds{}, inputEvent{Type: "button", Button: button}))
+ err = errors.Join(err, m.releaseButton(button))
}
}
return err
@@ -499,15 +513,14 @@ func (m *Manager) releaseConnection(c *connection) {
if !c.owner.coPlay {
m.release()
return
- } else if m.clearHeld(c) != nil {
- m.release()
- return
}
+ _ = m.clearHeld(c)
if c.pad != nil {
_ = c.pad.Close()
c.pad = nil
}
c.grant, c.suspended = "", false
+ c.inputFailed = false
if remaining := m.singleController(c.owner); remaining != nil && remaining.extended {
m.send(remaining, m.state(remaining))
}
@@ -519,60 +532,6 @@ func (m *Manager) releaseConnection(c *connection) {
m.owner = nil
}
-func (m *Manager) applyCoPlay(c *connection, b bounds, event inputEvent) error {
- switch event.Type {
- case "key":
- // A Browser has fewer than 256 physical keys. Bound even unknown codes
- // before recording them; the platform owns its supported key mapping.
- if event.Code == "" || len(event.Code) > 32 || event.Down && len(c.keys) >= 256 && !c.keys[event.Code] {
- return nil
- }
- if event.Down {
- if c.keys[event.Code] {
- // Repeat from an existing holder is intentional keyboard input.
- // Only another player's initial press is deduplicated below.
- return m.sink.Apply(b, event)
- }
- held := m.keyHeld(event.Code)
- if c.keys == nil {
- c.keys = make(map[string]bool)
- }
- c.keys[event.Code] = true
- if held {
- return nil
- }
- } else {
- if !c.keys[event.Code] {
- return nil
- }
- delete(c.keys, event.Code)
- if m.keyHeld(event.Code) {
- return nil
- }
- }
- case "button":
- if event.Button < 0 || event.Button > 4 || c.buttons[event.Button] == event.Down {
- return nil
- }
- held := m.buttonHeld(event.Button)
- if c.buttons == nil {
- c.buttons = make(map[int]bool)
- }
- if event.Down {
- c.buttons[event.Button] = true
- if held {
- return nil
- }
- } else {
- delete(c.buttons, event.Button)
- if m.buttonHeld(event.Button) {
- return nil
- }
- }
- }
- return m.sink.Apply(b, event)
-}
-
func (m *Manager) receive(c *connection, message webrtc.DataChannelMessage, epoch string) {
if !message.IsString || len(message.Data) > maxInputBytes {
return
@@ -593,6 +552,7 @@ func (m *Manager) receive(c *connection, message webrtc.DataChannelMessage, epoc
m.send(c, m.state(c))
case "capabilities":
c.extended = true
+ c.inputFeedback = event.InputFeedback
m.send(c, m.state(c))
case "request":
if !c.owner.available() || c.owner.mode == "off" || c.requested || c.grant != "" {
@@ -642,13 +602,7 @@ func (m *Manager) receive(c *connection, message webrtc.DataChannelMessage, epoc
}
if event.Type == "reset" {
m.receiveGamepad(c, inputEvent{Type: "gamepad-reset"})
- if c.owner.coPlay {
- if m.clearHeld(c) != nil {
- m.release()
- }
- } else {
- _ = m.sink.Reset()
- }
+ m.noteInputResult(c, m.clearHeld(c))
return
}
if event.Type == "text" && c.owner.coPlay && m.singleController(c.owner) != c {
@@ -661,17 +615,14 @@ func (m *Manager) receive(c *connection, message webrtc.DataChannelMessage, epoc
}
var err error
if event.Type == "text" {
- err = applyText(m.sink, b, event.Text)
- c.keys, c.buttons = nil, nil
- } else if c.owner.coPlay {
- err = m.applyCoPlay(c, b, event)
+ err = applyText(controllerTextSink{m, c}, b, event.Text)
} else {
- err = m.sink.Apply(b, event)
+ err = m.applyHeld(c, b, event)
}
if err != nil {
- m.release()
- m.send(c, "failed")
+ slog.Debug("remote-input", "event", "apply-failed", "connectionId", diagnostics.ID(c.edgeID), "inputGeneration", diagnostics.ID(c.epoch), diagnostics.Error(err))
}
+ m.noteInputResult(c, err)
}
}
@@ -775,6 +726,7 @@ func (m *Manager) Close() {
}
m.closed = true
m.release()
+ m.retryReleases()
if m.gamepads != nil {
_ = m.gamepads.Close()
m.gamepads = nil
diff --git a/internal/app/remoteinput/manager_test.go b/internal/app/remoteinput/manager_test.go
index 4e0d3474..be967154 100644
--- a/internal/app/remoteinput/manager_test.go
+++ b/internal/app/remoteinput/manager_test.go
@@ -1,6 +1,7 @@
package remoteinput
import (
+ "bytes"
"context"
"crypto/rand"
"encoding/base64"
@@ -8,12 +9,14 @@ import (
"encoding/hex"
"encoding/json"
"errors"
+ "log/slog"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "github.com/TNTcraftHIM/Piik/internal/diagnostics"
"github.com/TNTcraftHIM/Piik/internal/media/inputroute"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
"github.com/pion/webrtc/v4"
@@ -26,6 +29,9 @@ type recordedInput struct {
target bounds
geometry bounds
focused bool
+ targetErr error
+ applyErr error
+ resetErr error
}
func setTestPolicy(s *Session, mode, password string) error {
@@ -307,15 +313,70 @@ func TestPreparedPolicyFollowsTheCommittedSourceAndHostIntent(t *testing.T) {
}
func (s *recordedInput) Target(nativecapture.CaptureTarget, *nativecapture.Stream) (bounds, bool, error) {
- return s.geometry, s.focused, nil
+ return s.geometry, s.focused, s.targetErr
}
func (s *recordedInput) Apply(target bounds, event inputEvent) error {
+ if s.applyErr != nil {
+ return s.applyErr
+ }
s.target = target
s.events <- event
return nil
}
-func (s *recordedInput) Reset() error { s.resets <- struct{}{}; return nil }
+func (s *recordedInput) Reset() error {
+ if s.resets != nil {
+ s.resets <- struct{}{}
+ }
+ return s.resetErr
+}
+
+func TestInputFailureDiagnosticsPreserveExecutionAuthority(t *testing.T) {
+ for _, stage := range []string{"target", "apply"} {
+ t.Run(stage, func(t *testing.T) {
+ var logs bytes.Buffer
+ previous := slog.Default()
+ slog.SetDefault(slog.New(slog.NewJSONHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
+ t.Cleanup(func() { slog.SetDefault(previous) })
+ sink := &recordedInput{events: make(chan inputEvent, 8), resets: make(chan struct{}, 8), focused: true}
+ m := &Manager{sink: sink, connections: map[endpointKey]*connection{}}
+ _, peers := coPlaySession(t, m, true)
+ c := peers[0]
+ sendTestInput(m, c, inputEvent{Type: "request", Intent: strings.Repeat("f", 32)})
+ grant := c.grant
+ if grant == "" {
+ t.Fatal("control was not granted")
+ }
+ sendTestInput(m, c, inputEvent{Type: "key", Code: "KeyA", Down: true, Grant: grant})
+ failure := errors.New("platform input failed")
+ if stage == "target" {
+ sink.targetErr = failure
+ } else {
+ sink.applyErr = failure
+ }
+ sendTestInput(m, c, inputEvent{Type: "text", Text: "private committed text", Grant: grant})
+ if stage == "target" {
+ if m.owner != nil || c.grant != "" || len(c.keys) != 0 {
+ t.Fatal("invalid target retained authority")
+ }
+ } else if m.owner == nil || c.grant != grant || !c.inputFailed {
+ t.Fatal("execution failure retired authorization or hid failure")
+ }
+ var record map[string]any
+ if err := json.Unmarshal(logs.Bytes(), &record); err != nil {
+ t.Fatalf("expected one failure record: %v", err)
+ }
+ if record["event"] != stage+"-failed" || record["connectionId"] != diagnostics.ID(c.edgeID) || record["inputGeneration"] != diagnostics.ID(c.epoch) {
+ t.Fatalf("failure stage or generation missing: %v", record)
+ }
+ for _, private := range []string{grant, c.intent, c.edgeID, c.epoch, "KeyA", "private committed text"} {
+ if strings.Contains(logs.String(), private) {
+ t.Fatal("failure diagnostic recorded private input or authority")
+ }
+ }
+ })
+ }
+}
func TestCommittedMultilineText(t *testing.T) {
sink := &recordedInput{events: make(chan inputEvent, 16), resets: make(chan struct{}, 8)}
@@ -1095,16 +1156,20 @@ func TestAttendedGrantLifecycle(t *testing.T) {
peer, state string
capabilities []string
coPlay *bool
+ grant string
+ inputFailed *bool
}
states := make(chan observedState, 128)
bindings := map[string]protocol.RemoteInputPeer{}
ciphers := map[string]*inputCipher{}
+ epochs := map[string]string{}
for _, name := range []string{"first", "second"} {
id, key := make([]byte, 16), make([]byte, 32)
_, _ = rand.Read(id)
_, _ = rand.Read(key)
bindings[name] = protocol.RemoteInputPeer{PeerID: name + "-viewer", EndpointID: hex.EncodeToString(id), Secret: hex.EncodeToString(key)}
ciphers[name], _ = newInputCipher(bindings[name].Secret)
+ epochs[name] = bindings[name].EndpointID
}
if err := s.SetPeers([]protocol.RemoteInputPeer{bindings["first"], bindings["second"]}); err != nil {
t.Fatal(err)
@@ -1127,9 +1192,11 @@ func TestAttendedGrantLifecycle(t *testing.T) {
State string `json:"state"`
Capabilities []string `json:"capabilities"`
CoPlay *bool `json:"coPlay"`
+ Grant string `json:"grant"`
+ InputFailed *bool `json:"inputFailed"`
}
if json.Unmarshal(plain, &value) == nil {
- states <- observedState{c.Label(), value.State, value.Capabilities, value.CoPlay}
+ states <- observedState{c.Label(), value.State, value.Capabilities, value.CoPlay, value.Grant, value.InputFailed}
}
})
c.OnOpen(func() { channels <- c })
@@ -1204,7 +1271,7 @@ func TestAttendedGrantLifecycle(t *testing.T) {
binary.BigEndian.PutUint64(header, cipher.sent)
_, _ = rand.Read(header[8:])
plain, _ := json.Marshal(event)
- epoch := bindings[peer].EndpointID
+ epoch := epochs[peer]
data := cipher.aead.Seal(header, header[8:], plain, inputAAD(0, header[:8], epoch))
packet, _ := json.Marshal(inputroute.Packet{ID: bindings[peer].EndpointID, Epoch: epoch, Data: base64.StdEncoding.EncodeToString(data)})
if err := peers[peer].SendText(string(packet)); err != nil {
@@ -1401,16 +1468,60 @@ func TestAttendedGrantLifecycle(t *testing.T) {
t.Fatal("baseline sync added unnegotiated fields")
}
send("first", inputEvent{Type: "capabilities"})
- if state := waitReply("first", true); *state.coPlay || len(state.capabilities) != 1 || state.capabilities[0] != "coPlay" {
+ if state := waitReply("first", true); *state.coPlay || len(state.capabilities) != 2 || state.capabilities[0] != "coPlay" || state.capabilities[1] != "inputFeedback" {
t.Fatal("extended Guest did not receive the explicit single-controller capability state")
}
send("second", inputEvent{Type: "sync"})
if state := waitReply("second", false); len(state.capabilities) != 0 {
t.Fatal("one Guest opted another Guest into the extension")
}
+ // The old capabilities command does not opt a strict reader into new keys.
+ m.mu.Lock()
+ sink.applyErr = errors.New("execution temporarily refused")
+ m.mu.Unlock()
+ send("first", inputEvent{Type: "move", X: .4, Y: .3, Grant: nextGrant})
+ wait(func() bool { return first.inputFailed })
+ if state := waitReply("first", true); state.inputFailed != nil || state.grant != nextGrant || state.state != "active" {
+ t.Fatal("legacy feedback changed JSON shape or retired its grant")
+ }
+ send("first", inputEvent{Type: "capabilities", InputFeedback: true})
+ if state := waitReply("first", true); state.inputFailed == nil || !*state.inputFailed || state.grant != nextGrant {
+ t.Fatal("explicit feedback opt-in did not report retained-grant failure")
+ }
+ m.mu.Lock()
+ sink.applyErr = nil
+ m.mu.Unlock()
+ send("first", inputEvent{Type: "move", X: .2, Y: .3, Grant: nextGrant})
+ wait(func() bool { return !first.inputFailed })
+ if state := waitReply("first", true); state.inputFailed != nil || state.grant != nextGrant || state.state != "active" {
+ t.Fatal("fresh input did not clear failure within the same grant")
+ }
+ // Replacing this endpoint's live epoch retires its opt-in along with its grant.
+ epochs["first"] = strings.Repeat("a", 32)
+ send("first", inputEvent{Type: "sync"})
+ if state := waitReply("first", false); state.inputFailed != nil || state.grant != "" {
+ t.Fatal("new epoch inherited grant or feedback fields")
+ }
+ first = request("first")
+ nextGrant = decide(first, true)
+ // Consume the active baseline reply before checking a later failure reply.
+ for state := waitReply("first", false); state.state != "active"; state = waitReply("first", false) {
+ }
+ m.mu.Lock()
+ sink.applyErr = errors.New("new epoch execution refused")
+ m.mu.Unlock()
+ send("first", inputEvent{Type: "move", X: .4, Y: .3, Grant: nextGrant})
+ wait(func() bool { return first.inputFailed })
+ if state := waitReply("first", false); state.inputFailed != nil || state.grant != nextGrant || state.state != "active" {
+ t.Fatal("new legacy epoch received an unnegotiated feedback field")
+ }
+ m.mu.Lock()
+ sink.applyErr = nil
+ m.mu.Unlock()
+ beforeCloseEvents, beforeCloseResets := len(sink.events), len(sink.resets)
s.Close()
key(first, nextGrant)
- if len(sink.events) != 4 || len(sink.resets) != 7 || len(s.State().Requests) != 0 || s.Decide(first.requestID, true) == nil {
+ if len(sink.events) != beforeCloseEvents || len(sink.resets) != beforeCloseResets+1 || len(s.State().Requests) != 0 || s.Decide(first.requestID, true) == nil {
t.Fatal("source retirement kept a grant")
}
}
diff --git a/internal/app/remoteinput/platform_darwin.go b/internal/app/remoteinput/platform_darwin.go
index def982e3..622bfdba 100644
--- a/internal/app/remoteinput/platform_darwin.go
+++ b/internal/app/remoteinput/platform_darwin.go
@@ -1,4 +1,4 @@
-//go:build darwin && cgo
+//go:build darwin && !ios && cgo
package remoteinput
@@ -51,11 +51,18 @@ func (*macSink) Target(target nativecapture.CaptureTarget, _ *nativecapture.Stre
if err != nil || id == 0 || (target.Kind != "display" && target.Kind != "window") {
return bounds{}, false, errors.New("shared source is unavailable")
}
- created, _ := strconv.ParseUint(target.CreationTime, 10, 64)
+ created, creationErr := strconv.ParseUint(target.CreationTime, 10, 64)
+ if target.Kind == "window" && (creationErr != nil || created == 0 || target.PID == 0) {
+ return bounds{}, false, errors.New("shared window identity is invalid")
+ }
var b C.PiikInputBounds
- if !bool(C.piik_input_target(C.bool(target.Kind == "window"), C.uint32_t(id), C.uint32_t(target.PID), C.uint64_t(created), &b)) {
+ result := C.piik_input_target(C.bool(target.Kind == "window"), C.uint32_t(id), C.uint32_t(target.PID), C.uint64_t(created), &b)
+ if result == C.PiikInputTargetInvalid {
return bounds{}, false, errors.New("shared source is unavailable")
}
+ if result != C.PiikInputTargetReady {
+ return bounds{}, false, nil
+ }
return bounds{int32(b.left), int32(b.top), int32(b.width), int32(b.height)}, bool(b.focused), nil
}
diff --git a/internal/app/remoteinput/platform_darwin.h b/internal/app/remoteinput/platform_darwin.h
index 86d612b8..3d9a1190 100644
--- a/internal/app/remoteinput/platform_darwin.h
+++ b/internal/app/remoteinput/platform_darwin.h
@@ -14,7 +14,13 @@ typedef struct {
int64_t count;
} PiikInputClick;
-bool piik_input_target(bool window, uint32_t id, uint32_t pid, uint64_t created,
+typedef enum {
+ PiikInputTargetUnavailable,
+ PiikInputTargetInvalid,
+ PiikInputTargetReady
+} PiikInputTargetResult;
+
+PiikInputTargetResult piik_input_target(bool window, uint32_t id, uint32_t pid, uint64_t created,
PiikInputBounds *bounds);
bool piik_input_key(CGEventSourceRef source, uint16_t code, bool down,
uint64_t flags, bool repeat);
diff --git a/internal/app/remoteinput/platform_darwin.m b/internal/app/remoteinput/platform_darwin.m
index 0c057544..8c5e163d 100644
--- a/internal/app/remoteinput/platform_darwin.m
+++ b/internal/app/remoteinput/platform_darwin.m
@@ -1,4 +1,4 @@
-//go:build darwin && cgo
+//go:build darwin && !ios && cgo
#include "platform_darwin.h"
#include <ApplicationServices/ApplicationServices.h>
@@ -40,27 +40,33 @@ static bool focused_window(uint32_t pid, CGRect rect) {
return focused;
}
-bool piik_input_target(bool window, uint32_t id, uint32_t pid, uint64_t created,
+PiikInputTargetResult piik_input_target(bool window, uint32_t id, uint32_t pid, uint64_t created,
PiikInputBounds *bounds) {
CGRect rect = CGRectNull;
bool focused = true;
if (!window) {
- if (!CGDisplayIsActive(id)) return false;
+ if (!CGDisplayIsActive(id)) return PiikInputTargetUnavailable;
rect = CGDisplayBounds(id);
} else {
struct proc_bsdinfo process = {0};
- if (!pid || proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &process, sizeof(process)) != sizeof(process) ||
- process.pbi_start_tvsec * 1000000ULL + process.pbi_start_tvusec != created) return false;
+ if (!pid || !created) return PiikInputTargetInvalid;
+ if (proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &process, sizeof(process)) != sizeof(process))
+ return PiikInputTargetUnavailable;
+ if (process.pbi_start_tvsec * 1000000ULL + process.pbi_start_tvusec != created)
+ return PiikInputTargetInvalid;
CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
- if (!windows) return false;
+ if (!windows) return PiikInputTargetUnavailable;
+ bool found = false;
for (CFIndex index = 0; index < CFArrayGetCount(windows); ++index) {
CFDictionaryRef entry = CFArrayGetValueAtIndex(windows, index);
int64_t current = 0, owner = 0;
if (!number(entry, kCGWindowNumber, &current)) continue;
if (current != id || !number(entry, kCGWindowOwnerPID, &owner) || owner != pid) continue;
+ found = true;
CFDictionaryRef geometry = CFDictionaryGetValue(entry, kCGWindowBounds);
if (geometry) CGRectMakeWithDictionaryRepresentation(geometry, &rect);
}
+ if (!found) { CFRelease(windows); return PiikInputTargetInvalid; }
// Public AX has no CGWindowID. Match the actual key window's geometry only
// when it identifies exactly one window of this process; z-order is not focus.
unsigned matches = 0;
@@ -75,9 +81,9 @@ bool piik_input_target(bool window, uint32_t id, uint32_t pid, uint64_t created,
focused = matches == 1 && focused_window(pid, rect);
CFRelease(windows);
}
- if (CGRectIsNull(rect) || CGRectIsEmpty(rect)) return false;
+ if (CGRectIsNull(rect) || CGRectIsEmpty(rect)) return PiikInputTargetUnavailable;
*bounds = (PiikInputBounds){rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, focused};
- return true;
+ return PiikInputTargetReady;
}
static bool post(CGEventRef event, uint64_t flags) {
diff --git a/internal/app/remoteinput/platform_stub.go b/internal/app/remoteinput/platform_stub.go
index f36fa227..9c483a75 100644
--- a/internal/app/remoteinput/platform_stub.go
+++ b/internal/app/remoteinput/platform_stub.go
@@ -1,4 +1,4 @@
-//go:build (!windows && !darwin && !linux) || (darwin && !cgo)
+//go:build ios || (!windows && !darwin && !linux) || (darwin && !cgo)
package remoteinput
diff --git a/internal/app/remoteinput/platform_windows.go b/internal/app/remoteinput/platform_windows.go
index 704e7bf2..e307ab91 100644
--- a/internal/app/remoteinput/platform_windows.go
+++ b/internal/app/remoteinput/platform_windows.go
@@ -5,6 +5,7 @@ package remoteinput
import (
"context"
"errors"
+ "fmt"
"math"
"runtime"
"unicode/utf16"
@@ -49,9 +50,14 @@ func newPlatformSink(bool) (inputSink, error) {
func (*windowsSink) Authorize() error { return nil }
func inject(inputs ...winInput) error {
- n, _, _ := sendInput.Call(uintptr(len(inputs)), uintptr(unsafe.Pointer(&inputs[0])), unsafe.Sizeof(inputs[0]))
+ n, _, err := sendInput.Call(uintptr(len(inputs)), uintptr(unsafe.Pointer(&inputs[0])), unsafe.Sizeof(inputs[0]))
if n != uintptr(len(inputs)) {
- return errors.New("Windows did not accept input")
+ // Windows does not identify UIPI blocking through GetLastError. Preserve
+ // the accepted count and any reported code without inventing that cause.
+ if err != windows.ERROR_SUCCESS {
+ return fmt.Errorf("Windows accepted %d of %d input events: %w", n, len(inputs), err)
+ }
+ return fmt.Errorf("Windows accepted %d of %d input events", n, len(inputs))
}
return nil
}
diff --git a/internal/app/remoteinput/target_windows.go b/internal/app/remoteinput/target_windows.go
index 1983fb27..f7e61f38 100644
--- a/internal/app/remoteinput/target_windows.go
+++ b/internal/app/remoteinput/target_windows.go
@@ -32,7 +32,8 @@ func (s *windowsSink) Target(target nativecapture.CaptureTarget, _ *nativecaptur
info.size = uint32(unsafe.Sizeof(info))
ok, _, _ := user32.NewProc("GetMonitorInfoW").Call(uintptr(id), uintptr(unsafe.Pointer(&info)))
if ok == 0 {
- return bounds{}, false, errors.New("shared display is unavailable")
+ // A failed monitor observation does not prove the capture was retired.
+ return bounds{}, false, nil
}
area = info.monitor
case "window":
@@ -41,22 +42,28 @@ func (s *windowsSink) Target(target nativecapture.CaptureTarget, _ *nativecaptur
if pid == 0 || pid != target.PID {
return bounds{}, false, errors.New("shared window is unavailable")
}
+ creation, parseErr := strconv.ParseUint(target.CreationTime, 10, 64)
+ if parseErr != nil || creation == 0 {
+ return bounds{}, false, errors.New("shared window identity is invalid")
+ }
// Match the capture target's process incarnation, not a recycled HWND/PID.
process, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
if err != nil {
- return bounds{}, false, err
+ return bounds{}, false, nil
}
var created, exited, kernel, user windows.Filetime
err = windows.GetProcessTimes(process, &created, &exited, &kernel, &user)
windows.CloseHandle(process)
- creation, parseErr := strconv.ParseUint(target.CreationTime, 10, 64)
- if err != nil || parseErr != nil || creation != uint64(created.HighDateTime)<<32|uint64(created.LowDateTime) {
+ if err != nil {
+ return bounds{}, false, nil
+ }
+ if creation != uint64(created.HighDateTime)<<32|uint64(created.LowDateTime) {
return bounds{}, false, errors.New("shared window was replaced")
}
if hr, _, _ := extendedFrameBounds.Call(uintptr(id), 9, uintptr(unsafe.Pointer(&area)), unsafe.Sizeof(area)); int32(hr) < 0 {
ok, _, _ := user32.NewProc("GetWindowRect").Call(uintptr(id), uintptr(unsafe.Pointer(&area)))
if ok == 0 {
- return bounds{}, false, errors.New("shared window geometry is unavailable")
+ return bounds{}, false, nil
}
}
foreground, _, _ := user32.NewProc("GetForegroundWindow").Call()
@@ -67,7 +74,7 @@ func (s *windowsSink) Target(target nativecapture.CaptureTarget, _ *nativecaptur
return bounds{}, false, errors.New("source cannot accept input")
}
if area.right <= area.left || area.bottom <= area.top {
- return bounds{}, false, errors.New("shared source has no area")
+ return bounds{}, false, nil
}
return bounds{area.left, area.top, area.right - area.left, area.bottom - area.top}, focused, nil
}
diff --git a/internal/app/remoteinput/target_windows_test.go b/internal/app/remoteinput/target_windows_test.go
new file mode 100644
index 00000000..4cba67d5
--- /dev/null
+++ b/internal/app/remoteinput/target_windows_test.go
@@ -0,0 +1,109 @@
+//go:build windows
+
+package remoteinput
+
+import (
+ "runtime"
+ "strconv"
+ "testing"
+ "unsafe"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "golang.org/x/sys/windows"
+)
+
+func TestWindowTargetObservations(t *testing.T) {
+ // A hidden zero-style fixture never activates or injects desktop input.
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+ class, _ := windows.UTF16PtrFromString("STATIC")
+ window, _, err := user32.NewProc("CreateWindowExW").Call(0, uintptr(unsafe.Pointer(class)), 0, 0x80000000, 0, 0, 320, 200, 0, 0, 0, 0)
+ if window == 0 {
+ t.Fatalf("create hidden fixture: %v", err)
+ }
+ defer func() {
+ if window != 0 {
+ user32.NewProc("DestroyWindow").Call(window)
+ }
+ }()
+ var pid uint32
+ user32.NewProc("GetWindowThreadProcessId").Call(window, uintptr(unsafe.Pointer(&pid)))
+ var created, exited, kernel, user windows.Filetime
+ process, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer windows.CloseHandle(process)
+ if err := windows.GetProcessTimes(process, &created, &exited, &kernel, &user); err != nil {
+ t.Fatal(err)
+ }
+ target := nativecapture.CaptureTarget{Kind: "window", SourceID: strconv.FormatUint(uint64(window), 10), PID: pid, CreationTime: strconv.FormatUint(uint64(created.HighDateTime)<<32|uint64(created.LowDateTime), 10)}
+ sink := &windowsSink{}
+ if b, _, err := sink.Target(target, nil); err != nil || b.width <= 0 || b.height <= 0 {
+ t.Fatalf("valid geometry: %+v %v", b, err)
+ }
+ user32.NewProc("SetWindowPos").Call(window, 0, 0, 0, 0, 0, 0x0014) // NOZORDER | NOACTIVATE
+ if b, focused, err := sink.Target(target, nil); err != nil || focused || b != (bounds{}) {
+ t.Fatalf("empty geometry must suspend: %+v %v %v", b, focused, err)
+ }
+ user32.NewProc("SetWindowPos").Call(window, 0, 0, 0, 320, 200, 0x0014)
+ if b, _, err := sink.Target(target, nil); err != nil || b.width <= 0 {
+ t.Fatalf("geometry did not recover: %+v %v", b, err)
+ }
+ target.CreationTime = "1"
+ if _, _, err := sink.Target(target, nil); err == nil {
+ t.Fatal("replaced process identity accepted")
+ }
+ target.PID++
+ if _, _, err := sink.Target(target, nil); err == nil {
+ t.Fatal("different window owner accepted")
+ }
+ user32.NewProc("DestroyWindow").Call(window)
+ window = 0
+ if _, _, err := sink.Target(target, nil); err == nil {
+ t.Fatal("destroyed window accepted")
+ }
+}
+
+type monitorObservationSink struct {
+ *recordedInput
+ unavailable bool
+}
+
+func (s *monitorObservationSink) Target(target nativecapture.CaptureTarget, stream *nativecapture.Stream) (bounds, bool, error) {
+ if s.unavailable {
+ target.SourceID = "1"
+ }
+ return (&windowsSink{}).Target(target, stream)
+}
+
+func TestMonitorObservationSuspendsSameGrant(t *testing.T) {
+ monitor, _, _ := user32.NewProc("MonitorFromPoint").Call(0, 1)
+ if monitor == 0 {
+ t.Skip("no interactive display")
+ }
+ sink := &monitorObservationSink{recordedInput: &recordedInput{events: make(chan inputEvent, 16), resets: make(chan struct{}, 16), focused: true}}
+ m := &Manager{sink: sink, connections: map[endpointKey]*connection{}}
+ s, peers := coPlaySession(t, m, true)
+ s.target = nativecapture.CaptureTarget{Kind: "display", SourceID: strconv.FormatUint(uint64(monitor), 10)}
+ c := peers[0]
+ sendTestInput(m, c, inputEvent{Type: "request"})
+ grant := c.grant
+ sendTestInput(m, c, inputEvent{Type: "key", Code: "KeyA", Down: true})
+ <-sink.events
+ sink.unavailable = true
+ m.refreshActive()
+ sendTestInput(m, c, inputEvent{Type: "move", X: .8, Y: .4})
+ if grant == "" || c.grant != grant || !c.suspended || len(c.keys) != 0 || len(sink.events) != 0 {
+ t.Fatal("unavailable monitor did not safely suspend same grant")
+ }
+ sink.unavailable = false
+ m.refreshActive()
+ if c.suspended || c.grant != grant || len(sink.events) != 0 {
+ t.Fatal("monitor recovery changed grant or replayed input")
+ }
+ sendTestInput(m, c, inputEvent{Type: "move", X: .4, Y: .2})
+ if len(sink.events) != 1 {
+ t.Fatal("new input did not resume after monitor observation")
+ }
+}
diff --git a/internal/app/remoteinput/text.go b/internal/app/remoteinput/text.go
index a7033230..cbe1db92 100644
--- a/internal/app/remoteinput/text.go
+++ b/internal/app/remoteinput/text.go
@@ -11,7 +11,12 @@ const maxInputBytes = maxTextBytes*2 + 256 // JSON escaping and grant fields.
// Text is one explicit submission. Newlines and tabs use real keys on every
// platform; Unicode events handle the remaining text independently of layout.
-func applyText(sink inputSink, target bounds, text string) error {
+type textSink interface {
+ Reset() error
+ Apply(bounds, inputEvent) error
+}
+
+func applyText(sink textSink, target bounds, text string) error {
if text == "" || len(text) > maxTextBytes || !utf8.ValidString(text) {
return errors.New("invalid input text")
}
diff --git a/internal/httpassets/compress.go b/internal/httpassets/compress.go
new file mode 100644
index 00000000..bc87e7e1
--- /dev/null
+++ b/internal/httpassets/compress.go
@@ -0,0 +1,19 @@
+// Package httpassets supplies shared HTTP delivery for Piik's Web assets.
+package httpassets
+
+import (
+ "net/http"
+
+ "github.com/klauspost/compress/gzhttp"
+)
+
+// Compress negotiates gzip for text assets, leaving precompressed media alone.
+func Compress(handler http.Handler) http.Handler {
+ compress, err := gzhttp.NewWrapper(gzhttp.EnableZstd(false), gzhttp.ContentTypes([]string{
+ "text/html", "text/javascript", "application/javascript", "text/css",
+ }))
+ if err != nil {
+ panic(err)
+ }
+ return compress(handler)
+}
diff --git a/internal/media/encoded/packetizer.go b/internal/media/encoded/packetizer.go
index 8e31640c..dbc1d221 100644
--- a/internal/media/encoded/packetizer.go
+++ b/internal/media/encoded/packetizer.go
@@ -33,9 +33,6 @@ type Packetizer struct {
hasTimestamp bool
lastTimestamp time.Duration
timestampRemainder float64
- rebasePending bool
- rebaseInput time.Duration
- rebaseOutput time.Duration
}
func NewPacketizer(payloader rtp.Payloader, payloadType uint8, mtu uint16) *Packetizer {
@@ -48,17 +45,6 @@ func (p *Packetizer) Packetize(data []byte, timestamp, duration time.Duration) (
if len(data) == 0 || len(data) > MaxAccessUnitBytes || timestamp < 0 || duration <= 0 {
return nil, errors.New("encoded video access unit is invalid")
}
- if p.rebasePending {
- p.rebasePending = false
- p.rebaseInput = timestamp
- p.rebaseOutput = p.lastTimestamp + duration
- timestamp = p.rebaseOutput
- } else if p.rebaseOutput > 0 {
- if timestamp < p.rebaseInput {
- return nil, ErrInvalidTimestamp
- }
- timestamp = p.rebaseOutput + timestamp - p.rebaseInput
- }
if p.hasTimestamp {
if timestamp <= p.lastTimestamp {
return nil, ErrInvalidTimestamp
@@ -77,11 +63,3 @@ func (p *Packetizer) Packetize(data []byte, timestamp, duration time.Duration) (
}
return packets, nil
}
-
-// BeginGeneration rebases a replacement source clock. Layer changes within the
-// same source must not call it: they retain the original source timestamps.
-func (p *Packetizer) BeginGeneration() {
- p.rebasePending = true
- p.rebaseInput = 0
- p.rebaseOutput = 0
-}
diff --git a/internal/media/encoded/packetizer_test.go b/internal/media/encoded/packetizer_test.go
index 7a5d981e..e7f9468e 100644
--- a/internal/media/encoded/packetizer_test.go
+++ b/internal/media/encoded/packetizer_test.go
@@ -51,10 +51,12 @@ func TestPacketizerKeepsVP8IdentityAcrossAccessUnits(t *testing.T) {
if _, err := p.Packetize([]byte{1}, 0, time.Second/30); !errors.Is(err, ErrInvalidTimestamp) {
t.Fatalf("backward source timestamp error = %v", err)
}
- p.BeginGeneration()
- packets, err := p.Packetize([]byte{1}, 0, time.Second/30)
+ packets, err := p.Packetize([]byte{1}, 5*time.Second, time.Second/30)
if err != nil || len(packets) != 1 || packets[0].SequenceNumber != sequence+1 {
- t.Fatalf("source generation did not retain packet identity: %v", err)
+ t.Fatalf("dormant source did not retain packet identity: %v", err)
+ }
+ if advance := packets[0].Timestamp - timestamp; advance < 440999 || advance > 441001 {
+ t.Fatalf("dormant source clock advanced %d ticks, want 4.9 seconds", advance)
}
}
diff --git a/internal/media/forwarding/encoded_source.go b/internal/media/forwarding/encoded_source.go
index 2f1a9d00..e0b05af0 100644
--- a/internal/media/forwarding/encoded_source.go
+++ b/internal/media/forwarding/encoded_source.go
@@ -27,7 +27,10 @@ type EncodedSource struct {
inputPTS time.Duration
anchorPTS time.Duration
anchorTime time.Time
+ originTime time.Time
hasInput bool
+ clockSource *Source
+ clockLayer int
}
func NewEncodedSource(options SourceOptions) (*EncodedSource, error) {
@@ -58,6 +61,12 @@ func NewEncodedSource(options SourceOptions) (*EncodedSource, error) {
return input, nil
}
+// UseSourceClock binds derived output to the original layer's current remote
+// clock offset. The owner installs this before feeding any encoded frames.
+func (input *EncodedSource) UseSourceClock(source *Source, layer int) {
+ input.clockSource, input.clockLayer = source, layer
+}
+
func (input *EncodedSource) BeginFrame(pts time.Duration, at time.Time) error {
if input.IsClosed() {
return errors.New("encoded forwarding source is closed")
@@ -68,6 +77,9 @@ func (input *EncodedSource) BeginFrame(pts time.Duration, at time.Time) error {
if !input.hasInput {
input.anchorPTS, input.anchorTime = pts, at
}
+ if input.originTime.IsZero() {
+ input.originTime = at
+ }
input.hasInput, input.inputPTS = true, pts
return nil
}
@@ -77,7 +89,8 @@ func (input *EncodedSource) WriteFrame(layer int, frame encoded.Frame) error {
frame.PTS < input.anchorPTS || frame.PTS > input.inputPTS {
return errors.New("encoded output has no matching source input")
}
- packets, err := input.packetizers[layer].Packetize(frame.Data, frame.PTS, frame.Duration)
+ sourceTime := input.anchorTime.Add(frame.PTS - input.anchorPTS)
+ packets, err := input.packetizers[layer].Packetize(frame.Data, sourceTime.Sub(input.originTime), frame.Duration)
if err != nil {
return err
}
@@ -91,12 +104,22 @@ func (input *EncodedSource) WriteFrame(layer int, frame encoded.Frame) error {
input.rtpBytes[layer] += uint64(packet.MarshalSize())
}
input.codecBytes[layer] += uint64(len(frame.Data))
+ // Retain the capture instant's Go monotonic component when converting to
+ // the media core's epoch-based monotonic clock, including after wall jumps.
+ localTime := mono.FromTime(sourceTime)
+ if input.clockSource != nil {
+ var ok bool
+ localTime, ok = input.clockSource.LocalTime(input.clockLayer, sourceTime)
+ if !ok {
+ return errors.New("encoded output has no source clock reference")
+ }
+ }
// Correlate each layer with the actual input clock, not its encoder finish
// time. This is internal correlation data, not a backdated wire sender report.
return input.SetCorrelation(layer, &livekit.RTCPSenderReportState{
RtpTimestamp: packets[0].Timestamp,
- NtpTimestamp: uint64(mediatransportutil.ToNtpTime(input.anchorTime.Add(frame.PTS - input.anchorPTS))),
- Packets: input.packets[layer], Octets: input.octets[layer], At: mono.UnixNano(),
+ NtpTimestamp: uint64(mediatransportutil.ToNtpTime(sourceTime)),
+ Packets: input.packets[layer], Octets: input.octets[layer], At: localTime.UnixNano(),
})
}
@@ -114,11 +137,8 @@ func (input *EncodedSource) CodecBudget(layer int, rtpBudget int64) int64 {
}
func (input *EncodedSource) BeginGeneration() {
- // Capture replacement changes the input clock, not these RTP identities.
- // Rebase the packetizers; restarting DownTrack as well would rebase twice.
- for _, packetizer := range input.packetizers {
- packetizer.BeginGeneration()
- }
+ // Replacement resets input PTS, but all renditions retain the elapsed
+ // capture clock and RTP identities, including time spent dormant.
input.hasInput = false
clear(input.codecBytes)
clear(input.rtpBytes)
diff --git a/internal/media/forwarding/output.go b/internal/media/forwarding/output.go
index ba30039f..b1692077 100644
--- a/internal/media/forwarding/output.go
+++ b/internal/media/forwarding/output.go
@@ -275,7 +275,9 @@ func (receiver *outputReceiver) AddDownTrack(sfu.TrackSender) error {
sender := &outputSourceSender{Output: receiver.output, receiver: receiver}
sender.registering.Store(true)
err := receiver.Source.AddDownTrack(sender)
+ sender.reportMu.Lock()
sender.registering.Store(false)
+ sender.reportMu.Unlock()
return err
}
@@ -283,6 +285,7 @@ type outputSourceSender struct {
*Output
receiver *outputReceiver
registering atomic.Bool
+ reportMu sync.Mutex
}
func (sender *outputSourceSender) WriteRTP(packet *buffer.ExtPacket, layer int32) int32 {
@@ -329,13 +332,28 @@ func (sender *outputSourceSender) UpTrackBitrateReport(layers []int32, rates sfu
sender.withReceiver(func() { sender.Output.UpTrackBitrateReport(layers, rates) })
}
-func (sender *outputSourceSender) HandleRTCPSenderReportData(payloadType webrtc.PayloadType, layer int32, report *livekit.RTCPSenderReportState) error {
+func (sender *outputSourceSender) HandleRTCPSenderReportData(payloadType webrtc.PayloadType, layer int32, _ *livekit.RTCPSenderReportState) error {
+ // Registration already owns writeMu. Serialize bootstrap callbacks and
+ // read accepted state so a queued older snapshot cannot replace a live SR.
+ apply := func() error {
+ if sender.Output.Receiver() != sender.receiver {
+ return nil
+ }
+ current := sender.receiver.currentSenderReport(layer)
+ if current == nil {
+ return nil
+ }
+ return sender.Output.HandleRTCPSenderReportData(payloadType, layer, current)
+ }
+ sender.reportMu.Lock()
+ if sender.registering.Load() {
+ defer sender.reportMu.Unlock()
+ return apply()
+ }
+ sender.reportMu.Unlock()
sender.Output.writeMu.Lock()
defer sender.Output.writeMu.Unlock()
- if sender.Output.Receiver() != sender.receiver {
- return nil
- }
- return sender.Output.HandleRTCPSenderReportData(payloadType, layer, report)
+ return apply()
}
func (output *Output) replaceReceiver(receiver *outputReceiver) error {
diff --git a/internal/media/forwarding/publication.go b/internal/media/forwarding/publication.go
index 9fbd7f5e..d55c29ef 100644
--- a/internal/media/forwarding/publication.go
+++ b/internal/media/forwarding/publication.go
@@ -399,7 +399,9 @@ func (layer *publicationLayer) AddDownTrack(sender sfu.TrackSender) error {
}
filtered.registering.Store(true)
err := layer.Source.AddDownTrack(filtered)
+ filtered.reportMu.Lock()
filtered.registering.Store(false)
+ filtered.reportMu.Unlock()
return err
}
@@ -428,6 +430,7 @@ type publicationLayerSender struct {
layer int32
receiver *publicationLayer
registering atomic.Bool
+ reportMu sync.Mutex
}
func (sender *publicationLayerSender) UpTrackBitrateReport(available []int32, rates sfu.Bitrates) {
@@ -482,13 +485,26 @@ func (sender *publicationLayerSender) UpTrackMaxTemporalLayerSeenChange(layer in
sender.withReceiver(func() { sender.DownTrack.UpTrackMaxTemporalLayerSeenChange(layer) })
}
-func (sender *publicationLayerSender) HandleRTCPSenderReportData(payloadType webrtc.PayloadType, layer int32, report *livekit.RTCPSenderReportState) error {
+func (sender *publicationLayerSender) HandleRTCPSenderReportData(payloadType webrtc.PayloadType, layer int32, _ *livekit.RTCPSenderReportState) error {
+ apply := func() error {
+ if sender.Receiver() != sender.receiver {
+ return nil
+ }
+ current := sender.receiver.currentSenderReport(layer)
+ if current == nil {
+ return nil
+ }
+ return sender.DownTrack.HandleRTCPSenderReportData(payloadType, layer, current)
+ }
+ sender.reportMu.Lock()
+ if sender.registering.Load() {
+ defer sender.reportMu.Unlock()
+ return apply()
+ }
+ sender.reportMu.Unlock()
sender.receiver.publication.writeMu.Lock()
defer sender.receiver.publication.writeMu.Unlock()
- if sender.Receiver() != sender.receiver {
- return nil
- }
- return sender.DownTrack.HandleRTCPSenderReportData(payloadType, layer, report)
+ return apply()
}
type publicationTrack struct {
diff --git a/internal/media/forwarding/publication_clock_test.go b/internal/media/forwarding/publication_clock_test.go
new file mode 100644
index 00000000..3ef9f115
--- /dev/null
+++ b/internal/media/forwarding/publication_clock_test.go
@@ -0,0 +1,154 @@
+package forwarding
+
+import (
+ "context"
+ "net"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/TNTcraftHIM/Piik/internal/media/encoded"
+ "github.com/livekit/livekit-server/pkg/sfu"
+ "github.com/pion/webrtc/v4"
+)
+
+func TestPublicationDormantLayerRetainsClockAcrossGeneration(t *testing.T) {
+ ctx, cancel := context.WithTimeout(t.Context(), 15*time.Second)
+ defer cancel()
+ check := func(err error) {
+ t.Helper()
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ source, err := NewEncodedSource(clockSourceOptions("asymmetric-generation"))
+ check(err)
+ defer source.Close()
+ settings := webrtc.SettingEngine{}
+ settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
+ settings.SetIncludeLoopbackCandidate(true)
+ settings.SetIPFilter(func(ip net.IP) bool { return ip.IsLoopback() })
+ publication, err := NewPublication(TransportOptions{Source: source.Source, Settings: settings, ConnectionID: "asymmetric-generation", InitialBitrate: 2_000_000})
+ check(err)
+ defer publication.Close()
+ media := &webrtc.MediaEngine{}
+ check(media.RegisterDefaultCodecs())
+ check(media.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: publicationRIDExtension}, webrtc.RTPCodecTypeVideo))
+ check(media.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: "urn:ietf:params:rtp-hdrext:sdes:mid"}, webrtc.RTPCodecTypeVideo))
+ receiver, err := webrtc.NewAPI(webrtc.WithMediaEngine(media), webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})
+ check(err)
+ defer receiver.Close()
+ var packets [2]atomic.Uint32
+ var timestamps [2]atomic.Uint32
+ receiver.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
+ index := 0
+ if track.RID() == "h" {
+ index = 1
+ }
+ for {
+ packet, _, err := track.ReadRTP()
+ if err != nil {
+ return
+ }
+ if packet.Marker && len(packet.Payload) > 0 {
+ timestamps[index].Store(packet.Timestamp)
+ packets[index].Add(1)
+ }
+ }
+ })
+ offer, err := publication.PC.CreateOffer(nil)
+ check(err)
+ gather := webrtc.GatheringCompletePromise(publication.PC)
+ check(publication.PC.SetLocalDescription(offer))
+ select {
+ case <-gather:
+ case <-ctx.Done():
+ t.Fatal(ctx.Err())
+ }
+ check(receiver.SetRemoteDescription(*publication.PC.LocalDescription()))
+ answer, err := receiver.CreateAnswer(nil)
+ check(err)
+ gather = webrtc.GatheringCompletePromise(receiver)
+ check(receiver.SetLocalDescription(answer))
+ select {
+ case <-gather:
+ case <-ctx.Done():
+ t.Fatal(ctx.Err())
+ }
+ check(publication.PC.SetRemoteDescription(*receiver.LocalDescription()))
+ for publication.PC.ConnectionState() != webrtc.PeerConnectionStateConnected {
+ select {
+ case <-time.After(time.Millisecond):
+ case <-ctx.Done():
+ t.Fatal(ctx.Err())
+ }
+ }
+ check(publication.SetConnected())
+ origin := time.Now().Add(-2 * time.Second)
+ // Stream trackers attach asynchronously; establish both actual wire outputs.
+ for frame := 0; frame < 30; frame++ {
+ pts := time.Duration(frame) * time.Second / 60
+ check(source.BeginFrame(pts, origin.Add(pts)))
+ for layer := 0; layer < 2; layer++ {
+ check(source.WriteFrame(layer, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: pts, Duration: time.Second / 30, Recovery: true}))
+ }
+ time.Sleep(40 * time.Millisecond)
+ }
+ write := func(pts time.Duration, at time.Time, layers ...int) {
+ t.Helper()
+ check(source.BeginFrame(pts, at))
+ for _, layer := range layers {
+ before := packets[layer].Load()
+ check(source.WriteFrame(layer, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: pts, Duration: time.Second / 30, Recovery: true}))
+ for packets[layer].Load() == before {
+ select {
+ case <-time.After(time.Millisecond):
+ case <-ctx.Done():
+ t.Fatalf("layer %d did not forward: %v", layer, ctx.Err())
+ }
+ }
+ }
+ }
+ write(time.Second, origin.Add(time.Second), 0)
+ write(2*time.Second, origin.Add(2*time.Second), 1)
+ previousHigh := timestamps[1].Load()
+ for layer, track := range publication.tracks {
+ sr := track.CreateSenderReport()
+ if sr == nil {
+ t.Fatalf("initial layer %d report absent", layer)
+ }
+ }
+ source.BeginGeneration()
+ replacementOrigin := time.Now()
+ write(0, replacementOrigin, 0)
+ time.Sleep(200 * time.Millisecond)
+ highBefore := publication.tracks[1].CreateSenderReport()
+ if highBefore == nil {
+ t.Fatal("dormant high report absent before reentry")
+ }
+ // Wait until the new capture has advanced 5s while only the low rendition runs.
+ timer := time.NewTimer(time.Until(replacementOrigin.Add(5 * time.Second)))
+ defer timer.Stop()
+ select {
+ case <-timer.C:
+ case <-ctx.Done():
+ t.Fatal(ctx.Err())
+ }
+ // The dormant high SR has advanced normally on its previous timestamp clock.
+ highLatest := publication.tracks[1].CreateSenderReport()
+ if highLatest == nil {
+ t.Fatal("dormant high report absent at 5s")
+ }
+ write(5*time.Second, replacementOrigin.Add(5*time.Second), 0, 1)
+ highAfter := publication.tracks[1].CreateSenderReport()
+ if highAfter == nil {
+ t.Fatal("dormant high rendition resumes with a backward clock; actual retained publication cannot emit its new sender report")
+ }
+ // A fixed initial RTP anchor is valid. Its elapsed clock must survive the
+ // replacement even though the renditions last emitted different input PTS.
+ expected := replacementOrigin.Add(5 * time.Second).Sub(origin.Add(2 * time.Second))
+ actual := time.Duration(timestamps[1].Load()-previousHigh) * time.Second / 90_000
+ if mismatch := actual - expected; mismatch.Abs() > time.Millisecond {
+ t.Fatalf("replacement changed the wire capture clock by %v", mismatch)
+ }
+}
diff --git a/internal/media/forwarding/retarget_test.go b/internal/media/forwarding/retarget_test.go
index 2035c2c0..b2b0f2a1 100644
--- a/internal/media/forwarding/retarget_test.go
+++ b/internal/media/forwarding/retarget_test.go
@@ -3,16 +3,31 @@ package forwarding
import (
"context"
"net"
+ "sync"
"sync/atomic"
"testing"
"time"
"github.com/TNTcraftHIM/Piik/internal/media/encoded"
"github.com/livekit/livekit-server/pkg/sfu"
+ "github.com/livekit/livekit-server/pkg/sfu/buffer"
+ "github.com/livekit/protocol/livekit"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v4"
)
+type heldSourceClose struct {
+ buffer.BufferProvider
+ entered chan struct{}
+ release chan struct{}
+}
+
+func (input *heldSourceClose) CloseWithReason(reason string) (*livekit.RTPStats, error) {
+ close(input.entered)
+ <-input.release
+ return input.BufferProvider.CloseWithReason(reason)
+}
+
func TestSourceRetargetPreservesTransportAndPublication(t *testing.T) {
for _, simulcast := range []bool{false, true} {
name := "transport"
@@ -193,6 +208,37 @@ func TestSourceRetargetPreservesTransportAndPublication(t *testing.T) {
t.Fatal("retarget lost per-RID forwarding or counters")
}
}
+ queued := next.GetDownTracks()
+ var snapshots [2]*livekit.RTCPSenderReportState
+ for layer := range snapshots {
+ snapshots[layer] = next.GetAllBuffers()[layer].GetSenderReportData()
+ }
+ blocked := &heldSourceClose{BufferProvider: next.GetAllBuffers()[0],
+ entered: make(chan struct{}), release: make(chan struct{})}
+ next.ReceiverBase.AddBuffer(blocked, 0)
+ var release sync.Once
+ unblock := func() { release.Do(func() { close(blocked.release) }) }
+ defer unblock()
+ retired := make(chan struct{})
+ go func() { next.Close(); close(retired) }()
+ select {
+ case <-blocked.entered:
+ case <-ctx.Done():
+ t.Fatal(ctx.Err())
+ }
+ // ClearAllBuffers runs before DownTrack retirement. A queued SR still
+ // has the same receiver, but its current clock has already disappeared.
+ for _, sender := range queued {
+ for layer, snapshot := range snapshots {
+ check(sender.HandleRTCPSenderReportData(codec.PayloadType, int32(layer), snapshot))
+ }
+ }
+ unblock()
+ select {
+ case <-retired:
+ case <-ctx.Done():
+ t.Fatal(ctx.Err())
+ }
check(closeTransport())
if len(next.GetDownTracks()) != 0 || replace(old.Source) == nil {
t.Fatal("retarget retained membership after closure")
diff --git a/internal/media/forwarding/source.go b/internal/media/forwarding/source.go
index a1b553be..97d44e7a 100644
--- a/internal/media/forwarding/source.go
+++ b/internal/media/forwarding/source.go
@@ -26,6 +26,21 @@ type LayerFormat struct {
Width, Height, Bitrate uint32
}
+// Internal encoders and RTP copies already know the local time of their source
+// RTP/NTP correlation. Received reports still need the remote clock estimate.
+type sourceBuffer struct {
+ *buffer.Buffer
+ knownInputClock atomic.Bool
+}
+
+func (input *sourceBuffer) GetSenderReportData() *livekit.RTCPSenderReportState {
+ report := input.Buffer.GetSenderReportData()
+ if report != nil && input.knownInputClock.Load() {
+ report.AtAdjusted = report.At
+ }
+ return report
+}
+
type SourceOptions struct {
ID, StreamID string
Codec webrtc.RTPCodecParameters
@@ -41,7 +56,7 @@ type SourceOptions struct {
type Source struct {
*sfu.ReceiverBase
mu sync.Mutex
- buffers []*buffer.Buffer
+ buffers []*sourceBuffer
maxPackets int
onRTCP func(int, []rtcp.Packet)
closed bool
@@ -85,7 +100,7 @@ func NewSource(options SourceOptions) (*Source, error) {
HeaderExtensions: canonicalHeaderExtensions(options.HeaderExtensions), Logger: diagnostics.MediaLogger("source").WithValues("trackId", diagnostics.ID(options.ID)),
StreamTrackerManagerConfig: sfu.DefaultStreamTrackerManagerConfig,
}, info, sfu.ReceiverCodecStateNormal),
- buffers: make([]*buffer.Buffer, len(options.Formats)),
+ buffers: make([]*sourceBuffer, len(options.Formats)),
maxPackets: options.MaxPackets, onRTCP: options.OnRTCP,
}
source.currentTrackInfo.Store(info)
@@ -99,6 +114,7 @@ func (source *Source) TrackInfo() *livekit.TrackInfo {
// ReceiverBase reads initial metadata before registering the subscriber. A
// concurrent discovery notification can fall into that gap. Refresh after
// registration; DownTrack's seen-layer setters retain the highest observation.
+// Replay accepted clocks so a new forwarder need not await another wire report.
func (source *Source) AddDownTrack(track sfu.TrackSender) error {
if err := source.ReceiverBase.AddDownTrack(track); err != nil {
return err
@@ -106,9 +122,24 @@ func (source *Source) AddDownTrack(track sfu.TrackSender) error {
tracker := source.StreamTrackerManager()
track.UpTrackMaxPublishedLayerChange(tracker.GetMaxPublishedLayer())
track.UpTrackMaxTemporalLayerSeenChange(tracker.GetMaxTemporalLayerSeen())
+ for layer, input := range source.ReceiverBase.GetAllBuffers() {
+ if input != nil {
+ if report := input.GetSenderReportData(); report != nil {
+ _ = track.HandleRTCPSenderReportData(source.Codec().PayloadType, int32(layer), report)
+ }
+ }
+ }
return nil
}
+func (source *Source) currentSenderReport(layer int32) *livekit.RTCPSenderReportState {
+ inputs := source.ReceiverBase.GetAllBuffers()
+ if layer < 0 || int(layer) >= len(inputs) || inputs[layer] == nil {
+ return nil
+ }
+ return inputs[layer].GetSenderReportData()
+}
+
func canonicalHeaderExtensions(extensions []webrtc.RTPHeaderExtensionParameter) []webrtc.RTPHeaderExtensionParameter {
result := slices.Clone(extensions)
slices.SortFunc(result, func(left, right webrtc.RTPHeaderExtensionParameter) int {
@@ -221,7 +252,7 @@ func (source *Source) BindLayer(layer int, ssrc uint32, parameters webrtc.RTPPar
return errors.New("forwarded video layers cannot share an SSRC")
}
}
- input := buffer.NewBuffer(ssrc, source.maxPackets, buffer.InitPacketBufferSizeAudio)
+ input := &sourceBuffer{Buffer: buffer.NewBuffer(ssrc, source.maxPackets, buffer.InitPacketBufferSizeAudio)}
input.SetLogger(diagnostics.MediaLogger("rtp-buffer").WithValues("trackId", diagnostics.ID(string(source.TrackID()))))
if err := input.Bind(parameters, codec.RTPCodecCapability, int(source.TrackInfo().Layers[layer].Bitrate)); err != nil {
_ = input.Close()
@@ -236,7 +267,7 @@ func (source *Source) BindLayer(layer int, ssrc uint32, parameters webrtc.RTPPar
return nil
}
-func (source *Source) layerBuffer(layer int) (*buffer.Buffer, error) {
+func (source *Source) layerBuffer(layer int) (*sourceBuffer, error) {
source.mu.Lock()
defer source.mu.Unlock()
if source.closed {
@@ -295,7 +326,39 @@ func (source *Source) ReferenceTime(layer int, timestamp uint32) (time.Time, boo
return mediatransportutil.NtpTime(report.NtpTimestamp).Time().Add(delta), true
}
-// SetCorrelation receives the local source owner's RTP/NTP mapping unchanged.
+// LocalTime projects the source's clock through its current received report.
+// A derived encoder shares this clock offset with the untouched original layer.
+func (source *Source) LocalTime(layer int, sourceTime time.Time) (time.Time, bool) {
+ input, err := source.layerBuffer(layer)
+ if err != nil {
+ return time.Time{}, false
+ }
+ report := input.GetSenderReportData()
+ if report == nil || report.NtpTimestamp == 0 || report.AtAdjusted == 0 {
+ return time.Time{}, false
+ }
+ delta := sourceTime.Sub(mediatransportutil.NtpTime(report.NtpTimestamp).Time())
+ return time.Unix(0, report.AtAdjusted).Add(delta), true
+}
+
+// CopyCorrelation shares an already received clock with an internal RTP copy.
+// The original owns network-delay estimation; this buffer still extends and
+// orders its own RTP timestamps, including when it starts after a wrap.
+func (source *Source) CopyCorrelation(layer int, original *Source) error {
+ input, err := original.layerBuffer(layer)
+ if err != nil {
+ return err
+ }
+ report := input.GetSenderReportData()
+ if report == nil {
+ return nil
+ }
+ report.At = report.AtAdjusted
+ return source.SetCorrelation(layer, report)
+}
+
+// SetCorrelation receives the source owner's RTP/NTP mapping and its known
+// local input time in At. Encoder completion is not a network delay observation.
// The layer must already have received RTP; the media core owns report ordering.
func (source *Source) SetCorrelation(layer int, correlation *livekit.RTCPSenderReportState) error {
input, err := source.layerBuffer(layer)
@@ -305,6 +368,7 @@ func (source *Source) SetCorrelation(layer int, correlation *livekit.RTCPSenderR
if correlation == nil {
return errors.New("forwarded source correlation is absent")
}
+ input.knownInputClock.Store(true)
input.SetSenderReportData(correlation)
return nil
}
diff --git a/internal/media/forwarding/source_clock_test.go b/internal/media/forwarding/source_clock_test.go
new file mode 100644
index 00000000..6de2d4ea
--- /dev/null
+++ b/internal/media/forwarding/source_clock_test.go
@@ -0,0 +1,144 @@
+package forwarding
+
+import (
+ "testing"
+ "time"
+
+ "github.com/TNTcraftHIM/Piik/internal/media/encoded"
+ "github.com/livekit/livekit-server/pkg/sfu"
+ "github.com/livekit/livekit-server/pkg/sfu/rtpstats"
+ "github.com/livekit/mediatransportutil"
+ "github.com/livekit/protocol/utils/mono"
+ "github.com/pion/rtcp"
+ "github.com/pion/rtp"
+ "github.com/pion/webrtc/v4"
+)
+
+func clockSourceOptions(id string) SourceOptions {
+ return SourceOptions{
+ ID: id, StreamID: "clock",
+ Codec: webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{
+ MimeType: webrtc.MimeTypeVP8, ClockRate: 90_000,
+ }, PayloadType: 96},
+ Formats: []LayerFormat{{Width: 8, Height: 8, Bitrate: 100_000}, {Width: 16, Height: 16, Bitrate: 1_000_000}},
+ }
+}
+
+// Project through the same sender statistics as DownTrack.CreateSenderReport.
+// The wire NTP stays at emission time; compare the source-clock position it maps.
+func projectedSourceClock(t *testing.T, source *Source, layer int, stats **rtpstats.RTPStatsSender) time.Duration {
+ t.Helper()
+ reference := source.GetAllBuffers()[layer].GetSenderReportData()
+ if reference == nil {
+ t.Fatal("source clock is absent")
+ }
+ if *stats == nil {
+ *stats = rtpstats.NewRTPStatsSender(rtpstats.RTPStatsParams{}, 1024)
+ (*stats).SetClockRate(90_000)
+ (*stats).Update(mono.UnixNano(), 1, reference.RtpTimestampExt, true, 12, 20, 0, false)
+ }
+ before := mono.Now()
+ report := (*stats).GetRtcpSenderReport(uint32(layer+1), reference, 0, false)
+ after := mono.Now()
+ if report == nil {
+ t.Fatal("projected sender report is absent")
+ }
+ wireTime := mediatransportutil.NtpTime(report.NTPTime).Time()
+ if wireTime.Before(before.Add(-time.Nanosecond)) || wireTime.After(after.Add(time.Nanosecond)) {
+ t.Fatal("wire sender report does not use its emission time")
+ }
+ clock, ok := source.ReferenceTime(layer, report.RTPTime)
+ if !ok {
+ t.Fatal("projected source reference is absent")
+ }
+ raw, ok := source.ReferenceTime(layer, reference.RtpTimestamp)
+ if !ok || raw.Sub(mediatransportutil.NtpTime(reference.NtpTimestamp).Time()).Abs() > time.Microsecond {
+ t.Fatal("source RTP/NTP correlation changed")
+ }
+ return clock.Sub(wireTime)
+}
+
+func TestEncodedSourceClockIgnoresEncoderDelay(t *testing.T) {
+ input, err := NewEncodedSource(clockSourceOptions("local"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer input.Close()
+ check := func(err error) {
+ t.Helper()
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ write := func(layer int, pts time.Duration) {
+ t.Helper()
+ check(input.WriteFrame(layer, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: pts, Duration: time.Second / 30}))
+ }
+ // The first low output took 500 ms; the high output is current. Both carry
+ // their correct capture PTS, and later low output catches up completely.
+ origin := time.Now().Add(-500 * time.Millisecond)
+ check(input.BeginFrame(0, origin))
+ write(0, 0)
+ check(input.BeginFrame(500*time.Millisecond, origin.Add(500*time.Millisecond)))
+ write(1, 500*time.Millisecond)
+ var stats [2]*rtpstats.RTPStatsSender
+ before := projectedSourceClock(t, input.Source, 0, &stats[0]) - projectedSourceClock(t, input.Source, 1, &stats[1])
+ time.Sleep(40 * time.Millisecond)
+ check(input.BeginFrame(533*time.Millisecond, origin.Add(533*time.Millisecond)))
+ write(0, 533*time.Millisecond)
+ write(1, 533*time.Millisecond)
+ after := projectedSourceClock(t, input.Source, 0, &stats[0]) - projectedSourceClock(t, input.Source, 1, &stats[1])
+ if before.Abs() > time.Millisecond || after.Abs() > time.Millisecond {
+ t.Fatalf("encoder delay moved the layer clock: before=%v after=%v", before, after)
+ }
+}
+
+func TestDerivedSourceSharesRemoteClock(t *testing.T) {
+ for _, offset := range []time.Duration{-2 * time.Hour, 0, 2 * time.Hour} {
+ t.Run(offset.String(), func(t *testing.T) {
+ check := func(err error) {
+ t.Helper()
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ options := clockSourceOptions("remote")
+ original, err := NewSource(options)
+ check(err)
+ defer original.Close()
+ check(original.BindLayer(1, 2, webrtc.RTPParameters{Codecs: []webrtc.RTPCodecParameters{options.Codec}}))
+ derived, err := NewEncodedSource(clockSourceOptions("derived"))
+ check(err)
+ defer derived.Close()
+ derived.UseSourceClock(original, 1)
+ remoteOrigin := time.Now().Add(offset - 500*time.Millisecond)
+ var stats [2]*rtpstats.RTPStatsSender
+ // The second upstream report changes its estimated network/clock
+ // offset. Derived output must use that current original correlation.
+ for index, pts := range []time.Duration{0, 540 * time.Millisecond} {
+ if index > 0 {
+ time.Sleep(40 * time.Millisecond)
+ }
+ timestamp := uint32(900_000 + pts*90_000/time.Second)
+ check(original.WriteRTP(1, &rtp.Packet{Header: rtp.Header{
+ Version: 2, SSRC: 2, PayloadType: 96, SequenceNumber: uint16(index + 1), Timestamp: timestamp, Marker: true,
+ }, Payload: append([]byte{0x10}, sfu.VP8KeyFrame8x8...)}))
+ check(original.SenderReport(1, &rtcp.SenderReport{
+ SSRC: 2, RTPTime: timestamp, NTPTime: uint64(mediatransportutil.ToNtpTime(remoteOrigin.Add(pts))),
+ PacketCount: uint32(index + 1), OctetCount: uint32((index + 1) * 20),
+ }))
+ captured, ok := original.ReferenceTime(1, timestamp)
+ if !ok {
+ t.Fatal("original reference is absent")
+ }
+ check(derived.BeginFrame(pts, captured))
+ check(derived.WriteFrame(0, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: pts, Duration: time.Second / 30}))
+ high := projectedSourceClock(t, original, 1, &stats[1])
+ low := projectedSourceClock(t, derived.Source, 0, &stats[0])
+ if mismatch := low - high; mismatch.Abs() > time.Millisecond {
+ t.Fatalf("remote layer clocks disagree at frame %d: %v", index, mismatch)
+ }
+ }
+ })
+ }
+}
diff --git a/internal/media/forwarding/source_registration_test.go b/internal/media/forwarding/source_registration_test.go
new file mode 100644
index 00000000..67c35a54
--- /dev/null
+++ b/internal/media/forwarding/source_registration_test.go
@@ -0,0 +1,146 @@
+package forwarding
+
+import (
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/TNTcraftHIM/Piik/internal/media/encoded"
+ "github.com/livekit/livekit-server/pkg/sfu"
+ "github.com/livekit/livekit-server/pkg/sfu/buffer"
+ "github.com/livekit/protocol/livekit"
+)
+
+type heldClockSnapshot struct {
+ buffer.BufferProvider
+ reads atomic.Int32
+ held chan struct{}
+ live chan struct{}
+ release chan struct{}
+}
+
+func (input *heldClockSnapshot) GetSenderReportData() *livekit.RTCPSenderReportState {
+ report := input.BufferProvider.GetSenderReportData()
+ switch input.reads.Add(1) {
+ case 2: // Initial replay fetched an old snapshot inside the sender wrapper.
+ close(input.held)
+ <-input.release
+ case 3: // A new accepted report is about to enter its live callback.
+ close(input.live)
+ }
+ return report
+}
+
+func TestSourceRegistrationRetainsCurrentClock(t *testing.T) {
+ for _, simulcast := range []bool{false, true} {
+ name := "transport"
+ if simulcast {
+ name = "publication"
+ }
+ t.Run(name, func(t *testing.T) {
+ check := func(err error) {
+ t.Helper()
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ makeInput := func(at time.Time) *EncodedSource {
+ t.Helper()
+ input, err := NewEncodedSource(clockSourceOptions("joining"))
+ check(err)
+ t.Cleanup(input.Close)
+ check(input.BeginFrame(0, at))
+ for layer := 0; layer < 2; layer++ {
+ check(input.WriteFrame(layer, encoded.Frame{Data: sfu.VP8KeyFrame8x8, Duration: time.Second / 30}))
+ }
+ return input
+ }
+ origin := time.Now().Add(-time.Second)
+ input := makeInput(origin)
+ earlier := input.buffers[0].GetSenderReportData()
+ options := TransportOptions{Source: input.Source, ConnectionID: name, InitialBitrate: 1_000_000}
+ var tracks []*sfu.DownTrack
+ var connect func() error
+ var replace func(*Source) error
+ var closeTransport func() error
+ if simulcast {
+ publication, err := NewPublication(options)
+ check(err)
+ tracks, connect, replace, closeTransport = publication.tracks, publication.SetConnected, publication.ReplaceSource, publication.Close
+ } else {
+ transport, err := NewTransport(options)
+ check(err)
+ tracks, connect, replace, closeTransport = []*sfu.DownTrack{transport.Output.DownTrack}, transport.SetConnected, transport.ReplaceSource, transport.Close
+ }
+ defer closeTransport()
+ // Public migration state exposes the Forwarder's accepted SRs without
+ // binding a socket or making this a network/decoded-video test.
+ for _, track := range tracks {
+ track.SeedState(sfu.DownTrackState{ForwarderState: &livekit.RTPForwarderState{
+ Started: true, RtpMunger: &livekit.RTPMungerState{},
+ }})
+ }
+ held := &heldClockSnapshot{BufferProvider: input.GetAllBuffers()[0],
+ held: make(chan struct{}), live: make(chan struct{}), release: make(chan struct{})}
+ input.ReceiverBase.AddBuffer(held, 0)
+ var release sync.Once
+ unblock := func() { release.Do(func() { close(held.release) }) }
+ defer unblock()
+ connected := make(chan error, 1)
+ go func() { connected <- connect() }()
+ wait := func(signal <-chan struct{}) {
+ t.Helper()
+ select {
+ case <-signal:
+ case <-time.After(2 * time.Second):
+ t.Fatal("source registration did not reach the clock boundary")
+ }
+ }
+ wait(held.held)
+ updated := make(chan error, 1)
+ go func() {
+ if err := input.BeginFrame(33*time.Millisecond, origin.Add(33*time.Millisecond)); err != nil {
+ updated <- err
+ return
+ }
+ updated <- input.WriteFrame(0, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: 33 * time.Millisecond, Duration: time.Second / 30})
+ }()
+ wait(held.live)
+ unblock()
+ for _, result := range []<-chan error{connected, updated} {
+ select {
+ case err := <-result:
+ check(err)
+ case <-time.After(2 * time.Second):
+ t.Fatal("source registration and live report deadlocked")
+ }
+ }
+ assertClock := func(expected uint64) {
+ t.Helper()
+ for _, track := range tracks {
+ report := track.GetState().ForwarderState.SenderReportState[0]
+ if report == nil || report.NtpTimestamp != expected {
+ t.Fatal("subscriber retained an older sender-report clock")
+ }
+ }
+ }
+ latest := input.buffers[0].GetSenderReportData()
+ assertClock(latest.NtpTimestamp)
+ previous := input.GetDownTracks()
+ // Even a queued callback entering after registration uses accepted state.
+ for _, sender := range previous {
+ check(sender.HandleRTCPSenderReportData(input.Codec().PayloadType, 0, earlier))
+ }
+ assertClock(latest.NtpTimestamp)
+ next := makeInput(time.Now())
+ check(replace(next.Source))
+ latest = next.buffers[0].GetSenderReportData()
+ assertClock(latest.NtpTimestamp)
+ for _, sender := range previous {
+ check(sender.HandleRTCPSenderReportData(input.Codec().PayloadType, 0, input.buffers[0].GetSenderReportData()))
+ }
+ assertClock(latest.NtpTimestamp)
+ })
+ }
+}
diff --git a/internal/media/inputroute/relay.go b/internal/media/inputroute/relay.go
index 7637250a..6c3fb0d0 100644
--- a/internal/media/inputroute/relay.go
+++ b/internal/media/inputroute/relay.go
@@ -110,6 +110,12 @@ func (r *Relay) SetUpAvailable(available bool) {
func (r *Relay) AttachDown(edgeID string, channel *webrtc.DataChannel) {
r.mu.Lock()
+ // SCTP can close before its OnClose callback retires the old binding.
+ if old := r.down[edgeID]; !r.closed && old != nil && old.channel.ReadyState() == webrtc.DataChannelStateClosed {
+ r.mu.Unlock()
+ r.LoseDown(edgeID, old.channel)
+ r.mu.Lock()
+ }
if r.closed || r.down[edgeID] != nil {
r.mu.Unlock()
_ = channel.Close()
diff --git a/internal/media/inputroute/relay_test.go b/internal/media/inputroute/relay_test.go
new file mode 100644
index 00000000..f4ec6895
--- /dev/null
+++ b/internal/media/inputroute/relay_test.go
@@ -0,0 +1,151 @@
+package inputroute
+
+import (
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/pion/webrtc/v4"
+)
+
+func TestClosedDownstreamBindingAllowsReplacement(t *testing.T) {
+ received := make(chan Packet, 10)
+ relay := New(func(_ string, p Packet) { received <- p })
+ defer relay.Close()
+ oldSend, oldChannel := inputChannelPair(t)
+ relay.AttachDown("same-edge", oldChannel)
+ oldPacket := Packet{ID: strings.Repeat("a", 32), Epoch: strings.Repeat("b", 32), Data: "old"}
+ send(oldSend, oldPacket)
+ if p := receiveInputPacket(t, received); p != oldPacket {
+ t.Fatalf("old packet = %+v", p)
+ }
+ released, closed := make(chan struct{}), make(chan struct{})
+ var once sync.Once
+ release := func() { once.Do(func() { close(released) }) }
+ defer release()
+ // Hold the actual SCTP callback after its channel has entered Closed.
+ oldChannel.OnClose(func() {
+ <-released
+ relay.LoseDown("same-edge", oldChannel)
+ close(closed)
+ })
+ if err := oldSend.Close(); err != nil {
+ t.Fatal(err)
+ }
+ waitInputChannel(t, "old SCTP closes", func() bool { return oldChannel.ReadyState() == webrtc.DataChannelStateClosed })
+ newSend, newChannel := inputChannelPair(t)
+ relay.AttachDown("same-edge", newChannel)
+ if p := receiveInputPacket(t, received); p != (Packet{ID: oldPacket.ID, Epoch: oldPacket.Epoch, Closed: true}) {
+ t.Fatalf("old endpoint was not retired: %+v", p)
+ }
+ packet := Packet{ID: strings.Repeat("c", 32), Epoch: strings.Repeat("d", 32), Data: "new"}
+ checkReplacement := func() {
+ t.Helper()
+ send(newSend, packet)
+ if p := receiveInputPacket(t, received); p != packet {
+ t.Fatalf("replacement packet = %+v", p)
+ }
+ }
+ checkReplacement()
+ release()
+ select {
+ case <-closed:
+ case <-time.After(2 * time.Second):
+ t.Fatal("old OnClose did not finish")
+ }
+ checkReplacement() // Late cleanup must not detach the replacement.
+ _, duplicate := inputChannelPair(t)
+ relay.AttachDown("same-edge", duplicate)
+ waitInputChannel(t, "live duplicate rejected", func() bool { return duplicate.ReadyState() == webrtc.DataChannelStateClosed })
+ checkReplacement()
+}
+
+func receiveInputPacket(t *testing.T, packets <-chan Packet) Packet {
+ t.Helper()
+ select {
+ case p := <-packets:
+ return p
+ case <-time.After(2 * time.Second):
+ t.Fatal("no relayed input packet")
+ return Packet{}
+ }
+}
+
+func waitInputChannel(t *testing.T, label string, ready func() bool) {
+ t.Helper()
+ deadline := time.Now().Add(5 * time.Second)
+ for time.Now().Before(deadline) {
+ if ready() {
+ return
+ }
+ time.Sleep(5 * time.Millisecond)
+ }
+ t.Fatal(label)
+}
+
+func inputChannelPair(t *testing.T) (*webrtc.DataChannel, *webrtc.DataChannel) {
+ t.Helper()
+ settings := webrtc.SettingEngine{}
+ settings.SetIncludeLoopbackCandidate(true)
+ settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
+ api := webrtc.NewAPI(webrtc.WithSettingEngine(settings))
+ a, err := api.NewPeerConnection(webrtc.Configuration{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = a.Close() })
+ b, err := api.NewPeerConnection(webrtc.Configuration{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = b.Close() })
+ remote := make(chan *webrtc.DataChannel, 1)
+ b.OnDataChannel(func(c *webrtc.DataChannel) { remote <- c })
+ local, err := a.CreateDataChannel(Label, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ offer, err := a.CreateOffer(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ gather := webrtc.GatheringCompletePromise(a)
+ if err = a.SetLocalDescription(offer); err != nil {
+ t.Fatal(err)
+ }
+ select {
+ case <-gather:
+ case <-time.After(5 * time.Second):
+ t.Fatal("offer gather")
+ }
+ if err = b.SetRemoteDescription(*a.LocalDescription()); err != nil {
+ t.Fatal(err)
+ }
+ answer, err := b.CreateAnswer(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ gather = webrtc.GatheringCompletePromise(b)
+ if err = b.SetLocalDescription(answer); err != nil {
+ t.Fatal(err)
+ }
+ select {
+ case <-gather:
+ case <-time.After(5 * time.Second):
+ t.Fatal("answer gather")
+ }
+ if err = a.SetRemoteDescription(*b.LocalDescription()); err != nil {
+ t.Fatal(err)
+ }
+ var other *webrtc.DataChannel
+ select {
+ case other = <-remote:
+ case <-time.After(5 * time.Second):
+ t.Fatal("remote data channel")
+ }
+ waitInputChannel(t, "SCTP opens", func() bool {
+ return local.ReadyState() == webrtc.DataChannelStateOpen && other.ReadyState() == webrtc.DataChannelStateOpen
+ })
+ return local, other
+}
diff --git a/internal/server/app/app.go b/internal/server/app/app.go
index 6c29dbb6..bb38ad5c 100644
--- a/internal/server/app/app.go
+++ b/internal/server/app/app.go
@@ -103,6 +103,8 @@ type Server struct {
// startupDone is closed when listen settles, which is what shutdown awaits
// so the listener stays owned until an in-flight startup finishes.
startupDone chan struct{}
+ serveErr error
+ done chan error
shutdownOnce sync.Once
shutdownErr error
@@ -142,6 +144,7 @@ func New(options Options) (*Server, error) {
siteAccess: siteAccess,
logger: logger,
startupDone: make(chan struct{}),
+ done: make(chan error, 1),
}
server.listeners = append([]net.Listener(nil), options.Listeners...)
if options.Assets != nil {
@@ -204,6 +207,10 @@ func newRoomStore(options Options) (*room.Store, error) {
})
}
+// Done reports the first unexpected HTTP listener failure. Normal Close/End
+// leave it silent; owners also observe their own cancellation/lifetime signal.
+func (s *Server) Done() <-chan error { return s.done }
+
// Listen binds application sockets before recovering room authority and accepting traffic.
func (s *Server) Listen(ctx context.Context) (int, error) {
s.mu.Lock()
@@ -225,6 +232,15 @@ func (s *Server) Listen(ctx context.Context) (int, error) {
}
func (s *Server) start(ctx context.Context) (int, error) {
+ var publicIP string
+ if s.config.SFU != nil {
+ var err error
+ publicIP, err = resolveSFUPublicIP(ctx, s.config.SFU.PublicIP)
+ if err != nil {
+ _ = s.stopServing(ctx)
+ return 0, err
+ }
+ }
// Bind first. A listener owned by another process must fail before
// the room database is opened.
var err error
@@ -260,8 +276,8 @@ func (s *Server) start(ctx context.Context) (int, error) {
settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
settings.SetICEUDPMux(s.mediaMux)
settings.SetIncludeLoopbackCandidate(net.ParseIP(configuration.ListenHost).IsLoopback())
- if configuration.PublicIP != "" {
- settings.SetNAT1To1IPs([]string{configuration.PublicIP}, webrtc.ICECandidateTypeHost)
+ if publicIP != "" {
+ settings.SetNAT1To1IPs([]string{publicIP}, webrtc.ICECandidateTypeHost)
}
s.media = sfu.NewMedia(sfu.MediaOptions{Settings: settings, Events: func(event sfu.MediaEvent) {
if signaling := s.signaling.Load(); signaling != nil {
@@ -277,6 +293,13 @@ func (s *Server) start(ctx context.Context) (int, error) {
// Report permanent accept failures so an unexpectedly stopped HTTP
// listener is visible to the operator.
if err := s.httpServer.Serve(listener); !errors.Is(err, http.ErrServerClosed) {
+ s.mu.Lock()
+ if !s.closing && s.serveErr == nil {
+ s.serveErr = fmt.Errorf("Piik HTTP listener stopped unexpectedly: %w", err)
+ s.acceptingTraffic.Store(false)
+ s.done <- s.serveErr
+ }
+ s.mu.Unlock()
s.logger.Error("Piik HTTP server stopped unexpectedly", "errorType", fmt.Sprintf("%T", err))
s.logger.Debug("http-listener-failed", diagnostics.Error(err))
}
@@ -340,6 +363,9 @@ func (s *Server) reconcile() error {
if s.closing {
return nil
}
+ if s.serveErr != nil {
+ return s.serveErr
+ }
signaling, err := signal.New(s.signalOptions)
if err != nil {
return err
diff --git a/internal/server/app/listener_test.go b/internal/server/app/listener_test.go
new file mode 100644
index 00000000..f30450b7
--- /dev/null
+++ b/internal/server/app/listener_test.go
@@ -0,0 +1,100 @@
+package app
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "net"
+ "net/http"
+ "testing"
+ "time"
+)
+
+func TestOwnedHTTPListenerFailureReachesOwner(t *testing.T) {
+ listeners := make([]net.Listener, 2)
+ for index := range listeners {
+ listener, err := net.Listen("tcp4", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ listeners[index] = listener
+ defer listener.Close()
+ }
+ server, err := New(Options{Config: testConfig(t), Listeners: listeners})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer server.End(context.Background())
+ if _, err = server.Listen(t.Context()); err != nil {
+ t.Fatal(err)
+ }
+ client := &http.Client{Transport: &http.Transport{DisableKeepAlives: true}, Timeout: time.Second}
+ health := func(listener net.Listener) int {
+ t.Helper()
+ response, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/healthz", listener.Addr().(*net.TCPAddr).Port))
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ return response.StatusCode
+ }
+ if health(listeners[0]) != http.StatusOK || health(listeners[1]) != http.StatusOK {
+ t.Fatal("listeners did not become ready")
+ }
+ listeners[0].Close()
+ select {
+ case err = <-server.Done():
+ if !errors.Is(err, net.ErrClosed) {
+ t.Fatalf("lost listener failure: %v", err)
+ }
+ case <-time.After(time.Second):
+ t.Fatal("dead HTTP listener was not reported to its owner")
+ }
+ if server.acceptingTraffic.Load() || health(listeners[1]) != http.StatusServiceUnavailable {
+ t.Fatal("failed service remained ready on its surviving listener")
+ }
+ if err = server.reconcile(); err == nil || server.acceptingTraffic.Load() {
+ t.Fatal("startup reconciliation revived a failed listener")
+ }
+ listeners[1].Close()
+ if err = server.End(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ select {
+ case err := <-server.Done():
+ t.Fatalf("reported another fatal event: %v", err)
+ default:
+ }
+}
+
+func TestOwnerShutdownDoesNotReportListenerFailure(t *testing.T) {
+ for _, end := range []bool{false, true} {
+ t.Run(fmt.Sprint("end=", end), func(t *testing.T) {
+ listener, err := net.Listen("tcp4", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer listener.Close()
+ server, err := New(Options{Config: testConfig(t), Listeners: []net.Listener{listener}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err = server.Listen(t.Context()); err != nil {
+ t.Fatal(err)
+ }
+ if end {
+ err = server.End(context.Background())
+ } else {
+ err = server.Close(context.Background())
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ select {
+ case err := <-server.Done():
+ t.Fatalf("normal shutdown reported failure: %v", err)
+ default:
+ }
+ })
+ }
+}
diff --git a/internal/server/app/sfu_address.go b/internal/server/app/sfu_address.go
new file mode 100644
index 00000000..d39efa66
--- /dev/null
+++ b/internal/server/app/sfu_address.go
@@ -0,0 +1,30 @@
+package app
+
+import (
+ "context"
+ "fmt"
+ "net"
+ "net/netip"
+ "time"
+)
+
+// Pion advertises literal candidates; a configured DDNS name is resolved once
+// for this server lifetime, just like an explicitly configured public IPv4.
+func resolveSFUPublicIP(ctx context.Context, host string) (string, error) {
+ if host == "" {
+ return "", nil
+ }
+ if address, err := netip.ParseAddr(host); err == nil {
+ return address.String(), nil
+ }
+ ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
+ defer cancel()
+ addresses, err := net.DefaultResolver.LookupNetIP(ctx, "ip4", host)
+ if err != nil {
+ return "", fmt.Errorf("SFU_PUBLIC_IP could not resolve to IPv4: %w", err)
+ }
+ if len(addresses) == 0 {
+ return "", fmt.Errorf("SFU_PUBLIC_IP has no IPv4 address: %s", host)
+ }
+ return addresses[0].Unmap().String(), nil
+}
diff --git a/internal/server/app/sfu_address_test.go b/internal/server/app/sfu_address_test.go
new file mode 100644
index 00000000..f0d91b2c
--- /dev/null
+++ b/internal/server/app/sfu_address_test.go
@@ -0,0 +1,65 @@
+package app
+
+import (
+ "context"
+ "net"
+ "net/netip"
+ "strings"
+ "testing"
+
+ "github.com/TNTcraftHIM/Piik/internal/media/forwarding"
+ "github.com/TNTcraftHIM/Piik/internal/server/config"
+ "github.com/TNTcraftHIM/Piik/internal/server/sfu"
+)
+
+func TestSFUPublicHostnameStartsTheExistingMediaRuntime(t *testing.T) {
+ configuration := testConfig(t)
+ configuration.SFU = &config.SFUConfig{ListenHost: "127.0.0.1", Port: 0, PublicIP: "localhost"}
+ server := start(t, Options{Config: configuration})
+ fence := sfu.ResourceFence{RoomID: "1234", ShareGeneration: "share_12345678", PublicationGeneration: "publication_12345678"}
+ metadata := sfu.PublicationMedia{Codec: "vp8", RIDs: []string{""}, Formats: []forwarding.LayerFormat{{Width: 640, Height: 360, Bitrate: 1_000_000}}}
+ if err := server.media.PreparePublication(fence, "publisher_12345678", metadata); err != nil {
+ t.Fatalf("hostname-backed media initialization: %v", err)
+ }
+}
+
+func TestSFUPublicAddressResolvesOnlyAtRuntime(t *testing.T) {
+ for _, input := range []string{"", "198.51.100.5", "localhost"} {
+ value, err := resolveSFUPublicIP(t.Context(), input)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if input != "localhost" {
+ if value != input {
+ t.Fatalf("literal %q changed to %q", input, value)
+ }
+ continue
+ }
+ address, err := netip.ParseAddr(value)
+ if err != nil || !address.Is4() || !address.IsLoopback() {
+ t.Fatalf("localhost must resolve to a literal IPv4 candidate: %q, %v", value, err)
+ }
+ }
+ ctx, cancel := context.WithCancel(t.Context())
+ cancel()
+ if _, err := resolveSFUPublicIP(ctx, "unresolved.invalid"); err == nil || !strings.Contains(err.Error(), "SFU_PUBLIC_IP could not resolve") {
+ t.Fatalf("DNS failure must identify the configured SFU address: %v", err)
+ }
+}
+
+func TestSFUPublicDNSFailureClosesOwnedListeners(t *testing.T) {
+ listener, err := net.Listen("tcp4", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer listener.Close()
+ configuration := testConfig(t)
+ configuration.SFU = &config.SFUConfig{ListenHost: "127.0.0.1", Port: 0, PublicIP: "unresolved.invalid"}
+ server := newServer(t, Options{Config: configuration, Listeners: []net.Listener{listener}})
+ ctx, cancel := context.WithCancel(t.Context())
+ cancel()
+ if _, err = server.Listen(ctx); err == nil || !strings.Contains(err.Error(), "SFU_PUBLIC_IP could not resolve") {
+ t.Fatalf("startup must report the DNS failure: %v", err)
+ }
+ assertPortFree(t, listener.Addr().(*net.TCPAddr).Port)
+}
diff --git a/internal/server/app/static.go b/internal/server/app/static.go
index 24aaa83b..f881dcf4 100644
--- a/internal/server/app/static.go
+++ b/internal/server/app/static.go
@@ -7,6 +7,7 @@ import (
"path"
"strings"
+ "github.com/TNTcraftHIM/Piik/internal/httpassets"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
)
@@ -45,7 +46,7 @@ var staticContentTypes = map[string]string{
// staticHandler serves embedded assets and falls back to index.html for SPA
// routes. Missing files use the application's JSON 404 handler.
func staticHandler(assets fs.FS, notFound http.Handler) http.Handler {
- return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
+ return httpassets.Compress(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// API and health routes have already been handled by ServeHTTP.
cleaned := path.Clean("/" + request.URL.Path)
name := strings.TrimPrefix(cleaned, "/")
@@ -89,7 +90,7 @@ func staticHandler(assets fs.FS, notFound http.Handler) http.Handler {
}
}
notFound.ServeHTTP(writer, request)
- })
+ }))
}
// hasDotSegment excludes hidden assets, with the published .well-known exemption.
diff --git a/internal/server/app/static_test.go b/internal/server/app/static_test.go
index f769732a..4a3fec0b 100644
--- a/internal/server/app/static_test.go
+++ b/internal/server/app/static_test.go
@@ -2,16 +2,97 @@ package app
import (
"bytes"
+ "compress/gzip"
"io"
"io/fs"
"net/http"
"net/http/httptest"
+ "strconv"
+ "strings"
"testing"
"testing/fstest"
)
const staticNotFoundBody = `{"error":"Not found"}`
+func TestStaticCompressionKeepsNegotiationAndServeContentSemantics(t *testing.T) {
+ body := strings.Repeat("const application = 'owned fixture';\n", 100)
+ assets := fstest.MapFS{}
+ for _, name := range []string{"index.html", "app.js", "app.mjs", "app.css", "data.json", "audio.opus"} {
+ assets[name] = &fstest.MapFile{Data: []byte(body)}
+ }
+ handler := staticHandler(assets, staticNotFound)
+ for _, test := range []struct {
+ path, method, encoding, byteRange string
+ status int
+ compressed bool
+ length int
+ }{
+ {"/", "GET", "gzip", "", 200, true, len(body)},
+ {"/app.js", "GET", "br, gzip", "", 200, true, len(body)},
+ {"/app.mjs", "GET", "zstd, gzip", "", 200, true, len(body)},
+ {"/app.css", "GET", "gzip", "", 200, true, len(body)},
+ {"/app.js", "GET", "gzip;q=0", "", 200, false, len(body)},
+ {"/app.js", "GET", "", "", 200, false, len(body)},
+ {"/app.js", "HEAD", "gzip", "", 200, false, 0},
+ {"/app.js", "GET", "gzip", "bytes=0-2047", 206, false, 2048},
+ {"/app.js", "GET", "gzip", "bytes=99999-", 416, false, -1},
+ {"/data.json", "GET", "gzip", "", 200, false, len(body)},
+ {"/audio.opus", "GET", "gzip", "", 200, false, len(body)},
+ } {
+ t.Run(test.path+test.method+test.encoding+test.byteRange, func(t *testing.T) {
+ request := httptest.NewRequest(test.method, test.path, nil)
+ request.Header.Set("Accept-Encoding", test.encoding)
+ if test.byteRange != "" {
+ request.Header.Set("Range", test.byteRange)
+ }
+ response := httptest.NewRecorder()
+ handler.ServeHTTP(response, request)
+ if response.Code != test.status {
+ t.Fatalf("status=%d", response.Code)
+ }
+ if (response.Header().Get("Content-Encoding") == "gzip") != test.compressed {
+ t.Fatalf("encoding=%q", response.Header().Get("Content-Encoding"))
+ }
+ if response.Header().Get("Vary") != "Accept-Encoding" {
+ t.Fatal("missing encoding cache variance")
+ }
+ data := response.Body.Bytes()
+ if test.compressed {
+ reader, err := gzip.NewReader(bytes.NewReader(data))
+ if err != nil {
+ t.Fatal(err)
+ }
+ data, err = io.ReadAll(reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ reader.Close()
+ if string(data) != body || response.Body.Len() >= len(body) {
+ t.Fatal("compressed asset changed or grew")
+ }
+ if response.Header().Get("Content-Length") != "" {
+ t.Fatal("raw length retained on compressed asset")
+ }
+ }
+ if test.length >= 0 && len(data) != test.length {
+ t.Fatalf("body length=%d", len(data))
+ }
+ if test.method == "HEAD" && response.Header().Get("Content-Length") != strconv.Itoa(len(body)) {
+ t.Fatal("HEAD lost the unencoded asset length")
+ }
+ if test.status == 206 && response.Header().Get("Content-Range") != "bytes 0-2047/"+strconv.Itoa(len(body)) {
+ t.Fatalf("range=%q", response.Header().Get("Content-Range"))
+ }
+ for _, header := range []string{"Cache-Control", "Last-Modified", "ETag"} {
+ if response.Header().Get(header) != "" {
+ t.Fatalf("fabricated %s", header)
+ }
+ }
+ })
+ }
+}
+
// staticNotFound is app.ts's `sendJson(response, 404, { error: "Not found" })`,
// the next() callback sirv fell through to.
var staticNotFound = http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
diff --git a/internal/server/config/config.go b/internal/server/config/config.go
index 624b306f..d0d03dcc 100644
--- a/internal/server/config/config.go
+++ b/internal/server/config/config.go
@@ -315,8 +315,21 @@ func parseSFU(env map[string]string) (*SFUConfig, error) {
}
publicIP := strings.TrimSpace(env["SFU_PUBLIC_IP"])
if publicIP != "" {
- if address, err := netip.ParseAddr(publicIP); err != nil || !address.Is4() {
- return nil, errors.New("SFU_PUBLIC_IP must be an IPv4 address")
+ if address, err := netip.ParseAddr(publicIP); err == nil {
+ if !address.Is4() {
+ return nil, errors.New("SFU_PUBLIC_IP must be an IPv4 address or DNS hostname")
+ }
+ publicIP = address.String()
+ } else {
+ host, err := url.Parse("http://" + publicIP)
+ if err == nil && host.Host == publicIP && !strings.Contains(publicIP, ":") {
+ publicIP = normalizedHost(host)
+ } else {
+ publicIP = ""
+ }
+ if publicIP == "" {
+ return nil, errors.New("SFU_PUBLIC_IP must be an IPv4 address or DNS hostname")
+ }
}
}
return &SFUConfig{
diff --git a/internal/server/config/config_test.go b/internal/server/config/config_test.go
index 9f8e9a40..4c81869f 100644
--- a/internal/server/config/config_test.go
+++ b/internal/server/config/config_test.go
@@ -165,6 +165,19 @@ func TestLoadSFU(t *testing.T) {
}
}
+func TestLoadSFUPublicHostnameWithoutResolving(t *testing.T) {
+ for input, want := range map[string]string{
+ " Media.Example.COM ": "media.example.com",
+ "home.example.com.": "home.example.com.",
+ "例子.example": "xn--fsqu00a.example",
+ } {
+ configuration := mustLoad(t, map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": input})
+ if configuration.SFU.PublicIP != want {
+ t.Fatalf("advertised host %q = %q, want %q", input, configuration.SFU.PublicIP, want)
+ }
+ }
+}
+
func TestLoadSFUOptional(t *testing.T) {
for _, values := range []map[string]string{
nil,
@@ -369,12 +382,18 @@ func TestLoadRejects(t *testing.T) {
"SFU_LISTEN_HOST must be an IPv4 address"},
{"SFU bind mapped IPv6", map[string]string{"SFU_UDP_PORT": "7882", "SFU_LISTEN_HOST": "::ffff:192.0.2.5"},
"SFU_LISTEN_HOST must be an IPv4 address"},
- {"SFU public hostname", map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": "sfu.test"},
- "SFU_PUBLIC_IP must be an IPv4 address"},
+ {"SFU public URL", map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": "https://sfu.test"},
+ "SFU_PUBLIC_IP must be an IPv4 address or DNS hostname"},
+ {"SFU public port", map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": "sfu.test:7882"},
+ "SFU_PUBLIC_IP must be an IPv4 address or DNS hostname"},
+ {"SFU public userinfo", map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": "[email protected]"},
+ "SFU_PUBLIC_IP must be an IPv4 address or DNS hostname"},
+ {"SFU public path", map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": "sfu.test/path"},
+ "SFU_PUBLIC_IP must be an IPv4 address or DNS hostname"},
{"SFU public IPv6", map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": "2001:db8::5"},
- "SFU_PUBLIC_IP must be an IPv4 address"},
+ "SFU_PUBLIC_IP must be an IPv4 address or DNS hostname"},
{"SFU public mapped IPv6", map[string]string{"SFU_UDP_PORT": "7882", "SFU_PUBLIC_IP": "::ffff:198.51.100.5"},
- "SFU_PUBLIC_IP must be an IPv4 address"},
+ "SFU_PUBLIC_IP must be an IPv4 address or DNS hostname"},
// Bounded integers.
{"viewer limit above ceiling",
diff --git a/internal/server/sfu/voice_connection.go b/internal/server/sfu/voice_connection.go
index acbf78c5..7a15e66a 100644
--- a/internal/server/sfu/voice_connection.go
+++ b/internal/server/sfu/voice_connection.go
@@ -351,7 +351,7 @@ func (c *voiceConnection) signal(signal VoiceSignal) error {
if signal.Candidate != nil {
if c.pc.RemoteDescription() == nil {
if len(c.candidates) >= 64 {
- return errors.New("too many pending voice candidates")
+ return nil
}
c.candidates = append(c.candidates, *signal.Candidate)
return nil
diff --git a/internal/server/sfu/voice_test.go b/internal/server/sfu/voice_test.go
index 2fe6dc4b..2efb3235 100644
--- a/internal/server/sfu/voice_test.go
+++ b/internal/server/sfu/voice_test.go
@@ -445,9 +445,25 @@ func TestVoiceQueuedCandidateFailureDoesNotPoisonNegotiation(t *testing.T) {
VoiceSignal{Candidate: &webrtc.ICECandidateInit{Candidate: "candidate:malformed"}}); err != nil {
t.Fatal(err)
}
+ // Additional trickle input must not turn a full bounded candidate queue
+ // into a transport error while the first 64 candidates remain usable.
+ for index := range 64 {
+ candidate := webrtc.ICECandidateInit{Candidate: fmt.Sprintf("candidate:queued%d 1 udp 100 127.0.0.1 %d typ host", index, 10000+index)}
+ if err := service.SignalVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID,
+ VoiceSignal{Candidate: &candidate}); err != nil {
+ t.Fatalf("candidate %d falsely failed the voice transport: %v", index, err)
+ }
+ }
+ connection := service.voice.connections[plan.key()]
+ if len(connection.candidates) != 64 || connection.candidates[63].Candidate != "candidate:queued62 1 udp 100 127.0.0.1 10062 typ host" {
+ t.Fatal("candidate overflow changed the retained queue or its bound")
+ }
if err := peer.answer(first, false); err != nil {
t.Fatalf("valid answer was rejected by a queued candidate: %v", err)
}
+ if len(connection.candidates) != 0 || connection.pending != nil {
+ t.Fatal("valid answer did not drain retained candidates and finish negotiation")
+ }
for _, candidate := range []string{"candidate:malformed", "candidate:control 1 udp 2122260223 127.0.0.1 9999 typ host"} {
if err := service.SignalVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID,
VoiceSignal{Candidate: &webrtc.ICECandidateInit{Candidate: candidate}}); err != nil {
diff --git a/internal/server/signal/router_sfu_signal.go b/internal/server/signal/router_sfu_signal.go
index 189f208c..993e5522 100644
--- a/internal/server/signal/router_sfu_signal.go
+++ b/internal/server/signal/router_sfu_signal.go
@@ -308,6 +308,8 @@ func (r *router) handleSfuMediaEvent(event sfu.MediaEvent) {
if event.State != webrtc.PeerConnectionStateUnknown {
r.debug(participant.roomID, "sfu-transport-state",
"participant", r.debugPeer(participant.roomID, participant.peerID),
+ "connectionId", diagnostics.ID(event.ConnectionID),
+ "publicationGeneration", diagnostics.ID(event.Fence.PublicationGeneration),
"role", role, "phase", phase, "revision", revision, "state", event.State.String())
if event.State == webrtc.PeerConnectionStateFailed {
r.failSfuConnection(participant, message)
diff --git a/internal/server/signal/router_sfu_signal_test.go b/internal/server/signal/router_sfu_signal_test.go
index 7d3498db..0505f77a 100644
--- a/internal/server/signal/router_sfu_signal_test.go
+++ b/internal/server/signal/router_sfu_signal_test.go
@@ -9,6 +9,7 @@ import (
"testing"
"time"
+ diagnostic "github.com/TNTcraftHIM/Piik/internal/diagnostics"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
"github.com/TNTcraftHIM/Piik/internal/server/sfu"
livekitsfu "github.com/livekit/livekit-server/pkg/sfu"
@@ -264,8 +265,12 @@ func TestEmbeddedSfuAuthenticatedWebSocketCarriesActualRTP(t *testing.T) {
if !waitFor(t, time.Second, func() bool {
return diagnostics.calledWith(h.room.RoomID, "sfu-transport-state", map[string]any{
"role": "host", "phase": "prepare", "revision": int64(hostConfig.Revision), "state": "connected",
+ "connectionId": diagnostic.ID(hostConfig.ConnectionID),
+ "publicationGeneration": diagnostic.ID(hostConfig.PublicationGeneration),
}) && diagnostics.calledWith(h.room.RoomID, "sfu-transport-state", map[string]any{
"role": "viewer", "phase": "prepare", "revision": int64(viewerConfig.Revision), "state": "connected",
+ "connectionId": diagnostic.ID(viewerConfig.ConnectionID),
+ "publicationGeneration": diagnostic.ID(viewerConfig.PublicationGeneration),
})
}) {
t.Fatal("SFU diagnostics did not distinguish connected ingress and egress before route commit")
diff --git a/licenses/libcxx-ndk-30.txt b/licenses/libcxx-ndk-30.txt
new file mode 100644
index 00000000..e159d283
--- /dev/null
+++ b/licenses/libcxx-ndk-30.txt
@@ -0,0 +1,311 @@
+==============================================================================
+The LLVM Project is under the Apache License v2.0 with LLVM Exceptions:
+==============================================================================
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+
+---- LLVM Exceptions to the Apache 2.0 License ----
+
+As an exception, if, as a result of your compiling your source code, portions
+of this Software are embedded into an Object form of such source code, you
+may redistribute such embedded portions in such Object form without complying
+with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
+
+In addition, if you combine or link compiled forms of this Software with
+software that is licensed under the GPLv2 ("Combined Software") and if a
+court of competent jurisdiction determines that the patent provision (Section
+3), the indemnity provision (Section 9) or other Section of the License
+conflicts with the conditions of the GPLv2, you may retroactively and
+prospectively choose to deem waived or otherwise exclude such Section(s) of
+the License, but only in their entirety and only with respect to the Combined
+Software.
+
+==============================================================================
+Software from third parties included in the LLVM Project:
+==============================================================================
+The LLVM Project contains third party software which is under different license
+terms. All such code will be identified clearly using at least one of two
+mechanisms:
+1) It will be in a separate directory tree with its own `LICENSE.txt` or
+ `LICENSE` file at the top containing the specific license and restrictions
+ which apply to that software, or
+2) It will contain specific license and restriction terms at the top of every
+ file.
+
+==============================================================================
+Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy):
+==============================================================================
+
+The libc++ library is dual licensed under both the University of Illinois
+"BSD-Like" license and the MIT license. As a user of this code you may choose
+to use it under either license. As a contributor, you agree to allow your code
+to be used under both.
+
+Full text of the relevant licenses is included below.
+
+==============================================================================
+
+University of Illinois/NCSA
+Open Source License
+
+Copyright (c) 2009-2019 by the contributors listed in CREDITS.TXT
+
+All rights reserved.
+
+Developed by:
+
+ LLVM Team
+
+ University of Illinois at Urbana-Champaign
+
+ http://llvm.org
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimers.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimers in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the names of the LLVM Team, University of Illinois at
+ Urbana-Champaign, nor the names of its contributors may be used to
+ endorse or promote products derived from this Software without specific
+ prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
+SOFTWARE.
+
+==============================================================================
+
+Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/licenses/upstream.json b/licenses/upstream.json
index 95862ba3..b62ae9dd 100644
--- a/licenses/upstream.json
+++ b/licenses/upstream.json
@@ -1,4 +1,9 @@
{
+ "file": "libcxx-ndk-30.txt",
+ "source": "https://github.com/llvm/llvm-project/blob/2d287f51eff2a5fbf84458a33f7fb2493cf67965/libcxx/LICENSE.TXT",
+ "sha256": "539dd7aed86e8a4f12cbdd0e6c50c189c7d74847e4fecc64ce2c6ee3a01da38b"
+ },
"file": "webrtc-m152.7977.0.2.txt",
"source": "https://github.com/shiguredo-webrtc-build/webrtc-build/releases/tag/m152.7977.0.2",
diff --git a/mobile/.gitignore b/mobile/.gitignore
new file mode 100644
index 00000000..f6f9f1ff
--- /dev/null
+++ b/mobile/.gitignore
@@ -0,0 +1,10 @@
+android/.gradle/
+android/build/
+android/local.properties
+android/app/build/
+android/app/libs/*.aar
+android/app/libs/*-sources.jar
+android/app/src/main/jniLibs/
+android/app/src/main/assets/THIRD-PARTY-NOTICES.txt
+android/app/src/main/res/drawable-nodpi/piik.png
+*.keystore
diff --git a/mobile/README.md b/mobile/README.md
new file mode 100644
index 00000000..ae9a2e4a
--- /dev/null
+++ b/mobile/README.md
@@ -0,0 +1,167 @@
+# Android App
+
+This directory contains the Android App shell and capture bridge.
+It targets Android 10 or later on ARM64 and x64 devices, using the Android ABI
+identifiers `arm64-v8a` and `x86_64` in one APK.
+Piik opens its existing Web UI in the system browser. An Android service owns
+the shared Go runtime until the user stops App; the Activity handles visible
+system consent. A `mediaProjection` foreground service
+owns the approved projection token, display surface, platform encoding and eligible
+playback audio and optional commentary input; Go owns the existing room, native
+session, mixer and media routes.
+Screen sharing captures the whole display. Source audio is subject
+to Android's capture policy and excludes this app's playback.
+
+Phones and tablets use the existing responsive UI. Automatic capture prefers
+hardware AVC, then a Surface-capable MediaCodec VP8 encoder using the existing
+software-VP8 native path. An explicit AVC request never silently switches codec.
+Quality settings use the shared capture limits; the selected MediaCodec admits
+each requested profile during startup.
+When supported by that encoder, constant-bitrate mode keeps Android's quality
+floor from overriding the network allocation.
+The existing WebRTC FrameDropper also gates each encoder's input from its actual
+output size, so a codec that reaches its compression limit can reduce frame rate
+instead of ignoring the allocation. The shared capture clock and other outputs
+continue independently.
+Devices without either encoder can still join rooms. Piik needs an installed
+system browser.
+TV capture depends on the system's Projection and encoder support. Physical TV
+checks are pending.
+
+Native Android capture reuses the shared screen-share output plan and P2P/SFU
+transports. One Projection and captured texture feed the original and requested
+lower renditions. Optional encoders start on demand, retire when unused and leave
+the original available if the device cannot admit another encoder. Received
+media can use the same forwarding path.
+Live bitrate follows the shared [media-quality contract](../docs/standards/media-quality.md):
+remote allocations reach the encoder without the local preview holding it at its ceiling.
+Android can control a desktop through the shared browser controls; it does not
+accept remote keyboard, mouse or virtual-gamepad input itself. Detached tool
+windows depend on the browser's Document Picture-in-Picture support.
+
+## Build
+
+Use JDK 17, Gradle 8.11.1, Android platform/build-tools 35, platform-tools and
+NDK 30.0.16248370. Set `ANDROID_HOME` and `ANDROID_NDK_HOME` to those local tools.
+Set `GRADLE_HOME` to the installed Gradle distribution. Install the pinned
+`gomobile` and `gobind` tools, then run the repository build entry from its root:
+
+```sh
+go install golang.org/x/mobile/cmd/[email protected]
+go install golang.org/x/mobile/cmd/[email protected]
+node scripts/build-android.mjs
+```
+
+The entry builds the same Web assets, generates the existing icon and license
+notices, binds the Go runtime for both supported ABIs and packages the APK.
+The single mobile Go entry lives in `mobile/bridge`; this command generates its
+Android binding.
+`PIIK_GOMOBILE` may name an installed `gomobile` executable outside `PATH`.
+
+The build injects the same product version and source revision used by desktop
+App packages. The shared startup page displays that identity; development APKs
+also include their short revision in Android's application version.
+
+The build produces `android/app/build/outputs/apk/debug/app-debug.apk`
+relative to `mobile/`. The application ID remains `tv.piik.android`, so an update
+signed with the same key replaces the previous installation while retaining App settings.
+Generated libraries, APKs, Gradle state and signing material stay
+ignored. The `checklinkname` flag is required by the existing Android networking
+dependency [anet on Go 1.23+](https://github.com/wlynxg/anet); it is confined to
+this platform build. Android uses the repository's single Go dependency graph;
+pins and temporary upstream replacements remain owned by the root `go.mod`.
+
+### Signed release APK
+
+Use the same entry with `--release --output <new-directory>`. Set
+`PIIK_BUILD_VERSION` to the planned product version, and inject signing material
+through `PIIK_ANDROID_KEYSTORE` (absolute path), `PIIK_ANDROID_STORE_PASSWORD`,
+`PIIK_ANDROID_KEY_ALIAS` and `PIIK_ANDROID_KEY_PASSWORD`. Keep the keystore and
+passwords outside tracked files; back up the long-lived signing key securely.
+Missing signing inputs fail the release build. Release packages reject Android
+debug certificates and debuggable APKs. Release builds require a clean checkout
+and verify that the same clean source revision still owns the final output.
+
+The universal `piik-app-android-universal.apk` contains arm64 and x86-64.
+The build verifies its signature, package identity, version and ABIs, then writes
+the existing schema-2 release descriptor and SHA-256 sidecar beside the APK.
+The existing CI App matrix publishes it with the matching desktop/Server
+artifacts. Publication requires four repository secrets:
+`PIIK_ANDROID_KEYSTORE_BASE64` (the keystore bytes in Base64),
+`PIIK_ANDROID_STORE_PASSWORD`, `PIIK_ANDROID_KEY_ALIAS` and
+`PIIK_ANDROID_KEY_PASSWORD`. Manual candidate runs use the same release variant
+with a disposable signing key, and never publish.
+
+A formal APK cannot update an earlier debug-signed candidate. Uninstall that
+candidate before installing the first formal release; this removes its saved App
+settings. Later formal updates retain settings when signed with the same key.
+
+Install with `adb install -r <apk>`. The shared startup page offers Local and
+Public link. Use **+** beside the site section to enter a Piik site address;
+confirm its displayed origin before allowing and opening it. Approval, revocation
+and the starred startup site use the same management UI as desktop.
+On a connected site, use **App → Open App page** to manage saved origins.
+
+The ongoing notification opens Piik or stops the local backend. Starting screen
+sharing opens App for system recording consent when the browser permits the
+handoff; **Confirm in App** remains available when another click is needed.
+Replacing an active native source reuses recording consent without opening App.
+App returns to the original browser tab after the complete capture startup settles.
+Opening App while a request is pending also handles
+that request, so sites without the new shortcut can still confirm in App.
+The website's App approval still requires its displayed exact origin; waking
+App does not authorize a site. If the connection card cannot find App, choose
+**Open Piik App**, then return to the same browser page to continue discovery.
+Choose **Open App page** and confirm the displayed origin before granting access.
+Camera and room voice use the browser's own permissions.
+
+Local starts only when selected; Hosted sites use their own room service and
+the existing native App connection. Site
+approval and the starred startup site use the same persisted App configuration.
+Public link uses the same shared room service and supervised official cloudflared
+helper as desktop. Android executes the helper from the installed APK native
+library directory and writes its temporary configuration into the app cache.
+Failed room startup can retry from the same page after the failed attempt has
+retired. An unexpected room-service or helper exit ends that room; the launcher,
+App connection and other approved sites remain available. Select the room mode
+again to start a fresh room.
+
+Create a room and select Android screen capture in the ordinary source picker.
+Approve the Android projection prompt. Pure video does not request microphone
+or camera access. Camera sharing and room voice request their permissions in the
+browser. Native playback audio and native commentary request recording permission only
+when enabled. Commentary uses the OS-default microphone or an input from the
+existing device selector; it joins the same Go mixer and stable shared audio
+track as source sound. The capture service adds microphone foreground eligibility
+only for that input lifetime. Room voice keeps its independent Browser input.
+The browser owns navigation, fullscreen and camera/microphone capture lifetimes;
+Piik owns the native screen and source-audio resources.
+
+Screen capture and device sound do not turn on the microphone. Native commentary
+is enabled separately and uses Android's voice-communication capture mode for
+available system echo processing. Android limits simultaneous microphone capture
+by separate apps: if browser room voice and App commentary compete, use one
+microphone path. Headphones also prevent speaker sound from feeding back into
+the microphone.
+
+If a local room stops responding after switching to the browser or locking the
+screen, check Piik's battery and background-activity settings. Some devices
+restrict background network access even while the process remains running.
+Allow unrestricted background activity for Piik and keep its ongoing notification
+enabled. [Xiaomi's network troubleshooting](https://www.mi.com/ph/support/faq/details/KA-538010/)
+describes this battery-policy setting.
+
+Open the invitation in another device's Browser. Local HTTPS requires the usual
+certificate confirmation. Stop from Piik or Android's projection indicator;
+Android can also end projection on screen lock. Capture and optional audio use
+the existing stream replacement transaction; the foreground service owns the
+projection token and its single VirtualDisplay.
+
+Platform checks are summarized in [verification status](../docs/verification-status.md#device-evidence-boundary);
+remaining work stays in the [TODO ledger](../docs/todo.md).
+
+Platform references: [Projection lifecycle](https://developer.android.com/media/grow/media-projection),
+[playback audio](https://developer.android.com/media/platform/av-capture),
+[foreground service](https://developer.android.com/develop/background-work/services/fgs/service-types#media-projection),
+[encoder rate control](https://developer.android.com/reference/android/media/MediaCodec#minimum-quality-floor-for-video-encoding),
+[Go bindings](https://pkg.go.dev/golang.org/x/mobile/cmd/gomobile).
diff --git a/mobile/android/app/build.gradle b/mobile/android/app/build.gradle
new file mode 100644
index 00000000..f315e639
--- /dev/null
+++ b/mobile/android/app/build.gradle
@@ -0,0 +1,26 @@
+plugins { id 'com.android.application' }
+android {
+ namespace 'tv.piik.android'
+ compileSdk 35
+ defaultConfig {
+ applicationId 'tv.piik.android'; minSdk 29; targetSdk 35
+ versionCode providers.gradleProperty('piikVersionCode').getOrElse('1').toInteger()
+ versionName providers.gradleProperty('piikVersion').getOrElse('development')
+ ndk { abiFilters 'arm64-v8a', 'x86_64' }
+ }
+ compileOptions { sourceCompatibility JavaVersion.VERSION_17; targetCompatibility JavaVersion.VERSION_17 }
+ packaging { jniLibs { useLegacyPackaging = true } }
+ signingConfigs {
+ release {
+ def keystore = System.getenv('PIIK_ANDROID_KEYSTORE')
+ if (keystore) {
+ storeFile file(keystore)
+ storePassword System.getenv('PIIK_ANDROID_STORE_PASSWORD')
+ keyAlias System.getenv('PIIK_ANDROID_KEY_ALIAS')
+ keyPassword System.getenv('PIIK_ANDROID_KEY_PASSWORD')
+ }
+ }
+ }
+ buildTypes { release { signingConfig signingConfigs.release } }
+}
+dependencies { implementation files('libs/piik.aar') }
diff --git a/mobile/android/app/src/main/AndroidManifest.xml b/mobile/android/app/src/main/AndroidManifest.xml
new file mode 100644
index 00000000..e5c0071c
--- /dev/null
+++ b/mobile/android/app/src/main/AndroidManifest.xml
@@ -0,0 +1,24 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+ <uses-permission android:name="android.permission.INTERNET"/>
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
+ <uses-permission android:name="android.permission.RECORD_AUDIO"/>
+ <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION"/>
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE"/>
+ <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
+ <uses-feature android:name="android.hardware.microphone" android:required="false"/>
+ <uses-feature android:name="android.software.leanback" android:required="false"/>
+ <application android:label="@string/app_name" android:icon="@drawable/piik" android:theme="@style/PiikTheme" android:usesCleartextTraffic="true" android:allowBackup="false">
+ <activity android:name=".MainActivity" android:exported="true" android:configChanges="orientation|screenSize|keyboardHidden" android:resizeableActivity="true">
+ <intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/><category android:name="android.intent.category.LEANBACK_LAUNCHER"/></intent-filter>
+ <intent-filter><action android:name="android.intent.action.VIEW"/><category android:name="android.intent.category.DEFAULT"/><category android:name="android.intent.category.BROWSABLE"/><data android:scheme="piik" android:host="app"/><data android:scheme="piik" android:host="media"/></intent-filter>
+ </activity>
+ <service android:name=".CaptureService" android:foregroundServiceType="mediaProjection|microphone" android:exported="false"/>
+ <service android:name=".AppRuntimeService" android:foregroundServiceType="specialUse" android:exported="false">
+ <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="User-started local room server and native screen-sharing companion for the user's browser; runs until the user stops Piik."/>
+ </service>
+ </application>
+</manifest>
diff --git a/mobile/android/app/src/main/java/tv/piik/android/AppRuntimeService.java b/mobile/android/app/src/main/java/tv/piik/android/AppRuntimeService.java
new file mode 100644
index 00000000..d267f332
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/AppRuntimeService.java
@@ -0,0 +1,403 @@
+package tv.piik.android;
+
+import android.Manifest;
+import android.app.*;
+import android.content.*;
+import android.content.pm.PackageManager;
+import android.content.pm.ServiceInfo;
+import android.media.projection.MediaProjectionConfig;
+import android.media.projection.MediaProjectionManager;
+import android.net.*;
+import android.os.*;
+import java.net.Inet4Address;
+import java.util.ArrayDeque;
+import java.util.concurrent.*;
+import tv.piik.bridge.Bridge;
+import tv.piik.bridge.Runtime;
+
+// One user-started native companion owns the Go core and pending OS consent.
+public final class AppRuntimeService extends Service {
+ static volatile AppRuntimeService instance;
+ static final Handler main = new Handler(Looper.getMainLooper());
+ static final int PROJECTION = 10, AUDIO_PERMISSION = 12;
+ final ExecutorService work = Executors.newSingleThreadExecutor();
+ final ArrayDeque<MediaRequest> requests = new ArrayDeque<>();
+ Runtime runtime;
+ CompletableFuture<Runtime> startup;
+ Throwable failure;
+ MediaRequest active;
+ // Go prepares selected source audio after the first admitted video frame.
+ MediaRequest preparing;
+
+ static final class MediaRequest {
+ final Runtime owner;
+ final long id;
+ final String kind, configuration;
+ boolean awaitingResult;
+
+ MediaRequest(Runtime owner, long id, String kind, String configuration) {
+ this.owner = owner;
+ this.id = id;
+ this.kind = kind;
+ this.configuration = configuration;
+ }
+
+ boolean current() {
+ return owner == AppRuntimeService.runtime() && owner.captureCurrent(id);
+ }
+ }
+
+ static Runtime runtime() {
+ return instance == null ? null : instance.runtime;
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ instance = this;
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ if (intent != null && "stop".equals(intent.getAction())) {
+ stopSelf();
+ return START_NOT_STICKY;
+ }
+ try {
+ foreground();
+ } catch (RuntimeException error) {
+ failure = error;
+ notifyActivity();
+ stopSelf();
+ return START_NOT_STICKY;
+ }
+ if (runtime != null || failure != null) notifyActivity();
+ else if (startup == null) startRuntime();
+ return START_NOT_STICKY;
+ }
+
+ void foreground() {
+ startForeground(
+ 3,
+ notification(),
+ Build.VERSION.SDK_INT >= 34 ? ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE : 0);
+ }
+
+ void refreshNotification() {
+ getSystemService(NotificationManager.class).notify(3, notification());
+ }
+
+ Notification notification() {
+ NotificationManager notifications = getSystemService(NotificationManager.class);
+ notifications.createNotificationChannel(
+ new NotificationChannel(
+ "piik-runtime",
+ getString(R.string.runtime_channel),
+ NotificationManager.IMPORTANCE_LOW));
+ PendingIntent open =
+ PendingIntent.getActivity(
+ this,
+ 20,
+ new Intent(this, MainActivity.class),
+ PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
+ PendingIntent stop =
+ PendingIntent.getService(
+ this,
+ 21,
+ new Intent(this, AppRuntimeService.class).setAction("stop"),
+ PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
+ int title =
+ failure != null ? R.string.runtime_failed : runtime != null ? R.string.runtime_title : R.string.preparing;
+ int body =
+ failure != null ? R.string.runtime_restart : runtime != null ? R.string.runtime_body : R.string.runtime_channel;
+ return new Notification.Builder(this, "piik-runtime")
+ .setSmallIcon(android.R.drawable.ic_menu_share)
+ .setContentTitle(getString(title))
+ .setContentText(getString(body))
+ .setContentIntent(open)
+ .setOngoing(true)
+ .addAction(
+ new Notification.Action.Builder(null, getString(R.string.runtime_open), open)
+ .build())
+ .addAction(
+ new Notification.Action.Builder(null, getString(R.string.runtime_stop), stop)
+ .build())
+ .build();
+ }
+
+ void startRuntime() {
+ CompletableFuture<Runtime> starting =
+ CompletableFuture.supplyAsync(
+ () -> {
+ try {
+ ConnectivityManager manager = getSystemService(ConnectivityManager.class);
+ LinkProperties links = manager.getLinkProperties(manager.getActiveNetwork());
+ String address = "";
+ if (links != null)
+ for (LinkAddress link : links.getLinkAddresses())
+ if (link.getAddress() instanceof Inet4Address
+ && !link.getAddress().isLoopbackAddress()) {
+ address = link.getAddress().getHostAddress();
+ break;
+ }
+ CapturePlatform platform = new CapturePlatform(getApplicationContext());
+ Runtime started =
+ Bridge.start(
+ platform,
+ address,
+ EncoderCatalog.capabilities(),
+ getFilesDir().getAbsolutePath(),
+ getCacheDir().getAbsolutePath(),
+ new java.io.File(getApplicationInfo().nativeLibraryDir, "libcloudflared.so")
+ .getAbsolutePath(),
+ "piik://media");
+ platform.owner = new java.lang.ref.WeakReference<>(started);
+ return started;
+ } catch (Exception error) {
+ throw new CompletionException(error);
+ }
+ },
+ work);
+ startup = starting;
+ starting.whenComplete(
+ (started, error) ->
+ main.post(
+ () -> {
+ if (instance != this || startup != starting) {
+ return;
+ }
+ startup = null;
+ runtime = started;
+ failure = error;
+ refreshNotification();
+ notifyActivity();
+ }));
+ }
+
+ void notifyActivity() {
+ MainActivity activity = MainActivity.current;
+ if (activity != null && activity.visible) activity.runtimeAvailable(this);
+ }
+
+ static void enqueue(Runtime owner, long id, String kind, String configuration) {
+ AppRuntimeService service = instance;
+ if (service == null || owner == null || owner != service.runtime || !owner.captureCurrent(id))
+ return;
+ if (kind.equals("video") && CaptureService.replaceVideo(owner, id, configuration)) return;
+ if (kind.equals("audio")
+ && service.checkSelfPermission(Manifest.permission.RECORD_AUDIO)
+ == PackageManager.PERMISSION_GRANTED) {
+ CaptureService.startAudio(owner, id);
+ return;
+ }
+ service.requests.add(new MediaRequest(owner, id, kind, configuration));
+ service.processRequests();
+ }
+
+ void processRequests() {
+ if (active != null) return;
+ requests.removeIf(request -> !request.current());
+ MainActivity activity = MainActivity.current;
+ if (activity == null
+ || !activity.visible
+ || activity.notificationRequest
+ || !activity.canConfirmMedia()) return;
+ MediaRequest next = requests.poll();
+ if (next == null) {
+ if (preparing == null) activity.mediaFinished();
+ return;
+ }
+ active = next;
+ activity.mediaStarted();
+ activity.permissionOwner = this;
+ activity.permissionRequest = next;
+ if (next.kind.equals("video")) {
+ preparing = next;
+ try {
+ MediaProjectionManager manager = activity.getSystemService(MediaProjectionManager.class);
+ Intent consent =
+ Build.VERSION.SDK_INT >= 34
+ ? manager.createScreenCaptureIntent(
+ MediaProjectionConfig.createConfigForDefaultDisplay())
+ : manager.createScreenCaptureIntent();
+ next.awaitingResult = true;
+ activity.startActivityForResult(consent, PROJECTION);
+ } catch (RuntimeException error) {
+ android.util.Log.w("PiikRuntime", "projection consent failed", error);
+ complete(next, false);
+ }
+ } else if (activity.checkSelfPermission(Manifest.permission.RECORD_AUDIO)
+ != PackageManager.PERMISSION_GRANTED) {
+ next.awaitingResult = true;
+ activity.requestPermissions(
+ new String[] {Manifest.permission.RECORD_AUDIO}, AUDIO_PERMISSION);
+ } else audioPermissionResult(next);
+ }
+
+ void projectionResult(MainActivity activity, MediaRequest request, int result, Intent data) {
+ if (instance != this || request == null || active != request || !request.kind.equals("video"))
+ return;
+ request.awaitingResult = false;
+ if (result == Activity.RESULT_CANCELED && request.current())
+ request.owner.captureCanceled(request.id);
+ if (result != Activity.RESULT_OK || data == null || !request.current()) {
+ complete(request, false);
+ return;
+ }
+ Intent capture =
+ new Intent(this, CaptureService.class)
+ .putExtra("result", result)
+ .putExtra("data", data)
+ .putExtra("id", request.id)
+ .putExtra("config", request.configuration);
+ Bundle ownership = new Bundle();
+ ownership.putBinder("owner", new CaptureService.ProjectionOwner(request.owner));
+ capture.putExtras(ownership);
+ try {
+ activity.startForegroundService(capture);
+ } catch (RuntimeException error) {
+ complete(request, false);
+ }
+ // Return only after the physical service has admitted or failed this request.
+ }
+
+ void audioPermissionResult(MediaRequest request) {
+ if (instance != this || request == null || active != request || request.kind.equals("video"))
+ return;
+ request.awaitingResult = false;
+ if (!request.current()
+ || checkSelfPermission(Manifest.permission.RECORD_AUDIO)
+ != PackageManager.PERMISSION_GRANTED) {
+ complete(request, false);
+ } else if (request.kind.equals("microphone")) {
+ CaptureService.startMicrophone(request.owner, request.id, request.configuration);
+ } else CaptureService.startAudio(request.owner, request.id);
+ }
+
+ static void prepared(Runtime owner, long id) {
+ main.post(
+ () -> {
+ AppRuntimeService service = instance;
+ if (service != null
+ && service.active != null
+ && service.active.owner == owner
+ && service.active.id == id) service.complete(service.active, true);
+ });
+ }
+
+ static void preparationDone(Runtime owner, long id) {
+ AppRuntimeService service = instance;
+ if (service == null
+ || service.preparing == null
+ || service.preparing.owner != owner
+ || service.preparing.id != id) return;
+ service.preparing = null;
+ service.processRequests();
+ }
+
+ static void failed(Runtime owner, long id) {
+ main.post(
+ () -> {
+ AppRuntimeService service = instance;
+ if (service != null
+ && service.active != null
+ && service.active.owner == owner
+ && service.active.id == id
+ && !service.active.awaitingResult) service.complete(service.active, false);
+ });
+ }
+
+ static void retire(Runtime owner, long id) {
+ AppRuntimeService service = instance;
+ if (service == null || owner != service.runtime) return;
+ service.requests.removeIf(request -> request.owner == owner && request.id == id);
+ if (service.preparing != null && service.preparing.owner == owner && service.preparing.id == id)
+ service.preparing = null;
+ if (service.active != null
+ && service.active.owner == owner
+ && service.active.id == id
+ && !service.active.awaitingResult) service.complete(service.active, false);
+ else service.processRequests();
+ // Keep an in-flight OS result bound to its original request until it returns.
+ }
+
+ void cancelPending() {
+ for (MediaRequest request : requests) request.owner.captureEnded(request.id);
+ requests.clear();
+ if (preparing != null) {
+ preparing.owner.captureEnded(preparing.id);
+ preparing = null;
+ }
+ if (active != null && !active.awaitingResult) complete(active, false);
+ }
+
+ void complete(MediaRequest request, boolean admitted) {
+ if (active != request) return;
+ active = null;
+ if (!admitted && preparing == request) preparing = null;
+ MainActivity activity = MainActivity.current;
+ if (activity != null && activity.permissionRequest == request) {
+ activity.permissionOwner = null;
+ activity.permissionRequest = null;
+ }
+ if (!admitted) request.owner.captureEnded(request.id);
+ processRequests();
+ }
+
+ static void runtimeStopped(Runtime owner, String reason) {
+ AppRuntimeService service = instance;
+ if (service == null || service.runtime != owner) return;
+ service.failure = new IllegalStateException(reason);
+ service.runtime = null;
+ service.requests.clear();
+ service.active = null;
+ service.preparing = null;
+ service.refreshNotification();
+ CaptureService.shutdown();
+ service.notifyActivity();
+ }
+
+ static void stop() {
+ if (instance != null) instance.stopSelf();
+ }
+
+ @Override
+ public void onDestroy() {
+ if (instance == this) {
+ instance = null;
+ CaptureService.shutdown();
+ }
+ CompletableFuture<Runtime> pending = startup;
+ startup = null;
+ Runtime retired = runtime;
+ runtime = null;
+ requests.clear();
+ active = null;
+ preparing = null;
+ work.execute(
+ () -> {
+ Runtime closing = retired;
+ if (pending != null) {
+ try {
+ closing = pending.join();
+ } catch (CompletionException ignored) {
+ }
+ }
+ if (closing != null) {
+ try {
+ closing.close();
+ } catch (Exception error) {
+ android.util.Log.w("PiikRuntime", "runtime shutdown failed", error);
+ }
+ }
+ });
+ work.shutdown();
+ stopForeground(STOP_FOREGROUND_REMOVE);
+ super.onDestroy();
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/CapturePlatform.java b/mobile/android/app/src/main/java/tv/piik/android/CapturePlatform.java
new file mode 100644
index 00000000..494f527d
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/CapturePlatform.java
@@ -0,0 +1,69 @@
+package tv.piik.android;
+
+import java.lang.ref.WeakReference;
+import tv.piik.bridge.Platform;
+import tv.piik.bridge.Runtime;
+
+// Each JNI adapter belongs to one Go runtime, even when stream IDs restart at one.
+final class CapturePlatform implements Platform {
+ final android.content.Context context;
+ volatile WeakReference<Runtime> owner = new WeakReference<>(null);
+
+ CapturePlatform(android.content.Context context) {
+ this.context = context.getApplicationContext();
+ }
+
+ public String microphones() throws Exception {
+ return MicrophoneDevices.list(context);
+ }
+
+ public void startMicrophone(long id, String device) {
+ Runtime runtime = owner.get();
+ MainActivity.main.post(
+ () -> {
+ AppRuntimeService.enqueue(runtime, id, "microphone", device);
+ });
+ }
+
+ public void startVideo(long id, String config) {
+ Runtime runtime = owner.get();
+ MainActivity.main.post(
+ () -> {
+ AppRuntimeService.enqueue(runtime, id, "video", config);
+ });
+ }
+
+ public void startAudio(long id) {
+ Runtime runtime = owner.get();
+ MainActivity.main.post(
+ () -> {
+ AppRuntimeService.enqueue(runtime, id, "audio", "");
+ });
+ }
+
+ public void control(long id, String command, long layer, long value) {
+ CaptureService.control(owner.get(), id, command, (int) layer, value);
+ }
+
+ public void stop(long id) {
+ Runtime runtime = owner.get();
+ MainActivity.main.post(
+ () -> {
+ AppRuntimeService.retire(runtime, id);
+ CaptureService.stopCapture(runtime, id);
+ });
+ }
+
+ public void preparationDone(long id) {
+ Runtime runtime = owner.get();
+ MainActivity.main.post(() -> AppRuntimeService.preparationDone(runtime, id));
+ }
+
+ // Site revocation is enforced by the shared control service; Android owns no page.
+ public void siteAccessChanged() {}
+
+ public void runtimeStopped(String reason) {
+ Runtime runtime = owner.get();
+ MainActivity.main.post(() -> AppRuntimeService.runtimeStopped(runtime, reason));
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/CaptureRenderer.java b/mobile/android/app/src/main/java/tv/piik/android/CaptureRenderer.java
new file mode 100644
index 00000000..3bcaf5f7
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/CaptureRenderer.java
@@ -0,0 +1,342 @@
+package tv.piik.android;
+
+import android.graphics.SurfaceTexture;
+import android.opengl.*;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.view.Surface;
+import java.nio.*;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+
+// One Projection retains its latest GPU image. Encoder callbacks never wait for GL swaps.
+final class CaptureRenderer {
+ static final class Target {
+ final Surface surface;
+ final int width, height, sourceWidth, sourceHeight;
+ final Consumer<RuntimeException> failed;
+ final AtomicBoolean refreshQueued = new AtomicBoolean();
+ volatile boolean retired;
+ volatile boolean enabled = true;
+ Target owner;
+ Consumer<Long> inputFrame;
+ EGLSurface window = EGL14.EGL_NO_SURFACE;
+ long timestamp, inputTimestamp;
+ int bitrate, framerate;
+ boolean recovery = true;
+ FrameDropper dropper;
+
+ Target(Surface surface, int width, int height, int sourceWidth, int sourceHeight,
+ Consumer<RuntimeException> failed) {
+ this.surface = surface;
+ this.width = width;
+ this.height = height;
+ this.sourceWidth = sourceWidth;
+ this.sourceHeight = sourceHeight;
+ this.failed = failed;
+ }
+ }
+
+ final Handler callbacks;
+ final Consumer<RuntimeException> failedInitialization;
+ final HandlerThread thread = new HandlerThread("Piik capture GL");
+ final Handler gl;
+ final Set<Target> targets = new HashSet<>();
+ final float[] transform = new float[16];
+ final FloatBuffer vertices =
+ ByteBuffer.allocateDirect(16 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
+ volatile boolean closing;
+ EGLDisplay display = EGL14.EGL_NO_DISPLAY;
+ EGLContext context = EGL14.EGL_NO_CONTEXT;
+ EGLSurface parking = EGL14.EGL_NO_SURFACE;
+ EGLConfig config;
+ SurfaceTexture texture;
+ Surface input;
+ Target current;
+ int textureID, program, position, coordinates, matrix;
+ boolean hasFrame;
+ long frameTimestamp;
+ int sourceWidth, sourceHeight;
+
+ CaptureRenderer(Handler callbacks, Consumer<RuntimeException> failedInitialization) {
+ this.callbacks = callbacks;
+ this.failedInitialization = failedInitialization;
+ vertices.put(new float[] {-1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1});
+ thread.start();
+ gl = new Handler(thread.getLooper());
+ }
+
+ void select(Target target, Consumer<Surface> bound) {
+ gl.post(
+ () -> {
+ if (closing || target.retired) return;
+ if (texture == null) {
+ try {
+ initialize();
+ } catch (RuntimeException error) {
+ callbacks.post(() -> { if (!closing) failedInitialization.accept(error); });
+ return;
+ }
+ }
+ try {
+ if (target.window == EGL14.EGL_NO_SURFACE) {
+ target.window =
+ EGL14.eglCreateWindowSurface(
+ display, config, target.surface, new int[] {EGL14.EGL_NONE}, 0);
+ require(target.window != EGL14.EGL_NO_SURFACE, "create encoder surface");
+ targets.add(target);
+ target.dropper = new FrameDropper(target.bitrate, target.framerate);
+ }
+ // A rotated source must supply its new image before it proves a new format.
+ if (sourceWidth != target.sourceWidth || sourceHeight != target.sourceHeight)
+ hasFrame = false;
+ sourceWidth = target.sourceWidth;
+ sourceHeight = target.sourceHeight;
+ texture.setDefaultBufferSize(target.width, target.height);
+ current = target;
+ if (hasFrame) captureFrame();
+ callbacks.post(() -> { if (!closing && !target.retired) bound.accept(input); });
+ draw(target);
+ } catch (RuntimeException error) {
+ failed(target, error);
+ }
+ });
+ }
+
+ // Additional renditions sample the same retained texture and keep the original capture size.
+ void attach(Target target, Target owner) {
+ target.owner = owner;
+ gl.post(() -> {
+ if (closing || target.retired || owner.retired) return;
+ try {
+ target.window = EGL14.eglCreateWindowSurface(display, config, target.surface,
+ new int[] {EGL14.EGL_NONE}, 0);
+ require(target.window != EGL14.EGL_NO_SURFACE, "create derived encoder surface");
+ targets.add(target);
+ target.dropper = new FrameDropper(target.bitrate, target.framerate);
+ draw(target);
+ } catch (RuntimeException error) { failed(target, error); }
+ });
+ }
+
+ void refresh(Target target) {
+ if (target == null || closing || target.retired
+ || !target.refreshQueued.compareAndSet(false, true)) return;
+ gl.post(
+ () -> {
+ target.refreshQueued.set(false);
+ target.recovery = true;
+ try {
+ if (hasFrame && (target == current || target.owner == current)) captureFrame();
+ draw(target);
+ } catch (RuntimeException error) {
+ failed(target, error);
+ }
+ });
+ }
+
+ void encoded(Target target, int bytes, boolean keyframe) {
+ gl.post(() -> {
+ if (!closing && !target.retired && target.dropper != null)
+ target.dropper.encoded(bytes, keyframe);
+ });
+ }
+
+ void bitrate(Target target, int bitrate) {
+ gl.post(() -> {
+ if (!closing && !target.retired && target.dropper != null) target.dropper.bitrate(bitrate);
+ });
+ }
+
+ void release(Target target, Runnable released) {
+ target.retired = true;
+ gl.post(
+ () -> {
+ if (current == target) current = null;
+ destroyTarget(target);
+ callbacks.post(released);
+ });
+ }
+
+ void close(Runnable closed) {
+ closing = true;
+ gl.post(
+ () -> {
+ current = null;
+ if (texture != null) {
+ texture.setOnFrameAvailableListener(null);
+ texture.release();
+ }
+ if (input != null) input.release();
+ for (Target target : new HashSet<>(targets)) destroyTarget(target);
+ if (display != EGL14.EGL_NO_DISPLAY) {
+ EGL14.eglMakeCurrent(display, parking, parking, context);
+ if (program != 0) GLES20.glDeleteProgram(program);
+ if (textureID != 0) GLES20.glDeleteTextures(1, new int[] {textureID}, 0);
+ EGL14.eglMakeCurrent(
+ display, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT);
+ if (parking != EGL14.EGL_NO_SURFACE) EGL14.eglDestroySurface(display, parking);
+ if (context != EGL14.EGL_NO_CONTEXT) EGL14.eglDestroyContext(display, context);
+ EGL14.eglReleaseThread();
+ EGL14.eglTerminate(display);
+ }
+ callbacks.post(closed);
+ thread.quitSafely();
+ });
+ }
+
+ void initialize() {
+ display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
+ require(display != EGL14.EGL_NO_DISPLAY, "get EGL display");
+ int[] versions = new int[2];
+ require(EGL14.eglInitialize(display, versions, 0, versions, 1), "initialize EGL");
+ EGLConfig[] configs = new EGLConfig[1];
+ int[] count = new int[1];
+ require(
+ EGL14.eglChooseConfig(
+ display,
+ new int[] {
+ EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
+ EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT | EGL14.EGL_PBUFFER_BIT,
+ EGL14.EGL_RED_SIZE, 8, EGL14.EGL_GREEN_SIZE, 8, EGL14.EGL_BLUE_SIZE, 8,
+ 0x3142, 1, // EGL_RECORDABLE_ANDROID
+ EGL14.EGL_NONE
+ },
+ 0, configs, 0, 1, count, 0) && count[0] > 0,
+ "choose recordable EGL config");
+ config = configs[0];
+ context =
+ EGL14.eglCreateContext(
+ display, config, EGL14.EGL_NO_CONTEXT,
+ new int[] {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE}, 0);
+ require(context != EGL14.EGL_NO_CONTEXT, "create GL context");
+ parking =
+ EGL14.eglCreatePbufferSurface(
+ display, config,
+ new int[] {EGL14.EGL_WIDTH, 1, EGL14.EGL_HEIGHT, 1, EGL14.EGL_NONE}, 0);
+ require(parking != EGL14.EGL_NO_SURFACE, "create GL parking surface");
+ makeCurrent(parking);
+ program = GLES20.glCreateProgram();
+ int vertex = shader(GLES20.GL_VERTEX_SHADER,
+ "attribute vec4 position; attribute vec4 coordinates; uniform mat4 transform;"
+ + "varying vec2 uv; void main(){gl_Position=position;uv=(transform*coordinates).xy;}");
+ int fragment = shader(GLES20.GL_FRAGMENT_SHADER,
+ "#extension GL_OES_EGL_image_external : require\nprecision mediump float;"
+ + "uniform samplerExternalOES image; varying vec2 uv;"
+ + "void main(){gl_FragColor=texture2D(image,uv);}");
+ GLES20.glAttachShader(program, vertex);
+ GLES20.glAttachShader(program, fragment);
+ GLES20.glLinkProgram(program);
+ GLES20.glDeleteShader(vertex);
+ GLES20.glDeleteShader(fragment);
+ int[] linked = new int[1];
+ GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linked, 0);
+ require(linked[0] != 0, "link texture program");
+ position = GLES20.glGetAttribLocation(program, "position");
+ coordinates = GLES20.glGetAttribLocation(program, "coordinates");
+ matrix = GLES20.glGetUniformLocation(program, "transform");
+ int[] names = new int[1];
+ GLES20.glGenTextures(1, names, 0);
+ textureID = names[0];
+ GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureID);
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
+ texture = new SurfaceTexture(textureID);
+ input = new Surface(texture);
+ texture.setOnFrameAvailableListener(
+ ignored -> {
+ if (closing) return;
+ Target target = current;
+ try {
+ makeCurrent(parking);
+ texture.updateTexImage();
+ texture.getTransformMatrix(transform);
+ hasFrame = true;
+ captureFrame();
+ for (Target output : new HashSet<>(targets)) {
+ try { draw(output); } catch (RuntimeException error) { failed(output, error); }
+ }
+ } catch (RuntimeException error) {
+ if (target != null) failed(target, error);
+ }
+ }, gl);
+ }
+
+ void captureFrame() {
+ frameTimestamp = Math.max(System.nanoTime(), frameTimestamp + 1000);
+ Target owner = current;
+ if (owner != null && owner.inputFrame != null) {
+ long timestamp = frameTimestamp / 1000;
+ callbacks.post(() -> { if (!closing && !owner.retired) owner.inputFrame.accept(timestamp); });
+ }
+ }
+
+ void draw(Target target) {
+ if (closing || target == null || (current != target && current != target.owner) || target.retired || !target.enabled || !hasFrame) return;
+ if (frameTimestamp <= target.inputTimestamp) return;
+ target.inputTimestamp = frameTimestamp;
+ if (!target.dropper.shouldEncode(frameTimestamp, target.recovery)) return;
+ makeCurrent(target.window);
+ GLES20.glViewport(0, 0, target.width, target.height);
+ GLES20.glUseProgram(program);
+ GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
+ GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureID);
+ vertices.position(0);
+ GLES20.glVertexAttribPointer(position, 2, GLES20.GL_FLOAT, false, 16, vertices);
+ vertices.position(2);
+ GLES20.glVertexAttribPointer(coordinates, 2, GLES20.GL_FLOAT, false, 16, vertices);
+ GLES20.glEnableVertexAttribArray(position);
+ GLES20.glEnableVertexAttribArray(coordinates);
+ GLES20.glUniformMatrix4fv(matrix, 1, false, transform, 0);
+ GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
+ target.timestamp = frameTimestamp;
+ require(EGLExt.eglPresentationTimeANDROID(display, target.window, target.timestamp), "set presentation time");
+ // This can wait for codec input space. Its output callbacks run on another thread.
+ require(EGL14.eglSwapBuffers(display, target.window), "submit encoder image");
+ target.recovery = false;
+ require(GLES20.glGetError() == GLES20.GL_NO_ERROR, "render captured image");
+ }
+
+ void destroyTarget(Target target) {
+ if (target.dropper != null) {
+ target.dropper.close();
+ target.dropper = null;
+ }
+ if (target.window == EGL14.EGL_NO_SURFACE) return;
+ // Teardown still attempts every owned release after an EGL failure.
+ EGL14.eglMakeCurrent(display, parking, parking, context);
+ EGL14.eglDestroySurface(display, target.window);
+ target.window = EGL14.EGL_NO_SURFACE;
+ targets.remove(target);
+ }
+
+ void makeCurrent(EGLSurface surface) {
+ require(EGL14.eglMakeCurrent(display, surface, surface, context), "make GL context current");
+ }
+
+ void failed(Target target, RuntimeException error) {
+ callbacks.post(() -> { if (!closing && !target.retired) target.failed.accept(error); });
+ }
+
+ static int shader(int type, String source) {
+ int shader = GLES20.glCreateShader(type);
+ GLES20.glShaderSource(shader, source);
+ GLES20.glCompileShader(shader);
+ int[] compiled = new int[1];
+ GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
+ if (compiled[0] == 0) {
+ String error = GLES20.glGetShaderInfoLog(shader);
+ GLES20.glDeleteShader(shader);
+ throw new IllegalStateException("Capture shader failed: " + error);
+ }
+ return shader;
+ }
+
+ static void require(boolean passed, String operation) {
+ if (!passed) throw new IllegalStateException("Could not " + operation);
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/CaptureService.java b/mobile/android/app/src/main/java/tv/piik/android/CaptureService.java
new file mode 100644
index 00000000..c7a583c8
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/CaptureService.java
@@ -0,0 +1,1054 @@
+package tv.piik.android;
+
+import android.app.*;
+import android.content.*;
+import android.content.pm.PackageManager;
+import android.content.pm.ServiceInfo;
+import android.hardware.display.*;
+import android.media.*;
+import android.media.projection.*;
+import android.os.*;
+import android.view.*;
+import java.nio.*;
+import java.util.*;
+import org.json.*;
+
+public final class CaptureService extends Service {
+ static volatile CaptureService instance;
+
+ static final class ProjectionOwner extends Binder {
+ final tv.piik.bridge.Runtime runtime;
+
+ ProjectionOwner(tv.piik.bridge.Runtime runtime) {
+ this.runtime = runtime;
+ }
+ }
+
+ static final String CHANNEL = "piik-sharing";
+ HandlerThread thread;
+ Handler handler;
+ volatile MediaProjection projection;
+ VirtualDisplay display;
+ CaptureRenderer renderer;
+ final Set<Video> allocatedVideos = new HashSet<>();
+ int retiringCodecs;
+ volatile Video video;
+ volatile Video previousVideo;
+ CapturedAudio audio;
+ CapturedAudio previousAudio;
+ volatile CapturedAudio microphone, previousMicrophone;
+ Notification notice;
+ int foregroundTypes;
+ int captureWidth, captureHeight;
+ volatile boolean retiring;
+ volatile boolean stopRequested;
+ boolean destroyed, cleanupDone, restoringVideo;
+
+ final class Video {
+ final long id;
+ final tv.piik.bridge.Runtime runtime;
+
+ Video(long id, tv.piik.bridge.Runtime runtime) {
+ this.id = id;
+ this.runtime = runtime;
+ }
+
+ MediaCodec codec;
+ Surface surface;
+ CaptureRenderer.Target target;
+ int width, height, fps, bitrate, sourceWidth, sourceHeight;
+ String configuration;
+ byte[] config;
+ long base = -1, last = -1;
+ boolean ready, proven, released, cleaned;
+ final List<Runnable> releaseCallbacks = new ArrayList<>();
+ String actualCodec, profile = "";
+ List<EncoderCatalog.Entry> candidates;
+ int candidateIndex;
+ long deadline;
+ Runnable timeout;
+ Video replaced, root;
+ int layer = 1;
+ JSONArray outputs;
+ final Video[] children = new Video[6];
+ final int[] rates = new int[6];
+ final boolean[] active = new boolean[6], unavailable = new boolean[6];
+
+ Video owner() { return root == null ? this : root; }
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ thread = new HandlerThread("Piik capture");
+ thread.start();
+ handler = new Handler(thread.getLooper());
+ instance = this;
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ IBinder token =
+ intent == null || intent.getExtras() == null ? null : intent.getExtras().getBinder("owner");
+ final var runtime = token instanceof ProjectionOwner ? ((ProjectionOwner) token).runtime : null;
+ android.util.Log.d(
+ "PiikCapture",
+ "projection service received; localOwner="
+ + (token instanceof ProjectionOwner)
+ + ", current="
+ + (runtime == AppRuntimeService.runtime()));
+ if (runtime == null || runtime != AppRuntimeService.runtime()) {
+ if (projection == null) stopSelfResult(startId);
+ return START_NOT_STICKY;
+ }
+ if (intent != null && (retiring || stopRequested)) {
+ fail(runtime, intent.getLongExtra("id", 0));
+ stopSelf();
+ return START_NOT_STICKY;
+ }
+ if (intent == null
+ || runtime == null
+ || !runtime.captureCurrent(intent.getLongExtra("id", 0))) {
+ stopSelf();
+ return START_NOT_STICKY;
+ }
+ NotificationManager notifications = getSystemService(NotificationManager.class);
+ notifications.createNotificationChannel(
+ new NotificationChannel(
+ CHANNEL, getString(R.string.notification_channel), NotificationManager.IMPORTANCE_LOW));
+ PendingIntent open =
+ PendingIntent.getActivity(
+ this,
+ 0,
+ new Intent(this, MainActivity.class),
+ PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
+ notice =
+ new Notification.Builder(this, CHANNEL)
+ .setSmallIcon(android.R.drawable.ic_menu_share)
+ .setContentTitle(getString(R.string.notification_title))
+ .setContentText(getString(R.string.notification_body))
+ .setContentIntent(open)
+ .setOngoing(true)
+ .build();
+ startForeground(1, notice, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
+ foregroundTypes = ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION;
+ Intent consent = intent.getParcelableExtra("data");
+ int result = intent.getIntExtra("result", 0);
+ long id = intent.getLongExtra("id", 0);
+ String config = intent.getStringExtra("config");
+ handler.post(
+ () -> {
+ if (retiring || runtime == null || !runtime.captureCurrent(id)) {
+ fail(runtime, id);
+ finishProjection();
+ return;
+ }
+ try {
+ projection =
+ getSystemService(MediaProjectionManager.class).getMediaProjection(result, consent);
+ projection.registerCallback(
+ new MediaProjection.Callback() {
+ @Override
+ public void onCapturedContentResize(int width, int height) {
+ captureWidth = width;
+ captureHeight = height;
+ resizeVideo();
+ }
+
+ @Override
+ public void onStop() {
+ finishProjection();
+ }
+ },
+ handler);
+ beginVideo(runtime, id, config);
+ } catch (Exception error) {
+ fail(runtime, id);
+ finishProjection();
+ }
+ });
+ return START_NOT_STICKY;
+ }
+
+ static boolean replaceVideo(tv.piik.bridge.Runtime owner, long id, String config) {
+ CaptureService s = instance;
+ if (s == null
+ || s.projection == null
+ || s.stopRequested
+ || s.video == null
+ || s.video.runtime != owner) return false;
+ s.handler.post(() -> s.beginVideo(owner, id, config));
+ return true;
+ }
+
+ void beginVideo(tv.piik.bridge.Runtime runtime, long id, String json) {
+ if (runtime == null || runtime != AppRuntimeService.runtime() || !runtime.captureCurrent(id))
+ return;
+ if (retiring || stopRequested || projection == null) {
+ fail(runtime, id);
+ return;
+ }
+ if (previousVideo != null || restoringVideo) {
+ fail(runtime, id);
+ return;
+ }
+ // Keep the original usable while freeing optional instances for transactional preparation.
+ Video current = video;
+ List<Video> lower = new ArrayList<>();
+ if (current != null)
+ for (int layer = 0; layer < current.children.length; layer++)
+ if (layer != 1 && current.children[layer] != null) lower.add(current.children[layer]);
+ if (!lower.isEmpty()) {
+ restoringVideo = true;
+ int[] remaining = {lower.size()};
+ for (Video output : lower) releaseVideo(output, () -> {
+ if (current.children[output.layer] == output) current.children[output.layer] = null;
+ if (--remaining[0] == 0) {
+ restoringVideo = false;
+ beginVideo(runtime, id, json);
+ }
+ });
+ return;
+ }
+ beginVideo(
+ runtime,
+ id,
+ json,
+ EncoderCatalog.candidates(codecRequested(json)),
+ 0,
+ SystemClock.uptimeMillis() + 5000);
+ }
+
+ static String codecRequested(String json) {
+ try {
+ return new JSONObject(json).getString("codec");
+ } catch (Exception error) {
+ return "";
+ }
+ }
+
+ void beginVideo(
+ tv.piik.bridge.Runtime runtime,
+ long id,
+ String json,
+ List<EncoderCatalog.Entry> candidates,
+ int index,
+ long deadline) {
+ createVideo(runtime, id, json, candidates, index, deadline, null, 1);
+ }
+
+ void createVideo(tv.piik.bridge.Runtime runtime, long id, String json,
+ List<EncoderCatalog.Entry> candidates, int index, long deadline, Video root, int layer) {
+ if (runtime != AppRuntimeService.runtime()
+ || !runtime.captureCurrent(id)
+ || retiring
+ || stopRequested
+ || projection == null) return;
+ if (index >= candidates.size() || SystemClock.uptimeMillis() >= deadline) {
+ if (root != null) {
+ unavailableOutput(root, layer);
+ return;
+ }
+ // A rotation reuses the running stream identity; restored old capture stays valid.
+ if (video != null && video.id == id && video.runtime == runtime) return;
+ fail(runtime, id);
+ if (video == null) finishProjection();
+ return;
+ }
+ Video next = new Video(id, runtime);
+ next.root = root;
+ next.layer = layer;
+ if (root != null) root.children[layer] = next;
+ next.candidates = candidates;
+ next.candidateIndex = index;
+ next.deadline = deadline;
+ next.configuration = json;
+ Video old = root == null ? video : null;
+ next.replaced = old;
+ try {
+ JSONObject options = new JSONObject(json);
+ if (root == null) {
+ next.outputs = options.getJSONArray("outputs");
+ if (next.outputs.length() < 2 || next.outputs.length() > 6)
+ throw new IllegalArgumentException("Invalid capture outputs");
+ for (int slot = 0; slot < next.outputs.length(); slot++)
+ next.rates[slot] = next.outputs.getJSONObject(slot).getInt("bitrate");
+ next.children[1] = next;
+ } else options = root.outputs.getJSONObject(layer);
+ if (captureWidth <= 0 || captureHeight <= 0) updateCaptureSize();
+ next.sourceWidth = captureWidth;
+ next.sourceHeight = captureHeight;
+ double scale =
+ Math.min(
+ 1.0,
+ Math.min(
+ options.getDouble("width") / captureWidth,
+ options.getDouble("height") / captureHeight));
+ next.width = Math.max(2, ((int) (captureWidth * scale)) & ~1);
+ next.height = Math.max(2, ((int) (captureHeight * scale)) & ~1);
+ next.fps = options.getInt("fps");
+ EncoderCatalog.Entry selected = candidates.get(index);
+ next.actualCodec = selected.codec;
+ boolean h264 = selected.codec.equals("h264");
+ MediaCodecInfo.CodecCapabilities capabilities = EncoderCatalog.capabilities(selected);
+ if (codecInstances(selected.name, next) > capabilities.getMaxSupportedInstances())
+ throw new IllegalStateException("Encoder concurrent-instance capacity exhausted");
+ next.codec = MediaCodec.createByCodecName(selected.name);
+ allocatedVideos.add(next);
+ MediaCodecInfo.VideoCapabilities supported = capabilities.getVideoCapabilities();
+ next.width -= next.width % Math.max(2, supported.getWidthAlignment());
+ next.height -= next.height % Math.max(2, supported.getHeightAlignment());
+ MediaFormat format =
+ MediaFormat.createVideoFormat(
+ h264 ? "video/avc" : "video/x-vnd.on2.vp8", next.width, next.height);
+ format.setInteger(
+ MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
+ next.bitrate = root == null ? options.getInt("bitrate") : root.rates[layer];
+ format.setInteger(MediaFormat.KEY_BIT_RATE, next.bitrate);
+ // CBR avoids Android's VBR quality floor overriding transport bitrate budgets.
+ MediaCodecInfo.EncoderCapabilities rateControl = capabilities.getEncoderCapabilities();
+ if (rateControl.isBitrateModeSupported(MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CBR_FD))
+ format.setInteger(
+ MediaFormat.KEY_BITRATE_MODE, MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CBR_FD);
+ else if (rateControl.isBitrateModeSupported(MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CBR))
+ format.setInteger(
+ MediaFormat.KEY_BITRATE_MODE, MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CBR);
+ format.setInteger(MediaFormat.KEY_FRAME_RATE, next.fps);
+ format.setFloat(MediaFormat.KEY_MAX_FPS_TO_ENCODER, next.fps);
+ // Recovery requests keyframes on demand; some encoders reject -1 to disable the period.
+ format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3600);
+ if (h264)
+ format.setInteger(
+ MediaFormat.KEY_PROFILE,
+ MediaCodecInfo.CodecProfileLevel.AVCProfileConstrainedBaseline);
+ next.codec.setCallback(
+ new MediaCodec.Callback() {
+ @Override
+ public void onInputBufferAvailable(MediaCodec codec, int index) {}
+
+ @Override
+ public void onOutputBufferAvailable(
+ MediaCodec codec, int index, MediaCodec.BufferInfo info) {
+ try {
+ if (!currentVideo(next)) return;
+ ByteBuffer buffer = codec.getOutputBuffer(index);
+ if (buffer == null || info.size == 0) return;
+ buffer.position(info.offset);
+ buffer.limit(info.offset + info.size);
+ byte[] bytes = new byte[info.size];
+ buffer.get(bytes);
+ if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
+ next.config = bytes;
+ return;
+ }
+ if (!next.ready) return;
+ long pts = info.presentationTimeUs;
+ Video clock = next.owner();
+ if (clock.base < 0) clock.base = pts;
+ pts -= clock.base;
+ if (pts <= next.last) return;
+ next.last = pts;
+ boolean key = (info.flags & MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0;
+ if (h264 && key && next.config != null) {
+ byte[] combined = new byte[next.config.length + bytes.length];
+ System.arraycopy(next.config, 0, combined, 0, next.config.length);
+ System.arraycopy(bytes, 0, combined, next.config.length, bytes.length);
+ bytes = combined;
+ }
+ renderer.encoded(next.target, bytes.length, key);
+ boolean firstFrame = !next.proven;
+ if (firstFrame) {
+ if (SystemClock.uptimeMillis() >= next.deadline) {
+ retryVideo(next);
+ return;
+ }
+ if (!key) return;
+ boolean admitted = root == null
+ ? runtime.videoReady(id, next.actualCodec, next.width, next.height, next.profile)
+ : runtime.videoLayerReady(id, layer, next.actualCodec, next.width, next.height, next.profile);
+ if (!admitted) {
+ retryVideo(next);
+ return;
+ }
+ next.proven = true;
+ if (root == null) AppRuntimeService.prepared(runtime, id);
+ handler.removeCallbacks(next.timeout);
+ if (next.replaced != null && next.replaced.id == id) {
+ retireVideo(next.replaced);
+ next.replaced = null;
+ }
+ if (root == null) {
+ JSONObject locked = new JSONObject(next.configuration);
+ locked.put("codec", next.actualCodec);
+ next.configuration = locked.toString();
+ }
+ }
+ if (firstFrame && root == null) runtime.videoInput(id, pts);
+ runtime.videoLayer(id, layer, bytes, pts, 1000000 / next.fps, next.width, next.height, key);
+ if (firstFrame && root == null) handler.post(CaptureService.this::resizeVideo);
+ } catch (Exception error) {
+ retryVideo(next);
+ } finally {
+ try {
+ codec.releaseOutputBuffer(index, false);
+ } catch (Exception ignored) {
+ }
+ }
+ }
+
+ @Override
+ public void onError(MediaCodec codec, MediaCodec.CodecException error) {
+ if (!currentVideo(next)) return;
+ android.util.Log.w("PiikCapture", "encoder failed", error);
+ retryVideo(next);
+ }
+
+ @Override
+ public void onOutputFormatChanged(MediaCodec codec, MediaFormat output) {
+ if (!currentVideo(next)) return;
+ try {
+ if (!h264) {
+ next.profile = "";
+ next.ready = true;
+ return;
+ }
+ ByteBuffer a = output.getByteBuffer("csd-0"), b = output.getByteBuffer("csd-1");
+ byte[] sps = copy(a), pps = copy(b);
+ next.config = new byte[sps.length + pps.length];
+ System.arraycopy(sps, 0, next.config, 0, sps.length);
+ System.arraycopy(pps, 0, next.config, sps.length, pps.length);
+ String profile = null;
+ for (int i = 0; i + 3 < sps.length; i++)
+ if ((sps[i] & 31) == 7) {
+ profile =
+ String.format(
+ java.util.Locale.ROOT,
+ "%02x%02x%02x",
+ sps[i + 1] & 255,
+ sps[i + 2] & 255,
+ sps[i + 3] & 255);
+ break;
+ }
+ if (profile == null) throw new Exception("AVC configuration missing");
+ next.profile = profile;
+ next.ready = true;
+ } catch (Exception error) {
+ retryVideo(next);
+ }
+ }
+ },
+ handler);
+ next.codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
+ next.surface = next.codec.createInputSurface();
+ next.codec.start();
+ if (!runtime.captureCurrent(id)) throw new IllegalStateException("capture retired");
+ // The Go stream owner closes old on commit or next on rollback.
+ if (root == null) video = next;
+ next.timeout =
+ () -> {
+ if (!next.proven && currentVideo(next)) retryVideo(next);
+ };
+ // Consent has completed. Share one finite first-frame budget across encoders;
+ // a silent candidate yields to another, never to an idle watchdog.
+ handler.postAtTime(
+ next.timeout,
+ EncoderCatalog.attemptDeadline(
+ SystemClock.uptimeMillis(), deadline, index + 1 < candidates.size()));
+ if (old != null && old.id == id) {
+ // Display rotation changes the physical encoder, not the Go stream clock.
+ next.base = old.base;
+ next.last = old.last;
+ } else if (root == null) previousVideo = old;
+ if (renderer == null) renderer = new CaptureRenderer(handler,
+ error -> {
+ android.util.Log.w("PiikCapture", "GPU capture initialization failed", error);
+ fail(runtime, id);
+ finishProjection();
+ });
+ next.target = new CaptureRenderer.Target(next.surface, next.width, next.height,
+ next.sourceWidth, next.sourceHeight,
+ error -> {
+ android.util.Log.w("PiikCapture", "GPU capture failed", error);
+ if (currentVideo(next)) retryVideo(next);
+ });
+ next.target.bitrate = next.bitrate;
+ next.target.framerate = next.fps;
+ if (root == null) next.target.inputFrame = pts -> {
+ if (!currentVideo(next)) return;
+ if (next.base < 0) next.base = pts;
+ next.runtime.videoInput(next.id, pts - next.base);
+ };
+ if (root == null) bindVideo(next, () -> {});
+ else renderer.attach(next.target, root.target);
+ } catch (Exception error) {
+ android.util.Log.w("PiikCapture", "video capture failed", error);
+ retryVideo(next);
+ }
+ }
+
+ void bindVideo(Video value, Runnable bound) {
+ renderer.select(value.target,
+ input -> {
+ if (retiring || stopRequested || video != value || value.released) return;
+ try {
+ int density = getResources().getDisplayMetrics().densityDpi;
+ if (display == null)
+ display = projection.createVirtualDisplay(
+ "Piik", value.width, value.height, density,
+ DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, input, null, handler);
+ else display.resize(value.width, value.height, density);
+ if (display == null) throw new IllegalStateException("Could not create captured display");
+ bound.run();
+ } catch (RuntimeException error) {
+ fail(value.runtime, value.id);
+ finishProjection();
+ }
+ });
+ }
+
+ void retryVideo(Video failed) {
+ if (failed.released) return;
+ if (failed.root != null) {
+ unavailableOutput(failed.root, failed.layer);
+ return;
+ }
+ if (failed.timeout != null) handler.removeCallbacks(failed.timeout);
+ if (failed.proven) {
+ fail(failed.runtime, failed.id);
+ return;
+ }
+ Video old = failed.replaced;
+ restoringVideo = true;
+ releaseVideo(failed,
+ () -> {
+ if (retiring || stopRequested) {
+ retireVideo(old);
+ return;
+ }
+ if (video == failed) video = old;
+ if (previousVideo == old) previousVideo = null;
+ Runnable retry = () -> {
+ restoringVideo = false;
+ beginVideo(failed.runtime, failed.id, failed.configuration, failed.candidates,
+ failed.candidateIndex + 1, failed.deadline);
+ };
+ if (old == null) retry.run();
+ else bindVideo(old,
+ () -> {
+ control(old.runtime, old.id, "keyframe", 0);
+ retry.run();
+ });
+ });
+ }
+
+ boolean currentVideo(Video value) {
+ Video root = value.owner();
+ return !value.released && !root.released && (video == root || previousVideo == root)
+ && root.children[value.layer] == value;
+ }
+
+ int codecInstances(String name, Video pending) {
+ int count = 1;
+ for (Video output : allocatedVideos)
+ if (output != pending && output.codec.getName().equals(name)) count++;
+ return count;
+ }
+
+ void startOutput(Video root, int layer) {
+ if (!root.proven || root.released || video != root || previousVideo != null
+ || retiringCodecs > 0) return;
+ EncoderCatalog.Entry selected = root.candidates.get(root.candidateIndex);
+ createVideo(root.runtime, root.id, root.configuration, Collections.singletonList(selected),
+ 0, SystemClock.uptimeMillis() + 2000, root, layer);
+ }
+
+ void unavailableOutput(Video root, int layer) {
+ if (root.unavailable[layer]) return;
+ root.unavailable[layer] = true;
+ Video failed = root.children[layer];
+ root.children[layer] = null;
+ releaseVideo(failed);
+ root.runtime.videoLayerUnavailable(root.id, layer);
+ }
+
+ void updateCaptureSize() {
+ android.graphics.Point size = new android.graphics.Point();
+ getSystemService(WindowManager.class).getDefaultDisplay().getRealSize(size);
+ captureWidth = size.x;
+ captureHeight = size.y;
+ }
+
+ void resizeVideo() {
+ if (video == null || !video.proven || previousVideo != null || restoringVideo
+ || retiring || stopRequested)
+ return;
+ if (video.sourceWidth != captureWidth || video.sourceHeight != captureHeight)
+ beginVideo(video.runtime, video.id, video.configuration);
+ }
+
+ @Override
+ public void onConfigurationChanged(android.content.res.Configuration configuration) {
+ super.onConfigurationChanged(configuration);
+ handler.post(
+ () -> {
+ updateCaptureSize();
+ resizeVideo();
+ });
+ }
+
+ static byte[] copy(ByteBuffer source) {
+ if (source == null) return new byte[0];
+ ByteBuffer data = source.duplicate();
+ byte[] result = new byte[data.remaining()];
+ data.get(result);
+ return result;
+ }
+
+ static void control(tv.piik.bridge.Runtime owner, long id, String command, long value) {
+ control(owner, id, command, 1, value);
+ }
+
+ static void control(tv.piik.bridge.Runtime owner, long id, String command, int layer, long value) {
+ CaptureService s = instance;
+ if (s != null)
+ s.handler.post(
+ () -> {
+ Video current = s.video;
+ if (current == null || current.id != id || current.runtime != owner) return;
+ if (layer < 0 || layer >= current.outputs.length() || current.unavailable[layer]) return;
+ if (command.equals("bitrate")) current.rates[layer] = (int) value;
+ if (command.equals("active")) {
+ current.active[layer] = value != 0;
+ if (layer == 1) {
+ boolean resume = value != 0 && !current.target.enabled;
+ current.target.enabled = value != 0;
+ if (resume) control(owner, id, "keyframe", layer, 0);
+ return;
+ }
+ if (layer != 1 && value == 0) {
+ Video retired = current.children[layer];
+ if (retired == null || retired.released) return;
+ s.releaseVideo(retired, () -> {
+ if (current.children[layer] == retired) current.children[layer] = null;
+ if (current.active[layer] && !current.unavailable[layer]) s.startOutput(current, layer);
+ });
+ return;
+ }
+ if (layer != 1 && current.children[layer] == null && value != 0)
+ s.startOutput(current, layer);
+ return;
+ }
+ Video output = current.children[layer];
+ if (output == null || output.released) {
+ // Demand for an idle rendition must advance the shared input plan even on a static screen.
+ if (command.equals("keyframe")) s.renderer.refresh(current.target);
+ return;
+ }
+ if (command.equals("bitrate") && output.bitrate == value) return;
+ try {
+ Bundle params = new Bundle();
+ if (command.equals("keyframe"))
+ params.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
+ else if (command.equals("bitrate"))
+ params.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, (int) value);
+ else return;
+ output.codec.setParameters(params);
+ if (command.equals("bitrate")) {
+ output.bitrate = (int) value;
+ s.renderer.bitrate(output.target, (int) value);
+ }
+ if (command.equals("keyframe")) s.renderer.refresh(output.target);
+ } catch (Exception error) {
+ android.util.Log.w("PiikCapture", "encoder control failed", error);
+ if (layer == 1) s.fail(current.runtime, id);
+ else s.unavailableOutput(current, layer);
+ }
+ });
+ }
+
+ static void startAudio(tv.piik.bridge.Runtime owner, long id) {
+ CaptureService s = instance;
+ if (s == null) {
+ if (owner != null) owner.captureEnded(id);
+ return;
+ }
+ s.handler.post(() -> s.beginAudio(owner, id));
+ }
+
+ void beginAudio(tv.piik.bridge.Runtime runtime, long id) {
+ if (runtime == null || runtime != AppRuntimeService.runtime() || !runtime.captureCurrent(id))
+ return;
+ if (retiring || stopRequested) {
+ fail(runtime, id);
+ return;
+ }
+ if (previousAudio != null) {
+ fail(runtime, id);
+ return;
+ }
+ CapturedAudio next = new CapturedAudio(id, runtime);
+ CapturedAudio old = audio;
+ try {
+ if (projection == null
+ || checkSelfPermission(android.Manifest.permission.RECORD_AUDIO)
+ != PackageManager.PERMISSION_GRANTED) {
+ fail(runtime, id);
+ return;
+ }
+ AudioPlaybackCaptureConfiguration config =
+ new AudioPlaybackCaptureConfiguration.Builder(projection)
+ .addMatchingUsage(AudioAttributes.USAGE_MEDIA)
+ .addMatchingUsage(AudioAttributes.USAGE_GAME)
+ .addMatchingUsage(AudioAttributes.USAGE_UNKNOWN)
+ .excludeUid(android.os.Process.myUid())
+ .build();
+ AudioFormat format =
+ new AudioFormat.Builder()
+ .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
+ .setSampleRate(48000)
+ .setChannelMask(AudioFormat.CHANNEL_IN_STEREO)
+ .build();
+ int minimum =
+ AudioRecord.getMinBufferSize(
+ 48000, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
+ next.recorder =
+ new AudioRecord.Builder()
+ .setAudioFormat(format)
+ .setBufferSizeInBytes(Math.max(minimum, 3840 * 4))
+ .setAudioPlaybackCaptureConfig(config)
+ .build();
+ next.recorder.startRecording();
+ if (next.recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING)
+ throw new IllegalStateException("Playback audio did not start");
+ if (!runtime.captureCurrent(id)) throw new IllegalStateException("capture retired");
+ audio = next;
+ previousAudio = old;
+ next.start(2);
+ } catch (Exception error) {
+ android.util.Log.w("PiikCapture", "playback audio capture failed", error);
+ if (audio == next) {
+ audio = old;
+ previousAudio = null;
+ }
+ CapturedAudio.close(next);
+ }
+ }
+
+ static void startMicrophone(tv.piik.bridge.Runtime owner, long id, String device) {
+ CaptureService service = instance;
+ if (service == null) {
+ owner.captureEnded(id);
+ return;
+ }
+ service.handler.post(
+ () -> {
+ if (owner != AppRuntimeService.runtime() || !owner.captureCurrent(id)) return;
+ if (service.retiring
+ || service.stopRequested
+ || service.projection == null
+ || service.previousMicrophone != null) {
+ service.fail(owner, id);
+ return;
+ }
+ CapturedAudio next = new CapturedAudio(id, owner);
+ service.previousMicrophone = service.microphone;
+ service.microphone = next;
+ MainActivity.main.post(
+ () -> {
+ if (owner != AppRuntimeService.runtime()
+ || !owner.captureCurrent(id)
+ || service.microphone != next) {
+ service.handler.post(() -> service.discardMicrophone(next));
+ return;
+ }
+ try {
+ MainActivity activity = MainActivity.current;
+ if (service.previousMicrophone == null && (activity == null || !activity.visible))
+ throw new IllegalStateException("Open Piik before starting commentary");
+ service.updateMicrophoneForeground();
+ service.handler.post(() -> service.beginMicrophone(next, device));
+ } catch (RuntimeException error) {
+ service.handler.post(() -> service.discardMicrophone(next));
+ }
+ });
+ });
+ }
+
+ void beginMicrophone(CapturedAudio next, String deviceID) {
+ if (microphone != next
+ || next.stopped
+ || next.runtime != AppRuntimeService.runtime()
+ || !next.runtime.captureCurrent(next.id)) return;
+ try {
+ AudioDeviceInfo selected = MicrophoneDevices.selected(this, deviceID);
+ AudioFormat format =
+ new AudioFormat.Builder()
+ .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
+ .setSampleRate(48000)
+ .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
+ .build();
+ int minimum =
+ AudioRecord.getMinBufferSize(
+ 48000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
+ next.recorder =
+ new AudioRecord.Builder()
+ // Commentary is speech; let the platform apply its available echo processing.
+ .setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
+ .setAudioFormat(format)
+ .setBufferSizeInBytes(Math.max(minimum, 1920 * 4))
+ .build();
+ if (selected != null && !next.recorder.setPreferredDevice(selected))
+ throw new IllegalStateException("The selected microphone cannot be used");
+ next.recorder.startRecording();
+ if (next.recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING)
+ throw new IllegalStateException("The microphone did not start");
+ AudioDeviceInfo routed = next.recorder.getRoutedDevice();
+ if (selected != null && (routed == null || selected.getId() != routed.getId()))
+ throw new IllegalStateException("The selected microphone is not routed");
+ if (!next.runtime.captureCurrent(next.id))
+ throw new IllegalStateException("Microphone retired");
+ next.start(1);
+ } catch (Exception error) {
+ android.util.Log.w("PiikCapture", "commentary capture failed", error);
+ discardMicrophone(next);
+ }
+ }
+
+ void discardMicrophone(CapturedAudio next) {
+ if (microphone == next) {
+ microphone = previousMicrophone;
+ previousMicrophone = null;
+ }
+ CapturedAudio.close(next);
+ refreshMicrophoneForeground();
+ }
+
+ void updateMicrophoneForeground() {
+ if (projection == null || retiring || stopRequested)
+ throw new IllegalStateException("Projection retired");
+ int desired = ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION;
+ if (Build.VERSION.SDK_INT >= 30 && (microphone != null || previousMicrophone != null))
+ desired |= ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE;
+ if (foregroundTypes != desired) {
+ startForeground(1, notice, desired);
+ foregroundTypes = desired;
+ }
+ }
+
+ void refreshMicrophoneForeground() {
+ MainActivity.main.post(
+ () -> {
+ if (instance != this || projection == null || retiring || stopRequested) return;
+ try {
+ updateMicrophoneForeground();
+ } catch (RuntimeException error) {
+ android.util.Log.w("PiikCapture", "capture foreground types failed", error);
+ handler.post(this::finishProjection);
+ }
+ });
+ }
+
+ static void stopCapture(tv.piik.bridge.Runtime owner, long id) {
+ CaptureService s = instance;
+ if (s != null) {
+ if (s.video != null
+ && s.video.runtime == owner
+ && s.video.id == id
+ && s.previousVideo == null) s.stopRequested = true;
+ s.handler.post(
+ () -> {
+ if (s.previousMicrophone != null
+ && s.previousMicrophone.runtime == owner
+ && s.previousMicrophone.id == id) {
+ CapturedAudio committed = s.previousMicrophone;
+ s.previousMicrophone = null;
+ CapturedAudio.close(committed);
+ s.refreshMicrophoneForeground();
+ }
+ if (s.microphone != null && s.microphone.runtime == owner && s.microphone.id == id) {
+ CapturedAudio retired = s.microphone;
+ s.microphone = s.previousMicrophone;
+ s.previousMicrophone = null;
+ CapturedAudio.close(retired);
+ s.refreshMicrophoneForeground();
+ }
+ if (s.previousAudio != null
+ && s.previousAudio.runtime == owner
+ && s.previousAudio.id == id) {
+ CapturedAudio committed = s.previousAudio;
+ s.previousAudio = null;
+ CapturedAudio.close(committed);
+ }
+ if (s.audio != null && s.audio.runtime == owner && s.audio.id == id) {
+ CapturedAudio old = s.audio;
+ s.audio = s.previousAudio;
+ s.previousAudio = null;
+ CapturedAudio.close(old);
+ }
+ if (s.previousVideo != null
+ && s.previousVideo.runtime == owner
+ && s.previousVideo.id == id) {
+ Video committed = s.previousVideo;
+ s.previousVideo = null;
+ if (s.video != null && s.video.replaced == committed) s.video.replaced = null;
+ s.retireVideo(committed);
+ s.resizeVideo();
+ }
+ if (s.video != null && s.video.runtime == owner && s.video.id == id) {
+ if (s.previousVideo == null) {
+ s.finishProjection();
+ return;
+ }
+ Video discarded = s.video;
+ try {
+ Video restored = s.previousVideo;
+ s.video = restored;
+ s.previousVideo = null;
+ // Output queued while preparing next was discarded. Resume
+ // independently of those missing reference frames.
+ s.bindVideo(restored,
+ () -> {
+ control(restored.runtime, restored.id, "keyframe", 0);
+ s.releaseVideo(discarded);
+ s.resizeVideo();
+ });
+ } catch (Exception error) {
+ s.finishProjection();
+ }
+ }
+ });
+ }
+ }
+
+ void fail(tv.piik.bridge.Runtime runtime, long id) {
+ if (runtime != null) {
+ runtime.captureEnded(id);
+ AppRuntimeService.failed(runtime, id);
+ }
+ }
+
+ void releaseVideo(Video value) {
+ releaseVideo(value, () -> {});
+ }
+
+ void releaseVideo(Video value, Runnable released) {
+ if (value == null || value.cleaned) {
+ released.run();
+ return;
+ }
+ value.releaseCallbacks.add(released);
+ if (value.released) return;
+ value.released = true;
+ if (value.codec != null) retiringCodecs++;
+ if (value.root == null) {
+ for (int layer = 0; layer < value.children.length; layer++) {
+ if (layer != 1) {
+ Video child = value.children[layer];
+ value.children[layer] = null;
+ releaseVideo(child);
+ }
+ }
+ }
+ if (value.timeout != null) handler.removeCallbacks(value.timeout);
+ // Retire its EGL window before stopping the codec or releasing its input Surface.
+ Runnable finish = () -> {
+ releaseCodec(value);
+ value.cleaned = true;
+ List<Runnable> callbacks = new ArrayList<>(value.releaseCallbacks);
+ value.releaseCallbacks.clear();
+ for (Runnable callback : callbacks) callback.run();
+ };
+ if (value.target != null) renderer.release(value.target, finish);
+ else finish.run();
+ }
+
+ void releaseCodec(Video value) {
+ try {
+ if (value.codec != null) value.codec.stop();
+ } catch (Exception ignored) {
+ }
+ try {
+ if (value.codec != null) value.codec.release();
+ } catch (Exception ignored) {
+ }
+ if (value.surface != null) value.surface.release();
+ if (allocatedVideos.remove(value)) retiringCodecs--;
+ }
+
+ // A retired target can retain an unfinished rotation of the same Go stream.
+ // Rollback releases only the failed candidate; final retirement owns its chain.
+ void retireVideo(Video value) {
+ while (value != null) {
+ Video previous = value.replaced;
+ value.replaced = null;
+ releaseVideo(value);
+ value = previous;
+ }
+ }
+
+ void finishProjection() {
+ if (retiring) return;
+ retiring = true;
+ Video old = video;
+ video = null;
+ Video pendingOld = previousVideo;
+ previousVideo = null;
+ CapturedAudio oldAudio = audio;
+ audio = null;
+ CapturedAudio pendingOldAudio = previousAudio;
+ previousAudio = null;
+ CapturedAudio oldMicrophone = microphone;
+ microphone = null;
+ CapturedAudio oldPreviousMicrophone = previousMicrophone;
+ previousMicrophone = null;
+ if (display != null) {
+ display.release();
+ display = null;
+ }
+ // Unblock JNI output before waiting for the GL queue, including a pending swap.
+ if (old != null) old.runtime.captureEnded(old.id);
+ if (pendingOld != null) pendingOld.runtime.captureEnded(pendingOld.id);
+ retireVideo(old);
+ retireVideo(pendingOld);
+ CapturedAudio.close(oldAudio);
+ CapturedAudio.close(pendingOldAudio);
+ CapturedAudio.close(oldMicrophone);
+ CapturedAudio.close(oldPreviousMicrophone);
+ if (projection != null) {
+ MediaProjection oldProjection = projection;
+ projection = null;
+ oldProjection.stop();
+ }
+ stopForeground(STOP_FOREGROUND_REMOVE);
+ Runnable closed = () -> {
+ cleanupDone = true;
+ stopSelf();
+ if (destroyed) thread.quitSafely();
+ };
+ if (renderer != null) renderer.close(closed);
+ else closed.run();
+ }
+
+ static void shutdown() {
+ CaptureService s = instance;
+ if (s != null) s.handler.post(s::finishProjection);
+ }
+
+ @Override
+ public void onDestroy() {
+ if (instance == this) instance = null;
+ if (handler != null)
+ handler.post(
+ () -> {
+ destroyed = true;
+ finishProjection();
+ if (cleanupDone) thread.quitSafely();
+ });
+ super.onDestroy();
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/CapturedAudio.java b/mobile/android/app/src/main/java/tv/piik/android/CapturedAudio.java
new file mode 100644
index 00000000..3d6adac3
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/CapturedAudio.java
@@ -0,0 +1,81 @@
+package tv.piik.android;
+
+import android.media.AudioRecord;
+
+// One admitted PCM input owns its recorder and reader until close joins it.
+final class CapturedAudio {
+ final long id;
+ final tv.piik.bridge.Runtime runtime;
+
+ CapturedAudio(long id, tv.piik.bridge.Runtime runtime) {
+ this.id = id;
+ this.runtime = runtime;
+ }
+
+ AudioRecord recorder;
+ volatile boolean stopped;
+ Thread reader;
+
+ void start(int channels) {
+ CapturedAudio input = this;
+ input.reader =
+ new Thread(
+ () -> {
+ byte[] bytes = new byte[960 * channels * 2];
+ byte[] stereo = channels == 2 ? bytes : new byte[3840];
+ try {
+ while (!input.stopped) {
+ int offset = 0;
+ while (offset < bytes.length && !input.stopped) {
+ int count =
+ input.recorder.read(
+ bytes, offset, bytes.length - offset, AudioRecord.READ_BLOCKING);
+ if (count <= 0) throw new IllegalStateException("AudioRecord stopped");
+ offset += count;
+ }
+ if (!input.stopped) {
+ if (channels == 1)
+ for (int sample = 0; sample < 960; sample++) {
+ stereo[sample * 4] = stereo[sample * 4 + 2] = bytes[sample * 2];
+ stereo[sample * 4 + 1] = stereo[sample * 4 + 3] = bytes[sample * 2 + 1];
+ }
+ input.runtime.audio(input.id, stereo);
+ }
+ }
+ } catch (Exception error) {
+ if (!input.stopped) input.runtime.captureEnded(input.id);
+ }
+ },
+ "Piik captured audio");
+ input.reader.start();
+ input.runtime.audioReady(input.id);
+ AppRuntimeService.prepared(input.runtime, input.id);
+ }
+
+ static void close(CapturedAudio value) {
+ if (value == null || value.stopped) return;
+ value.stopped = true;
+ // A full Go frame queue can block the JNI producer independently of AudioRecord.
+ value.runtime.captureEnded(value.id);
+ AppRuntimeService.failed(value.runtime, value.id);
+ if (value.recorder != null) {
+ try {
+ value.recorder.stop();
+ } catch (Exception ignored) {
+ }
+ }
+ if (value.reader != null) {
+ try {
+ value.reader.join(1000);
+ } catch (InterruptedException error) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ if (value.recorder != null) {
+ try {
+ value.recorder.release();
+ } catch (Exception ignored) {
+ }
+ }
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/EncoderCatalog.java b/mobile/android/app/src/main/java/tv/piik/android/EncoderCatalog.java
new file mode 100644
index 00000000..5d8079d0
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/EncoderCatalog.java
@@ -0,0 +1,67 @@
+package tv.piik.android;
+
+import android.media.MediaCodecInfo;
+import android.media.MediaCodecList;
+import java.util.ArrayList;
+import java.util.List;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+/** Android's available Surface encoders; selection never substitutes an explicit codec. */
+final class EncoderCatalog {
+ static final class Entry {
+ final String name, codec;
+ Entry(String name, String codec) {
+ this.name = name;
+ this.codec = codec;
+ }
+ }
+
+ private static List<Entry> entries;
+
+ static synchronized List<Entry> candidates(String requested) {
+ if (entries == null) {
+ entries = new ArrayList<>();
+ for (String mime : new String[] {"video/avc", "video/x-vnd.on2.vp8"}) {
+ for (MediaCodecInfo info : new MediaCodecList(MediaCodecList.REGULAR_CODECS).getCodecInfos()) {
+ if (!info.isEncoder() || (mime.equals("video/avc") && !info.isHardwareAccelerated())) continue;
+ for (String type : info.getSupportedTypes()) {
+ if (!type.equalsIgnoreCase(mime)) continue;
+ try {
+ MediaCodecInfo.CodecCapabilities capabilities = info.getCapabilitiesForType(type);
+ for (int color : capabilities.colorFormats) {
+ if (color == MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface) {
+ entries.add(new Entry(info.getName(), mime.equals("video/avc") ? "h264" : "vp8"));
+ break;
+ }
+ }
+ } catch (IllegalArgumentException unavailable) { /* Continue with the next platform encoder. */ }
+ }
+ }
+ }
+ }
+ List<Entry> selected = new ArrayList<>();
+ for (Entry entry : entries) if (requested.equals("auto") || requested.equals(entry.codec)) selected.add(entry);
+ return selected;
+ }
+
+ static MediaCodecInfo.CodecCapabilities capabilities(Entry entry) {
+ for (MediaCodecInfo info : new MediaCodecList(MediaCodecList.REGULAR_CODECS).getCodecInfos())
+ if (info.getName().equals(entry.name))
+ return info.getCapabilitiesForType(entry.codec.equals("h264") ? "video/avc" : "video/x-vnd.on2.vp8");
+ throw new IllegalArgumentException("Encoder is no longer available");
+ }
+
+ static long attemptDeadline(long now, long deadline, boolean hasNext) {
+ return hasNext ? Math.min(deadline, now + 2000) : deadline;
+ }
+
+ static String capabilities() throws org.json.JSONException {
+ JSONArray encoders = new JSONArray();
+ for (Entry entry : candidates("auto")) {
+ encoders.put(new JSONObject().put("name", entry.name).put("codec", entry.codec));
+ }
+ return new JSONObject().put("platform", "android").put("provider", "Android MediaCodec")
+ .put("multiOutput", true).put("systemAudio", encoders.length() > 0).put("encoders", encoders).toString();
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/FrameDropper.java b/mobile/android/app/src/main/java/tv/piik/android/FrameDropper.java
new file mode 100644
index 00000000..7291e352
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/FrameDropper.java
@@ -0,0 +1,39 @@
+package tv.piik.android;
+
+/** Thin binding to the pinned WebRTC FrameDropper; owned only by the capture GL thread. */
+final class FrameDropper implements AutoCloseable {
+ static {
+ System.loadLibrary("piik_frame_dropper");
+ }
+
+ private long handle;
+
+ FrameDropper(int bitrate, int framerate) {
+ handle = create(bitrate, framerate);
+ if (handle == 0) throw new IllegalStateException("Could not initialize encoder rate control");
+ }
+
+ boolean shouldEncode(long timestamp, boolean recovery) {
+ return shouldEncodeNative(handle, timestamp, recovery);
+ }
+
+ void encoded(int bytes, boolean keyframe) {
+ fill(handle, bytes, keyframe);
+ }
+
+ void bitrate(int bitrate) {
+ setBitrate(handle, bitrate);
+ }
+
+ @Override
+ public void close() {
+ destroy(handle);
+ handle = 0;
+ }
+
+ private static native long create(int bitrate, int framerate);
+ private static native boolean shouldEncodeNative(long handle, long timestamp, boolean recovery);
+ private static native void fill(long handle, int bytes, boolean keyframe);
+ private static native void setBitrate(long handle, int bitrate);
+ private static native void destroy(long handle);
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/MainActivity.java b/mobile/android/app/src/main/java/tv/piik/android/MainActivity.java
new file mode 100644
index 00000000..d059a87d
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/MainActivity.java
@@ -0,0 +1,228 @@
+package tv.piik.android;
+
+import android.app.*;
+import android.content.*;
+import android.net.Uri;
+import android.os.*;
+import android.widget.*;
+
+public final class MainActivity extends Activity {
+ static MainActivity current;
+ static final Handler main = AppRuntimeService.main;
+ boolean visible, handledMedia, notificationRequest;
+ static final int NOTIFICATIONS = 13;
+ AppRuntimeService permissionOwner;
+ AppRuntimeService.MediaRequest permissionRequest;
+
+ static final class PermissionBinding extends Binder {
+ final AppRuntimeService service;
+ final AppRuntimeService.MediaRequest request;
+
+ PermissionBinding(AppRuntimeService service, AppRuntimeService.MediaRequest request) {
+ this.service = service;
+ this.request = request;
+ }
+
+ static PermissionBinding restore(Bundle state) {
+ IBinder saved = state.getBinder("permissionOwner");
+ if (!(saved instanceof PermissionBinding)) return null;
+ PermissionBinding binding = (PermissionBinding) saved;
+ // A canceled Go capture still owns its outstanding physical OS result.
+ return binding.service == AppRuntimeService.instance
+ && binding.service.active == binding.request
+ && binding.request.awaitingResult
+ ? binding
+ : null;
+ }
+ }
+
+ boolean mediaEntry() {
+ return getIntent().getData() != null
+ && "piik://media".equals(getIntent().getData().toString());
+ }
+
+ boolean wakeEntry() {
+ return getIntent().getData() != null && "piik://app".equals(getIntent().getData().toString());
+ }
+
+ boolean canConfirmMedia() {
+ return !wakeEntry() && (getIntent().getData() == null || mediaEntry());
+ }
+
+ @Override
+ public void onCreate(Bundle state) {
+ super.onCreate(state);
+ current = this;
+ if (state != null) {
+ handledMedia = state.getBoolean("handledMedia");
+ notificationRequest = state.getBoolean("notificationRequest");
+ PermissionBinding binding = PermissionBinding.restore(state);
+ if (binding != null) {
+ permissionOwner = binding.service;
+ permissionRequest = binding.request;
+ }
+ }
+ Uri requested = getIntent().getData();
+ if (requested != null && !wakeEntry() && !mediaEntry()) {
+ finish();
+ return;
+ }
+ TextView waiting = new TextView(this);
+ waiting.setPadding(24, 48, 24, 24);
+ waiting.setText(mediaEntry() ? R.string.media_waiting : R.string.preparing);
+ setContentView(WindowContent.wrap(getWindow(), waiting));
+ if (Build.VERSION.SDK_INT >= 33
+ && state == null
+ && requested == null
+ && checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS)
+ != android.content.pm.PackageManager.PERMISSION_GRANTED) {
+ notificationRequest = true;
+ requestPermissions(
+ new String[] {android.Manifest.permission.POST_NOTIFICATIONS}, NOTIFICATIONS);
+ }
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle state) {
+ state.putBoolean("handledMedia", handledMedia);
+ state.putBoolean("notificationRequest", notificationRequest);
+ state.putBinder(
+ "permissionOwner",
+ permissionOwner != null && permissionRequest != null
+ ? new PermissionBinding(permissionOwner, permissionRequest)
+ : null);
+ super.onSaveInstanceState(state);
+ }
+
+ @Override
+ protected void onStart() {
+ super.onStart();
+ current = this;
+ visible = true;
+ // Service foreground readiness precedes reopening even an existing runtime.
+ startForegroundService(new Intent(this, AppRuntimeService.class));
+ }
+
+ @Override
+ protected void onStop() {
+ visible = false;
+ super.onStop();
+ }
+
+ void runtimeAvailable(AppRuntimeService service) {
+ if (current != this || !visible || isFinishing() || notificationRequest) return;
+ if (service.failure != null) {
+ showRuntimeFailure();
+ return;
+ }
+ var owner = AppRuntimeService.runtime();
+ if (owner == null) return;
+ if (wakeEntry()) {
+ finish();
+ return;
+ }
+ if (mediaEntry()
+ || (getIntent().getData() == null
+ && (service.active != null
+ || service.preparing != null
+ || !service.requests.isEmpty()))) {
+ service.processRequests();
+ return;
+ }
+ if (getIntent().getData() == null) {
+ try {
+ startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(owner.entryURL())));
+ } catch (ActivityNotFoundException error) {
+ new AlertDialog.Builder(this)
+ .setMessage(R.string.no_browser)
+ .setPositiveButton(android.R.string.ok, (dialog, which) -> finish())
+ .show();
+ return;
+ }
+ }
+ // App wake returns to its caller. Only the launcher entry opens a URL.
+ finish();
+ }
+
+ void mediaStarted() {
+ handledMedia = true;
+ }
+
+ void mediaFinished() {
+ if (handledMedia) {
+ finish();
+ return;
+ }
+ if (!mediaEntry()) return;
+ LinearLayout empty = new LinearLayout(this);
+ empty.setOrientation(LinearLayout.VERTICAL);
+ empty.setPadding(24, 48, 24, 24);
+ TextView message = new TextView(this);
+ message.setText(R.string.no_pending_confirmation);
+ empty.addView(message);
+ Button back = new Button(this);
+ back.setText(R.string.return_browser);
+ back.setOnClickListener(ignored -> finish());
+ empty.addView(back);
+ setContentView(WindowContent.wrap(getWindow(), empty));
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int request, String[] names, int[] results) {
+ super.onRequestPermissionsResult(request, names, results);
+ if (request == NOTIFICATIONS) {
+ notificationRequest = false;
+ startForegroundService(new Intent(this, AppRuntimeService.class));
+ } else if (request == AppRuntimeService.AUDIO_PERMISSION && permissionOwner != null) {
+ AppRuntimeService owner = permissionOwner;
+ AppRuntimeService.MediaRequest pending = permissionRequest;
+ permissionOwner = null;
+ permissionRequest = null;
+ owner.audioPermissionResult(pending);
+ }
+ }
+
+ @Override
+ protected void onActivityResult(int request, int result, Intent data) {
+ super.onActivityResult(request, result, data);
+ if (request == AppRuntimeService.PROJECTION && permissionOwner != null) {
+ AppRuntimeService owner = permissionOwner;
+ AppRuntimeService.MediaRequest pending = permissionRequest;
+ permissionOwner = null;
+ permissionRequest = null;
+ owner.projectionResult(this, pending, result, data);
+ }
+ }
+
+ @Override
+ public void onBackPressed() {
+ if ((mediaEntry() || handledMedia) && AppRuntimeService.instance != null)
+ AppRuntimeService.instance.cancelPending();
+ finish();
+ }
+
+ @Override
+ protected void onDestroy() {
+ if (current == this) current = null;
+ super.onDestroy();
+ }
+
+ void showRuntimeFailure() {
+ LinearLayout failure = new LinearLayout(this);
+ failure.setOrientation(LinearLayout.VERTICAL);
+ failure.setPadding(24, 48, 24, 24);
+ TextView message = new TextView(this);
+ message.setText(R.string.runtime_failed);
+ failure.addView(message);
+ Button restart = new Button(this);
+ restart.setText(R.string.restart_piik);
+ restart.setOnClickListener(
+ ignored -> {
+ AppRuntimeService.stop();
+ finish();
+ startActivity(new Intent(this, MainActivity.class));
+ });
+ failure.addView(restart);
+ setContentView(WindowContent.wrap(getWindow(), failure));
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/MicrophoneDevices.java b/mobile/android/app/src/main/java/tv/piik/android/MicrophoneDevices.java
new file mode 100644
index 00000000..0e77e8a3
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/MicrophoneDevices.java
@@ -0,0 +1,30 @@
+package tv.piik.android;
+
+import android.content.Context;
+import android.media.AudioDeviceInfo;
+import android.media.AudioManager;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+final class MicrophoneDevices {
+ static String list(Context context) throws Exception {
+ JSONArray result = new JSONArray();
+ for (AudioDeviceInfo device :
+ context.getSystemService(AudioManager.class).getDevices(AudioManager.GET_DEVICES_INPUTS)) {
+ result.put(
+ new JSONObject()
+ .put("id", Integer.toString(device.getId()))
+ .put("label", device.getProductName().toString()));
+ }
+ return result.toString();
+ }
+
+ static AudioDeviceInfo selected(Context context, String id) {
+ if (id.isEmpty()) return null; // Empty is the existing system-default choice.
+ for (AudioDeviceInfo device :
+ context.getSystemService(AudioManager.class).getDevices(AudioManager.GET_DEVICES_INPUTS)) {
+ if (Integer.toString(device.getId()).equals(id)) return device;
+ }
+ throw new IllegalArgumentException("The selected microphone is unavailable");
+ }
+}
diff --git a/mobile/android/app/src/main/java/tv/piik/android/WindowContent.java b/mobile/android/app/src/main/java/tv/piik/android/WindowContent.java
new file mode 100644
index 00000000..38440e47
--- /dev/null
+++ b/mobile/android/app/src/main/java/tv/piik/android/WindowContent.java
@@ -0,0 +1,45 @@
+package tv.piik.android;
+
+import android.graphics.Insets;
+import android.os.Build;
+import android.view.View;
+import android.view.Window;
+import android.view.WindowInsets;
+import android.view.WindowManager;
+import android.widget.FrameLayout;
+
+// Native consent and runtime status content stays within system and keyboard space.
+final class WindowContent {
+ static View wrap(Window window, View content) {
+ window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
+ FrameLayout frame = new FrameLayout(content.getContext());
+ frame.addView(content, new FrameLayout.LayoutParams(-1, -1));
+ frame.setOnApplyWindowInsetsListener(
+ (view, insets) -> {
+ if (Build.VERSION.SDK_INT >= 30) {
+ Insets safe =
+ insets.getInsets(
+ WindowInsets.Type.systemBars()
+ | WindowInsets.Type.displayCutout()
+ | WindowInsets.Type.ime());
+ view.setPadding(safe.left, safe.top, safe.right, safe.bottom);
+ return WindowInsets.CONSUMED;
+ }
+ view.setPadding(
+ insets.getSystemWindowInsetLeft(),
+ insets.getSystemWindowInsetTop(),
+ insets.getSystemWindowInsetRight(),
+ insets.getSystemWindowInsetBottom());
+ return insets.consumeSystemWindowInsets();
+ });
+ frame.addOnAttachStateChangeListener(
+ new View.OnAttachStateChangeListener() {
+ public void onViewAttachedToWindow(View view) {
+ view.requestApplyInsets();
+ }
+
+ public void onViewDetachedFromWindow(View view) {}
+ });
+ return frame;
+ }
+}
diff --git a/mobile/android/app/src/main/res/values-night/themes.xml b/mobile/android/app/src/main/res/values-night/themes.xml
new file mode 100644
index 00000000..414bd8aa
--- /dev/null
+++ b/mobile/android/app/src/main/res/values-night/themes.xml
@@ -0,0 +1,8 @@
+<resources>
+ <style name="PiikTheme" parent="android:style/Theme.Material">
+ <item name="android:windowLightStatusBar">false</item>
+ <item name="android:windowLightNavigationBar">false</item>
+ <item name="android:statusBarColor">?android:attr/colorBackground</item>
+ <item name="android:navigationBarColor">?android:attr/colorBackground</item>
+ </style>
+</resources>
diff --git a/mobile/android/app/src/main/res/values-zh/strings.xml b/mobile/android/app/src/main/res/values-zh/strings.xml
new file mode 100644
index 00000000..de2401d7
--- /dev/null
+++ b/mobile/android/app/src/main/res/values-zh/strings.xml
@@ -0,0 +1,19 @@
+<resources>
+ <string name="app_name">Piik</string>
+ <string name="runtime_channel">Piik 本地服务</string>
+ <string name="runtime_title">Piik 已就绪</string>
+ <string name="runtime_body">可使用本地房间与原生分享。使用完毕后可停止 Piik。</string>
+ <string name="runtime_failed">Piik 已停止</string>
+ <string name="runtime_restart">打开 Piik 查看并重试。</string>
+ <string name="runtime_open">打开</string>
+ <string name="runtime_stop">停止 Piik</string>
+ <string name="media_waiting">请在浏览器发起屏幕或声音请求。按返回键回到浏览器。</string>
+ <string name="no_pending_confirmation">当前没有待确认的屏幕或声音请求。</string>
+ <string name="return_browser">返回浏览器</string>
+ <string name="no_browser">请安装或启用浏览器以打开 Piik。</string>
+ <string name="preparing">正在准备本地屏幕分享…</string>
+ <string name="notification_channel">屏幕分享</string>
+ <string name="notification_title">Piik 正在分享屏幕</string>
+ <string name="notification_body">点击返回并停止分享。</string>
+ <string name="restart_piik">重新启动 Piik</string>
+</resources>
diff --git a/mobile/android/app/src/main/res/values/strings.xml b/mobile/android/app/src/main/res/values/strings.xml
new file mode 100644
index 00000000..38c8de46
--- /dev/null
+++ b/mobile/android/app/src/main/res/values/strings.xml
@@ -0,0 +1,19 @@
+<resources>
+ <string name="app_name">Piik</string>
+ <string name="runtime_channel">Piik companion</string>
+ <string name="runtime_title">Piik is ready</string>
+ <string name="runtime_body">Local rooms and native sharing are available. Stop Piik when finished.</string>
+ <string name="runtime_failed">Piik stopped</string>
+ <string name="runtime_restart">Open Piik to retry.</string>
+ <string name="runtime_open">Open</string>
+ <string name="runtime_stop">Stop Piik</string>
+ <string name="media_waiting">Complete the screen or audio request from your browser. Press Back to return.</string>
+ <string name="no_pending_confirmation">No screen or audio confirmation is pending.</string>
+ <string name="return_browser">Return to browser</string>
+ <string name="no_browser">Install or enable a browser to open Piik.</string>
+ <string name="preparing">Preparing local screen sharing…</string>
+ <string name="notification_channel">Screen sharing</string>
+ <string name="notification_title">Piik — sharing screen</string>
+ <string name="notification_body">Tap to return and stop sharing.</string>
+ <string name="restart_piik">Restart Piik</string>
+</resources>
diff --git a/mobile/android/app/src/main/res/values/themes.xml b/mobile/android/app/src/main/res/values/themes.xml
new file mode 100644
index 00000000..4681c1bd
--- /dev/null
+++ b/mobile/android/app/src/main/res/values/themes.xml
@@ -0,0 +1,8 @@
+<resources>
+ <style name="PiikTheme" parent="android:style/Theme.Material.Light">
+ <item name="android:windowLightStatusBar">true</item>
+ <item name="android:windowLightNavigationBar">true</item>
+ <item name="android:statusBarColor">?android:attr/colorBackground</item>
+ <item name="android:navigationBarColor">?android:attr/colorBackground</item>
+ </style>
+</resources>
diff --git a/mobile/android/build.gradle b/mobile/android/build.gradle
new file mode 100644
index 00000000..74c6c79b
--- /dev/null
+++ b/mobile/android/build.gradle
@@ -0,0 +1 @@
+plugins { id 'com.android.application' version '8.9.2' apply false }
diff --git a/mobile/android/frame_dropper.cc b/mobile/android/frame_dropper.cc
new file mode 100644
index 00000000..c8b13d3f
--- /dev/null
+++ b/mobile/android/frame_dropper.cc
@@ -0,0 +1,62 @@
+#include <jni.h>
+#include <algorithm>
+#include <cstdint>
+#include <new>
+
+#include "modules/video_coding/utility/frame_dropper.h"
+
+namespace {
+// Calls are serialized by CaptureRenderer's GL queue, including final deletion.
+struct EncoderRate {
+ webrtc::FrameDropper dropper;
+ int bitrate;
+ float framerate;
+ int64_t last_input = 0;
+
+ EncoderRate(int rate, int fps) : bitrate(rate), framerate(fps) {
+ dropper.SetRates(bitrate / 1000.0f, framerate);
+ }
+};
+
+EncoderRate* state(jlong handle) {
+ return reinterpret_cast<EncoderRate*>(handle);
+}
+} // namespace
+
+extern "C" JNIEXPORT jlong JNICALL
+Java_tv_piik_android_FrameDropper_create(JNIEnv*, jclass, jint bitrate, jint fps) {
+ return reinterpret_cast<jlong>(new (std::nothrow) EncoderRate(bitrate, fps));
+}
+
+extern "C" JNIEXPORT jboolean JNICALL
+Java_tv_piik_android_FrameDropper_shouldEncodeNative(JNIEnv*, jclass, jlong handle,
+ jlong timestamp, jboolean recovery) {
+ EncoderRate& rate = *state(handle);
+ if (!recovery && rate.last_input > 0 && timestamp > rate.last_input) {
+ // Leak against observed capture cadence, not a configured encoder ceiling.
+ rate.framerate = std::max(1.0f, 1.0e9f / (timestamp - rate.last_input));
+ }
+ rate.last_input = timestamp;
+ rate.dropper.SetRates(rate.bitrate / 1000.0f, rate.framerate);
+ rate.dropper.Leak(static_cast<uint32_t>(rate.framerate + 0.5f));
+ bool drop = rate.dropper.DropFrame();
+ return recovery || !drop;
+}
+
+extern "C" JNIEXPORT void JNICALL
+Java_tv_piik_android_FrameDropper_fill(JNIEnv*, jclass, jlong handle, jint bytes,
+ jboolean keyframe) {
+ state(handle)->dropper.Fill(bytes, !keyframe);
+}
+
+extern "C" JNIEXPORT void JNICALL
+Java_tv_piik_android_FrameDropper_setBitrate(JNIEnv*, jclass, jlong handle, jint bitrate) {
+ EncoderRate& rate = *state(handle);
+ rate.bitrate = bitrate;
+ rate.dropper.SetRates(bitrate / 1000.0f, rate.framerate);
+}
+
+extern "C" JNIEXPORT void JNICALL
+Java_tv_piik_android_FrameDropper_destroy(JNIEnv*, jclass, jlong handle) {
+ delete state(handle);
+}
diff --git a/mobile/android/settings.gradle b/mobile/android/settings.gradle
new file mode 100644
index 00000000..07358a99
--- /dev/null
+++ b/mobile/android/settings.gradle
@@ -0,0 +1,4 @@
+pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
+dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS); repositories { google(); mavenCentral() } }
+rootProject.name = 'PiikAndroid'
+include ':app'
diff --git a/mobile/bridge/capture.go b/mobile/bridge/capture.go
new file mode 100644
index 00000000..cd6a9e27
--- /dev/null
+++ b/mobile/bridge/capture.go
@@ -0,0 +1,366 @@
+// Package bridge binds mobile capture to Piik's existing native session.
+package bridge
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "io"
+ "sync"
+ "sync/atomic"
+ "time"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/nativeaudio"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativehost"
+ "github.com/TNTcraftHIM/Piik/internal/media/encoded"
+)
+
+// Platform runs Platform-owned permission and codec work on its appropriate threads.
+// Byte buffers are copied before returning from a platform callback.
+type Platform interface {
+ StartVideo(id int64, configuration string)
+ PreparationDone(id int64)
+ StartAudio(id int64)
+ StartMicrophone(id int64, deviceID string)
+ Microphones() (string, error)
+ Control(id int64, command string, layer int64, value int64)
+ Stop(id int64)
+ SiteAccessChanged()
+ RuntimeStopped(reason string)
+}
+
+type capture struct {
+ platform Platform
+ multiOutput bool
+ encoders []encoderCapability
+ mu sync.Mutex
+ next int64
+ streams map[int64]*captureStream
+}
+
+type captureStream struct {
+ owner *capture
+ id int64
+ ctx context.Context
+ cancel context.CancelCauseFunc
+ frames chan nativecapture.Frame
+ outputs []nativecapture.OutputProfile
+ once sync.Once
+ format atomic.Uint64
+ ready chan struct{}
+ readyOnce sync.Once
+ codec atomic.Uint32
+ requested string
+ videoMu sync.Mutex
+ inputPTS time.Duration
+ hasInput bool
+ firstInputPTS time.Duration
+ inputReady bool
+}
+
+func (c *capture) stream(ctx context.Context, outputs []nativecapture.OutputProfile) *captureStream {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.next++
+ ctx, cancel := context.WithCancelCause(ctx)
+ s := &captureStream{owner: c, id: c.next, ctx: ctx, cancel: cancel, frames: make(chan nativecapture.Frame, 8), outputs: outputs, ready: make(chan struct{})}
+ c.streams[s.id] = s
+ go func() { <-ctx.Done(); _ = s.Close() }()
+ return s
+}
+
+func (c *capture) Video(ctx context.Context, options nativecapture.VideoOptions) (nativehost.CaptureStream, error) {
+ available := false
+ for _, encoder := range c.encoders {
+ if options.Codec == "auto" || options.Codec == encoder.Codec {
+ available = true
+ break
+ }
+ }
+ if !available {
+ return nil, errors.New("requested mobile encoder is unavailable")
+ }
+ output := nativecapture.OutputProfile{Width: options.Profile.Width, Height: options.Profile.Height, Framerate: options.Profile.Framerate, Bitrate: options.Profile.Bitrate}
+ outputs := []nativecapture.OutputProfile{output}
+ if c.multiOutput {
+ outputs = nativecapture.ScreenShareOutputs(options.Profile)
+ for range options.OutputGroups {
+ outputs = append(outputs, outputs[0])
+ }
+ }
+ s := c.stream(ctx, outputs)
+ s.requested = options.Codec
+ configuration, _ := json.Marshal(struct {
+ nativecapture.OutputProfile
+ Codec string `json:"codec"`
+ Outputs []nativecapture.OutputProfile `json:"outputs,omitempty"`
+ }{output, options.Codec, outputs})
+ c.platform.StartVideo(s.id, string(configuration))
+ return s, nil
+}
+
+func (c *capture) Audio(ctx context.Context, _ nativecapture.CaptureTarget, excluded *nativecapture.CaptureTarget) (nativehost.CaptureStream, error) {
+ if excluded != nil {
+ return nil, errors.New("mobile source exclusion is unavailable")
+ }
+ s := c.stream(ctx, nil)
+ c.platform.StartAudio(s.id)
+ select {
+ case <-s.ctx.Done():
+ return nil, errors.New("mobile playback audio is unavailable")
+ case <-s.ready:
+ return s, nil
+ }
+}
+func (c *capture) Microphone(ctx context.Context, device string) (nativehost.CaptureStream, error) {
+ s := c.stream(ctx, nil)
+ c.platform.StartMicrophone(s.id, device)
+ select {
+ case <-s.ctx.Done():
+ return nil, errors.New("mobile commentary microphone is unavailable")
+ case <-s.ready:
+ return s, nil
+ }
+}
+func (c *capture) ListSources(context.Context) ([]nativecapture.CaptureTarget, error) {
+ return []nativecapture.CaptureTarget{{Kind: "picker", SourceID: "1", Title: "Screen"}}, nil
+}
+func (c *capture) PreviewSource(context.Context, nativecapture.CaptureTarget) ([]byte, error) {
+ return nil, errors.New("mobile screen preview is unavailable before system consent")
+}
+func (c *capture) ListMicrophones(context.Context) ([]nativecapture.Microphone, error) {
+ payload, err := c.platform.Microphones()
+ if err != nil {
+ return nil, err
+ }
+ var devices []nativecapture.Microphone
+ if json.Unmarshal([]byte(payload), &devices) != nil || devices == nil || len(devices) > 64 {
+ return nil, errors.New("mobile microphone list is invalid")
+ }
+ for _, device := range devices {
+ if device.ID == "" || !nativecapture.ValidDeviceID(device.ID) || device.Label == "" || len(device.Label) > 512 {
+ return nil, errors.New("mobile microphone identity is invalid")
+ }
+ }
+ return devices, nil
+}
+
+func (s *captureStream) Read() (nativecapture.Frame, error) {
+ select {
+ case <-s.ctx.Done():
+ if errors.Is(context.Cause(s.ctx), nativecapture.ErrSourceCanceled) {
+ return nativecapture.Frame{}, nativecapture.ErrSourceCanceled
+ }
+ return nativecapture.Frame{}, io.EOF
+ case frame := <-s.frames:
+ return frame, nil
+ }
+}
+func (s *captureStream) Outputs() []nativecapture.OutputProfile {
+ return append([]nativecapture.OutputProfile(nil), s.outputs...)
+}
+func (s *captureStream) PreparationDone() {
+ s.owner.platform.PreparationDone(s.id)
+}
+
+func (s *captureStream) Close() error {
+ s.once.Do(func() {
+ s.cancel(nil)
+ s.owner.mu.Lock()
+ delete(s.owner.streams, s.id)
+ s.owner.mu.Unlock()
+ s.owner.platform.Stop(s.id)
+ })
+ return nil
+}
+func (s *captureStream) RequestKeyFrame(layer int) error {
+ s.owner.platform.Control(s.id, "keyframe", int64(layer), 0)
+ return nil
+}
+func (s *captureStream) SetOutputActive(layer int, active bool) error {
+ value := int64(0)
+ if active {
+ value = 1
+ }
+ s.owner.platform.Control(s.id, "active", int64(layer), value)
+ return nil
+}
+func (s *captureStream) SetOutputBitrate(layer int, bitrate uint32) error {
+ s.owner.platform.Control(s.id, "bitrate", int64(layer), int64(bitrate))
+ return nil
+}
+
+func (c *capture) get(id int64) *captureStream {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.streams[id]
+}
+
+func (r *Runtime) CaptureCurrent(id int64) bool { return r.capture.get(id) != nil }
+func (s *captureStream) push(frame nativecapture.Frame) bool {
+ select {
+ case <-s.ctx.Done():
+ return false
+ case s.frames <- frame:
+ return true
+ }
+}
+
+// VideoReady publishes actual encoder dimensions and profile, not requested capability.
+func (r *Runtime) VideoReady(id int64, codec string, width, height int64, profile string) bool {
+ s := r.capture.get(id)
+ if s == nil || len(s.outputs) == 0 {
+ return false
+ }
+ if (codec != "h264" && codec != "vp8") || (s.requested != "auto" && s.requested != codec) {
+ s.Close()
+ return false
+ }
+ kind := uint32(1)
+ if codec == "vp8" {
+ kind = 2
+ }
+ if previous := s.codec.Load(); previous != 0 && previous != kind {
+ s.Close()
+ return false
+ }
+ // An incompatible prepared encoder has not committed any state. The platform
+ // can try another candidate or restore the existing source on this stream.
+ if width < 2 || height < 2 || width > int64(s.outputs[min(1, len(s.outputs)-1)].Width) || height > int64(s.outputs[min(1, len(s.outputs)-1)].Height) || !nativecapture.ValidVideoSize(uint32(width), uint32(height)) || (codec == "h264" && !nativecapture.SupportedH264ProfileLevelID(profile)) || (codec == "vp8" && profile != "") {
+ return false
+ }
+ s.codec.Store(kind)
+ s.format.Store(uint64(width)<<32 | uint64(height))
+ state := nativehost.CaptureState{State: "active", HardwareOnly: codec == "h264", Codec: codec, Width: uint32(width), Height: uint32(height), FPS: s.outputs[min(1, len(s.outputs)-1)].Framerate, ProfileLevelID: profile, Outputs: s.outputs}
+ data, _ := json.Marshal(state)
+ s.videoMu.Lock()
+ defer s.videoMu.Unlock()
+ if !s.push(nativecapture.Frame{Kind: nativecapture.FrameStatus, Data: data}) {
+ return false
+ }
+ if !s.inputReady {
+ s.inputReady = true
+ if s.hasInput {
+ if !s.push(nativecapture.Frame{Kind: nativecapture.FrameBegin, Timestamp: s.firstInputPTS}) {
+ return false
+ }
+ if s.inputPTS > s.firstInputPTS {
+ return s.push(nativecapture.Frame{Kind: nativecapture.FrameBegin, Timestamp: s.inputPTS})
+ }
+ }
+ }
+ return true
+}
+
+// Video accepts a complete codec access unit; AVC recovery includes SPS/PPS.
+func (r *Runtime) Video(id int64, data []byte, ptsMicros, durationMicros int64, key bool) {
+ s := r.capture.get(id)
+ if s == nil {
+ return
+ }
+ format := s.format.Load()
+ if format == 0 || len(data) == 0 || len(data) > encoded.MaxAccessUnitBytes || ptsMicros < 0 || durationMicros <= 0 {
+ s.Close()
+ return
+ }
+ r.VideoInput(id, ptsMicros)
+ r.VideoLayer(id, int64(min(1, len(s.outputs)-1)), data, ptsMicros, durationMicros, int64(format>>32), int64(uint32(format)), key)
+}
+
+// VideoLayerReady admits optional encoders without replacing capture authority.
+func (r *Runtime) VideoLayerReady(id, layer int64, codec string, width, height int64, profile string) bool {
+ s := r.capture.get(id)
+ if s == nil || layer < 0 || layer >= int64(len(s.outputs)) || s.format.Load() == 0 {
+ return false
+ }
+ if (s.codec.Load() == 1 && codec != "h264") || (s.codec.Load() == 2 && codec != "vp8") {
+ return false
+ }
+ output := s.outputs[layer]
+ return width >= 2 && height >= 2 && width <= int64(output.Width) && height <= int64(output.Height) && nativecapture.ValidVideoSize(uint32(width), uint32(height)) && ((codec == "h264" && nativecapture.SupportedH264ProfileLevelID(profile)) || (codec == "vp8" && profile == ""))
+}
+
+// VideoInput advances the single capture-image clock before encoder submission.
+func (r *Runtime) VideoInput(id, ptsMicros int64) {
+ s := r.capture.get(id)
+ if s == nil || ptsMicros < 0 {
+ return
+ }
+ s.videoMu.Lock()
+ defer s.videoMu.Unlock()
+ pts := time.Duration(ptsMicros) * time.Microsecond
+ if s.hasInput && pts <= s.inputPTS {
+ return
+ }
+ if !s.hasInput {
+ s.firstInputPTS = pts
+ }
+ s.hasInput, s.inputPTS = true, pts
+ if s.inputReady {
+ s.push(nativecapture.Frame{Kind: nativecapture.FrameBegin, Timestamp: pts})
+ }
+}
+
+// VideoLayer preserves one capture timeline across independently completing encoders.
+func (r *Runtime) VideoLayer(id, layer int64, data []byte, ptsMicros, durationMicros, width, height int64, key bool) {
+ s := r.capture.get(id)
+ if s == nil {
+ return
+ }
+ if layer < 0 || layer >= int64(len(s.outputs)) || s.format.Load() == 0 || len(data) == 0 || len(data) > encoded.MaxAccessUnitBytes || ptsMicros < 0 || durationMicros <= 0 || width < 2 || height < 2 || width > int64(s.outputs[layer].Width) || height > int64(s.outputs[layer].Height) || !nativecapture.ValidVideoSize(uint32(width), uint32(height)) {
+ s.Close()
+ return
+ }
+ s.videoMu.Lock()
+ defer s.videoMu.Unlock()
+ pts := time.Duration(ptsMicros) * time.Microsecond
+ kind := nativecapture.FrameH264
+ if s.codec.Load() == 2 {
+ kind = nativecapture.FrameVP8
+ }
+ s.push(nativecapture.Frame{Kind: kind, Layer: int(layer), Width: uint32(width), Height: uint32(height), Data: append([]byte(nil), data...), Timestamp: pts, Duration: time.Duration(durationMicros) * time.Microsecond, KeyFrame: key})
+}
+
+// VideoLayerUnavailable retires optional output demand through the shared planner.
+func (r *Runtime) VideoLayerUnavailable(id, layer int64) {
+ s := r.capture.get(id)
+ if s == nil || layer < 0 || layer >= int64(len(s.outputs)) {
+ return
+ }
+ if layer == int64(min(1, len(s.outputs)-1)) {
+ s.Close()
+ return
+ }
+ s.push(nativecapture.Frame{Kind: nativecapture.FrameLayerUnavailable, Layer: int(layer)})
+}
+
+func (r *Runtime) Audio(id int64, data []byte) {
+ s := r.capture.get(id)
+ if s != nil {
+ if len(data) != nativeaudio.FrameBytes {
+ s.Close()
+ return
+ }
+ s.push(nativecapture.Frame{Kind: nativecapture.FramePCM, Data: append([]byte(nil), data...), Duration: 20 * time.Millisecond})
+ }
+}
+func (r *Runtime) AudioReady(id int64) {
+ if s := r.capture.get(id); s != nil {
+ s.readyOnce.Do(func() { close(s.ready) })
+ }
+}
+
+// CaptureCanceled is only for an explicit dismissal of system source selection.
+func (r *Runtime) CaptureCanceled(id int64) {
+ if s := r.capture.get(id); s != nil {
+ s.cancel(nativecapture.ErrSourceCanceled)
+ _ = s.Close()
+ }
+}
+
+func (r *Runtime) CaptureEnded(id int64) {
+ if s := r.capture.get(id); s != nil {
+ _ = s.Close()
+ }
+}
diff --git a/mobile/bridge/capture_test.go b/mobile/bridge/capture_test.go
new file mode 100644
index 00000000..35079ea1
--- /dev/null
+++ b/mobile/bridge/capture_test.go
@@ -0,0 +1,847 @@
+package bridge
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "slices"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/loopback"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativeaudio"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecontrol"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativehost"
+ "github.com/livekit/livekit-server/pkg/sfu"
+)
+
+type testPlatform struct {
+ mu sync.Mutex
+ stopped []int64
+ started chan int64
+ siteChanged func()
+ videoStarted func(int64, string)
+ preparationDone func(int64)
+}
+
+type bitratePlatform struct {
+ *testPlatform
+ bitrates chan int64
+}
+
+func (p *bitratePlatform) Control(_ int64, command string, _ int64, value int64) {
+ if command == "bitrate" {
+ p.bitrates <- value
+ }
+}
+
+func TestProfileReplacementPreservesMobileCaptureTimeline(t *testing.T) {
+ p := &bitratePlatform{testPlatform: &testPlatform{}, bitrates: make(chan int64, 8)}
+ c := &capture{platform: p, encoders: []encoderCapability{{Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ configurations := make(chan int64, 2)
+ p.videoStarted = func(id int64, configuration string) {
+ var output nativecapture.OutputProfile
+ if err := json.Unmarshal([]byte(configuration), &output); err != nil {
+ t.Error(err)
+ return
+ }
+ configurations <- id
+ r.VideoReady(id, "vp8", int64(output.Width), int64(output.Height), "")
+ }
+ video := nativecapture.VideoOptions{Codec: "vp8", Target: nativecapture.CaptureTarget{Kind: "picker"},
+ Profile: nativecapture.VideoProfile{Width: 1920, Height: 1080, Framerate: 30, Bitrate: 5_000_000, Preference: "balanced"}}
+ profile := nativehost.QualityProfile{Video: video.Profile, AudioBitrate: 64_000}
+ session, err := nativehost.Start(t.Context(), nativehost.Options{ShareID: "profile-clock", Capture: c,
+ Video: video, Profile: profile, EdgeCapacity: 2, BindAddress: "127.0.0.1:0"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer session.Close()
+ awaitBitrates := func(want int64) {
+ t.Helper()
+ for range 3 {
+ select {
+ case value := <-p.bitrates:
+ if value != want {
+ t.Fatalf("codec bitrate = %d, want %d", value, want)
+ }
+ case err := <-session.Done():
+ t.Fatalf("profile replacement ended sharing: %v", err)
+ case <-time.After(2 * time.Second):
+ t.Fatal("capture did not process input")
+ }
+ }
+ }
+ previous := <-configurations
+ // Capture PTS remains correct when the initial frame takes longer to encode
+ // than subsequent frames, projecting the first-arrival anchor ahead of now.
+ for _, pts := range []int64{0, 500_000, 533_333} {
+ r.Video(previous, sfu.VP8KeyFrame8x8, pts, 33_333, true)
+ }
+ awaitBitrates(5_000_000)
+ profile.Video.Bitrate = 100_000
+ if err = session.UpdateProfile(profile); err != nil {
+ t.Fatal(err)
+ }
+ current := <-configurations
+ if r.CaptureCurrent(previous) || current == previous {
+ t.Fatal("profile replacement retained the old capture")
+ }
+ for _, pts := range []int64{0, 33_333, 66_666} {
+ r.Video(current, sfu.VP8KeyFrame8x8, pts, 33_333, true)
+ }
+ awaitBitrates(100_000)
+}
+
+func TestVideoOnlyPlatformDoesNotAdvertisePlaybackAudio(t *testing.T) {
+ catalog, err := decodeEncoderCapabilities(`{"platform":"ios","provider":"VideoToolbox","encoders":[{"name":"hardware-avc","codec":"h264"}]}`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ native, media := catalog.nativeCapabilities(), catalog.mediaCapabilities()
+ if native.Platform != "ios" || native.Adapters[0].Name != "VideoToolbox" || !media.Video || !media.HardwareH264 {
+ t.Fatalf("video producer was not preserved: native=%+v media=%+v", native, media)
+ }
+ if native.SystemAudio || media.SystemAudio || media.ShareVolume {
+ t.Fatalf("video support invented playback audio: native=%+v media=%+v", native, media)
+ }
+}
+
+func (p *testPlatform) StartVideo(id int64, configuration string) {
+ if p.videoStarted != nil {
+ p.videoStarted(id, configuration)
+ }
+}
+func (*testPlatform) RuntimeStopped(string) {}
+func (p *testPlatform) PreparationDone(id int64) {
+ if p.preparationDone != nil {
+ p.preparationDone(id)
+ }
+}
+func (p *testPlatform) StartMicrophone(id int64, _ string) { p.StartAudio(id) }
+func (*testPlatform) Microphones() (string, error) { return "[]", nil }
+
+func TestVideoPreparationSpansSourceAudioAndCaptureCommit(t *testing.T) {
+ for _, outcome := range []string{"ready", "denied", "cancelled", "video-ended", "video-only", "mixed", "replace"} {
+ t.Run(outcome, func(t *testing.T) {
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ completed := make(chan int64, 4)
+ p := &testPlatform{started: make(chan int64, 4), preparationDone: func(id int64) { completed <- id }}
+ c := &capture{platform: p, encoders: []encoderCapability{{Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ p.videoStarted = func(id int64, configuration string) {
+ var output nativecapture.OutputProfile
+ if err := json.Unmarshal([]byte(configuration), &output); err != nil {
+ t.Error(err)
+ return
+ }
+ r.VideoReady(id, "vp8", int64(output.Width), int64(output.Height), "")
+ }
+ video := nativecapture.VideoOptions{Codec: "vp8", Target: nativecapture.CaptureTarget{Kind: "picker"},
+ Profile: nativecapture.VideoProfile{Width: 640, Height: 360, Framerate: 30, Bitrate: 1_000_000, Preference: "balanced"}}
+ var session *nativehost.Session
+ result := make(chan error, 1)
+ go func() {
+ var err error
+ session, err = nativehost.Start(ctx, nativehost.Options{ShareID: "preparation-fixture", Capture: c, Video: video,
+ Profile: nativehost.QualityProfile{Video: video.Profile, AudioBitrate: 64_000}, AudioEnabled: outcome != "video-only" && outcome != "replace",
+ MicrophoneMixing: outcome == "replace" || outcome == "mixed",
+ EdgeCapacity: 1, BindAddress: "127.0.0.1:0"})
+ result <- err
+ }()
+ awaitResult := func() error {
+ t.Helper()
+ select {
+ case err := <-result:
+ return err
+ case <-time.After(3 * time.Second):
+ t.Fatal("capture operation did not finish")
+ return nil
+ }
+ }
+ if outcome == "replace" {
+ if err := awaitResult(); err != nil {
+ t.Fatal(err)
+ }
+ if id := <-completed; id != 1 {
+ t.Fatal("initial preparation identity", id)
+ }
+ go func() { result <- session.ReplaceSource(video, true, nil) }()
+ }
+ var audioID int64
+ if outcome != "video-only" {
+ select {
+ case audioID = <-p.started:
+ case <-time.After(3 * time.Second):
+ t.Fatal("audio admission was not requested")
+ }
+ select {
+ case <-completed:
+ t.Fatal("video released preparation before source audio admission")
+ default:
+ }
+ if outcome == "mixed" {
+ if err := awaitResult(); err != nil {
+ t.Fatal(err)
+ }
+ if !session.HasAudio() || session.SourceAudio() == nil || *session.SourceAudio() {
+ t.Fatal("pending source input changed the mixed output shape or claimed readiness")
+ }
+ }
+ switch outcome {
+ case "cancelled":
+ cancel()
+ case "denied":
+ r.CaptureEnded(audioID)
+ case "video-ended":
+ r.CaptureEnded(1)
+ default:
+ r.AudioReady(audioID)
+ }
+ }
+ var err error
+ if outcome != "mixed" {
+ err = awaitResult()
+ }
+ if (err != nil) != (outcome == "cancelled" || outcome == "video-ended") {
+ t.Fatalf("operation result: %v", err)
+ }
+ if outcome == "video-ended" && (session != nil || !strings.Contains(err.Error(), "capture stopped unexpectedly")) {
+ t.Fatalf("ended video was not a failed startup: session=%v err=%v", session != nil, err)
+ }
+ if outcome == "ready" || outcome == "replace" {
+ if audio := c.get(audioID); audio == nil || audio.ctx.Err() != nil || !session.HasAudio() {
+ t.Fatal("successful startup retired live source audio")
+ }
+ }
+ if outcome == "replace" && (r.CaptureCurrent(1) || !r.CaptureCurrent(3)) {
+ t.Fatal("old source retirement affected the committed replacement")
+ }
+ select {
+ case id := <-completed:
+ want := int64(1)
+ if outcome == "replace" {
+ want = 3
+ }
+ if id != want {
+ t.Fatalf("preparation identity %d, want %d", id, want)
+ }
+ case <-time.After(3 * time.Second):
+ t.Fatal("aggregate preparation was not released")
+ }
+ if outcome == "mixed" && !*session.SourceAudio() {
+ t.Fatal("completed preparation did not attach the source input")
+ }
+ if session != nil {
+ _ = session.Close()
+ }
+ })
+ }
+}
+
+func TestSourceReplacementSharesPlatformHandlerWithAudioAdmission(t *testing.T) {
+ for _, outcome := range []string{"ready", "cancelled", "failed"} {
+ t.Run(outcome, func(t *testing.T) {
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ handler := make(chan func(), 16)
+ handlerDone := make(chan struct{})
+ go func() {
+ defer close(handlerDone)
+ for callback := range handler {
+ callback()
+ }
+ }()
+ p := &testPlatform{started: make(chan int64, 1)}
+ c := &capture{platform: p, encoders: []encoderCapability{{Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ var replacementID int64
+ p.videoStarted = func(id int64, configuration string) {
+ handler <- func() {
+ if id != 1 {
+ replacementID = id
+ if outcome == "cancelled" {
+ r.CaptureCanceled(id)
+ return
+ }
+ if outcome == "failed" {
+ r.CaptureEnded(id)
+ return
+ }
+ }
+ var output nativecapture.OutputProfile
+ if err := json.Unmarshal([]byte(configuration), &output); err != nil {
+ t.Error(err)
+ return
+ }
+ r.VideoReady(id, "vp8", int64(output.Width), int64(output.Height), "")
+ if id != 1 {
+ // MediaCodec callbacks and audio admission share one handler.
+ // An unread candidate fills the bridge queue before it yields.
+ for frame := int64(0); frame < 10; frame++ {
+ r.Video(id, []byte{0x10, 0, 0}, frame*33333, 33333, true)
+ }
+ }
+ }
+ }
+ video := nativecapture.VideoOptions{Codec: "vp8", Target: nativecapture.CaptureTarget{Kind: "picker"},
+ Profile: nativecapture.VideoProfile{Width: 640, Height: 360, Framerate: 30, Bitrate: 1_000_000, Preference: "balanced"}}
+ session, err := nativehost.Start(ctx, nativehost.Options{ShareID: "replacement-fixture", Capture: c, Video: video,
+ Profile: nativehost.QualityProfile{Video: video.Profile, AudioBitrate: 64_000}, MicrophoneMixing: true,
+ EdgeCapacity: 1, BindAddress: "127.0.0.1:0"})
+ if err != nil {
+ close(handler)
+ <-handlerDone
+ t.Fatal(err)
+ }
+ defer func() {
+ cancel()
+ _ = session.Close()
+ close(handler)
+ <-handlerDone
+ }()
+ result := make(chan error, 1)
+ go func() { result <- session.ReplaceSource(video, true, nil) }()
+ var audioID int64
+ select {
+ case audioID = <-p.started:
+ handler <- func() { r.AudioReady(audioID) }
+ case <-time.After(2 * time.Second):
+ t.Fatal("replacement audio admission was not requested")
+ }
+ select {
+ case err = <-result:
+ case <-time.After(2 * time.Second):
+ t.Fatal("replacement video blocked the handler needed for audio admission")
+ }
+ sourceAudio := session.SourceAudio()
+ if sourceAudio == nil {
+ t.Fatal("mixed source audio state is unavailable")
+ }
+ if outcome == "ready" {
+ if err != nil || !*sourceAudio || r.CaptureCurrent(1) || !r.CaptureCurrent(replacementID) {
+ t.Fatalf("replacement did not commit picture and sound: %v", err)
+ }
+ } else {
+ if err == nil || r.CaptureCurrent(audioID) || r.CaptureCurrent(replacementID) || !r.CaptureCurrent(1) || *sourceAudio {
+ t.Fatalf("failed preparation retained a candidate or retired the current picture: %v", err)
+ }
+ if outcome == "cancelled" && !errors.Is(err, nativecapture.ErrSourceCanceled) {
+ t.Fatalf("source cancellation lost its classification: %v", err)
+ }
+ }
+ })
+ }
+}
+
+func TestCommentaryWaitsForPlatformAdmissionAndRetiresIndependently(t *testing.T) {
+ for _, admitted := range []bool{false, true} {
+ t.Run(fmt.Sprint(admitted), func(t *testing.T) {
+ platform := &testPlatform{started: make(chan int64, 1)}
+ capture := &capture{platform: platform, streams: make(map[int64]*captureStream)}
+ runtime := &Runtime{capture: capture}
+ source := capture.stream(t.Context(), nil)
+ defer source.Close()
+ type result struct {
+ stream nativehost.CaptureStream
+ err error
+ }
+ done := make(chan result, 1)
+ go func() { stream, err := capture.Microphone(t.Context(), ""); done <- result{stream, err} }()
+ id := <-platform.started
+ select {
+ case <-done:
+ t.Fatal("microphone reported success before platform admission")
+ default:
+ }
+ if admitted {
+ runtime.AudioReady(id)
+ } else {
+ runtime.CaptureEnded(id)
+ }
+ var observed result
+ select {
+ case observed = <-done:
+ case <-time.After(time.Second):
+ t.Fatal("microphone admission remained blocked")
+ }
+ if admitted {
+ if observed.err != nil || observed.stream == nil {
+ t.Fatalf("admitted microphone failed: %v", observed.err)
+ }
+ pcm := make([]byte, nativeaudio.FrameBytes)
+ pcm[0], pcm[2] = 13, 13
+ runtime.Audio(id, pcm)
+ frame, err := observed.stream.Read()
+ if err != nil || frame.Kind != nativecapture.FramePCM || frame.Duration != 20*time.Millisecond || !bytes.Equal(frame.Data, pcm) {
+ t.Fatal("microphone did not use the existing stereo PCM contract")
+ }
+ observed.stream.Close()
+ } else if observed.err == nil || observed.stream != nil {
+ t.Fatal("rejected microphone was reported ready")
+ }
+ if !runtime.CaptureCurrent(source.id) || runtime.CaptureCurrent(id) {
+ t.Fatal("microphone retirement affected source audio or retained failed input")
+ }
+ })
+ }
+}
+func (p *testPlatform) SiteAccessChanged() {
+ if p.siteChanged != nil {
+ p.siteChanged()
+ }
+}
+func (p *testPlatform) StartAudio(id int64) {
+ if p.started != nil {
+ p.started <- id
+ }
+}
+func (*testPlatform) Control(int64, string, int64, int64) {}
+func (p *testPlatform) Stop(id int64) { p.mu.Lock(); p.stopped = append(p.stopped, id); p.mu.Unlock() }
+
+func TestRetiredCaptureUnblocksProducerAndCannotReachReplacement(t *testing.T) {
+ p := &testPlatform{}
+ c := &capture{platform: p, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ old := c.stream(context.Background(), nil)
+ for range cap(old.frames) {
+ old.push(nativecapture.Frame{Kind: nativecapture.FramePCM})
+ }
+ blocked := make(chan struct{})
+ go func() { r.Audio(old.id, make([]byte, nativeaudio.FrameBytes)); close(blocked) }()
+ old.Close()
+ old.Close()
+ select {
+ case <-blocked:
+ case <-time.After(time.Second):
+ t.Fatal("retired capture left JNI producer blocked")
+ }
+ next := c.stream(context.Background(), nil)
+ defer next.Close()
+ r.Audio(old.id, make([]byte, nativeaudio.FrameBytes))
+ if r.CaptureCurrent(old.id) || !r.CaptureCurrent(next.id) {
+ t.Fatal("wrong capture generation retained")
+ }
+ select {
+ case <-next.frames:
+ t.Fatal("old callback reached replacement")
+ default:
+ }
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ if len(p.stopped) != 1 || p.stopped[0] != old.id {
+ t.Fatal("capture retirement was not once-only")
+ }
+}
+
+func TestAudioProviderCommitsOnlyAfterPlatformStarted(t *testing.T) {
+ p := &testPlatform{started: make(chan int64, 1)}
+ c := &capture{platform: p, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ for _, success := range []bool{true, false} {
+ done := make(chan error, 1)
+ go func() {
+ stream, err := c.Audio(context.Background(), nativecapture.CaptureTarget{}, nil)
+ if stream != nil {
+ stream.Close()
+ }
+ done <- err
+ }()
+ id := <-p.started
+ select {
+ case <-done:
+ t.Fatal("audio returned before platform start")
+ default:
+ }
+ if success {
+ r.AudioReady(id)
+ } else {
+ r.CaptureEnded(id)
+ }
+ select {
+ case err := <-done:
+ if (err == nil) != success {
+ t.Fatalf("audio success=%v err=%v", success, err)
+ }
+ case <-time.After(time.Second):
+ t.Fatal("audio startup did not settle")
+ }
+ r.AudioReady(id)
+ if r.CaptureCurrent(id) {
+ t.Fatal("late ready revived retired audio")
+ }
+ }
+}
+
+func TestEncoderCatalogPreservesBothExplicitPaths(t *testing.T) {
+ catalog, err := decodeEncoderCapabilities(`{"platform":"android","provider":"Android MediaCodec","systemAudio":true,"encoders":[{"name":"hardware-avc","codec":"h264"},{"name":"platform-vp8","codec":"vp8"}]}`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ capabilities := catalog.nativeCapabilities()
+ health := catalog.mediaCapabilities()
+ if !health.HardwareH264 || !health.SoftwareVP8 || !capabilities.SoftwareVP8 || len(capabilities.Adapters[0].HardwareH264) != 1 {
+ t.Fatal("catalog hid an available codec")
+ }
+ if health.MaxVideoResolution.Width != nativecapture.MaxVideoDimension || health.MaxVideoResolution.Height != nativecapture.MaxVideoDimension ||
+ health.MaxVideoPixels != nativecapture.MaxVideoPixels || health.MaxVideoMacroblocksPerSecond != nativecapture.MaxVideoMacroblocksPerSecond ||
+ health.MaxVideoFramerate != nativecapture.MaxVideoFramerate || health.MaxVideoBitrate != nativecapture.MaxVideoBitrate || health.MaxAudioBitrate != nativeaudio.MaxSendBitrate {
+ t.Fatal("catalog replaced the shared implementation bounds with an encoder-specific envelope")
+ }
+ for _, requested := range []string{"h264", "vp8", "auto"} {
+ var configured string
+ p := &testPlatform{videoStarted: func(_ int64, configuration string) {
+ var options struct{ Codec string }
+ if err := json.Unmarshal([]byte(configuration), &options); err != nil {
+ t.Fatal(err)
+ }
+ configured = options.Codec
+ }}
+ c := &capture{platform: p, encoders: catalog.Encoders, streams: make(map[int64]*captureStream)}
+ stream, err := c.Video(context.Background(), nativecapture.VideoOptions{Codec: requested, Profile: nativecapture.VideoProfile{Width: 640, Height: 360, Framerate: 30, Bitrate: 1000000}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if configured != requested {
+ t.Fatal("explicit intent or auto preparation was replaced before actual encoding")
+ }
+ stream.Close()
+ }
+}
+
+func TestActualCodecCommitsStatusAndEncodedFrameTogether(t *testing.T) {
+ for _, actual := range []string{"h264", "vp8"} {
+ t.Run(actual, func(t *testing.T) {
+ c := &capture{platform: &testPlatform{}, encoders: []encoderCapability{{Codec: "h264"}, {Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ raw, err := c.Video(context.Background(), nativecapture.VideoOptions{Codec: "auto", Profile: nativecapture.VideoProfile{Width: 640, Height: 360, Framerate: 30, Bitrate: 1000000}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer raw.Close()
+ s := raw.(*captureStream)
+ r := &Runtime{capture: c}
+ profile := ""
+ kind := byte(nativecapture.FrameVP8)
+ if actual == "h264" {
+ profile = "42e01f"
+ kind = byte(nativecapture.FrameH264)
+ }
+ r.VideoReady(s.id, actual, 640, 360, profile)
+ frame, err := s.Read()
+ if err != nil {
+ t.Fatal(err)
+ }
+ var state struct{ Codec string }
+ if err = json.Unmarshal(frame.Data, &state); err != nil {
+ t.Fatal(err)
+ }
+ if frame.Kind != nativecapture.FrameStatus || state.Codec != actual {
+ t.Fatal("actual codec absent from active status")
+ }
+ r.Video(s.id, []byte{1, 2, 3}, 0, 33333, true)
+ _, _ = s.Read()
+ frame, err = s.Read()
+ if err != nil || byte(frame.Kind) != kind {
+ t.Fatal("encoded bytes mislabeled after auto fallback")
+ }
+ })
+ }
+}
+
+func TestExplicitCodecCannotCommitAnotherCodec(t *testing.T) {
+ c := &capture{platform: &testPlatform{}, encoders: []encoderCapability{{Codec: "h264"}, {Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ raw, err := c.Video(context.Background(), nativecapture.VideoOptions{Codec: "h264", Profile: nativecapture.VideoProfile{Width: 640, Height: 360, Framerate: 30, Bitrate: 1000000}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer raw.Close()
+ s := raw.(*captureStream)
+ r := &Runtime{capture: c}
+ r.VideoReady(s.id, "vp8", 640, 360, "")
+ if r.CaptureCurrent(s.id) {
+ t.Fatal("explicit AVC silently changed codec")
+ }
+}
+
+func TestCommittedCodecStaysLockedWhileRotationCanUpdateDimensions(t *testing.T) {
+ c := &capture{platform: &testPlatform{}, encoders: []encoderCapability{{Codec: "h264"}, {Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ raw, err := c.Video(context.Background(), nativecapture.VideoOptions{Codec: "auto", Profile: nativecapture.VideoProfile{Width: 640, Height: 640, Framerate: 30, Bitrate: 1000000}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer raw.Close()
+ s := raw.(*captureStream)
+ r := &Runtime{capture: c}
+ r.VideoReady(s.id, "vp8", 640, 360, "")
+ _, _ = s.Read()
+ r.VideoReady(s.id, "vp8", 360, 640, "")
+ frame, err := s.Read()
+ if err != nil || frame.Kind != nativecapture.FrameStatus || s.format.Load() != uint64(360)<<32|640 {
+ t.Fatal("same-codec rotation did not publish actual dimensions")
+ }
+ r.VideoReady(s.id, "h264", 360, 640, "42e01f")
+ if r.CaptureCurrent(s.id) {
+ t.Fatal("committed auto stream silently changed codec")
+ }
+}
+
+func TestIncompatibleAVCPreparationCanFallBackBeforeCommit(t *testing.T) {
+ c := &capture{platform: &testPlatform{}, encoders: []encoderCapability{{Codec: "h264"}, {Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ raw, err := c.Video(context.Background(), nativecapture.VideoOptions{Codec: "auto", Profile: nativecapture.VideoProfile{Width: 640, Height: 360, Framerate: 30, Bitrate: 1000000}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer raw.Close()
+ s := raw.(*captureStream)
+ r := &Runtime{capture: c}
+ if r.VideoReady(s.id, "h264", 640, 360, "640028") {
+ t.Fatal("unsupported actual AVC profile was accepted")
+ }
+ if !r.CaptureCurrent(s.id) || s.codec.Load() != 0 || s.format.Load() != 0 || len(s.frames) != 0 {
+ t.Fatal("rejected preparation changed or retired the uncommitted stream")
+ }
+ if !r.VideoReady(s.id, "vp8", 640, 360, "") {
+ t.Fatal("auto fallback could not prepare VP8")
+ }
+ frame, err := s.Read()
+ if err != nil || frame.Kind != nativecapture.FrameStatus {
+ t.Fatal("fallback status missing")
+ }
+ r.Video(s.id, []byte{1, 2, 3}, 0, 33333, true)
+ _, _ = s.Read()
+ frame, err = s.Read()
+ if err != nil || frame.Kind != nativecapture.FrameVP8 {
+ t.Fatal("fallback bytes used rejected AVC format")
+ }
+}
+
+func TestRejectedRotationProfileLeavesCommittedSourceUntouched(t *testing.T) {
+ c := &capture{platform: &testPlatform{}, encoders: []encoderCapability{{Codec: "h264"}}, streams: make(map[int64]*captureStream)}
+ raw, err := c.Video(context.Background(), nativecapture.VideoOptions{Codec: "h264", Profile: nativecapture.VideoProfile{Width: 640, Height: 640, Framerate: 30, Bitrate: 1000000}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer raw.Close()
+ s := raw.(*captureStream)
+ r := &Runtime{capture: c}
+ if !r.VideoReady(s.id, "h264", 640, 360, "42e01f") {
+ t.Fatal("initial AVC not ready")
+ }
+ _, _ = s.Read()
+ oldFormat := s.format.Load()
+ if r.VideoReady(s.id, "h264", 360, 640, "640028") {
+ t.Fatal("unsupported rotated profile accepted")
+ }
+ if !r.CaptureCurrent(s.id) || s.format.Load() != oldFormat || len(s.frames) != 0 {
+ t.Fatal("failed rotation destroyed or modified healthy source")
+ }
+ r.Video(s.id, []byte{1, 2, 3}, 0, 33333, true)
+ _, _ = s.Read()
+ frame, err := s.Read()
+ if err != nil || frame.Kind != nativecapture.FrameH264 || frame.Width != 640 || frame.Height != 360 {
+ t.Fatal("restored old source cannot continue")
+ }
+ if !r.VideoReady(s.id, "h264", 360, 640, "42e01f") {
+ t.Fatal("legal same-codec rotation rejected")
+ }
+}
+
+func TestSourceCancellationRequiresTheActualRequestOptIn(t *testing.T) {
+ for _, kind := range []string{"start-share", "replace-share-source"} {
+ for _, optedIn := range []bool{false, true} {
+ t.Run(fmt.Sprint(kind, "/", optedIn), func(t *testing.T) {
+ p := &testPlatform{}
+ c := &capture{platform: p, encoders: []encoderCapability{{Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ p.videoStarted = func(id int64, configuration string) {
+ if kind == "start-share" || id > 1 {
+ r.CaptureCanceled(id)
+ return
+ }
+ var output nativecapture.OutputProfile
+ if err := json.Unmarshal([]byte(configuration), &output); err != nil {
+ t.Error(err)
+ return
+ }
+ r.VideoReady(id, "vp8", int64(output.Width), int64(output.Height), "")
+ }
+ control := nativecontrol.NewWithCapture(nativecapture.Capabilities{SoftwareVP8: true}, false, nil, c)
+ defer control.Close()
+ handle := func(payload string) map[string]any {
+ t.Helper()
+ value, err := control.Handle(t.Context(), []byte(payload))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var request struct{ ID string }
+ if err := json.Unmarshal([]byte(payload), &request); err != nil {
+ t.Fatal(err)
+ }
+ deadline := time.NewTimer(3 * time.Second)
+ defer deadline.Stop()
+ for {
+ if value == nil {
+ select {
+ case value = <-control.Events():
+ case <-deadline.C:
+ t.Fatal("control response did not complete")
+ }
+ }
+ if event, ok := value.(loopback.ControlEvent); ok {
+ if event.Current != nil && !event.Current() {
+ value = nil
+ continue
+ }
+ if event.BeforeWrite != nil {
+ event.BeforeWrite()
+ }
+ value = event.Value
+ }
+ data, err := json.Marshal(value)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var result map[string]any
+ if err = json.Unmarshal(data, &result); err != nil {
+ t.Fatal(err)
+ }
+ if result["id"] == request.ID {
+ return result
+ }
+ value = nil
+ }
+ }
+ start := `{"version":10,"id":"request_start","type":"start-share","shareId":"share_123456","source":{"kind":"picker","sourceId":"1","title":"Fixture"},"edgeCapacity":1,"codec":"vp8","profile":{"resolution":{"width":640,"height":360},"maxFramerate":30,"maxBitrate":1000000,"degradationPreference":"balanced"}}`
+ payload := start
+ if kind == "replace-share-source" {
+ if result := handle(start); result["type"] != "share-started" {
+ t.Fatalf("initial share: %v", result)
+ }
+ payload = `{"version":10,"id":"request_replace","type":"replace-share-source","shareId":"share_123456","source":{"kind":"picker","sourceId":"1","title":"Fixture"},"audio":false}`
+ }
+ if optedIn {
+ payload = strings.TrimSuffix(payload, "}") + `,"reportSourceCancellation":true}`
+ }
+ result := handle(payload)
+ want := "operation-failed"
+ if optedIn {
+ want = "source-canceled"
+ }
+ if result["type"] != "request-failed" || result["code"] != want {
+ t.Fatalf("request opt-in lost: %v", result)
+ }
+ })
+ }
+ }
+}
+
+func TestProjectionDismissalPreservesCancellationAndCurrentCapture(t *testing.T) {
+ for _, canceled := range []bool{true, false} {
+ t.Run(fmt.Sprint(canceled), func(t *testing.T) {
+ p := &testPlatform{}
+ c := &capture{platform: p, encoders: []encoderCapability{{Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ p.videoStarted = func(id int64, configuration string) {
+ if id > 1 {
+ if canceled {
+ r.CaptureCanceled(id)
+ } else {
+ r.CaptureEnded(id)
+ }
+ return
+ }
+ var output nativecapture.OutputProfile
+ if err := json.Unmarshal([]byte(configuration), &output); err != nil {
+ t.Error(err)
+ return
+ }
+ r.VideoReady(id, "vp8", int64(output.Width), int64(output.Height), "")
+ }
+ video := nativecapture.VideoOptions{Codec: "vp8", Target: nativecapture.CaptureTarget{Kind: "picker"}, Profile: nativecapture.VideoProfile{Width: 640, Height: 360, Framerate: 30, Bitrate: 1_000_000, Preference: "balanced"}}
+ session, err := nativehost.Start(t.Context(), nativehost.Options{ShareID: "projection-cancel", Capture: c, Video: video, Profile: nativehost.QualityProfile{Video: video.Profile, AudioBitrate: 64_000}, EdgeCapacity: 1, BindAddress: "127.0.0.1:0"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer session.Close()
+ err = session.ReplaceSource(video, false, nil)
+ if err == nil || errors.Is(err, nativecapture.ErrSourceCanceled) != canceled {
+ t.Fatalf("replacement classification: %v", err)
+ }
+ if !r.CaptureCurrent(1) || r.CaptureCurrent(2) {
+ t.Fatal("failed replacement retired the current capture or retained the candidate")
+ }
+ if canceled {
+ p.videoStarted = func(id int64, _ string) { r.CaptureCanceled(id) }
+ pending, err := nativehost.Start(t.Context(), nativehost.Options{ShareID: "projection-start-cancel", Capture: c, Video: video, Profile: nativehost.QualityProfile{Video: video.Profile, AudioBitrate: 64_000}, EdgeCapacity: 1, BindAddress: "127.0.0.1:0"})
+ if pending != nil || !errors.Is(err, nativecapture.ErrSourceCanceled) {
+ t.Fatalf("initial picker cancellation: %v", err)
+ }
+ }
+ })
+ }
+}
+
+func TestMultiOutputCaptureUsesSharedProfilesAndOneInputClock(t *testing.T) {
+ p := &testPlatform{}
+ var configuration struct{ Outputs []nativecapture.OutputProfile }
+ p.videoStarted = func(_ int64, value string) {
+ if err := json.Unmarshal([]byte(value), &configuration); err != nil {
+ t.Fatal(err)
+ }
+ }
+ c := &capture{platform: p, multiOutput: true, encoders: []encoderCapability{{Codec: "vp8"}}, streams: make(map[int64]*captureStream)}
+ r := &Runtime{capture: c}
+ profile := nativecapture.VideoProfile{Width: 1920, Height: 1080, Framerate: 30, Bitrate: 1_000_000, Preference: "balanced"}
+ stream, err := c.Video(t.Context(), nativecapture.VideoOptions{Codec: "vp8", Profile: profile, OutputGroups: 2})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer stream.Close()
+ s := stream.(*captureStream)
+ want := nativecapture.ScreenShareOutputs(profile)
+ want = append(want, want[0], want[0])
+ if !slices.Equal(stream.Outputs(), want) || !slices.Equal(configuration.Outputs, want) {
+ t.Fatalf("outputs do not reuse shared plan: %v / %v", stream.Outputs(), configuration.Outputs)
+ }
+ // GPU input can progress before the asynchronous original encoder is admitted.
+ r.VideoInput(s.id, 0)
+ r.VideoInput(s.id, 66_666)
+ if !r.VideoReady(s.id, "vp8", 1920, 1080, "") {
+ t.Fatal("original not admitted")
+ }
+ if !r.VideoLayerReady(s.id, 0, "vp8", 960, 540, "") || r.VideoLayerReady(s.id, 0, "vp8", 1920, 1080, "") || r.VideoLayerReady(s.id, 0, "h264", 960, 540, "42e01f") {
+ t.Fatal("optional encoder admission ignored output contract")
+ }
+ // Independent encoders may complete out of order; neither creates input events.
+ r.VideoLayer(s.id, 1, []byte{1}, 33_333, 33_333, 1920, 1080, true)
+ r.VideoLayer(s.id, 0, []byte{2}, 0, 33_333, 960, 540, true)
+ r.VideoLayer(s.id, 2, []byte{3}, 66_666, 33_333, 960, 540, true)
+ for index, kind := range []nativecapture.FrameKind{nativecapture.FrameStatus, nativecapture.FrameBegin, nativecapture.FrameBegin, nativecapture.FrameVP8, nativecapture.FrameVP8, nativecapture.FrameVP8} {
+ frame, err := stream.Read()
+ if err != nil || frame.Kind != kind {
+ t.Fatalf("event%d=%+v,%v", index, frame, err)
+ }
+ if index == 1 && frame.Timestamp != 0 || index == 2 && frame.Timestamp != 66_666*time.Microsecond {
+ t.Fatalf("capture clock=%v", frame.Timestamp)
+ }
+ if index >= 3 && frame.Layer != []int{1, 0, 2}[index-3] {
+ t.Fatalf("wrong output slot: %+v", frame)
+ }
+ }
+ r.VideoLayerUnavailable(s.id, 2)
+ frame, err := stream.Read()
+ if err != nil || frame.Kind != nativecapture.FrameLayerUnavailable || frame.Layer != 2 || !r.CaptureCurrent(s.id) {
+ t.Fatalf("optional failure ended original: %+v,%v", frame, err)
+ }
+ r.VideoLayerUnavailable(s.id, 1)
+ if r.CaptureCurrent(s.id) {
+ t.Fatal("required original failure retained capture")
+ }
+}
diff --git a/mobile/bridge/encoders.go b/mobile/bridge/encoders.go
new file mode 100644
index 00000000..78812355
--- /dev/null
+++ b/mobile/bridge/encoders.go
@@ -0,0 +1,78 @@
+package bridge
+
+import (
+ "encoding/json"
+ "errors"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/loopback"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativeaudio"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
+ "github.com/TNTcraftHIM/Piik/internal/server/protocol"
+)
+
+type encoderCapability struct {
+ Name string `json:"name"`
+ Codec string `json:"codec"`
+}
+
+type encoderCapabilities struct {
+ Platform string `json:"platform"`
+ Provider string `json:"provider"`
+ MultiOutput bool `json:"multiOutput"`
+ SystemAudio bool `json:"systemAudio"`
+ Encoders []encoderCapability `json:"encoders"`
+}
+
+func decodeEncoderCapabilities(value string) (encoderCapabilities, error) {
+ var catalog encoderCapabilities
+ if err := json.Unmarshal([]byte(value), &catalog); err != nil {
+ return catalog, err
+ }
+ if catalog.Platform == "" || catalog.Provider == "" {
+ return encoderCapabilities{}, errors.New("mobile capture platform is invalid")
+ }
+ for _, encoder := range catalog.Encoders {
+ if encoder.Name == "" || (encoder.Codec != "h264" && encoder.Codec != "vp8") {
+ return encoderCapabilities{}, errors.New("mobile encoder capabilities are invalid")
+ }
+ }
+ return catalog, nil
+}
+
+func (catalog encoderCapabilities) nativeCapabilities() nativecapture.Capabilities {
+ result := nativecapture.Capabilities{Protocol: 7, Platform: catalog.Platform, VideoCapture: len(catalog.Encoders) > 0, SystemAudio: catalog.SystemAudio && len(catalog.Encoders) > 0, Adapters: []nativecapture.Adapter{}}
+ if !result.VideoCapture {
+ return result
+ }
+ adapter := nativecapture.Adapter{Index: 0, Name: catalog.Provider, Identity: catalog.Platform, HardwareH264: []nativecapture.Encoder{}}
+ for _, encoder := range catalog.Encoders {
+ if encoder.Codec == "vp8" {
+ result.SoftwareVP8 = true
+ } else {
+ adapter.HardwareH264 = append(adapter.HardwareH264, nativecapture.Encoder{Index: uint32(len(adapter.HardwareH264)), Name: encoder.Name, Identity: encoder.Name})
+ }
+ }
+ result.Adapters = append(result.Adapters, adapter)
+ return result
+}
+
+func (catalog encoderCapabilities) mediaCapabilities() loopback.NativeMediaCapabilities {
+ native := catalog.nativeCapabilities()
+ // Shared implementation bounds; the platform admits each actual request.
+ result := loopback.NativeMediaCapabilities{
+ Video: native.VideoCapture,
+ SoftwareVP8: native.SoftwareVP8,
+ SystemAudio: native.SystemAudio,
+ ShareVolume: native.SystemAudio,
+ ReceiverReuse: true,
+ MaxRelayChildren: protocol.MaxViewersPerRoomLimit,
+ MaxAudioBitrate: nativeaudio.MaxSendBitrate,
+ MaxVideoResolution: protocol.QualityResolution{Width: nativecapture.MaxVideoDimension, Height: nativecapture.MaxVideoDimension},
+ MaxVideoPixels: nativecapture.MaxVideoPixels,
+ MaxVideoMacroblocksPerSecond: nativecapture.MaxVideoMacroblocksPerSecond,
+ MaxVideoFramerate: nativecapture.MaxVideoFramerate,
+ MaxVideoBitrate: nativecapture.MaxVideoBitrate,
+ }
+ result.HardwareH264 = len(native.Adapters) > 0 && len(native.Adapters[0].HardwareH264) > 0
+ return result
+}
diff --git a/mobile/bridge/runtime.go b/mobile/bridge/runtime.go
new file mode 100644
index 00000000..81a3fcfc
--- /dev/null
+++ b/mobile/bridge/runtime.go
@@ -0,0 +1,503 @@
+package bridge
+
+import (
+ "context"
+ "crypto/tls"
+ "errors"
+ "log/slog"
+ "net"
+ "net/http"
+ "net/url"
+ "os"
+ "path/filepath"
+ "slices"
+ "strconv"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/buildinfo"
+ appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
+ "github.com/TNTcraftHIM/Piik/internal/app/failure"
+ "github.com/TNTcraftHIM/Piik/internal/app/lan"
+ "github.com/TNTcraftHIM/Piik/internal/app/launcher"
+ "github.com/TNTcraftHIM/Piik/internal/app/loopback"
+ "github.com/TNTcraftHIM/Piik/internal/app/nativecontrol"
+ "github.com/TNTcraftHIM/Piik/internal/app/publictunnel"
+ "github.com/TNTcraftHIM/Piik/internal/diagnostics"
+ serverapp "github.com/TNTcraftHIM/Piik/internal/server/app"
+ "github.com/TNTcraftHIM/Piik/internal/server/config"
+ "github.com/TNTcraftHIM/Piik/internal/server/webassets"
+ "github.com/TNTcraftHIM/Piik/internal/tlsconfig"
+)
+
+// The App process owns the room runtime; the platform owns capture resources.
+type Runtime struct {
+ capture *capture
+ server *serverapp.Server
+ control *loopback.Server
+ launcher *launcher.Server
+ ctx context.Context
+ cancel context.CancelFunc
+ once sync.Once
+ url string
+ configMu sync.Mutex
+ configPath string
+ config appconfig.Config
+ tunnel *publictunnel.Process
+ tunnelExecutable string
+ temporaryDirectory string
+ launchDone chan struct{}
+ closeErr error
+}
+
+func Start(platform Platform, lanAddress string, codecCapabilities string, dataDirectory, temporaryDirectory, tunnelExecutable, activationURL string) (*Runtime, error) {
+ if platform == nil {
+ return nil, errors.New("mobile capture platform is unavailable")
+ }
+ if activationURL != "" && activationURL != "piik://media" {
+ return nil, errors.New("mobile media activation URL is invalid")
+ }
+ if webassets.FS() == nil {
+ return nil, errors.New("build the Piik Web assets before the mobile library")
+ }
+ if !filepath.IsAbs(temporaryDirectory) {
+ return nil, errors.New("mobile cache directory is unavailable")
+ }
+ if err := os.MkdirAll(temporaryDirectory, 0700); err != nil {
+ return nil, err
+ }
+ catalog, err := decodeEncoderCapabilities(codecCapabilities)
+ if err != nil {
+ return nil, err
+ }
+ preferences, err := appconfig.LoadOrCreate(filepath.Join(dataDirectory, "client.json"))
+ if err != nil {
+ return nil, err
+ }
+ ctx, cancel := context.WithCancel(context.Background())
+ r := &Runtime{
+ capture: &capture{
+ platform: platform,
+ multiOutput: catalog.MultiOutput,
+ encoders: catalog.Encoders,
+ streams: make(map[int64]*captureStream),
+ },
+ ctx: ctx, cancel: cancel,
+ config: preferences, configPath: filepath.Join(dataDirectory, "client.json"),
+ temporaryDirectory: temporaryDirectory, tunnelExecutable: tunnelExecutable,
+ }
+ capabilities := catalog.nativeCapabilities()
+ microphones, microphoneErr := r.capture.ListMicrophones(ctx)
+ capabilities.Microphone = microphoneErr == nil && len(microphones) > 0
+ if microphoneErr != nil {
+ slog.Debug("mobile microphone enumeration failed", "error", microphoneErr)
+ }
+ mediaCapabilities := catalog.mediaCapabilities()
+ mediaCapabilities.Microphone = capabilities.Microphone
+ mediaCapabilities.SourceCancellation = true
+ r.control, err = loopback.Start(ctx, loopback.Options{
+ ActivationURL: activationURL,
+ MaxControlSessions: 1,
+ AllowedOrigins: preferences.ApprovedSites(),
+ NativeMedia: mediaCapabilities,
+ NewControl: func() loopback.ControlSession {
+ return nativecontrol.NewWithCapture(capabilities, false, nil, r.capture)
+ },
+ })
+ if err != nil {
+ cancel()
+ return nil, err
+ }
+ startup, err := launcher.LoadStartupSite(r.configPath, preferences.ApprovedSites()...)
+ if err != nil {
+ return nil, errors.Join(err, r.Close())
+ }
+ modes := []launcher.Mode{launcher.ModeLocal}
+ if helper, statErr := os.Stat(tunnelExecutable); statErr == nil && filepath.IsAbs(tunnelExecutable) && helper.Mode().IsRegular() {
+ modes = append(modes, launcher.ModeLink)
+ }
+ mode, err := launcher.LoadMode(r.configPath)
+ if err != nil {
+ slog.Debug("Could not restore the last launcher mode", diagnostics.Error(err))
+ }
+ if mode == "" {
+ mode = launcher.ModeLink
+ }
+ if !slices.Contains(modes, mode) {
+ mode = modes[0]
+ }
+ r.launcher, err = launcher.Start(ctx, webassets.FS(), launcher.Options{
+ Version: buildinfo.Version(), Revision: buildinfo.Revision,
+ ControlPort: r.control.Endpoint().Port,
+ Modes: modes, LocalAccessPassword: preferences.LocalAccessPassword, DefaultMode: mode, Sites: preferences.ApprovedSites(), StartupSite: startup, LANAddress: lanAddress,
+ ChangeSite: func(site string, allowed bool) ([]string, error) {
+ var err error
+ if allowed {
+ _, err = r.approveSite(site)
+ } else {
+ err = r.revokeSite(site)
+ }
+ r.configMu.Lock()
+ sites := r.config.ApprovedSites()
+ r.configMu.Unlock()
+ return sites, err
+ },
+ SaveStartupSite: func(site string) error { return launcher.SaveStartupSite(r.configPath, site) },
+ })
+ if err != nil {
+ return nil, errors.Join(err, r.Close())
+ }
+ r.control.SetConnectURL(r.launcher.URL())
+ r.configMu.Lock()
+ err = r.syncAllowedOriginsLocked()
+ r.configMu.Unlock()
+ if err != nil {
+ return nil, errors.Join(err, r.Close())
+ }
+ r.launchDone = make(chan struct{})
+ go func() {
+ defer close(r.launchDone)
+ r.runLauncher(lanAddress)
+ }()
+ go r.watchServices()
+ return r, nil
+}
+
+// Failed startup has already retired its resources before a new selection starts.
+func (r *Runtime) runLauncher(lanAddress string) {
+ for {
+ select {
+ case selected := <-r.launcher.Selection():
+ if err := launcher.SaveMode(r.configPath, selected.Mode); err != nil {
+ slog.Debug("Could not save the launcher mode", diagnostics.Error(err))
+ }
+ target, err := r.startLocal(selected, lanAddress)
+ r.launcher.SetResult(target, err)
+ if err == nil {
+ r.configMu.Lock()
+ server, tunnel := r.server, r.tunnel
+ r.configMu.Unlock()
+ err = r.watchRoom(server, tunnel)
+ if r.ctx.Err() != nil {
+ r.closeErr = errors.Join(r.closeErr, err)
+ return
+ }
+ r.launcher.SetResult("", err)
+ }
+ case <-r.ctx.Done():
+ return
+ }
+ }
+}
+
+func (r *Runtime) URL() string { return r.launcher.URL() }
+func (r *Runtime) EntryURL() string { return r.launcher.EntryURL() }
+
+func (r *Runtime) startLocal(selection launcher.Selection, address string) (target string, returnedErr error) {
+ r.configMu.Lock()
+ next := r.config
+ next.LocalAccessPassword = selection.LocalAccessPassword
+ err := appconfig.Save(r.configPath, next)
+ if err == nil {
+ r.config = next
+ }
+ r.configMu.Unlock()
+ if err != nil {
+ return "", err
+ }
+ if selection.LANAddress != nil {
+ address = *selection.LANAddress
+ }
+ link := selection.Mode == launcher.ModeLink
+ bind := "127.0.0.1:0"
+ if link {
+ bind = "0.0.0.0:0"
+ }
+ local, err := net.Listen("tcp4", bind)
+ if err != nil {
+ return "", err
+ }
+ port := local.Addr().(*net.TCPAddr).Port
+ listeners := []net.Listener{local}
+ var server *serverapp.Server
+ var tunnel *publictunnel.Process
+ committed := false
+ defer func() {
+ if committed {
+ return
+ }
+ if server != nil {
+ returnedErr = errors.Join(returnedErr, endRuntimeServer(server))
+ }
+ if tunnel != nil {
+ returnedErr = errors.Join(returnedErr, tunnel.Close())
+ }
+ for _, listener := range listeners {
+ _ = listener.Close()
+ }
+ }()
+ options := config.LocalOptions{PublicAddress: address, Port: port, SiteAccessPassword: selection.LocalAccessPassword}
+ if link {
+ options.STUNURLs, options.NATPredictionSTUNURLs, err = appconfig.PublicSTUNURLs(os.Getenv("PIIK_STUN_URLS"))
+ if err != nil {
+ return "", err
+ }
+ tunnel, err = publictunnel.Start(r.ctx, r.tunnelExecutable, "http://127.0.0.1:"+strconv.Itoa(port), r.temporaryDirectory)
+ if err != nil {
+ return "", err
+ }
+ options.PublicOrigin = tunnel.Origin()
+ options.AllowedAddresses, _ = lan.Addresses()
+ // A public invitation does not require a currently selected LAN interface.
+ options.PublicAddress = ""
+ } else {
+ secure, err := net.Listen("tcp4", net.JoinHostPort(address, strconv.Itoa(port)))
+ if err != nil {
+ return "", err
+ }
+ listeners = append(listeners, secure)
+ certificate, err := tlsconfig.SelfSigned(address)
+ if err != nil {
+ return "", err
+ }
+ listeners[len(listeners)-1] = tls.NewListener(secure, certificate)
+ }
+ configuration, err := config.Local(options)
+ if err != nil {
+ return "", err
+ }
+ server, err = serverapp.New(serverapp.Options{Config: configuration, Assets: webassets.FS(), Listeners: listeners})
+ if err != nil {
+ return "", err
+ }
+ if _, err = server.Listen(r.ctx); err != nil {
+ return "", err
+ }
+ target = launcher.LocalEntryURL("http://127.0.0.1:"+strconv.Itoa(port)+"/", selection.LocalAccessPassword, r.control.Endpoint().Port)
+ targetURL, _ := url.Parse(target)
+ r.configMu.Lock()
+ if r.ctx.Err() != nil {
+ r.configMu.Unlock()
+ return "", r.ctx.Err()
+ }
+ if err = r.syncAllowedOriginsLocked(targetURL.Scheme + "://" + targetURL.Host); err != nil {
+ r.configMu.Unlock()
+ return "", err
+ }
+ r.server, r.tunnel, r.url = server, tunnel, target
+ committed = true
+ r.configMu.Unlock()
+ return target, nil
+}
+
+func endRuntimeServer(server *serverapp.Server) error {
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ err := server.End(ctx)
+ if errors.Is(err, context.Canceled) || errors.Is(err, http.ErrServerClosed) {
+ return nil
+ }
+ return err
+}
+
+func (r *Runtime) watchRoom(server *serverapp.Server, tunnel *publictunnel.Process) error {
+ var tunnelDone <-chan struct{}
+ if tunnel != nil {
+ tunnelDone = tunnel.Done()
+ }
+ reason := failure.LocalServer
+ select {
+ case <-r.ctx.Done():
+ case <-server.Done():
+ case <-tunnelDone:
+ reason = failure.TunnelStopped
+ }
+ err := endRuntimeServer(server)
+ if tunnel != nil {
+ err = errors.Join(err, tunnel.Close())
+ }
+ r.configMu.Lock()
+ origin := ""
+ if r.server == server {
+ if r.url != "" {
+ origin = r.localOrigin()
+ }
+ r.server, r.tunnel, r.url = nil, nil, ""
+ }
+ r.configMu.Unlock()
+ if origin != "" {
+ retireErr := r.control.RemoveAllowedOrigin(origin)
+ // Whole-runtime cancellation also cancels this permission wait. Close
+ // joins control cleanup with its own deadline after launchDone instead.
+ if r.ctx.Err() == nil {
+ err = errors.Join(err, retireErr)
+ }
+ }
+ if r.ctx.Err() != nil {
+ return err
+ }
+ return errors.Join(reason, err)
+}
+
+func (r *Runtime) watchServices() {
+ var reason failure.Reason
+ select {
+ case <-r.ctx.Done():
+ return
+ case <-r.launcher.Done():
+ reason = failure.Launcher
+ case <-r.control.Done():
+ reason = failure.NativeControl
+ }
+ r.stop(reason)
+}
+
+func (r *Runtime) stop(reason failure.Reason) {
+ if r.ctx.Err() != nil {
+ return
+ }
+ if err := r.close(reason); err != nil {
+ slog.Warn("mobile runtime cleanup failed", "error", err)
+ }
+}
+
+// Sites retain the desktop configuration and exact-origin approval owner.
+func (r *Runtime) approveSite(value string) (string, error) {
+ site, err := appconfig.NormalizeSite(value)
+ if err != nil || !strings.HasPrefix(site, "https://") {
+ return "", errors.New("Enter an HTTPS Piik site origin")
+ }
+ r.configMu.Lock()
+ sites := r.config.ApprovedSites()
+ for _, current := range sites {
+ if current == site {
+ r.configMu.Unlock()
+ return site, nil
+ }
+ }
+ next := r.config
+ next.SetApprovedSites(append(sites, site))
+ if err = appconfig.Save(r.configPath, next); err != nil {
+ r.configMu.Unlock()
+ return "", err
+ }
+ r.config = next
+ err = r.syncAllowedOriginsLocked()
+ r.configMu.Unlock()
+ r.capture.platform.SiteAccessChanged()
+ return site, err
+}
+func (r *Runtime) revokeSite(value string) error {
+ r.configMu.Lock()
+ var sites []string
+ for _, site := range r.config.ApprovedSites() {
+ if site != value {
+ sites = append(sites, site)
+ }
+ }
+ next := r.config
+ next.SetApprovedSites(sites)
+ if err := appconfig.Save(r.configPath, next); err != nil {
+ r.configMu.Unlock()
+ return err
+ }
+ r.config = next
+ err := r.syncAllowedOriginsLocked()
+ r.configMu.Unlock()
+ r.capture.platform.SiteAccessChanged()
+ return err
+}
+
+// Caller holds configMu through the permission commit, so room startup cannot
+// replay a stale Site snapshot or discard the newly installed local origin.
+func (r *Runtime) syncAllowedOriginsLocked(pendingOrigins ...string) error {
+ origins := append(r.localOriginsLocked(), pendingOrigins...)
+ return r.control.SetAllowedOrigins(append(origins, r.config.ApprovedSites()...))
+}
+
+func (r *Runtime) localOrigins() []string {
+ r.configMu.Lock()
+ defer r.configMu.Unlock()
+ return r.localOriginsLocked()
+}
+
+func (r *Runtime) localOriginsLocked() []string {
+ origins := []string{}
+ if r.url != "" {
+ origins = append(origins, r.localOrigin())
+ }
+ if r.launcher != nil {
+ u, _ := url.Parse(r.launcher.URL())
+ origins = append(origins, u.Scheme+"://"+u.Host)
+ }
+ return origins
+}
+func (r *Runtime) localOrigin() string { u, _ := url.Parse(r.url); return u.Scheme + "://" + u.Host }
+func (r *Runtime) TrustedURL(value string) bool {
+ u, err := url.Parse(value)
+ if err != nil || u.User != nil {
+ return false
+ }
+ origin, err := appconfig.NormalizeSite(u.Scheme + "://" + u.Host)
+ if err != nil {
+ return false
+ }
+ for _, local := range r.localOrigins() {
+ if origin == local {
+ return true
+ }
+ }
+ r.configMu.Lock()
+ defer r.configMu.Unlock()
+ for _, site := range r.config.ApprovedSites() {
+ if site == origin {
+ return true
+ }
+ }
+ return false
+}
+
+func (r *Runtime) Close() error {
+ return r.close("")
+}
+
+func (r *Runtime) close(reason failure.Reason) error {
+ closed := false
+ r.once.Do(func() {
+ closed = true
+ r.cancel()
+ if r.launchDone != nil {
+ <-r.launchDone
+ }
+ r.configMu.Lock()
+ server := r.server
+ tunnel := r.tunnel
+ r.configMu.Unlock()
+ if server != nil {
+ r.closeErr = errors.Join(r.closeErr, endRuntimeServer(server))
+ }
+ if tunnel != nil {
+ r.closeErr = errors.Join(r.closeErr, tunnel.Close())
+ }
+ if r.control != nil {
+ r.closeErr = errors.Join(r.closeErr, r.control.Close())
+ }
+ if r.launcher != nil {
+ r.closeErr = errors.Join(r.closeErr, r.launcher.Close())
+ }
+ r.capture.mu.Lock()
+ streams := make([]*captureStream, 0, len(r.capture.streams))
+ for _, s := range r.capture.streams {
+ streams = append(streams, s)
+ }
+ r.capture.mu.Unlock()
+ for _, s := range streams {
+ r.closeErr = errors.Join(r.closeErr, s.Close())
+ }
+ })
+ if closed && reason != "" {
+ r.capture.platform.RuntimeStopped(string(reason))
+ }
+ return r.closeErr
+}
diff --git a/mobile/bridge/runtime_test.go b/mobile/bridge/runtime_test.go
new file mode 100644
index 00000000..78d5a931
--- /dev/null
+++ b/mobile/bridge/runtime_test.go
@@ -0,0 +1,214 @@
+package bridge
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "os"
+ "path/filepath"
+ "slices"
+ "strconv"
+ "strings"
+ "testing"
+
+ "github.com/TNTcraftHIM/Piik/internal/app/buildinfo"
+ appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
+ "github.com/TNTcraftHIM/Piik/internal/app/launcher"
+ "github.com/TNTcraftHIM/Piik/internal/app/loopback"
+ "github.com/TNTcraftHIM/Piik/internal/server/webassets"
+)
+
+func TestStartRejectsUnownedActivationURLBeforeAllocating(t *testing.T) {
+ _, err := Start(&testPlatform{}, "", "", "", "", "", "https://other.example/")
+ if err == nil || !strings.Contains(err.Error(), "activation URL") {
+ t.Fatalf("unexpected activation URL result: %v", err)
+ }
+}
+
+func TestLauncherRestoresOnlyAvailableModes(t *testing.T) {
+ if webassets.FS() == nil {
+ t.Skip("mobile runtime requires built Web assets")
+ }
+ previousVersion, previousRevision := buildinfo.Release, buildinfo.Revision
+ buildinfo.Release, buildinfo.Revision = "v2.4.1", strings.Repeat("a", 40)
+ t.Cleanup(func() { buildinfo.Release, buildinfo.Revision = previousVersion, previousRevision })
+ helper, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ directory := t.TempDir()
+ path := filepath.Join(directory, "client.json")
+ for _, test := range []struct {
+ name string
+ saved launcher.Mode
+ helper string
+ want launcher.Mode
+ modes []launcher.Mode
+ selectLocal bool
+ }{
+ {"first launch with helper", "", helper, launcher.ModeLink, []launcher.Mode{launcher.ModeLocal, launcher.ModeLink}, true},
+ {"saved local after restart", "", helper, launcher.ModeLocal, []launcher.Mode{launcher.ModeLocal, launcher.ModeLink}, false},
+ {"saved link without helper", launcher.ModeLink, "", launcher.ModeLocal, []launcher.Mode{launcher.ModeLocal}, false},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ if test.saved != "" {
+ if err := launcher.SaveMode(path, test.saved); err != nil {
+ t.Fatal(err)
+ }
+ }
+ r, err := Start(&testPlatform{}, "", `{"platform":"android","provider":"fixture","encoders":[]}`,
+ directory, t.TempDir(), test.helper, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer r.Close()
+ response, err := http.Get(strings.TrimSuffix(r.URL(), "/client") + "/api/client-launcher")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer response.Body.Close()
+ var state struct {
+ DefaultMode launcher.Mode
+ Modes []launcher.Mode
+ Version string
+ Revision string
+ }
+ if err = json.NewDecoder(response.Body).Decode(&state); err != nil {
+ t.Fatal(err)
+ }
+ if state.DefaultMode != test.want || !slices.Equal(state.Modes, test.modes) {
+ t.Fatalf("launcher mode = %+v, want default=%s modes=%v", state, test.want, test.modes)
+ }
+ if state.Version != buildinfo.Version() || state.Revision != buildinfo.Revision {
+ t.Fatalf("launcher did not receive App build identity: %+v", state)
+ }
+ if test.selectLocal {
+ origin := strings.TrimSuffix(r.URL(), "/client")
+ request, _ := http.NewRequest(http.MethodPost, origin+"/api/client-launcher/launch", strings.NewReader(`{"mode":"local","language":"en"}`))
+ request.Header.Set("Origin", origin)
+ request.Header.Set("Content-Type", "application/json")
+ chosen, err := http.DefaultClient.Do(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ chosen.Body.Close()
+ // Mode is remembered even when Local has no invitation interface.
+ if chosen.StatusCode != http.StatusServiceUnavailable {
+ t.Fatalf("Local without an address = %d", chosen.StatusCode)
+ }
+ }
+ })
+ }
+}
+
+func TestEntryTracksCurrentStartupPreferenceAndSiteRevocation(t *testing.T) {
+ if webassets.FS() == nil {
+ t.Skip("mobile runtime requires built Web assets")
+ }
+ directory := t.TempDir()
+ configPath := filepath.Join(directory, "client.json")
+ if err := appconfig.Save(configPath, appconfig.Config{Version: 1, Sites: []string{"https://first.example", "https://second.example"}}); err != nil {
+ t.Fatal(err)
+ }
+ if err := launcher.SaveStartupSite(configPath, "https://first.example"); err != nil {
+ t.Fatal(err)
+ }
+ r, err := Start(&testPlatform{}, "", `{"platform":"android","provider":"Android MediaCodec","systemAudio":true,"encoders":[]}`, directory, t.TempDir(), "", "piik://media")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer r.Close()
+ entrySuffix := "/?piik-client-port=" + strconv.Itoa(r.control.Endpoint().Port) + "#piik-client=1"
+ if r.EntryURL() != "https://first.example"+entrySuffix {
+ t.Fatal("saved entry was not restored")
+ }
+ startup := r.URL()
+ origin := strings.TrimSuffix(startup, "/client")
+ for _, test := range []struct{ setting, body, entry, startupSite string }{
+ {"startup-site", `{"site":"https://second.example"}`, "https://second.example" + entrySuffix, "https://second.example"},
+ {"startup-site", `{"site":""}`, startup, ""},
+ {"startup-site", `{"site":"https://first.example"}`, "https://first.example" + entrySuffix, "https://first.example"},
+ {"site", `{"site":"https://first.example","allowed":false}`, startup, ""},
+ {"site", `{"site":"https://third.example","allowed":true}`, startup, ""},
+ {"startup-site", `{"site":"https://third.example"}`, "https://third.example" + entrySuffix, "https://third.example"},
+ } {
+ request, _ := http.NewRequest(http.MethodPost, origin+"/api/client-launcher/"+test.setting, strings.NewReader(test.body))
+ request.Header.Set("Origin", origin)
+ request.Header.Set("Content-Type", "application/json")
+ response, err := http.DefaultClient.Do(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var state struct{ StartupSite string }
+ decodeErr := json.NewDecoder(response.Body).Decode(&state)
+ response.Body.Close()
+ if response.StatusCode != http.StatusOK || decodeErr != nil {
+ t.Fatalf("setting %s failed: %d %v", test.setting, response.StatusCode, decodeErr)
+ }
+ if r.EntryURL() != test.entry || r.URL() != startup || state.StartupSite != test.startupSite {
+ t.Fatalf("entry did not follow %s: got %q want %q", test.body, r.EntryURL(), test.entry)
+ }
+ }
+ for site, want := range map[string]int{"https://first.example": http.StatusForbidden, "https://third.example": http.StatusOK} {
+ request, _ := http.NewRequest(http.MethodGet, r.control.Endpoint().URL+"/health", nil)
+ request.Header.Set("Origin", site)
+ response, err := http.DefaultClient.Do(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ if response.StatusCode != want {
+ t.Fatalf("site authorization = %d want %d", response.StatusCode, want)
+ }
+ }
+ if r.server != nil {
+ t.Fatal("startup preference created a local room")
+ }
+}
+
+func TestSiteApprovalPersistsExactOriginAndRevocation(t *testing.T) {
+ control, err := loopback.Start(context.Background(), loopback.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer control.Close()
+ path := filepath.Join(t.TempDir(), "client.json")
+ cfg, err := appconfig.LoadOrCreate(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ platform := &testPlatform{}
+ r := &Runtime{capture: &capture{platform: platform}, control: control, url: "http://127.0.0.1:49152/#piik-client=1", configPath: path, config: cfg}
+ var notified []bool
+ platform.siteChanged = func() { notified = append(notified, r.TrustedURL("https://piik.example")) }
+ site, err := r.approveSite("PIIK.example:443")
+ if err != nil || site != "https://piik.example" {
+ t.Fatalf("approval: %q %v", site, err)
+ }
+ if !r.TrustedURL(site+"/room#local") || r.TrustedURL("https://other.example") || r.TrustedURL("http://piik.example") || !r.TrustedURL(r.url) {
+ t.Fatal("incorrect exact origin trust")
+ }
+ persisted, err := appconfig.LoadOrCreate(path)
+ if err != nil || len(persisted.ApprovedSites()) != 1 {
+ t.Fatalf("approval not persisted: %v", err)
+ }
+ if _, err = r.approveSite("https://piik.example/room"); err == nil {
+ t.Fatal("navigation URL granted as an origin")
+ }
+ if _, err = r.approveSite("http://piik.example"); err == nil {
+ t.Fatal("insecure hosted site approved")
+ }
+ if err = r.revokeSite(site); err != nil {
+ t.Fatal(err)
+ }
+ if r.TrustedURL(site) || !r.TrustedURL(r.url) {
+ t.Fatal("revocation changed wrong authority")
+ }
+ if !slices.Equal(notified, []bool{true, false}) {
+ t.Fatalf("platform observed stale site authority: %v", notified)
+ }
+ persisted, err = appconfig.LoadOrCreate(path)
+ if err != nil || len(persisted.ApprovedSites()) != 0 {
+ t.Fatal("revocation not persisted")
+ }
+}
diff --git a/mobile/bridge/runtime_tunnel_test.go b/mobile/bridge/runtime_tunnel_test.go
new file mode 100644
index 00000000..6daacb59
--- /dev/null
+++ b/mobile/bridge/runtime_tunnel_test.go
@@ -0,0 +1,635 @@
+package bridge
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net"
+ "net/http"
+ "net/url"
+ "os"
+ "path/filepath"
+ "slices"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+ "testing/fstest"
+ "time"
+
+ appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
+ "github.com/TNTcraftHIM/Piik/internal/app/failure"
+ "github.com/TNTcraftHIM/Piik/internal/app/launcher"
+ "github.com/TNTcraftHIM/Piik/internal/app/loopback"
+ serverapp "github.com/TNTcraftHIM/Piik/internal/server/app"
+ serverconfig "github.com/TNTcraftHIM/Piik/internal/server/config"
+ "github.com/coder/websocket"
+ "github.com/coder/websocket/wsjson"
+)
+
+type retiringRoomControl struct {
+ started chan struct{}
+ release chan struct{}
+}
+
+func (*retiringRoomControl) Handle(context.Context, []byte) (any, error) { return nil, nil }
+func (*retiringRoomControl) Events() <-chan any { return nil }
+func (control *retiringRoomControl) Close() error {
+ close(control.started)
+ <-control.release
+ return nil
+}
+
+func TestCloseJoinsLocalControlRetirementWithoutFalseError(t *testing.T) {
+ t.Setenv("PIIK_ANDROID_TUNNEL_FIXTURE", "ready")
+ r, _ := tunnelRuntime(t)
+ if err := r.control.Close(); err != nil {
+ t.Fatal(err)
+ }
+ control := &retiringRoomControl{started: make(chan struct{}), release: make(chan struct{})}
+ var release sync.Once
+ t.Cleanup(func() { release.Do(func() { close(control.release) }) })
+ var err error
+ r.control, err = loopback.Start(r.ctx, loopback.Options{NewControl: func() loopback.ControlSession { return control }})
+ if err != nil {
+ t.Fatal(err)
+ }
+ r.launcher, err = launcher.Start(r.ctx, fstest.MapFS{"index.html": {Data: []byte("launcher")}}, launcher.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ r.launchDone = make(chan struct{})
+ go func() { defer close(r.launchDone); r.runLauncher("") }()
+ launcherOrigin := strings.TrimSuffix(r.launcher.URL(), "/client")
+ request, _ := http.NewRequest("POST", launcherOrigin+"/api/client-launcher/launch", strings.NewReader(`{"mode":"link","language":"en"}`))
+ request.Header.Set("Origin", launcherOrigin)
+ request.Header.Set("Content-Type", "application/json")
+ response, err := http.DefaultClient.Do(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ if response.StatusCode != http.StatusOK {
+ t.Fatalf("launch = %d", response.StatusCode)
+ }
+ r.configMu.Lock()
+ origin := r.localOrigin()
+ r.configMu.Unlock()
+ endpoint := r.control.Endpoint()
+ connection, _, err := websocket.Dial(t.Context(), strings.Replace(endpoint.URL, "http://", "ws://", 1)+"/control", &websocket.DialOptions{
+ HTTPHeader: http.Header{"Origin": []string{origin}},
+ Subprotocols: []string{loopback.ControlSubprotocol + "." + endpoint.InstanceToken},
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer connection.CloseNow()
+ if err = wsjson.Write(t.Context(), connection, map[string]any{"version": loopback.ProtocolVersion, "id": "close_hello", "type": "hello"}); err != nil {
+ t.Fatal(err)
+ }
+ var ready map[string]any
+ if err = wsjson.Read(t.Context(), connection, &ready); err != nil {
+ t.Fatal(err)
+ }
+ if ready["type"] != "ready" {
+ t.Fatalf("control handshake = %v", ready)
+ }
+ closed := make(chan error, 1)
+ go func() { closed <- r.Close() }()
+ select {
+ case <-control.started:
+ case <-time.After(5 * time.Second):
+ t.Fatal("local control did not enter retirement")
+ }
+ select {
+ case err := <-closed:
+ t.Fatalf("Close returned before local control cleanup: %v", err)
+ case <-time.After(30 * time.Millisecond):
+ }
+ release.Do(func() { close(control.release) })
+ select {
+ case err := <-closed:
+ if err != nil {
+ t.Fatalf("completed local control cleanup reported failure: %v", err)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("Close did not join completed control cleanup")
+ }
+}
+
+// Run the real supervised process path without contacting Cloudflare.
+func TestMain(tests *testing.M) {
+ if mode := os.Getenv("PIIK_ANDROID_TUNNEL_FIXTURE"); mode != "" {
+ index := slices.Index(os.Args, "--config")
+ if index < 0 || index+1 >= len(os.Args) {
+ os.Exit(9)
+ }
+ if record := os.Getenv("PIIK_ANDROID_TUNNEL_STARTED"); record != "" {
+ if err := os.WriteFile(record, []byte(os.Args[index+1]), 0600); err != nil {
+ os.Exit(9)
+ }
+ }
+ if mode == "held" {
+ for {
+ if _, err := os.Stat(os.Getenv("PIIK_ANDROID_TUNNEL_RELEASE")); err == nil {
+ break
+ }
+ time.Sleep(time.Millisecond)
+ }
+ }
+ if mode != "pending" {
+ fmt.Println(`{"message":"https://android-fixture.trycloudflare.com"}`)
+ fmt.Println(`{"message":"Registered tunnel connection"}`)
+ }
+ for {
+ if trigger := os.Getenv("PIIK_ANDROID_TUNNEL_EXIT"); trigger != "" {
+ if _, err := os.Stat(trigger); err == nil {
+ os.Exit(7)
+ }
+ }
+ time.Sleep(time.Millisecond)
+ }
+ }
+ os.Exit(tests.Run())
+}
+
+func TestStartupAndSiteRevocationCommitOneOriginAuthority(t *testing.T) {
+ dir := t.TempDir()
+ started, release := filepath.Join(dir, "started"), filepath.Join(dir, "release")
+ t.Setenv("PIIK_ANDROID_TUNNEL_FIXTURE", "held")
+ t.Setenv("PIIK_ANDROID_TUNNEL_STARTED", started)
+ t.Setenv("PIIK_ANDROID_TUNNEL_RELEASE", release)
+ if err := appconfig.Save(filepath.Join(dir, "client.json"), appconfig.Config{Version: 1, Sites: []string{"https://revoked.example"}}); err != nil {
+ t.Fatal(err)
+ }
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ platform := &testPlatform{}
+ r, err := Start(platform, "", `{"platform":"android","provider":"fixture","encoders":[]}`, dir, t.TempDir(), executable, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer r.Close()
+ client := &http.Client{Timeout: 5 * time.Second}
+ origin := strings.TrimSuffix(r.URL(), "/client")
+ post := func(path, body string) int {
+ t.Helper()
+ request, _ := http.NewRequest(http.MethodPost, origin+"/api/client-launcher/"+path, strings.NewReader(body))
+ request.Header.Set("Origin", origin)
+ request.Header.Set("Content-Type", "application/json")
+ response, err := client.Do(request)
+ if err != nil {
+ t.Error(err)
+ return 0
+ }
+ response.Body.Close()
+ return response.StatusCode
+ }
+ health := func(site string) int {
+ t.Helper()
+ request, _ := http.NewRequest(http.MethodGet, r.control.Endpoint().URL+"/health", nil)
+ request.Header.Set("Origin", site)
+ response, err := client.Do(request)
+ if err != nil {
+ t.Error(err)
+ return 0
+ }
+ response.Body.Close()
+ return response.StatusCode
+ }
+ launched := make(chan int, 1)
+ go func() { launched <- post("launch", `{"mode":"link","language":"en"}`) }()
+ deadline := time.Now().Add(5 * time.Second)
+ for {
+ if _, err := os.Stat(started); err == nil {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("startup did not reach the pending helper")
+ }
+ time.Sleep(time.Millisecond)
+ }
+ notified, notificationReleased := make(chan struct{}), make(chan struct{})
+ var once sync.Once
+ defer once.Do(func() { close(notificationReleased) })
+ platform.siteChanged = func() {
+ if r.TrustedURL("https://revoked.example") || health("https://revoked.example") != http.StatusForbidden {
+ t.Error("platform notification preceded the exact-origin revocation commit")
+ }
+ close(notified)
+ <-notificationReleased
+ }
+ revoked := make(chan int, 1)
+ go func() { revoked <- post("site", `{"site":"https://revoked.example","allowed":false}`) }()
+ select {
+ case <-notified:
+ case <-time.After(5 * time.Second):
+ t.Fatal("revocation notification blocked")
+ }
+ if err := os.WriteFile(release, nil, 0600); err != nil {
+ t.Fatal(err)
+ }
+ var target string
+ for {
+ r.configMu.Lock()
+ target = r.url
+ r.configMu.Unlock()
+ if target != "" {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("platform notification held the runtime authority lock")
+ }
+ time.Sleep(time.Millisecond)
+ }
+ local, _ := url.Parse(target)
+ if !r.TrustedURL(target) || health(local.Scheme+"://"+local.Host) != http.StatusOK || health("https://revoked.example") != http.StatusForbidden {
+ t.Fatal("startup lost its local origin or restored the revoked Site")
+ }
+ once.Do(func() { close(notificationReleased) })
+ if status := <-revoked; status != http.StatusOK {
+ t.Fatalf("revocation = %d", status)
+ }
+ if status := <-launched; status != http.StatusOK {
+ t.Fatalf("startup = %d", status)
+ }
+}
+
+type runtimePlatform struct {
+ testPlatform
+ terminated chan string
+}
+
+func (platform *runtimePlatform) RuntimeStopped(reason string) { platform.terminated <- reason }
+
+func TestEssentialServicesRetireTheMobileRuntime(t *testing.T) {
+ for _, component := range []string{"launcher", "control", "room-and-control", "owner-close"} {
+ t.Run(component, func(t *testing.T) {
+ platform := &runtimePlatform{terminated: make(chan string, 4)}
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ r, err := Start(platform, "", `{"platform":"android","provider":"Android MediaCodec","encoders":[]}`, t.TempDir(), t.TempDir(), executable, "piik://media")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = r.Close() })
+ want := failure.Launcher
+ switch component {
+ case "launcher":
+ err = r.launcher.Close()
+ case "control":
+ want = failure.NativeControl
+ err = r.control.Close()
+ case "room-and-control":
+ t.Setenv("PIIK_ANDROID_TUNNEL_FIXTURE", "ready")
+ trigger := filepath.Join(t.TempDir(), "exit")
+ t.Setenv("PIIK_ANDROID_TUNNEL_EXIT", trigger)
+ if _, err = r.startLocal(launcher.Selection{Mode: launcher.ModeLink}, ""); err != nil {
+ t.Fatal(err)
+ }
+ if err = os.WriteFile(trigger, nil, 0600); err != nil {
+ t.Fatal(err)
+ }
+ err = r.control.Close()
+ case "owner-close":
+ err = r.Close()
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ if component != "owner-close" {
+ select {
+ case reason := <-platform.terminated:
+ if component == "room-and-control" {
+ if reason != string(failure.NativeControl) {
+ t.Fatalf("unexpected competing failure: %q", reason)
+ }
+ } else if reason != string(want) {
+ t.Fatalf("wrong service failure: %q", reason)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("essential listener ended but mobile Runtime remained live")
+ }
+ }
+ if r.ctx.Err() == nil {
+ t.Fatal("service exit did not retire the Runtime")
+ }
+ if component == "room-and-control" {
+ <-r.tunnel.Done()
+ }
+ r.watchServices()
+ select {
+ case reason := <-platform.terminated:
+ t.Fatalf("normal close or repeated completion reported an extra failure: %q", reason)
+ default:
+ }
+ })
+ }
+}
+
+func tunnelRuntime(t *testing.T) (*Runtime, *runtimePlatform) {
+ t.Helper()
+ ctx, cancel := context.WithCancel(t.Context())
+ control, err := loopback.Start(ctx, loopback.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ directory := t.TempDir()
+ configPath := filepath.Join(directory, "client.json")
+ configuration, err := appconfig.LoadOrCreate(configPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ platform := &runtimePlatform{terminated: make(chan string, 1)}
+ r := &Runtime{ctx: ctx, cancel: cancel, control: control, config: configuration, configPath: configPath,
+ capture: &capture{platform: platform, streams: make(map[int64]*captureStream)},
+ tunnelExecutable: executable, temporaryDirectory: directory}
+ t.Cleanup(func() { _ = r.Close() })
+ return r, platform
+}
+
+func TestLauncherRetriesFailedStartupAndKeepsSuccessfulRoom(t *testing.T) {
+ t.Setenv("PIIK_ANDROID_TUNNEL_FIXTURE", "ready")
+ r, _ := tunnelRuntime(t)
+ if _, err := r.approveSite("https://site.example"); err != nil {
+ t.Fatal(err)
+ }
+ var err error
+ r.launcher, err = launcher.Start(r.ctx, fstest.MapFS{"index.html": {Data: []byte("launcher")}}, launcher.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ r.launchDone = make(chan struct{})
+ go func() { defer close(r.launchDone); r.runLauncher("") }()
+ endpoint := strings.TrimSuffix(r.launcher.URL(), "/client") + "/api/client-launcher/launch"
+ client := &http.Client{Timeout: 5 * time.Second}
+ post := func(body string) *http.Response {
+ t.Helper()
+ request, _ := http.NewRequest("POST", endpoint, strings.NewReader(body))
+ request.Header.Set("Origin", strings.TrimSuffix(r.launcher.URL(), "/client"))
+ request.Header.Set("Content-Type", "application/json")
+ response, err := client.Do(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { response.Body.Close() })
+ return response
+ }
+ // No invitation interface was selected, so Local fails after opening its
+ // loopback listener. Public mode can then start without a LAN selection.
+ if response := post(`{"mode":"local","language":"en"}`); response.StatusCode != http.StatusServiceUnavailable {
+ t.Fatalf("Local startup without an address = %d", response.StatusCode)
+ }
+ if r.server != nil || r.tunnel != nil || !r.TrustedURL("https://site.example") {
+ t.Fatal("failed attempt retained a room or retired Site access")
+ }
+ if response := post(`{"mode":"link","language":"en"}`); response.StatusCode != http.StatusOK {
+ t.Fatalf("public retry = %d", response.StatusCode)
+ }
+ if mode, err := launcher.LoadMode(r.configPath); err != nil || mode != launcher.ModeLink {
+ t.Fatalf("Public selection was not saved: %q, %v", mode, err)
+ }
+ r.configMu.Lock()
+ server, tunnel, target := r.server, r.tunnel, r.url
+ r.configMu.Unlock()
+ if server == nil || tunnel == nil || !r.TrustedURL("https://site.example") || !r.TrustedURL(target) {
+ t.Fatal("retry did not retain room and Site authority")
+ }
+ response := post(`{"mode":"local","language":"en"}`)
+ var reopened struct{ Target string }
+ if response.StatusCode != http.StatusOK || json.NewDecoder(response.Body).Decode(&reopened) != nil || reopened.Target != target {
+ t.Fatalf("active room reentry = %d, target unchanged = %v", response.StatusCode, reopened.Target == target)
+ }
+ r.configMu.Lock()
+ if r.server != server || r.tunnel != tunnel {
+ r.configMu.Unlock()
+ t.Fatal("launcher reopening replaced a successful room")
+ }
+ r.configMu.Unlock()
+ siteCapture := r.capture.stream(r.ctx, nil)
+ if err := tunnel.Close(); err != nil {
+ t.Fatal(err)
+ }
+ deadline := time.Now().Add(5 * time.Second)
+ for {
+ response, err := client.Get(strings.TrimSuffix(r.launcher.URL(), "/client") + "/api/client-launcher")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var state struct {
+ Target string
+ Reason string
+ }
+ err = json.NewDecoder(response.Body).Decode(&state)
+ response.Body.Close()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if state.Target == "" && state.Reason != "" {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("room failure did not return launcher to retry")
+ }
+ time.Sleep(time.Millisecond)
+ }
+ if r.TrustedURL(target) || !r.TrustedURL("https://site.example") || r.capture.get(siteCapture.id) == nil {
+ t.Fatal("room failure changed unrelated authority")
+ }
+ if response := post(`{"mode":"link","language":"en"}`); response.StatusCode != http.StatusOK {
+ t.Fatalf("room retry = %d", response.StatusCode)
+ }
+ r.configMu.Lock()
+ if r.server == nil || r.server == server || r.tunnel == tunnel || r.url == target {
+ r.configMu.Unlock()
+ t.Fatal("retry did not create a fresh room")
+ }
+ r.configMu.Unlock()
+}
+
+func TestPublicLinkSharesServerAndRetiresOnDependencyExit(t *testing.T) {
+ t.Setenv("PIIK_ANDROID_TUNNEL_FIXTURE", "ready")
+ trigger := filepath.Join(t.TempDir(), "exit")
+ t.Setenv("PIIK_ANDROID_TUNNEL_EXIT", trigger)
+ r, platform := tunnelRuntime(t)
+ if _, err := r.approveSite("https://site.example"); err != nil {
+ t.Fatal(err)
+ }
+ siteCapture := r.capture.stream(r.ctx, nil)
+ target, err := r.startLocal(launcher.Selection{Mode: launcher.ModeLink}, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if r.tunnel == nil || r.tunnel.Origin() != "https://android-fixture.trycloudflare.com" || !r.TrustedURL(target) {
+ t.Fatal("public invitation did not retain the shared local Host")
+ }
+ endpoint, _ := url.Parse(target)
+ endpoint.Fragment, endpoint.Path = "", "/healthz"
+ client := &http.Client{Timeout: time.Second}
+ response, err := client.Get(endpoint.String())
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = response.Body.Close()
+ if response.StatusCode != http.StatusOK {
+ t.Fatalf("shared server not ready: %d", response.StatusCode)
+ }
+ result := make(chan error, 1)
+ r.launchDone = make(chan struct{})
+ go func() { defer close(r.launchDone); result <- r.watchRoom(r.server, r.tunnel) }()
+ if err := os.WriteFile(trigger, nil, 0600); err != nil {
+ t.Fatal(err)
+ }
+ select {
+ case err := <-result:
+ if !errors.Is(err, failure.TunnelStopped) {
+ t.Fatalf("wrong room failure: %v", err)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("dependency exit did not finish room retirement")
+ }
+ if r.ctx.Err() != nil || r.TrustedURL(target) || !r.TrustedURL("https://site.example") || r.capture.get(siteCapture.id) != siteCapture {
+ t.Fatal("room failure changed unrelated Runtime/Site/capture authority")
+ }
+ request, _ := http.NewRequest("GET", r.control.Endpoint().URL+"/health", nil)
+ request.Header.Set("Origin", "https://site.example")
+ if response, err := client.Do(request); err != nil {
+ t.Fatal(err)
+ } else {
+ response.Body.Close()
+ if response.StatusCode != http.StatusOK {
+ t.Fatal("Site control is unavailable")
+ }
+ }
+ select {
+ case <-platform.terminated:
+ t.Fatal("room failure ended Runtime")
+ default:
+ }
+ if response, err := client.Get(endpoint.String()); err == nil {
+ response.Body.Close()
+ t.Fatal("terminal runtime retained its room server")
+ }
+ configs, err := filepath.Glob(filepath.Join(r.temporaryDirectory, "piik-cloudflared-*.yml"))
+ if err != nil || len(configs) != 0 {
+ t.Fatalf("dependency retained temporary config: %v, %v", configs, err)
+ }
+}
+
+func TestRoomListenerFailureRetiresOnlyRoom(t *testing.T) {
+ t.Setenv("PIIK_ANDROID_TUNNEL_FIXTURE", "ready")
+ r, platform := tunnelRuntime(t)
+ if _, err := r.approveSite("https://site.example"); err != nil {
+ t.Fatal(err)
+ }
+ siteCapture := r.capture.stream(r.ctx, nil)
+ listener, err := net.Listen("tcp4", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { listener.Close() })
+ port := listener.Addr().(*net.TCPAddr).Port
+ configuration, err := serverconfig.Local(serverconfig.LocalOptions{Port: port, PublicOrigin: "https://fixture.example"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ server, err := serverapp.New(serverapp.Options{Config: configuration, Listeners: []net.Listener{listener}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ r.server = server
+ oldTarget := "http://127.0.0.1:" + strconv.Itoa(port) + "/"
+ r.url = oldTarget
+ if _, err := server.Listen(r.ctx); err != nil {
+ t.Fatal(err)
+ }
+ result := make(chan error, 1)
+ r.launchDone = make(chan struct{})
+ go func() { defer close(r.launchDone); result <- r.watchRoom(server, nil) }()
+ if err := listener.Close(); err != nil {
+ t.Fatal(err)
+ }
+ select {
+ case err := <-result:
+ if !errors.Is(err, failure.LocalServer) {
+ t.Fatalf("wrong room failure: %v", err)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("listener failure left the mobile runtime ready")
+ }
+ if r.ctx.Err() != nil || r.server != nil {
+ t.Fatal("listener failure retained room or ended Runtime")
+ }
+ if r.TrustedURL(oldTarget) || !r.TrustedURL("https://site.example") || r.capture.get(siteCapture.id) == nil {
+ t.Fatal("listener failure changed unrelated Site/capture authority")
+ }
+ request, _ := http.NewRequest("GET", r.control.Endpoint().URL+"/health", nil)
+ request.Header.Set("Origin", "https://site.example")
+ response, err := http.DefaultClient.Do(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ if response.StatusCode != http.StatusOK {
+ t.Fatal("listener failure ended Site control")
+ }
+ if target, err := r.startLocal(launcher.Selection{Mode: launcher.ModeLink}, ""); err != nil || target == oldTarget {
+ t.Fatalf("new selection did not recover after listener failure: %v", err)
+ }
+ select {
+ case <-platform.terminated:
+ t.Fatal("listener failure ended Runtime")
+ default:
+ }
+}
+
+func TestCloseJoinsUnreadyPublicLinkStartup(t *testing.T) {
+ t.Setenv("PIIK_ANDROID_TUNNEL_FIXTURE", "pending")
+ started := filepath.Join(t.TempDir(), "started")
+ t.Setenv("PIIK_ANDROID_TUNNEL_STARTED", started)
+ r, platform := tunnelRuntime(t)
+ r.launchDone = make(chan struct{})
+ result := make(chan error, 1)
+ go func() {
+ _, err := r.startLocal(launcher.Selection{Mode: launcher.ModeLink}, "")
+ result <- err
+ close(r.launchDone)
+ }()
+ deadline := time.Now().Add(5 * time.Second)
+ for {
+ if _, err := os.Stat(started); err == nil {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("fixture child did not start")
+ }
+ time.Sleep(time.Millisecond)
+ }
+ if err := r.Close(); err != nil {
+ t.Fatal(err)
+ }
+ if err := <-result; !errors.Is(err, context.Canceled) {
+ t.Fatalf("startup cancellation lost: %v", err)
+ }
+ configPath, err := os.ReadFile(started)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(string(configPath)); !os.IsNotExist(err) {
+ t.Fatalf("Close returned before child/config retirement: %v", err)
+ }
+ select {
+ case <-platform.terminated:
+ t.Fatal("explicit close reported an unexpected dependency exit")
+ default:
+ }
+}
diff --git a/mobile/cloudflared/README.md b/mobile/cloudflared/README.md
new file mode 100644
index 00000000..45746bdb
--- /dev/null
+++ b/mobile/cloudflared/README.md
@@ -0,0 +1,37 @@
+# Android cloudflared adapter
+
+`scripts/build-android.mjs` builds the official cloudflared source pinned by
+`scripts/app-package-targets.mjs` and `licenses/upstream.json`. Its archive is
+SHA256 checked. Original extracted source remains unchanged in the ignored build
+cache; Go overlays add the Piik MIT-licensed Android adapter and apply the small
+supervisor patch. Neither module graph nor Go toolchain is patched.
+
+`android_dns.go` sends stock Go Resolver DNS messages through Android API 29
+`android_res_nsend/nresult/cancel`. It keeps Android's default network, VPN and
+Private DNS handling. Go retains DNS parsing, retry and SRV ordering. The NDK
+result FD has one owner: `nresult` consumes it, or cancellation closes it, never
+both. A context wake pipe interrupts waiting without polling or a sampling timer.
+Closing the Go connection cancels and joins its relay.
+
+`edge-fallback.patch` makes an exhausted QUIC dial failure enter the existing
+HTTP2 fallback, including quic-go's separate `HandshakeTimeoutError`. Address
+retry limits and explicit protocol selection remain upstream-owned. The focused
+test is overlaid into the upstream package; it does not ship in the helper.
+The patch applies only to the Android source build. Desktop packages still ship
+the official prebuilt cloudflared executable and do not include this correction.
+
+Both the full Android builder and `node scripts/build-android.mjs --tunnel-only`
+compile the focused fallback test to `build/android-cloudflared/fallback.test`
+(`.exe` on Windows) and run it before building the helpers. They do not run the
+full upstream test suite. The latter entry needs only the existing Go toolchain
+and `ANDROID_NDK_HOME`, and also performs the Android PIE/16KB ELF checks.
+
+Sources:
+
+- <https://developer.android.com/ndk/reference/group/networking>
+- <https://android.googlesource.com/platform/system/netd/+/refs/heads/android15-release/client/NetdClient.cpp>
+- <https://github.com/cloudflare/cloudflared/blob/de6376c5af4a67cbc65258682e5a01781827643d/supervisor/tunnel.go>
+
+The APK's existing third-party notice includes the pinned cloudflared source and
+dependency licenses. Generated source overlays, probes and executables remain
+ignored. No test command or environment switch is added to the shipped CLI.
diff --git a/mobile/cloudflared/android_dns.go b/mobile/cloudflared/android_dns.go
new file mode 100644
index 00000000..d4ac0fe4
--- /dev/null
+++ b/mobile/cloudflared/android_dns.go
@@ -0,0 +1,163 @@
+//go:build android && piik_cloudflared
+
+// Piik's Android adapter for the pinned cloudflared executable. See README.md.
+package main
+
+/*
+#cgo LDFLAGS: -landroid
+#include <android/multinetwork.h>
+#include <errno.h>
+#include <poll.h>
+#include <stdint.h>
+
+static int piik_dns_wait(int query, int cancel) {
+ struct pollfd fds[2] = {{query, POLLIN, 0}, {cancel, POLLIN, 0}};
+ int result;
+ do { result = poll(fds, 2, -1); } while (result < 0 && errno == EINTR);
+ if (result < 0) return -errno;
+ return fds[1].revents ? 0 : 1;
+}
+*/
+import "C"
+
+import (
+ "context"
+ "encoding/binary"
+ "io"
+ "net"
+ "os"
+ "sync"
+ "syscall"
+ "unsafe"
+)
+
+func init() {
+ // A/AAAA via libc alone is insufficient: Go's SRV/TXT lookups use its DNS
+ // client. Route that client's raw messages through Android's system resolver.
+ net.DefaultResolver = &net.Resolver{PreferGo: true, Dial: dialAndroidDNS}
+}
+
+type androidDNSConn struct {
+ net.Conn
+ ctx context.Context
+ cancel context.CancelFunc
+ done chan struct{}
+ mu sync.Mutex
+ err error
+}
+
+func (c *androidDNSConn) Read(p []byte) (int, error) {
+ n, err := c.Conn.Read(p)
+ if err != nil {
+ c.mu.Lock()
+ if c.err != nil {
+ err = c.err
+ } else if c.ctx.Err() != nil {
+ err = c.ctx.Err()
+ }
+ c.mu.Unlock()
+ }
+ return n, err
+}
+
+func (c *androidDNSConn) Close() error {
+ c.cancel()
+ err := c.Conn.Close()
+ <-c.done
+ return err
+}
+
+func dialAndroidDNS(parent context.Context, _, _ string) (net.Conn, error) {
+ if err := parent.Err(); err != nil {
+ return nil, err
+ }
+ ctx, cancel := context.WithCancel(parent)
+ client, server := net.Pipe()
+ c := &androidDNSConn{Conn: client, ctx: ctx, cancel: cancel, done: make(chan struct{})}
+ go func() {
+ defer close(c.done)
+ defer server.Close()
+ defer cancel()
+ stop := context.AfterFunc(ctx, func() { _ = server.Close() })
+ defer stop()
+ err := relayAndroidDNS(ctx, server)
+ if ctx.Err() != nil {
+ err = ctx.Err()
+ }
+ c.mu.Lock()
+ c.err = err
+ c.mu.Unlock()
+ }()
+ return c, nil
+}
+
+func relayAndroidDNS(ctx context.Context, stream net.Conn) error {
+ // net.Pipe is a stream, so stock net.Resolver supplies the DNS TCP length
+ // prefix even when its original Dial request says UDP. No DNS parsing here.
+ var header [2]byte
+ if _, err := io.ReadFull(stream, header[:]); err != nil {
+ return err
+ }
+ query := make([]byte, int(binary.BigEndian.Uint16(header[:])))
+ if len(query) == 0 {
+ return syscall.EINVAL
+ }
+ if _, err := io.ReadFull(stream, query); err != nil {
+ return err
+ }
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+ // NETWORK_UNSPECIFIED preserves the process/default network, including VPN
+ // and Android Private DNS. The returned descriptor has exactly one owner.
+ fd := C.android_res_nsend(C.NETWORK_UNSPECIFIED, (*C.uint8_t)(unsafe.Pointer(&query[0])), C.size_t(len(query)), 0)
+ if fd < 0 {
+ return syscall.Errno(-fd)
+ }
+ answer, err := readAndroidDNS(ctx, fd)
+ if err != nil {
+ return err
+ }
+ binary.BigEndian.PutUint16(header[:], uint16(len(answer)))
+ if _, err = stream.Write(header[:]); err != nil {
+ return err
+ }
+ _, err = stream.Write(answer)
+ return err
+}
+
+// readAndroidDNS owns fd on entry. nresult closes it on every return; every
+// other path calls cancel (which also closes it). They never run concurrently.
+func readAndroidDNS(ctx context.Context, fd C.int) ([]byte, error) {
+ defer func() {
+ if fd >= 0 {
+ C.android_res_cancel(fd)
+ }
+ }()
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ r, w, err := os.Pipe()
+ if err != nil {
+ return nil, err
+ }
+ defer r.Close()
+ defer w.Close()
+ stop := context.AfterFunc(ctx, func() { _, _ = w.Write([]byte{1}) })
+ defer stop()
+ ready := C.piik_dns_wait(fd, C.int(r.Fd()))
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ if ready < 0 {
+ return nil, syscall.Errno(-ready)
+ }
+ answer := make([]byte, 65535)
+ var rcode C.int
+ n := C.android_res_nresult(fd, &rcode, (*C.uint8_t)(unsafe.Pointer(&answer[0])), C.size_t(len(answer)))
+ fd = -1 // nresult consumed ownership, including errors.
+ if n < 0 {
+ return nil, syscall.Errno(-n)
+ }
+ return answer[:int(n)], nil
+}
diff --git a/mobile/cloudflared/edge-fallback.patch b/mobile/cloudflared/edge-fallback.patch
new file mode 100644
index 00000000..f724a5c2
--- /dev/null
+++ b/mobile/cloudflared/edge-fallback.patch
@@ -0,0 +1,10 @@
+diff --git a/supervisor/tunnel.go b/supervisor/tunnel.go
+--- a/supervisor/tunnel.go
++++ b/supervisor/tunnel.go
+@@ -373,0 +374,6 @@ func isQuicBroken(cause error) bool {
++ // Serve reaches protocol selection for a dial failure only after
++ // IpAddrFallback has exhausted the configured edge-address retries.
++ // HandshakeTimeoutError is distinct from IdleTimeoutError in quic-go.
++ if _, ok := errors.AsType[*connection.EdgeQuicDialError](cause); ok {
++ return true
++ }
diff --git a/mobile/cloudflared/fallback_test.go b/mobile/cloudflared/fallback_test.go
new file mode 100644
index 00000000..93a6777f
--- /dev/null
+++ b/mobile/cloudflared/fallback_test.go
@@ -0,0 +1,43 @@
+//go:build piik_cloudflared
+
+// Compiled by overlay into the pinned upstream supervisor package, not Piik.
+package supervisor
+
+import (
+ "testing"
+ "time"
+
+ "github.com/cloudflare/cloudflared/connection"
+ "github.com/cloudflare/cloudflared/retry"
+ "github.com/quic-go/quic-go"
+ "github.com/rs/zerolog"
+)
+
+func TestPiikExhaustedQuicHandshakeFallsBack(t *testing.T) {
+ log := zerolog.Nop()
+ for _, mode := range []string{"auto", "quic"} {
+ t.Run(mode, func(t *testing.T) {
+ selector, err := connection.NewProtocolSelector(mode, &log)
+ if err != nil {
+ t.Fatal(err)
+ }
+ failure := &connection.EdgeQuicDialError{Cause: &quic.HandshakeTimeoutError{}}
+ rotate, classified := NewIPAddrFallback(0).ShouldGetNewAddress(0, failure)
+ connectivity, ok := classified.(*ConnectivityError)
+ if !rotate || !ok || !connectivity.HasReachedMaxRetries() {
+ t.Fatal("first handshake did not exhaust the configured edge retry budget")
+ }
+ backoff := retry.NewBackoff(5, time.Millisecond, true)
+ pending := &protocolFallback{backoff, connection.QUIC, false}
+ pending.BackoffTimer() // Serve has waited its first backoff before selecting.
+ selectNextProtocol(&log, pending, selector, failure)
+ want := connection.QUIC
+ if mode == "auto" {
+ want = connection.HTTP2
+ }
+ if pending.protocol != want {
+ t.Fatalf("protocol after first exhausted handshake = %s, want %s", pending.protocol, want)
+ }
+ })
+ }
+}
diff --git a/mobile/tools.go b/mobile/tools.go
new file mode 100644
index 00000000..e15ba362
--- /dev/null
+++ b/mobile/tools.go
@@ -0,0 +1,5 @@
+//go:build tools
+
+package tools
+
+import _ "golang.org/x/mobile/cmd/gobind"
diff --git a/native/capture/linux/main.c b/native/capture/linux/main.c
index 11885bf8..b6e8b47b 100644
--- a/native/capture/linux/main.c
+++ b/native/capture/linux/main.c
@@ -544,12 +544,16 @@ static gboolean configure_encoder(VideoOutput *output) {
}
set_numeric_property(G_OBJECT(output->encoder), "key-int-max",
output->profile.frame_rate * 2);
+ set_numeric_property(G_OBJECT(output->encoder), "gop-size",
+ output->profile.frame_rate * 2);
set_numeric_property(G_OBJECT(output->encoder), "b-frames", 0);
+ set_numeric_property(G_OBJECT(output->encoder), "bframes", 0);
set_numeric_property(G_OBJECT(output->encoder), "ref-frames", 1);
set_optional_boolean(G_OBJECT(output->encoder), "cabac", FALSE);
set_optional_boolean(G_OBJECT(output->encoder), "aud", TRUE);
set_optional_boolean(G_OBJECT(output->encoder), "zerolatency", TRUE);
set_optional_enum(G_OBJECT(output->encoder), "rate-control", "cbr");
+ set_optional_enum(G_OBJECT(output->encoder), "rc-mode", "cbr");
guint usage = output->profile.preference == kPreferResolution
? 1
: run->profile.preference == kPreferFramerate ? 7 : 4;
@@ -1118,12 +1122,18 @@ static int run_pipeline(CaptureRun *run, const char *starting_status) {
}
GstStateChangeReturn state =
gst_element_set_state(run->pipeline, GST_STATE_PLAYING);
- if (state == GST_STATE_CHANGE_FAILURE) {
- fail_run(run, "GStreamer pipeline failed to start");
- goto stopped;
- }
- if (gst_element_get_state(run->pipeline, NULL, NULL, 5 * GST_SECOND) ==
- GST_STATE_CHANGE_FAILURE) {
+ if (state == GST_STATE_CHANGE_FAILURE ||
+ gst_element_get_state(run->pipeline, NULL, NULL, 5 * GST_SECOND) ==
+ GST_STATE_CHANGE_FAILURE) {
+ // State changes can fail before the main loop dispatches the queued error.
+ // Preserve the same primary cause and diagnostics as a live pipeline error.
+ GstBus *failed_bus = gst_element_get_bus(run->pipeline);
+ GstMessage *message = gst_bus_pop_filtered(failed_bus, GST_MESSAGE_ERROR);
+ if (message != NULL) {
+ bus_message(failed_bus, message, run);
+ gst_message_unref(message);
+ }
+ gst_object_unref(failed_bus);
fail_run(run, "GStreamer pipeline failed to start");
goto stopped;
}
diff --git a/native/capture/linux/main.test.c b/native/capture/linux/main.test.c
index ad496885..60c049df 100644
--- a/native/capture/linux/main.test.c
+++ b/native/capture/linux/main.test.c
@@ -4,26 +4,44 @@
#undef main
#include <gst/video/video.h>
-typedef struct { GstElement parent; guint bitrate; } FixtureEncoder;
+typedef struct {
+ GstElement parent;
+ guint bitrate;
+ gint gop_size;
+ guint bframes;
+ gint rc_mode;
+} FixtureEncoder;
typedef struct { GstElementClass parent; } FixtureEncoderClass;
G_DEFINE_TYPE(FixtureEncoder, fixture_encoder, GST_TYPE_ELEMENT)
static void fixture_set_property(GObject *object, guint id,
const GValue *value, GParamSpec *property) {
- (void)id;
(void)property;
- ((FixtureEncoder *)object)->bitrate = g_value_get_uint(value);
+ FixtureEncoder *encoder = (FixtureEncoder *)object;
+ switch (id) {
+ case 1: encoder->bitrate = g_value_get_uint(value); break;
+ case 2: encoder->gop_size = g_value_get_int(value); break;
+ case 3: encoder->bframes = g_value_get_uint(value); break;
+ case 4: encoder->rc_mode = g_value_get_enum(value); break;
+ }
}
static void fixture_get_property(GObject *object, guint id,
GValue *value, GParamSpec *property) {
- (void)id;
(void)property;
- g_value_set_uint(value, ((FixtureEncoder *)object)->bitrate);
+ FixtureEncoder *encoder = (FixtureEncoder *)object;
+ switch (id) {
+ case 1: g_value_set_uint(value, encoder->bitrate); break;
+ case 2: g_value_set_int(value, encoder->gop_size); break;
+ case 3: g_value_set_uint(value, encoder->bframes); break;
+ case 4: g_value_set_enum(value, encoder->rc_mode); break;
+ }
}
static void fixture_encoder_init(FixtureEncoder *encoder) {
encoder->bitrate = 3000000;
+ encoder->gop_size = 75;
+ encoder->bframes = 2;
}
static void fixture_encoder_class_init(FixtureEncoderClass *klass) {
@@ -33,6 +51,16 @@ static void fixture_encoder_class_init(FixtureEncoderClass *klass) {
g_object_class_install_property(object_class, 1,
g_param_spec_uint("bitrate", "Bitrate", "Bitrate in bps", 1000, 50000000,
3000000, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING));
+ g_object_class_install_property(object_class, 2,
+ g_param_spec_int("gop-size", "GOP size", "Number of frames between intra frames",
+ -1, G_MAXINT, 75, G_PARAM_READWRITE));
+ g_object_class_install_property(object_class, 3,
+ g_param_spec_uint("bframes", "B frames", "Number of B frames", 0, 4, 2,
+ G_PARAM_READWRITE));
+ static const GEnumValue modes[] = {{0, "Default", "default"}, {1, "CBR", "cbr"}, {0, NULL, NULL}};
+ g_object_class_install_property(object_class, 4,
+ g_param_spec_enum("rc-mode", "RC mode", "Rate control mode",
+ g_enum_register_static("PiikFixtureRateControl", modes), 0, G_PARAM_READWRITE));
}
static void check_encoder_admission_and_control(void) {
@@ -53,6 +81,12 @@ static void check_encoder_admission_and_control(void) {
run.outputs[0] = (VideoOutput){.run = &run, .encoder = encoder,
.profile = {.bit_rate = 3000000, .frame_rate = 30}};
g_assert_true(configure_encoder(&run.outputs[0]));
+ gint gop_size = 0, rc_mode = 0;
+ guint bframes = 0;
+ g_object_get(encoder, "gop-size", &gop_size, "bframes", &bframes, "rc-mode", &rc_mode, NULL);
+ g_assert_cmpint(gop_size, ==, 60);
+ g_assert_cmpuint(bframes, ==, 0);
+ g_assert_cmpint(rc_mode, ==, 1);
g_assert_true(apply_control(&run, "B 0 3000000"));
g_assert_true(apply_control(&run, "B 0 1000000"));
guint applied = 0;
@@ -121,6 +155,31 @@ static void check_bus_error_retains_primary_cause(void) {
g_mutex_clear(&run.lock);
}
+static void check_startup_error_retains_primary_cause(void) {
+ char *missing = NULL;
+ int file = g_file_open_tmp("piik-missing-source-XXXXXX", &missing, NULL);
+ g_assert_cmpint(file, >=, 0);
+ close(file);
+ g_assert_cmpint(unlink(missing), ==, 0);
+ CaptureRun run = {.loop = g_main_loop_new(NULL, FALSE),
+ .pipeline = gst_parse_launch("filesrc name=source ! fakesink", NULL)};
+ g_assert_nonnull(run.pipeline);
+ g_mutex_init(&run.lock);
+ GstElement *source = gst_bin_get_by_name(GST_BIN(run.pipeline), "source");
+ g_assert_nonnull(source);
+ g_object_set(source, "location", missing, NULL);
+ gst_object_unref(source);
+ g_assert_cmpint(run_pipeline(&run, NULL), ==, 2);
+ char *expected = gst_error_get_message(GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND);
+ g_assert_cmpstr(run.failure, ==, expected);
+ g_free(expected);
+ g_free(missing);
+ g_free(run.failure);
+ gst_object_unref(run.pipeline);
+ g_main_loop_unref(run.loop);
+ g_mutex_clear(&run.lock);
+}
+
static void check_slot_profiles(void) {
char *arguments[23 + 7 * 5] = {
"capture", "--capture-video", "picker", "1", "0", "0",
@@ -659,6 +718,7 @@ int main(int argc, char **argv) {
check_pipewire_target_identity();
check_encoder_admission_and_control();
check_bus_error_retains_primary_cause();
+ check_startup_error_retains_primary_cause();
check_slot_profiles();
check_desktop_source_pixel_aspect();
check_source_geometry(1920, 1080);
diff --git a/native/capture/windows/main.cpp b/native/capture/windows/main.cpp
index a98f765a..6e3a8e0c 100644
--- a/native/capture/windows/main.cpp
+++ b/native/capture/windows/main.cpp
@@ -1,5 +1,6 @@
#define NOMINMAX
#include <windows.h>
+#include <dwmapi.h>
#include <codecapi.h>
#include <d3d10_1.h>
@@ -1535,9 +1536,29 @@ struct CaptureInput final {
UINT32 width = 0;
UINT32 height = 0;
SIZE presentation{};
+ // Presentation advances on still refresh; content identity does not.
UINT64 timestamp = 0;
+ UINT64 capture_timestamp = 0;
};
+std::shared_ptr<const CaptureInput> RefreshCaptureInput(const CaptureInput& latest) {
+ auto input = std::make_shared<CaptureInput>(latest);
+ // WGC SystemRelativeTime and MFGetSystemTime share the 100 ns QPC clock.
+ // A requested still frame is presented now, including after a quiet gap.
+ input->timestamp = static_cast<UINT64>(MFGetSystemTime());
+ return input;
+}
+
+std::optional<UINT64> CapturePresentationTimestamp(
+ UINT64 captured, UINT64 previous_capture, UINT64 previous_presentation,
+ UINT64 minimum_capture) {
+ if (captured <= previous_capture || captured < minimum_capture) return std::nullopt;
+ // A real frame can have been captured before a requested still was presented.
+ // Retain its new pixels and keep separate access units at least one RTP tick apart.
+ constexpr UINT64 rtp_tick_100ns = (10'000'000 + 90'000 - 1) / 90'000;
+ return std::max(captured, previous_presentation + rtp_tick_100ns);
+}
+
class OutputWorker final {
public:
using Factory = AdaptiveEncoder::Factory;
@@ -1545,10 +1566,12 @@ class OutputWorker final {
ProtocolWriter& writer, Factory create,
std::unique_ptr<VideoEncoder> initial, bool active,
std::function<void(const AdaptiveAccessUnit&)> on_output,
- std::function<void(std::exception_ptr)> on_failure)
+ std::function<void(std::exception_ptr)> on_failure,
+ std::function<void()> on_refresh = {})
: layer_(layer), profile_(profile), kind_(kind), device_(device), writer_(writer),
create_(std::move(create)), initial_(std::move(initial)),
on_output_(std::move(on_output)), on_failure_(std::move(on_failure)),
+ on_refresh_(std::move(on_refresh)),
mailbox_(profile.bit_rate, active), thread_([this]() { Run(); }) {}
~OutputWorker() { Stop(); Join(); }
@@ -1586,6 +1609,7 @@ class OutputWorker final {
Check(CoInitializeEx(nullptr, COINIT_MULTITHREADED), "output-com-apartment");
apartment = true;
UINT64 codec_generation = 0;
+ std::optional<std::pair<UINT64, UINT32>> delivered_input;
UINT32 converted_width = 0, converted_height = 0;
for (;;) {
const auto work = mailbox_.Take();
@@ -1594,13 +1618,17 @@ class OutputWorker final {
encoder.reset();
initial.reset();
converter.reset();
+ delivered_input.reset();
continue;
}
const auto& input = work.input;
+ // Quiet pixels still need publication at a newly requested encoder budget.
+ const auto requested_input = std::pair{input->capture_timestamp, work.bitrate};
if (codec_generation != work.generation) {
encoder.reset();
converter.reset();
codec_generation = work.generation;
+ delivered_input.reset();
}
std::optional<AdaptiveAccessUnit> output;
try {
@@ -1636,7 +1664,13 @@ class OutputWorker final {
converter.reset();
continue;
}
- if (!output) continue;
+ auto request_refresh = [&] {
+ if (on_refresh_ && mailbox_.Retry(work.generation, work.recovery)) on_refresh_();
+ };
+ if (!output) {
+ if (delivered_input != requested_input || work.recovery) request_refresh();
+ continue;
+ }
if (!mailbox_.Accept(work.generation)) continue;
const auto& access_unit = output->access_unit;
Check(writer_.Write(kind_, access_unit.key_frame ? 1 : 0,
@@ -1644,7 +1678,9 @@ class OutputWorker final {
access_unit.bytes.data(), static_cast<DWORD>(access_unit.bytes.size()),
layer_, static_cast<UINT16>(output->width),
static_cast<UINT16>(output->height)), "capture-video-output");
+ delivered_input = requested_input;
if (on_output_) on_output_(*output);
+ if (work.recovery && !access_unit.key_frame) request_refresh();
}
} catch (...) {
if (mailbox_.Fail()) on_failure_(std::current_exception());
@@ -1664,6 +1700,7 @@ class OutputWorker final {
std::unique_ptr<VideoEncoder> initial_;
std::function<void(const AdaptiveAccessUnit&)> on_output_;
std::function<void(std::exception_ptr)> on_failure_;
+ std::function<void()> on_refresh_;
using Mailbox = piik::capture::OutputMailbox<CaptureInput>;
Mailbox mailbox_;
std::thread thread_;
@@ -1719,7 +1756,8 @@ std::vector<std::unique_ptr<OutputWorker>> CreateOutputWorkers(
const ProductArguments& arguments, const Adapter& adapter, const DeviceContext& device,
ProtocolWriter& writer, VideoEncoderSelection encoder,
const std::function<void(const AdaptiveAccessUnit&)>& on_active,
- const std::function<void(size_t, std::exception_ptr)>& on_failure) {
+ const std::function<void(size_t, std::exception_ptr)>& on_failure,
+ const std::function<void()>& on_refresh = {}) {
const bool hardware = encoder.kind == OutputKind::h264;
const UINT encoder_index = arguments.mft_index;
const bool capture = arguments.mode == ProductArguments::Mode::video;
@@ -1740,7 +1778,7 @@ std::vector<std::unique_ptr<OutputWorker>> CreateOutputWorkers(
static_cast<UINT8>(layer), profile, encoder.kind, device.device.Get(), writer,
std::move(create), original ? std::move(encoder.initial) : nullptr, capture && layer < 2,
[original, on_active](const AdaptiveAccessUnit& output) { if (original) on_active(output); },
- [layer, on_failure](std::exception_ptr error) { on_failure(layer, error); }));
+ [layer, on_failure](std::exception_ptr error) { on_failure(layer, error); }, on_refresh));
}
return workers;
}
@@ -1909,6 +1947,7 @@ void RunEncodedVideo(ProductArguments arguments) {
input->height = height;
input->presentation = {static_cast<LONG>(width), static_cast<LONG>(height)};
input->timestamp = timestamp;
+ input->capture_timestamp = timestamp;
Check(writer.WriteBegin(timestamp, duration), "decoded-frame-begin");
for (auto& worker : workers) worker->Submit(input);
began = true;
@@ -1984,6 +2023,17 @@ void RunVideoCapture(ProductArguments arguments) {
arguments.source_id, arguments.pid, arguments.creation_time)
: piik::capture::ValidateDisplayTarget(arguments.source_id),
"target-identity-before-capture");
+ if (window_target) {
+ DWORD affinity = 0;
+ const bool available = GetWindowDisplayAffinity(
+ reinterpret_cast<HWND>(static_cast<UINT_PTR>(arguments.source_id)), &affinity);
+ const DWORD error = available ? ERROR_SUCCESS : GetLastError();
+ std::osyncstream diagnostic(std::cerr);
+ diagnostic << "result=capture-window-policy";
+ if (available) diagnostic << " display-affinity=0x" << std::hex << affinity;
+ else diagnostic << " display-affinity=unknown error=" << error;
+ diagnostic << '\n';
+ }
auto capture_device = CreateCaptureDevice(device.device.Get());
GraphicsCaptureItem item = window_target
? CreateCaptureItem(reinterpret_cast<HWND>(
@@ -2021,6 +2071,7 @@ void RunVideoCapture(ProductArguments arguments) {
piik::capture::CaptureEventHandler(frame_ready));
auto closed_token = item.Closed(piik::capture::CaptureEventHandler(shutdown));
std::vector<std::unique_ptr<OutputWorker>> workers;
+ std::atomic<bool> refresh_requested{false};
std::atomic<bool> active_status_written{false};
std::mutex failure_mutex;
std::exception_ptr failure;
@@ -2070,11 +2121,17 @@ void RunVideoCapture(ProductArguments arguments) {
return;
}
if (FAILED(writer.WriteUnavailable(static_cast<UINT8>(layer), OutputFailureDetail(layer, error)))) fail_capture(error);
+ }, [&] {
+ refresh_requested.store(true);
+ SetEvent(frame_ready->get());
});
if (!arguments.show_capture_border) border.Start(capture_session);
capture_session.StartCapture();
UINT64 previous_timestamp = 0;
+ UINT64 previous_capture_timestamp = 0;
+ UINT64 minimum_capture_timestamp = 0;
+ bool window_minimized = false;
UINT64 next_output_timestamp = 0;
UINT64 source_frames = 0;
auto pool_size = initial_size;
@@ -2092,6 +2149,7 @@ void RunVideoCapture(ProductArguments arguments) {
process.get(), shutdown->get(), frame_ready->get()};
const HANDLE display_waits[] = {shutdown->get(), frame_ready->get()};
for (;;) {
+ refresh_input = refresh_requested.exchange(false) || refresh_input;
for (const auto& control : controls.Read()) {
if (control.kind == 'Q') {
cleanup();
@@ -2105,6 +2163,18 @@ void RunVideoCapture(ProductArguments arguments) {
refresh_input = true;
}
border.Apply(capture_session);
+ if (window_target && !IsWindow(reinterpret_cast<HWND>(
+ static_cast<UINT_PTR>(arguments.source_id)))) {
+ Fail("capture-closed", "selected source stopped capture");
+ }
+ // WGC can deliver a minimized title-bar frame after the window is restored.
+ // Fence that suspended content by capture time, not current window geometry.
+ const bool minimized = window_target && IsIconic(reinterpret_cast<HWND>(
+ static_cast<UINT_PTR>(arguments.source_id)));
+ if (window_minimized && !minimized) {
+ minimum_capture_timestamp = static_cast<UINT64>(MFGetSystemTime());
+ }
+ window_minimized = minimized;
DWORD wait = window_target
? WaitForMultipleObjects(3, window_waits, FALSE, control_wait_ms)
: WaitForMultipleObjects(2, display_waits, FALSE, control_wait_ms);
@@ -2126,9 +2196,7 @@ void RunVideoCapture(ProductArguments arguments) {
if (wait == WAIT_TIMEOUT) {
// An explicit output/control request may reuse a quiet source; silence is not EOF.
if (refresh_input && latest_input) {
- auto input = std::make_shared<CaptureInput>(*latest_input);
- input->timestamp = previous_timestamp + frame_duration;
- submit(input);
+ submit(RefreshCaptureInput(*latest_input));
}
continue;
}
@@ -2154,23 +2222,14 @@ void RunVideoCapture(ProductArguments arguments) {
Fail("capture-timestamp", "captured frame has no QPC timestamp");
}
UINT64 timestamp = static_cast<UINT64>(signed_timestamp);
- if (previous_timestamp != 0 && timestamp <= previous_timestamp) continue;
- const UINT64 timing_tolerance = frame_duration / 20;
- if (next_output_timestamp != 0 && timestamp < next_output_timestamp &&
- next_output_timestamp - timestamp > timing_tolerance) {
- continue;
- }
+ const auto presentation_timestamp = CapturePresentationTimestamp(
+ timestamp, previous_capture_timestamp, previous_timestamp,
+ minimum_capture_timestamp);
+ if (!presentation_timestamp) continue;
auto content_size = latest.ContentSize();
if (content_size.Width <= 0 || content_size.Height <= 0) continue;
- // ContentSize belongs to the rendered frame; Size belongs to the current
- // target. Queued minimize/resize frames must not reconfigure the encoder
- // after the target has already changed again.
- const auto target_size = item.Size();
- if (content_size.Width != target_size.Width ||
- content_size.Height != target_size.Height) {
- latest.Close();
- continue;
- }
+ // ContentSize owns this rendered frame's geometry. Item/window bounds can
+ // differ on Windows 10; comparing them would discard every valid frame.
const auto color = display_color.Resolve();
if (content_size.Width != pool_size.Width ||
content_size.Height != pool_size.Height || color.Format() != pool_format) {
@@ -2195,11 +2254,20 @@ void RunVideoCapture(ProductArguments arguments) {
input->texture = sdr.Convert(source.Get(), color);
input->width = content_width;
input->height = content_height;
- input->timestamp = timestamp;
+ input->timestamp = *presentation_timestamp;
+ input->capture_timestamp = timestamp;
input->presentation = presentation.Resolve(content_width, content_height,
source_frames % arguments.profile.gop_frames() == 0);
latest.Close();
latest = nullptr;
+ previous_capture_timestamp = timestamp;
+ latest_input = input;
+ const UINT64 timing_tolerance = frame_duration / 20;
+ if (next_output_timestamp != 0 && timestamp < next_output_timestamp &&
+ next_output_timestamp - timestamp > timing_tolerance) {
+ refresh_input = true;
+ continue;
+ }
if (next_output_timestamp == 0) {
next_output_timestamp = timestamp + frame_duration;
} else {
@@ -2209,7 +2277,6 @@ void RunVideoCapture(ProductArguments arguments) {
next_output_timestamp +=
(elapsed / frame_duration + 1) * frame_duration;
}
- latest_input = input;
submit(input);
}
} catch (...) {
diff --git a/native/capture/windows/output_mailbox.h b/native/capture/windows/output_mailbox.h
index 83c85ff5..3988b90b 100644
--- a/native/capture/windows/output_mailbox.h
+++ b/native/capture/windows/output_mailbox.h
@@ -77,6 +77,15 @@ class OutputMailbox final {
return !stopped_ && !failed_ && active_ && generation == generation_;
}
+ // A dropped input still needs a fresh presentation from the capture owner.
+ // Retain its recovery intent without admitting work from a retired activation.
+ bool Retry(uint64_t generation, bool recovery) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (stopped_ || failed_ || !active_ || generation != generation_) return false;
+ key_requested_ = key_requested_ || recovery;
+ return true;
+ }
+
bool Fail(std::optional<uint64_t> generation = std::nullopt) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_ || failed_ ||
diff --git a/native/capture/windows/output_worker.test.cpp b/native/capture/windows/output_worker.test.cpp
index 0f5f6d25..4741fb77 100644
--- a/native/capture/windows/output_worker.test.cpp
+++ b/native/capture/windows/output_worker.test.cpp
@@ -10,6 +10,11 @@
#include <future>
#include <iterator>
+#define WEBRTC_WIN 1
+#define NDEBUG
+#include "api/video/video_adapter.h"
+#undef NDEBUG
+
namespace piik::capture::windows {
class AdaptiveEncoder::Impl final {
public:
@@ -27,22 +32,24 @@ std::optional<AdaptiveAccessUnit> AdaptiveEncoder::Encode(FrameProducer,
UINT32 width, UINT32 height, UINT64 timestamp, bool key, UINT32 bitrate,
EncoderClock::time_point deadline) {
impl_->encoder->SetBitrate(bitrate);
- return AdaptiveAccessUnit{impl_->encoder->Encode(nullptr, timestamp, key, deadline),
- width, height, 333'333};
+ auto output = impl_->encoder->Encode(nullptr, timestamp, key, deadline);
+ if (output.bytes.empty()) return std::nullopt;
+ return AdaptiveAccessUnit{std::move(output), width, height, 333'333};
}
} // namespace piik::capture::windows
class FixtureEncoder final : public VideoEncoder {
public:
- FixtureEncoder(std::promise<void>* entered, std::shared_future<void> released)
+ FixtureEncoder(std::promise<void>* entered, std::shared_future<void> released, bool drop = false)
: VideoEncoder(OutputKind::h264, "fixture", "fixture"),
- entered_(entered), released_(std::move(released)) {}
+ entered_(entered), released_(std::move(released)), drop_(drop) {}
EncodedAccessUnit Encode(ID3D11Texture2D*, UINT64 timestamp, bool key,
EncoderClock::time_point) override {
if (entered_) {
entered_->set_value();
released_.wait();
+ if (drop_) return {timestamp, false, {}};
throw GateFailure("fixture-encode", "synthetic encoder failed", E_FAIL);
}
return {timestamp, key, {1, 2, 3}};
@@ -52,8 +59,178 @@ class FixtureEncoder final : public VideoEncoder {
private:
std::promise<void>* entered_;
std::shared_future<void> released_;
+ bool drop_;
+};
+
+void CheckQuietCaptureRefresh() {
+ CaptureInput latest;
+ latest.width = 1280;
+ latest.height = 720;
+ latest.presentation = {1280, 720};
+ latest.timestamp = static_cast<UINT64>(MFGetSystemTime()) - 50'000'000;
+ latest.capture_timestamp = latest.timestamp;
+ const auto previous = latest.timestamp;
+ const auto before = static_cast<UINT64>(MFGetSystemTime());
+ const auto refreshed = RefreshCaptureInput(latest);
+ const auto after = static_cast<UINT64>(MFGetSystemTime());
+ assert(refreshed->timestamp >= before && refreshed->timestamp <= after);
+ assert(refreshed->timestamp - previous >= 50'000'000);
+ assert(latest.timestamp == previous);
+ assert(refreshed->capture_timestamp == latest.capture_timestamp);
+ assert(refreshed->width == latest.width && refreshed->height == latest.height &&
+ refreshed->presentation.cx == latest.presentation.cx &&
+ refreshed->presentation.cy == latest.presentation.cy &&
+ refreshed->color_space == latest.color_space && refreshed->texture.Get() == latest.texture.Get());
+ // The compositor changed the picture just before the refresh, but its frame
+ // callback arrives afterwards. It is new capture input, not a stale still.
+ const auto delayed_capture = before - 1;
+ const auto following = CapturePresentationTimestamp(delayed_capture, previous, refreshed->timestamp, 0);
+ assert(following && *following > refreshed->timestamp);
+ assert((*following - refreshed->timestamp) * 90'000 >= 10'000'000);
+ assert(!CapturePresentationTimestamp(previous, previous, refreshed->timestamp, 0));
+ const auto current_capture = after + 10'000'000;
+ const auto current = CapturePresentationTimestamp(current_capture, delayed_capture, *following, 0);
+ assert(current && *current == current_capture);
+ // Minimized title-bar frames can arrive after restore. Their timestamps are
+ // newer than the last real picture, but older than the observed restoration.
+ const auto restored_at = current_capture + 10'000'000;
+ assert(!CapturePresentationTimestamp(restored_at - 1, current_capture, *current, restored_at));
+ const auto restored = CapturePresentationTimestamp(restored_at + 1, current_capture, *current, restored_at);
+ assert(restored && *restored == restored_at + 1);
+}
+
+class CadenceEncoder final : public VideoEncoder {
+ public:
+ explicit CadenceEncoder(int fps) : VideoEncoder(OutputKind::h264, "cadence", "cadence") {
+ adapter_.OnOutputFormatRequest(std::nullopt, 64 * 64, fps);
+ }
+ EncodedAccessUnit Encode(ID3D11Texture2D*, UINT64 timestamp, bool key,
+ EncoderClock::time_point) override {
+ int crop_width, crop_height, width, height;
+ if (!adapter_.AdaptFrameResolution(64, 64, static_cast<int64_t>(timestamp * 100),
+ &crop_width, &crop_height, &width, &height)) return {timestamp, false, {}};
+ if (key) ++key_requests_;
+ return {timestamp, key && key_requests_ != 2,
+ {static_cast<BYTE>(bitrate_ >> 24), static_cast<BYTE>(bitrate_ >> 16),
+ static_cast<BYTE>(bitrate_ >> 8), static_cast<BYTE>(bitrate_)}};
+ }
+ void SetBitrate(UINT32 bitrate) override { bitrate_ = bitrate; }
+ private:
+ webrtc::VideoAdapter adapter_{2};
+ unsigned key_requests_ = 0;
+ UINT32 bitrate_ = 0;
};
+void CheckRetainedContentAtAdaptedCadence(ID3D11Device* device) {
+ HANDLE sink = CreateFileW(L"NUL", GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
+ assert(sink != INVALID_HANDLE_VALUE);
+ ProtocolWriter writer(sink);
+ const VideoProfile profile{64, 64, 30, 300'000};
+ std::atomic<bool> refresh{false};
+ std::atomic<UINT64> changed_at{UINT64_MAX};
+ std::array<std::atomic<unsigned>, 2> delivered{}, changed{}, keys{};
+ std::array<std::atomic<UINT32>, 2> published_bitrate{};
+ std::vector<std::unique_ptr<OutputWorker>> workers;
+ for (int layer = 0; layer < 2; ++layer) {
+ workers.push_back(std::make_unique<OutputWorker>(static_cast<UINT8>(layer), profile,
+ OutputKind::h264, device, writer, [layer](const VideoProfile&) {
+ return std::make_unique<CadenceEncoder>(layer == 0 ? 3 : 11);
+ }, nullptr, true, [&, layer](const AdaptiveAccessUnit& output) {
+ ++delivered[layer];
+ if (output.access_unit.timestamp100ns >= changed_at.load()) ++changed[layer];
+ if (output.access_unit.key_frame) ++keys[layer];
+ const auto& bytes = output.access_unit.bytes;
+ published_bitrate[layer].store((static_cast<UINT32>(bytes[0]) << 24) |
+ (static_cast<UINT32>(bytes[1]) << 16) | (static_cast<UINT32>(bytes[2]) << 8) | bytes[3]);
+ }, [](std::exception_ptr error) { std::rethrow_exception(error); },
+ [&] { refresh.store(true); }));
+ }
+ auto input = std::make_shared<CaptureInput>();
+ input->width = input->height = 64;
+ input->presentation = {64, 64};
+ input->timestamp = static_cast<UINT64>(MFGetSystemTime());
+ input->capture_timestamp = input->timestamp;
+ auto submit = [&](std::shared_ptr<const CaptureInput> next) {
+ for (auto& worker : workers) worker->Submit(next);
+ };
+ const auto deadline = EncoderClock::now() + std::chrono::seconds(3);
+ auto wait = [&](auto done) {
+ while (!done()) {
+ assert(EncoderClock::now() < deadline);
+ if (refresh.exchange(false)) submit(RefreshCaptureInput(*input));
+ std::this_thread::sleep_for(std::chrono::milliseconds(34));
+ }
+ };
+ for (auto& worker : workers) worker->SetBitrate(profile.bit_rate / 2);
+ submit(input);
+ wait([&] { return delivered[0] > 0 && delivered[1] > 0; });
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ input = std::make_shared<CaptureInput>(*input);
+ input->timestamp = static_cast<UINT64>(MFGetSystemTime());
+ input->capture_timestamp = input->timestamp;
+ changed_at.store(input->timestamp);
+ submit(input);
+ wait([&] { return changed[0] > 0 && changed[1] > 0; });
+ // One rendition's drop must not reopen the other rendition's delivered content.
+ if (refresh.exchange(false)) submit(RefreshCaptureInput(*input));
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ assert(!refresh.load());
+ const auto before = keys[0].load();
+ workers[0]->RequestKeyFrame();
+ submit(RefreshCaptureInput(*input));
+ wait([&] { return keys[0] > before; });
+ if (refresh.exchange(false)) submit(RefreshCaptureInput(*input));
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ assert(!refresh.load());
+ workers[0]->SetBitrate(profile.bit_rate);
+ submit(RefreshCaptureInput(*input));
+ wait([&] { return published_bitrate[0] == profile.bit_rate; });
+ if (refresh.exchange(false)) submit(RefreshCaptureInput(*input));
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ assert(!refresh.load());
+ for (auto& worker : workers) worker->Stop();
+ for (auto& worker : workers) worker->Join();
+ CloseHandle(sink);
+}
+
+void CheckRetiredDropCannotRequestRefresh(ID3D11Device* device, bool stop) {
+ std::promise<void> entered, release, output;
+ auto began = entered.get_future();
+ auto released = release.get_future().share();
+ auto delivered = output.get_future();
+ std::atomic<bool> refresh{false};
+ std::atomic<unsigned> created{0};
+ HANDLE sink = CreateFileW(L"NUL", GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
+ assert(sink != INVALID_HANDLE_VALUE);
+ ProtocolWriter writer(sink);
+ OutputWorker worker(0, VideoProfile{}, OutputKind::h264, device, writer,
+ [&](const VideoProfile&) {
+ return std::make_unique<FixtureEncoder>(created++ == 0 ? &entered : nullptr, released, true);
+ },
+ nullptr, true, [&](const AdaptiveAccessUnit&) { output.set_value(); },
+ [](std::exception_ptr error) { std::rethrow_exception(error); },
+ [&] { refresh.store(true); });
+ auto input = std::make_shared<CaptureInput>();
+ input->width = 1280;
+ input->height = 720;
+ input->presentation = {1280, 720};
+ input->timestamp = input->capture_timestamp = static_cast<UINT64>(MFGetSystemTime());
+ worker.Submit(input);
+ assert(began.wait_for(std::chrono::seconds(2)) == std::future_status::ready);
+ if (stop) worker.Stop();
+ else {
+ worker.SetActive(false);
+ assert(worker.SetActive(true));
+ worker.Submit(input);
+ }
+ release.set_value();
+ if (!stop) assert(delivered.wait_for(std::chrono::seconds(2)) == std::future_status::ready);
+ worker.Stop();
+ worker.Join();
+ assert(!refresh.load());
+ CloseHandle(sink);
+}
+
void CheckWorkerFailureGeneration(ID3D11Device* device, bool replace, bool active = true) {
std::promise<void> entered, release, output, failure;
auto began = entered.get_future();
@@ -568,6 +745,7 @@ int main() {
assert(!piik::capture::ValidCodedVideoSize(3840, 3840));
assert(!piik::capture::ValidCodedVideoSize(UINT32_MAX, UINT32_MAX));
CheckEncoderCandidates();
+ CheckQuietCaptureRefresh();
CheckH264ProfileCompatibility();
CheckPrimaryFailureDetails();
CheckAudioCapabilityIsolation();
@@ -584,4 +762,7 @@ int main() {
CheckWorkerDimensions(device.Get(), {1080, 1920}, {404, 720});
CheckWorkerDimensions(device.Get(), {3440, 1440}, {1280, 534});
CheckWorkerDimensions(device.Get(), {640, 480}, {640, 480});
+ CheckRetainedContentAtAdaptedCadence(device.Get());
+ CheckRetiredDropCannotRequestRefresh(device.Get(), false);
+ CheckRetiredDropCannotRequestRefresh(device.Get(), true);
}
diff --git a/native/third_party/webrtc-frame-dropper/LICENSE b/native/third_party/webrtc-frame-dropper/LICENSE
new file mode 100644
index 00000000..4c41b7b2
--- /dev/null
+++ b/native/third_party/webrtc-frame-dropper/LICENSE
@@ -0,0 +1,29 @@
+Copyright (c) 2011, The WebRTC project authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ * Neither the name of Google nor the names of its contributors may
+ be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/native/third_party/webrtc-frame-dropper/PATENTS b/native/third_party/webrtc-frame-dropper/PATENTS
new file mode 100644
index 00000000..190607ac
--- /dev/null
+++ b/native/third_party/webrtc-frame-dropper/PATENTS
@@ -0,0 +1,24 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Google as part of the WebRTC code package.
+
+Google hereby grants to you a perpetual, worldwide, non-exclusive,
+no-charge, irrevocable (except as stated in this section) patent
+license to make, have made, use, offer to sell, sell, import,
+transfer, and otherwise run, modify and propagate the contents of this
+implementation of the WebRTC code package, where such license applies
+only to those patent claims, both currently owned by Google and
+acquired in the future, licensable by Google that are necessarily
+infringed by this implementation of the WebRTC code package. This
+grant does not include claims that would be infringed only as a
+consequence of further modification of this implementation. If you or
+your agent or exclusive licensee institute or order or agree to the
+institution of patent litigation against any entity (including a
+cross-claim or counterclaim in a lawsuit) alleging that this
+implementation of the WebRTC code package or any code incorporated
+within this implementation of the WebRTC code package constitutes
+direct or contributory patent infringement, or inducement of patent
+infringement, then any patent rights granted to you under this License
+for this implementation of the WebRTC code package shall terminate as
+of the date such litigation is filed.
diff --git a/native/third_party/webrtc-frame-dropper/README.md b/native/third_party/webrtc-frame-dropper/README.md
new file mode 100644
index 00000000..a2324f73
--- /dev/null
+++ b/native/third_party/webrtc-frame-dropper/README.md
@@ -0,0 +1,12 @@
+# WebRTC encoder frame dropping
+
+These four unmodified upstream source files come from WebRTC commit
+`6f37672d358475cd17544121a12494da454d85fb`, the existing SDK pin in
+[webrtc-dependencies.json](../../capture/windows/webrtc-dependencies.json).
+Source: https://webrtc.googlesource.com/src/+/6f37672d358475cd17544121a12494da454d85fb/
+
+Android reuses FrameDropper and its ExpFilter dependency before encoder Surface
+submission when a platform encoder exceeds its allocation. They do not choose
+resolution, codec, media routes or transport budgets. The Android renderer owns
+one instance per physical encoder. Keep upstream code unchanged when updating
+the shared pin; LICENSE and PATENTS accompany this subset.
diff --git a/native/third_party/webrtc-frame-dropper/modules/video_coding/utility/frame_dropper.cc b/native/third_party/webrtc-frame-dropper/modules/video_coding/utility/frame_dropper.cc
new file mode 100644
index 00000000..4392fdc6
--- /dev/null
+++ b/native/third_party/webrtc-frame-dropper/modules/video_coding/utility/frame_dropper.cc
@@ -0,0 +1,270 @@
+/*
+ * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "modules/video_coding/utility/frame_dropper.h"
+
+#include <algorithm>
+#include <cstddef>
+#include <cstdint>
+
+namespace webrtc {
+
+namespace {
+
+const float kDefaultFrameSizeAlpha = 0.9f;
+const float kDefaultKeyFrameRatioAlpha = 0.99f;
+// 1 key frame every 10th second in 30 fps.
+const float kDefaultKeyFrameRatioValue = 1 / 300.0f;
+
+const float kDefaultDropRatioAlpha = 0.9f;
+const float kDefaultDropRatioValue = 0.96f;
+// Maximum duration over which frames are continuously dropped.
+const float kDefaultMaxDropDurationSecs = 4.0f;
+
+// Default target bitrate.
+// TODO(isheriff): Should this be higher to avoid dropping too many packets when
+// the bandwidth is unknown at the start ?
+const float kDefaultTargetBitrateKbps = 300.0f;
+const float kDefaultIncomingFrameRate = 30;
+const float kLeakyBucketSizeSeconds = 0.5f;
+
+// A delta frame that is bigger than `kLargeDeltaFactor` times the average
+// delta frame is a large frame that is spread out for accumulation.
+const int kLargeDeltaFactor = 3;
+
+// Cap on the frame size accumulator to prevent excessive drops.
+const float kAccumulatorCapBufferSizeSecs = 3.0f;
+} // namespace
+
+FrameDropper::FrameDropper()
+ : key_frame_ratio_(kDefaultKeyFrameRatioAlpha),
+ delta_frame_size_avg_kbits_(kDefaultFrameSizeAlpha),
+ drop_ratio_(kDefaultDropRatioAlpha, kDefaultDropRatioValue),
+ enabled_(true),
+ max_drop_duration_secs_(kDefaultMaxDropDurationSecs) {
+ Reset();
+}
+
+FrameDropper::~FrameDropper() = default;
+
+void FrameDropper::Reset() {
+ key_frame_ratio_.Reset(kDefaultKeyFrameRatioAlpha);
+ key_frame_ratio_.Apply(1.0f, kDefaultKeyFrameRatioValue);
+ delta_frame_size_avg_kbits_.Reset(kDefaultFrameSizeAlpha);
+
+ accumulator_ = 0.0f;
+ accumulator_max_ = kDefaultTargetBitrateKbps / 2;
+ target_bitrate_ = kDefaultTargetBitrateKbps;
+ incoming_frame_rate_ = kDefaultIncomingFrameRate;
+
+ large_frame_accumulation_count_ = 0;
+ large_frame_accumulation_chunk_size_ = 0;
+ large_frame_accumulation_spread_ = 0.5 * kDefaultIncomingFrameRate;
+
+ drop_next_ = false;
+ drop_ratio_.Reset(0.9f);
+ drop_ratio_.Apply(0.0f, 0.0f);
+ drop_count_ = 0;
+ was_below_max_ = true;
+}
+
+void FrameDropper::Enable(bool enable) {
+ enabled_ = enable;
+}
+
+void FrameDropper::Fill(size_t framesize_bytes, bool delta_frame) {
+ if (!enabled_) {
+ return;
+ }
+ float framesize_kbits = 8.0f * static_cast<float>(framesize_bytes) / 1000.0f;
+ if (!delta_frame) {
+ key_frame_ratio_.Apply(1.0, 1.0);
+ // Do not spread if we are already doing it (or we risk dropping bits that
+ // need accumulation). Given we compute the key frame ratio and spread
+ // based on that, this should not normally happen.
+ if (large_frame_accumulation_count_ == 0) {
+ if (key_frame_ratio_.filtered() > 1e-5 &&
+ 1 / key_frame_ratio_.filtered() < large_frame_accumulation_spread_) {
+ large_frame_accumulation_count_ =
+ static_cast<int32_t>(1 / key_frame_ratio_.filtered() + 0.5);
+ } else {
+ large_frame_accumulation_count_ =
+ static_cast<int32_t>(large_frame_accumulation_spread_ + 0.5);
+ }
+ large_frame_accumulation_chunk_size_ =
+ framesize_kbits / large_frame_accumulation_count_;
+ framesize_kbits = 0;
+ }
+ } else {
+ // Identify if it is an unusually large delta frame and spread accumulation
+ // if that is the case.
+ if (delta_frame_size_avg_kbits_.filtered() != -1 &&
+ (framesize_kbits >
+ kLargeDeltaFactor * delta_frame_size_avg_kbits_.filtered()) &&
+ large_frame_accumulation_count_ == 0) {
+ large_frame_accumulation_count_ =
+ static_cast<int32_t>(large_frame_accumulation_spread_ + 0.5);
+ large_frame_accumulation_chunk_size_ =
+ framesize_kbits / large_frame_accumulation_count_;
+ framesize_kbits = 0;
+ } else {
+ delta_frame_size_avg_kbits_.Apply(1, framesize_kbits);
+ }
+ key_frame_ratio_.Apply(1.0, 0.0);
+ }
+ // Change the level of the accumulator (bucket)
+ accumulator_ += framesize_kbits;
+ CapAccumulator();
+}
+
+void FrameDropper::Leak(uint32_t input_framerate) {
+ if (!enabled_) {
+ return;
+ }
+ if (input_framerate < 1) {
+ return;
+ }
+ if (target_bitrate_ < 0.0f) {
+ return;
+ }
+ // Add lower bound for large frame accumulation spread.
+ large_frame_accumulation_spread_ = std::max(0.5 * input_framerate, 5.0);
+ // Expected bits per frame based on current input frame rate.
+ float expected_bits_per_frame = target_bitrate_ / input_framerate;
+ if (large_frame_accumulation_count_ > 0) {
+ expected_bits_per_frame -= large_frame_accumulation_chunk_size_;
+ --large_frame_accumulation_count_;
+ }
+ accumulator_ -= expected_bits_per_frame;
+ if (accumulator_ < 0.0f) {
+ accumulator_ = 0.0f;
+ }
+ UpdateRatio();
+}
+
+void FrameDropper::UpdateRatio() {
+ if (accumulator_ > 1.3f * accumulator_max_) {
+ // Too far above accumulator max, react faster.
+ drop_ratio_.UpdateBase(0.8f);
+ } else {
+ // Go back to normal reaction.
+ drop_ratio_.UpdateBase(0.9f);
+ }
+ if (accumulator_ > accumulator_max_) {
+ // We are above accumulator max, and should ideally drop a frame. Increase
+ // the drop_ratio_ and drop the frame later.
+ if (was_below_max_) {
+ drop_next_ = true;
+ }
+ drop_ratio_.Apply(1.0f, 1.0f);
+ drop_ratio_.UpdateBase(0.9f);
+ } else {
+ drop_ratio_.Apply(1.0f, 0.0f);
+ }
+ was_below_max_ = accumulator_ < accumulator_max_;
+}
+
+// This function signals when to drop frames to the caller. It makes use of the
+// drop_ratio_ to smooth out the drops over time.
+bool FrameDropper::DropFrame() {
+ if (!enabled_) {
+ return false;
+ }
+ if (drop_next_) {
+ drop_next_ = false;
+ drop_count_ = 0;
+ }
+
+ if (drop_ratio_.filtered() >= 0.5f) { // Drops per keep
+ // Limit is the number of frames we should drop between each kept frame
+ // to keep our drop ratio. limit is positive in this case.
+ float denom = 1.0f - drop_ratio_.filtered();
+ if (denom < 1e-5) {
+ denom = 1e-5f;
+ }
+ int32_t limit = static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
+ // Put a bound on the max amount of dropped frames between each kept
+ // frame, in terms of frame rate and window size (secs).
+ int max_limit =
+ static_cast<int>(incoming_frame_rate_ * max_drop_duration_secs_);
+ if (limit > max_limit) {
+ limit = max_limit;
+ }
+ if (drop_count_ < 0) {
+ // Reset the drop_count_ since it was negative and should be positive.
+ drop_count_ = -drop_count_;
+ }
+ if (drop_count_ < limit) {
+ // As long we are below the limit we should drop frames.
+ drop_count_++;
+ return true;
+ } else {
+ // Only when we reset drop_count_ a frame should be kept.
+ drop_count_ = 0;
+ return false;
+ }
+ } else if (drop_ratio_.filtered() > 0.0f &&
+ drop_ratio_.filtered() < 0.5f) { // Keeps per drop
+ // Limit is the number of frames we should keep between each drop
+ // in order to keep the drop ratio. limit is negative in this case,
+ // and the drop_count_ is also negative.
+ float denom = drop_ratio_.filtered();
+ if (denom < 1e-5) {
+ denom = 1e-5f;
+ }
+ int32_t limit = -static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
+ if (drop_count_ > 0) {
+ // Reset the drop_count_ since we have a positive
+ // drop_count_, and it should be negative.
+ drop_count_ = -drop_count_;
+ }
+ if (drop_count_ > limit) {
+ if (drop_count_ == 0) {
+ // Drop frames when we reset drop_count_.
+ drop_count_--;
+ return true;
+ } else {
+ // Keep frames as long as we haven't reached limit.
+ drop_count_--;
+ return false;
+ }
+ } else {
+ drop_count_ = 0;
+ return false;
+ }
+ }
+ drop_count_ = 0;
+ return false;
+}
+
+void FrameDropper::SetRates(float bitrate, float incoming_frame_rate) {
+ // Bit rate of -1 means infinite bandwidth.
+ accumulator_max_ = bitrate * kLeakyBucketSizeSeconds;
+ if (target_bitrate_ > 0.0f && bitrate < target_bitrate_ &&
+ accumulator_ > accumulator_max_) {
+ // Rescale the accumulator level if the accumulator max decreases
+ accumulator_ = bitrate / target_bitrate_ * accumulator_;
+ }
+ target_bitrate_ = bitrate;
+ CapAccumulator();
+ incoming_frame_rate_ = incoming_frame_rate;
+}
+
+// Put a cap on the accumulator, i.e., don't let it grow beyond some level.
+// This is a temporary fix for screencasting where very large frames from
+// encoder will cause very slow response (too many frame drops).
+// TODO(isheriff): Remove this now that large delta frames are also spread out ?
+void FrameDropper::CapAccumulator() {
+ float max_accumulator = target_bitrate_ * kAccumulatorCapBufferSizeSecs;
+ if (accumulator_ > max_accumulator) {
+ accumulator_ = max_accumulator;
+ }
+}
+} // namespace webrtc
diff --git a/native/third_party/webrtc-frame-dropper/modules/video_coding/utility/frame_dropper.h b/native/third_party/webrtc-frame-dropper/modules/video_coding/utility/frame_dropper.h
new file mode 100644
index 00000000..e8016839
--- /dev/null
+++ b/native/third_party/webrtc-frame-dropper/modules/video_coding/utility/frame_dropper.h
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_
+#define MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "rtc_base/numerics/exp_filter.h"
+
+namespace webrtc {
+
+// The Frame Dropper implements a variant of the leaky bucket algorithm
+// for keeping track of when to drop frames to avoid bit rate
+// over use when the encoder can't keep its bit rate.
+class FrameDropper {
+ public:
+ FrameDropper();
+ ~FrameDropper();
+
+ // Resets the FrameDropper to its initial state.
+ void Reset();
+
+ void Enable(bool enable);
+
+ // Answers the question if it's time to drop a frame if we want to reach a
+ // given frame rate. Must be called for every frame.
+ //
+ // Return value : True if we should drop the current frame.
+ bool DropFrame();
+
+ // Updates the FrameDropper with the size of the latest encoded frame.
+ // The FrameDropper calculates a new drop ratio (can be seen as the
+ // probability to drop a frame) and updates its internal statistics.
+ //
+ // Input:
+ // - framesize_bytes : The size of the latest frame returned
+ // from the encoder.
+ // - delta_frame : True if the encoder returned a delta frame.
+ void Fill(size_t framesize_bytes, bool delta_frame);
+
+ void Leak(uint32_t input_framerate);
+
+ // Sets the target bit rate and the frame rate produced by the camera.
+ //
+ // Input:
+ // - bitrate : The target bit rate.
+ void SetRates(float bitrate, float incoming_frame_rate);
+
+ private:
+ void UpdateRatio();
+ void CapAccumulator();
+
+ ExpFilter key_frame_ratio_;
+ ExpFilter delta_frame_size_avg_kbits_;
+
+ // Key frames and large delta frames are not immediately accumulated in the
+ // bucket since they can immediately overflow the bucket leading to large
+ // drops on the following packets that may be much smaller. Instead these
+ // large frames are accumulated over several frames when the bucket leaks.
+
+ // `large_frame_accumulation_spread_` represents the number of frames over
+ // which a large frame is accumulated.
+ float large_frame_accumulation_spread_;
+ // `large_frame_accumulation_count_` represents the number of frames left
+ // to finish accumulating a large frame.
+ int large_frame_accumulation_count_;
+ // `large_frame_accumulation_chunk_size_` represents the size of a single
+ // chunk for large frame accumulation.
+ float large_frame_accumulation_chunk_size_;
+
+ float accumulator_;
+ float accumulator_max_;
+ float target_bitrate_;
+ bool drop_next_;
+ ExpFilter drop_ratio_;
+ int drop_count_;
+ float incoming_frame_rate_;
+ bool was_below_max_;
+ bool enabled_;
+ const float max_drop_duration_secs_;
+};
+
+} // namespace webrtc
+
+#endif // MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_
diff --git a/native/third_party/webrtc-frame-dropper/rtc_base/numerics/exp_filter.cc b/native/third_party/webrtc-frame-dropper/rtc_base/numerics/exp_filter.cc
new file mode 100644
index 00000000..5dbca9bb
--- /dev/null
+++ b/native/third_party/webrtc-frame-dropper/rtc_base/numerics/exp_filter.cc
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "rtc_base/numerics/exp_filter.h"
+
+#include <cmath>
+
+namespace webrtc {
+
+const float ExpFilter::kValueUndefined = -1.0f;
+
+void ExpFilter::Reset(float alpha) {
+ alpha_ = alpha;
+ filtered_ = kValueUndefined;
+}
+
+float ExpFilter::Apply(float exp, float sample) {
+ if (filtered_ == kValueUndefined) {
+ // Initialize filtered value.
+ filtered_ = sample;
+ } else if (exp == 1.0) {
+ filtered_ = alpha_ * filtered_ + (1 - alpha_) * sample;
+ } else {
+ float alpha = std::pow(alpha_, exp);
+ filtered_ = alpha * filtered_ + (1 - alpha) * sample;
+ }
+ if (max_ != kValueUndefined && filtered_ > max_) {
+ filtered_ = max_;
+ }
+ return filtered_;
+}
+
+void ExpFilter::UpdateBase(float alpha) {
+ alpha_ = alpha;
+}
+} // namespace webrtc
diff --git a/native/third_party/webrtc-frame-dropper/rtc_base/numerics/exp_filter.h b/native/third_party/webrtc-frame-dropper/rtc_base/numerics/exp_filter.h
new file mode 100644
index 00000000..b051e710
--- /dev/null
+++ b/native/third_party/webrtc-frame-dropper/rtc_base/numerics/exp_filter.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef RTC_BASE_NUMERICS_EXP_FILTER_H_
+#define RTC_BASE_NUMERICS_EXP_FILTER_H_
+
+namespace webrtc {
+
+// This class can be used, for example, for smoothing the result of bandwidth
+// estimation and packet loss estimation.
+
+class ExpFilter {
+ public:
+ static const float kValueUndefined;
+
+ explicit ExpFilter(float alpha, float max = kValueUndefined) : max_(max) {
+ Reset(alpha);
+ }
+
+ // Resets the filter to its initial state, and resets filter factor base to
+ // the given value `alpha`.
+ void Reset(float alpha);
+
+ // Applies the filter with a given exponent on the provided sample:
+ // y(k) = min(alpha_^ exp * y(k-1) + (1 - alpha_^ exp) * sample, max_).
+ float Apply(float exp, float sample);
+
+ // Returns current filtered value.
+ float filtered() const { return filtered_; }
+
+ // Changes the filter factor base to the given value `alpha`.
+ void UpdateBase(float alpha);
+
+ private:
+ float alpha_; // Filter factor base.
+ float filtered_; // Current filter output.
+ const float max_;
+};
+} // namespace webrtc
+
+
+#endif // RTC_BASE_NUMERICS_EXP_FILTER_H_
diff --git a/scripts/app-browser-fanout-gate.ts b/scripts/app-browser-fanout-gate.ts
index 2e3b44ef..280cb22f 100644
--- a/scripts/app-browser-fanout-gate.ts
+++ b/scripts/app-browser-fanout-gate.ts
@@ -240,7 +240,7 @@ async function main(): Promise<void> {
(sample) => sample.controlOpen && sample.requests.includes("list-sources"), 10000);
stage = "share-browser-capture";
await click(host, 'button[data-source-tab="browser"]');
- await click(host, '.lr-source-option.is-browser');
+ await click(host, 'button.lr-source-option[aria-label="Choose what to share"]');
stage = "share-ingress";
await waitForSample(() => read(host), (s) => s.sharing && s.receiveRequests === 1 && s.outbound === 1, 30000);
let invite = await evaluate<string>(cdp, host, "document.querySelector('.lr-invite-url').value", Date.now()+5000);
@@ -313,7 +313,7 @@ async function main(): Promise<void> {
await evaluate(cdp, host, `fanoutGate.audio(${!initialAudio})`, Date.now()+5000);
await click(host, 'button[aria-label="Switch source"]');
await click(host, 'button[data-source-tab="browser"]');
- await click(host, '.lr-source-option.is-browser');
+ await click(host, 'button.lr-source-option[aria-label="Choose what to share"]');
await waitForSample(() => read(host), (s) => s.receiveRequests === 2 && s.captures === 2 && s.sharing, 15000);
const beforeSource = await read(viewers[1]!);
await waitForSample(() => read(viewers[1]!), (s) => s.width > 0 && s.frames >= beforeSource.frames+20 && s.audio===Number(!initialAudio), 25000);
diff --git a/scripts/app-gate-endpoint.ts b/scripts/app-gate-endpoint.ts
index 9ba38730..fd2225b1 100644
--- a/scripts/app-gate-endpoint.ts
+++ b/scripts/app-gate-endpoint.ts
@@ -1,9 +1,6 @@
import type { ChildProcessWithoutNullStreams } from "node:child_process";
-import {
- NATIVE_CLIENT_PORT_END,
- NATIVE_CLIENT_PORT_START,
-} from "../src/client/native/wire";
+import { NATIVE_CLIENT_PORT_START, NATIVE_CLIENT_PORT_END } from "../src/client/native/discovery-ports";
import { withDeadline } from "./browser-gate-harness";
const INSTANCE_TOKEN_PATTERN = /^[A-Za-z0-9_-]{43}$/;
diff --git a/scripts/app-icons.mjs b/scripts/app-icons.mjs
index 99efe5dc..8f3d2505 100644
--- a/scripts/app-icons.mjs
+++ b/scripts/app-icons.mjs
@@ -3,7 +3,7 @@ import {
readFileSync,
writeFileSync,
} from "node:fs";
-import { join } from "node:path";
+import { dirname, join } from "node:path";
const PNG_SIGNATURE = Buffer.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
@@ -165,3 +165,8 @@ export function writeAppPlatformAssets({
? "Piik App.app"
: null;
}
+
+export function writeAndroidAppIcon(iconPath, outputFile) {
+ mkdirSync(dirname(outputFile), { recursive: true });
+ writeFileSync(outputFile, requireEntry(readPngEntries(iconPath), 256));
+}
diff --git a/scripts/app-loopback-gate.ts b/scripts/app-loopback-gate.ts
index f7982ab1..8096c9c7 100644
--- a/scripts/app-loopback-gate.ts
+++ b/scripts/app-loopback-gate.ts
@@ -3,6 +3,7 @@ import { mkdtemp } from "node:fs/promises";
import { createServer } from "node:net";
import { tmpdir } from "node:os";
import { join } from "node:path";
+import { NATIVE_CLIENT_PORT_START, NATIVE_CLIENT_PORT_END } from "../src/client/native/discovery-ports";
import {
CdpConnection,
@@ -17,8 +18,6 @@ import {
type AppEndpoint as Endpoint,
} from "./app-gate-endpoint";
import {
- NATIVE_CLIENT_PORT_END,
- NATIVE_CLIENT_PORT_START,
NATIVE_CLIENT_PROTOCOL,
NATIVE_CLIENT_SUBPROTOCOL,
} from "../src/client/native/wire";
diff --git a/scripts/app-native-host-gate.ts b/scripts/app-native-host-gate.ts
index 219068cc..ef1bb3b6 100644
--- a/scripts/app-native-host-gate.ts
+++ b/scripts/app-native-host-gate.ts
@@ -1043,7 +1043,7 @@ async function main(): Promise<void> {
const framesBefore4K = await evaluate<number>(cdp, viewer,
`document.querySelector('video')?.getVideoPlaybackQuality().totalVideoFrames ?? 0`, Date.now() + 5_000);
await evaluate<void>(cdp, host,
- `document.querySelector('button.lr-chip[aria-label="4K"]')?.click()`, Date.now() + 5_000);
+ `document.querySelector('button.lr-chip[aria-label="2160p"]')?.click()`, Date.now() + 5_000);
await waitForValue(deadline => evaluate<boolean>(cdp!, host, `(() => {
const fps = document.querySelector('input[name="maxFramerate"]');
const width = document.querySelector('.lr-video-size input[name="width"]');
diff --git a/scripts/app-package-targets.mjs b/scripts/app-package-targets.mjs
index 97523466..4c58292d 100644
--- a/scripts/app-package-targets.mjs
+++ b/scripts/app-package-targets.mjs
@@ -1,4 +1,23 @@
export const CLOUDFLARED_VERSION = "2026.10.3";
+export const CLOUDFLARED_SOURCE_SHA256 = "6c6901ae333508e1c529184bf90cb87ff59592620d293401db38415269908311";
+
+// Android packages both supported ABIs in one installable APK, not a Go CLI.
+export const ANDROID_APP_PACKAGE_TARGET = {
+ id: "android-universal",
+ artifact: "piik-app-android-universal.apk",
+};
+
+export function androidVersionCode(version) {
+ if (version === "development") return 1;
+ const parts = /^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.exec(version);
+ if (!parts) throw new Error("Android version must be vMAJOR.MINOR.PATCH or development");
+ const [major, minor, patch] = parts.slice(1).map(Number);
+ const code = major * 1_000_000 + minor * 1_000 + patch;
+ if (minor > 999 || patch > 999 || !Number.isSafeInteger(code) || code < 1 || code > 2_100_000_000) {
+ throw new Error("Android versionCode requires minor/patch <= 999 and a code in 1..2100000000");
+ }
+ return code;
+}
export const APP_PACKAGE_TARGETS = [
{
diff --git a/scripts/assemble-app.mjs b/scripts/assemble-app.mjs
index ce58cdca..36f0bfca 100644
--- a/scripts/assemble-app.mjs
+++ b/scripts/assemble-app.mjs
@@ -257,7 +257,7 @@ try {
"build",
"-trimpath",
"-ldflags",
- `-s -w -X github.com/TNTcraftHIM/Piik/internal/app.BuildRevision=${revision} -X github.com/TNTcraftHIM/Piik/internal/app.BuildVersion=${version}`,
+ `-s -w -X github.com/TNTcraftHIM/Piik/internal/app/buildinfo.Revision=${revision} -X github.com/TNTcraftHIM/Piik/internal/app/buildinfo.Release=${version}`,
"-o",
appPath,
"./cmd/piik-app",
diff --git a/scripts/build-android.mjs b/scripts/build-android.mjs
new file mode 100644
index 00000000..93c3ebfd
--- /dev/null
+++ b/scripts/build-android.mjs
@@ -0,0 +1,177 @@
+#!/usr/bin/env node
+import { spawnSync, execFileSync } from 'node:child_process';
+import { createHash } from 'node:crypto';
+import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
+import { dirname, join, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { writeAndroidLicenseNotices } from './package-licenses.mjs';
+import { writeAndroidAppIcon } from './app-icons.mjs';
+import { CLOUDFLARED_VERSION, CLOUDFLARED_SOURCE_SHA256, ANDROID_APP_PACKAGE_TARGET, androidVersionCode } from './app-package-targets.mjs';
+import { buildVersion } from './release-version.mjs';
+import { assertCleanRevision } from './build-workspace.mjs';
+
+const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
+const android = join(root, 'mobile', 'android');
+const gradleHome = process.env.GRADLE_HOME;
+const tunnelOnly = process.argv.includes('--tunnel-only');
+const release = process.argv.includes('--release');
+let output;
+for (let index = 2; index < process.argv.length; index++) {
+ const option = process.argv[index];
+ if (option === '--output' && !output && process.argv[index + 1] && !process.argv[index + 1].startsWith('--')) {
+ output = resolve(process.argv[++index]);
+ } else if (option !== '--tunnel-only' && option !== '--release') {
+ throw new Error(`Unknown Android build option: ${option}`);
+ }
+}
+if ((release && tunnelOnly) || (release !== Boolean(output)))
+ throw new Error('Use --release --output <new-directory> for a signed release APK.');
+if (output && existsSync(output)) throw new Error('Android release output must not already exist.');
+const releaseRevision = release ? assertCleanRevision(root) : undefined;
+if (release) {
+ for (const name of ['PIIK_ANDROID_KEYSTORE', 'PIIK_ANDROID_STORE_PASSWORD', 'PIIK_ANDROID_KEY_ALIAS', 'PIIK_ANDROID_KEY_PASSWORD'])
+ if (!process.env[name]) throw new Error(`Android release signing requires ${name}.`);
+ if (!existsSync(process.env.PIIK_ANDROID_KEYSTORE)) throw new Error('Android release keystore does not exist.');
+}
+if (!process.env.ANDROID_NDK_HOME || (!tunnelOnly && (!process.env.ANDROID_HOME || !gradleHome))) {
+ throw new Error('Set ANDROID_HOME, ANDROID_NDK_HOME and GRADLE_HOME to the installed SDK, NDK and Gradle distribution.');
+}
+function run(command, args, options = {}) {
+ const result = spawnSync(command, args, { cwd: root, env: process.env, stdio: 'inherit', windowsHide: true, ...options });
+ if (result.error) throw result.error;
+ if (result.status !== 0) throw new Error(`${command} failed with exit code ${result.status}`);
+}
+// Build upstream in its own pinned module: the App never imports its CLI graph.
+const notice = JSON.parse(readFileSync(join(root, 'licenses', 'upstream.json'), 'utf8'))[`cloudflared@${CLOUDFLARED_VERSION}`];
+const commit = new URL(notice.source).pathname.split('/').at(-1);
+if (!/^[a-f0-9]{40}$/.test(commit)) throw new Error('Cloudflared source must pin a commit.');
+const cache = join(root, 'build', 'android-cloudflared');
+mkdirSync(cache, { recursive: true });
+const archive = join(cache, `${commit}.tar.gz`);
+if (!existsSync(archive)) {
+ const response = await fetch(`https://codeload.github.com/cloudflare/cloudflared/tar.gz/${commit}`);
+ if (!response.ok) throw new Error(`Cloudflared source download failed: ${response.status}`);
+ writeFileSync(archive, Buffer.from(await response.arrayBuffer()));
+}
+if (createHash('sha256').update(readFileSync(archive)).digest('hex') !== CLOUDFLARED_SOURCE_SHA256) {
+ throw new Error('Cloudflared source archive differs from its pin.');
+}
+run('tar', ['-xzf', archive, '-C', cache]);
+const source = join(cache, `cloudflared-${commit}`);
+const adapter = join(root, 'mobile', 'cloudflared');
+const patched = join(cache, 'overlay');
+mkdirSync(join(patched, 'supervisor'), { recursive: true });
+copyFileSync(join(source, 'supervisor', 'tunnel.go'), join(patched, 'supervisor', 'tunnel.go'));
+run('git', ['apply', '--unidiff-zero', `--directory=${join('build', 'android-cloudflared', 'overlay').replaceAll('\\', '/')}`,
+ join(adapter, 'edge-fallback.patch')]);
+const overlay = join(cache, 'overlay.json');
+writeFileSync(overlay, JSON.stringify({ Replace: {
+ [join(source, 'cmd', 'cloudflared', 'piik_android_dns.go')]: join(adapter, 'android_dns.go'),
+ [join(source, 'supervisor', 'tunnel.go')]: join(patched, 'supervisor', 'tunnel.go'),
+ [join(source, 'supervisor', 'piik_fallback_test.go')]: join(adapter, 'fallback_test.go'),
+} }, null, 2));
+const [hostOS, hostArch] = execFileSync('go', ['env', 'GOHOSTOS', 'GOHOSTARCH'], {
+ cwd: root, encoding: 'utf8', windowsHide: true,
+}).trim().split(/\r?\n/);
+const fallbackTest = join(cache, `fallback.test${hostOS === 'windows' ? '.exe' : ''}`);
+run('go', ['test', '-c', '-mod=readonly', `-overlay=${overlay}`, '-tags=piik_cloudflared',
+ '-o', fallbackTest, './supervisor'], { cwd: source,
+ env: { ...process.env, GOOS: hostOS, GOARCH: hostArch, CGO_ENABLED: '0', GOWORK: 'off' } });
+run(fallbackTest, ['-test.run=^TestPiikExhaustedQuicHandshakeFallsBack$', '-test.count=1']);
+const host = { win32: 'windows-x86_64', linux: 'linux-x86_64', darwin: 'darwin-x86_64' }[process.platform];
+if (!host) throw new Error('Unsupported NDK build host.');
+const tools = join(process.env.ANDROID_NDK_HOME, 'toolchains', 'llvm', 'prebuilt', host, 'bin');
+const exe = process.platform === 'win32' ? '.exe' : '';
+const clang = join(tools, `clang${exe}`);
+for (const [abi, arch, target, machine] of [
+ ['arm64-v8a', 'arm64', 'aarch64-linux-android29', 'AArch64'],
+ ['x86_64', 'amd64', 'x86_64-linux-android29', 'Advanced Micro Devices X86-64'],
+]) {
+ const output = join(android, 'app', 'src', 'main', 'jniLibs', abi, 'libcloudflared.so');
+ mkdirSync(dirname(output), { recursive: true });
+ const env = { ...process.env, GOOS: 'android', GOARCH: arch, CGO_ENABLED: '1',
+ CC: `"${clang}" --target=${target}`, GOWORK: 'off' };
+ run('go', ['build', '-mod=readonly', `-overlay=${overlay}`, '-tags=piik_cloudflared', '-trimpath', '-buildvcs=false', '-buildmode=pie',
+ `-ldflags=-s -w -X main.Version=${CLOUDFLARED_VERSION} -X main.BuildTime=source-pinned -linkmode=external -extldflags=-Wl,-z,max-page-size=16384`,
+ '-o', output, './cmd/cloudflared'], { cwd: source, env });
+ chmodSync(output, 0o755);
+ const elf = execFileSync(join(tools, `llvm-readelf${exe}`), ['-h', '-l', '-d', output], { encoding: 'utf8', windowsHide: true });
+ const loads = [...elf.matchAll(/^\s*LOAD\s+.*$/gm)];
+ if (!elf.includes(machine) || !/Type:\s+DYN/.test(elf) || !/INTERP/.test(elf) || !loads.length ||
+ !/\/system\/bin\/linker64/.test(elf) ||
+ loads.some(([line]) => !/0x4000\s*$/.test(line))) {
+ throw new Error(`Cloudflared ${abi} must be Android PIE with 16KB LOAD alignment.`);
+ }
+ for (const [, library] of elf.matchAll(/Shared library: \[([^\]]+)\]/g)) {
+ if (!['libc.so', 'libdl.so', 'libm.so', 'liblog.so', 'libandroid.so'].includes(library)) {
+ throw new Error(`Unexpected cloudflared Android dependency: ${library}`);
+ }
+ }
+ writeFileSync(join(cache, `${abi}-elf.txt`), elf);
+ console.log(`Android cloudflared ${CLOUDFLARED_VERSION}: ${abi} PIE (16KB)`);
+}
+if (!tunnelOnly) {
+ const revision = releaseRevision ?? execFileSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf8', windowsHide: true }).trim();
+ const version = buildVersion(root, revision);
+ const versionCode = androidVersionCode(version);
+ const versionName = version === 'development' ? `${version}-${revision.slice(0, 7)}` : version;
+ const launcher = readdirSync(join(gradleHome, 'lib')).find(name => /^gradle-launcher-.*\.jar$/.test(name));
+ if (!launcher) throw new Error('GRADLE_HOME does not contain a Gradle launcher.');
+ const frameDropper = join(root, 'native', 'third_party', 'webrtc-frame-dropper');
+ for (const [abi, target] of [['arm64-v8a', 'aarch64-linux-android29'], ['x86_64', 'x86_64-linux-android29']]) {
+ const output = join(android, 'app', 'src', 'main', 'jniLibs', abi, 'libpiik_frame_dropper.so');
+ mkdirSync(dirname(output), { recursive: true });
+ run(join(tools, `clang++${exe}`), [`--target=${target}`, '-std=c++17', '-O2', '-fPIC', '-shared',
+ '-static-libstdc++', '-fno-exceptions', '-fno-rtti', '-fvisibility=hidden',
+ '-ffunction-sections', '-fdata-sections', '-Wl,--gc-sections', '-Wl,--exclude-libs,ALL',
+ '-Wl,-z,defs', '-Wl,-z,max-page-size=16384', '-Wl,-soname,libpiik_frame_dropper.so',
+ '-I', frameDropper, join(android, 'frame_dropper.cc'),
+ join(frameDropper, 'modules', 'video_coding', 'utility', 'frame_dropper.cc'),
+ join(frameDropper, 'rtc_base', 'numerics', 'exp_filter.cc'), '-o', output]);
+ const elf = execFileSync(join(tools, `llvm-readelf${exe}`), ['-h', '-l', '-d', output], { encoding: 'utf8', windowsHide: true });
+ const loads = [...elf.matchAll(/^\s*LOAD\s+.*$/gm)];
+ if (!/Type:\s+DYN/.test(elf) || !loads.length || loads.some(([line]) => !/0x4000\s*$/.test(line)))
+ throw new Error(`FrameDropper ${abi} must have 16KB LOAD alignment.`);
+ for (const [, library] of elf.matchAll(/Shared library: \[([^\]]+)\]/g))
+ if (!['libc.so', 'libdl.so', 'libm.so'].includes(library))
+ throw new Error(`Unexpected FrameDropper Android dependency: ${library}`);
+ console.log(`Android WebRTC FrameDropper: ${abi} (static C++ runtime, 16KB)`);
+ }
+ const webEnv = { ...process.env, VITE_PIIK_VERSION: version, VITE_PIIK_REVISION: revision };
+ if (process.platform === 'win32') run(process.env.ComSpec || 'cmd.exe', ['/d', '/s', '/c', 'npm run build:web'], { env: webEnv });
+ else run('npm', ['run', 'build:web'], { env: webEnv });
+ writeAndroidLicenseNotices(root, join(android, 'app', 'src', 'main', 'assets', 'THIRD-PARTY-NOTICES.txt'), 'go');
+ writeAndroidAppIcon(join(root, 'cmd', 'piik-app', 'piik.ico'), join(android, 'app', 'src', 'main', 'res', 'drawable-nodpi', 'piik.png'));
+ mkdirSync(join(android, 'app', 'libs'), { recursive: true });
+ run(process.env.PIIK_GOMOBILE || 'gomobile', ['bind', '-trimpath', '-target=android/arm64,android/amd64', '-androidapi', '29',
+ `-ldflags=-s -w -checklinkname=0 -X github.com/TNTcraftHIM/Piik/internal/app/buildinfo.Release=${version} -X github.com/TNTcraftHIM/Piik/internal/app/buildinfo.Revision=${revision}`,
+ '-javapkg', 'tv.piik', '-o', join(android, 'app', 'libs', 'piik.aar'), './mobile/bridge']);
+ // Recreate the final ZIP so replaced native libraries leave no free blocks.
+ run('java', ['-classpath', join(gradleHome, 'lib', launcher), 'org.gradle.launcher.GradleMain',
+ '-Dorg.gradle.jvmargs=-Xmx2g', '-p', android, '--no-daemon',
+ `-PpiikVersion=${versionName}`, `-PpiikVersionCode=${versionCode}`,
+ release ? ':app:packageRelease' : ':app:packageDebug', '--rerun',
+ release ? ':app:assembleRelease' : ':app:assembleDebug', '--console=plain']);
+ const variant = release ? 'release' : 'debug';
+ const apk = join(android, 'app', 'build', 'outputs', 'apk', variant, `app-${variant}.apk`);
+ if (!existsSync(apk)) throw new Error('The Android build did not produce its APK.');
+ if (release) {
+ const sdkTools = join(process.env.ANDROID_HOME, 'build-tools', '35.0.0');
+ const signing = execFileSync('java', ['-jar', join(sdkTools, 'lib', 'apksigner.jar'),
+ 'verify', '--verbose', '--print-certs', apk], { encoding: 'utf8', windowsHide: true });
+ if (/CN=Android Debug\b/i.test(signing)) throw new Error('A release APK cannot use an Android debug signing key.');
+ const badging = execFileSync(join(sdkTools, `aapt${exe}`), ['dump', 'badging', apk], { encoding: 'utf8', windowsHide: true });
+ if (!badging.includes(`package: name='tv.piik.android' versionCode='${versionCode}' versionName='${versionName}'`) ||
+ /application-debuggable/.test(badging) || !badging.includes("native-code: 'arm64-v8a' 'x86_64'"))
+ throw new Error('Android release identity, ABIs or debuggable flag differs from the package contract.');
+ const { id: target, artifact } = ANDROID_APP_PACKAGE_TARGET;
+ const digest = createHash('sha256').update(readFileSync(apk)).digest('hex');
+ assertCleanRevision(root, revision);
+ mkdirSync(output, { recursive: true });
+ copyFileSync(apk, join(output, artifact));
+ writeFileSync(join(output, `${artifact}.sha256`), `${digest} ${artifact}\n`, 'ascii');
+ writeFileSync(join(output, artifact.replace(/\.apk$/, '.release.json')),
+ `${JSON.stringify({ schema: 2, version, revision, target, artifact, artifactSha256: digest }, null, 2)}\n`);
+ console.log(`Piik Android signed release APK: ${join(output, artifact)}`);
+ } else console.log(`Piik Android development APK: ${apk}`);
+}
diff --git a/scripts/check-container.mjs b/scripts/check-container.mjs
index 3c831178..5254d88a 100644
--- a/scripts/check-container.mjs
+++ b/scripts/check-container.mjs
@@ -9,6 +9,7 @@ import { dirname, join, resolve } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { fileURLToPath } from "node:url";
import { resetBuildWorkspace } from "./build-workspace.mjs";
+import { checkWebSourceIdentity } from "./container-web-identity.mjs";
const [image, revision] = process.argv.slice(2);
if (process.argv.length !== 4 || !image || !/^[0-9a-f]{40}$/.test(revision ?? "")) {
@@ -90,10 +91,7 @@ try {
await expectServices(false, false);
assert.equal((await (await request("/api/site-access")).json()).required, false);
const html = await (await request("/")).text();
- const asset = html.match(/src="(\/assets\/index-[A-Za-z0-9_-]+\.js)"/)?.[1];
- assert.ok(asset, "Server must embed the Web UI");
- const script = await (await request(asset)).text();
- assert.ok(script.includes(revision), "Web and Server must have the same source identity");
+ await checkWebSourceIdentity(html, revision, async asset => (await request(asset)).text());
await binding(3478);
const room = await (await request("/api/rooms", {
method: "POST", headers: { Origin: "https://share.example.com", "Content-Type": "application/json" },
diff --git a/scripts/check-go.mjs b/scripts/check-go.mjs
index 47844611..838a3eab 100644
--- a/scripts/check-go.mjs
+++ b/scripts/check-go.mjs
@@ -18,7 +18,7 @@ const WEB_INDEX = join("internal", "server", "webassets", "dist", "index.html");
// Local dependency repairs retain upstream tests, including PCPv6 composition.
// Nested modules need explicit test patterns; remove these with the replacements.
// Go source lives here; ./... would also scan downloaded SDKs and build probes.
-const GO_TEST_PACKAGES = ["./cmd/...", "./internal/...", "github.com/netbirdio/go-nat/...", "github.com/jackpal/go-nat-pmp"];
+const GO_TEST_PACKAGES = ["./cmd/...", "./internal/...", "./mobile/bridge", "github.com/netbirdio/go-nat/...", "github.com/jackpal/go-nat-pmp"];
const root = realpathSync(resolve(dirname(fileURLToPath(import.meta.url)), ".."));
const captureRoot = join(root, "native", "capture");
@@ -60,7 +60,7 @@ function checkCore() {
const goRoot = run(go, ["env", "GOROOT"], { capture: true });
const gofmt = process.env.PIIK_GOFMT?.trim() ||
join(goRoot, "bin", process.platform === "win32" ? "gofmt.exe" : "gofmt");
- const unformatted = run(gofmt, ["-l", "cmd", "internal"], { capture: true });
+ const unformatted = run(gofmt, ["-l", "cmd", "internal", "mobile/bridge"], { capture: true });
if (unformatted) {
throw new Error(`Go source is not formatted:\n${unformatted}`);
}
diff --git a/scripts/container-web-identity.mjs b/scripts/container-web-identity.mjs
new file mode 100644
index 00000000..17dd2b03
--- /dev/null
+++ b/scripts/container-web-identity.mjs
@@ -0,0 +1,12 @@
+import assert from "node:assert/strict";
+
+// Vite can place shared build metadata in an eagerly loaded dependency. Check
+// the modules named by the served HTML, not an unrelated file in the image.
+export async function checkWebSourceIdentity(html, revision, readScript) {
+ const main = html.match(/<script\b[^>]*\bsrc="(\/assets\/index-[A-Za-z0-9_-]+\.js)"/)?.[1];
+ assert.ok(main, "Server must embed the Web UI");
+ const preloads = [...html.matchAll(/<link\b[^>]*\brel="modulepreload"[^>]*\bhref="(\/assets\/[A-Za-z0-9_-]+\.js)"/g)]
+ .map(match => match[1]);
+ const scripts = await Promise.all([...new Set([main, ...preloads])].map(asset => readScript(asset)));
+ assert.ok(scripts.some(script => script.includes(revision)), "Web and Server must have the same source identity");
+}
diff --git a/scripts/network-lab/endpoint.ts b/scripts/network-lab/endpoint.ts
index 9ea642df..5939a455 100644
--- a/scripts/network-lab/endpoint.ts
+++ b/scripts/network-lab/endpoint.ts
@@ -108,7 +108,7 @@ export async function endpointWorker(): Promise<void> {
`Boolean(document.querySelector('.lr-source-picker-refresh:not(:disabled)')) && networkLab.sample().then(value => value.controlOpen)`,
deadline), Boolean, 10_000);
await click('button[data-source-tab="browser"]');
- await click(".lr-source-option.is-browser");
+ await click('button.lr-source-option[aria-label="Choose what to share"]');
await waitForSample(sample, value => value.sharing && value.width > 0 &&
(request.prediction === undefined || value.natPrediction === request.prediction), 30_000);
result = await evaluate<string>(cdp!, page!, "document.querySelector('.lr-invite-url').value", Date.now() + 5000);
diff --git a/scripts/package-licenses.mjs b/scripts/package-licenses.mjs
index 3cb5cf3d..c38e5844 100644
--- a/scripts/package-licenses.mjs
+++ b/scripts/package-licenses.mjs
@@ -4,7 +4,7 @@ import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFi
import { createRequire } from "node:module";
import { dirname, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
-import { goBuildEnvironment } from "./app-package-targets.mjs";
+import { CLOUDFLARED_VERSION, goBuildEnvironment } from "./app-package-targets.mjs";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const pinned = JSON.parse(readFileSync(join(root, "licenses", "upstream.json"), "utf8"));
@@ -116,6 +116,31 @@ export function writeServerLicenseNotices(repositoryRoot, outputFile, goCommand,
WEB_NOTICE_LINE + notices.toolchain + moduleSections(notices.modules));
}
+export function writeAndroidLicenseNotices(repositoryRoot, outputFile, goCommand) {
+ const mobileRoot = join(repositoryRoot, "mobile");
+ const target = { goos: "android", goarch: "arm64", cgo: false };
+ const notices = goNotices(mobileRoot, goCommand, target, "./bridge");
+ // gomobile adds the JNI binding runtime when generating the AAR. It is not
+ // imported by the bridge package before generation, and has its own notice.
+ const runtime = JSON.parse(execFileSync(goCommand, ["list", "-m", "-json", "golang.org/x/mobile"], {
+ cwd: mobileRoot, encoding: "utf8", windowsHide: true,
+ }));
+ const files = new Map(noticesIn(runtime.Dir).map((file) => [file, readFileSync(join(runtime.Dir, file), "utf8")]));
+ if (!files.size) throw new Error("Missing Go mobile runtime license");
+ notices.modules.set(`${runtime.Path}@${runtime.Version}`, files);
+ mkdirSync(dirname(outputFile), { recursive: true });
+ writeFileSync(outputFile, "Piik Android third-party software notices\n" +
+ section("Piik (MIT)", readFileSync(join(repositoryRoot, "LICENSE"), "utf8")) +
+ WEB_NOTICE_LINE + notices.toolchain + moduleSections(notices.modules) +
+ section("WebRTC FrameDropper (BSD-3-Clause)",
+ ["LICENSE", "PATENTS"].map(file => readFileSync(join(repositoryRoot,
+ "native", "third_party", "webrtc-frame-dropper", file), "utf8")).join("\n")) +
+ section("Android NDK libc++ (Apache-2.0 WITH LLVM-exception)", pinnedNotice("[email protected]")) +
+ section(`cloudflared@${CLOUDFLARED_VERSION}`, pinnedNotice(`cloudflared@${CLOUDFLARED_VERSION}`)) +
+ section("Piik Android cloudflared changes (MIT)",
+ "Android system DNS adapter and exhausted-QUIC-dial fallback. Source and provenance: mobile/cloudflared/."));
+}
+
export function writeAppLicenseNotices(repositoryRoot, packageRoot, goCommand, target, tunnelVersion) {
copyFileSync(join(repositoryRoot, "LICENSE"), join(packageRoot, "LICENSE"));
if (tunnelVersion) {
diff --git a/scripts/peer-assisted-benchmark.ts b/scripts/peer-assisted-benchmark.ts
index a61ab2a6..7d51d9c1 100644
--- a/scripts/peer-assisted-benchmark.ts
+++ b/scripts/peer-assisted-benchmark.ts
@@ -2579,9 +2579,9 @@ async function startHost(
return true;
})()`,
);
- await waitForPage(cdp, page, "document.querySelector('.lr-source-option.is-browser')",
+ await waitForPage(cdp, page, `document.querySelector('button.lr-source-option[aria-label="Choose what to share"]')`,
5_000, "Browser capture choice", signal);
- await evaluate(cdp, page, "document.querySelector('.lr-source-option.is-browser').click()");
+ await evaluate(cdp, page, `document.querySelector('button.lr-source-option[aria-label="Choose what to share"]').click()`);
await waitForPage(
cdp,
page,
diff --git a/scripts/release-artifacts.mjs b/scripts/release-artifacts.mjs
index 643f999b..be40988e 100644
--- a/scripts/release-artifacts.mjs
+++ b/scripts/release-artifacts.mjs
@@ -1,7 +1,7 @@
import { createHash } from "node:crypto";
import { readFileSync, readdirSync } from "node:fs";
import { basename, join, resolve } from "node:path";
-import { APP_PACKAGE_TARGETS } from "./app-package-targets.mjs";
+import { APP_PACKAGE_TARGETS, ANDROID_APP_PACKAGE_TARGET } from "./app-package-targets.mjs";
import { SERVER_PACKAGE_TARGETS } from "./server-package-targets.mjs";
export function readReleaseArtifacts(directory, version, revision) {
@@ -49,7 +49,7 @@ export function readReleaseArtifacts(directory, version, revision) {
throw new Error(`App checksum file mismatch: ${target}`);
}
}
- const expected = [...SERVER_PACKAGE_TARGETS, ...APP_PACKAGE_TARGETS].map(target => target.id);
+ const expected = [...SERVER_PACKAGE_TARGETS, ...APP_PACKAGE_TARGETS, ANDROID_APP_PACKAGE_TARGET].map(target => target.id);
if (targets.size !== expected.length || expected.some((target) => !targets.has(target))) {
throw new Error("Publication requires every registered Server and App target");
}
diff --git a/scripts/room-voice-gate.ts b/scripts/room-voice-gate.ts
index 3497e41b..3c0e9e0c 100644
--- a/scripts/room-voice-gate.ts
+++ b/scripts/room-voice-gate.ts
@@ -424,6 +424,8 @@ async function main(): Promise<void> {
await button(host, "Hide sharing settings");
check("source commentary defaults off, sends shared audio alongside room voice, and can stop independently");
await cdp.call("Page.bringToFront", {}, host.sessionId, Date.now() + 3000);
+ // A stationary hover does not wake idle playback controls.
+ await ui(host, "hover", ".lr-top-right");
await ui(host, "hover", ".lr-tv video");
await waitForSample(() => evaluate<boolean>(cdp!, host, "!!document.querySelector('.lr-tv video')&&!document.querySelector('.lr-tv video').paused", Date.now() + 3000), Boolean, 5000);
if (!await evaluate<boolean>(cdp, host, "document.querySelector('.lr-tv video').muted", Date.now() + 3000)) throw new Error("Host preview started with sound");
diff --git a/site/film/ui/main.tsx b/site/film/ui/main.tsx
index e0b822cf..806af46b 100644
--- a/site/film/ui/main.tsx
+++ b/site/film/ui/main.tsx
@@ -194,7 +194,6 @@ function Screen({ shot }: { shot: Shot }) {
<AppHeader
homeHref={launcher ? "/client" : "/"}
atHome={!viewer}
- showAppConnection={shot !== "picker"}
led={
launcher ? undefined : (
<LedStrip
@@ -213,6 +212,7 @@ function Screen({ shot }: { shot: Shot }) {
onModeChange={noop}
sites={[]}
onRemoveSite={noop}
+ onSelectSite={noop}
localAccessPassword=""
onLocalAccessPasswordChange={noop}
onSubmit={(event) => event.preventDefault()}
diff --git a/site/films/external/engine/player.js b/site/films/external/engine/player.js
index 51579405..78ac5c93 100644
--- a/site/films/external/engine/player.js
+++ b/site/films/external/engine/player.js
@@ -180,7 +180,7 @@ export class PiikPromo extends HTMLElement {
this.film?.stage.toggleAttribute('data-ending-idle', !!heldEnd && !this.playing && this.isConnected && this._inView && !document.hidden);
this.$('.pk-track').disabled = !this.film;
this.$('.pk-track').max = String(this.duration);
- this.$('.pk-track').value = String(this._poster ? 0 : this.currentTime);
+ this.$('.pk-track').value = String(this.currentTime);
this.$('.pk-track').setAttribute('aria-label', label('Film position', '播放进度'));
this.$('.pk-lang').textContent = zh ? 'EN' : '中';
this.$('.pk-lang').setAttribute('aria-label', zh ? 'Switch to English' : '切换到简体中文');
@@ -200,7 +200,8 @@ export class PiikPromo extends HTMLElement {
if (this._loading && !replace) return this._loading;
const resume = this.playing || !!this._selection?.resume;
const id = this.getAttribute('film') || 'A';
- const position = this.meta?.id === id ? this.currentTime : this._selection?.id === id ? this._selection.position : null;
+ const position = this.meta?.id === id ? (this._poster ? null : this.currentTime)
+ : this._selection?.id === id ? this._selection.position : null;
this.pause();
const request = this._request;
this._selection = { id, position, resume };
@@ -241,8 +242,7 @@ export class PiikPromo extends HTMLElement {
this.meta = mod.meta;
this._sources = [this.audio.canPlayType('audio/webm; codecs="opus"') ? mod.meta.audio.webm : null, mod.meta.audio.m4a].filter(Boolean);
this._sourceIndex = -1;
- const poster = Number(this.getAttribute('poster') ?? mod.meta.poster ?? 0);
- this.t = Math.max(0, Math.min(film.duration, position ?? (Number.isFinite(poster) ? poster : 0)));
+ this.t = Math.max(0, Math.min(film.duration, position ?? 0));
this._poster = position === null;
this.fit();
this.draw();
@@ -351,10 +351,11 @@ export class PiikPromo extends HTMLElement {
draw() {
if (!this.film) return;
const t = this.currentTime;
- this.film.render(Math.min(this.duration - 1e-3, Math.max(0, t)));
- const position = this._poster ? 0 : t;
- this.$('.pk-track').value = String(position);
- const value = fmt(position) + ' / ' + fmt(this.duration);
+ // The cover is a rendered frame, independent of the actual playback clock.
+ const frame = this._poster ? Number(this.getAttribute('poster') ?? this.meta.poster ?? 0) : t;
+ this.film.render(Math.min(this.duration - 1e-3, Math.max(0, Number.isFinite(frame) ? frame : 0)));
+ this.$('.pk-track').value = String(t);
+ const value = fmt(t) + ' / ' + fmt(this.duration);
this.$('.pk-track').setAttribute('aria-valuetext', value);
if (this.$('.pk-time').textContent !== value) {
this.$('.pk-time').textContent = value;
diff --git a/site/index.html b/site/index.html
index afdd63c6..cd9bd0d8 100644
--- a/site/index.html
+++ b/site/index.html
@@ -48,6 +48,12 @@
stroke-linejoin="round"
/>
</symbol>
+ <symbol id="phone-mark" viewBox="0 0 32 32">
+ <g fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
+ <rect x="8" y="2" width="16" height="28" rx="4" />
+ <path d="M14 6h4m-4 20h4" />
+ </g>
+ </symbol>
</svg>
<header class="site-header wrap">
@@ -374,12 +380,12 @@
<ol class="guide-steps">
<li>
<strong
- ><span lang="en">Download and extract Piik App.</span
- ><span lang="zh-CN">下载并解压 Piik App。</span></strong
+ ><span lang="en">Download Piik App.</span
+ ><span lang="zh-CN">下载 Piik App。</span></strong
>
<p>
- <span lang="en"><a href="#download">Download the App package for your system</a>, extract it completely and keep the folder intact.</span
- ><span lang="zh-CN"><a href="#download">下载对应系统的 Piik App 程序包</a>,完整解压并保留目录结构。</span>
+ <span lang="en"><a href="#download">Download Piik App for your device</a>. On computers, extract the package. On phones and tablets, install the app.</span
+ ><span lang="zh-CN"><a href="#download">下载适合你设备的 Piik App</a>。电脑上解压后打开,手机或平板安装后打开。</span>
</p>
<dl class="launch-files">
<div>
@@ -394,6 +400,10 @@
<dt>Linux</dt>
<dd><span lang="en">Run </span><span lang="zh-CN">运行 </span><code>./piik-app</code></dd>
</div>
+ <div>
+ <dt><span lang="en">Mobile</span><span lang="zh-CN">手机/平板</span></dt>
+ <dd><span lang="en">Open Piik after installation</span><span lang="zh-CN">安装后打开 Piik</span></dd>
+ </div>
</dl>
</li>
<li>
@@ -633,11 +643,11 @@
</summary>
<p>
<span lang="en"
- >Windows App and browser sharing are the primary tested paths. Viewers can use a computer or phone browser.
+ >Piik App is available for Windows, macOS, Linux and Android. Friends can watch in a computer or phone browser.
Physical-device coverage for the macOS and Linux apps is still limited; if you have a device,
we'd welcome your test results and <a href="https://github.com/TNTcraftHIM/Piik/issues">feedback</a>.</span
><span lang="zh-CN"
- >目前主要测试 Windows 版 Piik App 和浏览器分享,观众可使用电脑或手机浏览器。
+ >Piik App 提供 Windows、macOS、Linux 和 Android 版本,朋友可使用电脑或手机浏览器观看。
macOS 和 Linux 版 Piik App 仍需更多实机测试,欢迎有设备的朋友试用并<a href="https://github.com/TNTcraftHIM/Piik/issues">反馈问题</a>。</span
>
</p>
@@ -706,11 +716,11 @@
</summary>
<p>
<span lang="en"
- >Yes. Share an app window or screen through Piik App, then enable <strong>Remote control</strong> and approve your friends.
+ >Yes. Share an app window or screen through Piik App on a computer, then enable <strong>Remote control</strong> and approve your friends.
Turn on <strong>Allow simultaneous control</strong> to share the keyboard and mouse with several friends.
Games need to support local multiplayer; see the <a href="./docs/remote-control.html?lang=en">co-play guide</a> for setup and optional gamepads.</span
><span lang="zh-CN"
- >可以。用 Piik App 分享程序窗口或屏幕,开启<strong>远程控制</strong>并授权朋友即可。
+ >可以。用电脑上的 Piik App 分享程序窗口或屏幕,开启<strong>远程控制</strong>并授权朋友即可。
打开<strong>允许多人同时控制</strong>,还能和多位朋友共用键鼠,一起玩支持本地多人模式的游戏。
操作步骤和可选手柄配置见<a href="./docs/zh/remote-control.html">远控与多人同乐指南</a>。</span
>
@@ -763,8 +773,8 @@
<h2 id="download-title"><span lang="en">Get Piik App.</span><span lang="zh-CN">下载 Piik App。</span></h2>
<p>
<span lang="en"
- >Run it on your computer. Friends join with a link.</span
- ><span lang="zh-CN">电脑上运行,朋友点开链接就能加入。</span>
+ >Share from your computer or phone. Friends join with a link.</span
+ ><span lang="zh-CN">电脑或手机上开播,朋友点开链接就能加入。</span>
</p>
<a class="download-release" href="https://github.com/TNTcraftHIM/Piik/releases/latest">
<span class="download-headline" id="download-headline" hidden><span lang="en"><strong></strong><span></span></span><span lang="zh-CN"><strong></strong><span></span></span></span>
@@ -777,90 +787,117 @@
</span>
</a>
</div>
- <div class="download-cards">
- <article class="download-card" aria-labelledby="download-windows">
- <div class="platform-mark" aria-hidden="true">
- <svg viewBox="0 0 32 32">
- <path d="M4 5h10v10H4zm14 0h10v10H18zM4 19h10v10H4zm14 0h10v10H18z" fill="currentColor" />
- </svg>
- </div>
- <h3 id="download-windows">Windows</h3>
- <fieldset class="lr-toggle download-architecture" disabled>
- <legend class="sr-only"><span lang="en">Windows architecture</span><span lang="zh-CN">Windows 架构</span></legend>
- <label><input type="radio" name="windows-architecture" value="windows-amd64" checked /><span><span lang="en">64-bit (x64)</span><span lang="zh-CN">64 位(x64)</span></span></label>
- </fieldset>
- <a
- class="lr-btn button download-button"
- href="https://github.com/TNTcraftHIM/Piik/releases/latest/download/piik-app-windows-amd64.zip"
- ><svg aria-hidden="true"><use href="#download-mark" /></svg><span lang="en">Download from GitHub</span
- ><span lang="zh-CN">GitHub 下载</span></a
- ><a href="https://gitee.com/TNTcraftHIM/Piik/releases/download/latest/piik-app-windows-amd64.zip"
- ><span lang="en">Download from Gitee</span><span lang="zh-CN">Gitee 国内镜像下载</span></a
- >
- </article>
- <article class="download-card" aria-labelledby="download-macos">
- <div class="platform-mark" aria-hidden="true">
- <svg
- viewBox="0 0 32 32"
- fill="none"
- stroke="currentColor"
- stroke-width="2.2"
- stroke-linecap="round"
- stroke-linejoin="round"
+ <section class="download-platforms" aria-labelledby="download-desktop-title">
+ <h3 class="download-group-title" id="download-desktop-title"><span lang="en">Desktop</span><span lang="zh-CN">桌面端</span></h3>
+ <div class="download-cards">
+ <article class="download-card" aria-labelledby="download-windows">
+ <div class="platform-mark" aria-hidden="true">
+ <svg viewBox="0 0 32 32">
+ <path d="M4 5h10v10H4zm14 0h10v10H18zM4 19h10v10H4zm14 0h10v10H18z" fill="currentColor" />
+ </svg>
+ </div>
+ <h4 id="download-windows">Windows</h4>
+ <p class="download-variant">x64</p>
+ <a
+ class="lr-btn button download-button"
+ href="https://github.com/TNTcraftHIM/Piik/releases/latest/download/piik-app-windows-amd64.zip"
+ ><svg aria-hidden="true"><use href="#download-mark" /></svg><span lang="en">Download from GitHub</span
+ ><span lang="zh-CN">GitHub 下载</span></a
+ ><a href="https://gitee.com/TNTcraftHIM/Piik/releases/download/latest/piik-app-windows-amd64.zip"
+ ><span lang="en">Download from Gitee</span><span lang="zh-CN">Gitee 国内镜像下载</span></a
>
- <rect x="6" y="4" width="20" height="18" rx="2" />
- <path d="M3 27h26M10 22l-2 5m14-5 2 5" />
- </svg>
- </div>
- <h3 id="download-macos">macOS</h3>
- <fieldset class="lr-toggle download-architecture" disabled>
- <legend class="sr-only"><span lang="en">Mac processor</span><span lang="zh-CN">Mac 芯片</span></legend>
- <label><input type="radio" name="macos-architecture" value="darwin-arm64" checked /><span><span lang="en">Apple silicon</span><span lang="zh-CN">Apple 芯片</span></span></label>
- <label><input type="radio" name="macos-architecture" value="darwin-amd64" /><span>Intel</span></label>
- </fieldset>
- <a
- class="lr-btn button download-button"
- href="https://github.com/TNTcraftHIM/Piik/releases/latest/download/piik-app-darwin-arm64.zip"
- ><svg aria-hidden="true"><use href="#download-mark" /></svg><span lang="en">Download from GitHub</span
- ><span lang="zh-CN">GitHub 下载</span></a
- ><a href="https://gitee.com/TNTcraftHIM/Piik/releases/download/latest/piik-app-darwin-arm64.zip"
- ><span lang="en">Download from Gitee</span><span lang="zh-CN">Gitee 国内镜像下载</span></a
- >
- <p><span lang="en">Native capture requires macOS 13+.</span><span lang="zh-CN">原生采集需 macOS 13 及以上。</span></p>
- </article>
- <article class="download-card" aria-labelledby="download-linux">
- <div class="platform-mark" aria-hidden="true">
- <svg
- viewBox="0 0 32 32"
- fill="none"
- stroke="currentColor"
- stroke-width="2.2"
- stroke-linecap="round"
- stroke-linejoin="round"
+ </article>
+ <article class="download-card" aria-labelledby="download-macos">
+ <div class="platform-mark" aria-hidden="true">
+ <svg
+ viewBox="0 0 32 32"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2.2"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ >
+ <rect x="6" y="4" width="20" height="18" rx="2" />
+ <path d="M3 27h26M10 22l-2 5m14-5 2 5" />
+ </svg>
+ </div>
+ <h4 id="download-macos">macOS</h4>
+ <fieldset class="lr-toggle download-architecture" disabled>
+ <legend class="sr-only"><span lang="en">Mac processor</span><span lang="zh-CN">Mac 芯片</span></legend>
+ <label><input type="radio" name="macos-architecture" value="darwin-arm64" checked /><span><span lang="en">Apple silicon</span><span lang="zh-CN">Apple 芯片</span></span></label>
+ <label><input type="radio" name="macos-architecture" value="darwin-amd64" /><span>Intel</span></label>
+ </fieldset>
+ <a
+ class="lr-btn button download-button"
+ href="https://github.com/TNTcraftHIM/Piik/releases/latest/download/piik-app-darwin-arm64.zip"
+ ><svg aria-hidden="true"><use href="#download-mark" /></svg><span lang="en">Download from GitHub</span
+ ><span lang="zh-CN">GitHub 下载</span></a
+ ><a href="https://gitee.com/TNTcraftHIM/Piik/releases/download/latest/piik-app-darwin-arm64.zip"
+ ><span lang="en">Download from Gitee</span><span lang="zh-CN">Gitee 国内镜像下载</span></a
>
- <rect x="3" y="5" width="26" height="22" rx="4" />
- <path d="m9 12 5 4-5 4m9 0h5" />
- </svg>
- </div>
- <h3 id="download-linux">Linux</h3>
- <fieldset class="lr-toggle download-architecture" disabled>
- <legend class="sr-only"><span lang="en">Linux architecture</span><span lang="zh-CN">Linux 架构</span></legend>
- <label><input type="radio" name="linux-architecture" value="linux-amd64" checked /><span><span lang="en">64-bit (x64)</span><span lang="zh-CN">64 位(x64)</span></span></label>
- <label><input type="radio" name="linux-architecture" value="linux-arm64" /><span>ARM64</span></label>
- </fieldset>
- <a
- class="lr-btn button download-button"
- href="https://github.com/TNTcraftHIM/Piik/releases/latest/download/piik-app-linux-amd64.zip"
- ><svg aria-hidden="true"><use href="#download-mark" /></svg><span lang="en">Download from GitHub</span
- ><span lang="zh-CN">GitHub 下载</span></a
- ><a href="https://gitee.com/TNTcraftHIM/Piik/releases/download/latest/piik-app-linux-amd64.zip"
- ><span lang="en">Download from Gitee</span><span lang="zh-CN">Gitee 国内镜像下载</span></a
- >
- </article>
- </div>
+ <p><span lang="en">Native capture requires macOS 13+.</span><span lang="zh-CN">原生采集需 macOS 13 及以上。</span></p>
+ </article>
+ <article class="download-card" aria-labelledby="download-linux">
+ <div class="platform-mark" aria-hidden="true">
+ <svg
+ viewBox="0 0 32 32"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2.2"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ >
+ <rect x="3" y="5" width="26" height="22" rx="4" />
+ <path d="m9 12 5 4-5 4m9 0h5" />
+ </svg>
+ </div>
+ <h4 id="download-linux">Linux</h4>
+ <fieldset class="lr-toggle download-architecture" disabled>
+ <legend class="sr-only"><span lang="en">Linux architecture</span><span lang="zh-CN">Linux 架构</span></legend>
+ <label><input type="radio" name="linux-architecture" value="linux-amd64" checked /><span>x64</span></label>
+ <label><input type="radio" name="linux-architecture" value="linux-arm64" /><span>ARM64</span></label>
+ </fieldset>
+ <a
+ class="lr-btn button download-button"
+ href="https://github.com/TNTcraftHIM/Piik/releases/latest/download/piik-app-linux-amd64.zip"
+ ><svg aria-hidden="true"><use href="#download-mark" /></svg><span lang="en">Download from GitHub</span
+ ><span lang="zh-CN">GitHub 下载</span></a
+ ><a href="https://gitee.com/TNTcraftHIM/Piik/releases/download/latest/piik-app-linux-amd64.zip"
+ ><span lang="en">Download from Gitee</span><span lang="zh-CN">Gitee 国内镜像下载</span></a
+ >
+ </article>
+ </div>
+ </section>
+ <section class="download-platforms" aria-labelledby="download-mobile-title">
+ <h3 class="download-group-title" id="download-mobile-title"><span lang="en">Mobile</span><span lang="zh-CN">移动端</span></h3>
+ <div class="download-cards download-cards--mobile">
+ <article class="download-card" aria-labelledby="download-android">
+ <div class="platform-mark" aria-hidden="true">
+ <svg aria-hidden="true"><use href="#phone-mark" /></svg>
+ </div>
+ <h4 id="download-android">Android</h4>
+ <p class="download-variant">ARM64 / x64</p>
+ <a class="lr-btn button download-button" href="https://github.com/TNTcraftHIM/Piik/releases/latest/download/piik-app-android-universal.apk"
+ ><svg aria-hidden="true"><use href="#download-mark" /></svg><span lang="en">Download from GitHub</span><span lang="zh-CN">GitHub 下载</span></a
+ ><a href="https://gitee.com/TNTcraftHIM/Piik/releases/download/latest/piik-app-android-universal.apk"
+ ><span lang="en">Download from Gitee</span><span lang="zh-CN">Gitee 国内镜像下载</span></a>
+ <p class="download-note"><span lang="en">Android 10 or later.</span><span lang="zh-CN">Android 10 及以上。</span></p>
+ </article>
+ <article class="download-card" aria-labelledby="download-ios">
+ <div class="platform-mark" aria-hidden="true">
+ <svg aria-hidden="true"><use href="#phone-mark" /></svg>
+ </div>
+ <h4 id="download-ios">iOS</h4>
+ <p class="download-variant">iPhone / iPad</p>
+ <a class="lr-btn button download-button" href="https://demo.piik.tv"
+ ><span lang="en">Use online</span><span lang="zh-CN">使用在线版</span><span aria-hidden="true">↗</span></a>
+ <p class="download-note"><span lang="en">Watch, chat and talk in your browser.</span><span lang="zh-CN">通过浏览器观看、聊天与语音。</span></p>
+ </article>
+ </div>
+ </section>
<div class="download-help">
<a class="lr-btn button" href="https://github.com/TNTcraftHIM/Piik"><span lang="en">View on GitHub</span><span lang="zh-CN">GitHub 项目</span><span aria-hidden="true"> ↗</span></a>
- <p><span lang="en">Physical-device coverage for the macOS and Linux apps is still limited. If you have a device, we'd welcome your <a href="https://github.com/TNTcraftHIM/Piik/issues">test results and feedback</a>.</span><span lang="zh-CN">macOS 和 Linux 版 Piik App 仍需更多实机测试,欢迎有设备的朋友试用并<a href="https://github.com/TNTcraftHIM/Piik/issues">反馈问题</a>。</span></p>
+ <p><span lang="en">Found a problem? <a href="https://github.com/TNTcraftHIM/Piik/issues">Let us know</a>.</span><span lang="zh-CN">遇到问题?欢迎<a href="https://github.com/TNTcraftHIM/Piik/issues">反馈</a>。</span></p>
</div>
</section>
@@ -875,11 +912,11 @@
</h2>
<p>
<span lang="en"
- >Self-host Piik on a Linux server or NAS with your own domain.
- Server packages support x64 and ARM64; Docker selects the matching architecture automatically.</span
+ >Run your own Piik site on Windows or Linux.
+ Windows packages support x64; Linux packages and Docker images support x64 and ARM64.</span
><span lang="zh-CN"
- >在 Linux 服务器或 NAS 上自部署 Piik,用自己的域名和朋友共享屏幕。
- 服务端程序支持 x64 和 ARM64,Docker 镜像会自动选择对应架构。</span
+ >在 Windows 或 Linux 上部署自己的 Piik 站点。
+ Windows 程序包支持 x64,Linux 程序包和 Docker 镜像支持 x64/ARM64。</span
>
</p>
<a class="docs-index" href="./docs/self-hosting.html" lang="en">Server downloads and setup<span aria-hidden="true"> ↗</span></a>
diff --git a/site/main.js b/site/main.js
index c990da97..38dc3f44 100644
--- a/site/main.js
+++ b/site/main.js
@@ -10,7 +10,7 @@ function syncDownloadLinks(picker) {
const selected = picker.querySelector('input:checked');
for (const link of picker.closest('.download-card').querySelectorAll('a')) {
const target = new URL(link.href);
- target.pathname = target.pathname.replace(/piik-app-[a-z0-9]+-[a-z0-9]+\.zip$/, `piik-app-${selected.value}.zip`);
+ target.pathname = target.pathname.replace(/piik-app-[a-z0-9]+-[a-z0-9]+(\.zip|\.apk)$/, `piik-app-${selected.value}$1`);
link.href = target.href;
}
}
diff --git a/site/styles.css b/site/styles.css
index 9e69f22a..968dac60 100644
--- a/site/styles.css
+++ b/site/styles.css
@@ -19,7 +19,7 @@
--comic-beat: 3.2s;
}
@media (prefers-color-scheme: dark) {
- :root:not([data-theme="light"]) { color-scheme: dark; --wall: var(--piik-dark-wall); --paper: var(--piik-dark-paper); --ink: var(--piik-dark-ink); --wall-2: var(--piik-dark-wall-2); --control-hover: var(--piik-dark-hover); --action: var(--piik-dark-action); --action-deep: var(--piik-dark-action); --action-ink: var(--piik-dark-action-ink); --action-text: var(--piik-dark-action-text); --control-selected: var(--piik-dark-selected); --control-selected-ink: var(--piik-dark-selected-ink); --orange: #f28d66; --soft-orange: #3d342c; }
+ :root:not([data-theme="light"]) { color-scheme: dark; --wall: var(--piik-dark-wall); --paper-rgb: var(--piik-dark-paper-rgb); --paper: var(--piik-dark-paper); --ink: var(--piik-dark-ink); --muted: var(--piik-dark-muted); --line: var(--piik-dark-line); --control-edge: var(--piik-dark-control-edge); --surface-rim: var(--piik-dark-surface-rim); --surface-shade-rgb: var(--piik-dark-surface-shade-rgb); --surface-shade-alpha: var(--piik-dark-surface-shade-alpha); --surface-glaze: var(--piik-dark-surface-glaze); --wall-2: var(--piik-dark-wall-2); --control-hover: var(--piik-dark-hover); --action: var(--piik-dark-action); --action-deep: var(--piik-dark-action); --action-ink: var(--piik-dark-action-ink); --action-text: var(--piik-dark-action-text); --control-selected: var(--piik-dark-selected); --control-selected-ink: var(--piik-dark-selected-ink); --orange: #f28d66; --soft-orange: #3d342c; }
}
:root[data-theme="dark"] { color-scheme: dark; --orange: #f28d66; --soft-orange: #3d342c; }
* { box-sizing: border-box; }
@@ -35,8 +35,8 @@ button, select, summary { cursor: pointer; }
button { background: none; }
button, a, select, summary { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--action); outline-offset: 5px; }
-h1, h2, h3, p { margin: 0; }
-h1, h2, h3 { text-wrap: balance; }
+h1, h2, h3, h4, p { margin: 0; }
+h1, h2, h3, h4 { text-wrap: balance; }
.phrase { display: inline-block; }
h1, h2 { font-weight: 750; letter-spacing: -.055em; }
p { text-wrap: pretty; }
@@ -113,12 +113,17 @@ h3 { font-size: 21px; font-weight: 700; letter-spacing: -.03em; line-height: 1.3
#download-release b { color: var(--ink); font-weight: 650; }
.download-release-action { text-decoration: underline; text-underline-offset: 4px; }
.download-release:is(:hover, :focus-visible) .download-release-action { color: var(--action-text); }
-.download-cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; margin-top: 32px; }
+.download-platforms { margin-top: 32px; }
+.download-group-title { color: var(--muted); font-size: 16px; font-weight: 650; text-align: center; }
+.download-cards { --download-gap: 18px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--download-gap); margin-top: 18px; }
+.download-cards--mobile { grid-template-columns: repeat(2, minmax(0, 1fr)); max-width: calc((100% + var(--download-gap)) * 2 / 3 - var(--download-gap)); margin-inline: auto; }
.download-card { display: grid; grid-row: span 6; grid-template-rows: subgrid; row-gap: 0; justify-items: start; align-items: start; padding: 28px; background: var(--paper); border: 1px solid var(--line); border-radius: 24px; box-shadow: 0 4px 0 color-mix(in srgb, var(--line) 40%, transparent); }
.platform-mark { display: grid; place-items: center; width: 52px; height: 52px; margin-bottom: 18px; border-radius: 16px; background: var(--wall); }
.platform-mark svg { width: 30px; height: 30px; }
-.download-card h3 { font-size: 25px; }
+.download-card h4 { font-size: 25px; }
.download-card > p { margin-top: 8px; color: var(--muted); font-size: 12px; line-height: 1.8; }
+.download-card > .download-variant { display: flex; align-items: center; align-self: center; min-height: var(--control-dense-size); margin: 16px 0 20px; line-height: 1.4; }
+.download-card > .download-note { grid-row: 6; }
.download-architecture { --selection-item-size: var(--control-dense-size); --control-border-width: 1px; min-width: 0; max-width: 100%; margin: 16px 0 20px; border-color: var(--line); border-radius: 24px; background: var(--wall); box-shadow: none; }
.download-architecture::before { border-radius: 18px; }
.download-architecture label > span { height: 100%; padding: 6px 12px; border-radius: 18px; font-size: 12px; }
diff --git a/src/client/components/living/AppConnectionButton.tsx b/src/client/components/living/AppConnectionButton.tsx
index c73822b5..d4fde859 100644
--- a/src/client/components/living/AppConnectionButton.tsx
+++ b/src/client/components/living/AppConnectionButton.tsx
@@ -8,9 +8,9 @@ import { Comic } from "./Comic";
import { HintComic } from "./hints";
import { LoadingStatus } from "./WaitingStatus";
import { useAnchoredPopover } from "./use-anchored-popover";
+import { androidAppIntent } from "../../native/activation";
interface AppConnectionProps {
- sourcePicker?: boolean;
onConnected?: () => void;
}
@@ -21,20 +21,10 @@ interface ConnectionAttempt {
cancel?: () => void;
}
-export function AppConnectionButton(props: AppConnectionProps) {
- // This is a desktop-package entry, not a test of Browser capture support.
- // iPad's desktop UA still identifies a touch-capable Mac platform.
- if (typeof navigator !== "undefined" && (
- /Android|iPhone|iPad|iPod/i.test(navigator.userAgent) ||
- (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1)
- )) return null;
- const button = <AppConnectionControl {...props} />;
- return props.sourcePicker ? <div className="lr-source-app-connection">{button}</div> : button;
-}
-
-function AppConnectionControl({ sourcePicker = false, onConnected }: AppConnectionProps) {
+export function AppConnectionButton({ onConnected }: AppConnectionProps) {
const { t, vis } = useCopy();
- const [state, setState] = useState<"idle" | "busy" | "approval" | "approving" | "ready" | "unavailable" | "incompatible">("idle");
+ const [state, setState] = useState<"idle" | "waking" | "busy" | "approval" | "approving" | "ready" | "unavailable" | "incompatible">("idle");
+ const wakeURL = androidAppIntent("piik://app");
const [helpOpen, setHelpOpen] = useState(false);
const downloadURL = useProjectLink("/#download", "https://github.com/TNTcraftHIM/Piik/releases/latest", helpOpen);
const helpId = useId();
@@ -44,7 +34,6 @@ function AppConnectionControl({ sourcePicker = false, onConnected }: AppConnecti
const pending = useRef<ConnectionAttempt | null>(null);
const healthCheck = useRef<AbortController | null>(null);
useEffect(() => {
- if (sourcePicker) return;
const refresh = async () => {
if (pending.current || help.current?.matches(":popover-open") || !isClientActivated()) return;
healthCheck.current?.abort();
@@ -67,43 +56,50 @@ function AppConnectionControl({ sourcePicker = false, onConnected }: AppConnecti
window.addEventListener("focus", onFocus);
const unwatch = watchClientActivation(onFocus);
return () => { healthCheck.current?.abort(); window.removeEventListener("focus", onFocus); unwatch(); };
- }, [sourcePicker]);
+ }, []);
useEffect(() => () => { pending.current?.controller.abort(); pending.current?.popup?.close(); }, []);
function showHelp(): void {
if (trigger.current) help.current?.showPopover({ source: trigger.current });
}
- async function connect(): Promise<void> {
+ async function connect(wake = false): Promise<void> {
if (pending.current) { pending.current.open?.(); return; }
- const manage = state === "ready";
+ let manage = state === "ready";
healthCheck.current?.abort();
// Discovery and its browser permission prompt stay in the current tab.
// A fresh click opens the real approval page without reserving a blank tab.
const attempt: ConnectionAttempt = { controller: new AbortController(), popup: null };
pending.current = attempt;
- setState("busy");
showHelp();
const signal = attempt.controller.signal;
let native: typeof import("../../native/client") | undefined;
let approving = false;
- const onMessage = async (event: MessageEvent) => {
- if (approving || !attempt.popup || event.source !== attempt.popup || !approvalOrigin || event.origin !== approvalOrigin ||
- event.data?.type !== "piik-app-approved" || event.data.site !== window.location.origin) return;
+ let waking = false;
+ const verifyApproval = async (reported = false) => {
+ if (approving || signal.aborted) return;
approving = true;
- // The approval message only asks us to recheck the App-owned permission.
const health = await native?.discoverNativeHealth({ signal }).catch(() => null);
if (signal.aborted) return;
- finish(health ? "ready" : "unavailable");
+ approving = false;
if (health) {
+ finish("ready");
activateClient();
onConnected?.();
- }
+ } else if (reported) finish("unavailable");
+ else if (attempt.popup?.closed) finish("idle");
+ };
+ const onMessage = (event: MessageEvent) => {
+ if (!attempt.popup || event.source !== attempt.popup || !approvalOrigin || event.origin !== approvalOrigin ||
+ event.data?.type !== "piik-app-approved" || event.data.site !== window.location.origin) return;
+ // A message or browser return asks us to verify App-owned permission.
+ void verifyApproval(true);
};
let approvalOrigin = "";
const finish = (result: typeof state, keepPage = false) => {
window.removeEventListener("message", onMessage);
window.removeEventListener("focus", onFocus);
+ document.removeEventListener("visibilitychange", onVisible);
attempt.controller.abort();
if (!keepPage) attempt.popup?.close();
if (pending.current === attempt) {
@@ -115,47 +111,74 @@ function AppConnectionControl({ sourcePicker = false, onConnected }: AppConnecti
};
attempt.cancel = () => finish(manage ? "ready" : "idle");
const onFocus = () => {
- if (attempt.popup?.closed && !approving) finish("idle");
+ if (waking) { attempt.open?.(); return; }
+ if (attempt.popup) void verifyApproval();
};
+ const onVisible = () => { if (document.visibilityState === "visible") onFocus(); };
window.addEventListener("message", onMessage);
window.addEventListener("focus", onFocus);
+ document.addEventListener("visibilitychange", onVisible);
signal.addEventListener("abort", () => {
window.removeEventListener("message", onMessage);
window.removeEventListener("focus", onFocus);
+ document.removeEventListener("visibilitychange", onVisible);
}, { once: true });
- try {
- native = await import("../../native/client");
- if (signal.aborted) return;
- const url = await native.discoverNativeConnect(signal);
- if (signal.aborted) return;
- if (!url) { finish("unavailable"); return; }
- approvalOrigin = new URL(url).origin;
+ const discover = async () => {
+ setState("busy");
+ try {
+ native = await import("../../native/client");
+ if (signal.aborted) return;
+ const url = await native.discoverNativeConnect(signal);
+ if (signal.aborted) return;
+ if (!url) { finish("unavailable"); return; }
+ const health = await native.discoverNativeHealth({ signal, waitForPermission: false });
+ if (signal.aborted) return;
+ manage = manage && health !== null;
+ if (health && !manage) {
+ finish("ready");
+ activateClient();
+ onConnected?.();
+ return;
+ }
+ approvalOrigin = new URL(url).origin;
+ attempt.open = () => {
+ if (signal.aborted || attempt.popup) return;
+ const destination = new URL(url);
+ if (manage) destination.searchParams.delete("site");
+ attempt.popup = window.open(destination.href, "_blank");
+ if (!attempt.popup) { finish("unavailable"); return; }
+ attempt.open = undefined;
+ if (manage) finish("ready", true);
+ else { setState("approving"); help.current?.hidePopover(); }
+ };
+ setState(manage ? "ready" : "approval");
+ } catch (error) {
+ if (!signal.aborted) finish(native && error instanceof native.NativeCompatibilityError ? "incompatible" : "unavailable");
+ }
+ };
+ if (wake && wakeURL) {
+ waking = true;
attempt.open = () => {
- if (signal.aborted || attempt.popup) return;
- const destination = new URL(url);
- if (manage) destination.searchParams.delete("site");
- attempt.popup = window.open(destination.href, "_blank");
- if (!attempt.popup) { finish("unavailable"); return; }
+ if (!waking || signal.aborted) return;
+ waking = false;
attempt.open = undefined;
- if (manage) finish("ready", true);
- else { setState("approving"); help.current?.hidePopover(); }
+ void discover();
};
- setState("approval");
- } catch (error) {
- if (!signal.aborted) finish(native && error instanceof native.NativeCompatibilityError ? "incompatible" : "unavailable");
- }
+ setState("waking");
+ window.location.assign(wakeURL);
+ } else await discover();
}
- const busy = state === "busy" || state === "approving";
+ const busy = state === "busy" || state === "waking" || state === "approving";
const label = t(state === "busy" ? "client.connect.discovering" : state === "approving" ? "client.connect.approving" : state === "approval" ? "client.connect.found" :
- state === "ready" ? "client.connect.manage" : state === "incompatible" ? "native.incompatible" :
+ state === "waking" ? "client.connect.returnToBrowser" : state === "ready" ? "client.connect.manage" : state === "incompatible" ? "native.incompatible" :
state === "unavailable" ? "client.connect.unavailable" : "client.connect.action");
const failed = state === "unavailable" || state === "incompatible";
return <><Tooltip kind={state === "ready" ? "hint-app-ready" : state === "unavailable" ? "signal-failed" : state === "incompatible" ? "warning" : "hint-client-site"}
- text={vis ? undefined : state === "idle" && !sourcePicker ? t("client.connect.hint") : label} disabled={helpOpen}
- place={sourcePicker ? "above" : "below"} align="end">
- <button ref={trigger} type="button" className={`${sourcePicker ? "lr-btn is-dense lr-source-picker-refresh lr-source-app-connect" : "lr-btn is-compact lr-app-connect"}${state === "ready" ? " is-ready" : ""}`}
- aria-label={sourcePicker ? t("client.connect.action") : label} aria-busy={busy || undefined}
+ text={vis ? undefined : state === "idle" ? t("client.connect.hint") : label} disabled={helpOpen}
+ place="below" align="end">
+ <button ref={trigger} type="button" className={`lr-btn is-compact lr-app-connect${state === "ready" ? " is-ready" : ""}`}
+ aria-label={label} aria-busy={busy || undefined}
popoverTarget={failed || state === "approval" ? helpId : undefined} aria-haspopup="dialog"
onClick={failed || state === "approval" ? undefined : event => {
// Failure may attach popoverTarget during this click; do not toggle the
@@ -177,7 +200,7 @@ function AppConnectionControl({ sourcePicker = false, onConnected }: AppConnecti
}}>
<div className="lr-app-connect-head">
{state === "busy" ? <LoadingStatus label="client.connect.discovering" /> :
- <div className="lr-app-connect-scene" aria-hidden="true">{helpOpen && (state === "approval" ? <HintComic kind="hint-client-site" /> :
+ <div className="lr-app-connect-scene" aria-hidden="true">{helpOpen && (state === "ready" ? <HintComic kind="hint-app-ready" /> : state === "approval" || state === "waking" ? <HintComic kind="hint-client-site" /> :
<Comic kind={state === "incompatible" ? "warning" : "signal-failed"} theme="paper" />)}</div>}
<button type="button" className="lr-btn is-dense is-quiet lr-app-connect-close" aria-label={t("common.close")}
popoverTarget={helpId} popoverTargetAction="hide">
@@ -186,9 +209,12 @@ function AppConnectionControl({ sourcePicker = false, onConnected }: AppConnecti
</div>
<p id={`${helpId}-message`} className={vis ? "visually-hidden" : undefined}>{state === "busy" ? t("client.connect.permissionHint") : label}</p>
<div className="lr-app-connect-actions">
- {state !== "busy" && <button type="button" className="lr-btn" aria-label={t(state === "approval" ? "client.connect.open" : "common.retry")} onClick={() => void connect()}>
- <Glyph name={state === "approval" ? "arrowRight" : "refresh"} size={16} /><span className={vis ? "visually-hidden" : undefined}>{t(state === "approval" ? "client.connect.open" : "common.retry")}</span>
+ {state !== "busy" && <button type="button" className="lr-btn" aria-label={t(state === "approval" || state === "ready" ? "client.connect.open" : state === "waking" ? "client.connect.continue" : "common.retry")} onClick={() => void connect()}>
+ <Glyph name={state === "approval" || state === "ready" ? "arrowRight" : "refresh"} size={16} /><span className={vis ? "visually-hidden" : undefined}>{t(state === "approval" || state === "ready" ? "client.connect.open" : state === "waking" ? "client.connect.continue" : "common.retry")}</span>
</button>}
+ {failed && wakeURL && <a className="lr-btn" href={wakeURL} onClick={event => { event.preventDefault(); void connect(true); }}>
+ <Glyph name="window" size={16} /><span className={vis ? "visually-hidden" : undefined}>{t("client.connect.wakeApp")}</span>
+ </a>}
{failed && <a className="lr-btn" href={downloadURL} target="_blank" rel="noopener noreferrer" aria-label={t("client.connect.download")}>
<Glyph name="arrowDown" size={16} /><span className={vis ? "visually-hidden" : undefined}>{t("client.connect.download")}</span>
</a>}
diff --git a/src/client/components/living/CaptureSourceCard.tsx b/src/client/components/living/CaptureSourceCard.tsx
index 1624a998..8af9e6a7 100644
--- a/src/client/components/living/CaptureSourceCard.tsx
+++ b/src/client/components/living/CaptureSourceCard.tsx
@@ -6,19 +6,20 @@ import type { HintKind } from "./hints";
/** One visual source card; its caller owns discovery and preview resources. */
export function CaptureSourceCard({ title, action, icon, hint, preview, disabled, selected,
nativeKey, cameraKey, buttonRef, onPreview, onSelect }: {
- title: string; action: string; icon: GlyphName; hint: HintKind; preview: string | null;
+ title: string; action: string; icon: GlyphName; hint: HintKind; preview?: string | null;
disabled?: boolean; selected?: boolean; nativeKey?: string; cameraKey?: string;
buttonRef?: Ref<HTMLButtonElement>; onPreview?: () => void; onSelect: () => void;
}) {
return <Tooltip kind={hint} text={title} className="lr-source-option-hint">
- <button ref={buttonRef} type="button" className="lr-source-option"
+ <button ref={buttonRef} type="button" className={`lr-source-option${preview === undefined ? " is-action" : ""}`}
data-native-source={nativeKey} data-camera-source={cameraKey}
aria-label={action} aria-current={selected || undefined} disabled={disabled}
onMouseEnter={onPreview} onFocus={onPreview} onClick={onSelect}>
+ {preview === undefined && <span className="lr-source-option-icon" aria-hidden="true"><Glyph name={icon} size={23} /></span>}
<span className="lr-source-option-copy"><strong>{title}</strong></span>
- <span className="lr-source-option-preview" aria-hidden="true">
+ {preview !== undefined && <span className="lr-source-option-preview" aria-hidden="true">
{preview ? <img src={preview} alt="" /> : <Glyph name={icon} size={23} />}
- </span>
+ </span>}
</button>
</Tooltip>;
}
diff --git a/src/client/components/living/CaptureSourcePicker.tsx b/src/client/components/living/CaptureSourcePicker.tsx
index 4df5adbe..bda2a1d9 100644
--- a/src/client/components/living/CaptureSourcePicker.tsx
+++ b/src/client/components/living/CaptureSourcePicker.tsx
@@ -11,21 +11,21 @@ import { Pill } from "./primitives";
import { CameraSources, useCameraSources } from "./CameraSources";
import { CaptureSourceCard } from "./CaptureSourceCard";
import { AudioExclusionSelect } from "./AudioExclusionSelect";
-import { AppConnectionButton } from "./AppConnectionButton";
import type { loadCameraPreviews } from "../../media/camera-previews";
-const SOURCE_TABS = ["browser", "camera", "window", "display"] as const;
+const SOURCE_TABS = ["browser", "camera", "window", "display", "picker"] as const;
type SourceTab = (typeof SOURCE_TABS)[number];
const SOURCE_ICONS = {
browser: "globe",
window: "window",
display: "display",
camera: "camera",
+ picker: "share",
} satisfies Record<SourceTab, GlyphName>;
export type NativeSourceList =
| { kind: "browser" }
- | { kind: "loading"; appDetected?: boolean }
+ | { kind: "loading"; appDetected?: boolean; sources?: NativeCaptureTarget[] }
| { kind: "unavailable" }
| { kind: "incompatible" }
| { kind: "unsupported" }
@@ -52,9 +52,8 @@ export function CaptureSourcePicker({
onPreview,
onRefresh,
onCancel,
- onAppConnected,
- browserAvailable = true,
- cameraAvailable = !!onCamera,
+ browserSupported = true,
+ cameraSupported = true,
initialTab = "window",
initialCamera = "",
activeCameraVideo,
@@ -78,9 +77,8 @@ export function CaptureSourcePicker({
) => Promise<string | null>;
onRefresh: () => void;
onCancel: () => void;
- onAppConnected?: () => void;
- browserAvailable?: boolean;
- cameraAvailable?: boolean;
+ browserSupported?: boolean;
+ cameraSupported?: boolean;
initialTab?: SourceTab;
initialAudio?: boolean;
initialShowCaptureBorder?: boolean;
@@ -91,20 +89,28 @@ export function CaptureSourcePicker({
const { vis, t } = useCopy();
const pickerId = useId();
const panelRef = useRef<HTMLDivElement>(null);
+ const bodyRef = useRef<HTMLDivElement>(null);
const [tab, setTab] = useState<SourceTab | null>(null);
const [shareAudio, setShareAudio] = useState(initialAudio);
const [showCaptureBorder, setShowCaptureBorder] = useState(initialShowCaptureBorder);
const [excludeAudio, setExcludeAudio] = useState(initialExcludeAudio);
const appDetected = nativeAppDetected(nativeSources);
- const tabs = SOURCE_TABS.filter((value) => appDetected || value === "browser" || value === "camera");
- const preferredTab = tab ?? initialTab;
- const tabAvailable = (value: SourceTab) => value === "camera" ? cameraAvailable
- : value === "browser" || nativeSources.kind === "browser" ? browserAvailable : true;
+ // Retained targets classify tabs during refresh; only ready targets are selectable.
+ const pickerOnly = (nativeSources.kind === "ready" || nativeSources.kind === "loading") && !!nativeSources.sources?.length &&
+ nativeSources.sources.every(target => target.kind === "picker");
+ const tabs = SOURCE_TABS.filter(value => value === "browser" ? browserSupported
+ : value === "camera" ? cameraSupported
+ : value === "picker" ? pickerOnly
+ : !pickerOnly && (appDetected || nativeSources.kind === "loading"));
+ const requestedTab = tab ?? initialTab;
+ const preferredTab = pickerOnly && (requestedTab === "window" || requestedTab === "display") ? "picker" : requestedTab;
+ const tabAvailable = (value: SourceTab) => value !== "camera" || !!onCamera;
const activeTab = tabs.includes(preferredTab) && tabAvailable(preferredTab)
- ? preferredTab : tabs.find(tabAvailable) ?? "browser";
- const nativeTab = activeTab === "window" || activeTab === "display";
+ ? preferredTab : tabs.find(tabAvailable) ?? tabs[0];
+ const nativeTab = activeTab === "window" || activeTab === "display" || activeTab === "picker";
+ useEffect(() => { bodyRef.current?.scrollTo({ top: 0 }); }, [activeTab]);
const browserCapture = activeTab === "browser";
- const cameraSources = useCameraSources(activeTab === "camera", activeCameraVideo, loadCameras);
+ const cameraSources = useCameraSources(activeTab === "camera" && !!onCamera, activeCameraVideo, loadCameras);
const supportsCaptureBorder = nativeTab && nativeSources.kind === "ready" && nativeSources.captureBorderControl === true;
const issueKey = !nativeTab ? null : nativeSources.kind === "incompatible" ? "native.incompatible"
: (nativeSources.kind === "unsupported" || nativeSources.kind === "failed")
@@ -224,7 +230,7 @@ export function CaptureSourcePicker({
</Tooltip>
</header>
- <div
+ {tabs.length > 0 && <div
className="lr-toggle lr-source-picker-tabs"
style={{ "--lr-segments": tabs.length, "--lr-selected-index": tabs.indexOf(activeTab) } as CSSProperties}
role="tablist"
@@ -259,7 +265,7 @@ export function CaptureSourcePicker({
return (
<Tooltip
key={value}
- kind={`hint-capture-${value}`}
+ kind={value === "picker" ? "hint-source-picker" : `hint-capture-${value}`}
text={vis ? undefined : t(value === "browser" ? "host.sourcePicker.browserHint" : `host.sourcePicker.tab.${value}`)}
place="below"
>
@@ -267,13 +273,14 @@ export function CaptureSourcePicker({
</Tooltip>
);
})}
- </div>
+ </div>}
<div
+ ref={bodyRef}
className="lr-source-picker-body"
- role="tabpanel"
+ role={activeTab ? "tabpanel" : undefined}
id={`${pickerId}-panel`}
- aria-labelledby={`${pickerId}-${activeTab}`}
+ aria-labelledby={activeTab ? `${pickerId}-${activeTab}` : undefined}
>
{nativeTab &&
nativeSources.kind === "ready" &&
@@ -331,7 +338,7 @@ export function CaptureSourcePicker({
) : null}
{activeTab === "camera" ? <CameraSources {...cameraSources} selected={initialCamera}
- disabled={selectionDisabled} onSelect={id => {
+ disabled={selectionDisabled || !onCamera} onSelect={id => {
cameraSources.cancel();
onCamera?.(id);
}} /> : <>
@@ -339,17 +346,17 @@ export function CaptureSourcePicker({
{browserCapture ? (
<button
type="button"
- className="lr-source-option is-browser"
+ className="lr-source-option is-action"
aria-label={t("host.sourcePicker.browser")}
- disabled={selectionDisabled || !tabAvailable(activeTab)}
+ disabled={selectionDisabled}
onClick={onBrowser}
>
<span className="lr-source-option-icon" aria-hidden="true">
- <Glyph name={SOURCE_ICONS[activeTab]} size={23} />
+ <Glyph name={SOURCE_ICONS.browser} size={23} />
</span>
{vis ? (
<span className="visually-hidden">
- {t(`host.sourcePicker.tab.${activeTab}`)}
+ {t("host.sourcePicker.tab.browser")}
</span>
) : (
<span className="lr-source-option-copy">
@@ -379,10 +386,6 @@ export function CaptureSourcePicker({
</div>
</>}
- {onAppConnected && (nativeSources.kind === "browser" || nativeSources.kind === "unavailable") && (
- <AppConnectionButton sourcePicker onConnected={onAppConnected} />
- )}
-
{nativeTab && nativeSources.kind === "loading" ? (
<span className="lr-source-picker-status" role="status" aria-label={t("host.sourcePicker.loading")}>
<Glyph name="refresh" className="lr-spin" size={18} />{!vis && t("host.sourcePicker.loading")}
@@ -393,10 +396,10 @@ export function CaptureSourcePicker({
<span
className="lr-source-picker-status"
role="status"
- aria-label={t("host.sourcePicker.empty")}
+ aria-label={t(activeTab ? "host.sourcePicker.empty" : "host.capture.noSource")}
>
<HintComic kind="hint-no-sources" size={240} />
- {vis ? null : t("host.sourcePicker.empty")}
+ {vis ? null : t(activeTab ? "host.sourcePicker.empty" : "host.capture.noSource")}
</span>
) : null}
</div>
@@ -468,6 +471,6 @@ function CaptureSourceOption({
return <CaptureSourceCard title={title} action={action}
icon={target.kind === "picker" ? "share" : target.kind}
hint={target.kind === "picker" ? "hint-source-picker" : target.kind === "display" ? "hint-capture-display" : "hint-capture-window"}
- preview={preview} nativeKey={nativeCaptureTargetKey(target)} buttonRef={buttonRef}
+ preview={target.kind === "picker" ? undefined : preview} nativeKey={nativeCaptureTargetKey(target)} buttonRef={buttonRef}
disabled={disabled} onPreview={requestPreview} onSelect={onSelect} />;
}
diff --git a/src/client/components/living/FloatingPanel.tsx b/src/client/components/living/FloatingPanel.tsx
index 2add4eb8..2aa00b37 100644
--- a/src/client/components/living/FloatingPanel.tsx
+++ b/src/client/components/living/FloatingPanel.tsx
@@ -221,8 +221,10 @@ export function FloatingPanel({ id, trigger, title, icon, compact = false, ancho
panelRef.current?.hidePopover(); focusTrigger();
}}><Glyph name="x" size={16} /></button>}
</header>
- {panelWindow.failed && <p className="lr-floating-error" role="status">{t("floating.detachFailed")}</p>}
- {children}
+ <div className="lr-floating-body lr-floating-scroll">
+ {panelWindow.failed && <p className="lr-floating-error" role="status">{t("floating.detachFailed")}</p>}
+ {children}
+ </div>
{!detached && (["nw", "se"] as const).map(corner => <span key={corner} className={`lr-floating-resize is-${corner}`}
role="group" tabIndex={0} aria-label={t("floating.resize")} aria-describedby={`${id}-resize-hint`}
onPointerDown={event => startGesture(event, corner)} onKeyDown={event => {
diff --git a/src/client/components/living/Header.tsx b/src/client/components/living/Header.tsx
index 242930ca..0867644e 100644
--- a/src/client/components/living/Header.tsx
+++ b/src/client/components/living/Header.tsx
@@ -162,13 +162,13 @@ export function AppHeader({
led,
homeHref = "/",
diagnosticControl,
- showAppConnection = true,
+ onAppConnected,
atHome = false,
}: {
led?: React.ReactNode;
homeHref?: string;
diagnosticControl?: React.ReactNode;
- showAppConnection?: boolean;
+ onAppConnected?: () => void;
atHome?: boolean;
}) {
const { t } = useCopy();
@@ -186,7 +186,7 @@ export function AppHeader({
<span className="lr-top-right">
{led}
<HeaderControls diagnosticControl={diagnosticControl}
- appControl={homeHref !== "/client" && showAppConnection ? <AppConnectionButton /> : undefined} />
+ appControl={homeHref !== "/client" ? <AppConnectionButton onConnected={onAppConnected} /> : undefined} />
</span>
</header>
);
diff --git a/src/client/components/living/LauncherForm.tsx b/src/client/components/living/LauncherForm.tsx
index 71730b57..b36427d6 100644
--- a/src/client/components/living/LauncherForm.tsx
+++ b/src/client/components/living/LauncherForm.tsx
@@ -1,7 +1,8 @@
-import { useId, useLayoutEffect, useRef, type FormEvent, type MouseEvent, type ReactNode } from "react";
+import { useId, useLayoutEffect, useRef, type CSSProperties, type FormEvent, type MouseEvent, type ReactNode } from "react";
import { BrandMark } from "./BrandMark";
import { Tooltip } from "./Tooltip";
import { WelcomeLine } from "./WelcomeLine";
+import { SiteAddressEntry } from "./SiteAddressEntry";
import { Btn, Pill } from "./primitives";
import { HintComic, type HintKind } from "./hints";
import { Glyph, type GlyphName } from "../../ui/icons";
@@ -34,15 +35,19 @@ const MODES: Array<{
// The product and film share this form; requests and navigation stay in the page.
export function LauncherForm({
mode,
+ modes,
onModeChange,
sites,
startupSite,
onToggleStartupSite,
onRemoveSite,
+ onSelectSite,
+ siteAddress,
siteBusy,
siteError,
target,
onOpen,
+ entryURL = target => target,
localAccessPassword,
onLocalAccessPasswordChange,
lan,
@@ -51,15 +56,19 @@ export function LauncherForm({
roomError,
}: {
mode: AppMode;
+ modes?: readonly AppMode[];
onModeChange: (mode: AppMode) => void;
sites: readonly string[];
startupSite: string;
onToggleStartupSite: (site: string) => void;
onRemoveSite: (site: string) => void;
+ onSelectSite?: (site: string) => void;
+ siteAddress?: string;
siteBusy?: boolean;
siteError?: string;
target?: string;
onOpen?: (event: MouseEvent<HTMLAnchorElement>) => void;
+ entryURL?: (target: string) => string;
localAccessPassword: string;
onLocalAccessPasswordChange: (password: string) => void;
lan?: {
@@ -72,6 +81,7 @@ export function LauncherForm({
roomError?: ReactNode;
}) {
const { vis, t } = useCopy();
+ const choices = modes ? MODES.filter(choice => modes.includes(choice.mode)) : MODES;
const accessHintId = useId();
const standbyRef = useRef<HTMLElement>(null);
const removalFocus = useRef<{ site: string; index: number } | null>(null);
@@ -119,26 +129,29 @@ export function LauncherForm({
{children}
<section ref={standbyRef} className="lr-client-standby" aria-label={t("client.connect.standby")}>
- <details className="lr-client-connect-guide">
- <summary>
- <span className="lr-client-standby-state">
- <Glyph name="check" size={18} />
- <strong className={vis ? "visually-hidden" : undefined}>{t("client.connect.standby")}</strong>
- </span>
- <span className="lr-client-connect-toggle">
- {vis && <Glyph name="window" size={17} />}
- <span className={vis ? "visually-hidden" : undefined}>{t("client.connect.guide")}</span>
- <Glyph name="chevron" size={14} />
- </span>
- </summary>
- <div className="lr-client-connect-steps">
- {vis && <HintComic kind="hint-client-site" />}
- <p className={vis ? "visually-hidden" : undefined}>{t("client.connect.standbyHint")}</p>
- </div>
- </details>
+ <div className="lr-client-standby-heading">
+ <details className="lr-client-connect-guide">
+ <summary>
+ <span className="lr-client-standby-state">
+ <Glyph name="check" size={18} />
+ <strong className={vis ? "visually-hidden" : undefined}>{t("client.connect.standby")}</strong>
+ </span>
+ <span className="lr-client-connect-toggle">
+ {vis && <Glyph name="window" size={17} />}
+ <span className={vis ? "visually-hidden" : undefined}>{t("client.connect.guide")}</span>
+ <Glyph name="chevron" size={14} />
+ </span>
+ </summary>
+ <div className="lr-client-connect-steps">
+ {vis && <HintComic kind="hint-client-site" />}
+ <p className={vis ? "visually-hidden" : undefined}>{t("client.connect.standbyHint")}</p>
+ </div>
+ </details>
+ {onSelectSite && <SiteAddressEntry initialAddress={siteAddress} onSelect={onSelectSite} />}
+ </div>
{siteError && <p role="alert">{siteError}</p>}
{sites.length > 0 && <div className="lr-client-approved-sites" role="list">{sites.map((site, index) => <div key={site} className="lr-client-approved-site" role="listitem">
- <a href={`${site}/#piik-client=1`} target="_blank" rel="noopener noreferrer" onClick={onOpen}>{site}<span aria-hidden="true">&nbsp;↗</span></a>
+ <a href={entryURL(`${site}/#piik-client=1`)} target="_blank" rel="noopener noreferrer" onClick={onOpen}>{site}<span aria-hidden="true">&nbsp;↗</span></a>
<div className="lr-client-site-actions">
<Btn icon="star" title="client.connect.openOnStart" size="dense" pressed={startupSite === site}
hint="hint-client-site" disabled={siteBusy} onClick={() => onToggleStartupSite(site)} />
@@ -151,24 +164,26 @@ export function LauncherForm({
</div>)}</div>}
</section>
- {roomError ?? (target ? <a className="lr-btn is-primary" href={target} onClick={onOpen} aria-label={t("client.launch.go")}>
+ {roomError}
+ {target ? <a className="lr-btn is-primary" href={entryURL(target)} onClick={onOpen} aria-label={t("client.launch.go")}>
<Glyph name="arrowRight" size={19} />{vis ? null : <span className="lr-cap">{t("client.launch.go")}</span>}
</a> : <>
<p className={vis ? "visually-hidden" : "lr-client-launch-section"}>{t("client.launch.room")}</p>
<div
className="lr-client-modes"
+ style={{ "--launcher-columns": choices.length } as CSSProperties}
role="radiogroup"
aria-label={t("client.launch.title")}
onKeyDown={(event) => {
if (!["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) return;
event.preventDefault();
const direction = event.key === "ArrowRight" || event.key === "ArrowDown" ? 1 : -1;
- const index = (MODES.findIndex((choice) => choice.mode === mode) + direction + MODES.length) % MODES.length;
- onModeChange(MODES[index]!.mode);
+ const index = (choices.findIndex((choice) => choice.mode === mode) + direction + choices.length) % choices.length;
+ onModeChange(choices[index]!.mode);
event.currentTarget.querySelectorAll<HTMLButtonElement>('[role="radio"]')[index]?.focus();
}}
>
- {MODES.map((choice) => {
+ {choices.map((choice) => {
const button = (
<button
key={choice.mode}
@@ -246,15 +261,15 @@ export function LauncherForm({
{t("client.launch.localHTTPS")}
</p>}
<Btn
- icon="arrowRight"
- title="client.launch.go"
- cap="client.launch.go"
+ icon={roomError ? "refresh" : "arrowRight"}
+ title={roomError ? "common.retry" : "client.launch.go"}
+ cap={roomError ? "common.retry" : "client.launch.go"}
tone="primary"
type="submit"
hint={MODES.find((choice) => choice.mode === mode)?.comic}
disabled={mode === "local" && lan !== undefined && !lan.selected}
/>
- </>)}
+ </>}
</form>
);
}
diff --git a/src/client/components/living/NumberControl.tsx b/src/client/components/living/NumberControl.tsx
new file mode 100644
index 00000000..ce4da5e4
--- /dev/null
+++ b/src/client/components/living/NumberControl.tsx
@@ -0,0 +1,55 @@
+import { useEffect, useId, useRef, useState, type ReactNode } from "react";
+import { useCopy } from "../../ui/copy";
+
+export function NumberControl({ value, min, max, step, unit, showUnit = true, name, label, disabled, onChange, className, children }: {
+ value: number;
+ min: number;
+ max: number;
+ step: number;
+ unit: string;
+ showUnit?: boolean;
+ name: string;
+ label: string;
+ disabled: boolean;
+ onChange: (value: number) => void;
+ className: string;
+ children: (input: ReactNode, select: (value: number) => void) => ReactNode;
+}) {
+ const { t } = useCopy();
+ const [invalid, setInvalid] = useState(false);
+ const errorId = useId();
+ const input = useRef<HTMLInputElement>(null);
+ useEffect(() => setInvalid(false), [value, min, max, step]);
+ const reset = () => { input.current!.value = String(value); setInvalid(false); };
+ const select = (next: number) => {
+ // A preset or slider replaces the draft, including a choice of the current value.
+ reset();
+ if (next !== value) onChange(next);
+ };
+ const number = <label className="lr-input lr-quality-number">
+ <input ref={input} type="number" inputMode={step < 1 ? "decimal" : "numeric"} name={name} key={value}
+ defaultValue={value} min={min} max={max} step={step} disabled={disabled} required aria-label={label}
+ aria-invalid={invalid || undefined} aria-describedby={invalid ? errorId : undefined}
+ onInput={() => setInvalid(false)}
+ onKeyDown={event => {
+ if (event.defaultPrevented || event.nativeEvent.isComposing ||
+ (event.key !== "Enter" && event.key !== "Escape")) return;
+ event.preventDefault();
+ event.stopPropagation();
+ if (event.key === "Escape") reset();
+ event.currentTarget.blur();
+ }} />
+ {showUnit && <span>{unit}</span>}
+ </label>;
+ return <div className={className} onBlur={event => {
+ if (event.currentTarget.contains(event.relatedTarget)) return;
+ const field = input.current!;
+ const valid = field.validity.valid && Number.isFinite(field.valueAsNumber);
+ setInvalid(!valid);
+ if (valid && field.valueAsNumber !== value) onChange(field.valueAsNumber);
+ }}>{children(number, select)}
+ {invalid && <small id={errorId} className="lr-quality-validation" role="alert">{t("host.quality.invalidNumber", {
+ min: String(min), max: String(max), step: String(step), unit,
+ })}</small>}
+ </div>;
+}
diff --git a/src/client/components/living/PlaybackControls.tsx b/src/client/components/living/PlaybackControls.tsx
index 752f55bf..a2bfd3f6 100644
--- a/src/client/components/living/PlaybackControls.tsx
+++ b/src/client/components/living/PlaybackControls.tsx
@@ -5,7 +5,7 @@ import { useCopy } from "../../ui/copy";
import { Glyph } from "../../ui/icons";
import { debugError } from "../../lib/debug";
import { Btn } from "./primitives";
-import { ViewerPlaybackSettings, type ViewerPlaybackSettingsProps } from "./ViewerPlaybackSettings";
+import { PlaybackPictureControls, PlaybackSettings, type PlaybackSettingsProps } from "./PlaybackSettings";
import { Tooltip } from "./Tooltip";
import { useAnchoredPopover } from "./use-anchored-popover";
import { usePlaybackControlsVisibility } from "./use-playback-controls-visibility";
@@ -43,7 +43,7 @@ export function PlaybackControls({
overlayAction?: ReactNode;
overlaySettings?: ReactNode;
remoteActions?: ReactNode;
- settings?: ViewerPlaybackSettingsProps;
+ settings?: PlaybackSettingsProps;
sharingAudio?: SharingAudioControl;
}) {
const audioRef = useRef<ViewerAudio | null>(null);
@@ -165,7 +165,7 @@ export function PlaybackControlsView({
togglePlayback: () => void; toggleFullscreen: () => void; toggleMute: () => void;
setLevel: (level: number) => void; onToggleTheater: () => void; onReconnect?: () => void;
reconnectAvailable?: boolean; overlayAction?: ReactNode; overlaySettings?: ReactNode; remoteActions?: ReactNode;
- settings?: ViewerPlaybackSettingsProps;
+ settings?: PlaybackSettingsProps;
sharingAudio?: boolean; audioControlAvailable?: boolean;
}) {
const { t, vis } = useCopy();
@@ -183,7 +183,6 @@ export function PlaybackControlsView({
const pipTitle = picture.failed ? "playback.pipFailed" : !picture.supported ? "playback.pipUnavailable"
: picture.active ? "playback.exitPip" : !canPlay ? "playback.pipWaiting" : "playback.pip";
const theaterTitle = theaterMode ? "viewer.theater.exit" : "viewer.theater";
- const nextFit = settings?.pictureFit === "contain" ? "cover" : settings?.pictureFit === "cover" ? "fill" : "contain";
const displayActions = <>
<Btn icon={picture.active ? "pipExit" : "pip"} draw="playback-pip" title={pipTitle}
hintTone={picture.failed ? "bad" : undefined}
@@ -232,16 +231,7 @@ export function PlaybackControlsView({
{remoteActions}
<span className="lr-playback-view" role="group" aria-label={t("playback.viewControls")}>
{overlayAction}
- {settings && <>
- <span className="lr-playback-fit"><Btn
- icon={settings.pictureFit === "contain" ? "fitContain" : settings.pictureFit === "cover" ? "fitCover" : "fitStretch"}
- title="playback.changeFit" hint={`hint-fit-${settings.pictureFit}`} hintText="playback.fitCycle"
- hintVars={{ current: t(`playback.fit.${settings.pictureFit}`), next: t(`playback.fit.${nextFit}`) }}
- onClick={() => settings.setPictureFit(nextFit)} /></span>
- <Btn icon="rotatePicture" title="playback.rotation" hint="hint-picture-rotation" hintText="playback.rotationHint"
- pressed={settings.pictureRotation !== 0}
- onClick={() => settings.setPictureRotation(settings.pictureRotation === 0 ? 90 : 0)} />
- </>}
+ {settings && <span className="lr-playback-picture-actions"><PlaybackPictureControls settings={settings} /></span>}
<PlaybackMore anchor={barRef} settings={settings} overlaySettings={overlaySettings}
onReconnect={onReconnect} reconnectAvailable={reconnectAvailable} />
</span>
@@ -268,14 +258,15 @@ export function PlaybackControlsView({
// Only detailed preferences belong here; direct playback actions stay outside.
function PlaybackMore({ anchor, settings, overlaySettings, onReconnect, reconnectAvailable }: {
- anchor: RefObject<HTMLDivElement | null>; settings?: ViewerPlaybackSettingsProps; overlaySettings?: ReactNode;
+ anchor: RefObject<HTMLDivElement | null>; settings?: PlaybackSettingsProps; overlaySettings?: ReactNode;
onReconnect?: () => void; reconnectAvailable?: boolean;
}) {
const { t } = useCopy();
const id = useId();
const panel = useRef<HTMLDivElement>(null);
const [open, setOpen] = useState(false);
- const available = Boolean(overlaySettings || settings?.route || settings?.relayVolunteer || onReconnect);
+ const detailed = Boolean(settings?.preview || settings?.remoteTouch || overlaySettings || settings?.route || settings?.relayVolunteer || onReconnect);
+ const available = detailed || Boolean(settings);
useAnchoredPopover(anchor, panel, { align: "center", minVisibleHeight: 160, enabled: available });
const reconnect = onReconnect ? <Btn icon="refresh" title="viewer.reconnect" size="dense" hint="hint-reconnect" draw="playback-reconnect"
cap="viewer.reconnect" disabled={!reconnectAvailable} onClick={() => {
@@ -283,11 +274,11 @@ function PlaybackMore({ anchor, settings, overlaySettings, onReconnect, reconnec
onReconnect();
}} /> : null;
if (!available) return null;
- return <span className="lr-playback-more">
+ return <span className={`lr-playback-more${detailed ? "" : " is-compact-only"}`}>
<Btn icon="sliders" title="playback.more" hint="hint-advanced" popoverTarget={id} expanded={open} controls={id} />
<div id={id} ref={panel} className="lr-floating-surface lr-playback-menu lr-playback-settings" popover="auto" aria-label={t("playback.more")}
onToggle={event => { if (event.target === event.currentTarget) setOpen(event.newState === "open"); }}>
- <ViewerPlaybackSettings {...settings} overlaySettings={overlaySettings} reconnect={reconnect} />
+ <PlaybackSettings {...settings} picture={settings} overlaySettings={overlaySettings} reconnect={reconnect} />
</div>
</span>;
}
diff --git a/src/client/components/living/PlaybackSettings.tsx b/src/client/components/living/PlaybackSettings.tsx
new file mode 100644
index 00000000..9427c5ed
--- /dev/null
+++ b/src/client/components/living/PlaybackSettings.tsx
@@ -0,0 +1,158 @@
+import { useId, type ReactNode } from "react";
+import type { ViewerRoutePreference } from "../../../shared/protocol";
+import type { VideoFit, VideoRotation } from "../../media/video-geometry";
+import type { RoomPreferences } from "../../lib/room-preferences";
+import { useCopy } from "../../ui/copy";
+import { Glyph } from "../../ui/icons";
+import { Btn, Cap, Chip, FieldCap, Pill, RowGroup, SwitchItem } from "./primitives";
+import { NumberControl } from "./NumberControl";
+import { Tooltip } from "./Tooltip";
+
+export interface PlaybackSettingsProps {
+ pictureFit: VideoFit;
+ setPictureFit: (fit: VideoFit) => void;
+ pictureRotation: VideoRotation;
+ setPictureRotation: (rotation: VideoRotation) => void;
+ preview?: {
+ autoPause: boolean;
+ onChange: (enabled: boolean) => void;
+ };
+ remoteTouch?: {
+ mode: RoomPreferences["remoteTouchMode"];
+ onChange: (mode: RoomPreferences["remoteTouchMode"]) => void;
+ };
+ route?: {
+ preference: ViewerRoutePreference;
+ sfuAvailable: boolean;
+ sfuOnly: boolean;
+ peerOnly: boolean;
+ disabled: boolean;
+ rejected: boolean;
+ onChange: (preference: ViewerRoutePreference) => void;
+ };
+ relayVolunteer?: {
+ enabled: boolean;
+ pending: boolean;
+ disabled: boolean;
+ capacity: number;
+ minimum: number;
+ maximum: number;
+ available: boolean;
+ onChange: (enabled: boolean) => void;
+ onCapacityChange: (capacity: number) => void;
+ };
+}
+
+export function PlaybackPictureControls({ settings, captions = false }: {
+ settings: Pick<PlaybackSettingsProps, "pictureFit" | "setPictureFit" | "pictureRotation" | "setPictureRotation">;
+ captions?: boolean;
+}) {
+ const { t } = useCopy();
+ const nextFit = settings.pictureFit === "contain" ? "cover" : settings.pictureFit === "cover" ? "fill" : "contain";
+ return <>
+ <Btn icon={settings.pictureFit === "contain" ? "fitContain" : settings.pictureFit === "cover" ? "fitCover" : "fitStretch"}
+ title="playback.changeFit" cap={captions ? `playback.fit.${settings.pictureFit}` : undefined}
+ hint={`hint-fit-${settings.pictureFit}`} hintText="playback.fitCycle"
+ hintVars={{ current: t(`playback.fit.${settings.pictureFit}`), next: t(`playback.fit.${nextFit}`) }}
+ onClick={() => settings.setPictureFit(nextFit)} />
+ <Btn icon="rotatePicture" title="playback.rotation" cap={captions ? "playback.rotation" : undefined}
+ hint="hint-picture-rotation" hintText="playback.rotationHint" pressed={settings.pictureRotation !== 0}
+ onClick={() => settings.setPictureRotation(settings.pictureRotation === 0 ? 90 : 0)} />
+ </>;
+}
+
+export function RemoteTouchSettings({ mode, onChange }: NonNullable<PlaybackSettingsProps["remoteTouch"]>) {
+ const { t, vis } = useCopy();
+ const id = useId();
+ const hint = mode === "direct" ? "remote.touchMode.directHint" : "remote.touchMode.trackpadHint";
+ return <section aria-label={t("remote.touchMode")}>
+ <div className="lr-viewer-settings-heading">
+ <h3><Glyph name="mouse" size={17} /><span className={vis ? "visually-hidden" : undefined}>{t("remote.touchMode")}</span></h3>
+ </div>
+ <div className="lr-toggle" role="radiogroup" aria-label={t("remote.touchMode")} aria-describedby={vis ? undefined : `${id}-hint`}>
+ {(["direct", "trackpad"] as const).map(value => <label key={value}>
+ <input type="radio" name={id} value={value} checked={mode === value}
+ aria-label={t(`remote.touchMode.${value}`)} onChange={() => onChange(value)} />
+ <span><Glyph name={value === "direct" ? "tv" : "mouse"} size={17} /><Cap k={`remote.touchMode.${value}`} /></span>
+ </label>)}
+ </div>
+ {!vis && <p id={`${id}-hint`}>{t(hint)}</p>}
+ </section>;
+}
+
+// Local display and connection intent; media and routing keep their owners.
+export function PlaybackSettings({ picture, preview, remoteTouch, route, relayVolunteer, overlaySettings, reconnect }: Pick<PlaybackSettingsProps, "preview" | "remoteTouch" | "route" | "relayVolunteer"> & {
+ picture?: PlaybackSettingsProps;
+ overlaySettings?: ReactNode; reconnect: ReactNode;
+}) {
+ const { t, vis } = useCopy();
+ const routeHint = route?.rejected ? "playback.routeRejected" : route?.sfuOnly ? "playback.routeSfuOnly"
+ : route?.peerOnly ? "playback.routePeerOnly" : !route?.sfuAvailable ? "playback.routeNoSfu"
+ : `playback.routeHint.${route.preference}` as const;
+ const detailedSections = [preview, remoteTouch, overlaySettings, route || relayVolunteer || reconnect].filter(Boolean).length;
+ return <div className={`lr-viewer-settings${detailedSections <= 1 ? " is-single" : ""}`}>
+ {picture && <section className="lr-playback-compact-picture" aria-label={t("playback.viewControls")}>
+ <div className="lr-viewer-settings-heading">
+ <h3><Glyph name="tv" size={17} /><span className={vis ? "visually-hidden" : undefined}>{t("playback.viewControls")}</span></h3>
+ </div>
+ <div className="lr-playback-picture-options"><PlaybackPictureControls settings={picture} captions /></div>
+ </section>}
+ {preview && <section aria-label={t("host.preview")}>
+ <div className="lr-viewer-settings-heading">
+ <h3><Glyph name="tv" size={17} /><span className={vis ? "visually-hidden" : undefined}>{t("host.preview")}</span></h3>
+ </div>
+ <RowGroup><SwitchItem checked={preview.autoPause} onChange={preview.onChange} icon="pause"
+ label={t("host.previewAutoPause")} note={t("host.previewAutoPauseHint")} hint="hint-local-pause" /></RowGroup>
+ {!vis && <p>{t("host.previewAutoPauseHint")}</p>}
+ </section>}
+ {remoteTouch && <RemoteTouchSettings {...remoteTouch} />}
+ {overlaySettings && <section aria-label={t("playback.overlay")}>
+ <div className="lr-viewer-settings-heading">
+ <h3><Glyph name="danmaku" size={17} /><span className={vis ? "visually-hidden" : undefined}>{t("playback.overlay")}</span></h3>
+ </div>
+ {overlaySettings}
+ </section>}
+ {(route || relayVolunteer || reconnect) && <section aria-label={t("playback.connection")}>
+ <div className="lr-viewer-settings-heading">
+ <h3><Glyph name="branch" size={17} /><span className={vis ? "visually-hidden" : undefined}>{t("playback.connection")}</span></h3>
+ {reconnect}
+ </div>
+ {route && <>
+ <RowGroup>{(["auto", "p2p", "sfu"] as const).map(preference => {
+ const locked = preference === "p2p" && route.sfuOnly ? "playback.routeSfuOnly"
+ : preference === "sfu" && !route.sfuAvailable ? route.peerOnly ? "playback.routePeerOnly" : "playback.routeNoSfu" : undefined;
+ return <Chip key={preference}
+ selected={route.preference === preference} title={t(`playback.route.${preference}`)}
+ disabled={route.disabled || Boolean(locked)}
+ hint={locked ? preference === "p2p" ? "hint-route-sfu-required" : "hint-route-p2p-required"
+ : preference === "auto" ? "hint-route-auto" : preference === "p2p" ? "hint-route-p2p" : "hint-route-sfu"}
+ hintText={t(locked ?? `playback.routeHint.${preference}`)}
+ onClick={() => { if (route.preference !== preference) route.onChange(preference); }}>
+ {vis && <Glyph name={preference === "auto" ? "branch" : preference === "p2p" ? "network" : "server"} size={17} />}
+ <Cap k={`playback.route.${preference}`} />
+ </Chip>;
+ })}</RowGroup>
+ {!vis && <p className={route.rejected ? "is-error" : undefined} role={route.rejected ? "status" : undefined}>{t(routeHint)}</p>}
+ {vis && route.rejected && <Pill icon="alert" tone="bad" label={t(routeHint)} comic="settings-failed" />}
+ </>}
+ {relayVolunteer && <div className="lr-viewer-relay-setting" aria-busy={relayVolunteer.pending}>
+ <div className="lr-door-group">
+ <RowGroup>
+ {vis && <Glyph name="branch" size={18} />}
+ <SwitchItem checked={relayVolunteer.enabled} disabled={relayVolunteer.disabled}
+ onChange={relayVolunteer.onChange} label={t("playback.relayVolunteer")}
+ note={t(relayVolunteer.available ? "playback.relayVolunteerHint" : "playback.relayUnavailable")} hint="hint-relay-volunteer" />
+ </RowGroup>
+ {relayVolunteer.enabled && <NumberControl value={relayVolunteer.capacity} min={relayVolunteer.minimum} max={relayVolunteer.maximum}
+ step={1} unit={t("playback.relayPeople")} showUnit={!vis} name="relay-capacity" label={t("playback.relayLimit")}
+ disabled={relayVolunteer.disabled || !relayVolunteer.available} onChange={relayVolunteer.onCapacityChange}
+ className="lr-door-group">{input => <div className="lr-quality-heading">
+ {vis ? <Glyph name="branch" size={16} /> : <FieldCap k="playback.relayLimit" />}
+ <Tooltip kind="hint-relay-volunteer" text={`${t("playback.relayLimit")}\n${t("playback.relayVolunteerHint")}`}>{input}</Tooltip>
+ </div>}</NumberControl>}
+ </div>
+ {!vis && <p>{t(relayVolunteer.available ? "playback.relayVolunteerHint" : "playback.relayUnavailable")}</p>}
+ </div>}
+ </section>}
+ </div>;
+}
diff --git a/src/client/components/living/QualityPresets.tsx b/src/client/components/living/QualityPresets.tsx
index 14abc0d2..b5b655ed 100644
--- a/src/client/components/living/QualityPresets.tsx
+++ b/src/client/components/living/QualityPresets.tsx
@@ -1,4 +1,4 @@
-import { Fragment, useEffect, useId, useRef, useState, type CSSProperties, type ReactNode } from "react";
+import { Fragment, useEffect, useId, useRef, useState, type CSSProperties } from "react";
import { MIN_AUDIO_BITRATE } from "../../../shared/protocol";
import {
QUALITY_PROFILES, QUALITY_PROFILE_KEYS, QUALITY_RESOLUTIONS, DEGRADATION_PREFERENCE_KEYS, qualityResolutionEqual,
@@ -10,6 +10,7 @@ import { useCopy, type CopyKey } from "../../ui/copy";
import { Glyph } from "../../ui/icons";
import { Cap, Chip } from "./primitives";
import { QualityTileGlyph } from "./QualityTileGlyph";
+import { NumberControl } from "./NumberControl";
import { Tooltip } from "./Tooltip";
const PRESET_LABELS: Record<QualityProfileId, CopyKey> = {
@@ -18,6 +19,11 @@ const PRESET_LABELS: Record<QualityProfileId, CopyKey> = {
"1080p60": "host.quality.highFps",
};
+const RESOLUTION_HINTS: Partial<Record<keyof typeof QUALITY_RESOLUTIONS, CopyKey>> = {
+ "1440p": "host.video.resolution1440p",
+ "2160p": "host.video.resolution2160p",
+};
+
export function QualityPresets({ selected, limits = VIDEO_CAPTURE_LIMITS, disabled = false, busy = false, onSelect }: {
selected: QualityProfileId | null;
limits?: VideoCaptureLimits;
@@ -124,7 +130,10 @@ export function ResolutionControl({ value, limits, disabled, onChange }: {
{Object.entries(QUALITY_RESOLUTIONS).map(([label, resolution]) => <Chip key={label}
selected={qualityResolutionEqual(value, resolution)}
disabled={disabled || captureFramerateCeiling(resolution, limits) < MIN_VIDEO_FRAMERATE}
- title={label} hint="hint-quality" onClick={() => {
+ title={label} hint="hint-quality"
+ hintText={t(RESOLUTION_HINTS[label as keyof typeof QUALITY_RESOLUTIONS] ?? "host.video.resolutionPreset", {
+ width: String(resolution.width), height: String(resolution.height),
+ })} onClick={() => {
// Selecting even the current preset replaces an unsubmitted draft.
reset(controls.current!);
onChange(resolution);
@@ -142,58 +151,6 @@ const VOICE_QUALITY_CAPTIONS: Record<ScreenAudioPreset, CopyKey> = {
saver: "voice.quality.saver", music: "voice.quality.standard", "very-high": "voice.quality.clear",
};
-function QualityNumberControl({ value, min, max, step, unit, name, label, disabled, onChange, className, children }: {
- value: number;
- min: number;
- max: number;
- step: number;
- unit: string;
- name: string;
- label: string;
- disabled: boolean;
- onChange: (value: number) => void;
- className: string;
- children: (input: ReactNode, select: (value: number) => void) => ReactNode;
-}) {
- const { t } = useCopy();
- const [invalid, setInvalid] = useState(false);
- const errorId = useId();
- const input = useRef<HTMLInputElement>(null);
- useEffect(() => setInvalid(false), [value, min, max, step]);
- const reset = () => { input.current!.value = String(value); setInvalid(false); };
- const select = (next: number) => {
- // A preset or slider replaces the draft, including a choice of the current value.
- reset();
- if (next !== value) onChange(next);
- };
- const number = <label className="lr-input lr-quality-number">
- <input ref={input} type="number" inputMode={step < 1 ? "decimal" : "numeric"} name={name} key={value}
- defaultValue={value} min={min} max={max} step={step} disabled={disabled} required aria-label={label}
- aria-invalid={invalid || undefined} aria-describedby={invalid ? errorId : undefined}
- onInput={() => setInvalid(false)}
- onKeyDown={event => {
- if (event.defaultPrevented || event.nativeEvent.isComposing ||
- (event.key !== "Enter" && event.key !== "Escape")) return;
- event.preventDefault();
- event.stopPropagation();
- if (event.key === "Escape") reset();
- event.currentTarget.blur();
- }} />
- <span>{unit}</span>
- </label>;
- return <div className={className} onBlur={event => {
- if (event.currentTarget.contains(event.relatedTarget)) return;
- const field = input.current!;
- const valid = field.validity.valid && Number.isFinite(field.valueAsNumber);
- setInvalid(!valid);
- if (valid && field.valueAsNumber !== value) onChange(field.valueAsNumber);
- }}>{children(number, select)}
- {invalid && <small id={errorId} className="lr-quality-validation" role="alert">{t("host.quality.invalidNumber", {
- min: String(min), max: String(max), step: String(step), unit,
- })}</small>}
- </div>;
-}
-
export function VideoRateControls({ value, limits, disabled, onChange }: {
value: QualityProfile;
limits: VideoCaptureLimits;
@@ -221,7 +178,7 @@ export function VideoRateControls({ value, limits, disabled, onChange }: {
const commonValue = Math.min(sliderMax, Math.max(control.sliderMin,
Math.round(control.value / control.sliderStep) * control.sliderStep));
const custom = !hasCommonRange || control.value !== commonValue;
- return <QualityNumberControl key={control.name} {...control}
+ return <NumberControl key={control.name} {...control}
className="lr-quality-rate" label={t(control.label)}
disabled={disabled || control.max < control.min} onChange={control.change}>
{(input, select) => <>
@@ -265,7 +222,7 @@ export function VideoRateControls({ value, limits, disabled, onChange }: {
</div>
</>}
</>}
- </QualityNumberControl>;
+ </NumberControl>;
})}
</div>;
}
@@ -281,7 +238,7 @@ export function AudioBitrateControl({ value, max, disabled, onChange, purpose =
const title = purpose === "voice" ? "voice.bitrate" : "host.advanced.audio";
const presets = purpose === "voice" ? { saver: 32_000, music: 64_000, "very-high": 96_000 } : SCREEN_AUDIO_BITRATES;
const captions = purpose === "voice" ? VOICE_QUALITY_CAPTIONS : AUDIO_QUALITY_CAPTIONS;
- return <QualityNumberControl value={value / 1_000} min={MIN_AUDIO_BITRATE / 1_000} max={max / 1_000} step={1}
+ return <NumberControl value={value / 1_000} min={MIN_AUDIO_BITRATE / 1_000} max={max / 1_000} step={1}
name="audioBitrate" unit="kbps" label={t(purpose === "voice" ? "voice.bitrate" : "host.advanced.audioBitrate")} className="lr-door-group lr-audio-quality"
disabled={disabled} onChange={kbps => onChange(kbps * 1_000)}>
{(input, select) => <>
@@ -302,7 +259,7 @@ export function AudioBitrateControl({ value, max, disabled, onChange, purpose =
})}
</div>
</>}
- </QualityNumberControl>;
+ </NumberControl>;
}
export function PicturePreferenceControl({ value, disabled, onChange }: {
diff --git a/src/client/components/living/RemoteControl.tsx b/src/client/components/living/RemoteControl.tsx
index 18c83202..fafa4e89 100644
--- a/src/client/components/living/RemoteControl.tsx
+++ b/src/client/components/living/RemoteControl.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
+import { useCallback, useEffect, useLayoutEffect, useRef, useState, type RefObject } from "react";
import { createPortal } from "react-dom";
import { useCopy, type CopyKey } from "../../ui/copy";
import { Glyph } from "../../ui/icons";
@@ -8,8 +8,9 @@ import { RemoteTouchpad } from "../../remote-control/touchpad";
import { RemoteViewport } from "../../remote-control/viewport";
import { RemoteGamepad } from "../../remote-control/gamepad";
import { matchesRemoteShortcut, remoteTextAvailable, remoteGamepadAvailable } from "../../remote-control/protocol";
-import { readRoomPreferences, saveRoomPreferences } from "../../lib/room-preferences";
+import { readRoomPreferences, saveRoomPreferences, type RoomPreferences } from "../../lib/room-preferences";
import { RemoteKeyboard } from "./RemoteKeyboard";
+import { RemoteMouse, placeRemotePointer, remotePointerRoom } from "./RemoteMouse";
import { Btn } from "./primitives";
import { videoFullscreenState, type FullscreenVideo } from "./video-fullscreen";
import { picturePoint, pictureRect, rotatePicturePoint, rotatePictureDelta, videoRotation, type VideoFit, type VideoRotation } from "../../media/video-geometry";
@@ -17,11 +18,12 @@ import "./remote-control.css";
// The video keeps its existing media owner. This temporary surface handles
// input only while its current routed endpoint holds a local App grant.
-export function RemoteControl({ channel, videoRef, available, pictureFit, pictureRotation = 0 }: {
+export function RemoteControl({ channel, videoRef, available, pictureFit, pictureRotation = 0, touchMode = "direct" }: {
channel: InputTransport | null; videoRef: RefObject<HTMLVideoElement | null>;
available: boolean;
pictureFit?: VideoFit;
pictureRotation?: VideoRotation;
+ touchMode?: RoomPreferences["remoteTouchMode"];
}) {
const { t, vis } = useCopy();
const [state, setState] = useState<RemoteInputState | null>(null);
@@ -34,19 +36,21 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
const [keyboardOpen, setKeyboardOpen] = useState(false);
const [keyboardDocument, setKeyboardDocument] = useState<Document | null>(null);
const keyboardDetached = Boolean(keyboardDocument && keyboardDocument !== document);
- const [touch, setTouch] = useState(false);
+ const [touchPointer, setTouchPointer] = useState<RemoteTouchpad | null>(null);
+ const touch = touchPointer !== null;
const [releaseKey, setReleaseKey] = useState(() => readRoomPreferences().remoteReleaseKey);
const session = useRef<RemoteInputSession | null>(null);
const surface = useRef<HTMLDivElement | null>(null);
- const cursor = useRef<SVGSVGElement | null>(null);
+ const cursor = useRef<HTMLDivElement | null>(null);
const touchpad = useRef<RemoteTouchpad | null>(null);
+ const mouseButtons = useRef(new Set<number>());
const viewport = useRef<RemoteViewport | null>(null);
const canControl = available && presentation === "playing";
const active = state?.state === "active" && canControl && channel?.readyState === "open";
const controlled = (state?.state === "active" || state?.state === "suspended") && canControl && channel?.readyState === "open";
const gamepadAvailable = state !== null && remoteGamepadAvailable(state) && typeof navigator.getGamepads === "function";
const input = useCallback((event: RemoteInputEvent) => session.current?.input(event), []);
- const reset = useCallback(() => { touchpad.current?.reset(); session.current?.input({ type: "reset" }); }, []);
+ const reset = useCallback(() => { mouseButtons.current.clear(); touchpad.current?.reset(); session.current?.input({ type: "reset" }); }, []);
const closeKeyboard = () => {
reset(); setKeyboardOpen(false); setKeyboardDocument(null); surface.current?.focus({ preventScroll: true });
};
@@ -86,22 +90,21 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
const current = new RemoteGamepad(input, setGamepadConnected);
return () => current.dispose();
}, [controlled, gamepadAvailable, state?.grant, channel, input]);
- useEffect(() => {
+ useLayoutEffect(() => {
const video = videoRef.current;
const element = surface.current;
- if (!controlled || !video || !element) { setTouch(false); return; }
+ setTouchPointer(null);
+ if (!controlled || !video || !element) return;
const updateCursor = () => {
const picture = pictureRect(video);
const rect = surface.current?.getBoundingClientRect();
if (!picture || !rect || !cursor.current) return;
const position = rotatePicturePoint(current.position, videoRotation(video));
- cursor.current.style.left = `${picture.left - rect.left + position.x * picture.width}px`;
- cursor.current.style.top = `${picture.top - rect.top + position.y * picture.height}px`;
+ placeRemotePointer(cursor.current,
+ picture.left - rect.left + position.x * picture.width,
+ picture.top - rect.top + position.y * picture.height);
};
- const view = new RemoteViewport(video, element, () => {
- current.position = view.visiblePosition(current.position);
- updateCursor();
- });
+ const view = new RemoteViewport(video, element, updateCursor);
viewport.current = view;
const current = new RemoteTouchpad(
() => {
@@ -110,23 +113,29 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
},
event => session.current?.input(event),
delta => session.current?.scroll(delta, 0),
- updateCursor,
+ () => {
+ view.follow(current.position, remotePointerRoom(cursor.current));
+ updateCursor();
+ },
view,
);
touchpad.current = current;
+ setTouchPointer(matchMedia("(pointer: coarse)").matches ? current : null);
// Layout changes and decoded aspect-ratio changes share the input geometry.
- const refresh = () => view.refresh();
+ const refresh = () => { view.refresh(); current.refresh(); };
const observer = new ResizeObserver(refresh);
observer.observe(video);
video.addEventListener("resize", refresh);
return () => {
observer.disconnect(); video.removeEventListener("resize", refresh);
+ mouseButtons.current.clear();
current.reset(); view.reset(); touchpad.current = null; viewport.current = null;
};
- }, [controlled, channel, videoRef]);
- useEffect(() => { viewport.current?.refresh(); }, [pictureFit]);
+ }, [controlled, state?.grant, channel, videoRef]);
+ useLayoutEffect(() => { if (touchPointer) { viewport.current?.refresh(); touchPointer.refresh(); } }, [touchPointer]);
+ useEffect(() => { viewport.current?.refresh(); touchpad.current?.refresh(); }, [pictureFit]);
useEffect(() => {
- reset(); viewport.current?.reset(); viewport.current?.refresh();
+ reset(); viewport.current?.reset(); viewport.current?.refresh(); touchpad.current?.refresh();
}, [pictureRotation, reset]);
useEffect(() => {
const video = videoRef.current as FullscreenVideo | null;
@@ -154,9 +163,11 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
element?.focus({ preventScroll: true });
const wheel = (event: WheelEvent) => { event.preventDefault(); session.current?.scroll(event.deltaY, event.deltaMode); };
element?.addEventListener("wheel", wheel, { passive: false });
- // Release can land outside the picture after a drag. Observe it for this
- // grant only; the native owner ignores buttons it never pressed.
- const mouseUp = (event: MouseEvent) => session.current?.input({ type: "button", button: event.button, down: false });
+ // Release a physical press even outside the picture. Virtual tools own
+ // their presses; clicking one must not release another finger's button.
+ const mouseUp = (event: MouseEvent) => {
+ if (mouseButtons.current.delete(event.button)) session.current?.input({ type: "button", button: event.button, down: false });
+ };
window.addEventListener("mouseup", mouseUp, true);
const lockChanged = () => {
reset(); setLocked(document.pointerLockElement === element);
@@ -165,6 +176,7 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
window.addEventListener("blur", reset);
document.addEventListener("pointerlockchange", lockChanged);
return () => {
+ mouseButtons.current.clear();
element?.removeEventListener("wheel", wheel);
window.removeEventListener("mouseup", mouseUp, true);
window.removeEventListener("blur", reset);
@@ -211,11 +223,12 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
} else session.current?.request();
};
const touchControls = touch || matchMedia("(pointer: coarse)").matches;
- const statusIcon = state.reason === "busy" ? "users" : state.reason === "password" || state.reason === "gamepad" || state.state === "failed" ? "alert"
+ const statusIcon = state.reason === "busy" ? "users" : state.reason === "password" || state.reason === "gamepad" || state.state === "failed" || state.inputFailed ? "alert"
: state.state === "suspended" ? "pause" : gamepadConnected && controlled ? "gamepad" : "clock";
const feedback: CopyKey | undefined = state.reason ? `remote.reason.${state.reason}`
: state.state === "suspended" ? "remote.suspended" : state.state === "failed" ? "remote.failed"
: state.state === "pending" ? state.mode === "ask" ? "remote.pending" : "remote.connecting"
+ : state.inputFailed ? "remote.inputFailed"
: gamepadConnected && controlled ? "remote.gamepadHelp"
: undefined;
const parent = videoRef.current?.parentElement;
@@ -224,7 +237,7 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
<span className="lr-remote-viewer-control">
<Btn icon="mouse" title={presentation === "external" ? "remote.inlineRequired" : presentation === "paused" ? "remote.playbackRequired" : state.state === "unavailable" ? "remote.unavailable" : buttonTitle}
hint={holding ? "hint-remote-stop" : "hint-remote-control"}
- hintText={feedback} hintTone={state.state === "failed" ? "warn" : undefined}
+ hintText={feedback} hintTone={state.state === "failed" || state.inputFailed ? "warn" : undefined}
busy={requesting} pressed={controlled} disabled={!canControl || state.state === "unavailable"}
onClick={() => requestControl()} />
{feedback && !passwordOpen && <span className="lr-remote-feedback" role="status">
@@ -276,7 +289,7 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
event.preventDefault(); event.stopPropagation();
touchpad.current?.move(event.pointerId, event.clientX, event.clientY); return;
}
- setTouch(false);
+ setTouchPointer(null);
if (document.pointerLockElement === surface.current) {
const delta = rotatePictureDelta({ x: event.movementX, y: event.movementY }, pictureRotation, true);
input({ type: "relative", dx: delta.x, dy: delta.y });
@@ -291,9 +304,9 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
onPointerDown={event => {
if (document.pointerLockElement !== surface.current) event.currentTarget.setPointerCapture(event.pointerId);
if (event.pointerType === "touch") {
- event.preventDefault(); event.stopPropagation(); setTouch(true);
+ event.preventDefault(); event.stopPropagation(); setTouchPointer(touchpad.current);
surface.current?.focus({ preventScroll: true });
- touchpad.current?.down(event.pointerId, event.clientX, event.clientY);
+ touchpad.current?.down(event.pointerId, event.clientX, event.clientY, point(event.clientX, event.clientY));
}
}}
onPointerUp={event => {
@@ -308,9 +321,9 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
onMouseDown={event => {
if (event.button > 4) return;
event.preventDefault(); surface.current?.focus({ preventScroll: true });
- if (document.pointerLockElement === surface.current) { input({ type: "button", button: event.button, down: true }); return; }
+ if (document.pointerLockElement === surface.current) { mouseButtons.current.add(event.button); input({ type: "button", button: event.button, down: true }); return; }
const position = point(event.clientX, event.clientY);
- if (position) { input({ type: "move", ...position }); input({ type: "button", button: event.button, down: true }); }
+ if (position) { mouseButtons.current.add(event.button); input({ type: "move", ...position }); input({ type: "button", button: event.button, down: true }); }
}}
onPointerCancel={reset}
onLostPointerCapture={event => {
@@ -325,9 +338,7 @@ export function RemoteControl({ channel, videoRef, available, pictureFit, pictur
if (!event.nativeEvent.isComposing) input({ type: "key", code: event.code, down: true });
}}
onKeyUp={event => { event.preventDefault(); event.stopPropagation(); input({ type: "key", code: event.code, down: false }); }}>
- <svg ref={cursor} className="lr-remote-touch-cursor" viewBox="0 0 24 28" aria-hidden="true" style={{ display: touch ? undefined : "none" }}>
- <path d="M2 2v21l6-6 5 9 4-2-5-9h9Z" />
- </svg>
+ {active && touchPointer && <RemoteMouse key={state.grant} pointer={touchPointer} positionRef={cursor} pointAt={point} touchMode={touchMode} />}
</div>}
{active && keyboardOpen && <RemoteKeyboard input={input} touch={touchControls} platform={state.platform}
allowText={remoteTextAvailable(state)}
diff --git a/src/client/components/living/RemoteMouse.tsx b/src/client/components/living/RemoteMouse.tsx
new file mode 100644
index 00000000..70698079
--- /dev/null
+++ b/src/client/components/living/RemoteMouse.tsx
@@ -0,0 +1,272 @@
+import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState, type PointerEvent, type RefObject } from "react";
+import { REMOTE_TOUCH_HOLD_MS, type RemoteTouchpad } from "../../remote-control/touchpad";
+import type { PicturePoint } from "../../media/video-geometry";
+import type { RoomPreferences } from "../../lib/room-preferences";
+import { useCopy } from "../../ui/copy";
+import { Glyph } from "../../ui/icons";
+import { useSurfaceDocument } from "../../ui/panel-window";
+import { PLAYER_CONTROLS_IDLE_MS } from "../../ui/control-timing";
+
+export function placeRemotePointer(layer: HTMLDivElement, x: number, y: number): void {
+ layer.style.setProperty("--remote-x", `${x}px`);
+ layer.style.setProperty("--remote-y", `${y}px`);
+}
+
+export function remotePointerRoom(layer: HTMLDivElement | null): { right: number; bottom: number } | null {
+ if (!layer) return null;
+ const cursor = layer.querySelector<SVGSVGElement>(".lr-remote-touch-cursor");
+ if (!cursor) return null;
+ const point = cursor.getBoundingClientRect();
+ const mouse = layer.querySelector<HTMLElement>(".lr-remote-mouse-widget.is-expanded")?.getBoundingClientRect();
+ return { right: Math.max(point.width, mouse ? mouse.right - point.left : 0),
+ bottom: Math.max(point.height, mouse ? mouse.bottom - point.top : 0) };
+}
+
+type MouseMode = "mini" | "expanded" | "left" | "right" | "top" | "bottom";
+type MouseContact = { x: number; y: number; moved: boolean; kind: "widget" | "pointer"; button?: 0 | 2 };
+type MouseWheel = { id: number; x: number; y: number; phase: "pending" | "scroll" | "middle"; timer: ReturnType<typeof setTimeout> | null };
+
+export function RemoteMouse({ pointer, positionRef, pointAt, touchMode }: {
+ pointer: RemoteTouchpad; positionRef: RefObject<HTMLDivElement | null>;
+ pointAt: (clientX: number, clientY: number) => PicturePoint | null;
+ touchMode: RoomPreferences["remoteTouchMode"];
+}) {
+ const { t } = useCopy();
+ const owner = useSurfaceDocument();
+ const id = useId();
+ const [mode, setMode] = useState<MouseMode>("mini");
+ const expanded = mode === "expanded";
+ const [held, setHeld] = useState<number[]>([]);
+ const contacts = useRef(new Map<number, MouseContact>());
+ const wheel = useRef<MouseWheel | null>(null);
+ const widget = useRef<HTMLDivElement>(null);
+ const mini = useRef<HTMLButtonElement>(null);
+ const focusMini = useRef(false);
+ const idle = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const suppressClick = useRef(false);
+ const heldButtons = () => [...contacts.current.values()].flatMap(contact => contact.button === undefined ? [] : [contact.button]);
+ const syncHeld = () => setHeld([...heldButtons(), ...(wheel.current?.phase === "middle" ? [1] : [])]);
+ const finishWheel = useCallback((cancelled: boolean) => {
+ const current = wheel.current;
+ if (!current) return;
+ wheel.current = null;
+ if (current.timer !== null) clearTimeout(current.timer);
+ if (current.phase === "middle") pointer.button(1, false);
+ else if (!cancelled && current.phase === "pending") {
+ pointer.button(1, true); pointer.button(1, false);
+ }
+ }, [pointer]);
+ const clearIdle = useCallback(() => {
+ if (idle.current !== null) clearTimeout(idle.current);
+ idle.current = null;
+ }, []);
+ const park = useCallback(() => {
+ clearIdle();
+ const layer = positionRef.current, element = widget.current;
+ if (!layer || !element || contacts.current.size || mini.current?.matches(":focus-visible")) return;
+ const side = element.offsetLeft <= 14 ? "left"
+ : element.offsetLeft + element.offsetWidth >= layer.clientWidth - 14 ? "right"
+ : element.offsetTop <= 14 ? "top"
+ : element.offsetTop + element.offsetHeight >= layer.clientHeight - 14 ? "bottom" : null;
+ if (side) idle.current = setTimeout(() => {
+ idle.current = null;
+ element.style.setProperty(side === "left" || side === "right" ? "--mouse-x" : "--mouse-y",
+ side === "left" || side === "top" ? "0%" : "100%");
+ setMode(side);
+ }, PLAYER_CONTROLS_IDLE_MS);
+ }, [clearIdle, positionRef]);
+ const moveWidget = (dx: number, dy: number) => {
+ const layer = positionRef.current, element = widget.current;
+ if (!layer || !element) return;
+ const x = Math.max(0, Math.min(1, (element.offsetLeft + element.offsetWidth / 2 + dx) / layer.clientWidth));
+ const y = Math.max(0, Math.min(1, (element.offsetTop + element.offsetHeight / 2 + dy) / layer.clientHeight));
+ element.style.setProperty("--mouse-x", `${x * 100}%`);
+ element.style.setProperty("--mouse-y", `${y * 100}%`);
+ };
+ const release = useCallback(() => {
+ clearIdle();
+ for (const contact of contacts.current.values()) {
+ if (contact.kind === "widget" && contact.moved) suppressClick.current = true;
+ }
+ for (const button of new Set(heldButtons())) pointer.button(button, false);
+ finishWheel(true);
+ widget.current?.removeAttribute("data-dragging");
+ contacts.current.clear(); wheel.current = null; setHeld([]);
+ }, [pointer, clearIdle, finishWheel]);
+ useLayoutEffect(() => () => pointer.setMode("direct"), [pointer]);
+ useLayoutEffect(() => {
+ if (!expanded) pointer.setMode(touchMode);
+ pointer.refresh();
+ }, [pointer, expanded, touchMode]);
+ useLayoutEffect(() => {
+ if (mode === "mini") {
+ if (focusMini.current) { mini.current?.focus({ preventScroll: true }); focusMini.current = false; }
+ park();
+ }
+ return clearIdle;
+ }, [mode, owner, park, clearIdle]);
+ useEffect(() => {
+ if (!owner) return;
+ const hidden = () => { if (owner.hidden) release(); };
+ owner.defaultView?.addEventListener("blur", release);
+ owner.addEventListener("visibilitychange", hidden);
+ return () => {
+ owner.defaultView?.removeEventListener("blur", release);
+ owner.removeEventListener("visibilitychange", hidden);
+ release();
+ };
+ }, [release, owner]);
+ const expand = () => {
+ const bounds = widget.current?.getBoundingClientRect();
+ release();
+ pointer.setMode("mouse", bounds ? pointAt(bounds.left, bounds.top) : null);
+ setMode("expanded");
+ };
+ const minimize = () => {
+ const layer = positionRef.current, element = widget.current;
+ if (layer && element) {
+ element.style.setProperty("--mouse-x", `${element.offsetLeft / layer.clientWidth * 100}%`);
+ element.style.setProperty("--mouse-y", `${element.offsetTop / layer.clientHeight * 100}%`);
+ }
+ release(); pointer.setMode(touchMode); focusMini.current = true; setMode("mini");
+ };
+ const finish = (id: number, cancelled = false) => {
+ const current = contacts.current.get(id);
+ if (current) {
+ contacts.current.delete(id);
+ if (current.button !== undefined && !heldButtons().includes(current.button)) pointer.button(current.button, false);
+ if (current.kind === "widget") {
+ widget.current?.removeAttribute("data-dragging");
+ suppressClick.current = current.moved || cancelled;
+ if (current.moved && !cancelled) park();
+ }
+ }
+ if (wheel.current?.id === id) finishWheel(cancelled);
+ syncHeld();
+ };
+ const capture = (event: PointerEvent<HTMLElement>) => {
+ if (event.button !== 0) return false;
+ event.preventDefault(); event.currentTarget.setPointerCapture(event.pointerId);
+ return true;
+ };
+ const button = (value: 0 | 2, className: string) => <button type="button" className={className}
+ aria-label={t(value === 0 ? "remote.mouseLeft" : "remote.mouseRight")}
+ aria-pressed={held.includes(value)}
+ onPointerDown={event => {
+ if (!capture(event)) return;
+ const alreadyHeld = heldButtons().includes(value);
+ contacts.current.set(event.pointerId, { x: event.clientX, y: event.clientY, moved: false, kind: "pointer", button: value });
+ if (!alreadyHeld) pointer.button(value, true);
+ syncHeld();
+ }}
+ onClick={event => {
+ if (event.detail === 0 && !held.includes(value)) {
+ pointer.button(value, true); pointer.button(value, false);
+ }
+ }}><span aria-hidden="true">{value === 0 ? "L" : "R"}</span></button>;
+ return <div ref={positionRef} className={`lr-remote-mouse${expanded ? " has-virtual-mouse" : ""}`} role="group" aria-label={t("remote.mouse")}
+ onPointerDown={event => event.stopPropagation()}
+ onPointerMove={event => {
+ event.stopPropagation();
+ const current = contacts.current.get(event.pointerId);
+ if (current) {
+ const dx = event.clientX - current.x, dy = event.clientY - current.y;
+ if (!current.moved && Math.hypot(dx, dy) <= 4) return;
+ current.moved = true; current.x = event.clientX; current.y = event.clientY;
+ if (current.kind === "widget") moveWidget(dx, dy);
+ else pointer.moveBy(dx, dy);
+ } else if (wheel.current?.id === event.pointerId) {
+ const current = wheel.current;
+ const dx = event.clientX - current.x, dy = event.clientY - current.y;
+ if (current.phase === "pending" && Math.hypot(dx, dy) <= 4) return;
+ if (current.phase === "pending") current.phase = "scroll";
+ current.x = event.clientX; current.y = event.clientY;
+ if (current.timer !== null) { clearTimeout(current.timer); current.timer = null; }
+ if (current.phase === "middle") pointer.moveBy(dx, dy);
+ else pointer.wheel(dy);
+ }
+ }}
+ onPointerUp={event => { event.stopPropagation(); finish(event.pointerId); }}
+ onPointerCancel={event => { event.stopPropagation(); release(); }}
+ onLostPointerCapture={event => { event.stopPropagation(); finish(event.pointerId, true); }}
+ onMouseDown={event => event.stopPropagation()} onMouseUp={event => event.stopPropagation()}
+ onMouseMove={event => event.stopPropagation()}
+ onClick={event => { event.preventDefault(); event.stopPropagation(); }}
+ onDoubleClick={event => { event.preventDefault(); event.stopPropagation(); }}
+ onAuxClick={event => { event.preventDefault(); event.stopPropagation(); }}
+ onContextMenu={event => { event.preventDefault(); event.stopPropagation(); }}
+ onKeyDown={event => {
+ event.stopPropagation();
+ if (event.key === "Escape" && expanded) {
+ event.preventDefault(); minimize();
+ }
+ }} onKeyUp={event => event.stopPropagation()}
+ onFocus={event => event.stopPropagation()}
+ onBlur={event => {
+ event.stopPropagation();
+ if (!event.currentTarget.contains(event.relatedTarget as Node | null)) release();
+ }}>
+ {(expanded || touchMode === "trackpad") && <svg className="lr-remote-touch-cursor" viewBox="0 0 24 28" aria-hidden="true">
+ <path d="M2 2v21l6-6 5 9 4-2-5-9h9Z" />
+ </svg>}
+ <div ref={widget} className={`lr-remote-mouse-widget${expanded ? " is-expanded" : ""}`}
+ data-docked={mode !== "mini" && !expanded ? mode : undefined}>
+ <button ref={mini} type="button" className="lr-remote-mouse-mini" aria-expanded={expanded}
+ aria-controls={id} aria-label={t("remote.mouseExpand")} aria-description={t("remote.mouseReposition")}
+ inert={expanded} aria-hidden={expanded}
+ onPointerDown={event => {
+ if (contacts.current.size || !capture(event)) return;
+ clearIdle(); suppressClick.current = false; setMode("mini");
+ contacts.current.set(event.pointerId, { x: event.clientX, y: event.clientY, moved: false, kind: "widget" });
+ widget.current?.setAttribute("data-dragging", "");
+ }}
+ onClick={event => {
+ if (suppressClick.current && event.detail > 0) { suppressClick.current = false; return; }
+ expand();
+ }}
+ onKeyDown={event => {
+ const dx = event.key === "ArrowLeft" ? -10 : event.key === "ArrowRight" ? 10 : 0;
+ const dy = event.key === "ArrowUp" ? -10 : event.key === "ArrowDown" ? 10 : 0;
+ if (dx || dy) { event.preventDefault(); clearIdle(); setMode("mini"); moveWidget(dx, dy); }
+ }} />
+ <div id={id} className="lr-remote-mouse-shell" inert={!expanded} aria-hidden={!expanded}>
+ {button(0, "lr-remote-mouse-left")}{button(2, "lr-remote-mouse-right")}
+ <button type="button" className="lr-remote-mouse-wheel" aria-label={t("remote.mouseWheel")}
+ aria-description={t("remote.mouseScroll")} aria-pressed={held.includes(1)}
+ onPointerDown={event => {
+ if (wheel.current || !capture(event)) return;
+ const current: MouseWheel = { id: event.pointerId, x: event.clientX, y: event.clientY, phase: "pending", timer: null };
+ wheel.current = current;
+ current.timer = setTimeout(() => {
+ current.timer = null; current.phase = "middle";
+ pointer.button(1, true); syncHeld();
+ }, REMOTE_TOUCH_HOLD_MS);
+ }}
+ onClick={event => {
+ if (event.detail === 0 && !held.includes(1)) { pointer.button(1, true); pointer.button(1, false); }
+ }}
+ onKeyDown={event => {
+ if (event.key !== "ArrowUp" && event.key !== "ArrowDown") return;
+ event.preventDefault(); pointer.wheel(event.key === "ArrowUp" ? -40 : 40);
+ }} />
+ <div className="lr-remote-mouse-body" role="group" tabIndex={0} aria-label={t("remote.mouseMove")}
+ onPointerDown={event => {
+ if (capture(event)) {
+ contacts.current.set(event.pointerId, { x: event.clientX, y: event.clientY, moved: false, kind: "pointer" });
+ }
+ }}
+ onKeyDown={event => {
+ const movement = event.key === "ArrowLeft" ? [-10, 0] : event.key === "ArrowRight" ? [10, 0]
+ : event.key === "ArrowUp" ? [0, -10] : event.key === "ArrowDown" ? [0, 10] : null;
+ if (movement) { event.preventDefault(); pointer.moveBy(movement[0], movement[1]); }
+ }}>
+ <Glyph name="grip" size={18} />
+ </div>
+ </div>
+ </div>
+ <button type="button" className="lr-remote-mouse-minimize" aria-label={t("remote.mouseCollapse")}
+ onClick={minimize} inert={!expanded} aria-hidden={!expanded}>
+ <Glyph name="chevron" size={18} />
+ </button>
+ </div>;
+}
diff --git a/src/client/components/living/SiteAddressEntry.tsx b/src/client/components/living/SiteAddressEntry.tsx
new file mode 100644
index 00000000..b4fa6645
--- /dev/null
+++ b/src/client/components/living/SiteAddressEntry.tsx
@@ -0,0 +1,78 @@
+import { useId, useRef, useState } from "react";
+import { useCopy } from "../../ui/copy";
+import { Glyph } from "../../ui/icons";
+import { Btn } from "./primitives";
+import { useAnchoredPopover } from "./use-anchored-popover";
+
+// Permission belongs to an exact origin, never to an invitation path or fragment.
+export function siteEntryOrigin(value: string): string | null {
+ try {
+ const address = value.trim();
+ const url = new URL(address.includes("://") ? address : `https://${address}`);
+ return ["http:", "https:"].includes(url.protocol) && !url.username && !url.password &&
+ !/\s/.test(decodeURIComponent(url.hostname)) ? url.origin : null;
+ } catch { return null; }
+}
+
+export function SiteAddressEntry({ initialAddress = "", onSelect }: {
+ initialAddress?: string; onSelect: (origin: string) => void;
+}) {
+ const { vis, t } = useCopy();
+ const id = useId();
+ const anchor = useRef<HTMLSpanElement>(null);
+ const panel = useRef<HTMLDivElement>(null);
+ const input = useRef<HTMLInputElement>(null);
+ const [open, setOpen] = useState(false);
+ const [invalid, setInvalid] = useState(false);
+ useAnchoredPopover(anchor, panel);
+ function selectSite() {
+ const origin = siteEntryOrigin(input.current?.value ?? "");
+ if (!origin) {
+ setInvalid(true);
+ input.current?.focus();
+ return;
+ }
+ panel.current?.hidePopover();
+ onSelect(origin);
+ }
+ return <>
+ <span ref={anchor} className="lr-client-site-add">
+ <Btn icon="plus" title="client.connect.addSite" size="dense" quiet hint="hint-client-site"
+ hintText="client.connect.addSiteHint" expanded={open} controls={id} popoverTarget={id} />
+ </span>
+ <div ref={panel} id={id} popover="auto" role="dialog" aria-label={t("client.connect.addSite")}
+ className="lr-anchored-card lr-client-site-entry" onToggle={event => {
+ if (event.target !== event.currentTarget) return;
+ const opened = event.newState === "open";
+ setOpen(opened);
+ if (opened) {
+ input.current?.focus();
+ input.current?.select();
+ }
+ }}>
+ <strong className={vis ? "visually-hidden" : undefined}>{t("client.connect.addSite")}</strong>
+ <div className="lr-client-site-address">
+ <label className="lr-input">
+ <Glyph name="globe" size={18} />
+ <input ref={input} defaultValue={initialAddress} type="text" inputMode="url" enterKeyHint="go" autoComplete="url"
+ autoCapitalize="none" spellCheck={false} placeholder="piik.example"
+ aria-label={t("client.connect.siteAddress")} aria-invalid={invalid}
+ aria-describedby={invalid ? `${id}-error` : undefined} onInput={() => setInvalid(false)}
+ onBlur={event => {
+ const origin = siteEntryOrigin(event.currentTarget.value);
+ if (origin) event.currentTarget.value = origin;
+ }}
+ onKeyDown={event => {
+ if (event.key === "Enter" && !event.nativeEvent.isComposing) {
+ event.preventDefault();
+ selectSite();
+ }
+ }} />
+ </label>
+ <Btn icon="arrowRight" title="client.connect.continue" size="compact" tone="primary" onClick={selectSite} />
+ </div>
+ {invalid && <p id={`${id}-error`} role="alert">{t("client.connect.invalid")}</p>}
+ <p className={vis ? "visually-hidden" : undefined}>{t("client.connect.addSiteHint")}</p>
+ </div>
+ </>;
+}
diff --git a/src/client/components/living/StatusIndicator.tsx b/src/client/components/living/StatusIndicator.tsx
index 451bbaff..77ec4528 100644
--- a/src/client/components/living/StatusIndicator.tsx
+++ b/src/client/components/living/StatusIndicator.tsx
@@ -27,6 +27,6 @@ export function StatusIndicator({ status, label, onActivate }: {
<Glyph name={status.icon} size={17} />
</button>
);
- return <Tooltip toggleOnClick={!onActivate} kind={hint} tone={status.tone} motion={status.pulse ? "progress" : "still"}
+ return <Tooltip toggleOnClick={!onActivate} autoOpen={Boolean(onActivate)} kind={hint} tone={status.tone} motion={status.pulse ? "progress" : "still"}
text={vis ? undefined : description}>{indicator}</Tooltip>;
}
diff --git a/src/client/components/living/Tooltip.tsx b/src/client/components/living/Tooltip.tsx
index 95a5139e..c7da2453 100644
--- a/src/client/components/living/Tooltip.tsx
+++ b/src/client/components/living/Tooltip.tsx
@@ -60,6 +60,7 @@ export function Tooltip({
place = "above",
align = "center",
toggleOnClick = false,
+ autoOpen = false,
disabled = false,
overflow,
children,
@@ -73,6 +74,8 @@ export function Tooltip({
align?: Align;
/** For help-only buttons; action controls keep click and touch long-press. */
toggleOnClick?: boolean;
+ /** Reveal new guidance once, retaining ordinary dismissal and focus. */
+ autoOpen?: boolean;
/** Suppress guidance while the control's own disclosure is open. */
disabled?: boolean;
children: ReactNode;
@@ -282,6 +285,12 @@ export function Tooltip({
// the panel stays inside the viewport mid-open — no page-level clamps.
const pickAlignRef = useRef(pickAlign);
pickAlignRef.current = pickAlign;
+ useEffect(() => {
+ if (!autoOpen || !enabled) return;
+ mountPanel(true);
+ pickAlignRef.current();
+ setPressOpen(true);
+ }, [autoOpen, enabled, kind, text, ownerDocument]);
useLayoutEffect(() => {
const tip = tipRef.current;
if (panelMounted) {
diff --git a/src/client/components/living/ViewerPlaybackSettings.tsx b/src/client/components/living/ViewerPlaybackSettings.tsx
deleted file mode 100644
index e64ee256..00000000
--- a/src/client/components/living/ViewerPlaybackSettings.tsx
+++ /dev/null
@@ -1,79 +0,0 @@
-import type { ReactNode } from "react";
-import type { ViewerRoutePreference } from "../../../shared/protocol";
-import type { VideoFit, VideoRotation } from "../../media/video-geometry";
-import { useCopy } from "../../ui/copy";
-import { Glyph } from "../../ui/icons";
-import { Cap, Chip, Pill, RowGroup, SwitchItem } from "./primitives";
-
-export interface ViewerPlaybackSettingsProps {
- pictureFit: VideoFit;
- setPictureFit: (fit: VideoFit) => void;
- pictureRotation: VideoRotation;
- setPictureRotation: (rotation: VideoRotation) => void;
- route?: {
- preference: ViewerRoutePreference;
- sfuAvailable: boolean;
- sfuOnly: boolean;
- peerOnly: boolean;
- disabled: boolean;
- rejected: boolean;
- onChange: (preference: ViewerRoutePreference) => void;
- };
- relayVolunteer?: {
- enabled: boolean;
- pending: boolean;
- disabled: boolean;
- onChange: (enabled: boolean) => void;
- };
-}
-
-// Local display and connection intent; media and routing keep their owners.
-export function ViewerPlaybackSettings({ route, relayVolunteer, overlaySettings, reconnect }: Pick<ViewerPlaybackSettingsProps, "route" | "relayVolunteer"> & {
- overlaySettings?: ReactNode; reconnect: ReactNode;
-}) {
- const { t, vis } = useCopy();
- const routeHint = route?.rejected ? "playback.routeRejected" : route?.sfuOnly ? "playback.routeSfuOnly"
- : route?.peerOnly ? "playback.routePeerOnly" : !route?.sfuAvailable ? "playback.routeNoSfu"
- : `playback.routeHint.${route.preference}` as const;
- return <div className="lr-viewer-settings">
- {overlaySettings && <section aria-label={t("playback.overlay")}>
- <div className="lr-viewer-settings-heading">
- <h3><Glyph name="danmaku" size={17} /><span className={vis ? "visually-hidden" : undefined}>{t("playback.overlay")}</span></h3>
- </div>
- {overlaySettings}
- </section>}
- {(route || relayVolunteer || reconnect) && <section aria-label={t("playback.connection")}>
- <div className="lr-viewer-settings-heading">
- <h3><Glyph name="branch" size={17} /><span className={vis ? "visually-hidden" : undefined}>{t("playback.connection")}</span></h3>
- {reconnect}
- </div>
- {route && <>
- <RowGroup>{(["auto", "p2p", "sfu"] as const).map(preference => {
- const locked = preference === "p2p" && route.sfuOnly ? "playback.routeSfuOnly"
- : preference === "sfu" && !route.sfuAvailable ? route.peerOnly ? "playback.routePeerOnly" : "playback.routeNoSfu" : undefined;
- return <Chip key={preference}
- selected={route.preference === preference} title={t(`playback.route.${preference}`)}
- disabled={route.disabled || Boolean(locked)}
- hint={locked ? preference === "p2p" ? "hint-route-sfu-required" : "hint-route-p2p-required"
- : preference === "auto" ? "hint-route-auto" : preference === "p2p" ? "hint-route-p2p" : "hint-route-sfu"}
- hintText={t(locked ?? `playback.routeHint.${preference}`)}
- onClick={() => { if (route.preference !== preference) route.onChange(preference); }}>
- {vis && <Glyph name={preference === "auto" ? "branch" : preference === "p2p" ? "network" : "server"} size={17} />}
- <Cap k={`playback.route.${preference}`} />
- </Chip>;
- })}</RowGroup>
- {!vis && <p className={route.rejected ? "is-error" : undefined} role={route.rejected ? "status" : undefined}>{t(routeHint)}</p>}
- {vis && route.rejected && <Pill icon="alert" tone="bad" label={t(routeHint)} comic="settings-failed" />}
- </>}
- {relayVolunteer && <div className="lr-viewer-relay-setting" aria-busy={relayVolunteer.pending}>
- <RowGroup>
- {vis && <Glyph name="branch" size={18} />}
- <SwitchItem checked={relayVolunteer.enabled} disabled={relayVolunteer.disabled}
- onChange={relayVolunteer.onChange} label={t("playback.relayVolunteer")}
- note={t("playback.relayVolunteerHint")} hint="hint-relay-volunteer" />
- </RowGroup>
- {!vis && <p>{t("playback.relayVolunteerHint")}</p>}
- </div>}
- </section>}
- </div>;
-}
diff --git a/src/client/components/living/floating-panel.css b/src/client/components/living/floating-panel.css
index 807feb4c..1b0eb4e9 100644
--- a/src/client/components/living/floating-panel.css
+++ b/src/client/components/living/floating-panel.css
@@ -1,8 +1,9 @@
.lr-floating-surface { color: var(--ink); background: var(--surface-glaze) var(--paper); border: 1px solid var(--control-edge); border-radius: 24px; box-shadow: var(--panel-shadow); }
-.lr-floating-panel { position: fixed; inset: auto 12px 12px auto; max-width: calc(100vw - 24px); max-height: calc(100dvh - 24px); margin: 0; padding: 16px; overflow: auto; overscroll-behavior: contain; transform-origin: right bottom; container: floating-panel / inline-size; }
+.lr-floating-panel { position: fixed; inset: auto 12px 12px auto; max-width: calc(100vw - 24px); max-height: calc(100dvh - 24px); margin: 0; padding: 16px; overflow: hidden; overscroll-behavior: contain; transform-origin: right bottom; container: floating-panel / inline-size; }
.lr-floating-panel:is(:popover-open, .\:popover-open) { display: flex; flex-direction: column; animation: lr-floating-enter 180ms var(--spring); }
.lr-floating-panel:focus { outline: none; }
.lr-floating-panel:focus-visible { outline: 2px solid var(--action); outline-offset: -2px; }
+.lr-floating-body { display: flex; flex: 1; flex-direction: column; }
/* Keep control outlines inside the scrollport without shifting content alignment. */
.lr-floating-scroll { min-height: 0; overflow: auto; overscroll-behavior: contain; scrollbar-width: thin; margin-inline: -6px; padding-inline: 6px; }
.lr-floating-header { display: flex; align-items: center; gap: 8px; min-height: 56px; flex-shrink: 0; margin: -16px -16px 0; padding: 6px 10px 6px 24px; border-bottom: 1px solid var(--wall-2); border-radius: 24px 24px 0 0; cursor: grab; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: none; }
diff --git a/src/client/components/living/participant-color.ts b/src/client/components/living/participant-color.ts
index 5ad57bb3..20971576 100644
--- a/src/client/components/living/participant-color.ts
+++ b/src/client/components/living/participant-color.ts
@@ -11,5 +11,37 @@ export function participantColor(identity: string): string {
const hue = (140 + (seed & 0xffff) / 0xffff * 250) % 360;
const lightness = 0.59 + ((seed >>> 16) & 255) / 255 * 0.18;
const chroma = 0.11 + (seed >>> 24) / 255 * 0.08;
- return `oklch(${lightness.toFixed(4)} ${chroma.toFixed(4)} ${hue.toFixed(4)})`;
+ return oklchToRgb(Number(lightness.toFixed(4)), Number(chroma.toFixed(4)), Number(hue.toFixed(4)));
+}
+
+// Standard conversion, with simple constant-lightness/hue chroma reduction.
+// https://www.w3.org/TR/css-color-4/#color-conversion-code
+// https://www.w3.org/TR/css-color-4/#chroma-reduction
+function oklchToRgb(lightness: number, chroma: number, hue: number): string {
+ const angle = hue * Math.PI / 180;
+ const cos = Math.cos(angle), sin = Math.sin(angle);
+ const linearRgb = (c: number) => {
+ const a = c * cos, b = c * sin;
+ const l = (lightness + .3963377773761749 * a + .2158037573099136 * b) ** 3;
+ const m = (lightness - .1055613458156586 * a - .0638541728258133 * b) ** 3;
+ const s = (lightness - .0894841775298119 * a - 1.2914855480194092 * b) ** 3;
+ return [
+ 4.076741636075958 * l - 3.3077115392580616 * m + .2309699031821043 * s,
+ -1.2684379732850317 * l + 2.6097573492876887 * m - .34131937600265727 * s,
+ -.004196076138675564 * l - .7034186179359363 * m + 1.7076146940746117 * s,
+ ];
+ };
+ const inGamut = (channels: number[]) => channels.every(channel => channel >= 0 && channel <= 1);
+ let rgb = linearRgb(chroma);
+ if (!inGamut(rgb)) {
+ let low = 0, high = chroma;
+ for (let step = 0; step < 12; step++) {
+ const middle = (low + high) / 2;
+ const candidate = linearRgb(middle);
+ if (inGamut(candidate)) { low = middle; rgb = candidate; }
+ else high = middle;
+ }
+ }
+ return `rgb(${rgb.map(channel => Math.round(255 * (channel <= .0031308
+ ? 12.92 * channel : 1.055 * channel ** (1 / 2.4) - .055))).join(" ")})`;
}
diff --git a/src/client/components/living/playback-controls.css b/src/client/components/living/playback-controls.css
index 1baa9e52..ea3c632d 100644
--- a/src/client/components/living/playback-controls.css
+++ b/src/client/components/living/playback-controls.css
@@ -9,6 +9,7 @@
transition: opacity 180ms ease, transform 220ms var(--spring);
}
.lr-playback.lr-media-controls { flex-wrap: nowrap; }
+.lr-playback.lr-media-toolbar { background: var(--surface-glaze) var(--paper-translucent); }
.lr-playback-main { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.lr-playback.is-hidden { opacity: 0; transform: translateY(6px); pointer-events: none; }
.lr-playback:has([popover]:is(:popover-open, .\:popover-open)) { opacity: 1; transform: none; pointer-events: auto; }
@@ -54,14 +55,17 @@
.lr-playback-level.is-boosted { color: var(--action-text); }
.lr-playback :is(.lr-remote-controls, .lr-playback-view), .lr-playback-window { display: inline-flex; align-items: center; flex: none; gap: 4px; }
.lr-playback :is(.lr-remote-controls, .lr-playback-view)::before, .lr-playback-window::before { content: ""; flex: none; width: 1px; height: 20px; margin-right: 4px; background: var(--control-edge); }
-.lr-playback-fit { display: inline-flex; align-items: center; }
+.lr-playback-picture-actions { display: inline-flex; align-items: center; gap: 4px; }
.lr-playback-more { display: inline-flex; }
+.lr-playback-more.is-compact-only, .lr-playback-compact-picture { display: none; }
+.lr-playback-picture-options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
+.lr-playback-picture-options .lr-btn { width: 100%; padding: 0 8px; }
.lr-playback-menu { position: fixed; inset: auto; margin: 0; padding: 8px; max-width: calc(100vw - 16px); overflow: auto; overscroll-behavior: contain; }
.lr-playback-menu:is(:popover-open, .\:popover-open) { display: grid; gap: 6px; animation: lr-language-open 180ms var(--spring); }
.lr-playback-settings { width: min(560px, calc(100vw - 16px)); padding: 16px; container: playback-settings / inline-size; }
-.lr-playback-settings:has(.lr-viewer-settings > section:only-of-type) { width: min(360px, calc(100vw - 16px)); }
+.lr-playback-settings:has(> .lr-viewer-settings.is-single) { width: min(360px, calc(100vw - 16px)); }
.lr-viewer-settings { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px; width: 100%; }
-.lr-viewer-settings:has(> section:only-of-type) { grid-template-columns: minmax(0, 1fr); }
+.lr-viewer-settings.is-single { grid-template-columns: minmax(0, 1fr); }
.lr-viewer-settings section { min-width: 0; }
.lr-viewer-settings-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: var(--control-dense-size); margin-bottom: 12px; }
.lr-viewer-settings h3 { display: flex; align-items: center; gap: 8px; margin: 0; font-size: 12px; font-weight: 650; }
@@ -81,30 +85,33 @@
.lr-playback-level { display: none; }
}
@container media-stage (max-width: 640px) {
- /* Keep one control surface and one hide/show owner. Sound/input precede
- viewing/window actions when a single row would make touch targets tiny. */
- .lr-tv-screen > .lr-playback.lr-media-controls { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; width: min(440px, calc(100% - 16px)); padding: 4px 8px; border-radius: 20px; }
- .lr-playback-main { display: contents; }
- .lr-playback-watch { grid-area: 1 / 1 / 2 / 3; }
- .lr-playback:has(.lr-remote-controls) .lr-playback-watch { grid-column: 1; }
- .lr-playback .lr-remote-controls { grid-area: 1 / 2; }
- .lr-playback-view { grid-area: 2 / 1; }
- .lr-playback-window { grid-area: 2 / 2; }
- .lr-playback .lr-btn { width: clamp(28px, 9cqw, var(--control-compact-size)); }
+ /* Keep playback, sound and window actions on one row. Picture adjustments
+ use the same controls in the attached settings while space is limited. */
+ .lr-tv-screen > .lr-playback.lr-media-controls { gap: 4px; width: min(560px, calc(100% - 16px)); padding: 4px; border-radius: 16px; }
+ .lr-playback-main { gap: 4px; }
+ .lr-playback .lr-btn { width: clamp(24px, 8.5cqw, var(--control-compact-size)); }
.lr-playback-menu .lr-btn { width: auto; }
+ .lr-playback > .lr-playback-main > .lr-playback-view > .lr-playback-picture-actions { display: none; }
+ .lr-playback-compact-picture { display: block; }
+ .lr-playback-more.is-compact-only { display: inline-flex; }
.lr-playback-watch { gap: 4px; }
- .lr-playback-volume { flex: 1; }
+ .lr-playback-volume { flex: 1 1 80px; }
.lr-playback .lr-playback-view::before, .lr-playback-window::before { display: none; }
}
@container media-stage (max-width: 440px) {
.lr-tv-screen > .lr-tv-overlay.has-comic { gap: 4px; padding: 6px; }
.lr-tv-overlay > .lr-waiting-caption { min-height: 1.4em; }
.lr-tv-overlay > .lr-waiting-caption > span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
- .lr-tv-screen > .lr-playback.lr-media-controls { gap: 0 4px; padding-inline: 4px; }
+ .lr-tv-screen > .lr-playback.lr-media-controls { gap: 2px; }
+ .lr-playback-main { gap: 2px; }
.lr-playback-watch { gap: 2px; }
.lr-playback :is(.lr-remote-controls, .lr-playback-view, .lr-playback-window) { gap: 2px; }
.lr-playback :is(.lr-remote-controls, .lr-playback-view)::before { display: none; }
}
+@container media-stage (max-width: 320px) {
+ .lr-tv-screen > .lr-playback.lr-media-controls { width: calc(100% - 8px); max-width: calc(100% - 8px); gap: 0; padding: 2px; }
+ .lr-playback-main, .lr-playback-watch, .lr-playback :is(.lr-remote-controls, .lr-playback-view, .lr-playback-window) { gap: 0; }
+}
@media (prefers-reduced-motion: reduce) {
.lr-playback, .lr-playback .lr-btn, .lr-playback-reveal, .lr-playback-reveal > svg { transition: none; }
.lr-playback-menu:is(:popover-open, .\:popover-open), .lr-playback-settings { animation: none; }
diff --git a/src/client/components/living/remote-control.css b/src/client/components/living/remote-control.css
index b2bd8f20..4e5557f7 100644
--- a/src/client/components/living/remote-control.css
+++ b/src/client/components/living/remote-control.css
@@ -46,8 +46,6 @@
.lr-remote-request-actions .lr-btn { width: 100%; }
.lr-remote-note kbd { white-space: nowrap; font: 12px var(--mono); }
.lr-remote-shortcut { margin-block: 12px; text-align: start; }
-.lr-remote-touch-cursor { position: absolute; width: 22px; height: 26px; left: 50%; top: 50%; pointer-events: none; filter: drop-shadow(0 1px 2px #0008); }
-.lr-remote-touch-cursor path { fill: #fff; stroke: #22313e; stroke-width: 1.5; stroke-linejoin: round; }
.lr-remote-game-mouse { display: flex; justify-content: center; margin-top: 8px; }
:is(.lr-remote-keyboard, .lr-remote-password) { grid-area: 3 / 1; z-index: 4; padding: 10px 12px 12px; background: var(--paper); color: var(--ink); border-top: 1px solid var(--control-edge); }
:is(.lr-remote-keyboard, .lr-remote-password) .lr-input { padding: 0 10px; }
@@ -122,3 +120,46 @@
display: grid; place-items: center; min-width: 24px; height: 24px;
border: 1px solid var(--line); border-radius: 5px; font: 600 14px var(--mono);
}
+.lr-remote-mouse { --mouse-offset-x: 24px; --mouse-offset-y: 28px; --mouse-shell-width: 96px; --mouse-shell-height: 100px; position: absolute; inset: 0; z-index: 1; pointer-events: none; }
+.lr-remote-touch-cursor { position: absolute; left: var(--remote-x, 50%); top: var(--remote-y, 50%); z-index: 2; width: 22px; height: 28px; pointer-events: none; filter: drop-shadow(0 1px 2px var(--surface-shade)); }
+.lr-remote-touch-cursor path { fill: var(--piik-dark-ink); stroke: var(--piik-dark-wall); stroke-width: 1.5; stroke-linejoin: round; }
+.lr-remote-mouse-widget { --mouse-width: 44px; --mouse-height: 44px; position: absolute; left: clamp(6px, calc(var(--mouse-x, 78%) - var(--mouse-width) / 2), max(6px, calc(100% - var(--mouse-width) - 6px))); top: clamp(6px, calc(var(--mouse-y, 65%) - var(--mouse-height) / 2), max(6px, calc(100% - var(--mouse-height) - 6px))); width: min(var(--mouse-width), calc(100% - 12px)); height: min(var(--mouse-height), calc(100% - 12px)); transition: transform 180ms ease-out; pointer-events: none; }
+.lr-remote-mouse-widget.is-expanded { --mouse-width: calc(var(--mouse-shell-width) + 44px); --mouse-height: var(--mouse-shell-height); left: calc(var(--remote-x, 50%) + var(--mouse-offset-x)); top: calc(var(--remote-y, 50%) + var(--mouse-offset-y)); }
+.lr-remote-mouse-widget:not(.is-expanded)[data-docked="left"] { transform: translateX(calc(-50% - 6px)); }
+.lr-remote-mouse-widget:not(.is-expanded)[data-docked="right"] { transform: translateX(calc(50% + 6px)); }
+.lr-remote-mouse-widget:not(.is-expanded)[data-docked="top"] { transform: translateY(calc(-50% - 6px)); }
+.lr-remote-mouse-widget:not(.is-expanded)[data-docked="bottom"] { transform: translateY(calc(50% + 6px)); }
+.lr-remote-mouse-mini { position: absolute; inset: 0; display: block; width: 100%; height: 100%; padding: 0; border: 0; border-radius: 10px; background: transparent; cursor: grab; pointer-events: auto; touch-action: none; transition: opacity 140ms ease; }
+.lr-remote-mouse-mini::before { content: ""; position: absolute; inset: 4px 8px; border: 1px solid var(--control-edge); border-radius: 10px 10px 9px 9px / 9px 9px 13px 13px; background: linear-gradient(var(--line), var(--line)) 50% 0 / 1px 13px no-repeat, var(--surface-glaze) var(--paper); box-shadow: inset 0 1px 0 var(--surface-rim), 0 1px 3px var(--surface-shade); }
+.lr-remote-mouse-mini::after { content: ""; position: absolute; top: 10px; left: calc(50% - 2px); width: 4px; height: 9px; border: 1px solid var(--control-edge); border-radius: 3px; background: var(--wall-2); }
+.lr-remote-mouse-mini:hover::before { border-color: var(--action); }
+.lr-remote-mouse-mini:active { cursor: grabbing; }
+.lr-remote-mouse-mini:active::before { border-color: var(--action); box-shadow: inset 0 0 0 1px var(--action); }
+.lr-remote-mouse-mini:focus-visible, .lr-remote-mouse-minimize:focus-visible { outline: 2px solid var(--action); outline-offset: 2px; }
+.lr-remote-mouse-widget.is-expanded .lr-remote-mouse-mini { opacity: 0; visibility: hidden; pointer-events: none; }
+.lr-remote-mouse-shell { position: absolute; left: 0; top: 0; width: var(--mouse-shell-width); height: var(--mouse-shell-height); display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: repeat(2, minmax(0, 1fr)); border: 1px solid var(--control-edge); border-radius: 34px 34px 30px 30px / 30px 30px 42px 42px; background: var(--surface-glaze) var(--paper); box-shadow: inset 0 1px 0 var(--surface-rim), 0 2px 4px var(--surface-shade); overflow: hidden; opacity: 0; visibility: hidden; transform: translate(8px, 4px) scale(.27, .25); transform-origin: top left; transition: transform 160ms ease-out, opacity 140ms ease, visibility 0s 160ms; pointer-events: none; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
+.lr-remote-mouse-widget.is-expanded .lr-remote-mouse-shell { opacity: 1; visibility: visible; transform: none; transition-delay: 0s; pointer-events: auto; }
+.lr-remote-mouse-minimize { position: absolute; left: clamp(6px, calc(var(--remote-x, 50%) + var(--mouse-offset-x) + var(--mouse-shell-width)), max(6px, calc(100% - 44px - 6px))); top: clamp(6px, calc(var(--remote-y, 50%) + var(--mouse-offset-y) + var(--mouse-shell-height) - 44px), max(6px, calc(100% - 44px - 6px))); z-index: 3; display: grid; place-items: center; width: 44px; height: 44px; padding: 0; border: 0; border-radius: 50%; background: transparent; color: var(--action-text); opacity: 0; visibility: hidden; transform: scale(.9); transition: transform 160ms ease-out, opacity 140ms ease; pointer-events: none; touch-action: manipulation; }
+.lr-remote-mouse-minimize::before { content: ""; position: absolute; inset: 6px; border: 1px solid var(--control-edge); border-radius: 50%; background: var(--surface-glaze) var(--paper); box-shadow: 0 1px 3px var(--surface-shade); }
+.lr-remote-mouse-minimize > svg { position: relative; width: 16px; height: 16px; }
+.lr-remote-mouse-minimize:hover::before { background: var(--control-hover); }
+.lr-remote-mouse.has-virtual-mouse > .lr-remote-mouse-minimize { opacity: 1; visibility: visible; transform: none; pointer-events: auto; }
+.lr-remote-mouse-widget[data-dragging], .lr-remote-mouse-widget[data-dragging] > * { transition: none; }
+.lr-remote-mouse-shell button { display: grid; place-items: center; min-width: 0; padding: 0; border: 0; background: transparent; color: var(--ink); font: 600 11px var(--font); touch-action: none; }
+.lr-remote-mouse-shell button:is(:hover, :focus-visible) { background: var(--control-hover); }
+.lr-remote-mouse-shell button:focus-visible, .lr-remote-mouse-wheel:focus-visible, .lr-remote-mouse-body:focus-visible { outline: 2px solid var(--action); outline-offset: -3px; }
+.lr-remote-mouse-shell button[aria-pressed="true"] { background: var(--control-selected); color: var(--control-selected-ink); box-shadow: inset 0 0 0 1px var(--action); }
+.lr-remote-mouse-shell .lr-remote-mouse-left { grid-area: 1 / 1; border-right: 1px solid var(--line); }
+.lr-remote-mouse-right { grid-area: 1 / 2; }
+.lr-remote-mouse-body { grid-area: 2 / 1 / 3 / 3; position: relative; display: grid; place-items: center; min-height: 0; border-top: 1px solid var(--line); color: var(--muted); cursor: grab; touch-action: none; }
+.lr-remote-mouse-body:hover { background: var(--control-hover); }
+.lr-remote-mouse-body:active { cursor: grabbing; }
+.lr-remote-mouse-shell .lr-remote-mouse-wheel { position: absolute; left: calc(50% - 14px); top: 3px; z-index: 1; width: 28px; height: 44px; border-radius: 8px; cursor: ns-resize; touch-action: none; }
+.lr-remote-mouse-wheel::before { content: ""; position: absolute; left: calc(50% - 6px); top: 7px; box-sizing: border-box; width: 12px; height: 30px; border: 1px solid var(--control-edge); border-radius: 5px; background: repeating-linear-gradient(to bottom, transparent 0 3px, var(--line) 3px 4px) var(--wall-2); box-shadow: inset 0 1px 2px var(--surface-shade); }
+.lr-remote-mouse-wheel[aria-pressed="true"]::before { border-color: var(--action); }
+@media (prefers-reduced-motion: reduce) {
+ .lr-remote-mouse-widget, .lr-remote-mouse-widget > *, .lr-remote-mouse > .lr-remote-mouse-minimize { transition: none; }
+}
+:is([data-reduced-motion], [data-comic-reduced-motion]) .lr-remote-mouse-widget,
+:is([data-reduced-motion], [data-comic-reduced-motion]) .lr-remote-mouse-widget > *,
+:is([data-reduced-motion], [data-comic-reduced-motion]) .lr-remote-mouse > .lr-remote-mouse-minimize { transition: none; }
diff --git a/src/client/components/living/room-interactions.css b/src/client/components/living/room-interactions.css
index 60c6a13f..02ca59ba 100644
--- a/src/client/components/living/room-interactions.css
+++ b/src/client/components/living/room-interactions.css
@@ -2,12 +2,14 @@
.lr-interaction-navigation { display: flex; flex: none; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.lr-interaction-tabs { flex: 1; min-width: 0; }
.lr-interaction-navigation > .lr-comic-tip-wrap { flex: none; }
-.lr-interaction-page { display: flex; flex: 1; flex-direction: column; min-height: 0; }
+.lr-interaction-page { display: flex; flex: 1; flex-direction: column; min-height: min-content; }
.lr-interaction-page[hidden] { display: none; }
.lr-voice-history { position: relative; flex: 1; }
.lr-interaction-voice-controls { display: flex; flex: none; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; padding-top: 12px; margin-top: 12px; border-top: 1px solid var(--control-edge); }
.lr-interaction-couch { position: relative; }
-.lr-interaction-conversation { position: relative; display: flex; flex: 1; flex-direction: column; min-height: 0; }
+/* Keep one usable history row and the composer's intrinsic size. The panel body
+ scrolls when the on-screen keyboard leaves less room. */
+.lr-interaction-conversation { position: relative; display: flex; flex: 1; flex-direction: column; min-height: calc(var(--control-compact-size) + 32px); contain: size; }
.lr-interaction-conversation[hidden] { display: none; }
.lr-interaction-conversation .lr-reaction-flight { overflow: hidden; }
.lr-interaction-back { flex: none; padding: 8px 0 0; }
@@ -23,7 +25,7 @@
.lr-interaction-chat-toggle { display: flex; position: relative; }
.lr-interaction-unread { align-self: flex-start; margin: -4px 0 0 -8px; z-index: 1; min-width: 22px; padding: 2px 6px; border-radius: 20px; color: var(--action-ink); background: var(--action-deep); font: 700 11px var(--mono); text-align: center; }
.lr-chat-compose { flex-shrink: 0; }
-.lr-chat-history { position: relative; flex: 1; min-height: 0; }
+.lr-chat-history { position: relative; flex: 1; min-height: 0; overflow: hidden; }
.lr-chat-log { height: 100%; overflow: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-gutter: stable; padding: 16px 2px; }
.lr-chat-latest { position: absolute; z-index: 6; bottom: 8px; left: 50%; translate: -50% 0; display: flex; align-items: center; gap: 6px; min-height: 36px; max-width: calc(100% - 16px); padding: 6px 12px; border: 1px solid var(--control-edge); border-radius: 18px; background: var(--paper); color: var(--ink); box-shadow: var(--control-shadow); font-size: 12px; white-space: nowrap; }
.lr-chat-latest:is(:hover, :focus-visible) { background: var(--control-hover); }
diff --git a/src/client/components/living/use-host-preview-playback.ts b/src/client/components/living/use-host-preview-playback.ts
new file mode 100644
index 00000000..bf1f61d1
--- /dev/null
+++ b/src/client/components/living/use-host-preview-playback.ts
@@ -0,0 +1,57 @@
+import { useEffect, useRef, useState, type RefObject } from "react";
+import { isPictureInPicture } from "./use-picture-in-picture";
+
+export function shouldPauseLocalPreview(
+ visibilityState: DocumentVisibilityState,
+ hasFocus: boolean,
+ pictureInPicture = false,
+ autoPause = true,
+): boolean {
+ return autoPause && !pictureInPicture && (visibilityState !== "visible" || !hasFocus);
+}
+
+// Local playback only; capture and sender quality retain their own lifetimes.
+export function useHostPreviewPlayback(
+ videoRef: RefObject<HTMLVideoElement | null>,
+ stream: MediaStream | null,
+ autoPause: boolean,
+) {
+ const [suspended, setSuspended] = useState(false);
+ const pausedByUser = useRef(false);
+
+ useEffect(() => {
+ const video = videoRef.current;
+ if (!video || !stream) {
+ pausedByUser.current = false;
+ setSuspended(false);
+ return;
+ }
+ const syncPlayback = () => {
+ const shouldPause = shouldPauseLocalPreview(document.visibilityState, document.hasFocus(), isPictureInPicture(video), autoPause);
+ setSuspended(shouldPause);
+ if (shouldPause) video.pause();
+ else if (!pausedByUser.current) void video.play().catch(() => undefined);
+ };
+ document.addEventListener("visibilitychange", syncPlayback);
+ window.addEventListener("blur", syncPlayback);
+ window.addEventListener("focus", syncPlayback);
+ const pictureEvents = ["enterpictureinpicture", "leavepictureinpicture", "webkitpresentationmodechanged"];
+ pictureEvents.forEach(event => video.addEventListener(event, syncPlayback));
+ syncPlayback();
+ return () => {
+ document.removeEventListener("visibilitychange", syncPlayback);
+ window.removeEventListener("blur", syncPlayback);
+ window.removeEventListener("focus", syncPlayback);
+ pictureEvents.forEach(event => video.removeEventListener(event, syncPlayback));
+ };
+ }, [videoRef, stream, autoPause]);
+
+ return {
+ suspended,
+ onPlay: () => {
+ pausedByUser.current = false;
+ void videoRef.current?.play().catch(() => undefined);
+ },
+ onPause: () => { pausedByUser.current = true; },
+ };
+}
diff --git a/src/client/components/living/use-playback-controls-visibility.ts b/src/client/components/living/use-playback-controls-visibility.ts
index 5ff889c6..2a101ad8 100644
--- a/src/client/components/living/use-playback-controls-visibility.ts
+++ b/src/client/components/living/use-playback-controls-visibility.ts
@@ -1,7 +1,5 @@
import { useEffect, useRef, useState, type RefObject } from "react";
-
-// Matches Media Chrome's default: hide playing-video controls after 2s idle.
-const CONTROLS_IDLE_MS = 2000;
+import { PLAYER_CONTROLS_IDLE_MS } from "../../ui/control-timing";
export function usePlaybackControlsVisibility(
videoRef: RefObject<HTMLVideoElement | null>,
@@ -40,7 +38,7 @@ export function usePlaybackControlsVisibility(
timer = window.setTimeout(() => {
timer = undefined;
if (!held()) setHidden(true);
- }, CONTROLS_IDLE_MS);
+ }, PLAYER_CONTROLS_IDLE_MS);
};
const show = () => {
setHidden(false);
diff --git a/src/client/lib/release-update.ts b/src/client/lib/release-update.ts
index 39be72c5..f0cd569f 100644
--- a/src/client/lib/release-update.ts
+++ b/src/client/lib/release-update.ts
@@ -33,8 +33,9 @@ function normalizeVersion(value: unknown): string | null {
function packageDownloadURL(release: PublishedRelease, assets: unknown, target?: string): string {
if (!target || !/^[a-z0-9]+-[a-z0-9]+$/.test(target) || !Array.isArray(assets)) return release.url;
- const names = [`piik-app-${target}.zip`];
- if (release.revision) names.push(`piik-app-${target}-${release.revision.slice(0, 7)}.zip`);
+ const extension = target === "android-universal" ? "apk" : "zip";
+ const names = [`piik-app-${target}.${extension}`];
+ if (release.revision && extension === "zip") names.push(`piik-app-${target}-${release.revision.slice(0, 7)}.zip`);
for (const name of names) {
const matches = assets.filter(asset => asset?.name === name);
if (!matches.length) continue;
diff --git a/src/client/lib/room-preferences.ts b/src/client/lib/room-preferences.ts
index cb8b923d..fff23d6c 100644
--- a/src/client/lib/room-preferences.ts
+++ b/src/client/lib/room-preferences.ts
@@ -10,6 +10,7 @@ export const REMOTE_KEYBOARD_PAGES = ["letters", "symbols", "functions", "text"]
const roomPreferencesSchema = z.object({
pictureFit: z.enum(["contain", "cover", "fill"]).default("contain"),
pictureRotation: z.union([z.literal(0), z.literal(90), z.literal(180), z.literal(270)]).catch(0).default(0),
+ previewAutoPause: z.boolean().default(true),
routePreference: viewerRoutePreferenceSchema.default("auto"),
overlayEnabled: z.boolean().default(false),
overlayAppearance: z.object({
@@ -25,6 +26,7 @@ const roomPreferencesSchema = z.object({
combineKeys: z.boolean().default(false),
remoteReleaseKey: remoteShortcutKeySchema.default(10),
remoteStopKey: remoteShortcutKeySchema.default(10),
+ remoteTouchMode: z.enum(["direct", "trackpad"]).default("direct"),
remoteControlMode: remoteControlModeSchema.exclude(["off"]).default("ask"),
});
export type RoomPreferences = z.infer<typeof roomPreferencesSchema>;
diff --git a/src/client/lib/session.ts b/src/client/lib/session.ts
index 9eebed42..4182261f 100644
--- a/src/client/lib/session.ts
+++ b/src/client/lib/session.ts
@@ -9,11 +9,22 @@ import { z } from "zod";
import { createOpaqueId } from "./opaque-id";
import { isLang, type Lang } from "../locales";
import { withBrowserDebug } from "./debug";
+import { NATIVE_CLIENT_PORT_START, NATIVE_CLIENT_PORT_END } from "../native/discovery-ports";
const CLIENT_ID_PATTERN = /^[A-Za-z0-9_-]{8,128}$/;
const CLIENT_LAUNCH_STORAGE_KEY = "piik:client-launch:v1";
+const CLIENT_RUNTIME_STORAGE_KEY = "piik:client-runtime-port";
const CLIENT_ACTIVATION_EVENT = "piik-client-activation";
-let clientActivatedInPage = false;
+// Keep the selected App through this tab's reloads, without copying it into invites.
+let clientPortInPage = parseClientRuntimePort(readSessionValue(CLIENT_RUNTIME_STORAGE_KEY));
+let clientActivatedInPage = clientPortInPage !== null;
+
+export function clientRuntimePort(): number | null { return clientPortInPage; }
+
+function parseClientRuntimePort(value: string | null): number | null {
+ const port = Number(value);
+ return Number.isInteger(port) && port >= NATIVE_CLIENT_PORT_START && port <= NATIVE_CLIENT_PORT_END ? port : null;
+}
export function isClientActivated(): boolean {
try { return clientActivatedInPage || window.localStorage.getItem(CLIENT_LAUNCH_STORAGE_KEY) === "1"; }
@@ -135,9 +146,10 @@ export interface ClientLaunchPresentation {
explicit?: readonly ("copy" | "theme")[];
}
-export function clientLaunchURL(target: string, presentation: ClientLaunchPresentation, debug?: boolean): string {
+export function clientLaunchURL(target: string, presentation: ClientLaunchPresentation, debug?: boolean, controlPort?: number): string {
const url = new URL(target);
const params = new URLSearchParams(url.hash.slice(1));
+ if (controlPort !== undefined) url.searchParams.set("piik-client-port", String(controlPort));
params.set("piik-lang", presentation.lang);
params.set("piik-mode", presentation.vis ? "vis" : "text");
params.set("piik-theme", presentation.theme ?? "system");
@@ -161,10 +173,15 @@ export function takeClientLaunchBootstrap(): ClientLaunchBootstrap {
"piik-theme",
] as const;
const query = new URLSearchParams(window.location.search);
- const present = keys.some((key) => params.has(key)) || query.has("piik-preferences");
+ const present = keys.some((key) => params.has(key)) || query.has("piik-preferences") || query.has("piik-client-port");
const accessValue = params.get("client-access");
const launchedFromFragment = params.get("piik-client") === "1";
- if (launchedFromFragment) clientActivatedInPage = true;
+ if (launchedFromFragment) {
+ clientActivatedInPage = true;
+ clientPortInPage = parseClientRuntimePort(query.get("piik-client-port"));
+ if (clientPortInPage !== null) writeSessionValue(CLIENT_RUNTIME_STORAGE_KEY, String(clientPortInPage));
+ else try { window.sessionStorage.removeItem(CLIENT_RUNTIME_STORAGE_KEY); } catch { /* Storage is optional. */ }
+ }
const lang = params.get("piik-lang");
const mode = params.get("piik-mode");
const theme = params.get("piik-theme");
@@ -199,6 +216,7 @@ export function takeClientLaunchBootstrap(): ClientLaunchBootstrap {
if (present) {
for (const key of keys) params.delete(key);
query.delete("piik-preferences");
+ query.delete("piik-client-port");
const search = query.toString();
const remaining = params.toString();
window.history.replaceState(
diff --git a/src/client/locales/en.ts b/src/client/locales/en.ts
index 9c7ec3ae..fb8e8017 100644
--- a/src/client/locales/en.ts
+++ b/src/client/locales/en.ts
@@ -12,7 +12,22 @@ export const en: Record<CopyKey, string> = {
"remote.gamepadSetup": "Gamepad setup",
"remote.reason.gamepad": "The virtual gamepad is unavailable. Check the Host's gamepad setup and free controller slots.",
"remote.keyboard": "Remote keyboard",
+ "remote.inputTools": "Input tools",
"remote.keyboardRequiresControl": "Take control to use the remote keyboard",
+ "remote.mouse": "Mouse",
+ "remote.mouseLeft": "Left mouse button",
+ "remote.mouseRight": "Right mouse button",
+ "remote.mouseWheel": "Scroll wheel and middle button",
+ "remote.mouseExpand": "Expand virtual mouse",
+ "remote.mouseCollapse": "Minimize mouse",
+ "remote.mouseReposition": "Drag to reposition; leave at an edge to tuck away",
+ "remote.mouseMove": "Drag to move the pointer",
+ "remote.mouseScroll": "Tap to middle-click, swipe to scroll, or hold to drag",
+ "remote.touchMode": "Default touch mode",
+ "remote.touchMode.direct": "Direct touch",
+ "remote.touchMode.trackpad": "Trackpad",
+ "remote.touchMode.directHint": "Tap a position directly on the picture.",
+ "remote.touchMode.trackpadHint": "Swipe to move the pointer; tap to click.",
"remote.keyPages": "Keyboard sections",
"remote.keys.letters": "Letter keys",
"remote.keys.symbols": "Numbers and symbols",
@@ -41,7 +56,7 @@ export const en: Record<CopyKey, string> = {
"remote.typeText": "Type remotely",
"remote.textHelp": "Enter adds a new line. The button types at the remote cursor.",
"remote.textTooLong": "Split this into smaller sections (up to 2 KB each).",
- "remote.touchHelp": "Swipe to move, tap to click, and hold to drag. With two fingers, tap to right-click or swipe to scroll. Pinch to zoom; swipe with two fingers to pan when zoomed.",
+ "remote.touchHelp": "Two-finger tap right-clicks; pinch to zoom. Two-finger swipes scroll normally, or pan when zoomed or the virtual mouse is expanded.",
"remote.resetZoom": "Reset picture zoom",
"remote.key.Escape": "Escape",
"remote.key.Tab": "Tab",
@@ -88,8 +103,8 @@ export const en: Record<CopyKey, string> = {
"remote.decline": "Decline",
"remote.surface": "Remote keyboard and mouse input",
"remote.controlling": "Controlling",
- "remote.hostFocus": "Focus the shared window to resume control",
- "remote.suspended": "Waiting for the Host to focus the shared window",
+ "remote.hostFocus": "Keep the shared window or screen available for control",
+ "remote.suspended": "Waiting for the shared window or screen to become available",
"remote.gameMouse": "Game mouse",
"remote.gameMouseHelp": "Capture the mouse for continuous game camera movement. Press Esc to release.",
"remote.releaseMouse": "Release mouse",
@@ -97,6 +112,7 @@ export const en: Record<CopyKey, string> = {
"remote.lockFailed": "Could not capture the mouse. Click again.",
"remote.pending": "Waiting for the Host to approve",
"remote.failed": "Remote control is unavailable. Request again.",
+ "remote.inputFailed": "The last input could not be applied. Try again.",
"remote.decisionFailed": "Couldn't apply this choice. Try again.",
"remote.macPermission": "Couldn't enable control. Allow Piik in System Settings → Privacy & Security → Accessibility, then try again.",
"remote.linuxStop": "End control here, or stop sharing from the system indicator.",
@@ -120,10 +136,13 @@ export const en: Record<CopyKey, string> = {
"playback.changeFit": "Switch picture fit",
"playback.fitCycle": "Current: {current}. Switch to {next}. Only changes your view.",
"playback.rotation": "Rotate picture",
- "playback.rotationHint": "Switch the page's picture between landscape and portrait without device auto-rotation.",
+ "playback.rotationHint": "Rotate the picture locally without changing the shared source.",
"playback.connection": "Connection",
"playback.relayVolunteer": "Help relay to more viewers",
"playback.relayVolunteerHint": "Uses more upload bandwidth for this visit.",
+ "playback.relayLimit": "Maximum relay viewers",
+ "playback.relayPeople": "viewers",
+ "playback.relayUnavailable": "This connection cannot relay to additional viewers.",
"playback.fit.contain": "Fit",
"playback.fit.cover": "Fill",
"playback.fit.fill": "Stretch",
@@ -212,13 +231,23 @@ export const en: Record<CopyKey, string> = {
"client.connect.action": "Connect Piik App",
"client.connect.hint": "Connect App for the full Piik experience.",
"client.connect.discovering": "Looking for Piik App…",
+ "client.connect.wakeApp": "Open Piik App",
+ "client.connect.returnToBrowser": "Return here after opening Piik App to continue connecting.",
+ "native.confirmInApp": "Confirm in App",
+ "host.sourcePicker.tab.picker": "System chooser",
"client.connect.permissionHint": "If your browser asks, allow access to this device or local network to find App.",
"client.connect.found": "Piik App found. Open its page to continue.",
"client.connect.open": "Open App page",
"client.connect.approving": "Waiting for App approval…",
"client.connect.standby": "App is ready",
"client.connect.guide": "Connect a site",
- "client.connect.standbyHint": "Click App at the top right of a Piik site, then choose Allow access.",
+ "client.connect.addSite": "Open a site",
+ "client.connect.addSiteHint": "Enter a Piik site address, then confirm its App access.",
+ "client.connect.siteAddress": "Site address",
+ "client.connect.continue": "Continue",
+ "client.connect.openSite": "Open site",
+ "client.connect.approveAndOpen": "Allow and open",
+ "client.connect.standbyHint": "Click App at the top right of a Piik site, choose Open App page, then confirm Allow access.",
"client.connect.remove": "Revoke this Site's App access",
"client.connect.openOnStart": "Open this site when App starts",
"client.connect.manage": "App is ready. Manage App.",
@@ -233,7 +262,7 @@ export const en: Record<CopyKey, string> = {
"client.connect.invalid": "Invalid Site address",
"client.connect.unavailable": "Could not connect to App. Keep Piik App running and allow popups and local-network access in your browser.",
"client.connect.download": "Download App",
- "client.connect.browserHint": "You can share and watch in your browser.",
+ "client.connect.browserHint": "You can still watch and use sharing sources supported by your browser.",
"client.launch.lanAddress": "Local invitation address",
"client.launch.chooseAddress": "Choose an address",
"client.launch.lanHint": "Choose an address on the same network as your friends.",
@@ -246,11 +275,11 @@ export const en: Record<CopyKey, string> = {
"client.launch.go": "Open Piik",
"client.launch.starting": "Preparing Piik",
"client.launch.error": "Room unavailable",
- "client.launch.reopen": "Reopen Piik to try again. Enable Debug launch if you need to record logs.",
+ "client.launch.retryHint": "Try again or adjust the settings below.",
"client.launch.errorDetails": "Technical details",
"client.launch.failure.tunnel-exited": "The public invitation service could not connect. Check your network or use App with an existing Piik Site.",
"client.launch.failure.tunnel-timeout": "The public invitation connection timed out. Check your network or use App with an existing Piik Site.",
- "client.launch.failure.tunnel-unavailable": "The public invitation component is unavailable. Extract the complete Piik package again.",
+ "client.launch.failure.tunnel-unavailable": "The public invitation component is unavailable. Check that the complete Piik App is installed.",
"client.launch.failure.tunnel-start": "The public invitation component could not start. Check the technical details.",
"client.launch.failure.tunnel-stopped": "The public invitation disconnected. Create a new invitation and send the new link.",
"client.launch.failure.local-port": "The local service port is unavailable. Check whether Piik is already running.",
@@ -299,13 +328,13 @@ export const en: Record<CopyKey, string> = {
"host.sourcePicker.browser": "Choose what to share",
"host.sourcePicker.browserHint": "Choose a screen, window or tab in your browser's sharing dialog.",
"host.sourcePicker.camera": "Share camera: {title}",
- "host.camera.denied": "Camera access was not allowed. Allow it in your browser and try again.",
+ "host.camera.denied": "Camera access was not allowed. Allow access, then try again.",
"host.camera.unavailable": "The camera is unavailable. Check that it is connected and not in use by another app.",
"host.microphone.settings": "Microphone settings",
"host.commentary.include": "Include my microphone",
"host.commentary.unavailable": "Microphone mixing is unavailable. Picture and source sound sharing continue.",
"host.commentary.description": "Add your voice to the shared picture, without joining room voice.",
- "host.commentary.duplicate": "Your microphones are sending to both shared audio and room voice.",
+ "host.commentary.duplicate": "Sharing and voice microphones are enabled. If one is silent or you hear echoes, use one path.",
"host.commentary.failed": "The shared microphone could not start. Check its permission and selected device.",
"host.audioInput.stopped": "An audio input stopped. Picture sharing continues.",
"host.device.microphone": "Microphone",
@@ -480,17 +509,17 @@ export const en: Record<CopyKey, string> = {
"native.fail.browserMedia": "The browser could not create a media connection; reload to retry.",
"native.fail.signaling": "Piik App signaling failed",
"native.fail.receiver": "Piik App media receive failed",
- "native.fail.controlDisconnected": "Disconnected from Piik App; check that the App is running, then reload.",
+ "native.fail.controlDisconnected": "Piik App disconnected. Reconnect using the App button.",
"native.fail.requestTimeout": "Piik App timed out; try again in a moment.",
"native.fail.invalidResponse": "The page could not read Piik App's response. Update the App and refresh the page.",
"native.fail.captureStart": "App capture failed; try another source.",
"native.fail.edgePrepare": "Piik App could not prepare the media connection. Enable diagnostics and try again.",
"shareHelp.title": "Sharing troubleshooting",
- "shareHelp.open": "Select for troubleshooting",
+ "shareHelp.open": "Select the status light for troubleshooting",
"shareHelp.source": "Try another ordinary window or screen. Keep the source open and allow screen recording when your system asks.",
"shareHelp.compare": "Compare with the Browser source picker. On Windows, also try Auto or VP8 with the same source.",
"shareHelp.report": "If it still fails, enable diagnostics, try again and save reports from that attempt. Include the failure message, Piik version and what you were doing.",
- "shareHelp.appDebug": "For App logs, enable Debug launch on the mode selection page, reproduce the problem, then press D in the terminal to export. The button below records browser diagnostics only.",
+ "shareHelp.appDebug": "The button below records browser diagnostics. For App logs, see the troubleshooting guide.",
"shareHelp.site": "Check the site address and your access. For a self-hosted site, check PUBLIC_BASE_URL, ALLOWED_ORIGINS and the reverse proxy configuration; see the guide for steps.",
"shareHelp.appConnection": "Keep the App running and click App at the top of this page to connect. Allow local-network access if your browser asks.",
"shareHelp.mediaConnection": "Check that the browser permits WebRTC and local network access, then refresh and try again. You can also compare with another browser.",
@@ -521,6 +550,9 @@ export const en: Record<CopyKey, string> = {
"host.stop": "Stop sharing",
"host.noAudio": "Source sound is not being shared",
"host.localPreviewPaused": "Local preview paused; sharing continues",
+ "host.preview": "Local preview",
+ "host.previewAutoPause": "Auto-pause preview",
+ "host.previewAutoPauseHint": "Pause the local preview when switching tabs or windows; sharing continues. Turn off to watch on another monitor.",
"host.pauseNotice": "Audio and video sharing paused",
"host.stopNotice": "Sharing stopped",
"host.roomReplaced": "Room code replaced",
@@ -626,6 +658,9 @@ export const en: Record<CopyKey, string> = {
"host.video.returnCommonHint": "Use the nearest common value: {value} {unit}.",
"host.video.inputHint": "Drag for common values, or enter {min}–{max} {unit} directly.",
"host.video.resolutionHint": "Width ≤ {width} px; height ≤ {height} px; total ≤ {pixels} pixels. Source proportions are preserved; the FPS limit follows the size.",
+ "host.video.resolutionPreset": "Up to {width} × {height} pixels",
+ "host.video.resolution1440p": "Up to {width} × {height} pixels (QHD, often called 2K)",
+ "host.video.resolution2160p": "Up to {width} × {height} pixels (4K)",
"host.video.resolutionInvalid": "Use even dimensions from 4 to {width} × {height}, within {pixels} total pixels.",
"host.quality.invalidNumber": "Use {min}–{max} {unit}, in steps of {step}.",
"host.audio.bitrateHint": "Audio bitrate limit: {min}–{max} kbps.",
diff --git a/src/client/locales/zh.ts b/src/client/locales/zh.ts
index e57487ef..bc739f95 100644
--- a/src/client/locales/zh.ts
+++ b/src/client/locales/zh.ts
@@ -12,7 +12,22 @@ export const zh = {
"remote.gamepadSetup": "手柄设置",
"remote.reason.gamepad": "虚拟手柄不可用,请检查房主的手柄设置及空闲手柄位。",
"remote.keyboard": "远程键盘",
+ "remote.inputTools": "输入工具",
"remote.keyboardRequiresControl": "取得控制权后可使用远程键盘",
+ "remote.mouse": "鼠标",
+ "remote.mouseLeft": "鼠标左键",
+ "remote.mouseRight": "鼠标右键",
+ "remote.mouseWheel": "滚轮与中键",
+ "remote.mouseExpand": "展开虚拟鼠标",
+ "remote.mouseCollapse": "缩为迷你鼠标",
+ "remote.mouseReposition": "拖动调整位置,贴边停留可自动收纳",
+ "remote.mouseMove": "拖动移动光标",
+ "remote.mouseScroll": "轻点中击,滑动滚动,长按后拖动",
+ "remote.touchMode": "默认触摸方式",
+ "remote.touchMode.direct": "直接触摸",
+ "remote.touchMode.trackpad": "光标触控板",
+ "remote.touchMode.directHint": "直接点击画面上的位置。",
+ "remote.touchMode.trackpadHint": "滑动移动光标,轻点点击。",
"remote.keyPages": "键盘分区",
"remote.keys.letters": "字母键",
"remote.keys.symbols": "数字与符号",
@@ -41,7 +56,7 @@ export const zh = {
"remote.typeText": "输入到对方",
"remote.textHelp": "回车可换行;点击按钮后输入到对方当前选中的位置。",
"remote.textTooLong": "内容较长,请分段输入(每次最多 2 KB)。",
- "remote.touchHelp": "滑动可移动鼠标,轻点点击,长按拖动。双指轻点右击、滑动滚动;捏合放大后,双指移动画面。",
+ "remote.touchHelp": "双指轻点右击,捏合缩放。双指滑动默认滚动;缩放后或展开虚拟鼠标时改为平移画面。",
"remote.resetZoom": "还原画面大小",
"remote.key.Escape": "退出键",
"remote.key.Tab": "制表键",
@@ -88,8 +103,8 @@ export const zh = {
"remote.decline": "拒绝",
"remote.surface": "远程键鼠控制区域",
"remote.controlling": "正在控制",
- "remote.hostFocus": "切回共享窗口即可继续控制",
- "remote.suspended": "等待房主切回共享窗口",
+ "remote.hostFocus": "请保持共享窗口或屏幕可操作",
+ "remote.suspended": "等待共享窗口或屏幕恢复",
"remote.gameMouse": "游戏鼠标",
"remote.gameMouseHelp": "锁定鼠标,连续转动游戏视角;按 Esc 释放。",
"remote.releaseMouse": "释放鼠标",
@@ -97,6 +112,7 @@ export const zh = {
"remote.lockFailed": "未能锁定鼠标,请再次点击",
"remote.pending": "等待房主允许",
"remote.failed": "远程控制不可用,请重新请求",
+ "remote.inputFailed": "刚才的操作未执行,请重试。",
"remote.decisionFailed": "操作未完成,请重试",
"remote.macPermission": "未能开启控制。请在系统设置 → 隐私与安全性 → 辅助功能中允许 Piik,然后重试。",
"remote.linuxStop": "可在本面板结束控制,也可通过系统指示器停止共享。",
@@ -120,10 +136,13 @@ export const zh = {
"playback.changeFit": "切换画面适配",
"playback.fitCycle": "当前:{current}。点击切换为{next},只影响自己的画面。",
"playback.rotation": "横竖切换",
- "playback.rotationHint": "网页画面横竖切换,无需开启手机自动旋转。",
+ "playback.rotationHint": "旋转当前画面的显示方向,不影响分享来源。",
"playback.connection": "连接",
"playback.relayVolunteer": "帮助更多观众转发",
"playback.relayVolunteerHint": "仅本次观看生效,会使用更多上行带宽。",
+ "playback.relayLimit": "最多转发人数",
+ "playback.relayPeople": "人",
+ "playback.relayUnavailable": "当前连接无法增加转发人数。",
"playback.fit.contain": "完整显示",
"playback.fit.cover": "等比铺满",
"playback.fit.fill": "拉伸铺满",
@@ -212,13 +231,23 @@ export const zh = {
"client.connect.action": "连接 Piik App",
"client.connect.hint": "连接 App,获得更完整的体验。",
"client.connect.discovering": "正在查找 Piik App…",
+ "client.connect.wakeApp": "打开 Piik App",
+ "client.connect.returnToBrowser": "打开 Piik App 后返回此页,继续连接。",
+ "native.confirmInApp": "前往 App 确认",
+ "host.sourcePicker.tab.picker": "系统选择器",
"client.connect.permissionHint": "如果浏览器询问,请允许访问此设备或本地网络,以查找 App。",
"client.connect.found": "已找到 Piik App,请打开 App 页面继续。",
"client.connect.open": "打开 App 页面",
"client.connect.approving": "正在等待 App 授权…",
"client.connect.standby": "App 已就绪",
"client.connect.guide": "如何连接站点",
- "client.connect.standbyHint": "在 Piik 站点右上角点击 App,再选择「允许使用」。",
+ "client.connect.addSite": "打开站点",
+ "client.connect.addSiteHint": "输入 Piik 站点地址,再确认其 App 使用权限。",
+ "client.connect.siteAddress": "站点地址",
+ "client.connect.continue": "继续",
+ "client.connect.openSite": "打开站点",
+ "client.connect.approveAndOpen": "允许并打开",
+ "client.connect.standbyHint": "在 Piik 站点右上角点击 App,选择「打开 App 页面」,再确认「允许使用」。",
"client.connect.remove": "取消此站点的 App 授权",
"client.connect.openOnStart": "启动 App 时打开此站点",
"client.connect.manage": "App 已就绪,点击管理。",
@@ -233,7 +262,7 @@ export const zh = {
"client.connect.invalid": "站点网址无效",
"client.connect.unavailable": "未能连接 App。请保持 Piik App 运行,并允许浏览器打开新窗口和访问本地网络。",
"client.connect.download": "下载 App",
- "client.connect.browserHint": "可继续使用浏览器分享和观看。",
+ "client.connect.browserHint": "仍可观看,并使用浏览器支持的分享方式。",
"client.launch.lanAddress": "局域网邀请地址",
"client.launch.chooseAddress": "选择地址",
"client.launch.lanHint": "选择与朋友处于同一网络的地址。",
@@ -246,11 +275,11 @@ export const zh = {
"client.launch.go": "进入 Piik",
"client.launch.starting": "正在准备 Piik",
"client.launch.error": "房间暂不可用",
- "client.launch.reopen": "重新打开 Piik 后重试;需要记录日志时,可开启「诊断启动」。",
+ "client.launch.retryHint": "可直接重试,也可调整下方设置。",
"client.launch.errorDetails": "技术详情",
"client.launch.failure.tunnel-exited": "公网邀请服务未能建立连接。可检查网络,或通过已有 Piik 站点使用 App。",
"client.launch.failure.tunnel-timeout": "连接公网邀请服务超时。可检查网络,或通过已有 Piik 站点使用 App。",
- "client.launch.failure.tunnel-unavailable": "公网邀请组件不可用,请重新完整解压 Piik 程序包。",
+ "client.launch.failure.tunnel-unavailable": "公网邀请组件不可用,请检查 Piik App 是否完整安装。",
"client.launch.failure.tunnel-start": "无法启动公网邀请组件,请查看技术详情。",
"client.launch.failure.tunnel-stopped": "公网邀请已断开。重新创建后,请发送新的邀请链接。",
"client.launch.failure.local-port": "本地服务端口不可用,请检查是否已有 Piik 在运行。",
@@ -299,13 +328,13 @@ export const zh = {
"host.sourcePicker.browser": "选择画面",
"host.sourcePicker.browserHint": "在浏览器弹窗中选择屏幕、窗口或标签页。",
"host.sourcePicker.camera": "分享摄像头:{title}",
- "host.camera.denied": "未获得摄像头权限,请在浏览器中允许访问后重试。",
+ "host.camera.denied": "未获得摄像头权限,请允许访问后重试。",
"host.camera.unavailable": "暂时无法使用摄像头,请检查连接,以及是否正被其他应用占用。",
"host.microphone.settings": "麦克风设置",
"host.commentary.include": "分享麦克风",
"host.commentary.unavailable": "麦克风混音不可用,画面和源声音仍可继续分享。",
"host.commentary.description": "将你的声音加入分享画面,无需加入房间语音。",
- "host.commentary.duplicate": "分享和语音都已开麦,听众会收到两路声音。",
+ "host.commentary.duplicate": "分享和语音都已开麦。若有回声或一路无声,请关闭一路。",
"host.commentary.failed": "分享麦克风未能开启,请检查权限和所选设备。",
"host.audioInput.stopped": "一项声音输入已停止,画面分享继续。",
"host.device.microphone": "麦克风",
@@ -480,17 +509,17 @@ export const zh = {
"native.fail.browserMedia": "浏览器无法建立媒体连接,请刷新页面重试。",
"native.fail.signaling": "Piik App 信令失败",
"native.fail.receiver": "Piik App 媒体接收失败",
- "native.fail.controlDisconnected": "与 Piik App 的连接已断开,请确认 App 运行后刷新页面。",
+ "native.fail.controlDisconnected": "与 Piik App 的连接已断开,请通过 App 按钮重新连接。",
"native.fail.requestTimeout": "Piik App 响应超时,请稍后重试。",
"native.fail.invalidResponse": "Piik App 返回的数据无法识别。请更新 App 并刷新页面。",
"native.fail.captureStart": "App 未能采集所选来源,请换一个来源重试。",
"native.fail.edgePrepare": "Piik App 未能准备媒体连接。请开启诊断后重试。",
"shareHelp.title": "分享排查",
- "shareHelp.open": "点按查看排查方法",
+ "shareHelp.open": "点击状态灯查看排查方法",
"shareHelp.source": "先换一个普通窗口或屏幕重试,确认来源仍然打开,并允许系统请求的录屏权限。",
"shareHelp.compare": "对照试一次「浏览器」选源。Windows 上也可将编码器改为「自动」或 VP8,再试同一来源。",
"shareHelp.report": "仍然失败时,开启诊断后再试一次,并保存同一次尝试的报告。请附上失败提示、Piik 版本和当时的操作。",
- "shareHelp.appDebug": "App 日志需从模式选择页开启「诊断启动」,重现后在终端按 D 导出。下方按钮只记录网页诊断。",
+ "shareHelp.appDebug": "下方按钮记录网页诊断,App 日志的获取方法见问题排查文档。",
"shareHelp.site": "确认当前站点地址和访问权限。自部署站点请核对 PUBLIC_BASE_URL、ALLOWED_ORIGINS 与反向代理配置,具体步骤见排查文档。",
"shareHelp.appConnection": "确认 App 仍在运行,点击页面顶部的 App 完成连接;浏览器询问本地网络权限时,请允许访问。",
"shareHelp.mediaConnection": "检查浏览器是否允许 WebRTC 和本地网络访问,再刷新页面重试;也可换一个浏览器对照。",
@@ -521,6 +550,9 @@ export const zh = {
"host.stop": "停止分享",
"host.noAudio": "未分享来源声音",
"host.localPreviewPaused": "本地预览已暂停,分享仍在继续",
+ "host.preview": "本地预览",
+ "host.previewAutoPause": "自动暂停预览",
+ "host.previewAutoPauseHint": "切换标签页或窗口时暂停本地预览,分享仍继续;副屏观看时可关闭。",
"host.pauseNotice": "音视频分享已暂停",
"host.stopNotice": "已停止分享",
"host.roomReplaced": "房间号已更换",
@@ -626,6 +658,9 @@ export const zh = {
"host.video.returnCommonHint": "使用最近的常用档位:{value} {unit}。",
"host.video.inputHint": "拖动选择常用值,也可直接输入 {min}–{max} {unit}。",
"host.video.resolutionHint": "宽 ≤ {width},高 ≤ {height} 像素;总像素 ≤ {pixels}。保留来源比例,帧率上限随尺寸调整。",
+ "host.video.resolutionPreset": "最高 {width} × {height} 像素",
+ "host.video.resolution1440p": "最高 {width} × {height} 像素(QHD,常称 2K)",
+ "host.video.resolution2160p": "最高 {width} × {height} 像素(4K)",
"host.video.resolutionInvalid": "宽高需为不小于 4 的偶数,上限为 {width} × {height},总像素不超过 {pixels}。",
"host.quality.invalidNumber": "请输入 {min}–{max} {unit},步长为 {step}。",
"host.audio.bitrateHint": "音频码率上限:{min}–{max} kbps。",
diff --git a/src/client/media/host-sfu-route.ts b/src/client/media/host-sfu-route.ts
index bf6d6165..e7944c37 100644
--- a/src/client/media/host-sfu-route.ts
+++ b/src/client/media/host-sfu-route.ts
@@ -334,7 +334,16 @@ export class HostSfuRoute {
: null;
}
- async replaceStream(stream: MediaStream): Promise<boolean> {
+ async replaceStream(stream: MediaStream, rebuildTransport = false): Promise<boolean> {
+ if (rebuildTransport) {
+ const slots = [...new Set([this.active, this.pending])].filter(
+ (slot): slot is HostPublisherSlot => slot !== null && !slot.failed,
+ );
+ for (const slot of slots) this.handleFailure(slot, false);
+ const closing = Promise.all(slots.map(slot => this.disconnectPublisher(slot))).then(() => undefined);
+ await this.queueTransition(() => closing);
+ return slots.length === 0;
+ }
const slots = this.publishingSlots();
const initialActive = this.active;
if (slots.length === 0) {
@@ -609,7 +618,7 @@ export class HostSfuRoute {
await this.disconnectPublisher(active);
}
- private handleFailure(slot: HostPublisherSlot): void {
+ private handleFailure(slot: HostPublisherSlot, refreshConnection = true): void {
if (this.closed || slot.failed) {
return;
}
@@ -656,7 +665,10 @@ export class HostSfuRoute {
if (!matchesPlannedRoute) {
return;
}
- this.requestRecovery(revision, slot.connectionId);
+ if (refreshConnection) this.requestRecovery(revision, slot.connectionId);
+ // A different source backend needs its own encoder/transport. Refresh only
+ // describes the old physical connection; authority must replace that slot.
+ else this.routeFailed(revision, "active", slot.connectionId);
}
private failPublisherSlot(slot: HostPublisherSlot): void {
diff --git a/src/client/media/quality.ts b/src/client/media/quality.ts
index 74542531..78daab42 100644
--- a/src/client/media/quality.ts
+++ b/src/client/media/quality.ts
@@ -112,7 +112,7 @@ export const QUALITY_RESOLUTIONS = {
"720p": { width: 1280, height: 720 },
"1080p": { width: 1920, height: 1080 },
"1440p": { width: 2560, height: 1440 },
- "4K": { width: 3840, height: 2160 },
+ "2160p": { width: 3840, height: 2160 },
} as const satisfies Record<string, QualityResolution>;
export function qualityResolutionEqual(left: QualityResolution, right: QualityResolution): boolean {
diff --git a/src/client/media/relay-capability.ts b/src/client/media/relay-capability.ts
index aaa772b9..49f4984e 100644
--- a/src/client/media/relay-capability.ts
+++ b/src/client/media/relay-capability.ts
@@ -1,5 +1,15 @@
import { MAX_ENDPOINT_MEDIA_COPY_CAPACITY } from "../../shared/media-copy-accounting";
-import type { ClientMessage } from "../../shared/protocol";
+import { MAX_VIEWERS_PER_ROOM_LIMIT, type ClientMessage } from "../../shared/protocol";
+
+export function relayVolunteerLimit(requested: number | null, ordinary: number, backend: number) {
+ const maximum = Math.min(backend, MAX_VIEWERS_PER_ROOM_LIMIT);
+ return {
+ capacity: Math.min(maximum, Math.max(ordinary + 1, requested ?? ordinary + 1)),
+ minimum: Math.min(ordinary + 1, maximum),
+ maximum,
+ available: maximum > ordinary,
+ };
+}
type RelayCapacityMessage = Extract<
ClientMessage,
diff --git a/src/client/native/activation.ts b/src/client/native/activation.ts
new file mode 100644
index 00000000..fee2a903
--- /dev/null
+++ b/src/client/native/activation.ts
@@ -0,0 +1,17 @@
+// Android intents only foreground Piik. App-owned pending requests still own
+// permission, and the normal loopback approval/health checks own Site access.
+export function androidAppIntent(action: unknown, userAgent = typeof navigator === "undefined" ? "" : navigator.userAgent): string | null {
+ if (action !== "piik://app" && action !== "piik://media") return null;
+ // A connected App supplies its own media action. Android tablets can report
+ // a desktop UA; only an unconnected cold wake needs the browser OS hint.
+ if (action === "piik://app" && !/Android/i.test(userAgent)) return null;
+ return `intent://${action.slice("piik://".length)}#Intent;scheme=piik;package=tv.piik.android;end`;
+}
+
+// Called only after the pending picker request has been sent. A browser may
+// decline the handoff; the existing confirmation control remains available.
+export function activateNativePicker(action: unknown): void {
+ const intent = androidAppIntent(action);
+ if (!intent || !navigator.userActivation?.isActive) return;
+ try { window.location.assign(intent); } catch { /* Continue with manual confirmation. */ }
+}
diff --git a/src/client/native/client.ts b/src/client/native/client.ts
index 1e3d532d..23c79808 100644
--- a/src/client/native/client.ts
+++ b/src/client/native/client.ts
@@ -1,6 +1,8 @@
import type { z } from "zod";
import type { RemoteControlMode } from "../remote-control/protocol";
import { debugError, debugEvent } from "../lib/debug";
+import { clientRuntimePort } from "../lib/session";
+import { NATIVE_CLIENT_PORT_START, NATIVE_CLIENT_PORT_END } from "./discovery-ports";
import type {
IceConfig,
@@ -23,8 +25,6 @@ import {
nativeDiscoveryIdentitySchema,
nativeConnectSchema,
nativeHealthSchema,
- NATIVE_CLIENT_PORT_END,
- NATIVE_CLIENT_PORT_START,
NATIVE_CLIENT_PROTOCOL,
NATIVE_CLIENT_SUBPROTOCOL,
pongResponseSchema,
@@ -56,7 +56,7 @@ interface NativeDiscoveryOptions {
signal?: AbortSignal;
}
-function isLoopbackPage(): boolean {
+export function isLoopbackPage(): boolean {
return ["localhost", "127.0.0.1", "[::1]"].includes(window.location.hostname);
}
@@ -158,17 +158,21 @@ async function discoverNative<T>(
}
if (signal?.aborted) return null;
const controller = new AbortController();
+ const abort = () => controller.abort(signal?.reason);
+ signal?.addEventListener("abort", abort, { once: true });
// The first request may wait for browser permission. One shared deadline
// bounds the scan; a silent port must not hide an App on another port.
const timer = window.setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
try {
- return await Promise.any(Array.from({
+ // An explicit launch stays with its App even when another approved App is faster.
+ const selectedPort = clientRuntimePort();
+ const ports = selectedPort === null ? Array.from({
length: NATIVE_CLIENT_PORT_END - NATIVE_CLIENT_PORT_START + 1,
- }, async (_, index) => {
- const port = NATIVE_CLIENT_PORT_START + index;
+ }, (_, index) => NATIVE_CLIENT_PORT_START + index) : [selectedPort];
+ return await Promise.any(ports.map(async port => {
const response = await fetch(`http://127.0.0.1:${port}/${path}`, {
cache: "no-store",
- signal: signal ? AbortSignal.any([signal, controller.signal]) : controller.signal,
+ signal: controller.signal,
...(isLoopbackPage() ? {} : { targetAddressSpace: "loopback" }),
} as RequestInit);
if (!response.ok) throw new Error("Piik App discovery was not accepted");
@@ -197,6 +201,7 @@ async function discoverNative<T>(
debugEvent("native", "unavailable", { stage: "discovery", timedOut: controller.signal.aborted });
return null;
} finally {
+ signal?.removeEventListener("abort", abort);
window.clearTimeout(timer);
controller.abort();
}
@@ -335,6 +340,7 @@ export class NativeClient {
async startShare(
input: NativeShareInput,
+ onRequested?: () => void,
): Promise<{ audio: boolean; codec: NativeVideoCodec; sourceAudio?: boolean }> {
// Older Apps reject unknown command fields.
const { showCaptureBorder = false, excludeAudio, ...shareInput } = input;
@@ -348,9 +354,11 @@ export class NativeClient {
...(this.health.nativeMedia.microphone || this.health.nativeMedia.shareVolume ? { microphoneMixing: true } : {}),
...(this.health.nativeMedia.remoteInput ? { remoteInput: true } : {}),
...(this.health.nativeMedia.captureBorderControl ? { showCaptureBorder } : {}),
+ ...(this.health.nativeMedia.sourceCancellation ? { reportSourceCancellation: true } : {}),
},
shareStartedResponseSchema,
input.source.kind === "picker" ? null : REQUEST_TIMEOUT_MS,
+ onRequested,
);
if (response.shareId !== input.shareId) {
throw new Error("Native share identity changed");
@@ -395,6 +403,7 @@ export class NativeClient {
adapterIndex: path.adapterIndex,
encoderIndex: path.encoderIndex,
...(this.health.nativeMedia.captureBorderControl ? { showCaptureBorder } : {}),
+ ...(this.health.nativeMedia.sourceCancellation ? { reportSourceCancellation: true } : {}),
},
shareSourceReplacedResponseSchema,
source.kind === "picker" ? null : REQUEST_TIMEOUT_MS,
@@ -770,6 +779,7 @@ export class NativeClient {
fields: object,
schema: z.ZodType<T>,
timeoutMs: number | null = REQUEST_TIMEOUT_MS,
+ onRequested?: () => void,
): Promise<T> {
if (this.closed || this.socket.readyState !== WebSocket.OPEN) {
return Promise.reject(new NativeRequestError("unavailable", type));
@@ -814,7 +824,9 @@ export class NativeClient {
this.pending.delete(id);
debugError("native", "request-failed", error, { ...details, durationMs: performance.now() - began });
rejectRequest(new NativeRequestError("send-failed", type, { cause: error }));
+ return;
}
+ onRequested?.();
});
}
@@ -835,6 +847,10 @@ export class NativeClient {
if (pending.timer !== null) window.clearTimeout(pending.timer);
const failure = requestFailedResponseSchema.safeParse(value);
if (failure.success) {
+ if (failure.data.code === "source-canceled") {
+ pending.reject(new DOMException("Source selection canceled", "NotAllowedError"));
+ return;
+ }
pending.reject(new NativeRequestError("rejected", pending.operation, { cause: { code: failure.data.code } }));
return;
}
diff --git a/src/client/native/discovery-ports.ts b/src/client/native/discovery-ports.ts
new file mode 100644
index 00000000..0b806360
--- /dev/null
+++ b/src/client/native/discovery-ports.ts
@@ -0,0 +1,2 @@
+export const NATIVE_CLIENT_PORT_START = 39_721;
+export const NATIVE_CLIENT_PORT_END = 39_730;
diff --git a/src/client/native/quality-preference.ts b/src/client/native/quality-preference.ts
index 92600293..a086bfe1 100644
--- a/src/client/native/quality-preference.ts
+++ b/src/client/native/quality-preference.ts
@@ -1,6 +1,6 @@
import { qualitySettingsSchema, type QualitySettings } from "../../shared/protocol";
import { debugError } from "../lib/debug";
-import { discoverNativeHealth } from "./client";
+import { discoverNativeHealth, isLoopbackPage } from "./client";
import type { NativeHealth } from "./wire";
/** Optional App storage; never claims a media control session or delays sharing. */
@@ -21,7 +21,7 @@ export class AppQualityPreference {
}
async read(): Promise<QualitySettings | null> {
- const value: unknown = await (await this.request()).json();
+ const value = await this.request();
return value === null ? null : qualitySettingsSchema.parse(value);
}
@@ -50,16 +50,27 @@ export class AppQualityPreference {
}
}
- private async request(settings?: QualitySettings): Promise<Response> {
- const response = await fetch(`http://127.0.0.1:${this.health.port}/quality-preference`, {
- method: settings ? "PUT" : "GET",
- headers: { "Content-Type": "application/json", "X-Piik-Instance": this.health.instanceToken },
- ...(settings ? { body: JSON.stringify(settings) } : {}),
- cache: "no-store",
- signal: AbortSignal.any([this.signal, AbortSignal.timeout(4_000)]),
- targetAddressSpace: "loopback",
- } as RequestInit);
- if (!response.ok) throw new Error(`Piik App preference request failed (${response.status})`);
- return response;
+ private async request(settings?: QualitySettings): Promise<unknown> {
+ const controller = new AbortController();
+ const abort = () => controller.abort(this.signal.reason);
+ if (this.signal.aborted) abort();
+ else this.signal.addEventListener("abort", abort, { once: true });
+ const timer = setTimeout(() => controller.abort(new DOMException("Piik App preference request timed out", "TimeoutError")), 4_000);
+ try {
+ const response = await fetch(`http://127.0.0.1:${this.health.port}/quality-preference`, {
+ method: settings ? "PUT" : "GET",
+ headers: { "Content-Type": "application/json", "X-Piik-Instance": this.health.instanceToken },
+ ...(settings ? { body: JSON.stringify(settings) } : {}),
+ cache: "no-store",
+ signal: controller.signal,
+ ...(isLoopbackPage() ? {} : { targetAddressSpace: "loopback" }),
+ } as RequestInit);
+ if (!response.ok) throw new Error(`Piik App preference request failed (${response.status})`);
+ return settings ? undefined : await response.json();
+ } finally {
+ clearTimeout(timer);
+ this.signal.removeEventListener("abort", abort);
+ controller.abort();
+ }
}
}
diff --git a/src/client/native/wire.ts b/src/client/native/wire.ts
index 011f42d0..e105e1aa 100644
--- a/src/client/native/wire.ts
+++ b/src/client/native/wire.ts
@@ -1,11 +1,10 @@
import { z } from "zod";
+import { NATIVE_CLIENT_PORT_START, NATIVE_CLIENT_PORT_END } from "./discovery-ports";
import { remoteControlModeSchema, remoteInputPlatformSchema, remoteShortcutKeySchema } from "../remote-control/protocol";
import { iceCandidateSchema, sfuMediaSchema, positiveUint32Schema, qualityResolutionSchema, punchPredictionSchema,
nullableSafeEvidenceNumber, nullableSafeEvidenceInteger } from "../../shared/protocol";
export const NATIVE_CLIENT_PROTOCOL = 10;
-export const NATIVE_CLIENT_PORT_START = 39_721;
-export const NATIVE_CLIENT_PORT_END = 39_730;
export const NATIVE_CLIENT_SUBPROTOCOL = "piik-client-v10";
const decimalIdentifierSchema = z.string().regex(/^[1-9]\d{0,19}$/);
@@ -29,6 +28,7 @@ export const nativeDiscoveryIdentitySchema = z.object({
export const nativeHealthSchema = nativeDiscoveryIdentitySchema.extend({
protocol: z.literal(NATIVE_CLIENT_PROTOCOL),
qualityPreference: z.boolean().optional(),
+ activationURL: z.literal("piik://media").optional(),
instanceToken: z
.string()
.length(43)
@@ -52,6 +52,7 @@ export const nativeHealthSchema = nativeDiscoveryIdentitySchema.extend({
processAudioExclusion: z.boolean().default(false),
microphone: z.boolean().default(false),
shareVolume: z.boolean().optional(),
+ sourceCancellation: z.boolean().optional(),
// API availability, not Windows approval or observed border visibility.
captureBorderControl: z.boolean().default(false),
hardwareH264: z.boolean().default(false),
@@ -152,7 +153,7 @@ export const requestFailedResponseSchema = z
.object({
...responseBase,
type: z.literal("request-failed"),
- code: z.literal("operation-failed"),
+ code: z.enum(["operation-failed", "source-canceled"]),
})
.strict();
export const captureOptionsResponseSchema = z
diff --git a/src/client/pages/AppLauncherPage.tsx b/src/client/pages/AppLauncherPage.tsx
index 48097fe0..ab750409 100644
--- a/src/client/pages/AppLauncherPage.tsx
+++ b/src/client/pages/AppLauncherPage.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef, useState, type FormEvent } from "react";
+import { useEffect, useRef, useState, useSyncExternalStore, type FormEvent } from "react";
import { z } from "zod";
import { BrandMark } from "../components/living/BrandMark";
@@ -11,7 +11,7 @@ import { Btn, Pill } from "../components/living/primitives";
import { hasCopyPreference, isCopyKey, useCopy } from "../ui/copy";
import { consoleLanguage } from "../locales";
import { Glyph } from "../ui/icons";
-import { currentThemePreference } from "../ui/theme";
+import { currentThemePreference, subscribeTheme } from "../ui/theme";
import { clientLaunchURL, type ClientLaunchPresentation } from "../lib/session";
import {
checkReleaseUpdate,
@@ -23,6 +23,7 @@ const launcherStateSchema = z.object({
startupSite: z.string(),
localAccessPassword: z.string(),
defaultMode: z.enum(["local", "link"]),
+ modes: z.array(z.enum(["local", "link"])).default(["local", "link"]),
target: z.string().optional(),
starting: z.boolean().optional(),
reason: z.string().optional(),
@@ -30,6 +31,7 @@ const launcherStateSchema = z.object({
revision: z.string(),
version: z.string().default("development"),
packageTarget: z.string().optional(),
+ controlPort: z.number().int().optional(),
debug: z.boolean().optional(),
lan: z.object({
addresses: z.array(z.object({ address: z.string(), name: z.string() })),
@@ -41,9 +43,12 @@ const launcherErrorSchema = z.object({ reason: z.string().optional(), detail: z.
export function AppLauncherPage() {
const { lang, vis, t } = useCopy();
+ // Rendered entry URLs follow theme changes before any browser link action.
+ useSyncExternalStore(subscribeTheme, currentThemePreference);
const [loading, setLoading] = useState(true);
const [starting, setStarting] = useState(false);
const [mode, setMode] = useState<AppMode>("link");
+ const [modes, setModes] = useState<readonly AppMode[]>(["local", "link"]);
const [sites, setSites] = useState<string[]>([]);
const [startupSite, setStartupSite] = useState("");
const [openOnApproval, setOpenOnApproval] = useState(false);
@@ -58,11 +63,15 @@ export function AppLauncherPage() {
const [update, setUpdate] = useState<ReleaseUpdateNotice | null>(null);
const [version, setVersion] = useState<string>();
const [target, setTarget] = useState<string>();
+ const [controlPort, setControlPort] = useState<number>();
const [siteBusy, setSiteBusy] = useState(false);
const [siteError, setSiteError] = useState<string>();
+ const [siteEntry, setSiteEntry] = useState({ origin: "", confirming: false });
// Background reads cannot replace a newer command's result or optimistic state.
const stateChanges = useRef({ revision: 0, pending: 0 });
- const requestedSite = new URLSearchParams(window.location.search).get("site");
+ const externalSite = new URLSearchParams(window.location.search).get("site");
+ const manualSite = externalSite === null && siteEntry.confirming;
+ const requestedSite = externalSite ?? (manualSite ? siteEntry.origin : null);
const validSite = requestedSite && (() => {
try {
const url = new URL(requestedSite);
@@ -77,8 +86,8 @@ export function AppLauncherPage() {
// The requesting page still verifies authorized health before using App.
// The opener closes this page. Finish an explicitly selected startup
// preference first, and leave a failed save visible for correction.
- if (approvalCurrent && !siteBusy && !siteError) window.opener?.postMessage({ type: "piik-app-approved", site: requestedSite }, requestedSite);
- }, [approvalCurrent, requestedSite, siteBusy, siteError]);
+ if (!manualSite && approvalCurrent && !siteBusy && !siteError) window.opener?.postMessage({ type: "piik-app-approved", site: requestedSite }, requestedSite);
+ }, [manualSite, approvalCurrent, requestedSite, siteBusy, siteError]);
useEffect(() => {
let current = true;
@@ -105,11 +114,14 @@ export function AppLauncherPage() {
if (!fresh()) return;
if (!restoredSettings) {
setMode(state.defaultMode);
+ setModes(state.modes);
setLocalAccessPassword(state.localAccessPassword);
setLan(state.lan);
restoredSettings = true;
}
- setVersion(state.version);
+ setVersion(state.version === "development" && /^[a-f0-9]{40}$/i.test(state.revision)
+ ? `${state.version} (${state.revision.slice(0, 7)})` : state.version);
+ setControlPort(state.controlPort);
setTarget(state.target);
setStarting(state.starting === true);
setSites(state.sites);
@@ -182,7 +194,7 @@ export function AppLauncherPage() {
...(theme !== null ? ["theme" as const] : []),
],
};
- return clientLaunchURL(target, presentation, debug);
+ return clientLaunchURL(target, presentation, debug, controlPort);
}
async function launch(event: FormEvent<HTMLFormElement>): Promise<void> {
@@ -223,8 +235,8 @@ export function AppLauncherPage() {
} finally { finish(); }
}
- async function changeSite(nextSite: string, setting: "site" | "startup-site" = "site", starAfterApproval = false, allowed = true): Promise<void> {
- if (siteBusy) return;
+ async function changeSite(nextSite: string, setting: "site" | "startup-site" = "site", starAfterApproval = false, allowed = true): Promise<boolean> {
+ if (siteBusy) return false;
const finish = beginStateChange();
setSiteBusy(true);
setSiteError(undefined);
@@ -246,19 +258,26 @@ export function AppLauncherPage() {
return true;
}
try {
- if (await save(setting) && starAfterApproval) await save("startup-site");
+ return await save(setting) && (!starAfterApproval || await save("startup-site"));
} catch {
setSiteError(t("client.connect.saveFailed"));
+ return false;
} finally { finish(); setSiteBusy(false); }
}
+ async function approveSite(site: string): Promise<void> {
+ if (await changeSite(site, "site", openOnApproval) && manualSite) {
+ window.location.replace(launchURL(`${site}/#piik-client=1`));
+ }
+ }
+
const errorKey = `client.launch.failure.${error?.reason}`;
return (
<div className="lr-app">
<AppHeader homeHref="/client" diagnosticControl={appDebug === undefined ? null : (
<Btn icon="cpu" title="client.launch.debugHint" size="compact"
pressed={debug} tone={debug ? "on" : undefined} hint="debug-start"
- disabled={appDebug || debugBusy || loading || starting || !!target || error !== null}
+ disabled={appDebug || debugBusy || loading || starting || !!target || error?.kind === "load"}
onClick={() => void enableDebug()} />
)} />
<main className="lr-client-launch">
@@ -308,10 +327,21 @@ export function AppLauncherPage() {
<span className={vis ? "visually-hidden" : undefined}>{t("client.connect.openOnStart")}</span>
</label>}
<div className="lr-client-approval-actions">
- <Btn icon="x" title={approvalCurrent ? "client.connect.close" : "common.cancel"} cap={approvalCurrent ? "client.connect.close" : "common.cancel"} hint="hint-close"
- onClick={() => window.close()} />
- {!approvalCurrent && <Btn icon="check" title="client.connect.approve" cap="client.connect.approve" tone="primary"
- hint="hint-client-site" busy={siteBusy} disabled={!validSite || siteBusy} onClick={() => void changeSite(requestedSite, "site", openOnApproval)} />}
+ <Btn icon="x" title={!manualSite && approvalCurrent ? "client.connect.close" : "common.cancel"} cap={!manualSite && approvalCurrent ? "client.connect.close" : "common.cancel"} hint="hint-close"
+ disabled={manualSite && siteBusy} onClick={() => {
+ if (manualSite) {
+ setSiteEntry(entry => ({ ...entry, confirming: false }));
+ setSiteError(undefined);
+ setOpenOnApproval(false);
+ } else window.close();
+ }} />
+ {(!approvalCurrent || (manualSite && siteBusy)) && <Btn icon="check" title={manualSite ? "client.connect.approveAndOpen" : "client.connect.approve"}
+ cap={manualSite ? "client.connect.approveAndOpen" : "client.connect.approve"} tone="primary"
+ hint="hint-client-site" busy={siteBusy} disabled={!validSite || siteBusy} onClick={() => void approveSite(requestedSite)} />}
+ {manualSite && approvalCurrent && !siteBusy && <a className="lr-btn is-primary" href={launchURL(`${requestedSite}/#piik-client=1`)}>
+ <Glyph name="arrowRight" size={19} />
+ <span className={vis ? "visually-hidden" : "lr-cap"}>{t("client.connect.openSite")}</span>
+ </a>}
</div>
</section>
) : (
@@ -319,14 +349,20 @@ export function AppLauncherPage() {
roomError={error?.kind === "launch" ? <>
<Pill icon="alert" tone="bad" label={t("client.launch.error")} alert comic="signal-failed" />
{isCopyKey(errorKey) && <p className={vis ? "visually-hidden" : "lr-client-launch-status"}>{t(errorKey)}</p>}
- <p className={vis ? "visually-hidden" : "lr-client-launch-status"}>{t("client.launch.reopen")}</p>
+ <p className={vis ? "visually-hidden" : "lr-client-launch-status"}>{t("client.launch.retryHint")}</p>
{error.detail && <details className="lr-client-launch-technical">
<summary>{t("client.launch.errorDetails")}</summary>
<p className="lr-client-launch-detail">{error.detail}</p>
</details>}
</> : undefined}
mode={mode}
+ modes={modes}
sites={sites}
+ siteAddress={siteEntry.origin}
+ onSelectSite={origin => {
+ setSiteEntry({ origin, confirming: true });
+ setSiteError(undefined);
+ }}
startupSite={startupSite}
onToggleStartupSite={site => void changeSite(startupSite === site ? "" : site, "startup-site")}
localAccessPassword={localAccessPassword}
@@ -335,7 +371,7 @@ export function AppLauncherPage() {
siteBusy={siteBusy}
siteError={siteError}
target={target}
- onOpen={(event) => { event.currentTarget.href = launchURL(event.currentTarget.href); }}
+ entryURL={launchURL}
onLocalAccessPasswordChange={setLocalAccessPassword}
lan={lan && { ...lan, onChange: (selected) => setLan({ ...lan, selected }) }}
onSubmit={launch}
diff --git a/src/client/pages/ControlsPreview.tsx b/src/client/pages/ControlsPreview.tsx
index 4703a47c..122b533b 100644
--- a/src/client/pages/ControlsPreview.tsx
+++ b/src/client/pages/ControlsPreview.tsx
@@ -82,6 +82,11 @@ export function ControlsPreview() {
const [invalid, setInvalid] = useState(false);
const [sourceOpen, setSourceOpen] = useState(displayPreview);
const [sourceRevision, setSourceRevision] = useState(0);
+ const [systemSourcePicker, setSystemSourcePicker] = useState(false);
+ const [browserCaptureSupported, setBrowserCaptureSupported] = useState(true);
+ const [nativePermissionPreview, setNativePermissionPreview] = useState(false);
+ const sourceTargets: NativeCaptureTarget[] = systemSourcePicker
+ ? [{ kind: "picker", sourceId: "1", title: "System chooser" }] : SOURCES.sources;
const [sourceState, setSourceState] = useState<NativeSourceList["kind"] | "refreshing" | "empty">("ready");
const [captureBorderAvailable, setCaptureBorderAvailable] = useState(true);
const [showCaptureBorder, setShowCaptureBorder] = useState(false);
@@ -284,6 +289,10 @@ export function ControlsPreview() {
{en ? ["Available", "Loading", "Refreshing", "App unavailable", "Update needed", "Capture unavailable", "Read failed", "Empty list"][index] : ["正常", "读取中", "刷新中", "App 未连接", "需要更新", "无法采集", "读取失败", "空列表"][index]}</Chip>)}
<SwitchItem checked={captureBorderAvailable} onChange={setCaptureBorderAvailable}
label={en ? "Windows border control" : "Windows 边框控制"} />
+ <SwitchItem checked={systemSourcePicker} onChange={value => { setSystemSourcePicker(value); setSourceOpen(true); setSourceRevision(value => value + 1); }}
+ label={t("host.sourcePicker.tab.picker")} />
+ <SwitchItem checked={browserCaptureSupported} onChange={setBrowserCaptureSupported}
+ label={en ? "Browser screen capture" : "浏览器录屏"} />
<a className="lr-btn" href="/__tooltip-preview?source=display#source-preview" onClick={() => {
// The same URL does not remount the picker or reset its selected tab.
setSourceState("ready"); setSourceOpen(true); setSourceRevision(value => value + 1);
@@ -294,14 +303,16 @@ export function ControlsPreview() {
<div className="cp-stage"><StageTv hasEntry label={t("host.sourcePicker.title")}>
<img className="cp-poster" src={POSTER} alt="" />
{sourceOpen ? <CaptureSourcePicker key={sourceRevision} nativeSources={sourceState === "empty" ? { ...SOURCES, sources: [], captureBorderControl: captureBorderAvailable }
- : sourceState === "ready" ? { ...SOURCES, captureBorderControl: captureBorderAvailable }
- : sourceState === "refreshing" ? { kind: "loading", appDetected: true } : { kind: sourceState }}
+ : sourceState === "ready" ? systemSourcePicker
+ ? { ...SOURCES, sources: sourceTargets, processAudio: false, processAudioExclusion: false, captureBorderControl: false }
+ : { ...SOURCES, captureBorderControl: captureBorderAvailable }
+ : sourceState === "refreshing" ? { kind: "loading", appDetected: true, sources: sourceTargets } : { kind: sourceState }}
initialShowCaptureBorder={showCaptureBorder}
+ browserSupported={browserCaptureSupported}
initialTab={displayPreview ? "display" : "window"}
onBrowser={() => { setSourceOpen(false); notify(); }}
onCamera={() => { setSourceOpen(false); notify(); }}
loadCameras={previewCameras}
- onAppConnected={() => setSourceState("ready")}
onNative={(_target, _audio, showBorder) => { setShowCaptureBorder(showBorder); setSourceOpen(false); notify(); }}
onPreview={previewSource} onRefresh={() => setSourceState("refreshing")} onCancel={() => setSourceOpen(false)} />
: <div className="cp-stage-action"><Btn icon="cast" tone="primary" title="host.start" cap="host.start" hint="hint-share-start" onClick={() => setSourceOpen(true)} /></div>}
@@ -319,6 +330,12 @@ export function ControlsPreview() {
onToggleTheater={() => setTheater(!theater)} onReconnect={notify} reconnectAvailable />
</StageTv></div></div>
<p><a href="/__playback-preview">{en ? "Open the full playback preview" : "进入完整播放预览"} →</a></p>
+ <SwitchItem checked={nativePermissionPreview} onChange={setNativePermissionPreview}
+ label={t("native.confirmInApp")} />
+ {nativePermissionPreview && <div className="cp-stage"><StageTv label={t("native.confirmInApp")}>
+ <StageOverlay icon="window" comic="source-starting" tone="busy" message={t("native.confirmInApp")}
+ onActivate={() => { setNativePermissionPreview(false); notify(); }} />
+ </StageTv></div>}
</section>
<section id="launcher-preview" className="cp-section">
<header className="cp-section-head"><span className="cp-number">08</span><h2>{t("client.launch.title")}</h2></header>
@@ -328,11 +345,14 @@ export function ControlsPreview() {
</button>
<SwitchItem checked={launchSites.length > 1} label={en ? "Many approved sites" : "多个已授权站点"}
onChange={many => { setLaunchSites(many ? ["https://demo.piik.tv", "https://friends.example.org", "https://screen-sharing-with-friends.preview.example.org:8443", "https://movie-night.example.net", "https://192.0.2.10:8787", "https://weekend-games.example.org"] : ["https://demo.piik.tv"]); setStartupSite(""); }} />
+ <SwitchItem checked={launchSites.length === 0} label={en ? "No approved sites" : "尚未授权站点"}
+ onChange={empty => { setLaunchSites(empty ? [] : ["https://demo.piik.tv"]); setStartupSite(""); }} />
</div>
<div className="lr-client-launch">
<LauncherForm mode={launchMode} onModeChange={setLaunchMode}
startupSite={startupSite} onToggleStartupSite={site => setStartupSite(value => value === site ? "" : site)}
- sites={launchSites} onRemoveSite={site => { setLaunchSites(sites => sites.filter(value => value !== site)); if (startupSite === site) setStartupSite(""); }}
+ sites={launchSites} onSelectSite={notify} onOpen={event => { event.preventDefault(); notify(); }}
+ onRemoveSite={site => { setLaunchSites(sites => sites.filter(value => value !== site)); if (startupSite === site) setStartupSite(""); }}
lan={{ selected: launchLan, onChange: setLaunchLan, addresses: [
{ address: "192.0.2.10", name: "Wi-Fi" },
{ address: "192.0.2.20", name: "vEthernet (Example Virtual Network Adapter with a Long Name)" },
@@ -368,8 +388,8 @@ export function ControlsPreview() {
function InteractionControlsPreview() {
const [viewer, setViewer] = useState(false);
const { session, receiveSample } = usePreviewInteractions(viewer);
- const voice = useRoomVoicePreview(viewer);
const [crowded, setCrowded] = useState(false);
+ const voice = useRoomVoicePreview(viewer, crowded);
const [reaction, setReaction] = useState<ReactionId>("heart");
const [theater, setTheater] = useTheaterMode();
const video = useRef<HTMLVideoElement>(null);
diff --git a/src/client/pages/HostPage.tsx b/src/client/pages/HostPage.tsx
index 4e864d81..f2f7cf43 100644
--- a/src/client/pages/HostPage.tsx
+++ b/src/client/pages/HostPage.tsx
@@ -31,7 +31,7 @@ import { RoomInteractions } from "../components/living/RoomInteractions";
import { RoomChatOverlay, RoomChatOverlaySettings, RoomChatToggle } from "../components/living/RoomChatOverlay";
import { PlaybackControls } from "../components/living/PlaybackControls";
import { useTheaterMode } from "../components/living/use-theater-mode";
-import { isPictureInPicture } from "../components/living/use-picture-in-picture";
+import { useHostPreviewPlayback } from "../components/living/use-host-preview-playback";
import { readRoomPreferences, saveRoomPreferences } from "../lib/room-preferences";
import type { VideoFit } from "../media/video-geometry";
import type { RoomInteractionSession } from "../lib/room-interactions";
@@ -180,6 +180,7 @@ import type {
import { HostPeer, type HostMediaPeer } from "../webrtc/host-peer";
import { NativeClient, NativeCompatibilityError, NativeRequestError } from "../native/client";
import { NativeClientConnection } from "../native/client-connection";
+import { activateNativePicker, androidAppIntent } from "../native/activation";
import {
NativeSenderPeer,
shouldUseBrowserQualityCandidate,
@@ -212,7 +213,6 @@ import { preferredVideoCodecForTrack } from "../webrtc/video-codec-preflight";
import {
hostActionErrorKey,
hostServerErrorKey,
- shouldPauseLocalPreview,
sourceSwitchNotice,
isCapturePermissionFailure,
hostFailureCode,
@@ -231,6 +231,7 @@ type NoticeValue = (
};
type StatusNoticeMessage = string | { key: CopyKey; vars?: Record<string, string>; failureCode?: string };
+type SourceSwitch = { replacingVideo?: MediaStreamTrack; audioOnly?: boolean; nativePending?: boolean };
type ViewerQualityEvidence = Extract<
ServerMessage,
@@ -564,11 +565,11 @@ export function HostPage({
const copied = copiedRoomLink !== null && copiedRoomLink === roomLink;
const copiedResetTimerRef = useRef<number | null>(null);
const copyRoomLinkRequestRef = useRef<object | null>(null);
- const [switchingSource, setSwitchingSource] = useState(false);
+ const [sourceSwitchState, setSourceSwitch] = useState<SourceSwitch | null>(null);
+ const switchingSource = sourceSwitchState !== null;
const [changingQuality, setChangingQuality] = useState(false);
const [sharingPaused, setSharingPaused] = useState(false);
- const [localPreviewPaused, setLocalPreviewPaused] = useState(false);
- const previewPausedByUser = useRef(false);
+ const [previewAutoPause, setPreviewAutoPause] = useState(() => readRoomPreferences().previewAutoPause);
const [theaterMode, setTheaterMode] = useTheaterMode();
const [pictureFit, setPictureFit] = useState<VideoFit>(() => readRoomPreferences().pictureFit);
const [pictureRotation, setPictureRotation] = useState(() => readRoomPreferences().pictureRotation);
@@ -617,7 +618,7 @@ export function HostPage({
const activeRouteRevisionRef = useRef(0);
const generationRef = useRef(0);
const activeGenerationRef = useRef<number | null>(null);
- const sourceSwitchRef = useRef<{ replacingVideo?: MediaStreamTrack; audioOnly?: boolean } | null>(null);
+ const sourceSwitchRef = useRef<SourceSwitch | null>(null);
const qualityChangeRef = useRef<object | null>(null);
const pendingQualityChangeRef = useRef<QualitySettings | null>(null);
const qualitySettingsRef = useRef<QualitySettings>(qualitySettings);
@@ -651,6 +652,9 @@ export function HostPage({
if (!client || !nativeModeRef.current) throw new Error("Piik App is unavailable");
return client.sources();
}, []);
+ const microphoneClient = nativeActive ? nativeConnection.current : null;
+ const loadMicrophoneDevices = useCallback(() => microphoneClient
+ ? microphoneClient.microphones() : browserCaptureDevices("audioinput"), [microphoneClient]);
const nativeSourceRequestRef = useRef<object | null>(null);
const nativePreviewTailRef = useRef<Promise<void>>(Promise.resolve());
const nativeSourcePathRef = useRef<NativeCapturePath | null>(null);
@@ -696,7 +700,6 @@ export function HostPage({
videoRef.current.srcObject = stream;
}
if (!stream) {
- previewPausedByUser.current = false;
setTheaterMode(false);
}
}, [stream]);
@@ -710,7 +713,10 @@ export function HostPage({
});
}, [stream]);
+ const previewPlayback = useHostPreviewPlayback(videoRef, stream, previewAutoPause);
+
useEffect(() => {
+ if (!stream) return;
let senderQualitySuspended = false;
const resetSenderQualityAuthority = () => {
if (senderQualitySuspended) {
@@ -722,48 +728,20 @@ export function HostPage({
signalRef.current?.send({ type: "reset-sender-quality" });
}
};
- const syncPreviewPlayback = () => {
- const video = videoRef.current;
- if (!video) {
- return;
- }
- const shouldPause = shouldPauseLocalPreview(
- document.visibilityState,
- document.hasFocus(),
- isPictureInPicture(video),
- );
+ const syncSenderQualityAuthority = () => {
if (document.visibilityState !== "visible") {
resetSenderQualityAuthority();
} else {
senderQualitySuspended = false;
}
- setLocalPreviewPaused(shouldPause);
- if (shouldPause) {
- video.pause();
- return;
- }
- if (streamRef.current && !previewPausedByUser.current) {
- void video.play().catch(() => undefined);
- }
};
- document.addEventListener("visibilitychange", syncPreviewPlayback);
- window.addEventListener("blur", syncPreviewPlayback);
- window.addEventListener("focus", syncPreviewPlayback);
+ document.addEventListener("visibilitychange", syncSenderQualityAuthority);
window.addEventListener("pagehide", resetSenderQualityAuthority);
- const video = videoRef.current;
- video?.addEventListener("enterpictureinpicture", syncPreviewPlayback);
- video?.addEventListener("leavepictureinpicture", syncPreviewPlayback);
- video?.addEventListener("webkitpresentationmodechanged", syncPreviewPlayback);
- syncPreviewPlayback();
+ syncSenderQualityAuthority();
return () => {
- document.removeEventListener("visibilitychange", syncPreviewPlayback);
- window.removeEventListener("blur", syncPreviewPlayback);
- window.removeEventListener("focus", syncPreviewPlayback);
+ document.removeEventListener("visibilitychange", syncSenderQualityAuthority);
window.removeEventListener("pagehide", resetSenderQualityAuthority);
- video?.removeEventListener("enterpictureinpicture", syncPreviewPlayback);
- video?.removeEventListener("leavepictureinpicture", syncPreviewPlayback);
- video?.removeEventListener("webkitpresentationmodechanged", syncPreviewPlayback);
};
}, [stream]);
@@ -1001,7 +979,7 @@ export function HostPage({
setViewerQualityEvidence(viewerQualityEvidenceStore.getSnapshot());
activeRouteRevisionRef.current = 0;
if (!keepRoomSession) setSignalStatus("offline");
- setSwitchingSource(false);
+ setSourceSwitch(null);
setChangingQuality(false);
sharingPausedRef.current = false;
setSharingPaused(false);
@@ -1311,11 +1289,21 @@ export function HostPage({
generation: number,
shareGeneration: string,
selection: Extract<ShareSourceSelection, { kind: "native" }>,
+ replacement?: SourceSwitch,
): Promise<MediaStream | null> {
const { client, target, audio, excludeAudio, showCaptureBorder, path } = selection;
+ // App capture lifetime is separate from the room's unchanged publication.
+ const nativeShareId = replacement ? createOpaqueId() : shareGeneration;
+ nativeSourceSelectionRef.current = selection;
let bridge: NativeMediaBridge | null = null;
let shareStarted = false;
let nativeEventCleanup: (() => void) | null = null;
+ let pendingRemoteControl: RemoteControlState | null = null;
+ const applyRemoteControl = (state: RemoteControlState) => {
+ setRemoteControlState(state);
+ signalRef.current?.setCoPlayGrants(state.coPlay ? state.requests.flatMap(request =>
+ request.state !== "pending" && request.endpointId ? [{ endpointId: request.endpointId }] : []) : []);
+ };
try {
await nativeShareCleanupRef.current;
await nativePreviewTailRef.current;
@@ -1324,44 +1312,48 @@ export function HostPage({
// Acquire the required Browser peer before starting capture. Pending
// cancellation owns this bridge, but still closes control to abort startup.
bridge = new NativeMediaBridge(
- shareGeneration,
+ nativeShareId,
client,
() => {
if (
nativeMediaBridgeRef.current === bridge &&
- isCurrentShare(generation, shareGeneration)
+ isCurrentShare(generation, shareGeneration) &&
+ (!replacement || streamRef.current === bridge?.stream)
) {
endSharing({ key: "native.fail.edge", failureCode: "app/browser-media/connection" }, true, "route-failed", "bad");
}
},
undefined,
() => {
- if (nativeMediaBridgeRef.current === bridge && isCurrentShare(generation, shareGeneration)) {
+ if (nativeMediaBridgeRef.current === bridge && isCurrentShare(generation, shareGeneration) &&
+ (!replacement || nativeModeRef.current)) {
nativeInputRoute.setUpstream(bridge?.getRemoteInput() ?? null);
}
},
);
nativeMediaBridgeRef.current = bridge;
nativeSourceAudioRef.current = undefined;
- audioAvailableRef.current = false;
- nativeSourceSelectionRef.current = selection;
+ if (!replacement) audioAvailableRef.current = false;
nativeEventCleanup = client.onEvent((event) => {
- if (event.shareId !== shareGeneration || !isCurrentShare(generation, shareGeneration) || nativeConnection.current !== client) return;
+ if (event.shareId !== nativeShareId || !isCurrentShare(generation, shareGeneration) || nativeConnection.current !== client) return;
if (event.type === "remote-control-state") {
- setRemoteControlState(event);
- signalRef.current?.setCoPlayGrants(event.coPlay ? event.requests.flatMap(request =>
- request.state !== "pending" && request.endpointId
- ? [{ endpointId: request.endpointId }] : []) : []);
+ if (replacement && !nativeModeRef.current) pendingRemoteControl = event;
+ else applyRemoteControl(event);
}
if (event.type === "audio-state") {
- setCommentary(event.microphone);
nativeSourceAudioRef.current = event.sourceAudio;
+ if (replacement && !nativeModeRef.current) return;
+ setCommentary(event.microphone);
audioAvailableRef.current = event.sourceAudio || event.microphone;
signalRef.current?.setAudioAvailable(audioAvailableRef.current);
setDetails(previous => previous ? { ...previous, hasSourceAudio: event.sourceAudio } : previous);
if (event.failed) setNoticeValue({ kind: "key", key: "host.audioInput.stopped", target: "operation", comic: "warning", tone: "warn" });
}
if (event.type === "share-ended") {
+ if (replacement && streamRef.current !== bridge?.stream) {
+ bridge?.dispose();
+ return;
+ }
endSharing(
event.failed ? { key: "host.shareEnded", failureCode: "app/capture/failed" } : { key: "host.stopNotice" },
true,
@@ -1372,7 +1364,7 @@ export function HostPage({
});
nativeEventCleanupRef.current = nativeEventCleanup;
const started = await client.startShare({
- shareId: shareGeneration,
+ shareId: nativeShareId,
source: target,
audio,
excludeAudio: audio ? excludeAudio : undefined,
@@ -1382,38 +1374,59 @@ export function HostPage({
edgeCapacity: MAX_ENDPOINT_MEDIA_CHILDREN,
profile: qualitySettingsRef.current,
codec: videoCodecModeRef.current,
- });
+ }, target.kind === "picker" ? () => activateNativePicker(client.health.activationURL) : undefined);
shareStarted = true;
if (!isCurrentShare(generation, shareGeneration)) {
if (nativeMediaBridgeRef.current === bridge) nativeMediaBridgeRef.current = null;
bridge.dispose();
- await client.stopShare(shareGeneration).catch(() => nativeConnection.discard(client));
+ await client.stopShare(nativeShareId).catch(() => nativeConnection.discard(client));
return null;
}
if (nativeConnection.current !== client) throw new NativeRequestError("unavailable", "start-share");
nativeSourceAudioRef.current ??= started.sourceAudio ?? started.audio;
- audioAvailableRef.current = nativeSourceAudioRef.current || audioAvailableRef.current;
- videoCodecRef.current = manualVideoCodecPreference(started.codec);
+ if (!replacement) audioAvailableRef.current = nativeSourceAudioRef.current || audioAvailableRef.current;
+ if (!replacement) videoCodecRef.current = manualVideoCodecPreference(started.codec);
// Register ownership before waiting for the local bridge. A native edge
// may fail immediately after becoming ready.
nativeConnection.adopt(client);
- nativeShareGenerationRef.current = shareGeneration;
- nativeModeRef.current = true;
- if (client.health.nativeMedia.remoteInput) await client.setRemoteInputPeers(shareGeneration, remoteInputPeersRef.current);
- setNativeActive(true);
+ nativeShareGenerationRef.current = nativeShareId;
+ if (replacement) {
+ replacement.nativePending = false;
+ setSourceSwitch({ ...replacement });
+ } else nativeModeRef.current = true;
+ if (client.health.nativeMedia.remoteInput) await client.setRemoteInputPeers(nativeShareId, remoteInputPeersRef.current);
+ if (!replacement) setNativeActive(true);
const stream = await bridge.start(started.audio);
- if (!isCurrentShare(generation, shareGeneration)) {
+ if (replacement) {
+ if (sharingPausedRef.current) await client.setPaused(nativeShareId, true);
+ if (client.health.nativeMedia.shareVolume) {
+ await client.setShareVolume(nativeShareId, shareVolume.muted ? 0 : shareVolume.level);
+ }
+ }
+ if (!isCurrentShare(generation, shareGeneration) || (replacement && sourceSwitchRef.current !== replacement)) {
if (nativeMediaBridgeRef.current === bridge) disposeNativeShare();
else bridge.dispose();
return null;
}
+ if (replacement && !stream.getVideoTracks().some(track => track.readyState === "live")) {
+ throw new NativeMediaBridgeError("Native capture ended before source replacement");
+ }
+ if (replacement) {
+ audioAvailableRef.current = !!nativeSourceAudioRef.current;
+ videoCodecRef.current = manualVideoCodecPreference(started.codec);
+ nativeModeRef.current = true;
+ setNativeActive(true);
+ if (pendingRemoteControl) applyRemoteControl(pendingRemoteControl);
+ nativeInputRoute.setUpstream(bridge.getRemoteInput());
+ }
return stream;
} catch (error) {
+ if (nativeSourceSelectionRef.current === selection) nativeSourceSelectionRef.current = null;
nativeEventCleanup?.();
if (nativeEventCleanupRef.current === nativeEventCleanup) nativeEventCleanupRef.current = null;
if (
nativeConnection.current === client &&
- nativeShareGenerationRef.current === shareGeneration
+ nativeShareGenerationRef.current === nativeShareId
) {
disposeNativeShare();
} else {
@@ -1421,7 +1434,7 @@ export function HostPage({
bridge?.dispose();
if (shareStarted) {
nativeShareCleanupRef.current = client
- .stopShare(shareGeneration)
+ .stopShare(nativeShareId)
.catch(() => nativeConnection.discard(client));
} else {
nativeConnection.discard(client);
@@ -1458,7 +1471,7 @@ export function HostPage({
function releaseUnusedNativeClient(): void {
// The picker may hand its connection to a starting share; only Native media
// needs to keep it after startup. Merely visiting the page owns no session.
- if (nativeSourceRequestRef.current || nativeShareGenerationRef.current ||
+ if (nativeSourceRequestRef.current || nativeShareGenerationRef.current || sourceSwitchRef.current ||
(activeGenerationRef.current !== null && roomMutationRef.current !== null)) return;
nativeConnection.release();
}
@@ -1479,11 +1492,13 @@ export function HostPage({
const request = {};
nativeSourceRequestRef.current = request;
nativeSourcePathRef.current = null;
- if (!isClientActivated() || (phase === "live" && !nativeModeRef.current)) {
+ if (!isClientActivated()) {
setNativeSources({ kind: "browser" });
return;
}
- setNativeSources(previous => ({ kind: "loading", appDetected: nativeAppDetected(previous) }));
+ setNativeSources(previous => ({ kind: "loading", appDetected: nativeAppDetected(previous),
+ sources: previous?.kind === "ready" || previous?.kind === "loading" ? previous.sources : undefined,
+ }));
let client: NativeClient | null;
try {
@@ -1576,19 +1591,28 @@ export function HostPage({
function recoverBrowserFanout(ingress: NativeMediaIngress): void {
if (nativeMediaIngressRef.current !== ingress) return;
+ const peerIds = retireNativeSenderPeers();
disposeNativeShare();
- discardPreparedHostChild();
const generation = activeGenerationRef.current;
if (generation === null || !streamRef.current) return;
- for (const [peerId, peer] of peersRef.current) {
- if (!(peer instanceof NativeSenderPeer)) continue;
- removePeer(peerId);
+ for (const peerId of peerIds) {
void startPeer(peerId, generation).catch((error: unknown) => {
if (isCurrentGeneration(generation)) setNoticeError(error, "connection");
});
}
}
+ function retireNativeSenderPeers(): string[] {
+ discardPreparedHostChild();
+ const peerIds: string[] = [];
+ for (const [peerId, peer] of peersRef.current) {
+ if (!(peer instanceof NativeSenderPeer)) continue;
+ removePeer(peerId);
+ peerIds.push(peerId);
+ }
+ return peerIds;
+ }
+
function requestSharing(): void {
setJoiningRoom(false);
void openCaptureSourcePicker();
@@ -1634,8 +1658,9 @@ export function HostPage({
const path = nativeSourcePathRef.current;
if (!client || !path) return;
setShowCaptureBorder(showCaptureBorder);
- if (phase === "live" && nativeModeRef.current) {
- void switchNativeSource(client, target, audio, path, showCaptureBorder, excludeAudio);
+ if (phase === "live") {
+ if (nativeModeRef.current) void switchNativeSource(client, target, audio, path, showCaptureBorder, excludeAudio);
+ else void switchToNativeSource({ kind: "native", client, target, audio, path, showCaptureBorder, excludeAudio });
return;
}
void startSharing({ kind: "native", client, target, audio, excludeAudio, showCaptureBorder, path });
@@ -1676,7 +1701,8 @@ export function HostPage({
return;
}
sourceSwitchRef.current = null;
- setSwitchingSource(false);
+ setSourceSwitch(null);
+ releaseUnusedNativeClient();
}
function commitQuality(settings: QualitySettings): void {
@@ -2162,7 +2188,7 @@ export function HostPage({
isCurrentGeneration(generation) && signalRef.current === signal &&
signal.send({ type: "route-transport-connected", revision, connectionId }),
createPeer:
- nativeClient && nativeShareGeneration
+ nativeClient && nativeShareGeneration && (nativeModeRef.current || nativeMediaIngressRef.current)
? (candidate, input, events) => {
const current = peersRef.current.get(candidate.childPeerId);
if (
@@ -2208,7 +2234,7 @@ export function HostPage({
profile: qualitySettingsRef.current,
videoCodec: videoCodecRef.current,
natPredictionEnabled: routePolicyRef.current.natPrediction,
- videoPool: nativeClient && nativeShareGeneration ? undefined : browserVideoPool(),
+ videoPool: nativeClient && nativeShareGeneration && (nativeModeRef.current || nativeMediaIngressRef.current) ? undefined : browserVideoPool(),
});
}
@@ -2253,7 +2279,8 @@ export function HostPage({
const activeStream = streamRef.current;
const nativeClient = nativeConnection.current;
const nativeShareGeneration = nativeShareGenerationRef.current;
- const useNative = nativeClient !== null && nativeShareGeneration !== null;
+ const useNative = nativeClient !== null && nativeShareGeneration !== null &&
+ (nativeModeRef.current || nativeMediaIngressRef.current !== null);
const iceConfig = iceConfigRef.current;
const signal = signalRef.current;
if ((!activeStream && !useNative) || !iceConfig || !signal) {
@@ -2877,13 +2904,7 @@ export function HostPage({
}
if (captured) {
if (selection.kind === "browser") {
- hostAudioRef.current = new HostAudio(captured, selection.source ?? "browser", available => {
- if (!isCurrentShare(generation, shareGeneration)) return;
- audioAvailableRef.current = available;
- signalRef.current?.setAudioAvailable(available);
- const source = hostAudioRef.current?.sourceStream;
- if (source) setDetails(captureDetails(source, false, hostAudioRef.current?.sourceAudioAvailable));
- }, enabled => { if (isCurrentShare(generation, shareGeneration)) setCommentary(enabled); });
+ hostAudioRef.current = createBrowserAudio(captured, selection.source ?? "browser", generation, shareGeneration);
const audio = hostAudioRef.current;
try {
captured = await audio.start();
@@ -3011,12 +3032,12 @@ export function HostPage({
return;
}
const previous = nativeSourceSelectionRef.current;
- const token = { audioOnly: target.kind !== "picker" && !!previous &&
+ const token = { nativePending: true, audioOnly: target.kind !== "picker" && !!previous &&
nativeCaptureTargetKey(previous.target) === nativeCaptureTargetKey(target) &&
previous.showCaptureBorder === showCaptureBorder };
sourceSwitchRef.current = token;
closeCaptureSourcePicker();
- setSwitchingSource(true);
+ setSourceSwitch(token);
setNoticeValue(null);
try {
await nativePreviewTailRef.current;
@@ -3043,6 +3064,8 @@ export function HostPage({
) {
return;
}
+ token.nativePending = false;
+ setSourceSwitch({ ...token });
nativeSourceSelectionRef.current = { kind: "native", client, target, audio, path, showCaptureBorder, excludeAudio };
const activeStream = streamRef.current;
if (activeStream) {
@@ -3079,6 +3102,57 @@ export function HostPage({
}
}
+ function createBrowserAudio(captured: MediaStream, source: BrowserCaptureSource, generation: number, shareGeneration: string): HostAudio {
+ const audio = new HostAudio(captured, source, available => {
+ if (!isCurrentShare(generation, shareGeneration) || hostAudioRef.current !== audio) return;
+ audioAvailableRef.current = available;
+ signalRef.current?.setAudioAvailable(available);
+ setDetails(captureDetails(audio.sourceStream, false, audio.sourceAudioAvailable));
+ }, enabled => {
+ if (isCurrentShare(generation, shareGeneration) && hostAudioRef.current === audio) setCommentary(enabled);
+ });
+ audio.setVolume(shareVolume.muted ? 0 : shareVolume.level);
+ return audio;
+ }
+
+ async function switchToNativeSource(selection: Extract<ShareSourceSelection, { kind: "native" }>): Promise<void> {
+ const generation = activeGenerationRef.current;
+ const shareGeneration = shareGenerationRef.current;
+ if (phase !== "live" || generation === null || !shareGeneration || !isCurrentGeneration(generation) ||
+ sourceSwitchRef.current || qualityChangeRef.current || nativeConnection.current !== selection.client) return;
+ const token = { nativePending: true };
+ sourceSwitchRef.current = token;
+ closeCaptureSourcePicker();
+ setSourceSwitch(token);
+ setNoticeValue(null);
+ try {
+ // The App has one source owner. Keep the Browser picture and fanout live
+ // while its optional ingress gives way to the new capture request.
+ const ingress = nativeMediaIngressRef.current;
+ if (ingress) recoverBrowserFanout(ingress);
+ const captured = await startNativeShare(generation, shareGeneration, selection, token);
+ if (!captured) return;
+ if (!isCurrentShare(generation, shareGeneration) || sourceSwitchRef.current !== token) {
+ if (nativeMediaBridgeRef.current?.stream === captured) disposeNativeShare();
+ return;
+ }
+ const previousAudio = hostAudioRef.current;
+ hostAudioRef.current = null;
+ previousAudio?.dispose();
+ setCommentary(false);
+ discardPreparedHostChild();
+ signalRef.current?.setAudioAvailable(audioAvailableRef.current);
+ setResolvedVideoCodec(videoCodecRef.current.primary);
+ await replaceBrowserStream(captured, generation, token, [], true);
+ } catch (error) {
+ if (isCurrentShare(generation, shareGeneration) && sourceSwitchRef.current === token) {
+ setNoticeError(error, "source");
+ }
+ } finally {
+ finishSourceSwitch(token);
+ }
+ }
+
async function changeCommentary(enabled: boolean, input = commentaryInput): Promise<void> {
const generation = activeGenerationRef.current;
if (generation === null || commentaryOperationRef.current) return;
@@ -3132,15 +3206,13 @@ export function HostPage({
await openCaptureSourcePicker();
return;
}
- if (nativeModeRef.current) return;
-
// Some browsers retire the old camera while opening the new one. That
// transition belongs to this replacement, not the share-ended observer.
const token = { replacingVideo: source === "camera" && hostAudioRef.current?.sourceKind === "camera"
? streamRef.current?.getVideoTracks()[0] : undefined };
sourceSwitchRef.current = token;
closeCaptureSourcePicker();
- setSwitchingSource(true);
+ setSourceSwitch(token);
setNoticeValue(null);
let captured: MediaStream;
@@ -3172,8 +3244,37 @@ export function HostPage({
try {
if (source === "camera") setCameraDevice(deviceId);
- captured = hostAudioRef.current?.attach(captured, source) ?? captured;
- await replaceBrowserStream(captured, generation, token);
+ const reboundPeerIds: string[] = [];
+ const wasNative = nativeModeRef.current;
+ if (wasNative) {
+ const shareGeneration = shareGenerationRef.current!;
+ const audio = createBrowserAudio(captured, source, generation, shareGeneration);
+ try {
+ captured = await audio.start();
+ if (!isCurrentShare(generation, shareGeneration) || sourceSwitchRef.current !== token) {
+ captured.getTracks().forEach(track => track.stop());
+ audio.dispose();
+ return;
+ }
+ } catch (error) {
+ audio.dispose();
+ throw error;
+ }
+ // Permission and audio setup succeeded; only now retire the old source.
+ token.replacingVideo = streamRef.current?.getVideoTracks()[0];
+ reboundPeerIds.push(...retireNativeSenderPeers());
+ disposeNativeShare();
+ hostAudioRef.current = audio;
+ audioAvailableRef.current = audio.audioAvailable;
+ signalRef.current?.setAudioAvailable(audio.audioAvailable);
+ setCommentary(false);
+ } else {
+ captured = hostAudioRef.current?.attach(captured, source) ?? captured;
+ }
+ await replaceBrowserStream(captured, generation, token, reboundPeerIds, wasNative);
+ if (wasNative && isCurrentGeneration(generation) && sourceSwitchRef.current === token) {
+ await startBrowserNativeIngress(generation, shareGenerationRef.current!, captured);
+ }
} catch (error) {
if (captured !== streamRef.current) captured.getVideoTracks().forEach(track => track.stop());
if (isCurrentGeneration(generation)) setNoticeError(error, "source");
@@ -3194,7 +3295,7 @@ export function HostPage({
failureCode: hostFailureCode(error, action, "camera") });
}
- async function replaceBrowserStream(captured: MediaStream, generation: number, token: object): Promise<void> {
+ async function replaceBrowserStream(captured: MediaStream, generation: number, token: object, reboundPeerIds: string[] = [], rebuildTransport = false): Promise<void> {
// The caller retires its operation; this function owns only stream resources.
const previousStream = streamRef.current;
if (!previousStream) {
@@ -3216,12 +3317,19 @@ export function HostPage({
setMediaPaused(captured, sharingPausedRef.current);
streamRef.current = captured;
setStream(captured);
- setDetails(captureDetails(hostAudioRef.current?.sourceStream ?? captured));
+ setDetails(captureDetails(hostAudioRef.current?.sourceStream ?? captured, nativeModeRef.current, nativeSourceAudioRef.current));
if (videoChanged) watchCaptureEnd(captured, generation);
try {
+ if (rebuildTransport) {
+ const useNative = nativeModeRef.current || nativeMediaIngressRef.current !== null;
+ for (const [peerId, peer] of peersRef.current) {
+ if ((peer instanceof NativeSenderPeer) === useNative) continue;
+ removePeer(peerId);
+ reboundPeerIds.push(peerId);
+ }
+ }
const ingress = nativeMediaIngressRef.current;
- const reboundPeerIds: string[] = [];
if (ingress) {
try {
if (ingress.hasAudio !== (captured.getAudioTracks().length > 0)) {
@@ -3277,7 +3385,7 @@ export function HostPage({
}),
),
provisional?.replaceStream(captured) ?? Promise.resolve(true),
- activeSfuRoute?.replaceStream(captured) ?? Promise.resolve(true),
+ activeSfuRoute?.replaceStream(captured, rebuildTransport) ?? Promise.resolve(true),
]);
if (
@@ -3709,12 +3817,23 @@ export function HostPage({
? nativeConnection.current?.health.nativeMedia.maxAudioBitrate ?? MAX_AUDIO_SEND_BITRATE
: MAX_AUDIO_SEND_BITRATE);
const videoLimits = videoCaptureLimits(nativeActive ? nativeConnection.current?.health.nativeMedia : undefined);
+ const nativePermissionIntent = androidAppIntent(nativeConnection.current?.health.activationURL);
+ const confirmNativeCapture = Boolean(nativePermissionIntent && (
+ (phase === "starting" && !nativeActive && nativeSourceSelectionRef.current) ||
+ sourceSwitchState?.nativePending
+ ));
+ function confirmNativePermission(): void {
+ const intent = androidAppIntent(nativeConnection.current?.health.activationURL);
+ if (intent && activeGenerationRef.current !== null) window.location.assign(intent);
+ }
return (
<div className="lr-app">
<AppHeader
atHome
- showAppConnection={!sourcePickerOpen}
+ onAppConnected={() => {
+ if (nativeSourceRequestRef.current) void openCaptureSourcePicker();
+ }}
led={
<LedStrip
state={hostStatus.connection.tone}
@@ -3756,18 +3875,21 @@ export function HostPage({
? !!nativeConnection.current?.health.nativeMedia.shareVolume : hostAudioRef.current?.commentaryAvailable === true,
onChange: changeShareVolume }}
audioAvailable={audioAvailableRef.current}
- canPlay={phase === "live" && !nativeSources && !sharingPaused && !localPreviewPaused}
+ canPlay={phase === "live" && !nativeSources && !sharingPaused && !previewPlayback.suspended}
theaterMode={theaterMode} onToggleTheater={() => setTheaterMode(value => !value)}
- onPlay={() => { previewPausedByUser.current = false; void videoRef.current?.play().catch(() => undefined); }}
- onPause={() => { previewPausedByUser.current = true; }}
+ onPlay={previewPlayback.onPlay}
+ onPause={previewPlayback.onPause}
overlayAction={<RoomChatToggle session={interactionSession} />}
overlaySettings={interactionSession && <RoomChatOverlaySettings session={interactionSession} />}
settings={{ pictureFit, pictureRotation,
+ preview: { autoPause: previewAutoPause, onChange: autoPause => {
+ setPreviewAutoPause(autoPause); saveRoomPreferences({ previewAutoPause: autoPause });
+ } },
setPictureFit: fit => { setPictureFit(fit); saveRoomPreferences({ pictureFit: fit }); },
setPictureRotation: rotation => { setPictureRotation(rotation); saveRoomPreferences({ pictureRotation: rotation }); },
}} />}
<RoomChatOverlay session={room ? interactionSession : null}
- visible={phase === "live" && !nativeSources && !sharingPaused && !(switchingSource && !sourceSwitchRef.current?.audioOnly) && !localPreviewPaused} />
+ visible={phase === "live" && !nativeSources && !sharingPaused && !(switchingSource && !sourceSwitchRef.current?.audioOnly) && !previewPlayback.suspended} />
{nativeSources ? (
<CaptureSourcePicker
nativeSources={nativeSources}
@@ -3779,11 +3901,8 @@ export function HostPage({
onPreview={loadNativeSourcePreview}
onRefresh={openCaptureSourcePicker}
onCancel={closeCaptureSourcePicker}
- onAppConnected={phase !== "live" ? () => {
- if (nativeSourceRequestRef.current) void openCaptureSourcePicker();
- } : undefined}
- browserAvailable={!nativeActive && !!navigator.mediaDevices?.getDisplayMedia}
- cameraAvailable={!nativeActive && !!navigator.mediaDevices?.getUserMedia}
+ browserSupported={!!navigator.mediaDevices?.getDisplayMedia}
+ cameraSupported={!!navigator.mediaDevices?.getUserMedia}
selectionDisabled={roomMutating || switchingSource || changingQuality}
initialAudio={
nativeActive
@@ -3865,21 +3984,24 @@ export function HostPage({
painted after it would cover and swallow its controls. */}
{nativeSources ? null : switchingSource && !sourceSwitchRef.current?.audioOnly ? (
<StageOverlay
- icon="refresh"
+ icon={confirmNativeCapture ? "window" : "refresh"}
comic="source-switching"
tone="busy"
waiting
dim
- message={t("host.switchingSource")}
+ message={t(confirmNativeCapture ? "native.confirmInApp" : "host.switchingSource")}
+ onActivate={confirmNativeCapture ? confirmNativePermission : undefined}
/>
) : sharingPaused ? (
<StageOverlay icon="pause" dim comic="host-paused" tone={hostStatus.activity.tone} message={t("host.pauseNotice")} />
) : phase === "starting" ? (
<>
<StaticNoise />
- <StageOverlay icon="cast" comic="source-starting" tone="busy" waiting message={t("host.starting")} />
+ <StageOverlay icon={confirmNativeCapture ? "window" : "cast"} comic="source-starting" tone="busy" waiting
+ message={t(confirmNativeCapture ? "native.confirmInApp" : "host.starting")}
+ onActivate={confirmNativeCapture ? confirmNativePermission : undefined} />
</>
- ) : stream && localPreviewPaused ? (
+ ) : stream && previewPlayback.suspended ? (
<StageOverlay
icon="eyeOff"
comic="preview-paused"
@@ -4025,6 +4147,9 @@ export function HostPage({
</div>
</>}
audio={<>
+ {nativePermissionIntent && nativeActive && ((commentaryBusy && !commentary) || (sourceSwitchState?.nativePending && sourceSwitchState.audioOnly)) &&
+ <Btn icon="window" title="native.confirmInApp" cap="native.confirmInApp" tone="primary"
+ hint="hint-client-site" onClick={confirmNativePermission} />}
{phase === "live" && (!nativeActive || nativeConnection.current?.health.nativeMedia.microphone) ?
<ShareMicrophoneSettings enabled={commentary}
browser={!nativeActive}
@@ -4035,8 +4160,7 @@ export function HostPage({
volume={commentaryInput.volume} onVolume={changeCommentaryVolume}
deviceId={commentaryInput.deviceId}
onDevice={deviceId => { void changeCommentary(true, { ...commentaryInput, deviceId }); }}
- loadDevices={() => nativeActive && nativeConnection.current
- ? nativeConnection.current.microphones() : browserCaptureDevices("audioinput")}
+ loadDevices={loadMicrophoneDevices}
voiceProcessing={nativeActive ? undefined : commentaryInput.voiceProcessing}
onVoiceProcessing={voiceProcessing => { void changeCommentary(true, { ...commentaryInput, voiceProcessing }); }} /> : null}
{phase === "live" && nativeActive && nativeSourceSelectionRef.current?.target.kind === "display" &&
diff --git a/src/client/pages/PlaybackPreviewPage.tsx b/src/client/pages/PlaybackPreviewPage.tsx
index 92ca377e..f69dae5e 100644
--- a/src/client/pages/PlaybackPreviewPage.tsx
+++ b/src/client/pages/PlaybackPreviewPage.tsx
@@ -7,11 +7,16 @@ import { RoomChatOverlay, RoomChatOverlaySettings, RoomChatToggle } from "../com
import { StageOverlay, StageTv } from "../components/living/Stage";
import { StatusIndicator } from "../components/living/StatusIndicator";
import { useTheaterMode } from "../components/living/use-theater-mode";
+import { useHostPreviewPlayback } from "../components/living/use-host-preview-playback";
+import { readRoomPreferences, saveRoomPreferences } from "../lib/room-preferences";
import { STATUS_CATALOG } from "../ui/media-status";
import { useCopy } from "../ui/copy";
import { usePreviewInteractions } from "./use-preview-interactions";
import { PreviewInput } from "./preview-input";
import { HostAudio } from "../media/host-audio";
+import { relayVolunteerLimit } from "../media/relay-capability";
+import { DEFAULT_ENDPOINT_MEDIA_COPY_CAPACITY } from "../../shared/media-copy-accounting";
+import { MAX_VIEWERS_PER_ROOM_LIMIT } from "../../shared/protocol";
type PreviewRole = "host" | "viewer";
@@ -117,7 +122,11 @@ function PlaybackSample({ role }: { role: PreviewRole }) {
const [theaterMode, setTheaterMode] = useTheaterMode();
const [pictureFit, setPictureFit] = useState<VideoFit>("contain");
const [pictureRotation, setPictureRotation] = useState<VideoRotation>(0);
+ const [previewAutoPause, setPreviewAutoPause] = useState(() => readRoomPreferences().previewAutoPause);
const [viewerRoute, setViewerRoute] = useState<"auto" | "p2p" | "sfu">("auto");
+ const [relayVolunteer, setRelayVolunteer] = useState(false);
+ const [relayCapacity, setRelayCapacity] = useState(DEFAULT_ENDPOINT_MEDIA_COPY_CAPACITY + 1);
+ const relayLimits = relayVolunteerLimit(relayCapacity, DEFAULT_ENDPOINT_MEDIA_COPY_CAPACITY, MAX_VIEWERS_PER_ROOM_LIMIT);
const [error, setError] = useState("");
const [waiting, setWaiting] = useState(true);
const [audioAvailable, setAudioAvailable] = useState(false);
@@ -154,6 +163,7 @@ function PlaybackSample({ role }: { role: PreviewRole }) {
video.srcObject = stream;
if (stream) play();
}, [stream]);
+ const previewPlayback = useHostPreviewPlayback(videoRef, viewer ? null : stream, previewAutoPause);
useEffect(() => () => { sourceAudio.current?.dispose(); mediaRef.current?.stop(); }, []);
useEffect(() => {
const input = new PreviewInput();
@@ -202,16 +212,24 @@ function PlaybackSample({ role }: { role: PreviewRole }) {
indicator={<StatusIndicator status={waiting ? STATUS_CATALOG.joining : stream ? STATUS_CATALOG.connected : STATUS_CATALOG.idle} />}>
<video ref={videoRef} playsInline muted={!viewer} tabIndex={0} inert={!stream} style={{ objectFit: pictureFit }}
aria-label={english ? "Local test video" : "本机测试视频"} />
- {waiting && <StageOverlay dim waiting icon={STATUS_CATALOG.joining.icon} message={t(STATUS_CATALOG.joining.labelKey)} comic={STATUS_CATALOG.joining.comic} />}
- <RoomChatOverlay session={session} visible={Boolean(stream) && !waiting} />
+ {waiting ? <StageOverlay dim waiting icon={STATUS_CATALOG.joining.icon} message={t(STATUS_CATALOG.joining.labelKey)} comic={STATUS_CATALOG.joining.comic} />
+ : previewPlayback.suspended && <StageOverlay dim icon="pause" message={t("host.localPreviewPaused")} comic="preview-paused" />}
+ <RoomChatOverlay session={session} visible={Boolean(stream) && !waiting && !previewPlayback.suspended} />
<PlaybackControls videoRef={videoRef} stream={stream} audioAvailable={audioAvailable}
sharingAudio={viewer ? undefined : { ...shareVolume, available: true, onChange: (level, muted) => {
sourceAudio.current?.setVolume(muted ? 0 : level); setShareVolume({ level, muted });
} }}
- settings={{ pictureFit, setPictureFit, pictureRotation, setPictureRotation, route: viewer ? { preference: viewerRoute, onChange: setViewerRoute,
- sfuAvailable: true, sfuOnly: false, peerOnly: false, disabled: false, rejected: false } : undefined }}
+ settings={{ pictureFit, setPictureFit, pictureRotation, setPictureRotation,
+ preview: viewer ? undefined : { autoPause: previewAutoPause, onChange: autoPause => {
+ setPreviewAutoPause(autoPause); saveRoomPreferences({ previewAutoPause: autoPause });
+ } },
+ route: viewer ? { preference: viewerRoute, onChange: setViewerRoute,
+ sfuAvailable: true, sfuOnly: false, peerOnly: false, disabled: false, rejected: false } : undefined,
+ relayVolunteer: viewer ? { ...relayLimits, enabled: relayVolunteer, pending: false, disabled: false,
+ onChange: setRelayVolunteer, onCapacityChange: setRelayCapacity } : undefined }}
audioTrackKey={stream?.getAudioTracks().map((track) => track.id).join(",")}
- canPlay={Boolean(stream) && !waiting} theaterMode={theaterMode} onPlay={play}
+ canPlay={Boolean(stream) && !waiting && !previewPlayback.suspended} theaterMode={theaterMode}
+ onPlay={viewer ? play : previewPlayback.onPlay} onPause={viewer ? undefined : previewPlayback.onPause}
onToggleTheater={() => setTheaterMode((value) => !value)}
overlayAction={<RoomChatToggle session={session} />}
overlaySettings={session && <RoomChatOverlaySettings session={session} />}
diff --git a/src/client/pages/RemoteControlsPreview.tsx b/src/client/pages/RemoteControlsPreview.tsx
index b2a63ce9..27774f97 100644
--- a/src/client/pages/RemoteControlsPreview.tsx
+++ b/src/client/pages/RemoteControlsPreview.tsx
@@ -2,15 +2,21 @@ import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "
import { HostRemoteControl } from "../components/living/HostRemoteControl";
import { RemoteControl } from "../components/living/RemoteControl";
import { PlaybackControls } from "../components/living/PlaybackControls";
+import { RemoteTouchSettings } from "../components/living/PlaybackSettings";
import { StageTv } from "../components/living/Stage";
import { StatusIndicator } from "../components/living/StatusIndicator";
import { useTheaterMode } from "../components/living/use-theater-mode";
import { RemoteKeyboard } from "../components/living/RemoteKeyboard";
+import { RemoteMouse, placeRemotePointer, remotePointerRoom } from "../components/living/RemoteMouse";
import { Couch } from "../components/living/Couch";
import { STATUS_CATALOG } from "../ui/media-status";
import { Btn } from "../components/living/primitives";
import type { RemoteControlRequest } from "../native/wire";
import type { RemoteInputEvent } from "../remote-control/input-session";
+import { picturePoint, pictureRect, rotatePicturePoint, videoRotation } from "../media/video-geometry";
+import { RemoteTouchpad } from "../remote-control/touchpad";
+import { RemoteViewport } from "../remote-control/viewport";
+import type { RoomPreferences } from "../lib/room-preferences";
import type { RemoteControlMode, RemoteInputPlatform } from "../remote-control/protocol";
import { useCopy } from "../ui/copy";
import { testMedia } from "./PlaybackPreviewPage";
@@ -31,6 +37,11 @@ export function RemoteControlsPreview() {
const [requests, setRequests] = useState<(RemoteControlRequest & { name: string })[]>([]);
const [keyboardOpen, setKeyboardOpen] = useState(false);
const [lastInput, setLastInput] = useState("");
+ const mouseVideo = useRef<HTMLVideoElement>(null);
+ const mouseLayer = useRef<HTMLDivElement>(null);
+ const mouseSurface = useRef<HTMLDivElement>(null);
+ const [pointer, setPointer] = useState<RemoteTouchpad | null>(null);
+ const [touchMode, setTouchMode] = useState<RoomPreferences["remoteTouchMode"]>("direct");
const [releaseKey, setReleaseKey] = useState(10);
const [stopKey, setStopKey] = useState(10);
const shortcut = useCallback(async (key?: number) => {
@@ -38,6 +49,49 @@ export function RemoteControlsPreview() {
return { key: key ?? stopKey, available: true };
}, [stopKey]);
const input = useCallback((event: RemoteInputEvent) => setLastInput(JSON.stringify(event)), []);
+ const placePointer = useCallback((position: { x: number; y: number }) => {
+ const layer = mouseLayer.current, video = mouseVideo.current;
+ if (!layer || !video) return;
+ const picture = pictureRect(video);
+ if (!picture) return;
+ const rect = layer.getBoundingClientRect();
+ const point = rotatePicturePoint(position, videoRotation(video));
+ placeRemotePointer(layer, picture.left - rect.left + point.x * picture.width, picture.top - rect.top + point.y * picture.height);
+ }, []);
+ useEffect(() => {
+ const element = mouseVideo.current!;
+ const surface = mouseSurface.current!;
+ const view = new RemoteViewport(element, surface, () => placePointer(current.position));
+ const current = new RemoteTouchpad(() => {
+ const rect = pictureRect(element);
+ return rect ? { ...rect, rotation: videoRotation(element) } : null;
+ }, input, deltaY => setLastInput(JSON.stringify({ type: "wheel", deltaY })), position => {
+ view.follow(position, remotePointerRoom(mouseLayer.current));
+ placePointer(position);
+ }, view);
+ setPointer(current);
+ const sample = testMedia(false, false);
+ element.srcObject = sample.stream;
+ void element.play().catch(() => {});
+ const wheel = (event: WheelEvent) => {
+ event.preventDefault();
+ current.wheel(event.deltaY);
+ };
+ surface.addEventListener("wheel", wheel, { passive: false });
+ const position = () => { view.refresh(); current.refresh(); };
+ const resize = new ResizeObserver(position);
+ resize.observe(surface);
+ element.addEventListener("resize", position);
+ position();
+ return () => {
+ resize.disconnect();
+ element.removeEventListener("resize", position);
+ surface.removeEventListener("wheel", wheel);
+ current.reset(); view.reset(); setPointer(null);
+ element.srcObject = null;
+ sample.stop();
+ };
+ }, [input, placePointer]);
const closeKeyboard = () => {
setKeyboardOpen(false);
document.getElementById("preview-remote-keyboard-toggle")?.focus();
@@ -45,16 +99,52 @@ export function RemoteControlsPreview() {
return <><PermissionRequestPreview /><section id="remote-control-preview" className="cp-section">
<header className="cp-section-head"><div>
- <h2>{t("remote.title")}</h2>
- <p>{en ? "Actual permission panel and keyboard, with local sample state. Key presses only appear below; they are not sent to an App or operating system."
- : "正式的授权浮窗与虚拟键盘,使用本地示例状态。按键只在下方显示,不发送到 App 或操作系统。"}</p>
+ <h2>{t("remote.inputTools")}</h2>
+ <p>{en ? "Expand the mini mouse and drag its lower half to move the pointer. Hold and move L or R to drag, or hold a button with one finger and move the lower half with another. Pinch or pan the picture with two fingers to adjust the local view. Tap the wheel to middle-click, swipe to scroll, or hold to drag. Try direct touch or the compact trackpad below. Input stays in the local log."
+ : "展开迷你鼠标,拖动下半部移动光标。按住 L 或 R 后移动即可拖拽,也可用一指按住按钮、另一指拖动下半部。在画面上双指缩放或平移可调整本地视图。滚轮可轻点中击、滑动滚动、长按后拖动。下方可切换直接触摸或光标触控板,输入仅记录在本地。"}</p>
</div></header>
+ <div className="cp-stage" style={{ maxWidth: 480 }}>
+ <StageTv live label={en ? "Local mouse example" : "本地鼠标示例"}>
+ <video ref={mouseVideo} muted playsInline style={{ objectFit: "cover" }}
+ aria-label={en ? "Local sample picture" : "本地示例画面"} />
+ <div ref={mouseSurface} className="lr-remote-input-surface" role="application" aria-label={t("remote.surface")} tabIndex={0}
+ onPointerDown={event => {
+ if (event.button !== 0) return;
+ event.preventDefault(); event.currentTarget.setPointerCapture(event.pointerId);
+ pointer?.down(event.pointerId, event.clientX, event.clientY,
+ mouseVideo.current && picturePoint(mouseVideo.current, event.clientX, event.clientY));
+ }}
+ onPointerMove={event => pointer?.move(event.pointerId, event.clientX, event.clientY)}
+ onPointerUp={event => pointer?.up(event.pointerId)}
+ onPointerCancel={event => pointer?.cancel(event.pointerId)}
+ onLostPointerCapture={event => pointer?.cancel(event.pointerId)}
+ onContextMenu={event => event.preventDefault()}>
+ {pointer && <RemoteMouse pointer={pointer} positionRef={mouseLayer} touchMode={touchMode}
+ pointAt={(x, y) => mouseVideo.current && picturePoint(mouseVideo.current, x, y)} />}
+ </div>
+ </StageTv>
+ </div>
+ <div className="lr-viewer-settings is-single" style={{ maxWidth: 480 }}>
+ <RemoteTouchSettings mode={touchMode} onChange={setTouchMode} />
+ </div>
<div className="cp-tools">
<label className="lr-input"><span>{en ? "Host platform" : "房主平台"}</span>
<select value={platform} onChange={event => setPlatform(event.target.value as RemoteInputPlatform)}>
<option value="windows">Windows</option><option value="darwin">macOS</option><option value="linux">Linux</option>
</select>
</label>
+ <Btn id="preview-remote-keyboard-toggle" icon="keyboard" title="remote.keyboard" cap="remote.keyboard" hint="hint-remote-keyboard"
+ expanded={keyboardOpen} controls="preview-remote-keyboard" onClick={() => setKeyboardOpen(value => !value)} />
+ </div>
+ <div id="preview-remote-keyboard" className="cp-keyboard" hidden={!keyboardOpen}>
+ {keyboardOpen && <RemoteKeyboard key={platform} platform={platform} touch input={input} close={closeKeyboard}
+ allowText={requests.filter(request => request.state !== "pending").length < 2}
+ releaseKey={releaseKey} onReleaseKeyChange={setReleaseKey} />}
+ </div>
+ <p className="cp-input-output"><span>{en ? "Latest input" : "最近一次输入"}</span>
+ <output>{lastInput || (en ? "Touch the picture directly, or tap the mini mouse for pointer controls." : "直接触摸画面,或轻点迷你鼠标展开光标控制。")}</output>
+ </p>
+ <div className="cp-tools">
<HostRemoteControl mode={mode} platform={platform} requests={requests}
gamepad={platform !== "darwin"}
simultaneous={simultaneous}
@@ -77,8 +167,6 @@ export function RemoteControlsPreview() {
{ id: "preview-gamepad", peerId: "preview-gamepad", state: "pending", name: "Player 3" },
]);
}}>{en ? "Simulate multiple controllers" : "模拟多人控制"}</button>
- <Btn id="preview-remote-keyboard-toggle" icon="keyboard" title="remote.keyboard" cap="remote.keyboard" hint="hint-remote-keyboard"
- expanded={keyboardOpen} controls="preview-remote-keyboard" onClick={() => setKeyboardOpen(value => !value)} />
</div>
<Couch view="host" host={{ key: "preview-host", name: "Piik", you: true }}
entries={participants.map(person => {
@@ -86,14 +174,6 @@ export function RemoteControlsPreview() {
return { key: person.peerId, name: person.name, status: STATUS_CATALOG.connected, selectable: false,
collaboration: Boolean(grant) };
})} />
- <div id="preview-remote-keyboard" className="cp-keyboard" hidden={!keyboardOpen}>
- {keyboardOpen && <RemoteKeyboard key={platform} platform={platform} touch={false} input={input} close={closeKeyboard}
- allowText={requests.filter(request => request.state !== "pending").length < 2}
- releaseKey={releaseKey} onReleaseKeyChange={setReleaseKey} />}
- </div>
- <p className="cp-input-output"><span>{en ? "Latest input" : "最近一次输入"}</span>
- <output>{lastInput || (en ? "Open the keyboard to try it." : "打开键盘即可试用。")}</output>
- </p>
</section></>;
}
@@ -143,6 +223,8 @@ function PermissionRequestPreview() {
</div>
<div className="cp-tools"><button type="button" className="lr-btn is-compact" onClick={() => channel.policy("ask")}>
{en ? "Reset example" : "重置示例"}</button>
+ <button type="button" className="lr-btn is-compact" disabled={state.state !== "active"} onClick={() => channel.failInput()}>
+ {en ? "Simulate an input failure" : "模拟单次操作失败"}</button>
<span className="cp-permission-caption">{en ? "Hover the picture to reveal its normal playback controls." : "鼠标移入画面,即可呼出正式播控。"}</span>
</div>
</section>;
diff --git a/src/client/pages/RoomVoicePreview.tsx b/src/client/pages/RoomVoicePreview.tsx
index 752308ba..2f604b1f 100644
--- a/src/client/pages/RoomVoicePreview.tsx
+++ b/src/client/pages/RoomVoicePreview.tsx
@@ -3,8 +3,9 @@ import { RoomVoiceControls, RoomVoicePanel, RoomVoiceSettings, type RoomVoiceVie
import type { RoomVoiceSnapshot } from "../room-voice/session";
import type { RoomVoiceCommand } from "../../shared/protocol";
-const names = new Map([["preview-host", "Piik"], ["preview-friend", "Piik friend"]]);
-function initialState(viewer: boolean): RoomVoiceSnapshot {
+const names = new Map([["preview-host", "Piik"], ["preview-friend", "Piik friend"],
+ ...Array.from({ length: 19 }, (_, index): [string, string] => [`preview-friend-${index + 1}`, `Friend ${index + 2}`])]);
+function initialState(viewer: boolean, crowded: boolean): RoomVoiceSnapshot {
return { peerId: viewer ? "preview-friend" : "preview-host", role: viewer ? "viewer" : "host", connected: true,
microphone: false, capturePending: false, deafened: false, listeningVolume: 1, peerVolumes: new Map(), autoplayBlocked: false, error: null, muted: new Set(),
input: { deviceId: "", volume: 1, voiceProcessing: true },
@@ -12,16 +13,19 @@ function initialState(viewer: boolean): RoomVoiceSnapshot {
members: [
{ peerId: "preview-host", connected: true, joined: true,
publicationId: viewer ? "preview-host-publication" : undefined, upstream: { kind: "none" } },
- { peerId: "preview-friend", connected: true, joined: true,
- publicationId: viewer ? undefined : "preview-friend-publication", upstream: { kind: "peer", peerId: "preview-host" } },
+ ...Array.from({ length: crowded ? 20 : 1 }, (_, index) => ({
+ peerId: index ? `preview-friend-${index}` : "preview-friend", connected: true, joined: true,
+ publicationId: viewer && index === 0 ? undefined : `preview-friend-publication-${index}`,
+ upstream: { kind: "peer" as const, peerId: "preview-host" },
+ })),
] },
};
}
// The catalogue stages the real view without opening a microphone or transport.
-export function useRoomVoicePreview(viewer: boolean) {
- const [state, setState] = useState(() => initialState(viewer));
- useEffect(() => setState(initialState(viewer)), [viewer]);
+export function useRoomVoicePreview(viewer: boolean, crowded = false) {
+ const [state, setState] = useState(() => initialState(viewer, crowded));
+ useEffect(() => setState(initialState(viewer, crowded)), [viewer, crowded]);
const session = useMemo<RoomVoiceView>(() => {
const snapshot: RoomVoiceSnapshot = { ...state, room: state.room && { ...state.room,
members: state.room.members.map(member => member.peerId === state.peerId
diff --git a/src/client/pages/StatusPreviewPage.tsx b/src/client/pages/StatusPreviewPage.tsx
index 09400255..8ad51015 100644
--- a/src/client/pages/StatusPreviewPage.tsx
+++ b/src/client/pages/StatusPreviewPage.tsx
@@ -22,6 +22,17 @@ import "./status-preview.css";
import { usePreviewAnchor } from "./use-preview-anchor";
import { usePreviewReducedMotion } from "./use-preview-reduced-motion";
+const SHARING_FAILURES = [
+ ["native.fail.captureStart", "app/start-share/rejected", "source-failed"],
+ ["native.fail.requestTimeout", "app/start-share/timeout", "source-failed"],
+ ["native.fail.controlDisconnected", "app/start-share/disconnected", "route-failed"],
+ ["native.fail.edge", "app/browser-media/connection", "route-failed"],
+ ["api.originNotAllowed", "site/http-403", "warning"],
+ ["host.capture.cancelled", "browser/NotAllowedError", "hint-capture-browser"],
+ ["host.camera.denied", "camera/NotAllowedError", "source-failed"],
+ ["host.roomInvalid", "status/unknown", "room-not-found"],
+] as const;
+
function StatusMark({ status }: { status: StatusDescriptor }) {
const { t, vis } = useCopy();
return (
@@ -40,6 +51,9 @@ export function StatusPreviewPage() {
usePreviewReducedMotion(reducedMotion);
const [couchView, setCouchView] = useState<"host" | "viewer">("viewer");
const [loadingLabel, setLoadingLabel] = useState<CopyKey | "">("client.launch.starting");
+ const [failureIndex, setFailureIndex] = useState(0);
+ const [failureKey, failureCode, failureHint] = SHARING_FAILURES[failureIndex]!;
+ const failureTone = failureCode.endsWith("NotAllowedError") ? "warn" : "bad";
const scenario = STATUS_SCENARIOS.find((item) => item.id === selected)!;
const presentation = deriveViewerPresentation(scenario.state);
const status = deriveViewerStatus(presentation, scenario.state.signal,
@@ -63,21 +77,18 @@ export function StatusPreviewPage() {
<AppHeader homeHref="/__status-preview" />
<main className="sp-main">
<section className="sp-catalog" id="sharing-troubleshooting">
- <header><h2>分享失败排查</h2><p>悬停看原因,点击状态图标打开排查;不会自动重试或更改设置。</p></header>
- <div className="sp-catalog-grid">
- {([
- ["native.fail.captureStart", "app/start-share/rejected", "source-failed"],
- ["native.fail.requestTimeout", "app/start-share/timeout", "source-failed"],
- ["native.fail.controlDisconnected", "app/start-share/disconnected", "route-failed"],
- ["native.fail.edge", "app/browser-media/connection", "route-failed"],
- ["api.originNotAllowed", "site/http-403", "warning"],
- ["host.capture.cancelled", "browser/NotAllowedError", "hint-capture-browser"],
- ["host.camera.denied", "camera/NotAllowedError", "source-failed"],
- ["host.roomInvalid", "status/unknown", "room-not-found"],
- ] as const).map(([key, code, tooltip]) => <article key={code}>
- <ShareFailureHelp status={{ icon: "alert", tone: code.endsWith("NotAllowedError") ? "warn" : "bad", tooltip, labelKey: key }} label={t(key)} code={code} checks={hostFailureChecks(code)} />
- <span>{t(key)}</span>
- </article>)}
+ <header><h2>分享失败排查</h2><p>出错时,电视下沿的状态灯会主动展开图示提示。点按状态灯查看排查方法。</p></header>
+ <label className="sp-failure-choice">错误示例
+ <select className="lr-input" value={failureIndex} onChange={event => setFailureIndex(Number(event.target.value))}>
+ {SHARING_FAILURES.map(([key, code], index) => <option key={code} value={index}>{t(key)}</option>)}
+ </select>
+ </label>
+ <div className="sp-failure-stage">
+ <StageTv label={t("host.stageAria")} indicator={<ShareFailureHelp key={failureCode}
+ status={{ icon: "alert", tone: failureTone, tooltip: failureHint, labelKey: failureKey }}
+ label={t(failureKey)} code={failureCode} checks={hostFailureChecks(failureCode)} />}>
+ <StageOverlay icon="alert" message={t(failureKey)} />
+ </StageTv>
</div>
</section>
<header className="sp-heading">
diff --git a/src/client/pages/TooltipPreviewPage.tsx b/src/client/pages/TooltipPreviewPage.tsx
index 7a96b92b..e396f74f 100644
--- a/src/client/pages/TooltipPreviewPage.tsx
+++ b/src/client/pages/TooltipPreviewPage.tsx
@@ -82,7 +82,7 @@ export function TooltipPreviewPage() {
<a href="#playback-preview">{en ? "Playback" : "播放栏"}</a><a href="#comic-preview">{en ? "Tooltips & comics" : "提示与漫画"}</a>
<a href="#launcher-preview">{en ? "App launcher" : "模式选择"}</a>
<a href="#interaction-preview">{en ? "Chat & reactions" : "聊天与互动"}</a>
- <a href="#remote-control-preview">{en ? "Remote control & keyboard" : "远程控制与键盘"}</a>
+ <a href="#remote-control-preview">{t("remote.inputTools")}</a>
<a href="#remote-permission-preview">{en ? "Input access requests" : "权限申请"}</a>
<a href="#brand-preview">{en ? "Brand motion" : "Mascot 动画"}</a>
diff --git a/src/client/pages/ViewerPage.tsx b/src/client/pages/ViewerPage.tsx
index 4e9cb7cd..9652f10c 100644
--- a/src/client/pages/ViewerPage.tsx
+++ b/src/client/pages/ViewerPage.tsx
@@ -79,7 +79,7 @@ import {
type CandidateQualityProbeResult,
} from "../media/candidate-quality-probe";
import type { QualitySettings } from "../media/quality";
-import { relayCapacityMessageForBrowser } from "../media/relay-capability";
+import { relayCapacityMessageForBrowser, relayVolunteerLimit } from "../media/relay-capability";
import {
invalidateSenderQualityEvidence,
senderQualityEvidenceFromSnapshot,
@@ -272,6 +272,7 @@ export function ViewerPage({
const voiceMetrics = useRoomVoiceMetrics(roomVoiceSession, showConnectionDetails || selectedPawn !== null, roomVoice);
const [pictureFit, setPictureFit] = useState<VideoFit>(() => readRoomPreferences().pictureFit);
const [pictureRotation, setPictureRotation] = useState(() => readRoomPreferences().pictureRotation);
+ const [remoteTouchMode, setRemoteTouchMode] = useState(() => readRoomPreferences().remoteTouchMode);
const [routeOptions, setRouteOptions] = useState<{ sfu: boolean; sfuOnly: boolean } | null>(null);
const [routePreference, setRoutePreference] = useState<ViewerRoutePreference>("auto");
const [routePreferencePending, setRoutePreferencePending] = useState(false);
@@ -279,7 +280,8 @@ export function ViewerPage({
const [relayVolunteerSupported, setRelayVolunteerSupported] = useState(false);
const [relayVolunteerEnabled, setRelayVolunteerEnabled] = useState(false);
const [relayVolunteerPending, setRelayVolunteerPending] = useState(false);
- const requestRelayVolunteerRef = useRef<((enabled: boolean) => void) | null>(null);
+ const [relayVolunteerLimits, setRelayVolunteerLimits] = useState(() => relayVolunteerLimit(null, MAX_ENDPOINT_MEDIA_CHILDREN, MAX_VIEWERS_PER_ROOM_LIMIT));
+ const requestRelayVolunteerRef = useRef<((enabled: boolean, capacity?: number) => void) | null>(null);
const [peerOnly, setPeerOnly] = useState(false);
useEffect(() => {
if (signalStatus !== "connected") {
@@ -469,7 +471,6 @@ export function ViewerPage({
useEffect(() => {
let active = true;
let currentIceConfig: IceConfig | null = null;
- let currentHostOnline = false;
let currentHostPaused = false;
let currentPeerId: string | null = null;
let currentRouteRevision = 0;
@@ -482,6 +483,8 @@ export function ViewerPage({
} | null = null;
let endpointMediaCopyCapacity = MAX_ENDPOINT_MEDIA_CHILDREN;
let relayBackendCapacity: number | null = null;
+ let ordinaryRelayCapacity = MAX_ENDPOINT_MEDIA_CHILDREN;
+ let requestedRelayCapacity: number | null = null;
let relayVolunteerAvailable = false;
let viewerAuthorizationGeneration: string | null = null;
let currentQualitySettings: QualitySettings = DEFAULT_QUALITY_SETTINGS;
@@ -617,21 +620,27 @@ export function ViewerPage({
setRelayVolunteerSupported(false);
setRelayVolunteerEnabled(false);
setRelayVolunteerPending(false);
- requestRelayVolunteerRef.current = enabled => {
- setRelayVolunteerPending(true);
+ function updateRelayVolunteer(enabled = signal.relayVolunteerRequest?.enabled ?? false, capacity?: number, send = true): void {
+ if (capacity !== undefined) requestedRelayCapacity = capacity;
// Keep consent in the signaling owner before asynchronous App discovery.
// Until its backend is known, an App gets only the legacy physical bound.
- const capacity = relayBackendCapacity ?? (clientActivationRef.current ? MAX_ENDPOINT_MEDIA_CHILDREN : MAX_VIEWERS_PER_ROOM_LIMIT);
- if (!signal.setRelayVolunteer(enabled, capacity)) setRelayVolunteerPending(false);
+ const backend = relayBackendCapacity ?? (clientActivationRef.current ? MAX_ENDPOINT_MEDIA_CHILDREN : MAX_VIEWERS_PER_ROOM_LIMIT);
+ const limits = relayVolunteerLimit(requestedRelayCapacity, ordinaryRelayCapacity, backend);
+ setRelayVolunteerLimits({ ...limits, available: relayBackendCapacity === null || limits.available });
+ if (!send || !relayVolunteerAvailable) return;
+ const effective = enabled ? limits.capacity : backend;
+ const intent = signal.relayVolunteerRequest;
+ if (intent?.enabled === enabled && intent.capacity === effective) return;
+ setRelayVolunteerPending(true);
+ if (!signal.setRelayVolunteer(enabled, effective)) setRelayVolunteerPending(false);
+ }
+ requestRelayVolunteerRef.current = (enabled, capacity) => {
+ updateRelayVolunteer(enabled, capacity);
void acquireNativeClient().then(client => {
if (!active || relayBackendCapacity !== null) return;
- const actualCapacity = client
+ relayBackendCapacity = client
? nativeRelayCapacity(MAX_VIEWERS_PER_ROOM_LIMIT, client.health) : MAX_VIEWERS_PER_ROOM_LIMIT;
- const intent = signal.relayVolunteerRequest;
- if (intent && intent.capacity !== actualCapacity) {
- setRelayVolunteerPending(true);
- signal.setRelayVolunteer(intent.enabled, actualCapacity);
- }
+ updateRelayVolunteer();
});
};
setAudioAvailable(null);
@@ -643,10 +652,7 @@ export function ViewerPage({
if (active) {
relayVolunteerAvailable = capabilities.relayVolunteer && !capabilities.sfuOnly;
setRelayVolunteerSupported(relayVolunteerAvailable);
- if (relayVolunteerAvailable && relayBackendCapacity !== null) {
- setRelayVolunteerPending(true);
- signal.setRelayVolunteer(signal.relayVolunteerRequest?.enabled ?? false, relayBackendCapacity);
- }
+ updateRelayVolunteer(undefined, undefined, relayBackendCapacity !== null);
}
if (active && capabilities.remoteInput) inputRoute.enable(identity => signal.send({ type: "subscribe-remote-input", ...identity }));
if (active && capabilities.viewerRouteSelection) {
@@ -770,10 +776,7 @@ export function ViewerPage({
? nativeRelayCapacity(MAX_VIEWERS_PER_ROOM_LIMIT, source.client.health) : MAX_VIEWERS_PER_ROOM_LIMIT;
if (relayBackendCapacity !== capacity) {
relayBackendCapacity = capacity;
- if (relayVolunteerAvailable) {
- setRelayVolunteerPending(true);
- signal.setRelayVolunteer(signal.relayVolunteerRequest?.enabled ?? false, capacity);
- }
+ updateRelayVolunteer();
}
const sourceKey = source
? `native:${source.sessionId}:${source.connectionId}:${source.generation}`
@@ -1478,15 +1481,8 @@ export function ViewerPage({
noteReceiveFailure(snapshot, currentRouteRevision);
activePeerMetrics = snapshot.metrics;
offerPeerQualityEvidence(snapshot, peer);
- if (
- !currentHostOnline &&
- (snapshot.connectionState === "failed" ||
- snapshot.connectionState === "closed")
- ) {
- clearPeerState();
- invalidatePresentedMedia();
- return;
- }
+ // Host signaling presence does not retire this edge's recovery
+ // or the relay's downstream connections.
setPeerSnapshot(snapshot);
dispatchPresentation({
type: "connection",
@@ -1555,6 +1551,7 @@ export function ViewerPage({
setViewerPasswordDraft("");
setViewerPasswordError(null);
relayChildEvidenceStore.clear();
+ ordinaryRelayCapacity = message.endpointMediaCopyCapacity;
endpointMediaCopyCapacity = currentPeerId === null ? message.endpointMediaCopyCapacity
: Math.max(endpointMediaCopyCapacity, message.endpointMediaCopyCapacity);
currentPeerId = message.peerId;
@@ -1587,8 +1584,9 @@ export function ViewerPage({
true,
);
signal.send(relayCapacityMessageForBrowser());
+ // Normalize this visit's limit before signaling replays its intent.
+ updateRelayVolunteer();
currentIceConfig = message.iceConfig;
- currentHostOnline = message.hostOnline;
const sharingPaused = message.hostPaused ?? false;
if (currentHostPaused !== sharingPaused && sharingPaused) {
invalidatePresentedMedia();
@@ -1819,7 +1817,6 @@ export function ViewerPage({
}
if (message.type === "host-status") {
if (!message.online) setCoPlayParticipants([]);
- currentHostOnline = message.online;
if (currentHostPaused !== message.paused && message.paused) {
invalidatePresentedMedia();
}
@@ -1852,7 +1849,6 @@ export function ViewerPage({
currentShareGeneration = null;
currentRoutePolicy = DEFAULT_ROUTE_POLICY;
setAssignedRoute(null);
- currentHostOnline = false;
currentHostPaused = false;
clearViewerSfuRoute();
clearPeerState(true);
@@ -2318,13 +2314,17 @@ export function ViewerPage({
overlayAction={<RoomChatToggle session={interactionSession} />}
overlaySettings={interactionSession && <RoomChatOverlaySettings session={interactionSession} />}
remoteActions={<RemoteControl channel={inputRoute.current} videoRef={videoRef}
- pictureFit={pictureFit} pictureRotation={pictureRotation}
+ pictureFit={pictureFit} pictureRotation={pictureRotation} touchMode={remoteTouchMode}
available={presentation.overlay === "none" && presentation.hasCurrentFrame} />}
settings={{ pictureFit, pictureRotation, setPictureRotation: rotation => {
setPictureRotation(rotation); saveRoomPreferences({ pictureRotation: rotation });
}, setPictureFit: fit => {
setPictureFit(fit); saveRoomPreferences({ pictureFit: fit });
},
+ remoteTouch: inputRoute.current ? {
+ mode: remoteTouchMode,
+ onChange: mode => { setRemoteTouchMode(mode); saveRoomPreferences({ remoteTouchMode: mode }); },
+ } : undefined,
route: routeOptions ? {
preference: routePreference, sfuAvailable: routeOptions.sfu && !peerOnly,
sfuOnly: routeOptions.sfuOnly, peerOnly,
@@ -2339,8 +2339,10 @@ export function ViewerPage({
} : undefined,
relayVolunteer: relayVolunteerSupported ? {
enabled: relayVolunteerEnabled, pending: relayVolunteerPending,
- disabled: signalStatus !== "connected" || relayVolunteerPending,
+ ...relayVolunteerLimits,
+ disabled: signalStatus !== "connected" || relayVolunteerPending || (!relayVolunteerLimits.available && !relayVolunteerEnabled),
onChange: enabled => requestRelayVolunteerRef.current?.(enabled),
+ onCapacityChange: capacity => requestRelayVolunteerRef.current?.(true, capacity),
} : undefined,
}}
videoRef={videoRef}
diff --git a/src/client/pages/host-page-notices.ts b/src/client/pages/host-page-notices.ts
index 5d81ee69..96426cdb 100644
--- a/src/client/pages/host-page-notices.ts
+++ b/src/client/pages/host-page-notices.ts
@@ -137,11 +137,3 @@ export function sourceSwitchNotice({
? say("host.notice.sourceSwitch.partial", { warning: peerWarning })
: say("host.notice.sourceSwitch.ok");
}
-
-export function shouldPauseLocalPreview(
- visibilityState: DocumentVisibilityState,
- hasFocus: boolean,
- pictureInPicture = false,
-): boolean {
- return !pictureInPicture && (visibilityState !== "visible" || !hasFocus);
-}
diff --git a/src/client/pages/preview-input.ts b/src/client/pages/preview-input.ts
index bc514480..dad5a8bc 100644
--- a/src/client/pages/preview-input.ts
+++ b/src/client/pages/preview-input.ts
@@ -7,7 +7,7 @@ export class PreviewInput extends EventTarget implements InputTransport {
bufferedAmount = 0;
private listeners = new Set<() => void>();
private password = "";
- private state: RemoteInputState = { state: "ready", mode: "ask", platform: "windows", coPlay: true, capabilities: ["coPlay", "text"] };
+ private state: RemoteInputState = { state: "ready", mode: "ask", platform: "windows", coPlay: true, capabilities: ["coPlay", "text", "inputFeedback"] };
getSnapshot = () => this.state;
subscribe = (listener: () => void) => { this.listeners.add(listener); return () => { this.listeners.delete(listener); }; };
private publish(next: RemoteInputState): void {
@@ -28,16 +28,21 @@ export class PreviewInput extends EventTarget implements InputTransport {
} else {
const pending = this.state.mode === "ask";
this.publish({ ...this.state, state: pending ? "pending" : "active", intent: event.intent,
- grant: pending ? undefined : "1".repeat(64), reason: undefined });
+ grant: pending ? undefined : "1".repeat(64), reason: undefined, inputFailed: undefined });
}
+ } else if (this.state.inputFailed) {
+ this.publish({ ...this.state, inputFailed: undefined });
}
}
+ failInput(): void {
+ if (this.state.state === "active") this.publish({ ...this.state, inputFailed: true });
+ }
reset(): void {
- this.publish({ ...this.state, state: this.state.mode === "off" ? "unavailable" : "ready", grant: undefined, reason: undefined });
+ this.publish({ ...this.state, state: this.state.mode === "off" ? "unavailable" : "ready", grant: undefined, reason: undefined, inputFailed: undefined });
}
policy(mode: RemoteControlMode, password = ""): void {
this.password = password;
- this.publish({ ...this.state, mode, state: mode === "off" ? "unavailable" : "ready", grant: undefined, reason: undefined });
+ this.publish({ ...this.state, mode, state: mode === "off" ? "unavailable" : "ready", grant: undefined, reason: undefined, inputFailed: undefined });
}
decide(allow: boolean): void {
if (!allow) this.reset();
diff --git a/src/client/pages/status-preview.css b/src/client/pages/status-preview.css
index 1d1ad63e..4a276399 100644
--- a/src/client/pages/status-preview.css
+++ b/src/client/pages/status-preview.css
@@ -1,5 +1,5 @@
.sp-page { --muted: color-mix(in srgb, var(--ink) 65%, var(--wall)); }
-.sp-main { max-width: 1190px; margin: 0 auto; padding: 32px 24px 64px; }
+.sp-main { width: 100%; max-width: 1190px; margin: 0 auto; padding: 32px 24px 64px; }
.sp-heading { display: flex; align-items: end; justify-content: space-between; gap: 24px; margin-bottom: 28px; }
.sp-heading h1 { margin: 6px 0 12px; font-size: clamp(1.5rem, 4vw, 2.2rem); letter-spacing: -.04em; }
.sp-heading p, .sp-catalog header p { margin: 0; color: var(--muted); line-height: 1.65; }
@@ -44,6 +44,9 @@
.sp-surfaces .sp-kicker, .sp-surfaces .sp-caption { margin: 0; }
.sp-catalog { margin-top: 36px; }
.sp-catalog header { margin-bottom: 18px; }
+.sp-failure-choice { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
+.sp-failure-choice select { flex: 1; min-width: 0; height: auto; padding: 8px; }
+.sp-failure-stage { max-width: 640px; margin-inline: auto; }
.sp-loading-preview { display: grid; place-items: center; min-height: 210px; margin-block: 18px; padding: 18px; background: var(--paper); border-radius: 18px; }
.sp-title-catalog { padding: 12px 0; border-bottom: 1px solid color-mix(in srgb, var(--ink) 15%, transparent); }
.sp-title-catalog summary { cursor: pointer; }
diff --git a/src/client/remote-control/input-session.ts b/src/client/remote-control/input-session.ts
index 501822bc..4dc06280 100644
--- a/src/client/remote-control/input-session.ts
+++ b/src/client/remote-control/input-session.ts
@@ -1,4 +1,5 @@
import type { InputTransport } from "./input-route";
+import { debugError, debugEvent } from "../lib/debug";
import { remoteInputStateSchema, remoteTextAvailable, remoteGamepadAvailable, type RemoteInputState, type RemoteGamepadReport } from "./protocol";
export type { RemoteInputState } from "./protocol";
export type RemoteInputEvent =
@@ -22,6 +23,7 @@ export class RemoteInputSession {
private intent: string | null = null;
private disposed = false;
private capabilitiesRequested = false;
+ private inputFeedbackRequested = false;
private pointer: PointerInput | null = null;
private pointerTimer: ReturnType<typeof setTimeout> | null = null;
private nextPointerAt = 0;
@@ -73,6 +75,10 @@ export class RemoteInputSession {
this.capabilitiesRequested = true;
if (!state.capabilities && !this.send({ type: "capabilities" })) return;
}
+ if (!this.inputFeedbackRequested && state.capabilities?.includes("inputFeedback")) {
+ this.inputFeedbackRequested = true;
+ if (!this.send({ type: "capabilities", inputFeedback: true })) return;
+ }
// Source availability is authoritative for every request. Pause/off may
// reach us before the Host receives our new intent; it still ends it.
if (state.state === "unavailable") this.intent = null;
@@ -82,6 +88,7 @@ export class RemoteInputSession {
return;
}
if (state.state !== "pending" && state.state !== "active" && state.state !== "suspended") this.intent = null;
+ if (state.state === "failed") debugEvent("remote-input", "host-failed", { previousState: this.state?.state, mode: state.mode });
this.set(state);
};
@@ -94,7 +101,8 @@ export class RemoteInputSession {
if (this.channel.bufferedAmount > 8192) throw new Error("input queue stalled");
this.channel.send(JSON.stringify(event));
return true;
- } catch {
+ } catch (error) {
+ debugError("remote-input", "send-failed", error, { state: this.state?.state, readyState: this.channel.readyState, bufferedAmount: this.channel.bufferedAmount });
this.intent = null;
this.set(this.state && { ...this.state, state: "failed", grant: undefined });
this.channel.close();
@@ -108,7 +116,7 @@ export class RemoteInputSession {
if (!state) return;
this.intent = crypto.randomUUID().replaceAll("-", "");
if (this.send({ type: "request", intent: this.intent, ...(password !== undefined ? { password } : {}) }))
- this.set({ ...state, state: "pending", grant: undefined, reason: undefined, intent: this.intent });
+ this.set({ ...state, state: "pending", grant: undefined, reason: undefined, inputFailed: undefined, intent: this.intent });
}
release(): void {
@@ -119,7 +127,7 @@ export class RemoteInputSession {
const intent = this.intent;
const state = this.state;
this.intent = null;
- if (this.send({ type: "release", intent })) this.set({ ...state, state: "ready", grant: undefined, intent: undefined });
+ if (this.send({ type: "release", intent })) this.set({ ...state, state: "ready", grant: undefined, intent: undefined, inputFailed: undefined });
}
input(event: RemoteInputEvent): void {
diff --git a/src/client/remote-control/protocol.ts b/src/client/remote-control/protocol.ts
index 4530cdf1..fc6bd37d 100644
--- a/src/client/remote-control/protocol.ts
+++ b/src/client/remote-control/protocol.ts
@@ -24,6 +24,7 @@ export const remoteInputStateSchema = z.object({
reason: z.enum(["password", "retry", "busy", "gamepad"]).optional(),
capabilities: z.array(z.string().min(1).max(32)).max(16).optional(),
coPlay: z.boolean().optional(),
+ inputFailed: z.boolean().optional(),
}).strict();
export type RemoteInputState = z.infer<typeof remoteInputStateSchema>;
diff --git a/src/client/remote-control/touchpad.ts b/src/client/remote-control/touchpad.ts
index ee0f0b00..5a71a295 100644
--- a/src/client/remote-control/touchpad.ts
+++ b/src/client/remote-control/touchpad.ts
@@ -1,12 +1,15 @@
import type { RemoteInputEvent } from "./input-session";
import type { RemoteViewport } from "./viewport";
-import { rotatePictureDelta, type VideoRotation } from "../media/video-geometry";
+import { rotatePictureDelta, rotatePicturePoint, type VideoRotation } from "../media/video-geometry";
+
+export const REMOTE_TOUCH_HOLD_MS = 500;
type Point = { x: number; y: number };
+type PointerMode = "direct" | "trackpad" | "mouse";
type Contact = Point & { start: Point };
-// Gesture lifetime belongs to the current input grant. Pointer coordinates
-// belong to the source picture; the local viewport supplies its visible bounds.
+// Gesture lifetime belongs to the current input grant. One source target drives
+// the pointer and tool; local view gestures reanchor it without remote input.
export class RemoteTouchpad {
position: Point = { x: 0.5, y: 0.5 };
private contacts = new Map<number, Contact>();
@@ -14,37 +17,59 @@ export class RemoteTouchpad {
private fingers = 0;
private moved = false;
private dragging = false;
+ private mode: PointerMode = "direct";
+ private held = new Set<number>();
private started = 0;
private pair: { center: Point; distance: number } | null = null;
private gesture: "pending" | "scroll" | "view" = "pending";
private frame: number | null = null;
constructor(
- private readonly picture: () => { width: number; height: number; rotation: VideoRotation } | null,
+ private readonly picture: () => { left: number; top: number; width: number; height: number; rotation: VideoRotation } | null,
private readonly input: (event: RemoteInputEvent) => void,
private readonly scroll: (deltaY: number) => void,
private readonly changed: (position: Point) => void,
- private readonly viewport: Pick<RemoteViewport, "scale" | "transform" | "visiblePosition">,
+ private readonly viewport: Pick<RemoteViewport, "scale" | "transform">,
) {}
- down(id: number, x: number, y: number): void {
+ setMode(mode: PointerMode, position?: Point | null): void {
+ if (this.mode === mode && (mode === "direct" || !position)) return;
+ this.reset();
+ this.mode = mode;
+ if (mode !== "direct") {
+ const next = position ?? this.position;
+ this.position = { x: Math.max(0, Math.min(1, next.x)), y: Math.max(0, Math.min(1, next.y)) };
+ this.input({ type: "move", ...this.position });
+ this.changed(this.position);
+ }
+ }
+
+ refresh(): void {
+ this.changed(this.position);
+ }
+
+ down(id: number, x: number, y: number, position: Point | null): void {
if (!this.contacts.size) {
+ if (this.mode === "direct") {
+ if (!position) return;
+ this.position = position;
+ this.changed(this.position);
+ }
this.started = performance.now();
this.fingers = 0;
- this.moved = false;
+ this.moved = this.held.size > 0;
this.gesture = "pending";
- this.changed(this.position);
}
this.contacts.set(id, { x, y, start: { x, y } });
this.fingers = Math.max(this.fingers, this.contacts.size);
this.clearHold();
- if (this.fingers === 1) {
+ if (this.fingers === 1 && !this.moved) {
this.hold = setTimeout(() => {
this.hold = null;
this.dragging = true;
this.input({ type: "move", ...this.position });
- this.input({ type: "button", button: 0, down: true });
- }, 500);
+ if (!this.held.has(0)) this.input({ type: "button", button: 0, down: true });
+ }, REMOTE_TOUCH_HOLD_MS);
} else {
this.endDrag();
this.pair = this.pairGeometry();
@@ -70,13 +95,19 @@ export class RemoteTouchpad {
// Keep the initial displacement while deciding tap versus motion. Updating
// the origin before this threshold loses the start of every small swipe.
contact.x = x; contact.y = y;
+ this.moveBy(dx, dy);
+ }
+
+ moveBy(dx: number, dy: number): void {
+ this.moved = true;
+ this.clearHold();
const picture = this.picture();
if (!picture?.width || !picture.height) return;
const delta = rotatePictureDelta({ x: dx / picture.width, y: dy / picture.height }, picture.rotation, true);
- this.position = this.viewport.visiblePosition({
+ this.position = {
x: Math.max(0, Math.min(1, this.position.x + delta.x)),
y: Math.max(0, Math.min(1, this.position.y + delta.y)),
- });
+ };
this.input({ type: "move", ...this.position });
this.changed(this.position);
}
@@ -96,11 +127,24 @@ export class RemoteTouchpad {
const pinch = Math.abs(next.distance - previous.distance) > Math.max(8, previous.distance * .08);
const pan = Math.hypot(next.center.x - previous.center.x, next.center.y - previous.center.y) > 8;
if (!pinch && !pan) return;
- this.gesture = pinch || this.viewport.scale > 1 ? "view" : "scroll";
+ this.gesture = this.mode === "mouse" || pinch || this.viewport.scale !== 1 ? "view" : "scroll";
this.moved = true;
}
- if (this.gesture === "view") this.viewport.transform(previous.center, next.center, next.distance / previous.distance);
- else this.scroll(previous.center.y - next.center.y);
+ if (this.gesture === "view") {
+ const before = this.mode === "mouse" ? this.picture() : null;
+ const point = before && rotatePicturePoint(this.position, before.rotation);
+ this.viewport.transform(previous.center, next.center, next.distance / previous.distance);
+ const after = point && this.picture();
+ if (before && point && after?.width && after.height) {
+ // The picture slides under the tool; only its local source target changes.
+ const target = rotatePicturePoint({
+ x: (before.left + point.x * before.width - after.left) / after.width,
+ y: (before.top + point.y * before.height - after.top) / after.height,
+ }, after.rotation, true);
+ this.position = { x: Math.max(0, Math.min(1, target.x)), y: Math.max(0, Math.min(1, target.y)) };
+ this.changed(this.position);
+ }
+ } else this.scroll(previous.center.y - next.center.y);
this.pair = next;
}
@@ -109,7 +153,7 @@ export class RemoteTouchpad {
if (!this.contacts.delete(id)) return;
this.clearHold();
if (this.contacts.size) return;
- if (!this.moved && !this.dragging && performance.now() - this.started < 500 && this.fingers <= 2) {
+ if (!this.moved && !this.dragging && performance.now() - this.started < REMOTE_TOUCH_HOLD_MS && this.fingers <= 2) {
const button = this.fingers === 2 ? 2 : 0;
this.input({ type: "move", ...this.position });
this.input({ type: "button", button, down: true });
@@ -124,10 +168,25 @@ export class RemoteTouchpad {
}
private endDrag(): void {
- if (this.dragging) this.input({ type: "button", button: 0, down: false });
+ if (this.dragging && !this.held.has(0)) this.input({ type: "button", button: 0, down: false });
this.dragging = false;
}
+ button(button: number, down: boolean): void {
+ if (this.held.has(button) === down) return;
+ this.clearHold();
+ this.moved = true;
+ if (down) {
+ this.held.add(button);
+ this.input({ type: "move", ...this.position });
+ } else this.held.delete(button);
+ if (button !== 0 || !this.dragging) this.input({ type: "button", button, down });
+ }
+
+ wheel(deltaY: number): void {
+ this.scroll(deltaY);
+ }
+
cancel(id: number): void {
if (this.contacts.has(id)) this.reset();
}
@@ -139,5 +198,6 @@ export class RemoteTouchpad {
this.clearHold();
this.endDrag();
this.contacts.clear();
+ for (const button of [...this.held]) this.button(button, false);
}
}
diff --git a/src/client/remote-control/viewport.ts b/src/client/remote-control/viewport.ts
index 865f01cf..6f412c42 100644
--- a/src/client/remote-control/viewport.ts
+++ b/src/client/remote-control/viewport.ts
@@ -7,6 +7,7 @@ type Point = { x: number; y: number };
export class RemoteViewport {
scale = 1;
private offset: Point = { x: 0, y: 0 };
+ private room: { right: number; bottom: number } | null = null;
constructor(
private readonly video: HTMLVideoElement,
@@ -16,7 +17,7 @@ export class RemoteViewport {
transform(from: Point, to: Point, ratio: number): void {
const rect = this.surface.getBoundingClientRect();
- const next = Math.max(1, Math.min(4, this.scale * ratio));
+ const next = Math.max(.5, Math.min(4, this.scale * ratio));
const factor = next / this.scale;
const center = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
this.offset = {
@@ -32,11 +33,15 @@ export class RemoteViewport {
const { width, height } = this.surface.getBoundingClientRect();
if (!videoWidth || !videoHeight || !width || !height) return;
const picture = videoPictureSize(this.video, width, height);
- const limitX = Math.max(0, (picture.width * this.scale - width) / 2);
- const limitY = Math.max(0, (picture.height * this.scale - height) / 2);
+ // Pan across the picture, adding only the visible tool's footprint at its
+ // edges. Zooming out keeps the natural gutters; the picture remains reachable.
+ const limit = (size: number, frame: number, room: number) => Math.max(0, Math.min((size + frame) / 2 - 1,
+ Math.abs(size - frame) / 2 + Math.min(room, frame - 1)));
+ const limitX = limit(picture.width * this.scale, width, this.room?.right ?? 0);
+ const limitY = limit(picture.height * this.scale, height, this.room?.bottom ?? 0);
const x = this.offset.x = Math.max(-limitX, Math.min(limitX, this.offset.x));
const y = this.offset.y = Math.max(-limitY, Math.min(limitY, this.offset.y));
- style.transform = this.scale > 1 ? `translate(${x}px, ${y}px) scale(${this.scale}) var(--picture-transform)` : "";
+ style.transform = this.scale !== 1 || x !== 0 || y !== 0 ? `translate(${x}px, ${y}px) scale(${this.scale}) var(--picture-transform, rotate(0deg))` : "";
// Clip to the original video cell, including when a keyboard occupies the
// next grid cell. Inverse-transform its edges into the video's local space.
const bounds = this.video.getBoundingClientRect(), surface = this.surface.getBoundingClientRect();
@@ -51,23 +56,28 @@ export class RemoteViewport {
this.changed();
}
- visiblePosition(position: Point): Point {
+ follow(position: Point, room: { right: number; bottom: number } | null): void {
+ const resized = this.room?.right !== room?.right || this.room?.bottom !== room?.bottom;
+ this.room = room;
+ if (!room) {
+ if (resized) this.refresh();
+ return;
+ }
const picture = pictureRect(this.video), rect = this.surface.getBoundingClientRect();
- if (!picture) return position;
- // Keep the local touch cursor in view while panning. No remote input is
- // emitted until the user next moves or clicks, at this visible position.
+ if (!picture) return;
const rotated = rotatePicturePoint(position, videoRotation(this.video));
- return rotatePicturePoint({
- x: Math.max(0, Math.min(1, Math.max((rect.left - picture.left) / picture.width,
- Math.min((rect.right - picture.left) / picture.width, rotated.x)))),
- y: Math.max(0, Math.min(1, Math.max((rect.top - picture.top) / picture.height,
- Math.min((rect.bottom - picture.top) / picture.height, rotated.y)))),
- }, videoRotation(this.video), true);
+ const x = picture.left + rotated.x * picture.width, y = picture.top + rotated.y * picture.height;
+ const dx = Math.max(rect.left, Math.min(rect.right - Math.min(room.right, rect.width - 1), x)) - x;
+ const dy = Math.max(rect.top, Math.min(rect.bottom - Math.min(room.bottom, rect.height - 1), y)) - y;
+ if (!dx && !dy && !resized) return;
+ this.offset.x += dx; this.offset.y += dy;
+ this.refresh();
}
reset(): void {
this.scale = 1;
this.offset = { x: 0, y: 0 };
+ this.room = null;
this.video.style.transform = "";
this.video.style.clipPath = "";
this.changed();
diff --git a/src/client/room-voice/browser-endpoint.ts b/src/client/room-voice/browser-endpoint.ts
index 9b7c638b..ce85673b 100644
--- a/src/client/room-voice/browser-endpoint.ts
+++ b/src/client/room-voice/browser-endpoint.ts
@@ -176,7 +176,7 @@ export class BrowserVoiceEndpoint {
releaseDebug: observeDebugConnection(pc, { connectionId: config.connectionId, role: "room-voice" }),
candidates: new NatPredictionCandidateEmitter(prediction, candidate => {
if (!candidate || !this.current(connection) || /\sTCP\s/i.test(candidate.candidate)) return;
- if (connection.localCandidates.length >= 64) { this.report(connection, "failed"); return; }
+ if (connection.localCandidates.length >= 64) return;
connection.localCandidates.push(candidate); this.flushCandidates(connection);
}, natPredictionSurveyUrls(this.iceConfig.iceServers, this.iceConfig.natPredictionStunUrls)),
queuedCandidates: [], localCandidates: [], senders: new Map(), remote: new Map(), dirty: this.isOfferer(config),
@@ -377,7 +377,6 @@ export class BrowserVoiceEndpoint {
if (message.candidate) {
if (connection.pc.remoteDescription) await this.wait(connection, () => addRemoteIceCandidate(connection.pc, message.candidate!));
else if (connection.queuedCandidates.length < 64) connection.queuedCandidates.push(message.candidate);
- else throw new Error("Too many voice ICE candidates");
return;
}
const description = message.description;
diff --git a/src/client/sfu/peer.ts b/src/client/sfu/peer.ts
index 9f1b68a1..2d35e915 100644
--- a/src/client/sfu/peer.ts
+++ b/src/client/sfu/peer.ts
@@ -43,11 +43,9 @@ export class SfuPeer {
this.pc.onicecandidate = ({ candidate }) => {
if (this.closed || !candidate || candidate.protocol === "tcp") return;
const value = { ...candidate.toJSON(), candidate: candidate.candidate };
- if (this.localCandidates.length >= 64) this.events.onState("failed");
- else {
- this.localCandidates.push(value);
- this.flushSignaling();
- }
+ if (this.localCandidates.length >= 64) return;
+ this.localCandidates.push(value);
+ this.flushSignaling();
};
this.pc.onconnectionstatechange = () => {
if (!this.closed) this.events.onState(this.pc.connectionState);
@@ -143,7 +141,6 @@ export class SfuPeer {
await this.waitForOperation(() => addRemoteIceCandidate(this.pc, message.candidate!));
else if (this.remoteCandidates.length < 64)
this.remoteCandidates.push(message.candidate);
- else throw new Error("Too many SFU ICE candidates");
}
};
const next = this.signalTail.then(work);
diff --git a/src/client/styles.css b/src/client/styles.css
index 45adc759..5fa38940 100644
--- a/src/client/styles.css
+++ b/src/client/styles.css
@@ -399,21 +399,24 @@ body.lr-theater-open .lr-top { visibility: hidden; }
/* App capture source picker */
.lr-source-picker {
+ z-index: 5;
place-content: stretch;
justify-items: stretch;
overflow: hidden;
- background: linear-gradient(155deg, color-mix(in srgb, var(--wall) 88%, var(--paper)), var(--wall));
+ background: var(--wall);
color: var(--ink);
text-align: left;
}
+.lr-tv-screen:has(> .lr-playback) > .lr-tv-overlay.lr-source-picker { grid-area: 1 / 1 / -1 / 2; }
.lr-source-picker-panel {
display: grid;
grid-template-rows: auto auto minmax(0, 1fr);
width: min(720px, 100%);
min-width: 0;
min-height: 0;
+ height: 100%;
max-height: 100%;
- margin: auto;
+ margin-inline: auto;
gap: 12px;
}
.lr-source-picker-head {
@@ -481,13 +484,12 @@ body.lr-theater-open .lr-top { visibility: hidden; }
flex-direction: column;
gap: 10px;
overflow-y: auto;
+ overscroll-behavior-y: contain;
scrollbar-gutter: stable;
padding: 8px;
margin: -8px;
}
.lr-source-picker-body > [role="status"] { text-align: center; }
-.lr-source-app-connection { display: flex; justify-content: flex-end; margin-top: 12px; }
-.lr-source-app-connect { display: flex; gap: 8px; width: auto; padding: 0 12px; margin: 0; border-radius: 999px; }
.lr-source-picker-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
@@ -522,19 +524,19 @@ body.lr-theater-open .lr-top { visibility: hidden; }
}
.lr-source-option:active:not(:disabled) { transform: var(--control-press-transform); box-shadow: none; transition-duration: 80ms; }
.lr-source-option[aria-current="true"] { border-color: var(--action); background: var(--control-selected); color: var(--control-selected-ink); }
-.lr-source-option:not(.is-browser) {
+.lr-source-option:not(.is-action) {
display: grid;
align-items: stretch;
gap: 6px;
padding: 8px;
}
-.lr-source-option:not(.is-browser) .lr-source-option-copy {
+.lr-source-option:not(.is-action) .lr-source-option-copy {
display: flex;
min-height: 36px;
align-items: flex-start;
gap: 8px;
}
-.lr-source-option:not(.is-browser) .lr-source-option-copy strong {
+.lr-source-option:not(.is-action) .lr-source-option-copy strong {
display: -webkit-box;
flex: 1;
min-width: 0;
@@ -572,7 +574,8 @@ body.lr-theater-open .lr-top { visibility: hidden; }
white-space: nowrap;
font-size: 13px;
}
-.lr-source-picker.is-visual .lr-source-option.is-browser {
+.lr-source-option.is-action .lr-source-option-copy strong { white-space: normal; }
+.lr-source-picker.is-visual .lr-source-option.is-action {
min-height: 90px;
justify-content: center;
padding: 8px;
@@ -1475,8 +1478,19 @@ body.lr-theater-open .lr-top { visibility: hidden; }
.lr-client-launch-title { font-size: 36px; line-height: 1.1; letter-spacing: -.04em; }
.lr-client-launch-title small { margin-left: 6px; color: var(--muted); font-size: 15px; letter-spacing: 0; font-weight: 650; }
.lr-client-brand-dot { color: #e77d38; }
-.lr-client-standby { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; width: 100%; padding: 12px 20px 16px; border: 1px solid color-mix(in srgb, var(--ink) 20%, transparent); border-radius: 22px; background: var(--surface-glaze) var(--paper); box-shadow: var(--control-shadow); }
+.lr-client-standby { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; width: 100%; padding: 12px 20px 16px; border: 1px solid var(--line); border-radius: 22px; background: var(--surface-glaze) var(--paper); box-shadow: var(--control-shadow); }
.lr-client-standby-state { display: inline-flex; align-items: center; gap: 9px; }
+.lr-client-standby-heading { display: flex; align-items: start; gap: 12px; min-width: 0; }
+.lr-client-standby-heading > details { flex: 1; min-width: 0; }
+.lr-client-site-add { display: inline-flex; padding-top: 4px; }
+.lr-client-site-entry { width: min(360px, calc(100vw - 16px)); }
+.lr-client-site-entry:is(:popover-open, .\:popover-open) { display: grid; gap: 12px; }
+.lr-client-site-entry > strong { font-size: 14px; }
+.lr-client-site-address { display: flex; align-items: center; gap: 8px; min-width: 0; }
+.lr-client-site-address > .lr-input { flex: 1; }
+.lr-client-site-address > .lr-btn { flex: none; }
+.lr-client-site-entry p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.65; }
+.lr-client-site-entry [role="alert"] { color: var(--danger); }
.lr-client-standby strong { font-size: 14px; }
.lr-client-standby p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.65; }
.lr-client-connect-guide > summary { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; min-height: 40px; border-radius: 8px; cursor: pointer; list-style: none; }
@@ -1494,7 +1508,7 @@ body.lr-theater-open .lr-top { visibility: hidden; }
.lr-client-launch-section { justify-self: start; margin: 5px 0 -8px; font-size: 13px; font-weight: 700; color: var(--muted); }
.lr-client-approval {
display: grid; width: min(480px, 100%); min-width: 0; gap: 20px;
- padding: clamp(18px, 5vw, 32px); border: 1px solid color-mix(in srgb, var(--ink) 20%, transparent);
+ padding: clamp(18px, 5vw, 32px); border: 1px solid var(--line);
border-radius: 28px; background: var(--paper); box-shadow: var(--control-shadow);
}
.lr-client-approval-heading { display: grid; justify-items: center; gap: 14px; text-align: center; }
@@ -1545,7 +1559,7 @@ body.lr-theater-open .lr-top { visibility: hidden; }
.lr-client-modes {
display: grid;
width: 100%;
- grid-template-columns: repeat(2, minmax(0, 1fr));
+ grid-template-columns: repeat(var(--launcher-columns, 2), minmax(0, 1fr));
gap: 10px;
}
.lr-client-modes > .lr-comic-tip-wrap {
@@ -1715,7 +1729,7 @@ body.lr-theater-open .lr-top { visibility: hidden; }
@media (max-width: 640px) {
.lr-client-launch { padding: 16px 14px 48px; }
.lr-client-launch-panel { gap: 16px; }
- .lr-client-modes { grid-template-columns: 1fr; gap: 8px; }
+ .lr-client-modes { gap: 8px; }
.lr-client-mode {
min-height: 58px;
grid-template-columns: 34px 1fr;
@@ -1772,13 +1786,14 @@ body.lr-theater-open .lr-top { visibility: hidden; }
.lr-door-body { padding: 12px; }
.lr-dial { width: 54px; height: 66px; font-size: 28px; }
.lr-tv-screen.has-entry { aspect-ratio: auto; min-height: 292px; }
- .lr-tv-screen:has(.lr-source-picker) { aspect-ratio: auto; min-height: 440px; }
+ .lr-tv-screen:has(.lr-source-picker) { aspect-ratio: auto; height: min(520px, 76svh); min-height: 300px; }
.lr-tv-big { width: 72px; height: 72px; }
.lr-source-picker { padding: 12px; }
.lr-source-picker-panel { gap: 8px; }
.lr-source-picker-mark { width: 36px; height: 36px; }
- .lr-source-picker-list { max-height: min(320px, 48vh); }
- .lr-source-option { min-height: 78px; padding: 6px 8px; }
+ .lr-source-picker-list { grid-template-columns: repeat(auto-fit, minmax(min(100%, 140px), 1fr)); }
+ .lr-source-option { min-height: 58px; padding: 6px 8px; }
+ .lr-source-option-preview { height: 90px; }
.lr-source-option-icon { width: 34px; height: 34px; }
.lr-leds-label { display: none; }
.lr-top-right { gap: 8px; }
@@ -1787,6 +1802,14 @@ body.lr-theater-open .lr-top { visibility: hidden; }
.lr-top .lr-btn.is-compact .lr-cap { display: none; }
}
+@media (orientation: landscape) and (max-height: 500px) {
+ .lr-room:not(.is-theater) .lr-tv:has(.lr-source-picker) { width: min(960px, 100%); }
+ .lr-tv-screen:has(.lr-source-picker) { aspect-ratio: auto; height: max(244px, 84svh); }
+ .lr-source-picker { padding: 10px 12px; }
+ .lr-source-picker-panel { gap: 8px; }
+ .lr-source-option-preview { height: 90px; }
+}
+
.lr-stage-notices {
display: flex;
flex-wrap: wrap;
@@ -1926,6 +1949,7 @@ details[open] > summary > .lr-sharing-chevron { transform: rotate(180deg); }
.lr-top:has(.lr-leds) .lr-header-controls { grid-column: 1 / -1; justify-self: end; }
}
@media (max-width: 360px) {
+ .lr-client-modes { grid-template-columns: 1fr; }
.lr-lcd { gap: 4px; padding-inline: 6px; font-size: 18px; }
.lr-top { column-gap: 4px; padding-inline: 10px; }
.lr-top-right { gap: 4px; }
diff --git a/src/client/ui/control-timing.ts b/src/client/ui/control-timing.ts
new file mode 100644
index 00000000..481a88a5
--- /dev/null
+++ b/src/client/ui/control-timing.ts
@@ -0,0 +1,2 @@
+// Playback and touch mouse tools share the same idle-hiding cadence.
+export const PLAYER_CONTROLS_IDLE_MS = 2000;
diff --git a/src/client/ui/palette.css b/src/client/ui/palette.css
index c56856d8..73655fb0 100644
--- a/src/client/ui/palette.css
+++ b/src/client/ui/palette.css
@@ -4,10 +4,13 @@
--person-ink: #263b43;
--wall: #f3f5f8;
--wall-2: #e5eaf2;
- --paper: #fffefb;
+ --paper-rgb: 255 254 251;
+ --paper: rgb(var(--paper-rgb));
+ --paper-translucent: rgb(var(--paper-rgb) / .84);
--ink: #29374a;
- --muted: color-mix(in srgb, var(--ink) 75%, var(--paper));
- --line: color-mix(in srgb, var(--ink) 20%, var(--paper));
+ /* Opaque sRGB roles also work in WebViews without color-mix. */
+ --muted: rgb(94.5 104.75 118.25);
+ --line: rgb(212.2 214.2 215.6);
/* Primary actions keep their identity; selection/focus follows the theme. */
--primary: #2765ce;
--primary-ink: #fff;
@@ -23,16 +26,26 @@
--control-hover: #eef3ff;
--control-selected: #e5efff;
--control-selected-ink: #214c9b;
- --control-edge: color-mix(in srgb, var(--ink) 57%, var(--paper));
+ --control-edge: rgb(133.02 140.57 150.11);
--surface-rim: rgb(255 255 255 / .95);
- --surface-shade: rgb(36 53 78 / .18);
+ --surface-shade-rgb: 36 53 78;
+ --surface-shade-alpha: .18;
+ --surface-shade: rgb(var(--surface-shade-rgb) / var(--surface-shade-alpha));
--surface-glaze: linear-gradient(165deg, rgb(255 255 255 / .6), transparent 65%, rgb(51 89 147 / .025));
- --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px color-mix(in srgb, var(--surface-shade) 45%, transparent);
- --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px color-mix(in srgb, var(--surface-shade) 55%, transparent);
+ --control-shadow: 0 3px 0 var(--surface-shade), 0 5px 10px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .45));
+ --panel-shadow: inset 0 1px 0 var(--surface-rim), 0 3px 0 var(--surface-shade), 0 16px 36px rgb(var(--surface-shade-rgb) / calc(var(--surface-shade-alpha) * .55));
--piik-dark-wall: #242a34;
--piik-dark-wall-2: #2c3543;
- --piik-dark-paper: #354153;
+ --piik-dark-paper-rgb: 53 65 83;
+ --piik-dark-paper: rgb(var(--piik-dark-paper-rgb));
--piik-dark-ink: #f6f3eb;
+ --piik-dark-muted: rgb(197.75 198.5 197);
+ --piik-dark-line: rgb(91.6 100.6 113.4);
+ --piik-dark-control-edge: rgb(145.64 150.44 155.96);
+ --piik-dark-surface-rim: rgb(222 234 255 / .12);
+ --piik-dark-surface-shade-rgb: 6 12 22;
+ --piik-dark-surface-shade-alpha: .38;
+ --piik-dark-surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
--piik-dark-action: #f6f3eb;
--piik-dark-action-ink: #29374a;
--piik-dark-action-text: var(--piik-dark-action);
@@ -43,8 +56,11 @@
:root[data-theme="dark"], :root.dark {
--wall: var(--piik-dark-wall);
--wall-2: var(--piik-dark-wall-2);
+ --paper-rgb: var(--piik-dark-paper-rgb);
--paper: var(--piik-dark-paper);
--ink: var(--piik-dark-ink);
+ --muted: var(--piik-dark-muted);
+ --line: var(--piik-dark-line);
--busy: #82b1ff;
--live: #3fb97a;
--warn: #e8a33d;
@@ -56,8 +72,9 @@
--control-hover: var(--piik-dark-hover);
--control-selected: var(--piik-dark-selected);
--control-selected-ink: var(--piik-dark-selected-ink);
- --control-edge: color-mix(in srgb, var(--ink) 48%, var(--paper));
- --surface-rim: rgb(222 234 255 / .12);
- --surface-shade: rgb(6 12 22 / .38);
- --surface-glaze: linear-gradient(165deg, rgb(224 237 255 / .06), transparent 65%, rgb(0 0 0 / .035));
+ --control-edge: var(--piik-dark-control-edge);
+ --surface-rim: var(--piik-dark-surface-rim);
+ --surface-shade-rgb: var(--piik-dark-surface-shade-rgb);
+ --surface-shade-alpha: var(--piik-dark-surface-shade-alpha);
+ --surface-glaze: var(--piik-dark-surface-glaze);
}
diff --git a/src/client/webrtc/host-peer.ts b/src/client/webrtc/host-peer.ts
index 29777cfb..1d7f9a7a 100644
--- a/src/client/webrtc/host-peer.ts
+++ b/src/client/webrtc/host-peer.ts
@@ -765,8 +765,10 @@ export class HostPeer {
const applied = await this.configureSender(this.videoSender, audio, {
profile: this.desiredProfile, profileRevision: revision, video: false, audio: true,
});
- if (!applied && !this.disposed && revision === this.profileRevision) this.fail();
- }).catch(error => this.fail(error));
+ // The negotiated ceiling changed independently of the requested
+ // bitrate. Keep the old readback, but let an explicit update retry.
+ if (!applied && !this.disposed && revision === this.profileRevision) this.appliedAudioBitrate = null;
+ }).catch(error => debugError("webrtc", "audio-parameters-failed", error, { connectionId: this.connectionId }));
}
await this.flushCandidates();
if (this.ownsAnswer(epoch)) {
diff --git a/src/shared/protocol.ts b/src/shared/protocol.ts
index 4d859982..5e83d5bd 100644
--- a/src/shared/protocol.ts
+++ b/src/shared/protocol.ts
@@ -1137,7 +1137,7 @@ export const serverMessageSchema = z.union([
z.object({ type: z.literal("co-play-presence"), participants: z.array(coPlayParticipantSchema).max(MAX_VIEWERS_PER_ROOM_LIMIT)
.refine(participants => new Set(participants.map(person => person.peerId)).size === participants.length) }).strict(),
z.object({ type: z.literal("remote-input-peers"), peers: z.array(z.object({ peerId: opaqueIdSchema,
- endpointId: z.string().regex(/^[0-9a-f]{32}$/), secret: z.string().regex(/^[0-9a-f]{64}$/) }).strict()).max(20) }).strict(),
+ endpointId: z.string().regex(/^[0-9a-f]{32}$/), secret: z.string().regex(/^[0-9a-f]{64}$/) }).strict()).max(MAX_VIEWERS_PER_ROOM_LIMIT) }).strict(),
z.object({ type: z.literal("room-interactions-ready"), serverTime: z.number().int().min(0).max(Number.MAX_SAFE_INTEGER) }).strict(),
z.object({ type: z.literal("room-interaction-rejected"), requestId: opaqueIdSchema,
reason: z.enum(["rate-limited", "target-unavailable", "not-subscribed", "busy"]) }).strict(),
diff --git a/tests/app-connection-button.test.ts b/tests/app-connection-button.test.ts
index ad4ca4e7..9b54ef6f 100644
--- a/tests/app-connection-button.test.ts
+++ b/tests/app-connection-button.test.ts
@@ -7,38 +7,31 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { AppConnectionButton } from "../src/client/components/living/AppConnectionButton";
import { CaptureSourcePicker } from "../src/client/components/living/CaptureSourcePicker";
-describe("desktop App connection entry", () => {
+describe("shared App connection entry", () => {
afterEach(() => vi.unstubAllGlobals());
it.each([
- ["Android phone", "Mozilla/5.0 (Linux; Android 16) Mobile", "Linux armv8l", 5],
- ["Android tablet", "Mozilla/5.0 (Linux; Android 16)", "Linux armv8l", 5],
["iPhone", "Mozilla/5.0 (iPhone; CPU iPhone OS 26_0 like Mac OS X)", "iPhone", 5],
["iPad", "Mozilla/5.0 (iPad; CPU OS 26_0 like Mac OS X)", "iPad", 5],
["desktop-mode iPad", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15)", "MacIntel", 5],
- ])("omits the desktop workflow on %s, retaining Browser and camera choices", (_, userAgent, platform, maxTouchPoints) => {
+ ["Android phone", "Mozilla/5.0 (Linux; Android 16) Mobile", "Linux armv8l", 5],
+ ["Android tablet", "Mozilla/5.0 (Linux; Android 16)", "Linux armv8l", 5],
+ ["touch Windows", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", "Win32", 10],
+ ["Mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15)", "MacIntel", 0],
+ ["Linux", "Mozilla/5.0 (X11; Linux x86_64)", "Linux x86_64", 0],
+ ["unknown", "", "", 0],
+ ])("offers the same connection entry on %s without hiding Browser capture", (_, userAgent, platform, maxTouchPoints) => {
vi.stubGlobal("navigator", { userAgent, platform, maxTouchPoints });
- expect(renderToStaticMarkup(createElement(AppConnectionButton))).toBe("");
+ expect(renderToStaticMarkup(createElement(AppConnectionButton))).toContain("lr-app-connect");
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "browser" },
onBrowser: () => {}, onNative: () => {}, onCamera: () => {},
- onPreview: async () => null, onRefresh: () => {}, onCancel: () => {}, onAppConnected: () => {},
+ onPreview: async () => null, onRefresh: () => {}, onCancel: () => {},
}));
expect(html).toContain('data-source-tab="browser"');
expect(html).toContain('data-source-tab="camera"');
expect(html).not.toContain("lr-source-app-connection");
- });
-
- it.each([
- ["touch Windows", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", "Win32", 10],
- ["Mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15)", "MacIntel", 0],
- ["Linux", "Mozilla/5.0 (X11; Linux x86_64)", "Linux x86_64", 0],
- ["unknown", "", "", 0],
- ])("retains the shared connection entry on %s", (_, userAgent, platform, maxTouchPoints) => {
- vi.stubGlobal("navigator", { userAgent, platform, maxTouchPoints });
- expect(renderToStaticMarkup(createElement(AppConnectionButton))).toContain("lr-app-connect");
- expect(renderToStaticMarkup(createElement(AppConnectionButton, { sourcePicker: true })))
- .toContain("lr-source-app-connection");
+ expect(html).not.toContain("lr-app-connect");
});
});
@@ -66,12 +59,14 @@ const settle = () => new Promise<void>(resolve => setImmediate(resolve));
const site = "https://site.example";
const approval = `http://127.0.0.1:39721/client?site=${encodeURIComponent(site)}`;
-function mount(activated = false, state: "idle" | "ready" = "idle") {
+function mount(activated = false, state: "idle" | "ready" = "idle", android = false) {
const popup = { closed: false, close: vi.fn(() => { popup.closed = true; }) };
- const window = Object.assign(new EventTarget(), { open: vi.fn((): typeof popup | null => popup), location: { origin: site } });
+ const window = Object.assign(new EventTarget(), { open: vi.fn((): typeof popup | null => popup), location: { origin: site, assign: vi.fn() } });
+ const document = Object.assign(new EventTarget(), { visibilityState: "visible" });
const native = {
discoverNativeConnect: vi.fn(async (_signal: AbortSignal): Promise<string | null> => approval),
- discoverNativeHealth: vi.fn(async (_options: { signal: AbortSignal }): Promise<object | null> => ({})),
+ discoverNativeHealth: vi.fn(async (options: { signal: AbortSignal; waitForPermission?: boolean }): Promise<object | null> =>
+ options.waitForPermission === false && !activated ? null : {}),
NativeCompatibilityError: class extends Error {},
};
const pending = { current: null as { controller: AbortController; cancel?: () => void } | null };
@@ -82,8 +77,9 @@ function mount(activated = false, state: "idle" | "ready" = "idle") {
const activateClient = vi.fn();
const onConnected = vi.fn();
const operation = runInNewContext(executable, {
- window, pending, healthCheck, help, showHelp, setState, activateClient, onConnected, AbortController, URL,
- sourcePicker: false, state, isClientActivated: () => activated,
+ window, document, pending, healthCheck, help, showHelp, setState, activateClient, onConnected, AbortController, URL,
+ wakeURL: android ? "intent://app#Intent;scheme=piik;package=tv.piik.android;end" : null,
+ state, isClientActivated: () => activated,
watchClientActivation: (changed: () => void) => {
window.addEventListener("activation", changed);
return () => window.removeEventListener("activation", changed);
@@ -92,7 +88,7 @@ function mount(activated = false, state: "idle" | "ready" = "idle") {
if (path !== "../../native/client") throw new Error(`Unexpected dependency: ${path}`);
return native;
},
- }) as { connect: () => Promise<void>; cleanup: () => void };
+ }) as { connect: (wake?: boolean) => Promise<void>; cleanup: () => void };
function message(overrides: Record<string, unknown> = {}) {
window.dispatchEvent(Object.assign(new Event("message"), {
source: popup, origin: new URL(approval).origin, data: { type: "piik-app-approved", site }, ...overrides,
@@ -100,10 +96,59 @@ function mount(activated = false, state: "idle" | "ready" = "idle") {
}
const activateElsewhere = () => { activated = true; window.dispatchEvent(new Event("activation")); };
const openApproval = async () => { await operation.connect(); await operation.connect(); };
- return { ...operation, openApproval, popup, window, native, pending, help, showHelp, setState, activateClient, activateElsewhere, onConnected, message };
+ return { ...operation, openApproval, popup, window, document, native, pending, help, showHelp, setState, activateClient, activateElsewhere, onConnected, message };
}
describe("App connection approval", () => {
+ it("uses fresh authorized health instead of offering approval again", async () => {
+ const page = mount();
+ page.native.discoverNativeHealth.mockResolvedValue({});
+ await page.connect();
+ expect(page.setState).toHaveBeenLastCalledWith("ready");
+ expect(page.setState).not.toHaveBeenCalledWith("approval");
+ expect(page.window.open).not.toHaveBeenCalled();
+ expect(page.activateClient).toHaveBeenCalledOnce();
+ expect(page.onConnected).toHaveBeenCalledOnce();
+ expect(page.pending.current).toBeNull();
+ page.cleanup();
+ });
+
+ it("wakes Android in the original tab, then resumes discovery and explicit approval on return", async () => {
+ const page = mount(false, "idle", true);
+ await page.connect(true);
+ expect(page.window.location.assign).toHaveBeenCalledWith("intent://app#Intent;scheme=piik;package=tv.piik.android;end");
+ expect(page.window.open).not.toHaveBeenCalled();
+ expect(page.native.discoverNativeConnect).not.toHaveBeenCalled();
+ expect(page.activateClient).not.toHaveBeenCalled();
+ page.window.dispatchEvent(new Event("focus"));
+ page.document.dispatchEvent(new Event("visibilitychange"));
+ await settle();
+ expect(page.native.discoverNativeConnect).toHaveBeenCalledOnce();
+ expect(page.setState).toHaveBeenLastCalledWith("approval");
+ expect(page.window.open).not.toHaveBeenCalled();
+ await page.connect();
+ expect(page.window.open).toHaveBeenCalledWith(approval, "_blank");
+ page.message();
+ await settle();
+ expect(page.native.discoverNativeHealth).toHaveBeenCalledTimes(2);
+ expect(page.activateClient).toHaveBeenCalledOnce();
+ page.cleanup();
+ });
+
+ it("cancels a cold wake without reviving discovery after a late App return", async () => {
+ const page = mount(false, "idle", true);
+ await page.connect(true);
+ page.pending.current!.cancel!();
+ page.window.dispatchEvent(new Event("focus"));
+ page.document.dispatchEvent(new Event("visibilitychange"));
+ await settle();
+ expect(page.native.discoverNativeConnect).not.toHaveBeenCalled();
+ expect(page.window.open).not.toHaveBeenCalled();
+ expect(page.activateClient).not.toHaveBeenCalled();
+ expect(page.pending.current).toBeNull();
+ page.cleanup();
+ });
+
it("keeps permission discovery in the original tab and opens only on a later click", async () => {
const page = mount();
let complete!: (url: string) => void;
@@ -118,7 +163,7 @@ describe("App connection approval", () => {
expect(page.setState).toHaveBeenLastCalledWith("approval");
expect(page.window.open).not.toHaveBeenCalled();
page.message({ source: null });
- expect(page.native.discoverNativeHealth).not.toHaveBeenCalled();
+ expect(page.native.discoverNativeHealth).toHaveBeenCalledOnce();
await page.connect();
expect(page.window.open).toHaveBeenCalledWith(approval, "_blank");
expect(page.setState).toHaveBeenLastCalledWith("approving");
@@ -149,7 +194,10 @@ describe("App connection approval", () => {
it("opens the launcher for an already connected Site without changing approval", async () => {
const page = mount(true, "ready");
- await page.openApproval();
+ await page.connect();
+ expect(page.setState).toHaveBeenLastCalledWith("ready");
+ expect(page.setState).not.toHaveBeenCalledWith("approval");
+ await page.connect();
expect(page.window.open).toHaveBeenCalledWith("http://127.0.0.1:39721/client", "_blank");
expect(page.setState).toHaveBeenLastCalledWith("ready");
expect(page.pending.current).toBeNull();
@@ -184,9 +232,10 @@ describe("App connection approval", () => {
it("requires the exact window, origin and site, then verifies App health once", async () => {
const page = mount();
+ await page.openApproval();
+ page.native.discoverNativeHealth.mockClear();
let complete!: (health: object) => void;
page.native.discoverNativeHealth.mockImplementation(() => new Promise(resolve => { complete = resolve; }));
- await page.openApproval();
await page.connect();
expect(page.window.open).toHaveBeenCalledOnce();
expect(page.window.open).toHaveBeenCalledWith(approval, "_blank");
@@ -214,9 +263,9 @@ describe("App connection approval", () => {
it.each(["absent", "failed"])("does not activate from an approval message when health is %s", async result => {
const page = mount();
+ await page.openApproval();
if (result === "absent") page.native.discoverNativeHealth.mockResolvedValue(null);
else page.native.discoverNativeHealth.mockRejectedValue(new Error("network unavailable"));
- await page.openApproval();
page.message();
await settle();
expect(page.activateClient).not.toHaveBeenCalled();
@@ -224,6 +273,7 @@ describe("App connection approval", () => {
expect(page.setState).toHaveBeenLastCalledWith("unavailable");
expect(page.popup.closed).toBe(true);
expect(page.pending.current).toBeNull();
+ page.native.discoverNativeHealth.mockResolvedValue(null);
await page.openApproval();
expect(page.window.open).toHaveBeenCalledTimes(2);
page.cleanup();
@@ -231,9 +281,10 @@ describe("App connection approval", () => {
it("ignores a late healthy response after unmount and closes the popup", async () => {
const page = mount();
+ await page.openApproval();
+ page.native.discoverNativeHealth.mockClear();
let complete!: (health: object) => void;
page.native.discoverNativeHealth.mockImplementation(() => new Promise(resolve => { complete = resolve; }));
- await page.openApproval();
page.message();
page.cleanup();
complete({});
@@ -260,15 +311,31 @@ describe("App connection approval", () => {
expect(page.activateClient).not.toHaveBeenCalled();
});
- it("releases a closed approval window on focus and permits retry", async () => {
+ it("verifies authorization on return even when the approval message is unavailable", async () => {
const page = mount();
await page.openApproval();
+ page.native.discoverNativeHealth.mockClear();
+ page.window.dispatchEvent(new Event("focus"));
+ await settle();
+ expect(page.native.discoverNativeHealth).toHaveBeenCalledOnce();
+ expect(page.setState).toHaveBeenLastCalledWith("ready");
+ expect(page.activateClient).toHaveBeenCalledOnce();
+ expect(page.onConnected).toHaveBeenCalledOnce();
+ expect(page.pending.current).toBeNull();
+ page.cleanup();
+ });
+
+ it("releases an unapproved closed window on focus and permits retry", async () => {
+ const page = mount();
+ await page.openApproval();
+ page.native.discoverNativeHealth.mockResolvedValue(null);
page.popup.closed = true;
page.window.dispatchEvent(new Event("focus"));
+ await settle();
expect(page.pending.current).toBeNull();
expect(page.setState).toHaveBeenLastCalledWith("idle");
page.message();
- expect(page.native.discoverNativeHealth).not.toHaveBeenCalled();
+ expect(page.native.discoverNativeHealth).toHaveBeenCalledTimes(2);
await page.openApproval();
expect(page.window.open).toHaveBeenCalledTimes(2);
page.cleanup();
diff --git a/tests/app-launcher-recovery.test.ts b/tests/app-launcher-recovery.test.ts
index 0fc1d5fd..93630a1b 100644
--- a/tests/app-launcher-recovery.test.ts
+++ b/tests/app-launcher-recovery.test.ts
@@ -1,8 +1,13 @@
import { readFileSync } from "node:fs";
import { runInNewContext } from "node:vm";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
import ts from "typescript";
import { describe, expect, it, vi } from "vitest";
import { z } from "zod";
+import { LauncherForm } from "../src/client/components/living/LauncherForm";
+import { siteEntryOrigin } from "../src/client/components/living/SiteAddressEntry";
+import { clientLaunchURL } from "../src/client/lib/session";
// Run the page's actual state-read effect, including cleanup, without a DOM.
const source = ts.createSourceFile("AppLauncherPage.tsx", readFileSync(
@@ -14,7 +19,7 @@ function collect(node: ts.Node): void {
if (ts.isVariableDeclaration(node) && /^launcher.*Schema$/.test(node.name.getText(source))) {
declarations.push(`const ${node.getText(source)};`);
}
- if (ts.isFunctionDeclaration(node) && ["beginStateChange", "changeSite", "enableDebug", "launch"].includes(node.name?.text ?? "")) {
+ if (ts.isFunctionDeclaration(node) && ["beginStateChange", "changeSite", "approveSite", "enableDebug", "launch"].includes(node.name?.text ?? "")) {
declarations.push(node.getText(source));
}
if (ts.isCallExpression(node) && node.expression.getText(source) === "useEffect"
@@ -25,7 +30,7 @@ function collect(node: ts.Node): void {
}
collect(source);
if (!effect) throw new Error("Missing App launcher state-read owner");
-const executable = ts.transpileModule(`${declarations.join("\n")}\n({cleanup: (${effect})(), changeSite, enableDebug, launch});`, {
+const executable = ts.transpileModule(`${declarations.join("\n")}\n({cleanup: (${effect})(), changeSite, approveSite, enableDebug, launch});`, {
compilerOptions: { target: ts.ScriptTarget.ES2022 },
}).outputText;
const settle = () => new Promise<void>(resolve => setImmediate(resolve));
@@ -36,10 +41,10 @@ const saved = {
};
const response = (state: typeof saved & { detail?: string } = saved) => ({ ok: true, status: 200, json: async () => state });
-function mount(fetch: ReturnType<typeof vi.fn>) {
+function mount(fetch: ReturnType<typeof vi.fn>, manualSite = false) {
const state: Record<string, unknown> = {};
const setters = Object.fromEntries([
- "Mode", "LocalAccessPassword", "Lan", "Version", "Target", "Starting", "Sites", "StartupSite",
+ "Mode", "Modes", "LocalAccessPassword", "Lan", "Version", "ControlPort", "Target", "Starting", "Sites", "StartupSite",
"AppDebug", "Debug", "Error", "Loading", "Update", "RemoteControlPrototype",
"SiteBusy", "SiteError", "DebugBusy", "DebugError", "Reload",
].map(name => [`set${name}`, vi.fn((value: unknown) => {
@@ -52,7 +57,7 @@ function mount(fetch: ReturnType<typeof vi.fn>) {
const actions = runInNewContext(executable, {
...setters, z, fetch, window, checkReleaseUpdate,
stateChanges: { current: { revision: 0, pending: 0 } },
- siteBusy: false, debugBusy: false, starting: false, sites: saved.sites,
+ siteBusy: false, debugBusy: false, starting: false, sites: saved.sites, manualSite, openOnApproval: false,
mode: "link", lang: "en", vis: false, debug: false, appDebug: false, localAccessPassword: "",
consoleLanguage: () => "en", launchURL: (target: string) => target, t: (key: string) => key,
setTimeout: (callback: () => void, delay: number) => {
@@ -61,8 +66,8 @@ function mount(fetch: ReturnType<typeof vi.fn>) {
return nextTimer;
},
clearTimeout: (id: number) => timers.delete(id),
- }) as { cleanup(): void; changeSite(site: string, setting?: "site" | "startup-site", starAfterApproval?: boolean, allowed?: boolean): Promise<void>; enableDebug(): Promise<void>; launch(event: { preventDefault(): void }): Promise<void> };
- return { state, setters, ...actions, checkReleaseUpdate, focus: () => window.dispatchEvent(new Event("focus")),
+ }) as { cleanup(): void; changeSite(site: string, setting?: "site" | "startup-site", starAfterApproval?: boolean, allowed?: boolean): Promise<boolean>; approveSite(site: string): Promise<void>; enableDebug(): Promise<void>; launch(event: { preventDefault(): void }): Promise<void> };
+ return { state, setters, ...actions, checkReleaseUpdate, navigate: window.location.replace, focus: () => window.dispatchEvent(new Event("focus")),
retry: () => {
expect(timers.size).toBe(1);
const [id, callback] = [...timers][0]!;
@@ -73,6 +78,119 @@ function mount(fetch: ReturnType<typeof vi.fn>) {
}
describe("App launcher state recovery", () => {
+ it.each([
+ ["v2.4.1", "v2.4.1"],
+ ["development", "development (aaaaaaa)"],
+ ])("shows the App build identity for %s without changing update inputs", async (version, label) => {
+ const revision = "a".repeat(40);
+ const page = mount(vi.fn(async () => response({ ...saved, version, revision })));
+ await settle();
+ expect(page.state.Version).toBe(label);
+ expect(page.checkReleaseUpdate).toHaveBeenCalledWith({ version, revision }, { packageTarget: undefined });
+ page.cleanup();
+ });
+
+ it("shows a failed Enter attempt and navigates after its retry succeeds", async () => {
+ type Result = { ok: boolean; status: number; json(): Promise<unknown> };
+ const fetch = vi.fn(async (): Promise<Result> => response());
+ const page = mount(fetch);
+ await settle();
+ let finish!: (value: Result) => void;
+ fetch.mockImplementationOnce(() => new Promise(resolve => { finish = resolve; }));
+ const event = { preventDefault() {} };
+ const first = page.launch(event);
+ expect(fetch).toHaveBeenCalledTimes(2); // Initial state and launch.
+ finish({ ok: false, status: 503, json: async () => ({ detail: "Startup failed" }) });
+ await first;
+ expect(page.state).toMatchObject({ Starting: false, Error: { kind: "launch", detail: "Startup failed" } });
+ fetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ target: "https://room.example/#piik-client=1" }) });
+ await page.launch(event);
+ expect(page.navigate).toHaveBeenCalledWith("https://room.example/#piik-client=1");
+ page.cleanup();
+ });
+
+ it("keeps mode choices and the submit action available after a failed room start", () => {
+ const html = renderToStaticMarkup(createElement(LauncherForm, {
+ mode: "link", sites: saved.sites, startupSite: "", localAccessPassword: "saved password",
+ onModeChange() {}, onToggleStartupSite() {}, onRemoveSite() {}, onSelectSite() {}, onLocalAccessPasswordChange() {}, onSubmit() {},
+ roomError: createElement("p", { role: "alert" }, "Connection timed out"),
+ }));
+ expect(html).toContain("Connection timed out");
+ expect(html).toContain('role="radiogroup"');
+ expect(html).toContain('value="saved password"');
+ expect(html).toMatch(/type="submit"[^>]*aria-label="(?:Retry|重试)"/);
+ expect(html).toContain(saved.sites[0]);
+ });
+
+ it("reopens a healthy room without offering another mode or password", () => {
+ const html = renderToStaticMarkup(createElement(LauncherForm, {
+ mode: "local", sites: [], startupSite: "", localAccessPassword: "saved password", target: "http://127.0.0.1:8787/#piik-client=1",
+ onModeChange() {}, onToggleStartupSite() {}, onRemoveSite() {}, onLocalAccessPasswordChange() {}, onSubmit() {},
+ }));
+ expect(html).toContain('href="http://127.0.0.1:8787/#piik-client=1"');
+ expect(html).not.toContain('role="radiogroup"');
+ expect(html).not.toContain('type="password"');
+ });
+
+ it("renders complete App entries for saved sites and the existing room before any click", () => {
+ const html = renderToStaticMarkup(createElement(LauncherForm, {
+ mode: "local", sites: saved.sites, startupSite: saved.sites[0]!, localAccessPassword: "",
+ target: "http://127.0.0.1:8787/#piik-client=1",
+ entryURL: target => clientLaunchURL(target, { lang: "en", vis: false, theme: "light", explicit: ["theme"] }, false, 39729),
+ onModeChange() {}, onToggleStartupSite() {}, onRemoveSite() {}, onLocalAccessPasswordChange() {}, onSubmit() {},
+ }));
+ const entries = [...html.matchAll(/href="([^"]+)"/g)]
+ .map(match => new URL(match[1]!.replaceAll("&amp;", "&")));
+ expect(entries).toHaveLength(2);
+ for (const entry of entries) {
+ expect(entry.searchParams.get("piik-client-port")).toBe("39729");
+ expect(entry.searchParams.get("piik-preferences")).toBe("theme");
+ const fragment = new URLSearchParams(entry.hash.slice(1));
+ expect(fragment.get("piik-client")).toBe("1");
+ expect(fragment.get("piik-lang")).toBe("en");
+ expect(fragment.get("piik-theme")).toBe("light");
+ }
+ expect(entries.map(entry => entry.origin)).toEqual([saved.sites[0], "http://127.0.0.1:8787"]);
+ });
+
+ it("selects only an HTTP(S) origin for explicit approval, including pasted full URLs", () => {
+ expect(siteEntryOrigin(" PIIK.example:443 ")).toBe("https://piik.example");
+ expect(siteEntryOrigin("https://piik.example/r/1234?lang=en#v=private-invitation")).toBe("https://piik.example");
+ expect(siteEntryOrigin("https://example.com/my room?q=hello world")).toBe("https://example.com");
+ expect(siteEntryOrigin("http://127.0.0.1:8787/")).toBe("http://127.0.0.1:8787");
+ for (const invalid of ["", "https://user:[email protected]", "javascript:alert(1)", "file:///local", "https://", "not a site"]) {
+ expect(siteEntryOrigin(invalid)).toBeNull();
+ }
+ });
+
+ it.each([true, false])("opens a manually entered Site only after explicit approval succeeds (manual: %s)", async manual => {
+ const fetch = vi.fn(async () => response());
+ const page = mount(fetch, manual);
+ await settle();
+ fetch.mockResolvedValueOnce({ ...response(), ok: false, status: 503 });
+ await page.approveSite("https://next.example");
+ expect(page.navigate).not.toHaveBeenCalled();
+ expect(page.state.SiteError).toBe("client.connect.saveFailed");
+ fetch.mockResolvedValueOnce(response({ ...saved, sites: [...saved.sites, "https://next.example"] }));
+ await page.approveSite("https://next.example");
+ if (manual) expect(page.navigate).toHaveBeenCalledWith("https://next.example/#piik-client=1");
+ else expect(page.navigate).not.toHaveBeenCalled();
+ expect(fetch.mock.calls[1]).toEqual(["/api/client-launcher/site", expect.objectContaining({
+ body: JSON.stringify({ site: "https://next.example", allowed: true }),
+ })]);
+ page.cleanup();
+ });
+
+ it("uses the App's available modes and keeps existing desktop defaults", async () => {
+ for (const modes of [undefined, ["local"]]) {
+ const page = mount(vi.fn(async () => response({ ...saved, ...(modes ? { modes } : {}) })));
+ await settle();
+ expect(page.state.Modes).toEqual(modes ?? ["local", "link"]);
+ expect(page.state.Mode).toBe("local");
+ page.cleanup();
+ }
+ });
+
it.each([true, false])("keeps approval pending until the optional startup preference settles (saved: %s)", async (savedStartup) => {
const nextSite = "https://next.example";
let finishStartup!: (value: ReturnType<typeof response>) => void;
diff --git a/tests/app-package-targets.test.ts b/tests/app-package-targets.test.ts
index e3932b7e..91203a82 100644
--- a/tests/app-package-targets.test.ts
+++ b/tests/app-package-targets.test.ts
@@ -2,10 +2,17 @@ import { describe, expect, it } from "vitest";
import {
APP_PACKAGE_TARGETS,
+ androidVersionCode,
goBuildEnvironment,
} from "../scripts/app-package-targets.mjs";
describe("App package targets", () => {
+ it("derives monotonically ordered Android codes from the product version", () => {
+ expect(["development", "v0.0.1", "v2.0.9", "v2.1.0", "v3.0.0", "v2100.0.0"].map(androidVersionCode))
+ .toEqual([1, 1, 2_000_009, 2_001_000, 3_000_000, 2_100_000_000]);
+ for (const version of ["v0.0.0", "v2.1000.0", "v2.0.1000", "v2100.0.1", "v2.0.0-rc.1"])
+ expect(() => androidVersionCode(version)).toThrow();
+ });
it("derives build and license discovery from one explicit cgo policy", () => {
const previous = process.env.CGO_ENABLED;
try {
diff --git a/tests/browser-debug.test.ts b/tests/browser-debug.test.ts
index 10025336..3b8e0a1e 100644
--- a/tests/browser-debug.test.ts
+++ b/tests/browser-debug.test.ts
@@ -20,6 +20,46 @@ afterEach(() => {
});
describe("opt-in Browser diagnostics", () => {
+ it("distinguishes Host input failure from a local stalled queue without exporting input authority or text", async () => {
+ browser();
+ const debug = await import("../src/client/lib/debug");
+ const { RemoteInputSession } = await import("../src/client/remote-control/input-session");
+ class Channel extends EventTarget {
+ readyState = "open";
+ bufferedAmount = 0;
+ sent: Record<string, unknown>[] = [];
+ send(data: string) { this.sent.push(JSON.parse(data)); }
+ close() { this.readyState = "closed"; }
+ receive(data: object) { this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify(data) })); }
+ }
+ const channel = new Channel();
+ const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
+ const grant = "b".repeat(64);
+ const activate = () => {
+ channel.receive({ platform: "windows", mode: "room", state: "ready" });
+ session.request();
+ const intent = channel.sent.at(-1)!.intent;
+ channel.receive({ platform: "windows", mode: "room", state: "active", grant, intent });
+ return intent;
+ };
+ const first = activate();
+ channel.receive({ platform: "windows", mode: "room", state: "failed", intent: first });
+ expect(session.state?.state).toBe("failed");
+ const second = activate();
+ channel.bufferedAmount = 8193;
+ session.input({ type: "text", text: "private input content" });
+ expect(session.state?.state).toBe("failed");
+ expect(channel.readyState).toBe("closed");
+ const exported = await debug.exportBrowserDebug();
+ const report = JSON.parse(exported);
+ expect(report.events).toEqual([
+ expect.objectContaining({ scope: "remote-input", event: "host-failed", details: { previousState: "active", mode: "room" } }),
+ expect.objectContaining({ scope: "remote-input", event: "send-failed", details: expect.objectContaining({ state: "active", readyState: "open", bufferedAmount: 8193 }) }),
+ ]);
+ for (const value of [grant, first, second, "private input content"]) expect(exported).not.toContain(value);
+ session.dispose();
+ });
+
it("collects current preview receive and playback evidence only at export, until owner retirement", async () => {
browser();
const debug = await import("../src/client/lib/debug");
diff --git a/tests/camera-previews.test.ts b/tests/camera-previews.test.ts
index b4d68441..70fc14f8 100644
--- a/tests/camera-previews.test.ts
+++ b/tests/camera-previews.test.ts
@@ -7,7 +7,8 @@ function setup({ permission = true, frames = true } = {}) {
const devices = ["front", "rear"].map(id => ({ kind: "videoinput", deviceId: id, label: `${id} camera` }));
const enumerateDevices = vi.fn(async () => permission ? devices : []);
const stream = (id: string) => {
- const track = { id, kind: "video", label: `${id} camera`, readyState: "live",
+ const events = new EventTarget();
+ const track = { addEventListener: events.addEventListener.bind(events), removeEventListener: events.removeEventListener.bind(events), id, kind: "video", label: `${id} camera`, readyState: "live",
getSettings: () => ({ deviceId: id }), stop: vi.fn(() => { track.readyState = "ended"; }) };
tracks.push(track);
return { getTracks: () => [track], getVideoTracks: () => [track] } as unknown as MediaStream;
diff --git a/tests/client-launch-presentation.test.ts b/tests/client-launch-presentation.test.ts
index 5aa39c3b..50704d4f 100644
--- a/tests/client-launch-presentation.test.ts
+++ b/tests/client-launch-presentation.test.ts
@@ -116,11 +116,12 @@ describe("App presentation handoff authority", () => {
const grant = `${"a".repeat(21)}A`;
const target = new URL(clientLaunchURL(`https://site.example/r/9527?debug=1#piik-client=1&v=${grant}`, {
lang: "zh", vis: true, theme: "dark", explicit: ["copy", "theme"],
- }));
+ }, undefined, 39730));
const oldFragment = new URLSearchParams(target.hash.slice(1));
expect(oldFragment.get("piik-lang")).toBe("zh");
expect(oldFragment.get("piik-mode")).toBe("vis");
expect(oldFragment.get("piik-theme")).toBe("dark");
+ expect(target.searchParams.get("piik-client-port")).toBe("39730");
// The released reader removes only these keys before its exact #v= parser.
for (const key of ["piik-client", "client-access", "piik-lang", "piik-mode", "piik-theme"]) oldFragment.delete(key);
expect(oldFragment.toString()).toBe(`v=${grant}`);
@@ -129,4 +130,28 @@ describe("App presentation handoff authority", () => {
expect(env.page.location.search).toBe("?debug=1");
expect(readViewerRoute()).toEqual({ roomId: "9527", viewerGrant: grant });
});
+
+ it.each(["39720", "39731", "NaN", "39721.5"])("ignores out-of-contract runtime ports: %s", async port => {
+ const env = browser();
+ env.page.location = new URL(`https://site.example/?piik-client-port=${port}#piik-client=1`);
+ const { takeClientLaunchBootstrap, clientRuntimePort } = await import("../src/client/lib/session");
+ expect(takeClientLaunchBootstrap().launchedByClient).toBe(true);
+ expect(clientRuntimePort()).toBeNull();
+ expect(env.page.location.href).toBe("https://site.example/");
+ expect(env.values.get("piik:client-launch:v1")).toBe("1");
+ });
+
+ it("keeps an inline App selection when browser storage is unavailable", async () => {
+ const env = browser([], true);
+ Object.assign(env.page, { sessionStorage: {
+ getItem: () => { throw new Error("blocked"); },
+ setItem: () => { throw new Error("blocked"); },
+ } });
+ env.page.location = new URL("https://site.example/?piik-client-port=39730#piik-client=1");
+ const { takeClientLaunchBootstrap, clientRuntimePort, isClientActivated } = await import("../src/client/lib/session");
+ takeClientLaunchBootstrap();
+ expect(clientRuntimePort()).toBe(39730);
+ expect(isClientActivated()).toBe(true);
+ expect(env.page.location.href).toBe("https://site.example/");
+ });
});
diff --git a/tests/container-web-identity.test.ts b/tests/container-web-identity.test.ts
new file mode 100644
index 00000000..031986f7
--- /dev/null
+++ b/tests/container-web-identity.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it, vi } from "vitest";
+import { checkWebSourceIdentity } from "../scripts/container-web-identity.mjs";
+
+const revision = "a".repeat(40);
+const main = "/assets/index-current.js";
+const shared = "/assets/session-current.js";
+const html = `<script type="module" crossorigin src="${main}"></script>
+<link rel="modulepreload" crossorigin href="${shared}">`;
+
+describe("container Web source identity", () => {
+ it.each([main, shared])("checks the served revision in %s", async identityAsset => {
+ const read = vi.fn(async (asset: string) => asset === identityAsset ? `const revision="${revision}"` : "export{};");
+ await checkWebSourceIdentity(html, revision, read);
+ expect(read.mock.calls.map(([asset]) => asset)).toEqual([main, shared]);
+ });
+
+ it("rejects a different revision even when an unrelated asset has the expected one", async () => {
+ const assets = new Map([[main, "export{};"], [shared, `const revision="${"b".repeat(40)}"`],
+ ["/assets/orphan.js", `const revision="${revision}"`]]);
+ await expect(checkWebSourceIdentity(html, revision, async (asset: string) => assets.get(asset)!))
+ .rejects.toThrow("Web and Server must have the same source identity");
+ });
+
+ it("rejects a missing declared module even when the entry has the right revision", async () => {
+ await expect(checkWebSourceIdentity(html, revision, async (asset: string) => {
+ if (asset === shared) throw new Error("HTTP 404");
+ return revision;
+ })).rejects.toThrow("HTTP 404");
+ });
+
+ it("requires the served Web entry", async () => {
+ const read = vi.fn();
+ await expect(checkWebSourceIdentity(`<link rel="modulepreload" href="${shared}">`, revision, read))
+ .rejects.toThrow("Server must embed the Web UI");
+ expect(read).not.toHaveBeenCalled();
+ });
+});
diff --git a/tests/host-page-notice.test.ts b/tests/host-page-notice.test.ts
index 0e27d8c1..2bb3a485 100644
--- a/tests/host-page-notice.test.ts
+++ b/tests/host-page-notice.test.ts
@@ -8,9 +8,9 @@ import {
hostFailureCode,
hostFailureChecks,
hostServerErrorKey,
- shouldPauseLocalPreview,
sourceSwitchNotice,
} from "../src/client/pages/host-page-notices.ts";
+import { shouldPauseLocalPreview } from "../src/client/components/living/use-host-preview-playback";
setCopy({ lang: "zh" });
@@ -124,6 +124,8 @@ describe("shouldPauseLocalPreview", () => {
expect(shouldPauseLocalPreview("visible", false)).toBe(true);
expect(shouldPauseLocalPreview("visible", true)).toBe(false);
expect(shouldPauseLocalPreview("hidden", false, true)).toBe(false);
+ expect(shouldPauseLocalPreview("visible", false, false, false)).toBe(false);
+ expect(shouldPauseLocalPreview("hidden", false, false, false)).toBe(false);
});
});
diff --git a/tests/host-peer-tracks.test.ts b/tests/host-peer-tracks.test.ts
index 0d75e9f3..9f1532af 100644
--- a/tests/host-peer-tracks.test.ts
+++ b/tests/host-peer-tracks.test.ts
@@ -1293,6 +1293,40 @@ describe("HostPeer source replacement", () => {
peer.dispose();
});
+ it("keeps the negotiated edge after an audio ceiling rejection and retries the same explicit profile", async () => {
+ const video = createTrack("video", "video"), sourceAudio = createTrack("audio", "audio");
+ const profile = { ...QUALITY_PROFILES["720p30"], maxAudioBitrate: 320_000 };
+ const peer = createPeer(createStream(video, sourceAudio), undefined, undefined, profile);
+ try {
+ await peer.start();
+ const connection = FakePeerConnection.latest!;
+ const videoCalls = connection.senders[0]!.setParameters.mock.calls.length;
+ const audio = connection.senders[1]!;
+ const previous = peer.getSnapshot().audioSenderParameters;
+ audio.getParameters().codecs = [{ mimeType: "audio/opus", clockRate: 48000, payloadType: 111,
+ sdpFmtpLine: "stereo=1;maxaveragebitrate=192000" }];
+ audio.failNextSetParameters = true;
+ await peer.acceptSignal({ kind: "description", connectionId: peer.connectionId,
+ description: { type: "answer", sdp: "test-answer" } });
+ await vi.waitFor(() => expect(audio.setParameters).toHaveBeenCalledTimes(2));
+ expect(connection.connectionState).not.toBe("closed");
+ expect(connection.senders[0]!.setParameters).toHaveBeenCalledTimes(videoCalls);
+ expect(audio.getParameters().encodings[0]?.maxBitrate).toBe(320_000);
+ expect(peer.getSnapshot().audioSenderParameters).toEqual(previous);
+ expect(video.stop).not.toHaveBeenCalled();
+ expect(sourceAudio.stop).not.toHaveBeenCalled();
+ expect(connection.senders[0]!.track!.stop).not.toHaveBeenCalled();
+
+ connection.connectionState = "connected";
+ await expect(peer.updateProfile(profile)).resolves.toBe(true);
+ expect(audio.getParameters().encodings[0]?.maxBitrate).toBe(192_000);
+ expect(peer.getSnapshot().audioSenderParameters).toMatchObject({
+ requestedMaxBitrate: 320_000, appliedMaxBitrate: 192_000, mismatch: true,
+ });
+ expect(connection.connectionState).toBe("connected");
+ } finally { peer.dispose(); }
+ });
+
it("updates quality parameters without replacing media tracks", async () => {
const video = createTrack("video", "video");
const audio = createTrack("audio", "audio");
diff --git a/tests/host-quality-ownership.test.ts b/tests/host-quality-ownership.test.ts
index 964d1ad4..59ee463c 100644
--- a/tests/host-quality-ownership.test.ts
+++ b/tests/host-quality-ownership.test.ts
@@ -1,3 +1,4 @@
+import { HostAudio } from "../src/client/media/host-audio";
import { readFileSync } from "node:fs";
import { createContext, runInContext } from "node:vm";
import ts from "typescript";
@@ -8,8 +9,10 @@ import {
type QualitySettings,
} from "../src/client/media/quality";
import { NativeSenderPeer } from "../src/client/native/native-sender-peer";
+import { HostPeer } from "../src/client/webrtc/host-peer";
import { NativeCompatibilityError, NativeRequestError, type NativeClient } from "../src/client/native/client";
import { NativeClientConnection } from "../src/client/native/client-connection";
+import { androidAppIntent } from "../src/client/native/activation";
import { nativeCaptureTargetKey } from "../src/client/native/capture-selection";
import { nativeAppDetected, type NativeSourceList } from "../src/client/components/living/CaptureSourcePicker";
import { NativeMediaBridgeError } from "../src/client/native/media-bridge";
@@ -25,16 +28,17 @@ const source = ts.createSourceFile("HostPage.tsx", readFileSync(
new URL("../src/client/pages/HostPage.tsx", import.meta.url), "utf8",
), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
const owners = new Set(["changeQuality", "commitQuality", "rememberQuality", "loadAppQualityPreference", "handleSignalMessage",
- "switchSource", "watchCaptureEnd", "switchNativeSource", "finishSourceSwitch", "replaceBrowserStream", "recoverBrowserFanout", "disposeNativeShare",
+ "switchSource", "watchCaptureEnd", "switchNativeSource", "switchToNativeSource", "createBrowserAudio", "retireNativeSenderPeers", "finishSourceSwitch", "replaceBrowserStream", "recoverBrowserFanout", "disposeNativeShare",
"acquireNativeClient", "requestSharing", "startNativeShare", "startBrowserNativeIngress",
"connectNativeClient", "handleNativeClientClosed", "releaseUnusedNativeClient", "closeCaptureSourcePicker",
"openCaptureSourcePicker", "startBrowserShareFromPicker", "startNativeShareFromPicker",
"startSharing", "connectRoomSignal", "createReplacementRoom", "beginRoomMutation", "finishRoomMutation", "setCaptureError", "toggleSharingPause",
"startPeer", "reconcileHostChildren", "setNotice", "setNoticeKey", "setStatusNotice", "setNoticeError", "setNoticeErrorKey", "endSharing", "disposeResources", "hostTerminationKey", "copyRoomLink", "isCurrentRoomAuthority",
- "handleRoomAccessFailure", "replaceCurrentRoom", "forgetRoom", "endRoomSession", "readableError"]);
+ "handleRoomAccessFailure", "replaceCurrentRoom", "forgetRoom", "endRoomSession", "readableError", "confirmNativePermission"]);
const functions: string[] = [];
let noticeRendering = "";
+const nativePermissionRendering: string[] = [];
function collect(node: ts.Node): void {
if (ts.isFunctionDeclaration(node) && node.name && owners.has(node.name.text)) {
functions.push(node.getText(source));
@@ -43,6 +47,9 @@ function collect(node: ts.Node): void {
if (ts.isVariableDeclaration(node) && node.name.getText(source) === "noticeText") {
noticeRendering = node.initializer!.getText(source);
}
+ if (ts.isVariableDeclaration(node) && ["nativePermissionIntent", "confirmNativeCapture"].includes(node.name.getText(source))) {
+ nativePermissionRendering.push(`const ${node.getText(source)};`);
+ }
ts.forEachChild(node, collect);
}
collect(source);
@@ -68,7 +75,7 @@ function fixture(launchedByClient = true, nativeConnected = true) {
let physical: QualitySettings = original;
const track = { stop: vi.fn() };
const stream = { getTracks: () => [track] };
- const bridge = { start: vi.fn(async (_audio: boolean) => stream), dispose: vi.fn() };
+ const bridge = { start: vi.fn(async (_audio: boolean) => stream), dispose: vi.fn(), getRemoteInput: () => null };
const peer = { updateCaptureProfile: vi.fn(async () => true), updateProfile: vi.fn(async () => true),
isConnected: vi.fn(() => true) };
const client = { updateShare: vi.fn(async () => undefined), replaceShareSource: vi.fn(async () => undefined),
@@ -99,6 +106,9 @@ function fixture(launchedByClient = true, nativeConnected = true) {
readPreferredRoomId: () => null, disposeResources: vi.fn(), ApiError,
NativeMediaBridge: vi.fn(function (_shareId: string, _client: unknown, _onFailed: () => void) { return bridge; }), manualVideoCodecPreference: vi.fn(),
phase: "live", qualitySettingsRef: ref<QualitySettings>(original), advancedQualityRef: ref<QualitySettings>(original),
+ nativeActive: false, switchingSource: false, sourceSwitchState: null, shareVolume: { level: 1, muted: false },
+ androidAppIntent: (action: unknown) => androidAppIntent(action, "Android"),
+ window: { location: { assign: vi.fn() } },
qualityChangeRef: ref<object | null>(null), pendingQualityChangeRef: ref<QualitySettings | null>(null),
appQualityPreferenceRef: ref<{ save: (quality: QualitySettings) => void } | null>(null),
qualityPreferenceEditedRef: ref(false), pendingAppQualityPreferenceRef: ref<QualitySettings | null>(null),
@@ -124,8 +134,8 @@ function fixture(launchedByClient = true, nativeConnected = true) {
qualitySettingsEqual, qualityResolutionEqual, videoQualitySettingsEqual, screenAudioBitrate, qualitySettingsLabel,
supportsCaptureProfile, videoLimits: videoCaptureLimits(), audioBitrateCeiling: MAX_AUDIO_SEND_BITRATE,
say: (key: string) => key, syncHostSfuQualityWarning: () => null,
- NativeSenderPeer, discardPreparedHostChild: vi.fn(), removePeer: vi.fn(), startPeer: vi.fn(async () => undefined),
- nativePreviewTailRef: ref(Promise.resolve()), setSwitchingSource: vi.fn(),
+ NativeSenderPeer, discardPreparedHostChild: vi.fn(), removePeer: vi.fn(), startPeer: vi.fn(async (_peerId: string, _generation: number): Promise<void> => {}),
+ nativePreviewTailRef: ref(Promise.resolve()), setSourceSwitch: vi.fn(),
invalidateSenderQualityEvidence: vi.fn(), routePolicyRef: ref({ topologyOptimization: false }),
sourceSwitchNotice: () => "source-switch-result", hostPeerIdRef: ref(null), endpointMediaCopyCapacityRef: ref(2),
creationProfileRef: ref({ roomPassword: null }), saveCreationProfile: vi.fn(), setCreationProfile: vi.fn(),
@@ -140,6 +150,8 @@ function fixture(launchedByClient = true, nativeConnected = true) {
state.activeGenerationRef.current === generation && state.shareGenerationRef.current === share,
});
runInContext(executable, context);
+ state.setNativeActive.mockImplementation((value: boolean) => { context.nativeActive = value; });
+ state.setSourceSwitch.mockImplementation((value: unknown) => { context.sourceSwitchState = value; context.switchingSource = value !== null; });
if (nativeConnected) state.nativeConnection.adopt(client as unknown as NativeClient);
const start = context.startSharing;
const startChild = context.startPeer;
@@ -161,6 +173,7 @@ function fixture(launchedByClient = true, nativeConnected = true) {
startBrowser: () => start({ kind: "browser" }) as Promise<void>,
writeNoticeError,
renderNotice: (noticeValue: unknown) => runInContext(noticeRendering, createContext({ noticeValue, t: say })),
+ renderNativePermission: () => runInContext(`(() => { ${nativePermissionRendering.join("\n")} return confirmNativeCapture; })()`, context),
discover: context.acquireNativeClient as () => Promise<typeof client | null>,
requestShare: context.requestSharing as () => void,
startNative: () => context.startNativeShare(1, "share", { client, target: {}, audio: false, path: {} }) as Promise<unknown>,
@@ -254,6 +267,181 @@ describe("Native sharing pause ownership", () => {
});
describe("Camera replacement ownership", () => {
+ function sourceFixture(native: boolean) {
+ const current = fixture();
+ class Track extends EventTarget {
+ readyState = "live";
+ enabled = true;
+ stop = vi.fn(() => { this.readyState = "ended"; });
+ getSettings = () => ({ width: 1280, height: 720 });
+ }
+ const picture = (sound = false) => {
+ const video = new Track();
+ const audio = sound ? new Track() : null;
+ return { video, getTracks: () => audio ? [video, audio] : [video], getVideoTracks: () => [video], getAudioTracks: () => audio ? [audio] : [] };
+ };
+ const previous = picture(), camera = picture(), nativeStream = picture(true);
+ const oldBridge = { stream: previous, dispose: vi.fn(() => previous.video.stop()) };
+ const peer = Object.assign(Object.create(HostPeer.prototype), { replaceStream: vi.fn(async () => true), dispose: vi.fn() });
+ const nativePeer = Object.assign(Object.create(NativeSenderPeer.prototype), { dispose: vi.fn() });
+ const startedPeerStreams: unknown[] = [];
+ const replacePublication = vi.fn(async () => true);
+ current.peersRef.current.clear();
+ current.peersRef.current.set("viewer", native ? nativePeer : peer as never);
+ current.removePeer.mockImplementation(id => {
+ const retired = current.peersRef.current.get(id) as unknown as { dispose?: () => void };
+ current.peersRef.current.delete(id);
+ retired?.dispose?.();
+ });
+ current.startPeer.mockImplementation(async id => {
+ startedPeerStreams.push(current.context.streamRef.current);
+ current.peersRef.current.set(id, peer as never);
+ });
+ Object.assign(current.context, {
+ HostAudio, streamRef: ref(previous), retiringStreamRef: ref(null), sharingPausedRef: ref(false),
+ hostAudioRef: ref(native ? null : new HostAudio(previous as unknown as MediaStream, "camera")),
+ nativeMediaBridgeRef: ref(native ? oldBridge : null), setCommentary: vi.fn(), setCameraDevice: vi.fn(),
+ setStream: vi.fn(), setResolvedVideoCodec: vi.fn(), setMediaPaused: vi.fn(),
+ captureBrowserSource: vi.fn(async () => camera), endSharing: vi.fn(),
+ createOpaqueId: () => "native-source-2", nativeSources: { kind: "ready" },
+ hostSfuRouteRef: ref({ replaceStream: replacePublication }),
+ iceConfigRef: ref({ iceServers: [] }), hostChildIsAssigned: () => true,
+ acceptHostPeerUpdate: vi.fn(), reportHostChildFailure: vi.fn(),
+ });
+ const nativeControl = {
+ prepareEdge: vi.fn(async () => ({ type: "offer", sdp: "native encoded offer" })),
+ closeEdge: vi.fn(async () => {}),
+ setRemoteInputPeers: vi.fn(async () => {
+ current.client.onEvent.mock.calls[0]![0](remoteSnapshot);
+ }),
+ };
+ const remoteSnapshot = { version: 10, type: "remote-control-state", shareId: "native-source-2", mode: "ask", platform: "windows", requests: [], coPlay: false };
+ Object.assign(current.client, nativeControl);
+ current.signalRef.current.send.mockReturnValue(true);
+ current.nativeModeRef.current = native;
+ current.context.nativeActive = native;
+ current.nativeShareGenerationRef.current = native ? "native-source-1" : null;
+ current.nativeSourcePathRef.current = { adapterIndex: 0, encoderIndex: 0 };
+ current.manualVideoCodecPreference.mockImplementation(codec => ({ primary: codec }));
+ current.bridge.start.mockResolvedValue(nativeStream);
+ Object.assign(current.bridge, { stream: nativeStream });
+ Object.assign(current.client.health, { activationURL: "piik://media" });
+ return { ...current, nativeControl, remoteSnapshot, picture, previous, camera, nativeStream, oldBridge, peer, nativePeer, startedPeerStreams, replacePublication };
+ }
+
+ it.each(["NotAllowedError", "NotReadableError"])("keeps native capture and its room when Camera fails with %s", async name => {
+ const current = sourceFixture(true);
+ const permission = deferred<never>();
+ current.context.captureBrowserSource.mockReturnValue(permission.promise);
+ current.context.startBrowserShareFromPicker("camera", "front");
+ expect(current.context.captureBrowserSource).toHaveBeenCalledExactlyOnceWith(original, "camera", "front");
+ expect(current.client.stopShare).not.toHaveBeenCalled();
+ permission.reject(new DOMException("camera rejected", name));
+ await vi.waitFor(() => expect(current.sourceSwitchRef.current).toBeNull());
+ expect(current.context.streamRef.current).toBe(current.previous);
+ expect(current.nativeModeRef.current).toBe(true);
+ expect(current.oldBridge.dispose).not.toHaveBeenCalled();
+ expect(current.createRoom).not.toHaveBeenCalled();
+ expect(current.context.endSharing).not.toHaveBeenCalled();
+ });
+
+ it("switches native → Camera → native through the picker handlers in one live room", async () => {
+ const current = sourceFixture(true);
+ const roomSignal = current.signalRef.current;
+ current.context.startBrowserShareFromPicker("camera", "front");
+ await vi.waitFor(() => expect(current.sourceSwitchRef.current).toBeNull());
+ expect(current.context.streamRef.current).toBe(current.camera);
+ expect(current.client.stopShare).toHaveBeenCalledExactlyOnceWith("native-source-1");
+ expect(current.previous.video.stop).toHaveBeenCalled();
+ expect(current.nativeModeRef.current).toBe(false);
+ expect(current.nativePeer.dispose).toHaveBeenCalledOnce();
+ expect(current.startedPeerStreams).toEqual([current.camera]);
+ expect(current.replacePublication).toHaveBeenCalledWith(current.camera, true);
+ expect(current.context.hostAudioRef.current).toBeInstanceOf(HostAudio);
+ expect(current.context.endSharing).not.toHaveBeenCalled();
+
+ // Revisiting the picker reacquires its control owner; the Browser camera
+ // remains the publication until App ACK and the required bridge both finish.
+ current.nativeConnection.adopt(current.client as unknown as NativeClient);
+ current.context.startPeer = current.startChild;
+ Object.assign(current.client.health.nativeMedia, { remoteInput: true });
+ current.setRemoteControlState.mockClear();
+ current.nativeSourcePathRef.current = { adapterIndex: 0, encoderIndex: 0 };
+ const ack = deferred<{ audio: boolean; codec: "h264" }>();
+ const bridge = deferred<typeof current.nativeStream>();
+ current.client.startShare.mockReturnValue(ack.promise);
+ current.bridge.start.mockReturnValue(bridge.promise);
+ current.context.startNativeShareFromPicker({ kind: "picker", sourceId: "1", title: "System" }, true, false);
+ await vi.waitFor(() => expect(current.client.startShare).toHaveBeenCalledOnce());
+ expect(current.renderNativePermission()).toBe(true);
+ expect(current.context.streamRef.current).toBe(current.camera);
+ ack.resolve({ audio: true, codec: "h264" });
+ await vi.waitFor(() => expect(current.bridge.start).toHaveBeenCalledOnce());
+ expect(current.renderNativePermission()).toBe(false);
+ expect(current.context.streamRef.current).toBe(current.camera);
+ expect(current.nativeModeRef.current).toBe(false);
+ expect(current.peersRef.current.get("viewer")).toBe(current.peer);
+ expect(current.peer.dispose).not.toHaveBeenCalled();
+ expect(current.setRemoteControlState).not.toHaveBeenCalledWith(current.remoteSnapshot);
+ expect(current.camera.video.stop).not.toHaveBeenCalled();
+ bridge.resolve(current.nativeStream);
+ await vi.waitFor(() => expect(current.sourceSwitchRef.current).toBeNull());
+ expect(current.context.streamRef.current).toBe(current.nativeStream);
+ expect(current.nativeModeRef.current).toBe(true);
+ expect(current.setRemoteControlState).toHaveBeenCalledWith(current.remoteSnapshot);
+ expect(current.peersRef.current.get("viewer")).toBeInstanceOf(NativeSenderPeer);
+ expect(current.peer.dispose).toHaveBeenCalledOnce();
+ expect(current.peer.replaceStream).not.toHaveBeenCalledWith(current.nativeStream);
+ expect(current.nativeControl.prepareEdge).toHaveBeenCalledWith(
+ "native-source-2", expect.any(String), { iceServers: [] }, undefined, false,
+ );
+ expect(current.replacePublication).toHaveBeenLastCalledWith(current.nativeStream, true);
+ expect(current.audioAvailableRef.current).toBe(true);
+ expect(current.nativeShareGenerationRef.current).toBe("native-source-2");
+ expect(current.shareGenerationRef.current).toBe("share");
+ expect(current.activeGenerationRef.current).toBe(1);
+ expect(current.signalRef.current).toBe(roomSignal);
+ expect(current.camera.video.stop).toHaveBeenCalled();
+ expect(current.createRoom).not.toHaveBeenCalled();
+ expect(current.disposeResources).not.toHaveBeenCalled();
+ expect(current.context.endSharing).not.toHaveBeenCalled();
+ const nativeEvents = current.client.onEvent.mock.calls[0]![0];
+ nativeEvents({ type: "share-ended", shareId: "native-source-1", failed: true });
+ expect(current.context.endSharing).not.toHaveBeenCalled();
+ nativeEvents({ type: "share-ended", shareId: "native-source-2", failed: true });
+ expect(current.context.endSharing).toHaveBeenCalledOnce();
+ });
+
+ it.each(["permission", "bridge"])("retains Camera picture, audio and codec after native %s fails", async stage => {
+ const current = sourceFixture(false);
+ const previousCodec = current.videoCodecRef.current;
+ current.audioAvailableRef.current = true;
+ Object.assign(current.client.health.nativeMedia, { remoteInput: true });
+ const failure = new NativeRequestError("rejected", "start-share");
+ if (stage === "permission") current.client.startShare.mockRejectedValue(failure);
+ else current.bridge.start.mockImplementation(async () => {
+ // Exercise the real bridge-failure callback as well as its rejected wait.
+ current.NativeMediaBridge.mock.calls.at(-1)![2]();
+ throw new NativeMediaBridgeError("local preview failed");
+ });
+ await current.context.switchToNativeSource({ kind: "native", client: current.client,
+ target: { kind: "picker", sourceId: "1", title: "System" }, audio: false, path: {} });
+ expect(current.context.streamRef.current).toBe(current.previous);
+ expect(current.previous.video.stop).not.toHaveBeenCalled();
+ expect(current.audioAvailableRef.current).toBe(true);
+ expect(current.videoCodecRef.current).toBe(previousCodec);
+ expect(current.nativeModeRef.current).toBe(false);
+ expect(current.context.hostAudioRef.current).toBeInstanceOf(HostAudio);
+ expect(current.context.endSharing).not.toHaveBeenCalled();
+ expect(current.createRoom).not.toHaveBeenCalled();
+ expect(current.peer.replaceStream).not.toHaveBeenCalled();
+ expect(current.peersRef.current.get("viewer")).toBe(current.peer);
+ expect(current.peer.dispose).not.toHaveBeenCalled();
+ expect(current.setRemoteControlState).not.toHaveBeenCalledWith(current.remoteSnapshot);
+ expect(current.sourceSwitchRef.current).toBeNull();
+ expect(current.setNoticeValue).toHaveBeenLastCalledWith(expect.objectContaining({ target: "operation" }));
+ });
+
it.each([true, false])("does not apply a retired ingress replacement to a new share: %s", async result => {
const current = fixture();
const pending = deferred<boolean>();
@@ -846,7 +1034,7 @@ describe("Host quality ownership", () => {
expect(selection).toMatchObject({ kind: "native", target, audio: false, showCaptureBorder: true });
current.client.startShare.mockRejectedValue(new Error("capture unavailable"));
await expect(current.context.startNativeShare(1, "share", selection)).rejects.toThrow("capture unavailable");
- expect(current.client.startShare).toHaveBeenCalledWith(expect.objectContaining({ showCaptureBorder: true }));
+ expect(current.client.startShare).toHaveBeenCalledWith(expect.objectContaining({ showCaptureBorder: true }), undefined);
}
});
@@ -1030,6 +1218,30 @@ describe("Host quality ownership", () => {
}
});
+ it("offers Android confirmation for pending native capture, then keeps frame waiting passive", async () => {
+ const current = fixture();
+ Object.assign(current.client.health, { activationURL: "piik://media" });
+ current.context.phase = "starting";
+ current.nativeShareGenerationRef.current = null;
+ const started = deferred<{ audio: boolean; codec: "h264" }>();
+ const frames = deferred<Awaited<ReturnType<typeof current.bridge.start>>>();
+ current.client.startShare.mockReturnValue(started.promise);
+ current.bridge.start.mockReturnValue(frames.promise);
+ const starting = current.startNative();
+ await vi.waitFor(() => expect(current.client.startShare).toHaveBeenCalledOnce());
+ expect(current.renderNativePermission()).toBe(true);
+ current.context.confirmNativePermission();
+ expect(current.window.location.assign).toHaveBeenCalledWith("intent://media#Intent;scheme=piik;package=tv.piik.android;end");
+ started.resolve({ audio: false, codec: "h264" });
+ await vi.waitFor(() => expect(current.bridge.start).toHaveBeenCalledOnce());
+ expect(current.renderNativePermission()).toBe(false);
+ frames.resolve(current.streamRef.current!);
+ await starting;
+ current.activeGenerationRef.current = null;
+ current.context.confirmNativePermission();
+ expect(current.window.location.assign).toHaveBeenCalledOnce();
+ });
+
it.each(["camera", "browser"])("retains the %s capture cause when presenting its notice", (source) => {
const current = fixture();
const report = vi.fn();
@@ -1193,15 +1405,16 @@ describe("Host quality ownership", () => {
current.client.sources.mockReturnValueOnce(enumeration.promise);
const refresh = current.openPicker();
await vi.waitFor(() => expect(current.client.sources).toHaveBeenCalledTimes(2));
- expect(sources).toEqual({ kind: "loading", appDetected: true });
+ expect(sources).toEqual({ kind: "loading", appDetected: true, sources: [] });
enumeration.resolve([]);
await refresh;
expect(sources).toMatchObject({ kind: "ready", sources: [] });
current.context.phase = "live";
await current.openPicker();
- expect(current.setNativeSources).toHaveBeenLastCalledWith({ kind: "browser" });
- expect(current.client.sources).toHaveBeenCalledTimes(2);
+ expect(current.setNativeSources).toHaveBeenLastCalledWith(expect.objectContaining({ kind: "ready" }));
+ expect(current.client.sources).toHaveBeenCalledTimes(3);
+ expect(current.streamRef.current).not.toBeNull();
});
it("shows App incompatibility in the picker and clears it on a successful refresh", async () => {
@@ -1361,6 +1574,7 @@ describe("Host quality ownership", () => {
sourceStream = captured;
start = async () => captured;
dispose = vi.fn();
+ setVolume = vi.fn();
}, setStream: vi.fn(), watchCaptureEnd: vi.fn(),
resolveStreamVideoCodec, startBrowserNativeIngress, setResolvedVideoCodec,
});
diff --git a/tests/native-activation.test.ts b/tests/native-activation.test.ts
new file mode 100644
index 00000000..0777f245
--- /dev/null
+++ b/tests/native-activation.test.ts
@@ -0,0 +1,51 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { activateNativePicker, androidAppIntent } from "../src/client/native/activation";
+import { nativeHealthSchema, NATIVE_CLIENT_PROTOCOL } from "../src/client/native/wire";
+import { NATIVE_CLIENT_PORT_START } from "../src/client/native/discovery-ports";
+
+describe("Android App foreground actions", () => {
+ afterEach(() => vi.unstubAllGlobals());
+ const android = "Mozilla/5.0 (Linux; Android 16) Chrome/150 Mobile";
+ it.each([true, false, undefined])("hands off only while the original gesture is active: %s", isActive => {
+ const assign = vi.fn();
+ vi.stubGlobal("navigator", { userAgent: android, userActivation: isActive === undefined ? undefined : { isActive } });
+ vi.stubGlobal("window", { location: { assign } });
+ activateNativePicker(undefined);
+ expect(assign).not.toHaveBeenCalled();
+ activateNativePicker("piik://media");
+ expect(assign).toHaveBeenCalledTimes(isActive ? 1 : 0);
+ if (isActive) {
+ expect(assign).toHaveBeenCalledWith("intent://media#Intent;scheme=piik;package=tv.piik.android;end");
+ assign.mockImplementation(() => { throw new DOMException("Blocked", "SecurityError"); });
+ expect(() => activateNativePicker("piik://media")).not.toThrow();
+ }
+ });
+ it("allows only fixed Piik actions in its explicit Android package", () => {
+ expect(androidAppIntent("piik://app", android)).toBe("intent://app#Intent;scheme=piik;package=tv.piik.android;end");
+ expect(androidAppIntent("piik://media", android)).toBe("intent://media#Intent;scheme=piik;package=tv.piik.android;end");
+ for (const action of [undefined, null, "https://piik.tv", "piik://media?approve=true", "piik://media#token=secret",
+ "piik://user@media", "piik://media/start", "intent://media#Intent;package=other;end"]) {
+ expect(androidAppIntent(action, android)).toBeNull();
+ }
+ expect(androidAppIntent("piik://app", "Mozilla/5.0 (Windows NT 10.0)")).toBeNull();
+ expect(androidAppIntent("piik://app", "Mozilla/5.0 (iPhone)")).toBeNull();
+ });
+
+ it("uses the connected App's media action in Android desktop mode", () => {
+ const desktop = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/150.0.0.0 Safari/537.36";
+ const assign = vi.fn();
+ vi.stubGlobal("navigator", { userAgent: desktop, userActivation: { isActive: true } });
+ vi.stubGlobal("window", { location: { assign } });
+ activateNativePicker("piik://media");
+ expect(assign).toHaveBeenCalledWith("intent://media#Intent;scheme=piik;package=tv.piik.android;end");
+ expect(androidAppIntent("piik://app", desktop)).toBeNull();
+ });
+
+ it("keeps activation optional and validates the known health extension", () => {
+ const health = { protocol: NATIVE_CLIENT_PROTOCOL, service: "piik-client", port: NATIVE_CLIENT_PORT_START,
+ instanceToken: "a".repeat(43), nativeMedia: {} };
+ expect(nativeHealthSchema.parse(health).activationURL).toBeUndefined();
+ expect(nativeHealthSchema.parse({ ...health, activationURL: "piik://media" }).activationURL).toBe("piik://media");
+ expect(nativeHealthSchema.safeParse({ ...health, activationURL: "piik://app" }).success).toBe(false);
+ });
+});
diff --git a/tests/native-capture-selection.test.ts b/tests/native-capture-selection.test.ts
index 5e5c27ac..c35b9d50 100644
--- a/tests/native-capture-selection.test.ts
+++ b/tests/native-capture-selection.test.ts
@@ -33,6 +33,60 @@ describe("native capture source selection", () => {
const game = target("My Game", "1");
afterEach(() => setCopy({ lang: "en", vis: true }));
+ it("shows one system chooser for picker-only Apps instead of duplicate desktop tabs", () => {
+ setCopy({ lang: "en", vis: false });
+ const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ nativeSources: { kind: "ready", sources: [{ kind: "picker", sourceId: "1", title: "Choose on device" }], processAudio: false, systemAudio: true },
+ onBrowser() {}, onNative() {}, onCamera() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
+ }));
+ expect(html).toMatch(/data-source-tab="picker"[^>]*aria-selected="true"/);
+ expect(html).not.toContain('data-source-tab="window"');
+ expect(html).not.toContain('data-source-tab="display"');
+ expect(html).toContain('data-source-tab="browser"');
+ expect(html).toContain('data-source-tab="camera"');
+ expect(html).toContain(t("en", "host.sourcePicker.systemAction"));
+ expect(html).toContain('class="lr-source-option is-action"');
+ expect(html).not.toContain('class="lr-source-option-preview"');
+ });
+
+ it("omits unsupported Browser capture while keeping the system chooser and Camera", () => {
+ const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ nativeSources: { kind: "ready", sources: [{ kind: "picker", sourceId: "1", title: "Choose on device" }], processAudio: false, systemAudio: true },
+ browserSupported: false,
+ onBrowser() {}, onNative() {}, onCamera() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
+ }));
+ expect([...html.matchAll(/data-source-tab="([^"]+)"/g)].map(match => match[1]))
+ .toEqual(["camera", "picker"]);
+ expect(html).toMatch(/data-source-tab="picker"[^>]*aria-selected="true"/);
+ });
+
+ it("keeps an explicitly selected system chooser while refreshing without offering stale sources", () => {
+ setCopy({ lang: "en", vis: false });
+ const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ nativeSources: { kind: "loading", appDetected: true,
+ sources: [{ kind: "picker", sourceId: "1", title: "Choose on device" }] },
+ initialTab: "picker", browserSupported: false,
+ onBrowser() {}, onNative() {}, onCamera() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
+ }));
+ expect([...html.matchAll(/data-source-tab="([^"]+)"/g)].map(match => match[1]))
+ .toEqual(["camera", "picker"]);
+ expect(html).toMatch(/data-source-tab="picker"[^>]*aria-selected="true"/);
+ expect(html).toContain(`role="status" aria-label="${t("en", "host.sourcePicker.loading")}"`);
+ expect(html).not.toContain('class="lr-source-option is-action"');
+ expect(html).not.toContain('data-camera-source=');
+ });
+
+ it("shows the existing no-source result when neither Browser API nor App capture is available", () => {
+ setCopy({ lang: "en", vis: false });
+ const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ nativeSources: { kind: "browser" }, browserSupported: false, cameraSupported: false,
+ onBrowser() {}, onNative() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
+ }));
+ expect(html).not.toContain('role="tab"');
+ expect(html).not.toContain("is-action");
+ expect(html).toContain(t("en", "host.capture.noSource"));
+ });
+
it("keeps enumerated cameras selectable while optional thumbnails load", () => {
const props = {
sources: [{ id: "front", label: "Front camera", preview: null }],
@@ -64,6 +118,24 @@ describe("native capture source selection", () => {
expect(nativeTab).toContain('role="status"');
});
+ it("keeps pending native selection from implicitly opening a mobile camera", () => {
+ const props = { onBrowser: () => {}, onNative: () => {}, onCamera: () => {},
+ onPreview: async () => null, onRefresh: () => {}, onCancel: () => {},
+ browserSupported: false };
+ const pending = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ ...props, nativeSources: { kind: "loading" }, initialTab: "window",
+ }));
+ expect(pending).toMatch(/aria-selected="true"[^>]*data-source-tab="window"|data-source-tab="window"[^>]*aria-selected="true"/);
+ const explicit = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ ...props, nativeSources: { kind: "loading" }, initialTab: "camera",
+ }));
+ expect(explicit).toMatch(/aria-selected="true"[^>]*data-source-tab="camera"|data-source-tab="camera"[^>]*aria-selected="true"/);
+ const browser = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ ...props, nativeSources: { kind: "browser" }, initialTab: "window",
+ }));
+ expect(browser).toMatch(/aria-selected="true"[^>]*data-source-tab="camera"|data-source-tab="camera"[^>]*aria-selected="true"/);
+ });
+
it("uses the complete window identity as the UI key", () => {
expect(nativeCaptureTargetKey(game)).toBe("window:1:10:123456");
expect(
@@ -112,7 +184,7 @@ describe("native capture source selection", () => {
expect(browserTab).toContain(`aria-label="${t(lang, "host.sourcePicker.browser")}"`);
const browserCopy = browserTab.replaceAll("&#x27;", "'");
expect(browserCopy).not.toContain('role="status"');
- expect(browserTab).not.toMatch(/class="lr-source-option is-browser"[^>]*disabled=""/);
+ expect(browserTab).not.toMatch(/class="lr-source-option is-action"[^>]*disabled=""/);
}
});
@@ -123,13 +195,13 @@ describe("native capture source selection", () => {
onBrowser: () => {}, onNative: () => {}, onPreview: async () => null,
onRefresh: () => {}, onCancel: () => {},
}));
- expect(html).toMatch(/data-source-tab="browser"[^>]*aria-selected="true"/);
- expect(html).not.toContain('data-source-tab="window"');
+ expect(html).toMatch(new RegExp(`data-source-tab="${initialTab}"[^>]*aria-selected="true"`));
+ expect(html).not.toMatch(/data-source-tab="browser"[^>]*disabled=""/);
expect(html).not.toContain("lr-brand-loader");
expect(html).not.toMatch(/class="lr-source-picker-close"[^>]*disabled=""/);
});
- it("separates windows from displays and locks Browser switching during native sharing", () => {
+ it("separates windows from displays while keeping Browser and Camera switching available", () => {
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: {
kind: "ready",
@@ -137,16 +209,17 @@ describe("native capture source selection", () => {
processAudio: false,
systemAudio: true,
},
- browserAvailable: false,
audioLocked: true,
onBrowser: () => {},
+ onCamera: () => {},
onNative: () => {},
onPreview: async () => null,
onRefresh: () => {},
onCancel: () => {},
}));
expect(html).toContain('data-source-tab="browser"');
- expect(html).toMatch(/data-source-tab="browser"[^>]*disabled=""/);
+ expect(html).not.toMatch(/data-source-tab="browser"[^>]*disabled=""/);
+ expect(html).not.toMatch(/data-source-tab="camera"[^>]*disabled=""/);
expect(html).toMatch(/data-native-source="window:1:10:123456"[^>]*disabled=""/);
expect(html).not.toContain('data-native-source="display:2"');
expect(html.match(/role="tab"/g)).toHaveLength(4);
@@ -168,14 +241,14 @@ describe("native capture source selection", () => {
expect(html).not.toContain('role="status"');
});
- it("selects a focusable Camera tab when Browser sharing is unavailable", () => {
+ it("selects a focusable Camera tab when Browser capture is unsupported", () => {
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
- nativeSources: { kind: "browser" }, browserAvailable: false,
+ nativeSources: { kind: "browser" }, browserSupported: false,
onBrowser: () => {}, onCamera: () => {}, onNative: () => {},
onPreview: async () => null, onRefresh: () => {}, onCancel: () => {},
}));
expect(html).toMatch(/data-source-tab="camera"[^>]*aria-selected="true"[^>]*tabindex="0"/);
- expect(html).toMatch(/data-source-tab="browser"[^>]*tabindex="-1"[^>]*disabled=""/);
+ expect(html).not.toContain('data-source-tab="browser"');
});
it("retains the platform-owned combined source picker", () => {
@@ -204,7 +277,7 @@ describe("native capture source selection", () => {
onBrowser: () => {}, onNative: () => {}, onPreview: async () => null,
onRefresh: () => {}, onCancel: () => {},
}));
- expect(html).toMatch(/class="lr-source-option(?: is-browser)?"[^>]*disabled=""/);
+ expect(html).toMatch(/class="lr-source-option(?: is-action)?"[^>]*disabled=""/);
expect(html).not.toMatch(/class="lr-source-picker-close"[^>]*disabled=""/);
});
diff --git a/tests/native-client.test.ts b/tests/native-client.test.ts
index 25e1bb4e..214516cb 100644
--- a/tests/native-client.test.ts
+++ b/tests/native-client.test.ts
@@ -1,30 +1,41 @@
import { afterEach, describe, expect, it, vi } from "vitest";
+import { z } from "zod";
import { discoverNativeConnect, discoverNativeHealth, NativeClient, NativeCompatibilityError, notifyNativePresentation } from "../src/client/native/client";
import { DEFAULT_QUALITY_SETTINGS } from "../src/shared/protocol";
import { AppQualityPreference } from "../src/client/native/quality-preference";
+import { NATIVE_CLIENT_PORT_START, NATIVE_CLIENT_PORT_END } from "../src/client/native/discovery-ports";
import {
nativeEventSchema,
nativeHealthSchema,
nativeRelayCapacity,
NATIVE_CLIENT_PROTOCOL,
- NATIVE_CLIENT_PORT_START,
- NATIVE_CLIENT_PORT_END,
nativeCaptureTargetSchema,
shareSourceReplacedResponseSchema,
shareStartedResponseSchema,
shareUpdatedResponseSchema,
sourcePreviewResponseSchema,
receiveAnswerResponseSchema,
+ requestFailedResponseSchema,
} from "../src/client/native/wire";
afterEach(() => {
+ vi.restoreAllMocks();
vi.useRealTimers();
vi.unstubAllGlobals();
});
describe("native App private wire", () => {
+ it("preserves the published strict failure reader for requests without cancellation opt-in", () => {
+ const published = z.object({ version: z.literal(10), id: z.string(), type: z.literal("request-failed"), code: z.literal("operation-failed") }).strict();
+ const legacy = { version: 10, id: "request_start", type: "request-failed", code: "operation-failed" };
+ expect(published.safeParse(legacy).success).toBe(true);
+ expect(requestFailedResponseSchema.safeParse(legacy).success).toBe(true);
+ const optedIn = { ...legacy, code: "source-canceled" };
+ expect(published.safeParse(optedIn).success).toBe(false);
+ expect(requestFailedResponseSchema.safeParse(optedIn).success).toBe(true);
+ });
const health = {
protocol: NATIVE_CLIENT_PROTOCOL,
service: "piik-client",
@@ -33,32 +44,50 @@ describe("native App private wire", () => {
nativeMedia: { video: true, hardwareH264: true },
};
+ it("discovers with caller cancellation when AbortSignal.any is unavailable", async () => {
+ vi.spyOn(AbortSignal, "any").mockImplementation(() => { throw new TypeError("AbortSignal.any is not a function"); });
+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("http://127.0.0.1:8787/") });
+ const fetcher = vi.fn(async (url: string) => {
+ if (new URL(url).port !== String(health.port)) throw new TypeError("No listener");
+ return new Response(JSON.stringify(health));
+ });
+ vi.stubGlobal("fetch", fetcher);
+ await expect(discoverNativeHealth({ signal: new AbortController().signal })).resolves.toMatchObject({ port: health.port });
+ expect(fetcher).toHaveBeenCalled();
+ });
+
it.each(["localhost", "127.0.0.1", "[::1]"])("discovers and updates presentation from %s without an address-space hint", async hostname => {
vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL(`http://${hostname}:8787/`) });
const query = vi.fn().mockResolvedValue({ state: "prompt" });
vi.stubGlobal("navigator", { permissions: { query } });
const fetcher = vi.fn((url: string, init: RequestInit) => {
if ("targetAddressSpace" in init) throw new TypeError("loopback is not a valid IPAddressSpace enum value");
- return Promise.resolve(new Response(JSON.stringify(url.endsWith("connect-discovery")
- ? { ...health, connectURL: "http://127.0.0.1:54321/client" } : health)));
+ return Promise.resolve(new Response(JSON.stringify(url.endsWith("quality-preference") ? DEFAULT_QUALITY_SETTINGS
+ : url.endsWith("connect-discovery") ? { ...health, connectURL: "http://127.0.0.1:54321/client" }
+ : { ...health, qualityPreference: true })));
});
vi.stubGlobal("fetch", fetcher);
await expect(discoverNativeHealth({ waitForPermission: false })).resolves.toMatchObject({ port: health.port });
await expect(discoverNativeConnect()).resolves.toContain("http://127.0.0.1:54321/client?");
await notifyNativePresentation("en", new AbortController().signal);
+ const preference = await AppQualityPreference.connect(new AbortController().signal);
+ await expect(preference!.read()).resolves.toEqual(DEFAULT_QUALITY_SETTINGS);
expect(query).not.toHaveBeenCalled();
- expect(fetcher).toHaveBeenLastCalledWith(`http://127.0.0.1:${health.port}/presentation`, expect.objectContaining({ method: "POST" }));
+ expect(fetcher).toHaveBeenCalledWith(`http://127.0.0.1:${health.port}/presentation`, expect.objectContaining({ method: "POST" }));
expect(fetcher.mock.calls.every(([, init]) => !("targetAddressSpace" in init))).toBe(true);
});
it("retains the address-space hint for hosted discovery and presentation without a no-hint fallback", async () => {
vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });
vi.stubGlobal("navigator", { permissions: { query: vi.fn().mockResolvedValue({ state: "granted" }) } });
- const fetcher = vi.fn(async (_url: string, _init: RequestInit) => new Response(JSON.stringify(health)));
+ const fetcher = vi.fn(async (url: string, _init: RequestInit) => new Response(JSON.stringify(
+ url.endsWith("quality-preference") ? DEFAULT_QUALITY_SETTINGS : { ...health, qualityPreference: true })));
vi.stubGlobal("fetch", fetcher);
await notifyNativePresentation("en", new AbortController().signal);
+ const preference = await AppQualityPreference.connect(new AbortController().signal);
+ await expect(preference!.read()).resolves.toEqual(DEFAULT_QUALITY_SETTINGS);
expect(fetcher.mock.calls.every(call => (call[1] as RequestInit & { targetAddressSpace: string }).targetAddressSpace === "loopback")).toBe(true);
- expect(fetcher).toHaveBeenLastCalledWith(`http://127.0.0.1:${health.port}/presentation`, expect.objectContaining({ targetAddressSpace: "loopback" }));
+ expect(fetcher).toHaveBeenCalledWith(`http://127.0.0.1:${health.port}/presentation`, expect.objectContaining({ targetAddressSpace: "loopback" }));
fetcher.mockImplementation(() => { throw new TypeError("Unsupported IPAddressSpace"); });
fetcher.mockClear();
await expect(discoverNativeHealth()).resolves.toBeNull();
@@ -110,6 +139,7 @@ describe("native App private wire", () => {
});
it("cancels the whole approval scan with its caller", async () => {
+ vi.spyOn(AbortSignal, "any").mockImplementation(() => { throw new TypeError("AbortSignal.any is not a function"); });
vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });
const signals: AbortSignal[] = [];
vi.stubGlobal("fetch", vi.fn((_url: string, init: RequestInit) => new Promise((_resolve, reject) => {
@@ -361,6 +391,7 @@ describe("native App private wire", () => {
it("bounds all silent discovery ports with one deadline", async () => {
vi.useFakeTimers();
+ vi.spyOn(AbortSignal, "any").mockImplementation(() => { throw new TypeError("AbortSignal.any is not a function"); });
vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });
const signals: AbortSignal[] = [];
vi.stubGlobal("fetch", vi.fn(async (_url: string, options: RequestInit) => {
@@ -369,7 +400,7 @@ describe("native App private wire", () => {
options.signal!.addEventListener("abort", () => reject(new DOMException("Aborted", "AbortError")), { once: true });
});
}));
- const discovery = discoverNativeHealth();
+ const discovery = discoverNativeHealth({ signal: new AbortController().signal });
await vi.advanceTimersByTimeAsync(8_000);
await expect(discovery).resolves.toBeNull();
expect(signals).toHaveLength(NATIVE_CLIENT_PORT_END - NATIVE_CLIENT_PORT_START + 1);
@@ -437,6 +468,7 @@ describe("native App private wire", () => {
it("notifies the owner once on an unexpected close and stays silent on cleanup", async () => {
const sockets: FakeWebSocket[] = [];
const token = "a".repeat(43);
+ let failureCode = "operation-failed";
class FakeWebSocket extends EventTarget {
static readonly OPEN = 1;
static readonly CLOSING = 2;
@@ -463,7 +495,7 @@ describe("native App private wire", () => {
version: 10,
id: request.id,
type: request.type === "hello" ? "ready" : request.type === "ping" ? "pong" : "request-failed",
- ...(request.type === "update-share" ? { code: "operation-failed" } : {}),
+ ...(request.type === "update-share" ? { code: failureCode } : {}),
}),
});
this.dispatchEvent(event);
@@ -503,6 +535,9 @@ describe("native App private wire", () => {
client!.onClose(unexpected);
await expect(client!.updateShare("share_123456", DEFAULT_QUALITY_SETTINGS))
.rejects.toMatchObject({ name: "NativeRequestError", reason: "rejected", operation: "update-share" });
+ failureCode = "source-canceled";
+ await expect(client!.updateShare("share_123456", DEFAULT_QUALITY_SETTINGS))
+ .rejects.toMatchObject({ name: "NotAllowedError" });
await client!.ping();
expect(sockets[0]!.close).not.toHaveBeenCalled();
expect(unexpected).not.toHaveBeenCalled();
@@ -612,7 +647,7 @@ describe("native App private wire", () => {
vi.stubGlobal("WebSocket", CaptureSocket);
vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });
vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({
- ...health, nativeMedia: { ...health.nativeMedia, captureBorderControl: supported, remoteInput: supported },
+ ...health, nativeMedia: { ...health.nativeMedia, captureBorderControl: supported, remoteInput: supported, sourceCancellation: supported },
}))));
const client = await NativeClient.connect();
expect(client).not.toBeNull();
@@ -624,9 +659,15 @@ describe("native App private wire", () => {
};
try {
for (const showCaptureBorder of [undefined, false, true]) {
- await client!.startShare({ ...input, showCaptureBorder });
+ const onRequested = vi.fn(() => expect(requests.at(-1)?.type).toBe("start-share"));
+ let acknowledged = false;
+ const starting = client!.startShare({ ...input, showCaptureBorder }, onRequested);
+ void starting.then(() => { acknowledged = true; });
+ expect(onRequested).toHaveBeenCalledOnce();
+ expect(acknowledged).toBe(false);
+ await starting;
await client!.replaceShareSource(input.shareId, input.source, input.audio, input, showCaptureBorder);
- const extension = supported ? { showCaptureBorder: showCaptureBorder ?? false } : {};
+ const extension = supported ? { showCaptureBorder: showCaptureBorder ?? false, reportSourceCancellation: true } : {};
expect(requests.at(-2)).toEqual({
version: NATIVE_CLIENT_PROTOCOL, id: expect.any(String), type: "start-share", ...input, ...extension,
...(supported ? { remoteInput: true } : {}),
@@ -636,6 +677,13 @@ describe("native App private wire", () => {
shareId: input.shareId, source: input.source, audio: true, adapterIndex: 0, encoderIndex: 0, ...extension,
});
}
+ const unsent = vi.fn();
+ vi.spyOn(CaptureSocket.prototype, "send").mockImplementationOnce(() => { throw new Error("send failed"); });
+ await expect(client!.startShare(input, unsent)).rejects.toMatchObject({ reason: "send-failed" });
+ expect(unsent).not.toHaveBeenCalled();
+ client!.close();
+ await expect(client!.startShare(input, unsent)).rejects.toMatchObject({ reason: "unavailable" });
+ expect(unsent).not.toHaveBeenCalled();
} finally { client!.close(); }
});
diff --git a/tests/native-quality-preference.test.ts b/tests/native-quality-preference.test.ts
index a70a2082..40a9315a 100644
--- a/tests/native-quality-preference.test.ts
+++ b/tests/native-quality-preference.test.ts
@@ -2,10 +2,11 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { DEFAULT_QUALITY_SETTINGS } from "../src/shared/protocol";
import { AppQualityPreference } from "../src/client/native/quality-preference";
import { discoverNativeHealth } from "../src/client/native/client";
-import { nativeHealthSchema, NATIVE_CLIENT_PROTOCOL, NATIVE_CLIENT_PORT_START } from "../src/client/native/wire";
+import { nativeHealthSchema, NATIVE_CLIENT_PROTOCOL } from "../src/client/native/wire";
+import { NATIVE_CLIENT_PORT_START } from "../src/client/native/discovery-ports";
-vi.mock("../src/client/native/client", () => ({ discoverNativeHealth: vi.fn() }));
-afterEach(() => { vi.unstubAllGlobals(); vi.clearAllMocks(); });
+vi.mock("../src/client/native/client", () => ({ discoverNativeHealth: vi.fn(), isLoopbackPage: () => false }));
+afterEach(() => { vi.useRealTimers(); vi.restoreAllMocks(); vi.unstubAllGlobals(); vi.clearAllMocks(); });
const health = nativeHealthSchema.parse({
protocol: NATIVE_CLIENT_PROTOCOL, service: "piik-client", port: NATIVE_CLIENT_PORT_START,
@@ -13,6 +14,48 @@ const health = nativeHealthSchema.parse({
});
describe("App quality preference transport", () => {
+ it.each(["caller", "deadline"])("cancels a pending response body through its %s", async cause => {
+ vi.useFakeTimers();
+ vi.spyOn(AbortSignal, "any").mockImplementation(() => { throw new TypeError("AbortSignal.any is not a function"); });
+ vi.mocked(discoverNativeHealth).mockResolvedValue(health);
+ vi.stubGlobal("fetch", vi.fn(async (_url: string, init: RequestInit) => new Response(new ReadableStream({
+ start(body) { init.signal!.addEventListener("abort", () => body.error(init.signal!.reason), { once: true }); },
+ }))));
+ const owner = new AbortController();
+ const preference = (await AppQualityPreference.connect(owner.signal))!;
+ const rejected = expect(preference.read()).rejects.toMatchObject({ name: cause === "caller" ? "AbortError" : "TimeoutError" });
+ await Promise.resolve();
+ if (cause === "caller") owner.abort();
+ else await vi.advanceTimersByTimeAsync(4_000);
+ await rejected;
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
+ it("reads a delayed body without AbortSignal.any and releases its deadline afterward", async () => {
+ vi.useFakeTimers();
+ vi.spyOn(AbortSignal, "any").mockImplementation(() => { throw new TypeError("AbortSignal.any is not a function"); });
+ vi.mocked(discoverNativeHealth).mockResolvedValue(health);
+ let body!: ReadableStreamDefaultController<Uint8Array>;
+ let requestSignal!: AbortSignal;
+ vi.stubGlobal("fetch", vi.fn(async (_url: string, init: RequestInit) => {
+ requestSignal = init.signal!;
+ return new Response(new ReadableStream<Uint8Array>({ start(controller) {
+ body = controller;
+ requestSignal.addEventListener("abort", () => controller.error(requestSignal.reason), { once: true });
+ } }));
+ }));
+ const preference = (await AppQualityPreference.connect(new AbortController().signal))!;
+ const reading = preference.read();
+ void reading.catch(() => undefined);
+ await Promise.resolve();
+ expect(requestSignal).toBeDefined();
+ expect(requestSignal.aborted).toBe(false);
+ body.enqueue(new TextEncoder().encode(JSON.stringify(DEFAULT_QUALITY_SETTINGS)));
+ body.close();
+ await expect(reading).resolves.toEqual(DEFAULT_QUALITY_SETTINGS);
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
it("leaves older Apps alone and does not discover after retirement", async () => {
const fetcher = vi.fn();
vi.stubGlobal("fetch", fetcher);
diff --git a/tests/native-runtime-affinity.test.ts b/tests/native-runtime-affinity.test.ts
new file mode 100644
index 00000000..878a3689
--- /dev/null
+++ b/tests/native-runtime-affinity.test.ts
@@ -0,0 +1,101 @@
+import { createServer, type Server } from "node:http";
+import { afterEach, expect, it, vi } from "vitest";
+import { NATIVE_CLIENT_PROTOCOL } from "../src/client/native/wire";
+import { NATIVE_CLIENT_PORT_START, NATIVE_CLIENT_PORT_END } from "../src/client/native/discovery-ports";
+
+const servers: Server[] = [];
+afterEach(async () => {
+ await Promise.all(servers.splice(0).map(server => new Promise<void>(resolve => {
+ server.close(() => resolve());
+ server.closeAllConnections();
+ })));
+ vi.unstubAllGlobals();
+ vi.resetModules();
+});
+
+async function runtime(delay: number) {
+ const requests: string[] = [];
+ for (let port = NATIVE_CLIENT_PORT_END; port >= NATIVE_CLIENT_PORT_START; port--) {
+ const server = createServer((request, response) => {
+ requests.push(request.url!);
+ setTimeout(() => {
+ response.setHeader("Content-Type", "application/json");
+ response.end(JSON.stringify({ protocol: NATIVE_CLIENT_PROTOCOL, service: "piik-client",
+ port, instanceToken: "a".repeat(43), nativeMedia: { video: true },
+ ...(request.url === "/connect-discovery" ? { connectURL: `http://127.0.0.1:${port}/client` } : {}),
+ }));
+ }, delay);
+ });
+ try {
+ await new Promise<void>((resolve, reject) => {
+ server.once("error", reject);
+ server.listen(port, "127.0.0.1", resolve);
+ });
+ servers.push(server);
+ return { server, port, requests };
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "EADDRINUSE") throw error;
+ }
+ }
+ throw new Error("Runtime affinity check needs two free native discovery ports");
+}
+
+it("selects the launched runtime over a faster resident App and never substitutes it after exit", async () => {
+ const old = await runtime(0);
+ const selected = await runtime(30);
+ const saved = new Map<string, string>();
+ const tabSaved = new Map<string, string>();
+ const page = {
+ location: new URL("http://127.0.0.1:8787/"), setTimeout, clearTimeout,
+ localStorage: { getItem: (key: string) => saved.get(key) ?? null,
+ setItem: (key: string, value: string) => { saved.set(key, value); } },
+ sessionStorage: { getItem: (key: string) => tabSaved.get(key) ?? null,
+ setItem: (key: string, value: string) => { tabSaved.set(key, value); },
+ removeItem: (key: string) => { tabSaved.delete(key); } },
+ history: { state: null, replaceState: (_state: unknown, _unused: string, value: string) => {
+ page.location = new URL(value, page.location);
+ } },
+ };
+ vi.stubGlobal("window", page);
+ const { discoverNativeHealth, discoverNativeConnect } = await import("../src/client/native/client");
+ const { takeClientLaunchBootstrap, clientRuntimePort } = await import("../src/client/lib/session");
+ // Both actual HTTP stubs are discoverable; the older one responds first.
+ await expect(discoverNativeHealth()).resolves.toMatchObject({ port: old.port });
+ page.location.search = `?piik-client-port=${selected.port}`;
+ page.location.hash = "piik-client=1";
+ takeClientLaunchBootstrap();
+ old.requests.length = 0;
+ await expect(discoverNativeHealth()).resolves.toMatchObject({ port: selected.port });
+ await expect(discoverNativeConnect()).resolves.toContain(`127.0.0.1:${selected.port}/client?`);
+ expect(old.requests).toEqual([]);
+ expect(page.location.href).toBe("http://127.0.0.1:8787/");
+ expect([...saved.keys()]).toEqual(["piik:client-launch:v1"]);
+ expect([...tabSaved.values()]).toEqual([String(selected.port)]);
+ takeClientLaunchBootstrap(); // Consuming an already-clean URL keeps this page's affinity.
+ expect(clientRuntimePort()).toBe(selected.port);
+ await new Promise<void>(resolve => { selected.server.close(() => resolve()); selected.server.closeAllConnections(); });
+ await expect(discoverNativeHealth()).resolves.toBeNull();
+ await expect(discoverNativeConnect()).resolves.toBeNull();
+ expect(old.requests).toEqual([]);
+ vi.resetModules();
+ const reloaded = await import("../src/client/lib/session");
+ reloaded.takeClientLaunchBootstrap();
+ expect(reloaded.clientRuntimePort()).toBe(selected.port);
+ expect(reloaded.isClientActivated()).toBe(true);
+ const reloadedDiscovery = await import("../src/client/native/client");
+ await expect(reloadedDiscovery.discoverNativeHealth()).resolves.toBeNull();
+ expect(old.requests).toEqual([]);
+ // A new explicit launch replaces this tab's previous selection.
+ page.location.search = `?piik-client-port=${old.port}`;
+ page.location.hash = "piik-client=1";
+ reloaded.takeClientLaunchBootstrap();
+ expect(reloaded.clientRuntimePort()).toBe(old.port);
+ expect([...tabSaved.values()]).toEqual([String(old.port)]);
+ // A fresh tab shares activation preference, but has its own session storage.
+ vi.stubGlobal("window", { ...page, sessionStorage: { getItem: () => null } });
+ vi.resetModules();
+ const freshTab = await import("../src/client/lib/session");
+ expect(freshTab.clientRuntimePort()).toBeNull();
+ const freshDiscovery = await import("../src/client/native/client");
+ await expect(freshDiscovery.discoverNativeHealth()).resolves.toMatchObject({ port: old.port });
+});
diff --git a/tests/participant-color.test.ts b/tests/participant-color.test.ts
index 09298e58..728ff762 100644
--- a/tests/participant-color.test.ts
+++ b/tests/participant-color.test.ts
@@ -2,16 +2,21 @@ import { describe, expect, it } from "vitest";
import { participantColor } from "../src/client/components/living/participant-color";
describe("participant identity colours", () => {
- it("separates identities that collided in the eight-colour palette, independently of roster order", () => {
+ it("keeps identity colours valid in sRGB and independent of roster order", () => {
const ids = [
"00000000-0000-4000-8000-000000000000",
"00000000-0000-4000-8000-000000000008",
"00000000-0000-4000-8000-000000000011",
+ "participant-1", // Its assigned OKLCH colour is outside sRGB.
];
const colours = ids.map(participantColor);
expect(new Set(colours).size).toBe(ids.length);
- const hues = colours.map(colour => Number(colour.slice(colour.lastIndexOf(" ") + 1, -1))).sort((a, b) => a - b);
- expect(Math.min(hues[1]! - hues[0]!, hues[2]! - hues[1]!)).toBeGreaterThan(15);
+ for (const colour of colours) {
+ expect(colour).toMatch(/^rgb\(\d+ \d+ \d+\)$/);
+ const channels = colour.match(/\d+/g)!.map(Number);
+ expect(channels.every(channel => channel >= 0 && channel <= 255)).toBe(true);
+ expect(channels.some(channel => channel > 0)).toBe(true);
+ }
expect(ids.toReversed().map(participantColor)).toEqual(colours.toReversed());
});
});
diff --git a/tests/publish-release.test.ts b/tests/publish-release.test.ts
index eb278ebd..1b6cf6dd 100644
--- a/tests/publish-release.test.ts
+++ b/tests/publish-release.test.ts
@@ -14,10 +14,10 @@ vi.mock("node:timers/promises", () => ({ setTimeout: async () => undefined }));
const directory = mkdtempSync(join(tmpdir(), "piik-publisher-"));
const version = "v1.0.1", revision = "a".repeat(40);
const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex");
-for (const target of ["server", "server-linux-arm64", "server-windows-amd64", "windows-amd64", "linux-amd64", "linux-arm64", "darwin-arm64", "darwin-amd64"]) {
+for (const target of ["server", "server-linux-arm64", "server-windows-amd64", "windows-amd64", "linux-amd64", "linux-arm64", "darwin-arm64", "darwin-amd64", "android-universal"]) {
const server = target.startsWith("server");
// The Windows Server package ships a ZIP; the Linux Server packages keep tar.gz.
- const archive = target === "server-windows-amd64" ? "zip" : server ? "tar.gz" : "zip";
+ const archive = target === "android-universal" ? "apk" : target === "server-windows-amd64" ? "zip" : server ? "tar.gz" : "zip";
const artifact = `${target}.${archive}`;
writeFileSync(join(directory, artifact), target);
writeFileSync(join(directory, `${target}.release.json`), JSON.stringify({
@@ -29,7 +29,7 @@ for (const target of ["server", "server-linux-arm64", "server-windows-amd64", "w
}
afterAll(() => rmSync(directory, { recursive: true, force: true }));
-const packageNames = readdirSync(directory).filter((name) => /\.(zip|tar\.gz)$/.test(name));
+const packageNames = readdirSync(directory).filter((name) => /\.(apk|zip|tar\.gz)$/.test(name));
const completeAssets = packageNames.map((name) => ({ name, state: "uploaded" }));
type Release = { tag_name: string; target_commitish: string; draft: boolean;
prerelease: boolean; body?: string; assets: typeof completeAssets };
diff --git a/tests/quality-controls.test.ts b/tests/quality-controls.test.ts
index 7ef3087e..ddc7c43b 100644
--- a/tests/quality-controls.test.ts
+++ b/tests/quality-controls.test.ts
@@ -8,15 +8,15 @@ import { describe, expect, it, vi } from "vitest";
import { VideoRateControls } from "../src/client/components/living/QualityPresets";
import { QUALITY_PROFILES, VIDEO_CAPTURE_LIMITS } from "../src/client/media/quality";
-// Exercise the private draft owner without adding a DOM runtime to unit tests.
+// Exercise the shared draft owner without adding a DOM runtime to unit tests.
// Real pointer/focus ordering is also checked in the existing Browser preview.
-const source = ts.createSourceFile("QualityPresets.tsx", readFileSync(
- new URL("../src/client/components/living/QualityPresets.tsx", import.meta.url), "utf8",
+const source = ts.createSourceFile("NumberControl.tsx", readFileSync(
+ new URL("../src/client/components/living/NumberControl.tsx", import.meta.url), "utf8",
), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
-const owner = source.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "QualityNumberControl");
+const owner = source.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "NumberControl");
if (!owner) throw new Error("Missing numeric quality draft owner");
-const executable = ts.transpileModule(`${owner.getText(source)}\nQualityNumberControl;`, {
- fileName: "QualityPresets.tsx",
+const executable = ts.transpileModule(`${owner.getText(source)}\nNumberControl;`, {
+ fileName: "NumberControl.tsx",
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX },
}).outputText;
diff --git a/tests/quality.test.ts b/tests/quality.test.ts
index 55bc72dc..ef65e1a9 100644
--- a/tests/quality.test.ts
+++ b/tests/quality.test.ts
@@ -54,7 +54,7 @@ afterEach(() => {
describe("realtime quality controls", () => {
it("bounds custom size, orientation and frame rate together before capture", () => {
- expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["4K"])).toBe(64);
+ expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["2160p"])).toBe(64);
expect(captureFramerateCeiling({ width: 2160, height: 3840 })).toBe(64);
expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["1440p"])).toBe(144);
expect(captureFramerateCeiling({ width: 1440, height: 2560 })).toBe(144);
@@ -62,7 +62,7 @@ describe("realtime quality controls", () => {
expect(captureFramerateCeiling({ width: 5120, height: 1440 })).toBe(72);
expect(captureFramerateCeiling({ width: 1920, height: 1080 })).toBe(240);
const level51 = videoCaptureLimits({ maxVideoMacroblocksPerSecond: 983_040 });
- expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["4K"], level51)).toBe(30);
+ expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["2160p"], level51)).toBe(30);
expect(captureFramerateCeiling({ width: 2160, height: 3840 }, level51)).toBe(30);
for (const size of [{ width: 3840, height: 3840 }, { width: 1921, height: 1080 },
{ width: 8194, height: 100 }, { width: 6562, height: 1438 },
@@ -71,7 +71,7 @@ describe("realtime quality controls", () => {
}
const limited = videoCaptureLimits({ maxVideoResolution: { width: 1920, height: 1080 },
maxVideoPixels: 1920 * 1080, maxVideoMacroblocksPerSecond: 245_760 });
- expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["4K"], limited)).toBe(0);
+ expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["2160p"], limited)).toBe(0);
expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["1080p"], limited)).toBe(30);
expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["720p"], limited)).toBe(68);
expect(videoCaptureLimits({ maxVideoResolution: { width: 8192, height: 8192 },
@@ -115,7 +115,7 @@ describe("realtime quality controls", () => {
const { stream, videoTrack } = createVideoStream();
const getDisplayMedia = vi.fn(async () => stream);
vi.stubGlobal("navigator", { mediaDevices: { getDisplayMedia } });
- for (const resolution of [QUALITY_RESOLUTIONS["4K"], { width: 2160, height: 3840 }]) {
+ for (const resolution of [QUALITY_RESOLUTIONS["2160p"], { width: 2160, height: 3840 }]) {
const profile = { ...QUALITY_PROFILES["1080p60"], resolution, maxBitrate: 50_000_000 };
expect(supportsCaptureProfile(profile)).toBe(true);
await captureDisplay(profile);
diff --git a/tests/relay-capability.test.ts b/tests/relay-capability.test.ts
index eca290b2..023f4090 100644
--- a/tests/relay-capability.test.ts
+++ b/tests/relay-capability.test.ts
@@ -1,7 +1,14 @@
-import { describe, expect, it } from "vitest";
+import { readFileSync } from "node:fs";
+import { createContext, runInContext } from "node:vm";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import ts from "typescript";
+import { describe, expect, it, vi } from "vitest";
+import { PlaybackSettings, type PlaybackSettingsProps } from "../src/client/components/living/PlaybackSettings";
import {
relayCapacityMessageForBrowser,
+ relayVolunteerLimit,
} from "../src/client/media/relay-capability.ts";
describe("browser relay capability", () => {
@@ -13,3 +20,103 @@ describe("browser relay capability", () => {
});
});
+
+// Execute the page's actual request owner and React props together. Backend
+// changes must adjust the applied limit without replacing the user's choice.
+const source = ts.createSourceFile("ViewerPage.tsx", readFileSync(
+ new URL("../src/client/pages/ViewerPage.tsx", import.meta.url), "utf8"), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
+let update = "", props = "";
+const authentication: string[] = [];
+function collect(node: ts.Node): void {
+ if (ts.isFunctionDeclaration(node) && node.name?.text === "updateRelayVolunteer") update = node.getText(source);
+ if (ts.isPropertyAssignment(node) && node.name.getText(source) === "relayVolunteer" && ts.isConditionalExpression(node.initializer)) {
+ props = node.initializer.getText(source);
+ }
+ if (ts.isIfStatement(node) && node.expression.getText(source) === 'message.type === "authenticated"' && ts.isBlock(node.thenStatement)) {
+ for (const statement of node.thenStatement.statements) {
+ const text = statement.getText(source);
+ if (text.startsWith("ordinaryRelayCapacity =") || text.startsWith("signal.send(relayCapacityMessageForBrowser())") || text.startsWith("updateRelayVolunteer(")) authentication.push(text);
+ }
+ }
+ ts.forEachChild(node, collect);
+}
+collect(source);
+if (!update || !props || authentication.length !== 3) throw new Error("Viewer relay capacity owner was not found");
+const executable = ts.transpile(`${update}\n globalThis.update = updateRelayVolunteer; globalThis.props = () => (${props});
+ globalThis.reauthenticate = () => { ${authentication.join("\n")} };`,
+ { target: ts.ScriptTarget.ES2022 });
+
+it("keeps the requested limit through backend changes while React displays the effective value", () => {
+ const wire: Array<{ enabled: boolean; capacity: number }> = [];
+ const order: string[] = [];
+ const signal = {
+ relayVolunteerRequest: null as { enabled: boolean; capacity: number } | null,
+ send: () => { order.push("ordinary"); return true; },
+ setRelayVolunteer(enabled: boolean, capacity: number) {
+ this.relayVolunteerRequest = { enabled, capacity };
+ wire.push({ enabled, capacity });
+ order.push("volunteer");
+ return true;
+ },
+ };
+ const context = createContext({
+ signal, relayVolunteerLimit, relayCapacityMessageForBrowser, message: { endpointMediaCopyCapacity: 2 },
+ relayBackendCapacity: 20, ordinaryRelayCapacity: 2, requestedRelayCapacity: null,
+ clientActivationRef: { current: false }, MAX_ENDPOINT_MEDIA_CHILDREN: 3, MAX_VIEWERS_PER_ROOM_LIMIT: 20,
+ relayVolunteerAvailable: true, relayVolunteerSupported: true, relayVolunteerEnabled: false,
+ relayVolunteerPending: false, signalStatus: "connected", relayVolunteerLimits: relayVolunteerLimit(null, 2, 20),
+ requestRelayVolunteerRef: { current: null as ((enabled: boolean, capacity?: number) => void) | null },
+ setRelayVolunteerLimits: (limits: ReturnType<typeof relayVolunteerLimit>) => { context.relayVolunteerLimits = limits; },
+ setRelayVolunteerPending: vi.fn(),
+ });
+ runInContext(executable, context);
+ const request = context.update as (enabled?: boolean, capacity?: number, send?: boolean) => void;
+ context.requestRelayVolunteerRef.current = request;
+ const settings = () => (context.props as () => NonNullable<PlaybackSettingsProps["relayVolunteer"]>)();
+ const render = () => renderToStaticMarkup(createElement(PlaybackSettings, { relayVolunteer: settings(), reconnect: null }));
+ expect(render()).not.toContain('name="relay-capacity"');
+ settings().onChange(true);
+ expect(wire.at(-1)).toEqual({ enabled: true, capacity: 3 });
+ context.relayVolunteerEnabled = true;
+ const number = render().match(/<input[^>]*name="relay-capacity"[^>]*>/)?.[0];
+ for (const attribute of ['min="3"', 'max="20"', 'value="3"']) expect(number).toContain(attribute);
+ settings().onCapacityChange(8);
+ expect(wire.at(-1)).toEqual({ enabled: true, capacity: 8 });
+ context.relayBackendCapacity = 3;
+ request();
+ expect(settings().capacity).toBe(3);
+ expect(wire.at(-1)).toEqual({ enabled: true, capacity: 3 });
+ context.relayBackendCapacity = 20;
+ request();
+ expect(settings().capacity).toBe(8);
+ expect(wire.at(-1)).toEqual({ enabled: true, capacity: 8 });
+ const count = wire.length;
+ request(undefined, undefined, false);
+ expect(wire).toHaveLength(count);
+ context.ordinaryRelayCapacity = context.relayBackendCapacity = 3;
+ request();
+ expect(settings().available).toBe(false);
+ expect(render()).toContain('name="relay-capacity"');
+ settings().onChange(false);
+ expect(wire.at(-1)).toEqual({ enabled: false, capacity: 3 });
+ context.relayVolunteerEnabled = false;
+ expect(settings().disabled).toBe(true);
+ expect(render()).not.toContain('name="relay-capacity"');
+ context.ordinaryRelayCapacity = 2;
+ context.relayBackendCapacity = 20;
+ request();
+ expect(wire.at(-1)).toEqual({ enabled: false, capacity: 20 });
+ settings().onChange(true);
+ expect(wire.at(-1)).toEqual({ enabled: true, capacity: 8 });
+ settings().onCapacityChange(3);
+ const before = order.length;
+ context.message.endpointMediaCopyCapacity = 3;
+ (context.reauthenticate as () => void)();
+ expect(settings().capacity).toBe(4);
+ expect(wire.at(-1)).toEqual({ enabled: true, capacity: 4 });
+ expect(order.slice(before)).toEqual(["ordinary", "volunteer"]);
+ context.message.endpointMediaCopyCapacity = 2;
+ (context.reauthenticate as () => void)();
+ expect(settings().capacity).toBe(3);
+ expect(wire.at(-1)).toEqual({ enabled: true, capacity: 3 });
+});
diff --git a/tests/release-automation.test.ts b/tests/release-automation.test.ts
index 65330715..352af9c2 100644
--- a/tests/release-automation.test.ts
+++ b/tests/release-automation.test.ts
@@ -245,9 +245,9 @@ describe("release automation", () => {
const revision = "a".repeat(40);
const run = () => spawnSync(process.execPath, [publisher, root, version, revision, "--dry-run"], { encoding: "utf8" });
try {
- for (const target of ["server", "server-linux-arm64", "server-windows-amd64", "windows-amd64", "linux-amd64", "linux-arm64", "darwin-arm64", "darwin-amd64"]) {
+ for (const target of ["server", "server-linux-arm64", "server-windows-amd64", "windows-amd64", "linux-amd64", "linux-arm64", "darwin-arm64", "darwin-amd64", "android-universal"]) {
const server = target.startsWith("server");
- const archive = target === "server-windows-amd64" ? "zip" : server ? "tar.gz" : "zip";
+ const archive = target === "android-universal" ? "apk" : target === "server-windows-amd64" ? "zip" : server ? "tar.gz" : "zip";
const artifact = `${target}.${archive}`;
writeFileSync(join(root, artifact), target);
const descriptor = { schema: 2, version, revision, artifact, artifactSha256: sha(target),
@@ -258,8 +258,13 @@ describe("release automation", () => {
}
const complete = run();
expect(complete.status, complete.stderr).toBe(0);
- expect(JSON.parse(complete.stdout).targets).toHaveLength(8);
- expect(JSON.parse(complete.stdout).files).toBe(8);
+ expect(JSON.parse(complete.stdout).targets).toHaveLength(9);
+ expect(JSON.parse(complete.stdout).files).toBe(9);
+ const androidDescriptor = join(root, "android-universal.release.json");
+ const androidMetadata = readFileSync(androidDescriptor, "utf8");
+ rmSync(androidDescriptor);
+ expect(run().stderr).toContain("every registered Server and App target");
+ writeFileSync(androidDescriptor, androidMetadata);
const checksum = join(root, "windows-amd64.zip.sha256");
const originalChecksum = readFileSync(checksum, "utf8");
writeFileSync(checksum, "wrong checksum");
diff --git a/tests/release-update.test.ts b/tests/release-update.test.ts
index af8d1db4..c69fa59c 100644
--- a/tests/release-update.test.ts
+++ b/tests/release-update.test.ts
@@ -49,6 +49,19 @@ describe("App release update notice", () => {
.toBe(current.browser_download_url);
});
+ it("selects the Android APK from either release provider", () => {
+ const name = "piik-app-android-universal.apk";
+ const asset = { name, size: 1024, state: "uploaded",
+ browser_download_url: `https://github.com/TNTcraftHIM/Piik/releases/download/${release.tag_name}/${name}` };
+ expect(parseReleaseMetadata({ ...release, assets: [asset] }, "android-universal")?.url)
+ .toBe(asset.browser_download_url);
+ const mirrorAsset = { name,
+ browser_download_url: `https://gitee.com/TNTcraftHIM/Piik/releases/download/${release.tag_name}/${name}` };
+ expect(parseMirrorReleaseMetadata({ tag_name: release.tag_name, prerelease: false,
+ body: `<!-- piik-source: ${latestRevision} -->`, assets: [mirrorAsset] }, "android-universal")?.url)
+ .toBe(mirrorAsset.browser_download_url);
+ });
+
it("keeps the release page when a matching safe ZIP is missing, without trying another provider", async () => {
const asset = packageAsset("windows-amd64");
for (const assets of [[], [packageAsset("linux-amd64")], [packageAsset("windows-amd64", "-aaaaaaa")],
diff --git a/tests/remote-input.test.ts b/tests/remote-input.test.ts
index e7bbb801..5914f760 100644
--- a/tests/remote-input.test.ts
+++ b/tests/remote-input.test.ts
@@ -23,6 +23,38 @@ class Channel extends EventTarget {
receive(value: object) { this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify(value) })); }
}
+it("opts into input feedback only when advertised and retains the same grant after a rejected input", () => {
+ const channel = new Channel();
+ const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
+ const ready = { platform: "windows", mode: "room", state: "ready" };
+ channel.receive(ready);
+ expect(channel.sent).toEqual([{ type: "sync" }, { type: "capabilities" }]);
+ channel.receive({ ...ready, capabilities: ["coPlay", "text"] });
+ expect(channel.sent).toHaveLength(2);
+ channel.receive({ ...ready, capabilities: ["coPlay", "text", "inputFeedback"] });
+ expect(channel.sent.at(-1)).toEqual({ type: "capabilities", inputFeedback: true });
+ channel.receive({ ...ready, capabilities: ["coPlay", "text", "inputFeedback"] });
+ expect(channel.sent).toHaveLength(3);
+ session.request();
+ const intent = channel.sent.at(-1)?.intent, grant = "f".repeat(64);
+ const active = { ...ready, state: "active", intent, grant, capabilities: ["inputFeedback"] };
+ channel.receive(active);
+ session.input({ type: "key", code: "KeyW", down: true });
+ const beforeFeedback = channel.sent.length;
+ channel.receive({ ...active, inputFailed: true });
+ expect(session.state).toMatchObject({ state: "active", grant, intent, inputFailed: true });
+ expect(channel.sent).toHaveLength(beforeFeedback);
+ session.input({ type: "key", code: "KeyD", down: true });
+ expect(channel.sent.at(-1)).toEqual({ type: "key", code: "KeyD", down: true, grant });
+ channel.receive(active);
+ expect(session.state?.inputFailed).toBeUndefined();
+ expect(session.state?.grant).toBe(grant);
+ expect(channel.readyState).toBe("open");
+ session.release();
+ expect(channel.sent.at(-1)).toEqual({ type: "release", intent });
+ session.dispose();
+});
+
it("updates text availability without replacing an active keyboard grant", () => {
const channel = new Channel();
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
diff --git a/tests/remote-mouse.test.ts b/tests/remote-mouse.test.ts
new file mode 100644
index 00000000..666f8e2e
--- /dev/null
+++ b/tests/remote-mouse.test.ts
@@ -0,0 +1,255 @@
+import { readFileSync } from "node:fs";
+import { runInNewContext } from "node:vm";
+import * as jsxRuntime from "react/jsx-runtime";
+import ts from "typescript";
+import { expect, it, vi } from "vitest";
+import { PLAYER_CONTROLS_IDLE_MS } from "../src/client/ui/control-timing";
+import { REMOTE_TOUCH_HOLD_MS } from "../src/client/remote-control/touchpad";
+
+// Exercise the actual component callbacks without adding a DOM runtime. Native
+// click generation and pointer capture ordering still require Browser acceptance.
+const source = ts.createSourceFile("RemoteMouse.tsx", readFileSync(
+ new URL("../src/client/components/living/RemoteMouse.tsx", import.meta.url), "utf8",
+), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
+const component = source.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "RemoteMouse");
+if (!component) throw new Error("Missing remote mouse owner");
+const executable = ts.transpileModule(`${component.getText(source)}\nRemoteMouse;`, {
+ fileName: "RemoteMouse.tsx",
+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX },
+}).outputText;
+
+type Element = { props: Record<string, any> };
+type Slot = { value?: any; deps?: readonly unknown[]; cleanup?: () => void };
+function mouse(left = 100, touchMode: "direct" | "trackpad" = "direct") {
+ const slots: Slot[] = [], effects: (() => void)[] = [];
+ let index = 0, tree: Element;
+ const listeners = new Map<string, () => void>();
+ const owner = { hidden: false, activeElement: null,
+ addEventListener: (type: string, callback: () => void) => listeners.set(type, callback),
+ removeEventListener: (type: string) => listeners.delete(type),
+ defaultView: { addEventListener: vi.fn(), removeEventListener: vi.fn() } };
+ const layer = { clientWidth: 400, clientHeight: 300 };
+ const positionRef = { current: layer };
+ const widget = { offsetLeft: left, offsetTop: 100, offsetWidth: 44, offsetHeight: 56,
+ getBoundingClientRect: () => ({ left, top: 100 }),
+ style: { setProperty: vi.fn() }, setAttribute: vi.fn(), removeAttribute: vi.fn(), contains: () => false };
+ const mini = { matches: () => false, focus: vi.fn() };
+ const pointer = { button: vi.fn(), moveBy: vi.fn(), wheel: vi.fn(), setMode: vi.fn(), refresh: vi.fn() };
+ const slot = () => slots[index++] ?? (slots[index - 1] = {});
+ const changed = (previous: readonly unknown[] | undefined, next: readonly unknown[]) =>
+ !previous || previous.length !== next.length || next.some((value, i) => !Object.is(value, previous[i]));
+ const effect = (callback: () => (() => void) | void, deps: readonly unknown[]) => {
+ const current = slot();
+ if (!changed(current.deps, deps)) return;
+ current.deps = deps;
+ effects.push(() => { current.cleanup?.(); current.cleanup = callback() || undefined; });
+ };
+ const renderComponent = runInNewContext(executable, {
+ exports: {}, require: () => jsxRuntime, setTimeout, clearTimeout, PLAYER_CONTROLS_IDLE_MS, REMOTE_TOUCH_HOLD_MS,
+ useCopy: () => ({ t: (key: string) => key }), useSurfaceDocument: () => owner, useId: () => "mouse",
+ useState: (initial: unknown) => {
+ const current = slot();
+ if (!("value" in current)) current.value = initial;
+ return [current.value, (value: unknown) => { current.value = typeof value === "function" ? value(current.value) : value; }];
+ },
+ useRef: (initial: unknown) => { const current = slot(); return current.value ??= { current: initial }; },
+ useCallback: (callback: unknown, deps: readonly unknown[]) => {
+ const current = slot();
+ if (changed(current.deps, deps)) { current.deps = deps; current.value = callback; }
+ return current.value;
+ },
+ useEffect: effect, useLayoutEffect: effect, Glyph: () => null,
+ });
+ const nodes = (element: Element): Element[] => [element, ...[element.props.children].flat(Infinity)
+ .filter((child): child is Element => Boolean(child && typeof child === "object" && "props" in child)).flatMap(nodes)];
+ const find = (name: string) => nodes(tree).find(node => node.props.className?.split(" ").includes(name))!;
+ const render = () => {
+ index = 0;
+ tree = renderComponent({ pointer, positionRef, touchMode, pointAt: () => ({ x: .25, y: .3 }) });
+ find("lr-remote-mouse-widget").props.ref.current = widget;
+ find("lr-remote-mouse-mini").props.ref.current = mini;
+ effects.splice(0).forEach(callback => callback());
+ };
+ render();
+ const event = (x = 0, y = 0) => ({ pointerId: 1, button: 0, clientX: x, clientY: y,
+ currentTarget: { setPointerCapture: vi.fn() }, preventDefault() {}, stopPropagation() {} });
+ return { pointer, widget, owner, listeners, find, render, event,
+ setTouchMode: (next: typeof touchMode) => { touchMode = next; render(); },
+ expanded: () => !find("lr-remote-mouse-shell").props.inert,
+ docked: () => find("lr-remote-mouse-widget").props["data-docked"],
+ dispose: () => slots.forEach(current => current.cleanup?.()) };
+}
+
+it("retains the compact trackpad option while the full mouse is minimized", () => {
+ const view = mouse(100, "trackpad");
+ try {
+ expect(view.find("lr-remote-touch-cursor")).toBeDefined();
+ expect(view.expanded()).toBe(false);
+ expect(view.pointer.setMode).toHaveBeenLastCalledWith("trackpad");
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 0 }); view.render();
+ expect(view.expanded()).toBe(true);
+ view.find("lr-remote-mouse-minimize").props.onClick(); view.render();
+ expect(view.expanded()).toBe(false);
+ expect(view.find("lr-remote-touch-cursor")).toBeDefined();
+ expect(view.pointer.setMode).toHaveBeenLastCalledWith("trackpad");
+ view.setTouchMode("direct");
+ expect(view.find("lr-remote-touch-cursor")).toBeUndefined();
+ expect(view.pointer.setMode).toHaveBeenLastCalledWith("direct");
+ } finally { view.dispose(); }
+});
+
+it("moves only the mini widget and suppresses its following pointer click after capture ends", () => {
+ const view = mouse();
+ try {
+ expect(view.find("lr-remote-touch-cursor")).toBeUndefined();
+ view.find("lr-remote-mouse-mini").props.onPointerDown(view.event());
+ view.find("lr-remote-mouse").props.onPointerMove(view.event(20, 12));
+ view.find("lr-remote-mouse").props.onPointerUp(view.event(20, 12));
+ view.find("lr-remote-mouse").props.onLostPointerCapture(view.event(20, 12));
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 1 }); view.render();
+ expect(view.widget.style.setProperty).toHaveBeenCalled();
+ expect(view.pointer.moveBy).not.toHaveBeenCalled();
+ expect(view.pointer.button).not.toHaveBeenCalled();
+ expect(view.expanded()).toBe(false);
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 0 }); view.render();
+ expect(view.expanded()).toBe(true);
+ expect(view.find("lr-remote-touch-cursor")).toBeDefined();
+ expect(view.pointer.setMode).toHaveBeenLastCalledWith("mouse", { x: .25, y: .3 });
+ } finally { view.dispose(); }
+});
+
+it("opens with a mini tap and minimizes explicitly while releasing held remote input", () => {
+ const view = mouse();
+ try {
+ view.find("lr-remote-mouse-mini").props.onPointerDown(view.event());
+ view.find("lr-remote-mouse").props.onPointerUp(view.event());
+ view.find("lr-remote-mouse").props.onLostPointerCapture(view.event());
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 1 }); view.render();
+ expect(view.expanded()).toBe(true);
+ view.find("lr-remote-mouse-left").props.onPointerDown(view.event());
+ view.find("lr-remote-mouse-body").props.onPointerDown({ ...view.event(), pointerId: 2 });
+ view.find("lr-remote-mouse").props.onPointerMove({ ...view.event(20, 12), pointerId: 2 });
+ expect(view.pointer.moveBy).toHaveBeenCalledWith(20, 12);
+ view.find("lr-remote-mouse").props.onPointerUp({ ...view.event(20, 12), pointerId: 2 });
+ expect(view.pointer.button.mock.calls).toEqual([[0, true]]);
+ view.find("lr-remote-mouse").props.onPointerMove(view.event(10, 8));
+ expect(view.pointer.moveBy).toHaveBeenLastCalledWith(10, 8);
+ expect(view.widget.style.setProperty).not.toHaveBeenCalled();
+ view.find("lr-remote-mouse-minimize").props.onClick(); view.render();
+ view.find("lr-remote-mouse").props.onLostPointerCapture(view.event());
+ expect(view.expanded()).toBe(false);
+ expect(view.find("lr-remote-touch-cursor")).toBeUndefined();
+ expect(view.pointer.setMode).toHaveBeenLastCalledWith("direct");
+ expect(view.pointer.button.mock.calls).toEqual([[0, true], [0, false]]);
+ } finally { view.dispose(); }
+});
+
+it.each([["left", 0], ["right", 2]] as const)("drags while holding %s and releases once on cancellation", (name, button) => {
+ const view = mouse();
+ try {
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 0 }); view.render();
+ const surface = () => view.find("lr-remote-mouse").props;
+ view.find(`lr-remote-mouse-${name}`).props.onPointerDown(view.event(30, 20));
+ surface().onPointerMove(view.event(32, 21));
+ expect(view.pointer.moveBy).not.toHaveBeenCalled();
+ surface().onPointerMove(view.event(40, 28));
+ surface().onPointerMove(view.event(44, 30));
+ expect(view.pointer.button.mock.calls).toEqual([[button, true]]);
+ expect(view.pointer.moveBy.mock.calls).toEqual([[10, 8], [4, 2]]);
+ expect(view.widget.style.setProperty).not.toHaveBeenCalled();
+ surface().onPointerCancel(view.event(44, 30));
+ surface().onLostPointerCapture(view.event(44, 30));
+ surface().onPointerMove(view.event(60, 60));
+ view.dispose();
+ expect(view.pointer.button.mock.calls).toEqual([[button, true], [button, false]]);
+ expect(view.pointer.moveBy).toHaveBeenCalledTimes(2);
+ } finally { view.dispose(); }
+});
+
+it("retains a shared button until its final contact ends and releases remaining buttons on disposal", () => {
+ const view = mouse();
+ try {
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 0 }); view.render();
+ view.find("lr-remote-mouse-left").props.onPointerDown(view.event());
+ view.find("lr-remote-mouse-left").props.onPointerDown({ ...view.event(), pointerId: 2 });
+ view.find("lr-remote-mouse-right").props.onPointerDown({ ...view.event(), pointerId: 3 });
+ view.find("lr-remote-mouse").props.onPointerUp(view.event());
+ expect(view.pointer.button.mock.calls).toEqual([[0, true], [2, true]]);
+ view.find("lr-remote-mouse").props.onPointerUp({ ...view.event(), pointerId: 2 });
+ view.dispose();
+ expect(view.pointer.button.mock.calls).toEqual([[0, true], [2, true], [0, false], [2, false]]);
+ } finally { view.dispose(); }
+});
+
+it("tucks only an idle edge mini and cancels the timer on interaction, expansion, hiding and disposal", () => {
+ vi.useFakeTimers();
+ const view = mouse(0);
+ try {
+ vi.advanceTimersByTime(PLAYER_CONTROLS_IDLE_MS - 1); view.render(); expect(view.docked()).toBeUndefined();
+ view.find("lr-remote-mouse-mini").props.onPointerDown(view.event());
+ expect(vi.getTimerCount()).toBe(0);
+ view.find("lr-remote-mouse").props.onPointerUp(view.event());
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 1 }); view.render();
+ expect(view.expanded()).toBe(true); expect(vi.getTimerCount()).toBe(0);
+ view.find("lr-remote-mouse-minimize").props.onClick(); view.render();
+ vi.advanceTimersByTime(PLAYER_CONTROLS_IDLE_MS); view.render(); expect(view.docked()).toBe("left");
+ view.find("lr-remote-mouse-mini").props.onPointerDown(view.event());
+ view.find("lr-remote-mouse").props.onPointerMove(view.event(10, 0));
+ view.find("lr-remote-mouse").props.onPointerUp(view.event(10, 0)); view.render();
+ expect(vi.getTimerCount()).toBe(1);
+ view.owner.hidden = true; view.listeners.get("visibilitychange")?.();
+ expect(vi.getTimerCount()).toBe(0);
+ view.dispose();
+ const pending = mouse(0);
+ expect(vi.getTimerCount()).toBe(1); pending.dispose(); expect(vi.getTimerCount()).toBe(0);
+ } finally { view.dispose(); vi.useRealTimers(); }
+});
+
+it("distinguishes wheel taps, scrolling and held middle dragging and retires its timer and button", () => {
+ vi.useFakeTimers();
+ const view = mouse();
+ try {
+ view.find("lr-remote-mouse-mini").props.onClick({ detail: 0 }); view.render();
+ const start = () => view.find("lr-remote-mouse-wheel").props.onPointerDown(view.event());
+ const end = () => {
+ view.find("lr-remote-mouse").props.onPointerUp(view.event());
+ view.find("lr-remote-mouse").props.onLostPointerCapture(view.event());
+ view.find("lr-remote-mouse-wheel").props.onClick({ detail: 1 });
+ };
+ start(); end();
+ expect(view.pointer.button.mock.calls).toEqual([[1, true], [1, false]]);
+ expect(vi.getTimerCount()).toBe(0);
+ view.pointer.button.mockClear();
+
+ start();
+ view.find("lr-remote-mouse").props.onPointerMove(view.event(0, 3));
+ expect(view.pointer.wheel).not.toHaveBeenCalled();
+ view.find("lr-remote-mouse").props.onPointerMove(view.event(0, 14));
+ vi.advanceTimersByTime(REMOTE_TOUCH_HOLD_MS); end();
+ expect(view.pointer.wheel.mock.calls).toEqual([[14]]);
+ expect(view.pointer.button).not.toHaveBeenCalled();
+ expect(view.pointer.moveBy).not.toHaveBeenCalled();
+ expect(vi.getTimerCount()).toBe(0); view.pointer.wheel.mockClear();
+
+ start(); vi.advanceTimersByTime(REMOTE_TOUCH_HOLD_MS - 1);
+ expect(view.pointer.button).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(1); view.render();
+ expect(view.find("lr-remote-mouse-wheel").props["aria-pressed"]).toBe(true);
+ view.find("lr-remote-mouse").props.onPointerMove(view.event(8, 12)); end();
+ expect(view.pointer.button.mock.calls).toEqual([[1, true], [1, false]]);
+ expect(view.pointer.moveBy.mock.calls).toEqual([[8, 12]]);
+ expect(view.pointer.wheel).not.toHaveBeenCalled(); view.pointer.button.mockClear();
+
+ start(); view.find("lr-remote-mouse").props.onPointerCancel(view.event());
+ vi.advanceTimersByTime(REMOTE_TOUCH_HOLD_MS);
+ expect(view.pointer.button).not.toHaveBeenCalled(); expect(vi.getTimerCount()).toBe(0);
+ start(); vi.advanceTimersByTime(REMOTE_TOUCH_HOLD_MS);
+ view.owner.hidden = true; view.listeners.get("visibilitychange")?.();
+ expect(view.pointer.button.mock.calls).toEqual([[1, true], [1, false]]);
+ expect(vi.getTimerCount()).toBe(0);
+ view.pointer.button.mockClear(); view.owner.hidden = false;
+ start(); expect(vi.getTimerCount()).toBe(1);
+ view.dispose(); vi.advanceTimersByTime(REMOTE_TOUCH_HOLD_MS);
+ expect(view.pointer.button).not.toHaveBeenCalled(); expect(vi.getTimerCount()).toBe(0);
+ } finally { view.dispose(); vi.useRealTimers(); }
+});
diff --git a/tests/remote-touchpad.test.ts b/tests/remote-touchpad.test.ts
index 1778f047..fda9bcc7 100644
--- a/tests/remote-touchpad.test.ts
+++ b/tests/remote-touchpad.test.ts
@@ -1,7 +1,119 @@
import { expect, it, vi } from "vitest";
import { RemoteTouchpad } from "../src/client/remote-control/touchpad";
import type { RemoteInputEvent } from "../src/client/remote-control/input-session";
-import type { VideoRotation } from "../src/client/media/video-geometry";
+import { picturePoint, pictureRect, rotatePicturePoint, type VideoRotation } from "../src/client/media/video-geometry";
+
+it("targets the touched picture directly, ignores letterboxing and retires gestures when mouse mode changes", () => {
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
+ try {
+ const video = { videoWidth: 400, videoHeight: 200, style: { objectFit: "contain" }, dataset: {},
+ getBoundingClientRect: () => ({ left: 0, top: 0, width: 400, height: 300 }) } as unknown as HTMLVideoElement;
+ const events: RemoteInputEvent[] = [];
+ const pad = new RemoteTouchpad(() => {
+ const picture = pictureRect(video);
+ return picture && { ...picture, rotation: 0 };
+ }, event => events.push(event), () => {}, () => {},
+ { scale: 1, transform: () => {} });
+ pad.down(1, 320, 10, picturePoint(video, 320, 10)); pad.move(1, 350, 100); pad.up(1);
+ expect(events).toEqual([]); expect(vi.getTimerCount()).toBe(0);
+
+ pad.down(1, 320, 100, picturePoint(video, 320, 100)); pad.up(1);
+ expect(events).toEqual([{ type: "move", x: .8, y: .25 },
+ { type: "button", button: 0, down: true }, { type: "button", button: 0, down: false }]);
+ events.length = 0;
+ pad.down(2, 80, 150, picturePoint(video, 80, 150)); vi.advanceTimersByTime(500);
+ pad.move(2, 160, 190); pad.up(2);
+ expect(events).toEqual([{ type: "move", x: .2, y: .5 }, { type: "button", button: 0, down: true },
+ { type: "move", x: .4, y: .7 }, { type: "button", button: 0, down: false }]);
+
+ events.length = 0;
+ pad.down(3, 100, 150, picturePoint(video, 100, 150)); vi.advanceTimersByTime(500);
+ pad.setMode("trackpad"); pad.up(3);
+ expect(events.filter(event => event.type === "button")).toEqual([
+ { type: "button", button: 0, down: true }, { type: "button", button: 0, down: false }]);
+ expect(events.at(-1)).toEqual({ type: "move", x: .25, y: .5 });
+ expect(vi.getTimerCount()).toBe(0);
+ events.length = 0;
+ pad.down(4, 100, 150, { x: .9, y: .9 }); pad.move(4, 140, 150); pad.up(4);
+ expect(events).toEqual([{ type: "move", x: .35, y: .5 }]);
+ pad.setMode("direct");
+ pad.down(5, 320, 100, picturePoint(video, 320, 100)); pad.up(5);
+ expect(events.at(-3)).toEqual({ type: "move", x: .8, y: .25 });
+ } finally { vi.useRealTimers(); }
+});
+
+it("anchors expansion in source coordinates and refreshes geometry without input", () => {
+ const video = { videoWidth: 400, videoHeight: 200, style: { objectFit: "contain" }, dataset: {},
+ getBoundingClientRect: () => ({ left: 0, top: 0, width: 400, height: 300 }) } as unknown as HTMLVideoElement;
+ const events: RemoteInputEvent[] = [], changed = vi.fn();
+ const pad = new RemoteTouchpad(() => {
+ const picture = pictureRect(video);
+ return picture && { ...picture, rotation: 0 };
+ }, event => events.push(event), () => {}, changed, { scale: 1, transform: () => {} });
+ pad.setMode("trackpad", picturePoint(video, 320, 100));
+ expect(pad.position).toEqual({ x: .8, y: .25 });
+ expect(events).toEqual([{ type: "move", x: .8, y: .25 }]);
+ expect(changed.mock.calls).toEqual([[pad.position]]);
+ pad.moveBy(-40, 20);
+ expect(pad.position.x).toBeCloseTo(.7); expect(pad.position.y).toBeCloseTo(.35);
+ expect(events.at(-1)).toEqual({ type: "move", ...pad.position });
+ expect(changed).toHaveBeenLastCalledWith(pad.position);
+ pad.setMode("direct");
+ events.length = 0; changed.mockClear();
+ pad.setMode("trackpad", picturePoint(video, 320, 10));
+ expect(events).toEqual([{ type: "move", ...pad.position }]);
+ expect(changed.mock.calls).toEqual([[pad.position]]);
+ pad.setMode("trackpad", { x: .1, y: .9 });
+ expect(pad.position).toEqual({ x: .1, y: .9 });
+ expect(events.at(-1)).toEqual({ type: "move", x: .1, y: .9 });
+ events.length = 0; changed.mockClear();
+ pad.setMode("trackpad");
+ expect(changed).not.toHaveBeenCalled();
+ const position = pad.position;
+ pad.refresh();
+ expect(changed.mock.calls).toEqual([[position]]);
+ expect(pad.position).toBe(position);
+ expect(events).toEqual([]);
+ pad.reset();
+});
+
+it("shares virtual buttons with picture gestures until their final owner releases", () => {
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
+ try {
+ const events: RemoteInputEvent[] = [];
+ const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: 0 }), event => events.push(event),
+ () => {}, () => {}, { scale: 1, transform: () => {} });
+ pad.setMode("trackpad");
+ events.length = 0;
+ const buttons = () => events.filter(event => event.type === "button");
+ pad.button(0, true);
+ pad.down(1, 20, 20, null); pad.move(1, 24, 22); pad.up(1);
+ pad.down(2, 20, 20, null); vi.advanceTimersByTime(600); pad.move(2, 70, 45); pad.up(2);
+ expect(buttons()).toEqual([{ type: "button", button: 0, down: true }]);
+ expect(events.at(-1)).toEqual({ type: "move", x: .77, y: .77 });
+ pad.button(0, false);
+ expect(buttons().at(-1)).toEqual({ type: "button", button: 0, down: false });
+
+ events.length = 0;
+ pad.down(3, 20, 20, null); vi.advanceTimersByTime(500);
+ pad.button(0, true); pad.up(3);
+ expect(buttons()).toEqual([{ type: "button", button: 0, down: true }]);
+ pad.button(0, false);
+ expect(buttons()).toHaveLength(2);
+
+ events.length = 0;
+ pad.down(4, 20, 20, null); pad.button(2, true); pad.button(2, false);
+ vi.advanceTimersByTime(600); pad.up(4);
+ expect(buttons()).toEqual([{ type: "button", button: 2, down: true }, { type: "button", button: 2, down: false }]);
+ pad.down(5, 20, 20, null); vi.advanceTimersByTime(500); pad.button(0, true); pad.button(1, true);
+ events.length = 0;
+ pad.moveBy(20, 10);
+ expect(events).toEqual([{ type: "move", ...pad.position }]);
+ pad.cancel(5); pad.reset(); pad.up(5); pad.button(0, false); pad.button(1, false); vi.advanceTimersByTime(600);
+ expect(buttons()).toEqual([{ type: "button", button: 0, down: false }, { type: "button", button: 1, down: false }]);
+ expect(vi.getTimerCount()).toBe(0);
+ } finally { vi.useRealTimers(); }
+});
it("keeps taps, dragging, two-finger gestures and grant retirement distinct", () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
@@ -9,13 +121,15 @@ it("keeps taps, dragging, two-finger gestures and grant retirement distinct", ()
const events: RemoteInputEvent[] = [], scroll: number[] = [];
vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16));
vi.stubGlobal("cancelAnimationFrame", clearTimeout);
- const touchpad = new RemoteTouchpad(() => ({ width: 200, height: 100, rotation: 0 }),
- event => events.push(event), delta => scroll.push(delta), () => {}, { scale: 1, transform: () => {}, visiblePosition: position => position });
- touchpad.down(1, 20, 20); touchpad.up(1);
+ const touchpad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: 0 }),
+ event => events.push(event), delta => scroll.push(delta), () => {}, { scale: 1, transform: () => {} });
+ touchpad.setMode("trackpad");
+ events.length = 0;
+ touchpad.down(1, 20, 20, null); touchpad.up(1);
expect(events).toEqual([{ type: "move", x: .5, y: .5 },
{ type: "button", button: 0, down: true }, { type: "button", button: 0, down: false }]);
events.length = 0;
- touchpad.down(1, 20, 20);
+ touchpad.down(1, 20, 20, null);
touchpad.move(1, 24, 22); // Below the tap threshold: retain, don't lose motion.
touchpad.move(1, 26, 23);
touchpad.move(1, 70, 45); touchpad.up(1);
@@ -23,7 +137,7 @@ it("keeps taps, dragging, two-finger gestures and grant retirement distinct", ()
expect(events.some(event => event.type === "button")).toBe(false);
events.length = 0;
- touchpad.down(1, 20, 20); vi.advanceTimersByTime(500); touchpad.move(1, 500, 45);
+ touchpad.down(1, 20, 20, null); vi.advanceTimersByTime(500); touchpad.move(1, 500, 45);
expect(touchpad.position).toEqual({ x: 1, y: 1 });
touchpad.reset(); touchpad.up(1); vi.advanceTimersByTime(1000);
expect(events.filter(event => event.type === "button")).toEqual([
@@ -31,20 +145,20 @@ it("keeps taps, dragging, two-finger gestures and grant retirement distinct", ()
]);
events.length = 0;
- touchpad.down(1, 20, 20); touchpad.down(2, 40, 20); touchpad.up(2);
+ touchpad.down(1, 20, 20, null); touchpad.down(2, 40, 20, null); touchpad.up(2);
touchpad.cancel(2); // Normal pointer-up also emits lostpointercapture.
touchpad.up(1);
expect(events.slice(-2)).toEqual([
{ type: "button", button: 2, down: true }, { type: "button", button: 2, down: false },
]);
events.length = 0;
- touchpad.down(1, 20, 20); touchpad.down(2, 40, 20);
+ touchpad.down(1, 20, 20, null); touchpad.down(2, 40, 20, null);
touchpad.move(1, 20, 50); touchpad.move(2, 40, 50);
touchpad.up(1); touchpad.move(2, 40, 80); touchpad.up(2);
expect(scroll.reduce((sum, delta) => sum + delta, 0)).toBe(-30);
expect(events.some(event => event.type === "button")).toBe(false);
- touchpad.down(1, 20, 20); touchpad.reset(); vi.advanceTimersByTime(600);
+ touchpad.down(1, 20, 20, null); touchpad.reset(); vi.advanceTimersByTime(600);
expect(events.some(event => event.type === "button")).toBe(false);
} finally { vi.useRealTimers(); vi.unstubAllGlobals(); }
});
@@ -55,9 +169,10 @@ it("keeps pinch/pan local and never turns the remaining finger into a remote cli
vi.stubGlobal("cancelAnimationFrame", clearTimeout);
try {
const input = vi.fn(), scroll = vi.fn(), transform = vi.fn();
- const viewport = { scale: 1, transform, visiblePosition: (position: { x: number; y: number }) => position };
- const pad = new RemoteTouchpad(() => ({ width: 200, height: 100, rotation: 0 }), input, scroll, () => {}, viewport);
- pad.down(1, 40, 50); pad.down(2, 160, 50);
+ const viewport = { scale: 1, transform };
+ const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: 0 }), input, scroll, () => {}, viewport);
+ pad.setMode("trackpad");
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
input.mockClear();
pad.move(1, 10, 50); pad.move(2, 190, 50);
vi.advanceTimersByTime(16);
@@ -67,7 +182,15 @@ it("keeps pinch/pan local and never turns the remaining finger into a remote cli
expect(scroll).not.toHaveBeenCalled();
viewport.scale = 1.5;
- pad.down(1, 40, 50); pad.down(2, 160, 50);
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
+ pad.move(1, 60, 70); pad.move(2, 180, 70);
+ vi.advanceTimersByTime(16);
+ expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
+ expect(scroll).not.toHaveBeenCalled();
+ pad.up(1); pad.up(2);
+
+ viewport.scale = .5;
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
pad.move(1, 60, 70); pad.move(2, 180, 70);
vi.advanceTimersByTime(16);
expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
@@ -76,27 +199,111 @@ it("keeps pinch/pan local and never turns the remaining finger into a remote cli
// A zoomed two-finger tap remains a right-click, and cancellation discards
// pending local movement instead of replaying it after the grant ends.
- pad.down(1, 40, 50); pad.down(2, 160, 50);
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
pad.up(1); pad.up(2);
expect(input.mock.calls.slice(-2)).toEqual([
[{ type: "button", button: 2, down: true }], [{ type: "button", button: 2, down: false }],
]);
- pad.down(1, 40, 50); pad.down(2, 160, 50);
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
pad.move(1, 10, 50); transform.mockClear(); pad.reset();
vi.advanceTimersByTime(600);
expect(transform).not.toHaveBeenCalled();
} finally { vi.useRealTimers(); vi.unstubAllGlobals(); }
});
+it.each([0, 90, 180, 270] as const)("keeps the full mouse fixed while picture pan and pinch reanchor its local target (%s degrees)", rotation => {
+ vi.useFakeTimers();
+ vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16));
+ vi.stubGlobal("cancelAnimationFrame", clearTimeout);
+ const input = vi.fn(), scroll = vi.fn(), changed = vi.fn();
+ let picture = { left: 0, top: 0, width: 200, height: 100, rotation };
+ const transform = vi.fn((from: { x: number; y: number }, to: { x: number; y: number }, ratio: number) => {
+ picture = { ...picture, left: to.x + (picture.left - from.x) * ratio,
+ top: to.y + (picture.top - from.y) * ratio, width: picture.width * ratio, height: picture.height * ratio };
+ });
+ const pad = new RemoteTouchpad(() => picture,
+ input, scroll, changed, { scale: 1, transform });
+ const clientTarget = () => {
+ const point = rotatePicturePoint(pad.position, rotation);
+ return { x: picture.left + point.x * picture.width, y: picture.top + point.y * picture.height };
+ };
+ try {
+ pad.setMode("mouse"); input.mockClear(); changed.mockClear();
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
+ expect(changed).not.toHaveBeenCalled();
+ pad.move(1, 60, 70); pad.move(2, 180, 70); vi.advanceTimersByTime(16);
+ expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
+ expect(clientTarget().x).toBeCloseTo(100); expect(clientTarget().y).toBeCloseTo(50);
+ expect(pad.position).toEqual(rotatePicturePoint({ x: .4, y: .3 }, rotation, true));
+ pad.up(1); pad.up(2);
+
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
+ pad.move(1, 10, 50); pad.move(2, 190, 50); vi.advanceTimersByTime(16);
+ expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 100, y: 50 }, 1.5);
+ expect(clientTarget().x).toBeCloseTo(100); expect(clientTarget().y).toBeCloseTo(50);
+ pad.move(1, 30, 70); pad.move(2, 210, 70); vi.advanceTimersByTime(16);
+ expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
+ expect(clientTarget().x).toBeCloseTo(100); expect(clientTarget().y).toBeCloseTo(50);
+ pad.up(1); pad.move(2, 220, 80); pad.up(2);
+ expect(input).not.toHaveBeenCalled(); expect(scroll).not.toHaveBeenCalled();
+ expect(changed).toHaveBeenLastCalledWith(pad.position);
+
+ // Past the source edge the target stays on that edge instead of inventing
+ // coordinates in the gutter; rendering/follow owns its final reachability.
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
+ pad.move(1, 440, 50); pad.move(2, 560, 50); vi.advanceTimersByTime(16);
+ expect(rotatePicturePoint(pad.position, rotation).x).toBeCloseTo(0);
+ expect(input).not.toHaveBeenCalled(); expect(scroll).not.toHaveBeenCalled();
+ pad.up(1); pad.up(2);
+
+ pad.button(0, true); pad.button(2, true);
+ pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
+ pad.move(1, 20, 50); transform.mockClear(); input.mockClear();
+ pad.cancel(1); pad.reset(); pad.up(2); vi.advanceTimersByTime(600);
+ expect(transform).not.toHaveBeenCalled();
+ expect(input.mock.calls).toEqual([
+ [{ type: "button", button: 0, down: false }], [{ type: "button", button: 2, down: false }],
+ ]);
+ expect(vi.getTimerCount()).toBe(0);
+ } finally { pad.reset(); vi.useRealTimers(); vi.unstubAllGlobals(); }
+});
+
+it("moves across the full source through the rendered zoom without a delayed click", () => {
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
+ try {
+ const events: RemoteInputEvent[] = [];
+ const changed = vi.fn(), scroll = vi.fn(), transform = vi.fn();
+ // pictureRect already includes the local zoom; do not divide by scale again.
+ const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 400, height: 200, rotation: 0 }),
+ event => events.push(event), scroll, changed, { scale: 2, transform });
+ pad.setMode("trackpad");
+ events.length = 0;
+ pad.down(1, 20, 20, null);
+ pad.moveBy(100, 50);
+ expect(pad.position).toEqual({ x: .75, y: .75 });
+ expect(changed).toHaveBeenLastCalledWith(pad.position);
+ expect(events).toEqual([{ type: "move", x: .75, y: .75 }]);
+ vi.advanceTimersByTime(600); pad.up(1);
+ expect(events).toHaveLength(1);
+ pad.moveBy(400, 200);
+ expect(events.at(-1)).toEqual({ type: "move", x: 1, y: 1 });
+ expect(scroll).not.toHaveBeenCalled();
+ expect(transform).not.toHaveBeenCalled();
+ expect(vi.getTimerCount()).toBe(0);
+ } finally { vi.useRealTimers(); }
+});
+
it.each([
[0, .75, .5], [90, .5, .25], [180, .25, .5], [270, .5, .75],
-] as const)("keeps touch motion aligned with the rotated picture and releases a held drag (%s°)", (rotation, x, y) => {
+] as const)("aligns picture and widget motion with rotation and releases a held drag (%s°)", (rotation, x, y) => {
vi.useFakeTimers();
try {
const events: RemoteInputEvent[] = [];
- const pad = new RemoteTouchpad(() => ({ width: 200, height: 100, rotation: rotation as VideoRotation }),
- event => events.push(event), () => {}, () => {}, { scale: 1, transform: () => {}, visiblePosition: position => position });
- pad.down(1, 20, 20);
+ const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: rotation as VideoRotation }),
+ event => events.push(event), () => {}, () => {}, { scale: 1, transform: () => {} });
+ pad.setMode("trackpad");
+ events.length = 0;
+ pad.down(1, 20, 20, null);
vi.advanceTimersByTime(500);
pad.move(1, 70, 20);
expect(events.at(-1)).toEqual({ type: "move", x, y });
@@ -104,5 +311,10 @@ it.each([
expect(events.at(-1)).toEqual({ type: "button", button: 0, down: false });
pad.up(1);
expect(events.filter(event => event.type === "button")).toHaveLength(2);
+ events.length = 0;
+ pad.position = { x: .5, y: .5 };
+ pad.moveBy(50, 0);
+ vi.advanceTimersByTime(600);
+ expect(events).toEqual([{ type: "move", x, y }]);
} finally { vi.useRealTimers(); }
});
diff --git a/tests/remote-viewport.test.ts b/tests/remote-viewport.test.ts
new file mode 100644
index 00000000..7ff175d8
--- /dev/null
+++ b/tests/remote-viewport.test.ts
@@ -0,0 +1,118 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { RemoteViewport } from "../src/client/remote-control/viewport";
+import { RemoteTouchpad } from "../src/client/remote-control/touchpad";
+import { picturePoint, pictureRect, rotatePicturePoint, type VideoRotation } from "../src/client/media/video-geometry";
+import type { RemoteInputEvent } from "../src/client/remote-control/input-session";
+
+afterEach(() => vi.unstubAllGlobals());
+
+// The boundary models CSS translate/scale around the video cell's center.
+// Camera bounds, fitted picture and source coordinates use production owners.
+function viewport(width = 400, height = 200, rotation: VideoRotation = 0) {
+ const frame = { left: 20, top: 30, width, height };
+ const rect = (left: number, top: number, width: number, height: number) =>
+ ({ left, top, width, height, right: left + width, bottom: top + height });
+ const surface = { getBoundingClientRect: () => rect(frame.left, frame.top, frame.width, frame.height) } as HTMLElement;
+ const style = { transform: "", clipPath: "", objectFit: "fill", width: `${width}px`, height: `${height}px` };
+ const video = { videoWidth: width, videoHeight: height, style, dataset: { pictureRotation: String(rotation) },
+ getBoundingClientRect: () => {
+ const match = /translate\(([-\d.e]+)px, ([-\d.e]+)px\) scale\(([-\d.e]+)\)/.exec(style.transform);
+ const x = Number(match?.[1] ?? 0), y = Number(match?.[2] ?? 0), scale = Number(match?.[3] ?? 1);
+ return rect(frame.left + frame.width * (1 - scale) / 2 + x,
+ frame.top + frame.height * (1 - scale) / 2 + y, frame.width * scale, frame.height * scale);
+ } } as unknown as HTMLVideoElement;
+ vi.stubGlobal("getComputedStyle", () => style);
+ const changed = vi.fn(), view = new RemoteViewport(video, surface, changed);
+ const point = (position: { x: number; y: number }) => {
+ const picture = pictureRect(video)!, rotated = rotatePicturePoint(position, rotation);
+ return { x: picture.left + rotated.x * picture.width, y: picture.top + rotated.y * picture.height };
+ };
+ return { frame, video, surface, style, changed, view, point };
+}
+
+it("zooms out with black gutters, bounds panning by the tool and restores the original cell", () => {
+ const { view, video, style, surface, point } = viewport();
+ const center = { x: 220, y: 130 };
+ view.transform(center, center, .1);
+ expect(view.scale).toBe(.5);
+ expect(pictureRect(video)).toEqual({ left: 120, top: 80, width: 200, height: 100 });
+ expect(picturePoint(video, 30, 40)).toBeNull();
+ expect(style.clipPath).toContain("inset(");
+ expect(style.clipPath).toContain("-");
+ view.follow({ x: 1, y: 1 }, { right: 128, bottom: 172 });
+ expect(point({ x: 1, y: 1 }).y + 172).toBe(surface.getBoundingClientRect().bottom);
+ view.transform(center, center, 20);
+ expect(view.scale).toBe(4);
+ view.reset();
+ view.follow({ x: .5, y: .5 }, { right: 120, bottom: 80 });
+ view.transform(center, { x: 10000, y: 10000 }, 1);
+ const picture = pictureRect(video)!, frame = surface.getBoundingClientRect();
+ expect(view.scale).toBe(1);
+ expect(style.transform).toContain("translate(");
+ expect(style.clipPath).not.toBe("");
+ expect(picture.left).toBe(frame.left + 120);
+ expect(picture.top).toBe(frame.top + 80);
+ view.transform(center, { x: -10000, y: -10000 }, 1);
+ const opposite = pictureRect(video)!;
+ expect(opposite.left + opposite.width).toBe(frame.right - 120);
+ expect(opposite.top + opposite.height).toBe(frame.bottom - 80);
+ view.reset();
+ view.transform(center, { x: 10000, y: 10000 }, 1);
+ expect(pictureRect(video)).toEqual({ left: frame.left, top: frame.top, width: frame.width, height: frame.height });
+ view.reset();
+ expect(style.transform).toBe(""); expect(style.clipPath).toBe("");
+});
+
+it.each([0, 90, 180, 270] as const)("follows the source point without changing it after rotation %s", rotation => {
+ const { view, point, surface, changed } = viewport(300, 300, rotation);
+ const position = { x: 1, y: 1 }, original = { ...position }, room = { right: 128, bottom: 172 };
+ view.follow(position, room);
+ const cursor = point(position), frame = surface.getBoundingClientRect();
+ expect(cursor.x).toBeGreaterThanOrEqual(frame.left);
+ expect(cursor.y).toBeGreaterThanOrEqual(frame.top);
+ expect(cursor.x + room.right).toBeLessThanOrEqual(frame.right);
+ expect(cursor.y + room.bottom).toBeLessThanOrEqual(frame.bottom);
+ expect(position).toEqual(original);
+ changed.mockClear();
+ view.follow(position, room);
+ expect(changed).not.toHaveBeenCalled();
+});
+
+it("uses actual tool room even when only a narrow strip of picture remains", () => {
+ const { view, point, video, surface } = viewport(390, 185);
+ const position = { x: 1, y: 1 };
+ view.follow(position, { right: 128, bottom: 172 });
+ expect(point(position)).toEqual({ x: 282, y: 43 });
+ expect(pictureRect(video)!.top).toBeLessThan(surface.getBoundingClientRect().top);
+ expect(pictureRect(video)!.top + pictureRect(video)!.height - surface.getBoundingClientRect().top).toBe(13);
+ // An oversized tool still leaves a pixel of picture, rather than losing it.
+ view.follow(position, { right: 1000, bottom: 1000 });
+ expect(point(position)).toEqual({ x: 21, y: 31 });
+});
+
+it("republishes geometry on resize and reaches the full remote source beyond the visible crop", () => {
+ const { view, video, point, frame } = viewport();
+ const events: RemoteInputEvent[] = [], room = { right: 128, bottom: 172 };
+ const pad = new RemoteTouchpad(() => ({ ...pictureRect(video)!, rotation: 0 }), event => events.push(event),
+ () => {}, position => view.follow(position, room), view);
+ pad.setMode("mouse");
+ pad.button(0, true);
+ pad.moveBy(400, 200);
+ const position = pad.position;
+ expect(position).toEqual({ x: 1, y: 1 });
+ expect(events.at(-1)).toEqual({ type: "move", x: 1, y: 1 });
+ expect(point(position).y + room.bottom).toBe(frame.top + frame.height);
+ frame.height = 185;
+ view.refresh();
+ const count = events.length;
+ pad.refresh();
+ expect(pad.position).toBe(position);
+ expect(events).toHaveLength(count);
+ expect(point(position).y + room.bottom).toBe(frame.top + frame.height);
+ pad.reset();
+ expect(events.at(-1)).toEqual({ type: "button", button: 0, down: false });
+ view.follow(position, { right: 22, bottom: 28 });
+ expect(point(position)).toEqual({ x: frame.left + frame.width - 22, y: frame.top + frame.height - 28 });
+ view.follow(position, null);
+ expect(video.style.transform).toBe("");
+});
diff --git a/tests/room-preferences.test.ts b/tests/room-preferences.test.ts
index 4226c25c..c3b9604d 100644
--- a/tests/room-preferences.test.ts
+++ b/tests/room-preferences.test.ts
@@ -15,11 +15,13 @@ function storage() {
it("keeps independent room-tool choices across reads without storing private runtime fields", () => {
const values = storage();
- saveRoomPreferences({ pictureFit: "cover", pictureRotation: 270, routePreference: "p2p", keyboardPage: "text", combineKeys: true });
- saveRoomPreferences({ remoteControlMode: "password", remoteReleaseKey: 9, remoteStopKey: 8,
+ expect(readRoomPreferences().previewAutoPause).toBe(true);
+ expect(readRoomPreferences().remoteTouchMode).toBe("direct");
+ saveRoomPreferences({ pictureFit: "cover", pictureRotation: 270, previewAutoPause: false, routePreference: "p2p", keyboardPage: "text", combineKeys: true });
+ saveRoomPreferences({ remoteControlMode: "password", remoteReleaseKey: 9, remoteStopKey: 8, remoteTouchMode: "trackpad",
voiceListeningVolume: 1.5, voiceInput: { deviceId: "", volume: 2, voiceProcessing: true } });
- expect(readRoomPreferences()).toMatchObject({ pictureFit: "cover", pictureRotation: 270, routePreference: "p2p", keyboardPage: "text",
- combineKeys: true, remoteControlMode: "password", remoteReleaseKey: 9, remoteStopKey: 8,
+ expect(readRoomPreferences()).toMatchObject({ pictureFit: "cover", pictureRotation: 270, previewAutoPause: false, routePreference: "p2p", keyboardPage: "text",
+ combineKeys: true, remoteControlMode: "password", remoteReleaseKey: 9, remoteStopKey: 8, remoteTouchMode: "trackpad",
voiceListeningVolume: 1.5, voiceInput: { deviceId: "", volume: 2, voiceProcessing: true } });
// Storage accepts only the preference contract, even if a caller supplies a
@@ -33,8 +35,10 @@ it("keeps independent room-tool choices across reads without storing private run
for (const field of ["password", "grant", "text", "modifiers", "controller"]) expect(saved).not.toHaveProperty(field);
expect([...values.values()][0]).not.toContain("private");
expect(readRoomPreferences().pictureFit).toBe("cover");
+ saveRoomPreferences(JSON.parse('{"remoteTouchMode":"unknown"}'));
+ expect(readRoomPreferences().remoteTouchMode).toBe("trackpad");
values.set("piik:room-preferences:v1", JSON.stringify({ pictureFit: "cover", pictureRotation: 45 }));
- expect(readRoomPreferences()).toMatchObject({ pictureFit: "cover", pictureRotation: 0 });
+ expect(readRoomPreferences()).toMatchObject({ pictureFit: "cover", pictureRotation: 0, remoteTouchMode: "direct" });
values.set("piik:room-preferences:v1", JSON.stringify({ overlayAppearance: { scale: 1.2, opacity: .7 } }));
expect(readRoomPreferences().overlayAppearance).toEqual({ scale: 1.2, opacity: .7, speed: 1, colorBySender: false });
});
@@ -46,7 +50,7 @@ it("rejects malformed saved data and invalid patches while retaining useful defa
const [key] = [...values.keys()];
saveRoomPreferences(JSON.parse('{"remoteControlMode":"off"}'));
expect(readRoomPreferences().remoteControlMode).toBe("room");
- for (const raw of ["{", "null", JSON.stringify({ overlayAppearance: { scale: 100, opacity: 0 } })]) {
+ for (const raw of ["{", "null", JSON.stringify({ overlayAppearance: { scale: 100, opacity: 0 } }), JSON.stringify({ remoteTouchMode: "unknown" })]) {
values.set(key, raw);
expect(readRoomPreferences()).toEqual(defaults);
}
@@ -66,7 +70,7 @@ it("restores overlay choices in a new room without restoring messages or room au
it("keeps room tools working when Browser storage is unavailable", () => {
vi.stubGlobal("window", { get localStorage() { throw new Error("storage denied"); } });
- expect(readRoomPreferences()).toMatchObject({ pictureFit: "contain", routePreference: "auto", remoteControlMode: "ask" });
+ expect(readRoomPreferences()).toMatchObject({ pictureFit: "contain", routePreference: "auto", remoteControlMode: "ask", remoteTouchMode: "direct" });
expect(() => saveRoomPreferences({ keyboardPage: "symbols" })).not.toThrow();
const session = new RoomInteractionSession(() => true);
session.setOverlayEnabled(true);
diff --git a/tests/room-voice-browser-endpoint.test.ts b/tests/room-voice-browser-endpoint.test.ts
new file mode 100644
index 00000000..36a923f9
--- /dev/null
+++ b/tests/room-voice-browser-endpoint.test.ts
@@ -0,0 +1,77 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { BrowserVoiceEndpoint } from "../src/client/room-voice/browser-endpoint";
+import type { ClientMessage, RoomVoiceState } from "../src/shared/protocol";
+
+const candidate = (index: number) => ({
+ candidate: `candidate:${index} 1 UDP 100 192.0.2.1 ${40000 + index} typ host`,
+ sdpMid: "0", sdpMLineIndex: 0,
+});
+
+class Peer {
+ static instances: Peer[] = [];
+ connectionState: RTCPeerConnectionState = "connected";
+ signalingState: RTCSignalingState = "stable";
+ localDescription: RTCSessionDescriptionInit | null = null;
+ remoteDescription: RTCSessionDescriptionInit | null = null;
+ onicecandidate: ((event: { candidate: RTCIceCandidateInit | null }) => void) | null = null;
+ getTransceivers = () => [];
+ addTransceiver = () => {};
+ createDataChannel = () => {};
+ createOffer = async () => ({ type: "offer" as const, sdp: "offer" });
+ createAnswer = async () => ({ type: "answer" as const, sdp: "answer" });
+ setLocalDescription = async (description: RTCSessionDescriptionInit) => {
+ this.localDescription = description;
+ this.signalingState = description.type === "offer" ? "have-local-offer" : "stable";
+ for (let index = 0; index < 65; index++) this.onicecandidate?.({ candidate: candidate(index) });
+ };
+ setRemoteDescription = async (description: RTCSessionDescriptionInit) => { this.remoteDescription = description; };
+ addIceCandidate = vi.fn(async (_candidate: RTCIceCandidateInit) => {});
+ close = vi.fn(() => { this.connectionState = "closed"; });
+ constructor() { Peer.instances.push(this); }
+}
+
+afterEach(() => { vi.unstubAllGlobals(); Peer.instances = []; });
+
+it.each(["local", "remote"])("retains and flushes the first 64 %s voice candidates without failing the transport", async phase => {
+ vi.stubGlobal("RTCPeerConnection", Peer);
+ const messages: ClientMessage[] = [];
+ const endpoint = new BrowserVoiceEndpoint({ iceServers: [] }, message => { messages.push(message); return true; }, () => {});
+ const connection = { connectionId: "voice-connection", transport: "peer" as const,
+ parentPeerId: phase === "local" ? "self" : "other", childPeerId: phase === "local" ? "other" : "self",
+ revision: 1, pending: false, send: [], receive: [] };
+ const authority: RoomVoiceState = { type: "room-voice-state", revision: 1, enabled: true, hostPeerId: "self",
+ members: [{ peerId: "self", connected: true, joined: true, upstream: { kind: "none" } }],
+ publications: [], connections: [connection], unavailable: [] };
+ try {
+ endpoint.setAuthority(authority, "self");
+ await endpoint.reconcile();
+ const pc = Peer.instances[0]!;
+ if (phase === "local") {
+ expect(messages.filter(message => message.type === "room-voice-signal" && message.candidate)
+ .map(message => message.type === "room-voice-signal" ? message.candidate : undefined))
+ .toEqual(Array.from({ length: 64 }, (_, index) => candidate(index)));
+ } else {
+ for (let index = 0; index < 65; index++) await endpoint.receive({
+ type: "room-voice-signal", connectionId: connection.connectionId, candidate: candidate(index),
+ });
+ expect(pc.addIceCandidate).not.toHaveBeenCalled();
+ await endpoint.receive({ type: "room-voice-signal", connectionId: connection.connectionId,
+ description: { revision: 1, sdp: { type: "offer", sdp: "offer" }, tracks: [] } });
+ expect(pc.addIceCandidate.mock.calls.map(([value]) => value))
+ .toEqual(Array.from({ length: 64 }, (_, index) => candidate(index)));
+ }
+ expect(messages.filter(message => message.type === "room-voice-transport" && message.state === "failed")).toEqual([]);
+ expect(endpoint.connected).toBe(true);
+ expect(pc.close).not.toHaveBeenCalled();
+
+ // ICE state reports failure; the room's subsequent authority update owns retirement.
+ pc.connectionState = "failed";
+ await endpoint.reconcile();
+ expect(messages).toContainEqual({ type: "room-voice-transport", connectionId: connection.connectionId, state: "failed" });
+ endpoint.setAuthority({ ...authority, revision: 2, connections: [] }, "self");
+ expect(pc.close).toHaveBeenCalledOnce();
+ const count = messages.length;
+ pc.onicecandidate?.({ candidate: candidate(99) });
+ expect(messages).toHaveLength(count);
+ } finally { endpoint.close(); }
+});
diff --git a/tests/room-voice-client.test.ts b/tests/room-voice-client.test.ts
index 5f184a19..8e9bc531 100644
--- a/tests/room-voice-client.test.ts
+++ b/tests/room-voice-client.test.ts
@@ -5,7 +5,8 @@ import { clientMessageSchema, type ClientMessage, type RoomVoiceState } from "..
afterEach(() => vi.unstubAllGlobals());
function microphone() {
- const track = { readyState: "live", contentHint: "", onended: null as null | (() => void),
+ const events = new EventTarget();
+ const track = { kind: "audio", addEventListener: events.addEventListener.bind(events), removeEventListener: events.removeEventListener.bind(events), readyState: "live", contentHint: "", onended: null as null | (() => void),
stop: vi.fn(() => { track.readyState = "ended"; }) };
const stream = { getTracks: () => [track], getAudioTracks: () => [track] } as unknown as MediaStream;
return { track, stream };
diff --git a/tests/route-transition.test.ts b/tests/route-transition.test.ts
index f09b1eb4..48042487 100644
--- a/tests/route-transition.test.ts
+++ b/tests/route-transition.test.ts
@@ -6,6 +6,7 @@ import type {
PreparedRouteCandidate,
} from "../src/shared/protocol.ts";
import { HostSfuRoute } from "../src/client/media/host-sfu-route.ts";
+import { NativeSfuPublisher } from "../src/client/native/native-sfu-publisher.ts";
import { QUALITY_PROFILES } from "../src/client/media/quality.ts";
import {
invalidateSenderQualityEvidence,
@@ -711,6 +712,66 @@ describe("minimal route transition contracts", () => {
expect(evidence()?.state).toBe("unknown");
});
+ it("rebinds source backends through the existing SFU authority after retiring the old publisher", async () => {
+ const send = vi.fn(() => true);
+ let native = false;
+ let stream = {} as MediaStream;
+ let releaseBrowser!: () => void;
+ const retiring = new Promise<void>(resolve => { releaseBrowser = resolve; });
+ const browser = createFakePublisher([], "browser");
+ browser.disconnect.mockReturnValueOnce(retiring);
+ const media = { codec: "h264" as const, layers: [{ rid: "", width: 1280, height: 720, bitrate: 1_000_000 }], audio: false, audioBitrate: 0 };
+ const control = {
+ preparePublication: vi.fn(async () => ({ description: { type: "offer" as const, sdp: "native encoded source" }, media })),
+ publicationMedia: vi.fn(async () => media), acceptPublicationSignal: vi.fn(async () => {}),
+ closePublication: vi.fn(async () => {}), onEvent: vi.fn(() => () => {}),
+ };
+ const publishers: Array<NativeSfuPublisher | typeof browser> = [];
+ const route = new HostSfuRoute({
+ getStream: () => stream, getProfile: () => QUALITY_PROFILES["720p30"],
+ getVideoCodec: () => native ? "h264" : "vp8", reconcileChildren: () => {}, send,
+ createPublisher: onDisconnected => {
+ const publisher = native ? new NativeSfuPublisher(control, "native-source", { iceServers: [] }, { send, onDisconnected })
+ : publishers.length === 0 ? browser : createFakePublisher([], "camera");
+ publishers.push(publisher);
+ return publisher;
+ },
+ });
+ await route.acceptAndWait({ revision: 1, phase: "active", assignment: hostAssignment("browser-publication") });
+ await route.acceptConfig(sfuConfig(1, "browser-publication"));
+ send.mockClear();
+ native = true;
+ stream = {} as MediaStream;
+ const replacing = route.replaceStream(stream, true);
+ expect(send).toHaveBeenCalledWith({ type: "route-failed", revision: 1, phase: "active", connectionId: sfuConfig(1).connectionId });
+ expect(send).not.toHaveBeenCalledWith({ type: "refresh-sfu", revision: 1 });
+ expect(browser.replaceStream).not.toHaveBeenCalled();
+ const accepting = route.acceptAndWait({ revision: 2, phase: "active", assignment: hostAssignment("native-publication") });
+ const configuring = route.acceptConfig(sfuConfig(2, "native-publication"));
+ await vi.waitFor(() => expect(publishers[1]).toBeInstanceOf(NativeSfuPublisher));
+ expect(control.preparePublication).not.toHaveBeenCalled();
+ releaseBrowser();
+ await Promise.all([replacing, accepting, configuring]);
+ expect(control.preparePublication).toHaveBeenCalledExactlyOnceWith(
+ "native-source", "native-publication", sfuConfig(2).connectionId, { iceServers: [] },
+ );
+ expect(send).toHaveBeenCalledWith(expect.objectContaining({
+ type: "sfu-signal", kind: "description", description: { type: "offer", sdp: "native encoded source" }, media,
+ }));
+
+ native = false;
+ stream = {} as MediaStream;
+ await route.replaceStream(stream, true);
+ expect(control.closePublication).toHaveBeenCalledExactlyOnceWith("native-source", "native-publication", sfuConfig(2).connectionId);
+ expect(send).toHaveBeenCalledWith({ type: "route-failed", revision: 2, phase: "active", connectionId: sfuConfig(2).connectionId });
+ await route.acceptAndWait({ revision: 3, phase: "active", assignment: hostAssignment("camera-publication") });
+ await route.acceptConfig(sfuConfig(3, "camera-publication"));
+ expect(publishers[2]).not.toBeInstanceOf(NativeSfuPublisher);
+ expect(publishers[2]).not.toBe(browser);
+ expect(publishers[2]!.activate).toHaveBeenCalledOnce();
+ await route.disconnect();
+ });
+
it("reports and retires an exact active SFU source replacement failure", async () => {
const messages: ClientMessage[] = [];
const publishers: ReturnType<typeof createFakePublisher>[] = [];
diff --git a/tests/sfu-client.test.ts b/tests/sfu-client.test.ts
index c50a4477..7105b5f5 100644
--- a/tests/sfu-client.test.ts
+++ b/tests/sfu-client.test.ts
@@ -690,7 +690,7 @@ describe("embedded SFU browser transport", () => {
expect(onDisconnected).toHaveBeenCalledOnce();
});
- it("bounds pending ICE and reports one terminal failure", async () => {
+ it("bounds pending remote ICE without retiring the publication and flushes retained candidates", async () => {
const { publisher: host, pc, onDisconnected } = await publisher();
for (let index = 0; index < 65; index++) {
await host.acceptSignal(
@@ -700,9 +700,33 @@ describe("embedded SFU browser transport", () => {
}),
);
}
+ expect(pc.close).not.toHaveBeenCalled();
+ expect(onDisconnected).not.toHaveBeenCalled();
+ await host.acceptSignal(signal({ description: { type: "answer", sdp: "answer" } }));
+ expect(pc.addIceCandidate.mock.calls.map(([candidate]) => candidate.candidate))
+ .toEqual(Array.from({ length: 64 }, (_, index) => `candidate:${index}`));
+ pc.state("failed");
+ expect(pc.close).toHaveBeenCalledOnce();
+ expect(onDisconnected).toHaveBeenCalledOnce();
+ });
+
+ it("bounds local ICE during signaling loss and flushes retained candidates on return", async () => {
+ const { publisher: host, pc, send, onDisconnected } = await publisher();
+ pc.state("connected");
+ send.mockReturnValue(false);
+ for (let index = 0; index < 65; index++) {
+ const value = { candidate: `candidate:${index}` };
+ pc.onicecandidate?.({ candidate: { ...value, protocol: "udp", toJSON: () => value } });
+ }
+ expect(pc.close).not.toHaveBeenCalled();
+ expect(onDisconnected).not.toHaveBeenCalled();
+ send.mockClear().mockReturnValue(true);
+ host.updateConfig({ ...config, revision: 5 });
+ expect(send.mock.calls.map(([message]) => message.candidate?.candidate))
+ .toEqual(Array.from({ length: 64 }, (_, index) => `candidate:${index}`));
+ pc.state("failed");
expect(pc.close).toHaveBeenCalledOnce();
expect(onDisconnected).toHaveBeenCalledOnce();
- expect(host.getFailureStage()).toBe("transport");
});
it("changes audio ceilings without touching video capture or sender parameters", async () => {
diff --git a/tests/signaling-admission.test.ts b/tests/signaling-admission.test.ts
index 38f032ae..2873f9e7 100644
--- a/tests/signaling-admission.test.ts
+++ b/tests/signaling-admission.test.ts
@@ -38,7 +38,7 @@ function fixture(role: "host" | "viewer" = "host") {
}
describe("room authentication and site admission", () => {
- it("replays only this page's relay intent after the authenticated page's ordinary capacity report", async () => {
+ it.each([3, 8, 20])("replays this page's relay limit %s after its ordinary capacity report", async (capacity) => {
const { signal, sockets, events } = fixture("viewer");
const wire = JSON.parse(readFileSync(new URL("./fixtures/wire-samples.json", import.meta.url), "utf8"));
const authenticated = JSON.parse(wire.serverMessages.find(
@@ -49,14 +49,14 @@ describe("room authentication and site admission", () => {
sockets[0]!.receive(authenticated);
expect(signal.relayVolunteerRequest).toBeNull();
expect(sockets[0]!.send.mock.calls.map(([raw]) => JSON.parse(raw).type)).not.toContain("set-relay-volunteer");
- expect(signal.setRelayVolunteer(true, 20)).toBe(true);
- sockets[0]!.receive({ type: "relay-volunteer", enabled: true, capacity: 20 });
+ expect(signal.setRelayVolunteer(true, capacity)).toBe(true);
+ sockets[0]!.receive({ type: "relay-volunteer", enabled: true, capacity });
for (const enabled of [true, false]) {
if (!enabled) {
- signal.setRelayVolunteer(false, 20);
+ signal.setRelayVolunteer(false, capacity);
// An earlier enabled acknowledgement must not restore consent while
// the newer withdrawal is in flight or replay it after socket loss.
- sockets.at(-1)!.receive({ type: "relay-volunteer", enabled: true, capacity: 20 });
+ sockets.at(-1)!.receive({ type: "relay-volunteer", enabled: true, capacity });
expect(signal.relayVolunteerRequest?.enabled).toBe(false);
sockets.at(-1)!.receive({ type: "relay-volunteer", enabled: false, capacity: 2 });
}
@@ -69,7 +69,7 @@ describe("room authentication and site admission", () => {
message.type === "relay-capacity" || message.type === "set-relay-volunteer");
expect(reports).toEqual([
{ type: "relay-capacity", downstreamEdges: 3 },
- { type: "set-relay-volunteer", enabled, capacity: 20 },
+ { type: "set-relay-volunteer", enabled, capacity },
]);
}
signal.stop();
diff --git a/tests/viewer-route-health.test.ts b/tests/viewer-route-health.test.ts
index 6000fa40..3b7a08f8 100644
--- a/tests/viewer-route-health.test.ts
+++ b/tests/viewer-route-health.test.ts
@@ -9,7 +9,7 @@ import type { ViewerPeerEvents } from "../src/client/webrtc/viewer-peer";
const source = ts.createSourceFile("ViewerPage.tsx", readFileSync(
new URL("../src/client/pages/ViewerPage.tsx", import.meta.url), "utf8",
), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
-const owners = new Set(["ensurePeer", "reportActivePeerFailure", "noteReceiveFailure"]);
+const owners = new Set(["ensurePeer", "reportActivePeerFailure", "noteReceiveFailure", "clearPeerState"]);
const functions: string[] = [];
function collect(node: ts.Node): void {
if (ts.isFunctionDeclaration(node) && node.name && owners.delete(node.name.text)) {
@@ -23,19 +23,23 @@ const executable = ts.transpile(functions.join("\n"), { target: ts.ScriptTarget.
afterEach(() => vi.useRealTimers());
-it("retains a connected route through quiet video and still reports actual recovery exhaustion", () => {
+it.each([true, false])("retains quiet media and edge recovery while Host signaling is online=%s", (hostOnline) => {
vi.useFakeTimers();
let events!: ViewerPeerEvents;
+ let recovering = false;
const peer = {
- isRecovering: () => false,
+ isRecovering: () => recovering,
getConnectionIdentity: () => ({ parentPeerId: "host", connectionId: "edge" }),
hasConnectionId: (id: string) => id === "edge",
+ dispose: vi.fn(),
};
const send = vi.fn();
const reportPeerFailure = vi.fn(() => true);
const setPeerSnapshot = vi.fn();
+ const stopRelay = vi.fn();
+ const dispatchPresentation = vi.fn();
const context = createContext({
- Date, active: true, currentHostOnline: true,
+ Date, active: true, currentHostOnline: hostOnline,
currentIceConfig: { iceServers: [] }, currentRoutePolicy: { natPrediction: false },
currentRouteRevision: 1, currentRouteAssignment: { upstream: { kind: "peer", peerId: "host" } },
activePeerMetrics: null, peerRef: { current: null },
@@ -43,7 +47,9 @@ it("retains a connected route through quiet video and still reports actual recov
createViewerMediaPeer: (_config: unknown, callbacks: ViewerPeerEvents) => { events = callbacks; return peer; },
createOwnedViewerRestartSender: () => vi.fn(),
offerPeerQualityEvidence: vi.fn(), setPeerSnapshot,
- dispatchPresentation: vi.fn(), connectionFact: (state: string) => state,
+ clearUpstreamState: () => peer.dispose(), relayChildEvidenceStore: { clear: vi.fn() },
+ viewerRelay: { stop: stopRelay }, invalidatePresentedMedia: vi.fn(),
+ dispatchPresentation, connectionFact: (state: string) => state,
});
runInContext(executable + "\nensurePeer();", context);
const snapshot = {
@@ -62,6 +68,16 @@ it("retains a connected route through quiet video and still reports actual recov
expect(setPeerSnapshot).toHaveBeenLastCalledWith(expect.objectContaining({
metrics: expect.objectContaining({ intervalFramesDecoded: 30 }),
}));
+ // Recovery has already begun before the media peer publishes its failed
+ // snapshot. A signaling outage must not dispose it or healthy descendants.
+ recovering = true;
+ events.onUpdate({ ...snapshot, connectionState: "failed" } as Parameters<ViewerPeerEvents["onUpdate"]>[0]);
+ expect(peer.dispose).not.toHaveBeenCalled();
+ expect(stopRelay).not.toHaveBeenCalled();
+ expect(dispatchPresentation).toHaveBeenLastCalledWith({ type: "connection", revision: 1, connection: "reconnecting" });
+ recovering = false;
+ events.onUpdate(snapshot as Parameters<ViewerPeerEvents["onUpdate"]>[0]);
+ expect(dispatchPresentation).toHaveBeenLastCalledWith({ type: "connection", revision: 1, connection: "connected" });
events.onRecoveryExhausted?.("host", "edge");
expect(reportPeerFailure).toHaveBeenCalledExactlyOnceWith("host", "edge");
});