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