Fix(android): keep encoder operating rate at 30 to restore screen streaming

3ab5154 tied KEY_OPERATING_RATE to the tuned fps, so any profile below 30 (Good=20, Medium=15, Low=12, Minimal=8) pushed it under 30. Some hardware encoders, notably TV SoCs, then drop into a low-power mode that accepts configure/createInputSurface/start without error but never emits a single buffer. The controller receives BITMAPINFO and waits forever on the first decoded frame.

Confirmed on a Changhong Android TV: with operating rate 20 the log stopped right after SendBitmapInfo with no H.264 output at all; clamping to maxOf(fps, 30) produced a first IDR within the same second. KEY_OPERATING_RATE is a resource-planning hint, not a frame limiter - the actual rate is driven by KEY_FRAME_RATE and scene content.

Harden the capture lifecycle while here: fail loudly when createVirtualDisplay returns null instead of advertising a dead surface; downgrade the foreground type back to dataSync before stopping that projection, otherwise Android 14+ tears down a service still declaring mediaProjection and the controller loses the connection entirely; release the previous capture when MainActivity is reopened, including the projection-only state left behind when encoder creation throws before mediaCodec is assigned; ignore stale MediaProjection onStop callbacks that would otherwise tear down a fresh encoder; zero the native screen size on teardown; and make SendLoop's wait timeout so a silent zero-output encoder is reported instead of hanging.
This commit is contained in:
yuanyuanxiang committed 2026-11-14 20:09:14 +01:00
1 parent db4543ad54
commit 7faca2c01b
2 files changed
+96 -4

No files matched your search

