dsh-miniwin 0.0.0-stage → 0.2.1
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/LICENSE +28 -0
- package/README.en.md +83 -0
- package/README.md +289 -2
- package/cordis.patch.yml +10 -0
- package/lib/client.js +532 -0
- package/lib/discover.js +244 -0
- package/lib/index.js +2290 -0
- package/lib/qr.js +313 -0
- package/package.json +57 -6
- package/runtime/adb-control.js +470 -0
- package/runtime/apk/miniwin.apk +0 -0
- package/runtime/assets/icon.ico +0 -0
- package/runtime/assets/icon.png +0 -0
- package/runtime/assets/tray-16.png +0 -0
- package/runtime/assets/tray-32.png +0 -0
- package/runtime/au.js +131 -0
- package/runtime/audio-bridge.js +267 -0
- package/runtime/audio-healer.js +369 -0
- package/runtime/avcc.js +186 -0
- package/runtime/device.js +307 -0
- package/runtime/main.js +3268 -0
- package/runtime/mkv-reader.js +326 -0
- package/runtime/package.json +5 -0
- package/runtime/preload.js +106 -0
- package/runtime/scrcpy-capture.js +619 -0
- package/runtime/streamer.js +192 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/AdbWinApi.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/AdbWinUsbApi.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/LICENSE.txt +204 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/SDL3.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/adb.exe +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/avcodec-63.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/avformat-63.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/avutil-61.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/disconnected.png +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/libusb-1.0.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/open_a_terminal_here.bat +1 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy-noconsole.vbs +7 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy-server +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy.exe +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy.png +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/swresample-7.dll +0 -0
- package/runtime/ui.html +3143 -0
- package/runtime/usb-hotplug.js +194 -0
- package/runtime/wifi-adb-keeper.js +395 -0
- package/runtime/wifi-discovery.js +213 -0
- package/runtime/wifi-receiver.js +733 -0
- package/screenshots.json +5 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 音频桥 —— 让「scrcpy 出画面 + APK 出声音」两种引擎并存。
|
|
3
|
+
*
|
|
4
|
+
* ══════════ 为什么需要它 ══════════
|
|
5
|
+
*
|
|
6
|
+
* 这台华为是 Android 12,scrcpy 的音频捕获要 Android 13+
|
|
7
|
+
* (实测报错:audio playback capture source not supported
|
|
8
|
+
* before Android 13)。所以 scrcpy 只能给我们画面。
|
|
9
|
+
*
|
|
10
|
+
* 声音必须走我们自己的 APK —— 它用 AudioPlaybackCapture
|
|
11
|
+
* 抓 Android 内部的播放流(Android 10 起就有),这是唯一
|
|
12
|
+
* 能在 12 上拿到「手机正在播放的声音」的路径。
|
|
13
|
+
*
|
|
14
|
+
* ══════════ 职责边界 ══════════
|
|
15
|
+
*
|
|
16
|
+
* 画面:scrcpy.exe → 命名管道 → MkvReader → VideoDecoder
|
|
17
|
+
* 声音:APK → TCP → wifi-receiver → /audio → AudioDecoder
|
|
18
|
+
*
|
|
19
|
+
* 两条链路完全独立:
|
|
20
|
+
* - 画面断了不影响声音
|
|
21
|
+
* - 声音断了不影响画面
|
|
22
|
+
* - 各自有独立的背压和重连
|
|
23
|
+
*
|
|
24
|
+
* ══════════ 静音 ══════════
|
|
25
|
+
*
|
|
26
|
+
* APK 侧在开始捕获时把 STREAM_MUSIC 调到 0(AudioEngine
|
|
27
|
+
* 里的 mutePhone),手机不出声,声音只在电脑播放。
|
|
28
|
+
* 停止投屏时自动恢复原音量。
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const { spawn } = require('node:child_process')
|
|
32
|
+
const path = require('node:path')
|
|
33
|
+
const net = require('node:net')
|
|
34
|
+
|
|
35
|
+
class AudioBridge {
|
|
36
|
+
/**
|
|
37
|
+
* @param {object} o
|
|
38
|
+
* @param {string} o.node node 可执行文件
|
|
39
|
+
* @param {string} o.receiverJs wifi-receiver.js 路径
|
|
40
|
+
* @param {string} o.adb adb 路径
|
|
41
|
+
* @param {number} [o.lanPort] receiver 的 LAN 端口
|
|
42
|
+
* @param {(m:string)=>void} [o.onLog]
|
|
43
|
+
* @param {(cfg:object)=>void} [o.onConfig] 收到 AudioSpecificConfig
|
|
44
|
+
* @param {(buf:Buffer, pts:number)=>void} [o.onFrame] 收到音频帧
|
|
45
|
+
* @param {(info:object)=>void} [o.onState] 状态变化
|
|
46
|
+
*/
|
|
47
|
+
constructor (o) {
|
|
48
|
+
this.node = o.node
|
|
49
|
+
this.receiverJs = o.receiverJs
|
|
50
|
+
this.adb = o.adb
|
|
51
|
+
this.lanPort = o.lanPort || 27190
|
|
52
|
+
this.onLog = o.onLog || (() => {})
|
|
53
|
+
this.onConfig = o.onConfig || (() => {})
|
|
54
|
+
this.onFrame = o.onFrame || (() => {})
|
|
55
|
+
this.onState = o.onState || (() => {})
|
|
56
|
+
|
|
57
|
+
this.proc = null
|
|
58
|
+
this.httpPort = this.lanPort + 1
|
|
59
|
+
this.pulling = false
|
|
60
|
+
this.running = false
|
|
61
|
+
this._frames = 0
|
|
62
|
+
this._cfgSent = false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
log (m) { this.onLog('[audio] ' + m) }
|
|
66
|
+
|
|
67
|
+
get stats () { return { frames: this._frames, running: this.running } }
|
|
68
|
+
|
|
69
|
+
/** 启动 wifi-receiver 子进程(如果还没起) */
|
|
70
|
+
async startReceiver () {
|
|
71
|
+
if (this.proc) return { ok: true, port: this.httpPort }
|
|
72
|
+
|
|
73
|
+
const args = [this.receiverJs, String(this.lanPort), String(this.httpPort), this.adb]
|
|
74
|
+
this.log(`启动接收端: node ${path.basename(this.receiverJs)} ${this.lanPort} ${this.httpPort}`)
|
|
75
|
+
|
|
76
|
+
this.proc = spawn(this.node, args, {
|
|
77
|
+
windowsHide: true,
|
|
78
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
this.proc.stdout.on('data', (d) => {
|
|
82
|
+
const s = d.toString().trim()
|
|
83
|
+
if (s) this.log(s.slice(0, 200))
|
|
84
|
+
})
|
|
85
|
+
this.proc.stderr.on('data', (d) => {
|
|
86
|
+
const s = d.toString().trim()
|
|
87
|
+
if (s && /error|fail|EADDR/i.test(s)) this.log('stderr: ' + s.slice(0, 200))
|
|
88
|
+
})
|
|
89
|
+
this.proc.on('exit', (code) => {
|
|
90
|
+
this.log(`接收端退出 code=${code}`)
|
|
91
|
+
this.proc = null
|
|
92
|
+
this.running = false
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
// 等端口就绪
|
|
96
|
+
const ok = await this._waitPort(this.httpPort, 8000)
|
|
97
|
+
if (!ok) {
|
|
98
|
+
this.log('接收端启动超时')
|
|
99
|
+
await this.stop()
|
|
100
|
+
return { ok: false, error: '接收端启动超时' }
|
|
101
|
+
}
|
|
102
|
+
this.log('接收端已就绪')
|
|
103
|
+
return { ok: true, port: this.httpPort }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 轮询 HTTP 端口直到可连 */
|
|
107
|
+
_waitPort (port, timeoutMs) {
|
|
108
|
+
const deadline = Date.now() + timeoutMs
|
|
109
|
+
return new Promise((resolve) => {
|
|
110
|
+
const tryOnce = () => {
|
|
111
|
+
if (Date.now() > deadline) return resolve(false)
|
|
112
|
+
const sock = new net.Socket()
|
|
113
|
+
let done = false
|
|
114
|
+
const finish = (v) => {
|
|
115
|
+
if (done) return
|
|
116
|
+
done = true
|
|
117
|
+
try { sock.destroy() } catch {}
|
|
118
|
+
if (v) resolve(true)
|
|
119
|
+
else setTimeout(tryOnce, 250)
|
|
120
|
+
}
|
|
121
|
+
sock.setTimeout(400)
|
|
122
|
+
sock.once('connect', () => finish(true))
|
|
123
|
+
sock.once('timeout', () => finish(false))
|
|
124
|
+
sock.once('error', () => finish(false))
|
|
125
|
+
sock.connect(port, '127.0.0.1')
|
|
126
|
+
}
|
|
127
|
+
tryOnce()
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 开始拉音频。
|
|
133
|
+
*
|
|
134
|
+
* 用「长轮询 + 批量」而不是每帧一个请求 —— 音频帧率约 43/s,
|
|
135
|
+
* 每帧一次 HTTP 往返的开销太大了。
|
|
136
|
+
*/
|
|
137
|
+
async startPulling () {
|
|
138
|
+
if (this.pulling) return
|
|
139
|
+
this.pulling = true
|
|
140
|
+
this.running = true
|
|
141
|
+
this._pullLoop()
|
|
142
|
+
this.log('开始拉取音频流')
|
|
143
|
+
|
|
144
|
+
// 先取一次解码器配置
|
|
145
|
+
this._fetchConfig().catch(() => {})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 取 AudioSpecificConfig。
|
|
150
|
+
*
|
|
151
|
+
* /audio-config 返回**裸二进制**(就是 AAC 的
|
|
152
|
+
* AudioSpecificConfig 那几个字节),不是 JSON。
|
|
153
|
+
* 204 = 还没有音频流(APK 没开始推)。
|
|
154
|
+
*/
|
|
155
|
+
async _fetchConfig () {
|
|
156
|
+
try {
|
|
157
|
+
const r = await fetch(`http://127.0.0.1:${this.httpPort}/audio-config`)
|
|
158
|
+
if (r.status === 204 || !r.ok) return false
|
|
159
|
+
const buf = Buffer.from(await r.arrayBuffer())
|
|
160
|
+
if (!buf.length) return false
|
|
161
|
+
this.log(`音频配置 ${buf.length}B(AudioSpecificConfig)`)
|
|
162
|
+
this.onConfig({ description: buf })
|
|
163
|
+
this._cfgSent = true
|
|
164
|
+
return true
|
|
165
|
+
} catch { return false }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 拉流主循环。
|
|
170
|
+
*
|
|
171
|
+
* /audio 返回的是一批帧(JSON 数组,每项 {data: base64, pts}),
|
|
172
|
+
* 一次最多取 8 帧(receiver 内部队列上限)。取到就立刻解码,
|
|
173
|
+
* 空了就等 20ms 再来 —— 这个节奏和音频帧率(~43/s)匹配。
|
|
174
|
+
*/
|
|
175
|
+
async _pullLoop () {
|
|
176
|
+
let idleStreak = 0
|
|
177
|
+
while (this.pulling) {
|
|
178
|
+
try {
|
|
179
|
+
/**
|
|
180
|
+
* /audio 是**二进制**协议,不是 JSON。
|
|
181
|
+
*
|
|
182
|
+
* 帧格式(receiver 侧 L467-488 写的):
|
|
183
|
+
* [4B] 帧数量 N(大端 uint32)
|
|
184
|
+
* 重复 N 次:
|
|
185
|
+
* [8B] pts(大端 double)
|
|
186
|
+
* [4B] 数据长度 L(大端 uint32)
|
|
187
|
+
* [L] AAC 裸帧
|
|
188
|
+
*
|
|
189
|
+
* HTTP 204 表示这次没数据(receiver 的长轮询超时)。
|
|
190
|
+
*/
|
|
191
|
+
const r = await fetch(`http://127.0.0.1:${this.httpPort}/audio?n=24&wait=800`, {
|
|
192
|
+
signal: AbortSignal.timeout(4000)
|
|
193
|
+
})
|
|
194
|
+
if (r.status === 204) {
|
|
195
|
+
idleStreak++
|
|
196
|
+
this.onState({ waiting: true })
|
|
197
|
+
await sleep(20)
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
200
|
+
if (!r.ok) throw new Error('HTTP ' + r.status)
|
|
201
|
+
|
|
202
|
+
const ab = Buffer.from(await r.arrayBuffer())
|
|
203
|
+
const list = parseAudioBatch(ab)
|
|
204
|
+
|
|
205
|
+
// 第一帧到达前再尝试一次配置(APK 可能刚推上来)
|
|
206
|
+
if (list.length && !this._cfgSent) {
|
|
207
|
+
await this._fetchConfig()
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (!list.length) {
|
|
211
|
+
idleStreak++
|
|
212
|
+
await sleep(idleStreak > 20 ? 100 : 20)
|
|
213
|
+
if (idleStreak === 25) this.onState({ waiting: true })
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
idleStreak = 0
|
|
218
|
+
this.onState({ waiting: false })
|
|
219
|
+
|
|
220
|
+
for (const f of list) {
|
|
221
|
+
if (!f || !f.data || !f.data.length) continue
|
|
222
|
+
this._frames++
|
|
223
|
+
this.onFrame(f.data, Number(f.pts) || 0)
|
|
224
|
+
}
|
|
225
|
+
} catch (e) {
|
|
226
|
+
if (!this.pulling) break
|
|
227
|
+
await sleep(200)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async stop () {
|
|
233
|
+
this.pulling = false
|
|
234
|
+
this.running = false
|
|
235
|
+
if (this.proc) {
|
|
236
|
+
try { this.proc.kill() } catch {}
|
|
237
|
+
this.proc = null
|
|
238
|
+
}
|
|
239
|
+
this.log('已停止')
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 解析 /audio 的二进制批。
|
|
245
|
+
*
|
|
246
|
+
* 布局:N(4) + N * [pts(8, double BE) + len(4) + data(len)]
|
|
247
|
+
* 用 readDoubleBE 读 pts —— receiver 那边写的是 writeDoubleBE。
|
|
248
|
+
*/
|
|
249
|
+
function parseAudioBatch (buf) {
|
|
250
|
+
if (!buf || buf.length < 4) return []
|
|
251
|
+
const count = buf.readUInt32BE(0)
|
|
252
|
+
const out = []
|
|
253
|
+
let off = 4
|
|
254
|
+
for (let i = 0; i < count; i++) {
|
|
255
|
+
if (off + 12 > buf.length) break
|
|
256
|
+
const pts = buf.readDoubleBE(off); off += 8
|
|
257
|
+
const len = buf.readUInt32BE(off); off += 4
|
|
258
|
+
if (len <= 0 || off + len > buf.length) break
|
|
259
|
+
out.push({ data: buf.subarray(off, off + len), pts })
|
|
260
|
+
off += len
|
|
261
|
+
}
|
|
262
|
+
return out
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function sleep (ms) { return new Promise(r => setTimeout(r, ms)) }
|
|
266
|
+
|
|
267
|
+
module.exports = { AudioBridge }
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 音频链路自愈 —— 让声音断了能自己接回来,不用手动重启。
|
|
3
|
+
*
|
|
4
|
+
* ══════════ 要解决什么 ══════════
|
|
5
|
+
*
|
|
6
|
+
* 这台华为(HarmonyOS 4.2)对「第三方 App 持续占用 MediaProjection」
|
|
7
|
+
* 管得非常严:
|
|
8
|
+
*
|
|
9
|
+
* - 已经加进 deviceidle whitelist
|
|
10
|
+
* - 已经 appops RUN_IN_BACKGROUND allow
|
|
11
|
+
* - 前台服务 + 通知栏常驻
|
|
12
|
+
*
|
|
13
|
+
* 仍然会被杀。实测表现:
|
|
14
|
+
* audioFrames 停止增长 → /meta 返回 connected=false → ps 查不到进程
|
|
15
|
+
*
|
|
16
|
+
* 被杀之后声音就断了,而画面(scrcpy)还在,用户看到的就是
|
|
17
|
+
* 「有画面没声音」,必须手动重新推深链才有声 —— 这个体验不可接受。
|
|
18
|
+
*
|
|
19
|
+
* ══════════ 怎么自愈 ══════════
|
|
20
|
+
*
|
|
21
|
+
* 每 3 秒探一次 /meta:
|
|
22
|
+
* connected=true → 正常,重置计数
|
|
23
|
+
* connected=false 但刚断 → 容忍期(APK 可能在重启)
|
|
24
|
+
* 连续 N 次 false → 判定死了,自动:
|
|
25
|
+
* ① adb reverse 重建(隧道可能随 App 一起丢了)
|
|
26
|
+
* ② am start 深链拉起 APK
|
|
27
|
+
* ③ 若弹了 MediaProjection 授权框,自动点「允许」
|
|
28
|
+
*
|
|
29
|
+
* 授权框的坐标不固定,所以用 uiautomator dump 找「允许」按钮的真实
|
|
30
|
+
* bounds,不硬编码坐标 —— 硬编码会点错地方(实测点到了红果短剧)。
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
const { execFile } = require('node:child_process')
|
|
34
|
+
|
|
35
|
+
class AudioHealer {
|
|
36
|
+
/**
|
|
37
|
+
* @param {object} o
|
|
38
|
+
* @param {string} o.adb adb 路径
|
|
39
|
+
* @param {string} o.serial 设备序列号
|
|
40
|
+
* @param {number} [o.port] 接收端 TCP 端口(默认 27190)
|
|
41
|
+
* @param {number} [o.httpPort] 接收端 HTTP 端口(默认 27191)
|
|
42
|
+
* @param {(m:string)=>void} [o.onLog]
|
|
43
|
+
* @param {()=>void} [o.onRecovered] 恢复成功回调
|
|
44
|
+
*/
|
|
45
|
+
constructor (o) {
|
|
46
|
+
this.adb = o.adb
|
|
47
|
+
this.serial = o.serial
|
|
48
|
+
this.port = o.port || 27190
|
|
49
|
+
this.httpPort = o.httpPort || 27191
|
|
50
|
+
this.onLog = o.onLog || (() => {})
|
|
51
|
+
this.onRecovered = o.onRecovered || (() => {})
|
|
52
|
+
|
|
53
|
+
this.timer = null
|
|
54
|
+
this.deadStreak = 0
|
|
55
|
+
this.recovering = false
|
|
56
|
+
this.lastRecoverAt = 0
|
|
57
|
+
this.recoverCount = 0
|
|
58
|
+
|
|
59
|
+
/** 连续多少次探测不到连接才判定死亡(3 秒一次 → 9 秒) */
|
|
60
|
+
this.DEAD_THRESHOLD = 3
|
|
61
|
+
/** 两次恢复尝试的最小间隔,避免风暴 */
|
|
62
|
+
this.RECOVER_COOLDOWN = 12000
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
log (m) { this.onLog('[heal] ' + m) }
|
|
66
|
+
|
|
67
|
+
_run (args, timeout = 10000) {
|
|
68
|
+
return new Promise(resolve => {
|
|
69
|
+
execFile(this.adb, args, { timeout, windowsHide: true },
|
|
70
|
+
(err, stdout, stderr) => resolve({
|
|
71
|
+
ok: !err,
|
|
72
|
+
out: String(stdout || ''),
|
|
73
|
+
err: String(stderr || (err && err.message) || '')
|
|
74
|
+
}))
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
start () {
|
|
79
|
+
this.stop()
|
|
80
|
+
this.log('音频自愈已启动')
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* ══════════ 首次立即拉起,不等 9 秒 ══════════
|
|
84
|
+
*
|
|
85
|
+
* 用户的原话:「码呢,手机上扫码我扫你妈」。
|
|
86
|
+
*
|
|
87
|
+
* 之前的设计要求用户在手机上点「扫码连接」再对准 PC 的二维码 ——
|
|
88
|
+
* 但 PC 端根本没展示二维码,这个流程完全是多余的仪式。
|
|
89
|
+
*
|
|
90
|
+
* 现在 PC 侧会话一开,这里就主动:
|
|
91
|
+
* ① adb reverse
|
|
92
|
+
* ② am start 深链(拉起 APK 并直接进入投屏)
|
|
93
|
+
* ③ 自动点「允许」授权
|
|
94
|
+
*
|
|
95
|
+
* 用户插上线(或 WiFi 连着)什么都不用做,声音就通了。
|
|
96
|
+
*/
|
|
97
|
+
this._kickoff().finally(() => {
|
|
98
|
+
const tick = async () => {
|
|
99
|
+
try { await this._check() } catch {}
|
|
100
|
+
this.timer = setTimeout(tick, 3000)
|
|
101
|
+
}
|
|
102
|
+
tick()
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 首次拉起:会话一开始就主动把 APK 拉起来并授权 */
|
|
107
|
+
async _kickoff () {
|
|
108
|
+
try {
|
|
109
|
+
// 隧道先建好,否则 APK 启动后连不上
|
|
110
|
+
await this._run(['-s', this.serial, 'reverse',
|
|
111
|
+
`tcp:${this.port}`, `tcp:${this.port}`], 8000)
|
|
112
|
+
|
|
113
|
+
const alive = await this._apkAlive()
|
|
114
|
+
if (alive) {
|
|
115
|
+
// 已在跑,看看连上没。没连上就重推一次深链。
|
|
116
|
+
const m = await this._meta()
|
|
117
|
+
if (m && m.connected) {
|
|
118
|
+
this.log('音频已在线,无需拉起')
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.log('正在自动拉起手机端…')
|
|
124
|
+
await this._startViaDeeplink()
|
|
125
|
+
|
|
126
|
+
await this._autoGrant()
|
|
127
|
+
|
|
128
|
+
await new Promise(r => setTimeout(r, 12000))
|
|
129
|
+
const m2 = await this._meta()
|
|
130
|
+
if (m2 && m2.connected) {
|
|
131
|
+
this.log('✓ 音频已自动连通(无需手动操作手机)')
|
|
132
|
+
this.onRecovered()
|
|
133
|
+
} else {
|
|
134
|
+
this.log('自动拉起后仍未连通,转入周期性重试')
|
|
135
|
+
}
|
|
136
|
+
} catch (e) {
|
|
137
|
+
this.log('自动拉起异常: ' + e.message)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
stop () {
|
|
142
|
+
if (this.timer) { clearTimeout(this.timer); this.timer = null }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async _meta () {
|
|
146
|
+
try {
|
|
147
|
+
const r = await fetch(`http://127.0.0.1:${this.httpPort}/meta`, {
|
|
148
|
+
signal: AbortSignal.timeout(2500)
|
|
149
|
+
})
|
|
150
|
+
if (!r.ok) return null
|
|
151
|
+
return await r.json()
|
|
152
|
+
} catch { return null }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async _check () {
|
|
156
|
+
if (this.recovering) return
|
|
157
|
+
|
|
158
|
+
const m = await this._meta()
|
|
159
|
+
if (!m) return // 接收端自己没跑,不管
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 把「音频到底通没通」暴露出去 —— 主进程要把它并进每秒的状态推送里,
|
|
163
|
+
* 让界面上能看见音频状态。
|
|
164
|
+
*
|
|
165
|
+
* 用户的投诉:「有时候画面在跑但电脑没声音,我根本不知道是没连上
|
|
166
|
+
* 还是我电脑静音了」。以前这个信息只躺在日志里。
|
|
167
|
+
*/
|
|
168
|
+
this.connected = !!m.connected
|
|
169
|
+
this.frames = m.audioFrames || 0
|
|
170
|
+
|
|
171
|
+
if (m.connected) {
|
|
172
|
+
if (this.deadStreak > 0) this.log('音频连接已恢复')
|
|
173
|
+
this.deadStreak = 0
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this.deadStreak++
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 关键:区分「APK 活着但还没连上」和「APK 真的死了」。
|
|
181
|
+
*
|
|
182
|
+
* 实测:APK 从启动到建立起 TCP 连接要 8~12 秒
|
|
183
|
+
* (MediaProjection 授权 → 前台服务 → 建 socket → 推首帧)。
|
|
184
|
+
* 早期版本 9 秒就判死,于是「刚拉起 → 判定失败 → 再拉起」死循环,
|
|
185
|
+
* 每次重拉还会把已经快建好的连接掐断,永远连不上。
|
|
186
|
+
*
|
|
187
|
+
* 现在:先看进程在不在。
|
|
188
|
+
* 进程在 → 只等,不重拉(耐心等它自己连上)
|
|
189
|
+
* 进程没了 → 立刻重拉
|
|
190
|
+
*/
|
|
191
|
+
if (this.deadStreak < this.DEAD_THRESHOLD) return
|
|
192
|
+
if (Date.now() - this.lastRecoverAt < this.RECOVER_COOLDOWN) return
|
|
193
|
+
|
|
194
|
+
const alive = await this._apkAlive()
|
|
195
|
+
if (alive) {
|
|
196
|
+
// APK 还活着,只是在建连接阶段。再给它一个周期,不打断。
|
|
197
|
+
if (this.deadStreak < this.DEAD_THRESHOLD * 4) return
|
|
198
|
+
this.log('APK 存活但长时间连不上,重建隧道')
|
|
199
|
+
} else {
|
|
200
|
+
this.log('APK 已退出,重新拉起')
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this.lastRecoverAt = Date.now()
|
|
204
|
+
this.deadStreak = 0
|
|
205
|
+
await this._recover()
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** APK 进程是否还在 */
|
|
209
|
+
async _apkAlive () {
|
|
210
|
+
try {
|
|
211
|
+
const r = await this._run(['-s', this.serial, 'shell',
|
|
212
|
+
'ps -A 2>/dev/null | grep -c com.miniwin.stream'], 6000)
|
|
213
|
+
const n = parseInt((r.out || '0').trim(), 10)
|
|
214
|
+
return n > 0
|
|
215
|
+
} catch { return false }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async _recover () {
|
|
219
|
+
if (this.recovering) return
|
|
220
|
+
this.recovering = true
|
|
221
|
+
this.recoverCount++
|
|
222
|
+
this.log(`音频断开,第 ${this.recoverCount} 次自动恢复…`)
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
/**
|
|
226
|
+
* ⓪ 先检查是否有**僵尸 MediaProjection 会话**。
|
|
227
|
+
*
|
|
228
|
+
* 踩过的坑(实测):
|
|
229
|
+
* dumpsys media_projection 显示
|
|
230
|
+
* Media Projection:
|
|
231
|
+
* (com.miniwin.stream, uid=10617): TYPE_SCREEN_CAPTURE
|
|
232
|
+
* 但进程其实早就不在了。
|
|
233
|
+
*
|
|
234
|
+
* Android 规定同一 UID 只能有一个活跃投影会话,
|
|
235
|
+
* 所以后续所有授权请求都被**静默拒绝**:
|
|
236
|
+
* - 不弹授权框
|
|
237
|
+
* - Service 不启动
|
|
238
|
+
* - 界面停在「启动投屏服务…」
|
|
239
|
+
* - 音频链路完全起不来 → 手机继续外放、电脑没声音
|
|
240
|
+
*
|
|
241
|
+
* 更坑的是 `am force-stop` 清不掉它,必须 `pm clear`。
|
|
242
|
+
* 所以这里主动检测并清理。
|
|
243
|
+
*/
|
|
244
|
+
const stale = await this._run(['-s', this.serial, 'shell',
|
|
245
|
+
'dumpsys media_projection | grep -c "com.miniwin.stream"'], 6000)
|
|
246
|
+
const hasStale = parseInt((stale.out || '0').trim(), 10) > 0
|
|
247
|
+
|
|
248
|
+
if (hasStale) {
|
|
249
|
+
const alive = await this._apkAlive()
|
|
250
|
+
if (!alive) {
|
|
251
|
+
this.log('检测到僵尸投影会话(进程已死但会话未释放),正在清理…')
|
|
252
|
+
// pm clear 是唯一能强制释放的手段
|
|
253
|
+
await this._run(['-s', this.serial, 'shell', 'pm', 'clear', 'com.miniwin.stream'], 15000)
|
|
254
|
+
await new Promise(r => setTimeout(r, 1500))
|
|
255
|
+
// pm clear 会清掉运行时权限,必须补回来
|
|
256
|
+
await this._run(['-s', this.serial, 'shell', 'pm', 'grant',
|
|
257
|
+
'com.miniwin.stream', 'android.permission.RECORD_AUDIO'], 8000)
|
|
258
|
+
this.log('僵尸会话已清理,权限已恢复')
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ① 重建反向隧道(App 被杀时隧道通常还在,但重建一次更稳)
|
|
263
|
+
await this._run(['-s', this.serial, 'reverse',
|
|
264
|
+
`tcp:${this.port}`, `tcp:${this.port}`], 8000)
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* ①.5 APK 活着但没连上 → 必须先 force-stop。
|
|
268
|
+
*
|
|
269
|
+
* StreamService.onStartCommand 里写着:
|
|
270
|
+
* if (intent == null || engine != null) return START_NOT_STICKY;
|
|
271
|
+
* engine 在 onDestroy 里才置空。也就是说一旦引擎进入
|
|
272
|
+
* 「连接断开,已停止重连」状态,进程还在、engine 还在,
|
|
273
|
+
* 之后**所有**深链都会被这一行直接吞掉 —— 界面永远停在
|
|
274
|
+
* 「重新连接」,点它也只是重连旧引擎。
|
|
275
|
+
*/
|
|
276
|
+
const mNow = await this._meta()
|
|
277
|
+
if ((await this._apkAlive()) && !(mNow && mNow.connected)) {
|
|
278
|
+
this.log('APK 存活但连接已死,强制重启 App 以清掉僵死引擎')
|
|
279
|
+
await this._run(['-s', this.serial, 'shell', 'am', 'force-stop', 'com.miniwin.stream'], 10000)
|
|
280
|
+
await new Promise(r => setTimeout(r, 1200))
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ② 拉起 APK
|
|
284
|
+
const r = await this._startViaDeeplink()
|
|
285
|
+
if (!r.ok) this.log('拉起 APK 失败: ' + String(r.err || '').slice(0, 80))
|
|
286
|
+
|
|
287
|
+
// ③ 等授权框出现并自动点「允许」
|
|
288
|
+
await this._autoGrant()
|
|
289
|
+
|
|
290
|
+
// ④ 验证 —— 给足 12 秒(授权 + 建连接 + 首帧)
|
|
291
|
+
await new Promise(r2 => setTimeout(r2, 12000))
|
|
292
|
+
const m = await this._meta()
|
|
293
|
+
if (m && m.connected) {
|
|
294
|
+
this.log('✓ 音频已恢复')
|
|
295
|
+
this.onRecovered()
|
|
296
|
+
} else {
|
|
297
|
+
this.log('恢复未成功,下轮再试')
|
|
298
|
+
}
|
|
299
|
+
} catch (e) {
|
|
300
|
+
this.log('恢复异常: ' + e.message)
|
|
301
|
+
} finally {
|
|
302
|
+
this.recovering = false
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* ══════════ 用深链拉起 APK(带端口,必须加引号)══════════
|
|
308
|
+
*
|
|
309
|
+
* ── 这里是「声音永远连不上」的真正元凶 ──
|
|
310
|
+
*
|
|
311
|
+
* 原来是这么发的:
|
|
312
|
+
* am start -a android.intent.action.VIEW \
|
|
313
|
+
* -d miniwin://stream?host=127.0.0.1&port=27190
|
|
314
|
+
*
|
|
315
|
+
* 走的是 `adb shell`,而 adb 会把这些 argv 用空格拼成**一条**命令行
|
|
316
|
+
* 交给手机上的 /system/bin/sh 去解析。于是那个 `&` 变成了
|
|
317
|
+
* shell 的后台运算符:
|
|
318
|
+
*
|
|
319
|
+
* sh -c "am start ... -d miniwin://stream?host=127.0.0.1" &
|
|
320
|
+
* "port=27190" ← 被当成一条新命令,直接失败
|
|
321
|
+
*
|
|
322
|
+
* 结果 APK 收到的 Intent 里**只有 host,没有 port**:
|
|
323
|
+
* dat=miniwin://stream?host=127.0.0.1 ← logcat 实证
|
|
324
|
+
*
|
|
325
|
+
* 而 StreamActivity 的判断是 `h != null && p != null` 才连 ——
|
|
326
|
+
* host 有、port 没有,条件不成立,既不连接也不报错,
|
|
327
|
+
* 界面停在「重新连接 / 连接断开,已停止重连」,
|
|
328
|
+
* 手机继续外放、电脑永远没声音。
|
|
329
|
+
*
|
|
330
|
+
* 修法:把整条 URL 用单引号包起来,让手机端 shell 不再拆它。
|
|
331
|
+
* 实测验证:加引号后 logcat 里出现完整
|
|
332
|
+
* dat=miniwin://stream?host=127.0.0.1&port=27190
|
|
333
|
+
* 授权弹窗随即出现,音频建立成功。
|
|
334
|
+
*/
|
|
335
|
+
async _startViaDeeplink () {
|
|
336
|
+
return this._run(['-s', this.serial, 'shell', 'am', 'start',
|
|
337
|
+
'-a', 'android.intent.action.VIEW',
|
|
338
|
+
'-d', `'miniwin://stream?host=127.0.0.1&port=${this.port}'`], 15000)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* 自动点「允许」。
|
|
343
|
+
*
|
|
344
|
+
* 不硬编码坐标 —— 不同 ROM / 不同分辨率下按钮位置差别很大,
|
|
345
|
+
* 硬编码会点错(实测点早跳到红果短剧去了)。
|
|
346
|
+
*
|
|
347
|
+
* 做法:uiautomator dump 出 XML,正则找 text="允许" 的 bounds,
|
|
348
|
+
* 取中心点。找不到就说明没弹窗(已授权过),直接返回。
|
|
349
|
+
*/
|
|
350
|
+
async _autoGrant () {
|
|
351
|
+
for (let i = 0; i < 4; i++) {
|
|
352
|
+
await new Promise(r => setTimeout(r, 1200))
|
|
353
|
+
const dump = await this._run(['-s', this.serial, 'shell',
|
|
354
|
+
'uiautomator dump /sdcard/_heal.xml >/dev/null 2>&1; cat /sdcard/_heal.xml'], 8000)
|
|
355
|
+
if (!dump.out) continue
|
|
356
|
+
|
|
357
|
+
const hit = /text="(?:允许|立即开始)"[^>]*bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/.exec(dump.out)
|
|
358
|
+
if (!hit) continue
|
|
359
|
+
|
|
360
|
+
const x = Math.round((Number(hit[1]) + Number(hit[3])) / 2)
|
|
361
|
+
const y = Math.round((Number(hit[2]) + Number(hit[4])) / 2)
|
|
362
|
+
await this._run(['-s', this.serial, 'shell', 'input', 'tap', String(x), String(y)], 8000)
|
|
363
|
+
this.log(`已点授权 (${x},${y})`)
|
|
364
|
+
return
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
module.exports = { AudioHealer }
|