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
+92
-35
@@ -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) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user