fix: 移除服务端黑帧自动切换无障碍截图模式逻辑
- 连续黑帧不再发送captureMode:accessibility指令给安卓端 - 黑帧计数仅用于日志统计,不触发模式切换 - 避免服务端误判导致安卓端回退到低帧率无障碍截图
This commit is contained in:
BIN
devices.db
BIN
devices.db
Binary file not shown.
17
src/index.ts
17
src/index.ts
@@ -1786,17 +1786,14 @@ class RemoteControlServer {
|
|||||||
}, 5000) // 5s grace period for reconnect
|
}, 5000) // 5s grace period for reconnect
|
||||||
|
|
||||||
} else if (socket.clientType === 'web' && socket.clientId) {
|
} else if (socket.clientType === 'web' && socket.clientId) {
|
||||||
// 🔧 优化Web客户端断开处理
|
|
||||||
const clientId = socket.clientId
|
const clientId = socket.clientId
|
||||||
const client = this.webClientManager.getClient(clientId)
|
const client = this.webClientManager.getClient(clientId)
|
||||||
|
|
||||||
if (client) {
|
if (client) {
|
||||||
// 如果客户端正在控制设备,释放控制权
|
|
||||||
if (client.controllingDeviceId) {
|
if (client.controllingDeviceId) {
|
||||||
this.logger.info(`🔓 Web客户端断开,释放设备控制权: ${client.controllingDeviceId}`)
|
this.logger.info(`[Disconnect] Web client disconnected, releasing device control: ${client.controllingDeviceId}`)
|
||||||
this.webClientManager.releaseDeviceControl(client.controllingDeviceId)
|
this.webClientManager.releaseDeviceControl(client.controllingDeviceId)
|
||||||
|
|
||||||
// 通知设备控制者已离开
|
|
||||||
const deviceSocketId = this.deviceManager.getDeviceSocketId(client.controllingDeviceId)
|
const deviceSocketId = this.deviceManager.getDeviceSocketId(client.controllingDeviceId)
|
||||||
if (deviceSocketId) {
|
if (deviceSocketId) {
|
||||||
const deviceSocket = this.io.sockets.sockets.get(deviceSocketId)
|
const deviceSocket = this.io.sockets.sockets.get(deviceSocketId)
|
||||||
@@ -1807,19 +1804,15 @@ class RemoteControlServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.webClientManager.removeClient(clientId)
|
this.webClientManager.removeClient(clientId)
|
||||||
this.logger.info(`Web客户端断开连接: ${clientId} (IP: ${client.ip})`)
|
this.logger.info(`[Disconnect] Web client removed: ${clientId} (IP: ${client.ip})`)
|
||||||
} else {
|
} else {
|
||||||
// 通过Socket ID移除客户端
|
|
||||||
this.webClientManager.removeClientBySocketId(socket.id)
|
this.webClientManager.removeClientBySocketId(socket.id)
|
||||||
this.logger.info(`Web客户端断开连接 (通过Socket ID): ${socket.id}`)
|
this.logger.info(`[Disconnect] Web client removed by socketId: ${socket.id}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.info(`📊 当前Web客户端数量: ${this.webClientManager.getClientCount()}`)
|
this.logger.info(`[Disconnect] Web client count: ${this.webClientManager.getClientCount()}`)
|
||||||
} else {
|
} else {
|
||||||
// 🔧 处理未识别的连接类型
|
this.logger.warn(`[Disconnect] Unknown client type: ${socket.id} (type: ${socket.clientType})`)
|
||||||
this.logger.warn(`⚠️ 未识别的连接断开: ${socket.id} (类型: ${socket.clientType})`)
|
|
||||||
|
|
||||||
// 尝试清理可能存在的记录
|
|
||||||
this.webClientManager.removeClientBySocketId(socket.id)
|
this.webClientManager.removeClientBySocketId(socket.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -546,52 +546,21 @@ export class MessageRouter {
|
|||||||
if (dataSize > 0 && dataSize < MIN_VALID_FRAME_SIZE) {
|
if (dataSize > 0 && dataSize < MIN_VALID_FRAME_SIZE) {
|
||||||
this.droppedFrames++
|
this.droppedFrames++
|
||||||
|
|
||||||
// ✅ 追踪连续黑帧数
|
// 追踪连续黑帧数(仅记录日志,不触发模式切换)
|
||||||
const deviceId = screenData.deviceId
|
const deviceId = screenData.deviceId
|
||||||
const count = (this.consecutiveBlackFrames.get(deviceId) || 0) + 1
|
const count = (this.consecutiveBlackFrames.get(deviceId) || 0) + 1
|
||||||
this.consecutiveBlackFrames.set(deviceId, count)
|
this.consecutiveBlackFrames.set(deviceId, count)
|
||||||
|
|
||||||
if (this.routedFrames % 100 === 0) {
|
if (count % 100 === 1) {
|
||||||
this.logger.warn(`⚠️ 过滤黑屏帧: ${dataSize} 字符 < ${MIN_VALID_FRAME_SIZE}, 设备${deviceId}, 连续黑帧${count}, 已丢弃${this.droppedFrames}帧`)
|
this.logger.warn(`过滤黑屏帧: ${dataSize} 字符 < ${MIN_VALID_FRAME_SIZE}, 设备${deviceId}, 连续黑帧${count}, 已丢弃${this.droppedFrames}帧`)
|
||||||
}
|
|
||||||
|
|
||||||
// ✅ 连续50个黑帧后,通知Android端切换到无障碍截图模式
|
|
||||||
if (count >= 50 && !this.captureModeSwitchSent.has(deviceId)) {
|
|
||||||
this.captureModeSwitchSent.add(deviceId)
|
|
||||||
this.logger.warn(`🔄 设备${deviceId}连续${count}个黑帧,发送切换到无障碍截图模式指令`)
|
|
||||||
|
|
||||||
try {
|
|
||||||
const deviceSocketId = this.deviceManager.getDeviceSocketId(deviceId)
|
|
||||||
if (deviceSocketId) {
|
|
||||||
const deviceSocket = this.webClientManager.io?.sockets.sockets.get(deviceSocketId)
|
|
||||||
if (deviceSocket) {
|
|
||||||
deviceSocket.emit('quality_adjust', {
|
|
||||||
captureMode: 'accessibility',
|
|
||||||
fps: 10,
|
|
||||||
quality: 50,
|
|
||||||
maxWidth: 480,
|
|
||||||
maxHeight: 854
|
|
||||||
})
|
|
||||||
this.logger.info(`📤 已向设备${deviceId}发送切换采集模式指令`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
this.logger.error(`❌ 发送切换采集模式指令失败:`, e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ 收到有效帧,重置黑帧计数
|
// 收到有效帧,重置黑帧计数
|
||||||
if (screenData.deviceId) {
|
if (screenData.deviceId) {
|
||||||
const prevCount = this.consecutiveBlackFrames.get(screenData.deviceId) || 0
|
|
||||||
if (prevCount > 0) {
|
|
||||||
this.logger.info(`✅ 设备${screenData.deviceId}收到有效帧(${dataSize}字符),重置黑帧计数(之前${prevCount})`)
|
|
||||||
}
|
|
||||||
this.consecutiveBlackFrames.set(screenData.deviceId, 0)
|
this.consecutiveBlackFrames.set(screenData.deviceId, 0)
|
||||||
// 收到有效帧后允许再次发送切换指令(如果后续又出现黑帧)
|
|
||||||
this.captureModeSwitchSent.delete(screenData.deviceId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔧 检查设备是否有控制者,没有控制者直接丢弃(提前检查,减少处理开销)
|
// 🔧 检查设备是否有控制者,没有控制者直接丢弃(提前检查,减少处理开销)
|
||||||
|
|||||||
Reference in New Issue
Block a user