@@ -253,6 +253,22 @@ class CaptureService : Service() {
// Android 14+: upgrade foreground type to MEDIA_PROJECTION in the same
// start command that calls getMediaProjection(), as required by API 34+.
startForegroundTyped(ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
// 重复打开 MainActivity 会再次走到这里:它只在系统重建 Activity
// (savedInstanceState != null)时才跳过授权,用户手动再点一次图标仍会
// 重新走授权 → startForegroundService → 本函数。不先收掉上一次的
// VirtualDisplay 和编码器,就会同时存在两套镜像 + 两路编码,白耗 CPU,
// 而且旧 projection 一直挂在 onStop 回调上。
//
// mediaProjection 也必须纳入判断:本函数先赋值 mediaProjection,之后才进
// startCapture() 建编码器。若 startCapture 在 mediaCodec 赋值之前抛异常
// (createEncoderByType 失败、三套 configure 全失败),或分辨率非法压根
// 没进 startCapture,就会停在「只有 mediaProjection 非空」的状态。只看
// codec/vd 会漏掉它,旧 projection 永不 stop(),投屏指示器一直亮着,
// 它的 onStop 回调也继续挂着。
if (mediaCodec != null || virtualDisplay != null || mediaProjection != null) {
Log.i(TAG, "onStartCommand: releasing previous capture before restart")
stopCapture()
}
val pm = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
val mp = pm.getMediaProjection(projResult, projData)
mediaProjection = mp
@@ -345,6 +361,14 @@ class CaptureService : Service() {
// Android 14+ requires callback registered before createVirtualDisplay()
mp.registerCallback(object : MediaProjection.Callback() {
override fun onStop() {
// stop() 的回调是异步投递到主线程的,可能在新一轮 startCapture 之后
// 才到达。身份不一致说明这是被我们主动收掉的旧 projection,忽略它,
// 否则迟到的回调会把刚建好的 VirtualDisplay 和编码器一起拆掉。
// 顺带也让 stopCapture() 内部的 mp.stop() 不再递归触发自己。
if (mediaProjection !== mp) {
Log.i(TAG, "stale MediaProjection onStop ignored")
return
}
Log.i(TAG, "MediaProjection stopped — switching to dataSync foreground type")
stopCapture()
// Android 14+: system auto-kills a foreground service whose mediaProjection
@@ -353,12 +377,40 @@ class CaptureService : Service() {
}
}, null)
virtualDisplay = mp.createVirtualDisplay(
val vd = mp.createVirtualDisplay(
"YAMA",
encW, encH, dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
surface, null, null
)
// 返回 null 不会抛异常。原实现直接往下走并调用 nativeSetScreenSize,于是
// ScreenSpyThread 认为投屏就绪、照常发 BITMAPINFO,而 surface 没有任何生产者,
// 编码器永远不输出 → 服务端停在"请等待"。这里必须显式失败,不能静默。
if (vd == null) {
Log.e(TAG, "createVirtualDisplay returned null (enc=${encW}x${encH} dpi=$dpi) — " +
"no frames will be produced; check MediaProjection token")
// 500ms 前已经 postDelayed 过 idrRunnable,不收掉它会每秒空转(mediaCodec
// 已置 null,setParameters 是 null-safe,但没意义)。
idrHandler.removeCallbacks(idrRunnable)
runCatching { codec.stop() }
runCatching { codec.release() }
mediaCodec = null
// projection 也要收掉:token 是一次性的,留着它既占资源,又让 onStop 回调
// 继续挂在上面;下次授权拿到的会是另一个 projection。
// 先置 null,让下面 mp.stop() 触发的异步 onStop 落到 stale 分支,不会再把
// 刚降级好的前台类型改回去、也不会重入 stopCapture。
mediaProjection = null
// 关键:进本函数前 onStartCommand 已把前台类型升级成 MEDIA_PROJECTION。
// projection 一旦 stop(),Android 14+ 会把仍声明该类型的前台服务一并停掉,
// 服务被杀 → onDestroy → nativeStop → 主控端直接掉线。这里虽然没有画面可抓,
// 但鼠标键盘控制、以及靠这条连接下发新档位/触发重新授权的通道都要保住,
// 所以必须先降级回 DATA_SYNC(与 onStop 处理器同一套处理)。降级放在 stop()
// 之前,避免系统按「此刻仍是 mediaProjection 类型」判定。
startForegroundTyped(ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
runCatching { mp.stop() }
return
}
virtualDisplay = vd
YamaBridge.nativeSetScreenSize(encW, encH)
@@ -379,7 +431,13 @@ class CaptureService : Service() {
setInteger(MediaFormat.KEY_FRAME_RATE, fps)
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2)
setInteger(MediaFormat.KEY_PRIORITY, 0)
setInteger(MediaFormat.KEY_OPERATING_RATE, fps)
// KEY_OPERATING_RATE 是给编码器的资源规划提示,不是限帧开关——真正的出帧
// 节奏由画面内容和 KEY_FRAME_RATE 的码控决定。3ab5154 把它从写死的 30 改成
// fps 之后,只要档位低于 30(Good=20、Medium=15、Low=12、Minimal=8)就会把
// 它压到 30 以下;部分硬件编码器(TV 芯片尤其明显)会据此进入低功耗档,
// configure/start 都成功却一帧都不输出,表现就是主控端一直停在"请等待"。
// 保持不低于 30,与改动前固定 30 的行为一致。
setInteger(MediaFormat.KEY_OPERATING_RATE, maxOf(fps, 30))
if (lowLatency) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
setInteger(MediaFormat.KEY_LATENCY, 0)
@@ -423,6 +481,13 @@ class CaptureService : Service() {
virtualDisplay = null
mediaCodec = null
mediaProjection = null
// 编码器已经没了,必须把 native 侧记录的尺寸一起清零。ScreenSpyThread 只靠
// g_screenWidth != 0 判断「投屏就绪」,不清零就会出现:MediaProjection 被系统
// 停掉(电视待机/切走/系统回收都会触发 onStop)后编码器已释放,但尺寸还是旧值,
// 客户端照常建子连接、发 TOKEN_BITMAPINFO,服务端收到尺寸后进入"请等待",而
// 编码器一帧都产不出来 → 永远停在"请等待"。token 是一次性的,客户端自己恢复
// 不了,只能重新走授权,所以这里唯一正确的动作是如实报告「没有投屏」。
YamaBridge.nativeSetScreenSize(0, 0)
}
// ---------------------------------------------------------------- 辅助函数