diff --git a/README.md b/README.md index 94bfc68..f85770c 100644 --- a/README.md +++ b/README.md @@ -433,8 +433,10 @@ POST /mirror/request?token= 触发一次投屏确认(便 ## 8. 平台能力边界(写进 README 免责) -- **Android 被控**:普通签名下 MediaProjection 每次连接弹授权框(系统限制),控制只能走 Accessibility, - 延迟高;**系统签名应用**(安装在系统分区 + 平台签名 / `android.uid.system`)三项全免:屏幕录制用 +- **Android 被控**:普通签名下 MediaProjection 每次投屏都要重新授权(系统限制)。Android 14+ 起一个 + `MediaProjection` 实例只允许建一次 VirtualDisplay,用完即废,复用会抛 `SecurityException` 且永远没画面; + 因此被控端把凭证当一次性资源:开流即消费、停流即 `stop()`,下一次投屏重新走授权。 + 控制只能走 Accessibility,延迟高;**系统签名应用**(安装在系统分区 + 平台签名 / `android.uid.system`)三项全免:屏幕录制用 `CAPTURE_VIDEO_OUTPUT` 直采、输入用 `INJECT_EVENTS`、运行时权限安装即授予(代价是该模式没有音频) - **iOS 被控**:❌ 不做,无系统级采集 API - **主控端**:Flutter 四端全覆盖,视频 External Texture(Win: 当前为 CPU BGRA + GDI,可升级 D3D11→DXGI 共享;mac: CVPixelBuffer→Metal;Linux: glTexSubImage2D;Android/iOS: 平台纹理) diff --git a/android_receiver/app/src/main/java/com/ttstd/lancast/receiver/MediaProjectionService.kt b/android_receiver/app/src/main/java/com/ttstd/lancast/receiver/MediaProjectionService.kt index 435e0a1..7f48050 100644 --- a/android_receiver/app/src/main/java/com/ttstd/lancast/receiver/MediaProjectionService.kt +++ b/android_receiver/app/src/main/java/com/ttstd/lancast/receiver/MediaProjectionService.kt @@ -78,6 +78,7 @@ class MediaProjectionService : Service() { var pendingRequest = false private set + /** 是否持有「尚未消费」的屏幕录制凭证(凭证一次性,投屏一次后即失效)。 */ @Volatile var projectionReady = false private set @@ -92,10 +93,14 @@ class MediaProjectionService : Service() { var directCapture = false private set - /** 已持有可用的屏幕录制凭证(不代表正在投屏)。 */ + /** 是否持有可用的屏幕录制凭证(不代表正在投屏)。 */ val hasProjection: Boolean get() = projectionReady - /** 当前是否具备开流条件:系统签名免授权,或已拿到屏幕录制凭证。 */ + /** + * 当前是否具备开流条件:系统签名免授权,或手握一张还没用过的一次性凭证。 + * + * 投屏一次后凭证即被消费,这里会变回 false,主控端再请求时必须重新授权。 + */ val canCapture: Boolean get() = directCapture || projectionReady val isStreaming: Boolean get() = state == State.STREAMING @@ -148,8 +153,16 @@ class MediaProjectionService : Service() { private var beacon: HostBeacon? = null private var fileServer: FileHttpServer? = null - private var projection: MediaProjection? = null - private var projectionCallback: MediaProjection.Callback? = null + /** + * 已授权、还没用过的一次性凭证。 + * + * Android 14+ 起一个 MediaProjection 实例只允许建一次 VirtualDisplay,用完即废, + * 所以「授权一次」只能支撑一次投屏;下次投屏必须重新走系统授权。 + */ + private var pendingProjection: ProjectionHandle? = null + + /** 当前投屏会话正在使用的凭证;投屏结束必须释放,不能再次拿去开流。 */ + private var activeProjection: ProjectionHandle? = null private var hostOverride = "" private var signalingUrl = "" @@ -239,9 +252,11 @@ class MediaProjectionService : Service() { handler.post { handleMirrorAction(action, message) } } created.onStreamingLost = { - projectionReady = false - state = State.STANDBY - updateNotification() + handler.post { + releaseActiveProjection() + state = State.STANDBY + updateNotification() + } } session = created created.start() @@ -339,13 +354,22 @@ class MediaProjectionService : Service() { // 这里在真正开流前再确认一次(此时用户已授予屏幕录制权限)。 ensureForeground() val current = session ?: return - val active = projection - if (active == null && !directCapture) { - current.notifyMirror(MirrorAction.ERROR, "设备尚未授予屏幕录制权限") - Toast.makeText(this, "尚未授权屏幕录制,请先完成授权", Toast.LENGTH_SHORT).show() + val pending = pendingProjection + if (pending == null && !directCapture) { + // 凭证可能已被上一次投屏消费掉(Android 14+ 一次性),这里不能再拿旧凭证开流, + // 否则 createVirtualDisplay 会抛 SecurityException 且永远没有画面。 + current.notifyMirror(MirrorAction.ERROR, "屏幕录制授权已失效,请重新授权") + Toast.makeText(this, "屏幕录制授权已失效,请重新授权", Toast.LENGTH_SHORT).show() return } cancelPendingRequest() + // 一次性凭证:交给采集链路前就挪出待用槽位,之后只能释放、不能复用。 + if (pending != null) { + pendingProjection = null + projectionReady = false + activeProjection = pending + } + val active = pending?.projection execWorker { val started = current.beginMirroring(active) handler.post { @@ -356,6 +380,8 @@ class MediaProjectionService : Service() { } else { state = State.STANDBY current.notifyMirror(MirrorAction.ERROR, "屏幕采集启动失败") + // 启动失败说明凭证已经不可用,别留着骗下一次开流。 + releaseActiveProjection() } updateNotification() } @@ -364,7 +390,15 @@ class MediaProjectionService : Service() { private fun endMirroring(reply: Boolean) { val current = session - if (current != null) execWorker { current.endMirroring() } + if (current != null) { + execWorker { + current.endMirroring() + // 采集已停,凭证也已被消费:必须 stop 掉,下次投屏重新授权。 + handler.post { releaseActiveProjection() } + } + } else { + releaseActiveProjection() + } if (state == State.STREAMING) { state = State.STANDBY if (reply) session?.notifyMirror(MirrorAction.STOPPED) @@ -373,10 +407,11 @@ class MediaProjectionService : Service() { } } - /** 拿到/复用 MediaProjection 凭证。真正创建 VirtualDisplay 在 beginMirroring 里。 */ + /** 拿到一张尚未消费的 MediaProjection 凭证。真正创建 VirtualDisplay 在 beginMirroring 里。 */ private fun attachProjection(resultCode: Int, data: Intent, mirrorAfter: Boolean) { ensureSession() - if (projection != null && projectionReady) { + if (pendingProjection != null) { + // 手上已经有一张没用过的一次性凭证,直接拿它开流。 if (mirrorAfter) beginMirroring() return } @@ -397,17 +432,24 @@ class MediaProjectionService : Service() { override fun onStop() { Log.i(TAG, "MediaProjection 已被系统或用户停止") handler.post { - if (projection === created) { - releaseProjection() - session?.endMirroring() - state = if (session != null) State.STANDBY else State.IDLE - updateNotification() + val pending = pendingProjection + val active = activeProjection + if (pending?.projection !== created && active?.projection !== created) { + // 我们自己 release 时已先清空引用,走到这里说明是外部停止。 + return@post } + if (pending?.projection === created) { + pendingProjection = null + projectionReady = false + } + if (active?.projection === created) releaseActiveProjection() + session?.endMirroring() + state = if (session != null) State.STANDBY else State.IDLE + updateNotification() } } } - projection = created - projectionCallback = callback + pendingProjection = ProjectionHandle(created, callback) projectionReady = true created.registerCallback(callback, handler) Log.i(TAG, "已获得屏幕录制凭证(尚未投屏)") @@ -415,20 +457,18 @@ class MediaProjectionService : Service() { if (mirrorAfter) beginMirroring() } - private fun releaseProjection() { - val callback = projectionCallback - val current = projection - projectionCallback = null - projection = null + private fun releaseActiveProjection() { + val handle = activeProjection ?: return + activeProjection = null + handle.release() + } + + private fun releaseAllProjections() { + val handles = listOfNotNull(pendingProjection, activeProjection) + pendingProjection = null + activeProjection = null projectionReady = false - if (callback != null && current != null) { - runCatching { current.unregisterCallback(callback) } - .onFailure { Log.w(TAG, "unregisterCallback 失败", it) } - } - if (current != null) { - runCatching { current.stop() } - .onFailure { Log.w(TAG, "projection.stop() 失败", it) } - } + handles.forEach { it.release() } } private fun teardown() { @@ -445,7 +485,7 @@ class MediaProjectionService : Service() { beacon?.stop() beacon = null HostBeacon.resetHost() - releaseProjection() + releaseAllProjections() notificationManager?.cancel(REQUEST_NOTIFICATION_ID) } @@ -599,3 +639,20 @@ class MediaProjectionService : Service() { return metrics } } + +/** + * 一张屏幕录制凭证及其停止回调,成对持有、成对释放。 + * + * 一次性凭证用完必须 `stop()`,否则系统侧的投影会话会一直挂着(状态栏投屏图标不消失)。 + */ +private class ProjectionHandle( + val projection: MediaProjection, + val callback: MediaProjection.Callback, +) { + fun release() { + runCatching { projection.unregisterCallback(callback) } + .onFailure { Log.w("MediaProjectionService", "unregisterCallback 失败", it) } + runCatching { projection.stop() } + .onFailure { Log.w("MediaProjectionService", "projection.stop() 失败", it) } + } +}