dsh-miniwin 0.2.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/lib/index.js +184 -17
- package/package.json +2 -2
- package/runtime/audio-router.js +188 -0
- package/runtime/main.js +117 -3
- package/runtime/pair-page.js +133 -0
- package/runtime/preload.js +16 -0
- package/runtime/receiver-stderr.log +0 -0
- package/runtime/scrcpy-capture.js +4 -0
- package/runtime/scrcpy-transport.js +32 -0
- package/runtime/ui.html +60 -37
- package/runtime/wifi-audio.js +479 -0
package/README.md
CHANGED
|
@@ -288,3 +288,18 @@ QR 编码器自己实现(byte mode, 版本 1–10, EC L/M/Q/H, mask 0),
|
|
|
288
288
|
- LAN 端口每次启动随机变化(重新出码即可)
|
|
289
289
|
- **不支持在手机浏览器里投屏**:`getDisplayMedia` 在
|
|
290
290
|
[Chrome for Android 不支持](https://caniuse.com/mdn-api_mediadevices_getdisplaymedia)
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## 声音规则
|
|
295
|
+
|
|
296
|
+
| 场景 | 行为 |
|
|
297
|
+
|---|---|
|
|
298
|
+
| 手机插着耳机(有线/蓝牙) | **声音留在手机**,不投到电脑;插件会停掉手机端采集,避免它把音量锁在 0 |
|
|
299
|
+
| 手机外放,刚连上投屏 | **双端静音**:手机静音,投屏音量默认 0 |
|
|
300
|
+
| 手动把投屏音量调高 | 才开始把手机声音投到电脑 |
|
|
301
|
+
| 拖投屏音量 | 只改电脑这一端,不动手机音量 |
|
|
302
|
+
| 按手机音量键 | 投屏音量被**重置**成手机当前音量 |
|
|
303
|
+
| 断开连接 | 停音频 + 杀掉手机端 App |
|
|
304
|
+
|
|
305
|
+
耳机插着时投屏音量恒为 0,拔掉也不会自动出声 —— 必须手动调高。
|
package/lib/index.js
CHANGED
|
@@ -22,6 +22,16 @@
|
|
|
22
22
|
|
|
23
23
|
import { fileURLToPath } from 'node:url'
|
|
24
24
|
import path from 'node:path'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 别名。
|
|
28
|
+
*
|
|
29
|
+
* 有的请求处理函数里会 const path = ... 承接 URL 路径,把模块名遮住了 ——
|
|
30
|
+
* 于是 pathMod.join(...) 变成「对一个字符串取 join」,实测报
|
|
31
|
+
* pathMod.join is not a function
|
|
32
|
+
* 表现为手机点「下载 APK」拿 500。统一改用 pathMod,别再被遮。
|
|
33
|
+
*/
|
|
34
|
+
const pathMod = path
|
|
25
35
|
import fs from 'node:fs'
|
|
26
36
|
|
|
27
37
|
/* ==========================================================================
|
|
@@ -38,12 +48,12 @@ import fs from 'node:fs'
|
|
|
38
48
|
旧的写法是把作者本机的绝对路径写死在源码里 ——
|
|
39
49
|
只有作者那台机器能跑,别人装上之后扫码出码、投屏起不来。
|
|
40
50
|
========================================================================== */
|
|
41
|
-
const PKG_DIR =
|
|
51
|
+
const PKG_DIR = pathMod.dirname(fileURLToPath(import.meta.url)) // <pkg>/lib
|
|
42
52
|
|
|
43
53
|
function runtimeRoot() {
|
|
44
54
|
if (process.env.MINIWIN_HOME) return process.env.MINIWIN_HOME
|
|
45
|
-
const bundled =
|
|
46
|
-
if (fs.existsSync(
|
|
55
|
+
const bundled = pathMod.join(PKG_DIR, '..', 'runtime')
|
|
56
|
+
if (fs.existsSync(pathMod.join(bundled, 'main.js'))) return bundled
|
|
47
57
|
throw new Error(
|
|
48
58
|
'dsh-miniwin: 找不到投屏运行时目录:' + bundled +
|
|
49
59
|
'。请重新安装插件;如果是从源码目录挂载,设置环境变量 MINIWIN_HOME 指向 miniwin-stream 目录。'
|
|
@@ -53,19 +63,168 @@ function runtimeRoot() {
|
|
|
53
63
|
/**
|
|
54
64
|
* 迷你窗用哪个 Electron。
|
|
55
65
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
66
|
+
* ── 为什么不能直接用 process.execPath ──
|
|
67
|
+
*
|
|
68
|
+
* 上一版写的是「宿主是 Electron,execPath 就是 electron.exe,直接复用」。
|
|
69
|
+
* 实测被打脸:DSH 桌面端的可执行文件叫 **`DSH NEXT.exe`** —— 它确实是
|
|
70
|
+
* Electron 内核,但它是**打包过的**应用(同级有 resources/app.asar),
|
|
71
|
+
* `DSH NEXT.exe <某个脚本>` 只会重新拉起它自己,根本不会跑我们的 main.js。
|
|
72
|
+
* 结果是插件点「启动」直接报:
|
|
73
|
+
*
|
|
74
|
+
* dsh-miniwin: 找不到 Electron 宿主可执行文件(process.execPath=...\DSH NEXT.exe)
|
|
75
|
+
*
|
|
76
|
+
* 用户看到的现象就是「连不上」。
|
|
77
|
+
*
|
|
78
|
+
* ── 现在的查找顺序 ──
|
|
79
|
+
* 1. MINIWIN_ELECTRON 环境变量(显式指定,永远优先)
|
|
80
|
+
* 2. 宿主确实是未打包的 Electron(没有 resources/app.asar)→ 复用它
|
|
81
|
+
* 3. 插件包内 node_modules/electron/dist/electron.exe
|
|
82
|
+
* 4. 工作目录 / 工作目录下一两级的 node_modules/electron/dist/electron.exe
|
|
83
|
+
* (开发机上常见的 `research/_probe/node_modules/electron` 就在这一层被找到)
|
|
84
|
+
* 5. DSH 运行时的 dsh-runtimes 目录里翻一层
|
|
85
|
+
* 6. 全都找不到 → 报错,并明确告诉用户两条出路
|
|
86
|
+
*/
|
|
87
|
+
function looksPackaged(exe) {
|
|
88
|
+
try {
|
|
89
|
+
return fs.existsSync(pathMod.join(pathMod.dirname(exe), 'resources', 'app.asar'))
|
|
90
|
+
} catch { return false }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function listDirs (dir) {
|
|
94
|
+
try {
|
|
95
|
+
return fs.readdirSync(dir, { withFileTypes: true })
|
|
96
|
+
.filter(d => d.isDirectory() && !d.name.startsWith('.'))
|
|
97
|
+
.map(d => pathMod.join(dir, d.name))
|
|
98
|
+
} catch { return [] }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 缓存文件:解析成功的 electron 路径落盘。
|
|
103
|
+
*
|
|
104
|
+
* 为什么需要它:DSH 宿主的工作目录不一定是用户的项目目录(可能直接是
|
|
105
|
+
* 安装目录),只按 cwd 猜会漏。第一次全盘搜到之后记下来,下次直接命中,
|
|
106
|
+
* 也省得每次都扫盘。
|
|
107
|
+
*/
|
|
108
|
+
function configPath () {
|
|
109
|
+
const home = process.env.USERPROFILE || process.env.HOME || PKG_DIR
|
|
110
|
+
return pathMod.join(home, '.dsh', 'dsh-miniwin.json')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function readCachedElectron () {
|
|
114
|
+
try {
|
|
115
|
+
const raw = JSON.parse(fs.readFileSync(configPath(), 'utf8'))
|
|
116
|
+
if (raw && typeof raw.electron === 'string' && fs.existsSync(raw.electron)) return raw.electron
|
|
117
|
+
} catch {}
|
|
118
|
+
return null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function writeCachedElectron (exe) {
|
|
122
|
+
try {
|
|
123
|
+
const p = configPath()
|
|
124
|
+
let cfg = {}
|
|
125
|
+
try { cfg = JSON.parse(fs.readFileSync(p, 'utf8')) } catch {}
|
|
126
|
+
cfg.electron = exe
|
|
127
|
+
fs.mkdirSync(pathMod.dirname(p), { recursive: true })
|
|
128
|
+
fs.writeFileSync(p, JSON.stringify(cfg, null, 2))
|
|
129
|
+
} catch {}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 有界深搜:从几个根往下找 `<某个目录>/node_modules/electron/dist/electron.exe`。
|
|
134
|
+
*
|
|
135
|
+
* 约束:最多 6 层、最多访问 20000 个目录、3 秒硬超时,遇到 node_modules
|
|
136
|
+
* 就不再往里钻(只在它下面按固定相对路径查一次),跳过 AppData / .git /
|
|
137
|
+
* Windows 这类明显的无底洞。命中即返回。
|
|
59
138
|
*/
|
|
139
|
+
const SKIP_DIRS = new Set(['appdata', 'windows', 'program files', 'program files (x86)',
|
|
140
|
+
'programdata', '$recycle.bin', 'system volume information', '.git', '.cache',
|
|
141
|
+
'.npm', '.pnpm-store', '.vscode', '.cursor', 'dist', 'out', 'build'])
|
|
142
|
+
|
|
143
|
+
function deepFindElectron (roots) {
|
|
144
|
+
const t0 = Date.now()
|
|
145
|
+
const seen = new Set()
|
|
146
|
+
let visited = 0
|
|
147
|
+
const queue = roots.filter(Boolean).map(r => ({ dir: r, depth: 0 }))
|
|
148
|
+
while (queue.length) {
|
|
149
|
+
if (Date.now() - t0 > 3000 || visited > 20000) break
|
|
150
|
+
const { dir, depth } = queue.shift()
|
|
151
|
+
if (seen.has(dir) || depth > 6) continue
|
|
152
|
+
seen.add(dir)
|
|
153
|
+
visited++
|
|
154
|
+
const direct = pathMod.join(dir, 'node_modules', 'electron', 'dist', 'electron.exe')
|
|
155
|
+
try { if (fs.existsSync(direct)) return direct } catch {}
|
|
156
|
+
if (dir.toLowerCase().endsWith('node_modules')) continue
|
|
157
|
+
let entries = []
|
|
158
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }) } catch { continue }
|
|
159
|
+
for (const e of entries) {
|
|
160
|
+
if (!e.isDirectory()) continue
|
|
161
|
+
const name = e.name.toLowerCase()
|
|
162
|
+
if (SKIP_DIRS.has(name) || name.startsWith('.')) continue
|
|
163
|
+
queue.push({ dir: pathMod.join(dir, e.name), depth: depth + 1 })
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return null
|
|
167
|
+
}
|
|
168
|
+
|
|
60
169
|
function resolveElectron() {
|
|
61
170
|
if (process.env.MINIWIN_ELECTRON) return process.env.MINIWIN_ELECTRON
|
|
171
|
+
|
|
172
|
+
// 上次解析成功过就直接用
|
|
173
|
+
const cached = readCachedElectron()
|
|
174
|
+
if (cached) return cached
|
|
175
|
+
|
|
62
176
|
const exe = process.execPath || ''
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
177
|
+
const isElectronHost = !!(process.versions && process.versions.electron)
|
|
178
|
+
if (isElectronHost && exe && !looksPackaged(exe)) {
|
|
179
|
+
writeCachedElectron(exe)
|
|
180
|
+
return exe
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const rel = ['node_modules', 'electron', 'dist', 'electron.exe']
|
|
184
|
+
const cands = []
|
|
185
|
+
|
|
186
|
+
// 插件包内(如果有人把它装成依赖)与它旁边一层
|
|
187
|
+
cands.push(pathMod.join(PKG_DIR, '..', ...rel))
|
|
188
|
+
cands.push(pathMod.join(PKG_DIR, '..', '..', ...rel))
|
|
189
|
+
|
|
190
|
+
// 工作目录及其下两级(覆盖 <cwd>/xxx/node_modules 这种布局)
|
|
191
|
+
const cwd = process.cwd()
|
|
192
|
+
cands.push(pathMod.join(cwd, ...rel))
|
|
193
|
+
for (const d1 of listDirs(cwd)) {
|
|
194
|
+
cands.push(pathMod.join(d1, ...rel))
|
|
195
|
+
for (const d2 of listDirs(d1)) cands.push(pathMod.join(d2, ...rel))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// DSH 运行时目录
|
|
199
|
+
const home = process.env.USERPROFILE || process.env.HOME || ''
|
|
200
|
+
if (home) {
|
|
201
|
+
const rt = pathMod.join(home, '.dsh', 'dsh-runtimes')
|
|
202
|
+
for (const d1 of listDirs(rt)) {
|
|
203
|
+
cands.push(pathMod.join(d1, ...rel))
|
|
204
|
+
for (const d2 of listDirs(d1)) cands.push(pathMod.join(d2, ...rel))
|
|
205
|
+
cands.push(pathMod.join(d1, 'dependencies', 'electron', 'dist', 'electron.exe'))
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (const c of cands) {
|
|
210
|
+
try {
|
|
211
|
+
if (c && fs.existsSync(c)) { writeCachedElectron(c); return c }
|
|
212
|
+
} catch {}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 兜底:有界深搜(cwd / 用户目录 / DSH_HOME)
|
|
216
|
+
const hit = deepFindElectron([cwd, home, process.env.DSH_HOME])
|
|
217
|
+
if (hit) {
|
|
218
|
+
writeCachedElectron(hit)
|
|
219
|
+
return hit
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
|
|
66
223
|
throw new Error(
|
|
67
|
-
'dsh-miniwin:
|
|
68
|
-
'
|
|
224
|
+
'dsh-miniwin: 找不到可用的 Electron(迷你窗需要一个真正的 electron.exe,' +
|
|
225
|
+
'而宿主 ' + (exe || '未知') + ' 是打包过的应用,不能拿来跑脚本)。' +
|
|
226
|
+
'两条出路:① 设置环境变量 MINIWIN_ELECTRON 指向任意 electron.exe;' +
|
|
227
|
+
'② 在与 DSH 相同的工作目录里装一份 `npm i -D electron`。'
|
|
69
228
|
)
|
|
70
229
|
}
|
|
71
230
|
|
|
@@ -195,7 +354,7 @@ async function resolveNodeBin() {
|
|
|
195
354
|
if (process.env.DSH_HOME) roots.push(process.env.DSH_HOME)
|
|
196
355
|
if (process.resourcesPath) {
|
|
197
356
|
roots.push(process.resourcesPath) // <install>/resources
|
|
198
|
-
roots.push(
|
|
357
|
+
roots.push(pathMod.dirname(process.resourcesPath)) // <install>
|
|
199
358
|
}
|
|
200
359
|
const rels = [
|
|
201
360
|
'runtime/x64/primary-runtime/dependencies/node/bin/node.exe',
|
|
@@ -204,7 +363,7 @@ async function resolveNodeBin() {
|
|
|
204
363
|
'node/bin/node.exe'
|
|
205
364
|
]
|
|
206
365
|
for (const r of roots) {
|
|
207
|
-
for (const rel of rels) candidates.push(
|
|
366
|
+
for (const rel of rels) candidates.push(pathMod.join(r, rel))
|
|
208
367
|
}
|
|
209
368
|
|
|
210
369
|
// PATH 里的 node
|
|
@@ -515,7 +674,7 @@ async function startLanServer(onPair, onConnect, onLaunch, getState) {
|
|
|
515
674
|
if ((req.method === 'GET' || req.method === 'HEAD') && routePath === '/app.apk') {
|
|
516
675
|
try {
|
|
517
676
|
const fs = await import('node:fs')
|
|
518
|
-
const apkPath =
|
|
677
|
+
const apkPath = pathMod.join(runtimeRoot(), 'apk', 'miniwin.apk')
|
|
519
678
|
const stat = fs.statSync(apkPath)
|
|
520
679
|
res.writeHead(200, {
|
|
521
680
|
'Content-Type': 'application/vnd.android.package-archive',
|
|
@@ -1072,7 +1231,13 @@ function apply(ctx) {
|
|
|
1072
1231
|
* 3. 返回手机要扫的深链
|
|
1073
1232
|
* 手机装过 APK 后扫这个码 → App 自动填地址 → 点允许 → 画面出现。
|
|
1074
1233
|
*/
|
|
1075
|
-
|
|
1234
|
+
/**
|
|
1235
|
+
* WiFi(APK 推流)模式的完整启动流程。
|
|
1236
|
+
*
|
|
1237
|
+
* 抽成具名函数是因为 /launch 也要用它:没有 adb 设备时,
|
|
1238
|
+
* 点「启动迷你窗」不该走去等 adb 的那条路(那条要等 3 分钟)。
|
|
1239
|
+
*/
|
|
1240
|
+
const startWifiMode = async (req, res) => {
|
|
1076
1241
|
try {
|
|
1077
1242
|
const { spawn } = await import('node:child_process')
|
|
1078
1243
|
const m = await loadMiniwin()
|
|
@@ -1171,7 +1336,9 @@ function apply(ctx) {
|
|
|
1171
1336
|
pushLog('error', 'wifi-start 异常: ' + String((e && e.stack) || e).slice(0, 400))
|
|
1172
1337
|
sendJson(res, 500, { ok: false, error: String((e && e.message) || e) })
|
|
1173
1338
|
}
|
|
1174
|
-
}
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
route('POST', ROUTE_BASE + '/wifi-start', startWifiMode)
|
|
1175
1342
|
|
|
1176
1343
|
/* ---------- 本机地址(给 PC 端生成二维码用) ---------- */
|
|
1177
1344
|
route('GET', ROUTE_BASE + '/addrs', async (req, res) => {
|
|
@@ -1722,7 +1889,7 @@ function apply(ctx) {
|
|
|
1722
1889
|
function adbPath() {
|
|
1723
1890
|
if (process.env.MINIWIN_ADB) return process.env.MINIWIN_ADB
|
|
1724
1891
|
try {
|
|
1725
|
-
return
|
|
1892
|
+
return pathMod.join(runtimeRoot(), 'tools', 'scrcpy', 'scrcpy-win64-v5.0.1', 'adb.exe')
|
|
1726
1893
|
} catch {
|
|
1727
1894
|
// 运行时没就位时不要让设备发现直接崩,退回 PATH 里的 adb
|
|
1728
1895
|
return 'adb'
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-miniwin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Phone screen mirroring in the DSH sidebar: scan a QR code to pair, then the phone screen opens in a transparent desktop mini-window. Ships its own scrcpy runtime;
|
|
3
|
+
"version": "0.3.3",
|
|
4
|
+
"description": "Phone screen mirroring in the DSH sidebar: scan a QR code to pair, then the phone screen opens in a transparent desktop mini-window. Ships its own scrcpy runtime; audio follows a headset-aware routing policy and starts muted on both ends.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
/**
|
|
3
|
+
* 音频路由策略(手机端声音到底去哪)。
|
|
4
|
+
*
|
|
5
|
+
* 用户规则:
|
|
6
|
+
* 1. 手机插耳机(有线/蓝牙)时:不做任何投音频,声音留在手机
|
|
7
|
+
* 2. 手机外放时:连接后默认「双端静音」——手机静音 + 投屏音量 0
|
|
8
|
+
* 3. 手动把投屏音量调上去(>0)才把手机声音投到电脑
|
|
9
|
+
* 4. 投屏音量不影响手机音量
|
|
10
|
+
* 5. 手机音量变化 → 投屏音量被重置成手机音量
|
|
11
|
+
* 6. 断开连接 → 音频断开,并杀掉手机端 APK
|
|
12
|
+
*
|
|
13
|
+
* 数据来源(实测这台 HBN-AL00 可用):
|
|
14
|
+
* adb shell dumpsys audio → STREAM_MUSIC 段落里的
|
|
15
|
+
* "Devices: speaker, bt_a2dp"
|
|
16
|
+
* "streamVolume:10"
|
|
17
|
+
*/
|
|
18
|
+
const { execFile } = require('child_process')
|
|
19
|
+
|
|
20
|
+
const HEADSET_DEVICES = ['headset', 'headphone', 'bt_a2dp', 'bt_a2dp_hp', 'bt_a2dp_spk',
|
|
21
|
+
'bt_sco', 'bt_sco_hs', 'bt_sco_carkit', 'usb_headset']
|
|
22
|
+
const SPEAKER_DEVICES = ['speaker', 'speaker_safe']
|
|
23
|
+
|
|
24
|
+
class AudioRouter {
|
|
25
|
+
constructor (opts = {}) {
|
|
26
|
+
this.adb = opts.adb
|
|
27
|
+
this.serial = opts.serial
|
|
28
|
+
this.apkPackage = opts.apkPackage || 'com.miniwin.stream'
|
|
29
|
+
/** 接收端 HTTP 端口(/meta 的 phoneVolume 是手机音量意图的权威来源) */
|
|
30
|
+
this.httpPort = opts.httpPort || 0
|
|
31
|
+
this.onLog = opts.onLog || (() => {})
|
|
32
|
+
this.onState = opts.onState || (() => {})
|
|
33
|
+
this.timer = null
|
|
34
|
+
this.last = { devices: [], headset: false, speaker: false, phoneVolume: 0, pcVolume: 0 }
|
|
35
|
+
this.pcVolume = 0
|
|
36
|
+
this.manualOverride = false
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_run (args, timeout = 8000) {
|
|
40
|
+
const full = this.serial ? ['-s', this.serial, ...args] : args
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
execFile(this.adb, full, { timeout, windowsHide: true },
|
|
43
|
+
(err, stdout, stderr) => resolve({ ok: !err, out: String(stdout || ''), err: String(stderr || '') }))
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 解析 dumpsys audio 里的音频路由与音乐音量 */
|
|
48
|
+
static parse (text) {
|
|
49
|
+
const idx = text.indexOf('STREAM_MUSIC')
|
|
50
|
+
if (idx < 0) return { devices: [], streamVolume: 0 }
|
|
51
|
+
const block = text.slice(idx, idx + 1200)
|
|
52
|
+
const devLine = (block.match(/Devices:\s*([^\n\r]+)/) || [])[1] || ''
|
|
53
|
+
const devices = devLine.split(',').map(s => s.trim()).filter(Boolean)
|
|
54
|
+
const vol = parseInt((block.match(/streamVolume:(\d+)/) || [])[1] || '0', 10)
|
|
55
|
+
return { devices, streamVolume: vol }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async poll () {
|
|
59
|
+
/**
|
|
60
|
+
* 没有 adb 通道时(纯 WiFi、手机没开 tcpip 也没插线),
|
|
61
|
+
* 拿不到 dumpsys —— 这时候不能盲判成「外放」把手机静音掉。
|
|
62
|
+
* 我们只保留「/meta 的手机音量」这一路,headset 记为未知(false),
|
|
63
|
+
* 由上层决定降级行为;投屏侧音量仍然默认 0。
|
|
64
|
+
*/
|
|
65
|
+
let p = { devices: [], streamVolume: 0 }
|
|
66
|
+
if (this.serial) {
|
|
67
|
+
const { out } = await this._run(['shell', 'dumpsys audio'])
|
|
68
|
+
p = AudioRouter.parse(out)
|
|
69
|
+
}
|
|
70
|
+
const headset = p.devices.some(d => HEADSET_DEVICES.includes(d))
|
|
71
|
+
const speaker = p.devices.some(d => SPEAKER_DEVICES.includes(d))
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 「用户按了手机音量键」这件事,dumpsys 的 streamVolume 看不出来 ——
|
|
75
|
+
* APK 静音后它一直是 0。真正的意图值在 APK 读取的
|
|
76
|
+
* Settings.System.volume_music 里,它通过接收端 /meta 的
|
|
77
|
+
* phoneVolume(0..255)暴露出来。
|
|
78
|
+
*
|
|
79
|
+
* 所以:设备列表看 dumpsys,音量意图看 /meta。
|
|
80
|
+
*/
|
|
81
|
+
let intentVol = p.streamVolume
|
|
82
|
+
if (this.httpPort) {
|
|
83
|
+
try {
|
|
84
|
+
const r = await fetch(`http://127.0.0.1:${this.httpPort}/meta`, {
|
|
85
|
+
cache: 'no-store', signal: AbortSignal.timeout(1500)
|
|
86
|
+
})
|
|
87
|
+
const j = await r.json()
|
|
88
|
+
if (j && typeof j.phoneVolume === 'number') {
|
|
89
|
+
intentVol = Math.round((j.phoneVolume / 255) * 15)
|
|
90
|
+
}
|
|
91
|
+
} catch { /* 接收端没起就用 dumpsys 的值 */ }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const prev = this.last
|
|
95
|
+
this.last = { devices: p.devices, headset, speaker, phoneVolume: intentVol, pcVolume: this.pcVolume }
|
|
96
|
+
|
|
97
|
+
if (headset) {
|
|
98
|
+
/**
|
|
99
|
+
* 耳机在场:投屏音量归零,并且**清掉手动标记**。
|
|
100
|
+
*
|
|
101
|
+
* 不清会踩这个坑:耳机插着时用户拖过投屏音量(或手机音量被同步过来),
|
|
102
|
+
* pcVolume 停在 7;耳机一拔条件立刻成立 → 手机声音突然被投到电脑,
|
|
103
|
+
* 而用户根本没开。规则是「外放默认静音,手动调高才转发」。
|
|
104
|
+
*/
|
|
105
|
+
if (this.pcVolume !== 0 || this.manualOverride) this.onLog('[audio] 耳机在场:投屏音量归零')
|
|
106
|
+
this.pcVolume = 0
|
|
107
|
+
this.manualOverride = false
|
|
108
|
+
this.last.pcVolume = 0
|
|
109
|
+
} else if (intentVol !== prev.phoneVolume) {
|
|
110
|
+
/**
|
|
111
|
+
* 规则 5:手机端音量变化 → 投屏端直接重置成手机端音量。
|
|
112
|
+
* 不看 manualOverride —— 用户明确要求「手机端调音量就把投屏端重置过去」。
|
|
113
|
+
*/
|
|
114
|
+
this.pcVolume = intentVol
|
|
115
|
+
this.manualOverride = this.pcVolume > 0
|
|
116
|
+
this.last.pcVolume = this.pcVolume
|
|
117
|
+
this.onLog(`[audio] 手机音量 ${intentVol}/15 → 投屏音量重置为 ${this.pcVolume}/15`)
|
|
118
|
+
}
|
|
119
|
+
this.onState({ ...this.last, forwarding: this.shouldForward() })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 规则 1:耳机时永不转发 */
|
|
123
|
+
shouldForward () {
|
|
124
|
+
if (this.last.headset) return false
|
|
125
|
+
return this.manualOverride && this.pcVolume > 0
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 规则 3:用户手动调高投屏音量 → 允许转发 */
|
|
129
|
+
setPcVolume (v) {
|
|
130
|
+
this.pcVolume = Math.max(0, Math.min(15, Math.round(v)))
|
|
131
|
+
this.manualOverride = this.pcVolume > 0
|
|
132
|
+
this.last.pcVolume = this.pcVolume
|
|
133
|
+
const fwd = this.shouldForward()
|
|
134
|
+
this.onLog(`[audio] 投屏音量=${this.pcVolume} → ${fwd ? '开始转发手机声音' : '不转发'}`)
|
|
135
|
+
this.onState({ ...this.last, forwarding: fwd })
|
|
136
|
+
return { pcVolume: this.pcVolume, forwarding: fwd }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 规则 2:外放时连接即双端静音(用音量键把媒体音量降到 0) */
|
|
140
|
+
async mutePhone () {
|
|
141
|
+
if (!this.serial) return
|
|
142
|
+
// `cmd media_session` 一条命令就能把媒体音量设到 0,比连按音量键干净得多
|
|
143
|
+
const r = await this._run(['shell', 'cmd media_session volume --stream 3 --set 0'])
|
|
144
|
+
if (r.ok) {
|
|
145
|
+
this.last.phoneVolume = 0
|
|
146
|
+
this.onLog('[audio] 手机媒体音量已设 0(外放默认静音)')
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
// 老系统没有 cmd media_session:退回音量键
|
|
150
|
+
for (let i = 0; i < 16; i++) {
|
|
151
|
+
await this._run(['shell', 'input keyevent 25']) // VOLUME_DOWN
|
|
152
|
+
}
|
|
153
|
+
this.last.phoneVolume = 0
|
|
154
|
+
this.onLog('[audio] 手机媒体音量已降到 0(音量键兜底)')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 只停手机端 APK,不停轮询。
|
|
159
|
+
*
|
|
160
|
+
* 耳机插上时用它:APK 会把手机 STREAM_MUSIC 锁在 0,
|
|
161
|
+
* 不 force-stop 的话耳机里也没声。
|
|
162
|
+
*/
|
|
163
|
+
async killApk () {
|
|
164
|
+
/** 没有 adb 通道(纯 WiFi)时做不到,直接跳过 —— 别让 adb 报「no devices」刷屏 */
|
|
165
|
+
if (!this.serial) return
|
|
166
|
+
await this._run(['shell', `am force-stop ${this.apkPackage}`])
|
|
167
|
+
this.onLog(`[audio] 已停止手机端 ${this.apkPackage}`)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 规则 6:断开时停音频并杀掉手机端 APK */
|
|
171
|
+
async teardown () {
|
|
172
|
+
this.stop()
|
|
173
|
+
await this._run(['shell', `am force-stop ${this.apkPackage}`])
|
|
174
|
+
this.onLog(`[audio] 已断开音频并停止手机端 ${this.apkPackage}`)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
start (intervalMs = 2000) {
|
|
178
|
+
if (this.timer) return
|
|
179
|
+
this.timer = setInterval(() => { this.poll().catch(() => {}) }, intervalMs)
|
|
180
|
+
this.poll().catch(() => {})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
stop () {
|
|
184
|
+
if (this.timer) { clearInterval(this.timer); this.timer = null }
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
module.exports = { AudioRouter, HEADSET_DEVICES, SPEAKER_DEVICES }
|
package/runtime/main.js
CHANGED
|
@@ -1475,23 +1475,56 @@ async function startScrcpySession (opts = {}) {
|
|
|
1475
1475
|
* · 投屏音量不影响手机;手机音量变化 → 投屏音量同步成手机音量
|
|
1476
1476
|
* · 断开连接 → 停音频 + 杀掉手机端 APK
|
|
1477
1477
|
*/
|
|
1478
|
+
/** 耳机态:APK 已被停掉(声音留在手机端)。放在 try 外面,后面的自愈要用 */
|
|
1479
|
+
let apkSuppressed = false
|
|
1478
1480
|
try {
|
|
1479
1481
|
const { AudioRouter } = require('./audio-router.js')
|
|
1482
|
+
|
|
1483
|
+
/**
|
|
1484
|
+
* 耳机态:APK 必须停掉。
|
|
1485
|
+
*
|
|
1486
|
+
* 不只是「把投屏音量压到 0」—— 那个 APK 一旦跑起来就会把手机
|
|
1487
|
+
* STREAM_MUSIC 锁在 0,耳机里也会跟着没声。所以耳机在场时
|
|
1488
|
+
* 直接 force-stop 它,让手机按自己的方式出声;
|
|
1489
|
+
* 耳机拔掉再把自愈拉起来,重新接管。
|
|
1490
|
+
*/
|
|
1491
|
+
const applyAudioRoute = (st) => {
|
|
1492
|
+
if (!session || !st) return
|
|
1493
|
+
if (st.headset && !apkSuppressed) {
|
|
1494
|
+
apkSuppressed = true
|
|
1495
|
+
try { if (session.healer) session.healer.stop() } catch {}
|
|
1496
|
+
router.killApk()
|
|
1497
|
+
.then(() => logLine('ok', '[audio] 耳机已连接:已停掉手机端采集,声音留在手机'))
|
|
1498
|
+
.catch(() => {})
|
|
1499
|
+
} else if (!st.headset && apkSuppressed) {
|
|
1500
|
+
apkSuppressed = false
|
|
1501
|
+
logLine('info', '[audio] 耳机已拔出:恢复手机端声音采集')
|
|
1502
|
+
try { if (session.healer) session.healer.start() } catch {}
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1480
1506
|
const router = new AudioRouter({
|
|
1481
1507
|
adb: adbPath(),
|
|
1482
1508
|
serial: dev.serial,
|
|
1483
1509
|
apkPackage: 'com.miniwin.stream',
|
|
1510
|
+
httpPort: 27191,
|
|
1484
1511
|
onLog: (m) => logLine('info', m),
|
|
1485
|
-
onState: (st) => {
|
|
1512
|
+
onState: (st) => {
|
|
1513
|
+
try { win && win.webContents.send('audio:state', st) } catch {}
|
|
1514
|
+
applyAudioRoute(st)
|
|
1515
|
+
}
|
|
1486
1516
|
})
|
|
1487
1517
|
session.router = router
|
|
1518
|
+
|
|
1488
1519
|
await router.poll()
|
|
1489
1520
|
if (router.last.headset) {
|
|
1490
1521
|
logLine('info', `[audio] 耳机已连接(${router.last.devices.join('+')}):声音留在手机端`)
|
|
1522
|
+
apkSuppressed = true
|
|
1523
|
+
await router.killApk()
|
|
1491
1524
|
} else {
|
|
1492
|
-
|
|
1525
|
+
// 外放:投屏音量默认 0。手机那头由 APK 自己静音,不用在这里灌音量键。
|
|
1493
1526
|
router.setPcVolume(0)
|
|
1494
|
-
logLine('info', '[audio]
|
|
1527
|
+
logLine('info', '[audio] 外放:投屏音量默认 0,调高才开始把手机声音投到电脑')
|
|
1495
1528
|
}
|
|
1496
1529
|
router.start()
|
|
1497
1530
|
} catch (e) {
|
|
@@ -1563,6 +1596,14 @@ async function startScrcpySession (opts = {}) {
|
|
|
1563
1596
|
})
|
|
1564
1597
|
healer.start()
|
|
1565
1598
|
session.healer = healer
|
|
1599
|
+
/**
|
|
1600
|
+
* 耳机在场时不要把自愈拉起来 —— 它会立刻把 APK 重新推上去,
|
|
1601
|
+
* 而那个 APK 一跑就把手机音量锁 0,耳机里照样没声。
|
|
1602
|
+
*/
|
|
1603
|
+
if (apkSuppressed) {
|
|
1604
|
+
try { healer.stop() } catch {}
|
|
1605
|
+
logLine('info', '[audio] 耳机仍连接中:暂不拉起手机端采集')
|
|
1606
|
+
}
|
|
1566
1607
|
} else {
|
|
1567
1608
|
logLine('warn', '音频接收端启动失败: ' + (ar.error || ''))
|
|
1568
1609
|
}
|
|
@@ -3110,6 +3151,79 @@ async function bootWindow () {
|
|
|
3110
3151
|
*/
|
|
3111
3152
|
win.webContents.on('did-finish-load', () => { replayStateToRenderer() })
|
|
3112
3153
|
|
|
3154
|
+
/**
|
|
3155
|
+
* ══════════ WiFi 模式:把流地址交给渲染层 ══════════
|
|
3156
|
+
*
|
|
3157
|
+
* 这里曾经是个致命缺口:`--wifi <url>` 解析出来了,却**从来没有人把它
|
|
3158
|
+
* 发给渲染层** —— ui.html 里的 `onWifiStart` 监听器永远等不到事件,
|
|
3159
|
+
* 于是 WiFi 模式下窗口就是一块占位图,用户看到的是「连不上」。
|
|
3160
|
+
*
|
|
3161
|
+
* 现在每次加载完成都发一次(页面重载后也能自己恢复),
|
|
3162
|
+
* 渲染层收到就开始长轮询 /frames 拉画面 + 起 WifiAudio 播放声音。
|
|
3163
|
+
*/
|
|
3164
|
+
if (wifiSource) {
|
|
3165
|
+
const sendWifiStart = () => {
|
|
3166
|
+
if (!win || win.isDestroyed()) return
|
|
3167
|
+
try {
|
|
3168
|
+
win.webContents.send('wifi:start', { source: wifiSource })
|
|
3169
|
+
logLine('info', '[wifi] 已通知渲染层拉流: ' + wifiSource)
|
|
3170
|
+
} catch (e) { logLine('warn', '[wifi] 通知渲染层失败: ' + e.message) }
|
|
3171
|
+
}
|
|
3172
|
+
win.webContents.on('did-finish-load', sendWifiStart)
|
|
3173
|
+
sendWifiStart()
|
|
3174
|
+
/**
|
|
3175
|
+
* 首帧加载时的竞态:bootWindow() 是 await loadFile 回来的,此时
|
|
3176
|
+
* did-finish-load 已经过去,而渲染层的监听器可能刚注册完 —— 一条事件
|
|
3177
|
+
* 丢了就是「窗口一直空白」。补两次延迟发送,渲染层用 wifiRunning 去重,
|
|
3178
|
+
* 多发无害。
|
|
3179
|
+
*/
|
|
3180
|
+
setTimeout(sendWifiStart, 700)
|
|
3181
|
+
setTimeout(sendWifiStart, 2200)
|
|
3182
|
+
|
|
3183
|
+
/**
|
|
3184
|
+
* WiFi 模式下的音频策略。
|
|
3185
|
+
*
|
|
3186
|
+
* 复用同一套 AudioRouter:
|
|
3187
|
+
* · 有 adb 通道 → 能识别耳机,完整规则生效
|
|
3188
|
+
* · 没有 adb → 耳机不可知,按外放处理(投屏音量默认 0,
|
|
3189
|
+
* 手机端由 APK 自己锁 0),手机音量键仍能遥控投屏音量
|
|
3190
|
+
* 投屏音量 = 渲染层本地增益,永远不会写回手机。
|
|
3191
|
+
*/
|
|
3192
|
+
;(async () => {
|
|
3193
|
+
try {
|
|
3194
|
+
const { AudioRouter } = require('./audio-router.js')
|
|
3195
|
+
let serial = null
|
|
3196
|
+
try {
|
|
3197
|
+
const adb = new Adb(adbPath())
|
|
3198
|
+
const list = await adb.devices()
|
|
3199
|
+
const ready = list.filter(d => d.state === 'device')
|
|
3200
|
+
if (ready.length) serial = ready[0].serial
|
|
3201
|
+
} catch {}
|
|
3202
|
+
const httpPort = (() => {
|
|
3203
|
+
try { return Number(new URL(wifiSource).port) || 0 } catch { return 0 }
|
|
3204
|
+
})()
|
|
3205
|
+
const router = new AudioRouter({
|
|
3206
|
+
adb: adbPath(),
|
|
3207
|
+
serial,
|
|
3208
|
+
httpPort,
|
|
3209
|
+
apkPackage: 'com.miniwin.stream',
|
|
3210
|
+
onLog: (m) => logLine('info', m),
|
|
3211
|
+
onState: (st) => { try { win && win.webContents.send('audio:state', st) } catch {} }
|
|
3212
|
+
})
|
|
3213
|
+
await router.poll()
|
|
3214
|
+
router.setPcVolume(0)
|
|
3215
|
+
router.start()
|
|
3216
|
+
app.on('will-quit', () => { try { router.stop() } catch {} })
|
|
3217
|
+
logLine(serial ? 'info' : 'warn',
|
|
3218
|
+
serial
|
|
3219
|
+
? '[audio] WiFi 模式:音频策略已接管,能识别耳机(' + serial + ')'
|
|
3220
|
+
: '[audio] WiFi 模式:没有 adb 通道,耳机状态无法探测,按外放处理(投屏音量默认 0)')
|
|
3221
|
+
} catch (e) {
|
|
3222
|
+
logLine('warn', '[audio] WiFi 模式音频策略初始化失败: ' + e.message)
|
|
3223
|
+
}
|
|
3224
|
+
})()
|
|
3225
|
+
}
|
|
3226
|
+
|
|
3113
3227
|
/**
|
|
3114
3228
|
* ══════════ Esc 只在窗口有焦点时才退出 ══════════
|
|
3115
3229
|
*
|