feat(receiver): 处理 msgType=9 会话配置帧
- ControlFrames 新增 SessionConfig 事件(payload 为 UTF-8 JSON) - ReceiverTransport 分发配置到 ReceiverSession,并按 clipboardSync 丢弃剪贴板内容 - ReceiverSession 用会话配置覆盖构造默认值:preferH265 / bitrateMbps / audioEnabled / clipboardSync - 投屏中改编码格式或码率会重建采集链路;软编降档阈值随编码格式重新计算 - audioEnabled=false 时不建 AudioPlaybackCapture,clipboardSync=false 时不再上行本机剪贴板
This commit is contained in:
1 parent
4c0eba9717
commit
59b3cefb97
4 files changed
+105
-10
No files matched your search
@@ -24,6 +24,7 @@ import com.ttstd.lancast.receiver.file.FileService
|
||||
import com.ttstd.lancast.receiver.input.InputInjector
|
||||
import com.ttstd.lancast.receiver.protocol.ControlEvent
|
||||
import com.ttstd.lancast.receiver.session.ReceiverTransport
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.Executors
|
||||
@@ -76,9 +77,23 @@ class ReceiverSession(
|
||||
var onStreamingLost: (() -> Unit)? = null
|
||||
|
||||
private val encoder = VideoEncoder { buffer, info ->
|
||||
transport?.sendVideo(buffer, info, preferH265)
|
||||
transport?.sendVideo(buffer, info, useH265)
|
||||
}
|
||||
|
||||
// 会话配置:可由主控端的 msgType=9 会话配置帧在线覆盖(构造参数只是默认值)。
|
||||
@Volatile
|
||||
private var useH265: Boolean = preferH265
|
||||
@Volatile
|
||||
private var targetBitrateMbps: Int = bitrateMbps
|
||||
@Volatile
|
||||
private var audioSyncEnabled: Boolean = true
|
||||
@Volatile
|
||||
private var clipboardSyncEnabled: Boolean = true
|
||||
|
||||
/** 编码长边上限:随编码格式变化(软编降一档)。 */
|
||||
@Volatile
|
||||
private var maxLongSide: Int = MAX_LONG_SIDE
|
||||
|
||||
private var capturer: ScreenCapturer? = null
|
||||
private var audio: AudioCapturer? = null
|
||||
private var announcer: ReceiverAnnouncer? = null
|
||||
@@ -112,9 +127,12 @@ class ReceiverSession(
|
||||
fun start() {
|
||||
if (running) return
|
||||
running = true
|
||||
maxLongSide = computeMaxLongSide()
|
||||
displayManager?.registerDisplayListener(displayListener, mainHandler)
|
||||
opusEncoder.start()
|
||||
clipboard.start { text -> transport?.sendControl(ControlEvent.Clipboard(text)) }
|
||||
clipboard.start { text ->
|
||||
if (clipboardSyncEnabled) transport?.sendControl(ControlEvent.Clipboard(text))
|
||||
}
|
||||
startPublishing()
|
||||
executor.execute {
|
||||
while (running) {
|
||||
@@ -183,7 +201,7 @@ class ReceiverSession(
|
||||
val screen = realScreenSize()
|
||||
injector.updateGeometry(captureWidth, captureHeight, screen[0], screen[1])
|
||||
|
||||
encoder.start(captureWidth, captureHeight, 30, bitrateMbps * 1_000_000, preferH265)
|
||||
encoder.start(captureWidth, captureHeight, 30, targetBitrateMbps * 1_000_000, useH265)
|
||||
val surface = encoder.inputSurface
|
||||
?: throw IllegalStateException("视频编码器未返回输入 Surface")
|
||||
// projection 为 null 时由 DisplayManager 直采(系统签名 + CAPTURE_VIDEO_OUTPUT)。
|
||||
@@ -193,7 +211,9 @@ class ReceiverSession(
|
||||
|
||||
// 音频采集(AudioPlaybackCapture)强依赖 MediaProjection;免授权直采时只投画面。
|
||||
val active = projection
|
||||
if (active != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
if (!audioSyncEnabled) {
|
||||
Log.i(TAG, "主控端关闭了音频同步:不建音频采集")
|
||||
} else if (active != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
audio = AudioCapturer(active) { pcm ->
|
||||
val transport = transport ?: return@AudioCapturer
|
||||
opusEncoder.encode(pcm).forEach { packet ->
|
||||
@@ -285,6 +305,7 @@ class ReceiverSession(
|
||||
fileService,
|
||||
)
|
||||
created.onMirrorEvent = { action, message -> onMirrorRequest?.invoke(action, message) }
|
||||
created.onSessionConfig = { json -> applySessionConfig(json) }
|
||||
transport = created
|
||||
created.start()
|
||||
return
|
||||
@@ -298,12 +319,61 @@ class ReceiverSession(
|
||||
}
|
||||
|
||||
/** 长边上限:软编(模拟器/无硬编)降一档,否则编码器跟不上、画面一直卡。 */
|
||||
private val maxLongSide: Int by lazy {
|
||||
if (VideoEncoder.isSoftwareEncoder(preferH265)) {
|
||||
private fun computeMaxLongSide(): Int {
|
||||
if (VideoEncoder.isSoftwareEncoder(useH265)) {
|
||||
Log.i(TAG, "只有软件编码器:长边限制 $MAX_LONG_SIDE_SOFTWARE(否则编不动会掉帧)")
|
||||
MAX_LONG_SIDE_SOFTWARE
|
||||
} else {
|
||||
MAX_LONG_SIDE
|
||||
return MAX_LONG_SIDE_SOFTWARE
|
||||
}
|
||||
return MAX_LONG_SIDE
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用主控端下发的会话配置(msgType=9,UTF-8 JSON)。
|
||||
*
|
||||
* 字段全部可选,缺省则沿用当前值;投屏中改编码格式或码率会重建采集链路,
|
||||
* 音频/剪贴板开关下一帧起生效。
|
||||
*/
|
||||
private fun applySessionConfig(json: String) {
|
||||
val config = try {
|
||||
JSONObject(json)
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "会话配置不是合法 JSON,忽略: ${error.message}")
|
||||
return
|
||||
}
|
||||
val nextH265 = config.optBoolean("preferH265", useH265)
|
||||
val nextBitrate = config.optInt("bitrateMbps", targetBitrateMbps).coerceIn(1, 50)
|
||||
val nextAudio = config.optBoolean("audioEnabled", audioSyncEnabled)
|
||||
val nextClipboard = config.optBoolean("clipboardSync", clipboardSyncEnabled)
|
||||
|
||||
val rebuildNeeded = mirroring && (nextH265 != useH265 || nextBitrate != targetBitrateMbps)
|
||||
useH265 = nextH265
|
||||
targetBitrateMbps = nextBitrate
|
||||
audioSyncEnabled = nextAudio
|
||||
clipboardSyncEnabled = nextClipboard
|
||||
maxLongSide = computeMaxLongSide()
|
||||
transport?.clipboardSyncEnabled = clipboardSyncEnabled
|
||||
Log.i(
|
||||
TAG,
|
||||
"应用会话配置 h265=$useH265 bitrate=${targetBitrateMbps}Mbps " +
|
||||
"audio=$audioSyncEnabled clipboard=$clipboardSyncEnabled",
|
||||
)
|
||||
|
||||
if (rebuildNeeded) {
|
||||
executor.execute {
|
||||
synchronized(captureLock) {
|
||||
if (!mirroring) return@execute
|
||||
Log.i(TAG, "会话配置变化,重建采集")
|
||||
releaseCapture()
|
||||
try {
|
||||
startCapture()
|
||||
} catch (error: Exception) {
|
||||
Log.e(TAG, "按会话配置重建采集失败", error)
|
||||
mirroring = false
|
||||
releaseCapture()
|
||||
onStreamingLost?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -81,6 +81,8 @@ class InputInjector(private val context: Context) {
|
||||
is ControlEvent.Ping, is ControlEvent.Pong -> Unit
|
||||
// 投屏请求由 Service 处理,不参与输入注入。
|
||||
is ControlEvent.Mirror -> Unit
|
||||
// 会话配置由 ReceiverSession 处理。
|
||||
is ControlEvent.SessionConfig -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
@@ -17,6 +17,15 @@ sealed class ControlEvent {
|
||||
* payload = `[action:u8]` + 可选 UTF-8 文本(用于 ERROR/DENIED 的原因说明)。
|
||||
*/
|
||||
data class Mirror(val action: Int, val message: String = "") : ControlEvent()
|
||||
|
||||
/**
|
||||
* 会话配置(msgType = 9)。
|
||||
*
|
||||
* payload 是 UTF-8 JSON,字段全部可选:
|
||||
* `preferH265` / `bitrateMbps` / `audioEnabled` / `clipboardSync`。
|
||||
* 主控端在请求投屏之前下发,被控端据此决定编码格式、码率与音频/剪贴板开关。
|
||||
*/
|
||||
data class SessionConfig(val json: String) : ControlEvent()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,6 +124,7 @@ object ControlFrames {
|
||||
}
|
||||
ControlEvent.Mirror(action, message)
|
||||
}
|
||||
9 -> ControlEvent.SessionConfig(String(payload, StandardCharsets.UTF_8))
|
||||
else -> return null
|
||||
}
|
||||
return ControlFrame(version, sequence, event)
|
||||
@@ -142,6 +152,7 @@ object ControlFrames {
|
||||
} else {
|
||||
byteArrayOf(event.action.toByte()) + event.message.toByteArray(StandardCharsets.UTF_8)
|
||||
}
|
||||
is ControlEvent.SessionConfig -> event.json.toByteArray(StandardCharsets.UTF_8)
|
||||
}
|
||||
return byteArrayOf(VERSION.toByte(), msgType(event).toByte()) +
|
||||
u16Bytes(sequence) + u32Bytes(payload.size) + payload
|
||||
@@ -156,6 +167,7 @@ object ControlFrames {
|
||||
is ControlEvent.Ping -> 5
|
||||
is ControlEvent.Pong -> 6
|
||||
is ControlEvent.Mirror -> 8
|
||||
is ControlEvent.SessionConfig -> 9
|
||||
}
|
||||
|
||||
private fun u16(bytes: ByteArray, offset: Int) =
|
||||
|
||||
+12
-1
@@ -76,6 +76,14 @@ class ReceiverTransport(
|
||||
@Volatile
|
||||
var onMirrorEvent: ((Int, String) -> Unit)? = null
|
||||
|
||||
/** 收到主控端会话配置(msgType=9,UTF-8 JSON)时的回调。 */
|
||||
@Volatile
|
||||
var onSessionConfig: ((String) -> Unit)? = null
|
||||
|
||||
/** 主控端是否开启了剪贴板同步;关闭时收到上行/下发剪贴板都直接丢弃。 */
|
||||
@Volatile
|
||||
var clipboardSyncEnabled: Boolean = true
|
||||
|
||||
/** 回送投屏应答(GRANTED / DENIED / STOPPED / ERROR)。 */
|
||||
fun sendMirror(action: Int, message: String = "") {
|
||||
sendControl(ControlEvent.Mirror(action, message))
|
||||
@@ -288,10 +296,13 @@ class ReceiverTransport(
|
||||
else -> {
|
||||
val frame = ControlFrames.decode(plaintext) ?: continue
|
||||
when (val event = frame.event) {
|
||||
is ControlEvent.Clipboard -> clipboard.publish(event.text)
|
||||
is ControlEvent.Clipboard -> if (clipboardSyncEnabled) {
|
||||
clipboard.publish(event.text)
|
||||
}
|
||||
is ControlEvent.Ping -> sendControl(ControlEvent.Pong(event.timestamp))
|
||||
is ControlEvent.Pong -> Unit
|
||||
is ControlEvent.Mirror -> onMirrorEvent?.invoke(event.action, event.message)
|
||||
is ControlEvent.SessionConfig -> onSessionConfig?.invoke(event.json)
|
||||
else -> injector.inject(frame)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user