@@ -16,26 +16,29 @@ import kotlinx.coroutines.launch
1616import kotlinx.coroutines.runBlocking
1717import kotlinx.coroutines.withTimeoutOrNull
1818
19- data class FrameSample (val timestamp : Long , val path : String )
20-
2119/* *
2220 * 用户录制期间的截图采样器。
2321 *
2422 * 以固定间隔抓帧并保存到 [RecordingWriter],同时维护最近若干帧的时间戳索引。
2523 * 用户动作发生时用 [frameBefore] 取动作前最近一帧,作为 `beforeFrame` 写入事件。
24+ *
25+ * 页面切换后调用 [invalidateSamples]:旧页面的样本不再参与 `beforeFrame`,
26+ * 避免把新页面的动作标成上一个页面的截图。
2627 */
2728class RecordingCaptureLoop (
2829 private val writer : RecordingWriter ,
2930 private val intervalMs : Long ,
3031 private val logBus : LogBus ,
31- private val captureProvider : () -> Bitmap ? ,
32+ /* * [fresh] 为 true 时要求后端等待一帧比调用时刻更新的画面,避免拿到缓存的旧页。 */
33+ private val captureProvider : (fresh: Boolean ) -> Bitmap ? ,
3234 private val onCaptureFailure : () -> Unit = {},
3335) {
3436
3537 private val scope = CoroutineScope (SupervisorJob () + Dispatchers .IO )
36- private val lock = Any ()
38+ private val samples = FrameSelector (
39+ maxAgeMs = maxOf(intervalMs * 2 , FrameSelector .DEFAULT_MAX_AGE_MS ),
40+ )
3741 private val afterLock = Any ()
38- private val samples = ArrayDeque <FrameSample >()
3942 private val afterJobs = mutableSetOf<Job >()
4043 private var job: Job ? = null
4144
@@ -58,10 +61,10 @@ class RecordingCaptureLoop(
5861 acceptingAfter = true
5962 job = scope.launch {
6063 // 先抓一帧,保证第一下点击也有可用的“动作前”画面。
61- captureOnce()
64+ captureOnce(fresh = false )
6265 while (isActive && ! stopped) {
6366 delay(intervalMs.coerceAtLeast(MIN_INTERVAL_MS ))
64- captureOnce()
67+ captureOnce(fresh = false )
6568 }
6669 }
6770 }
@@ -88,33 +91,31 @@ class RecordingCaptureLoop(
8891 stopped = true
8992 scope.cancel()
9093 synchronized(afterLock) { afterJobs.clear() }
91- synchronized(lock) { samples.clear() }
94+ samples.clear()
9295 }
9396
9497 /* *
9598 * 返回动作时间点之前最近的一帧。
9699 *
97- * 采样队列只保留最近 [MAX_SAMPLES] 帧。 找不到动作前的帧时返回 null,
98- * 不再用动作之后的画面冒充 beforeFrame。
100+ * 采样队列只保留最近若干帧,且页面切换后会被清空; 找不到动作前的帧时返回 null,
101+ * 不再用动作之后或上一个页面的画面冒充 beforeFrame。
99102 */
100- fun frameBefore (timestamp : Long ): String? = synchronized(lock) {
101- samples.lastOrNull { it.timestamp <= timestamp }?.path
103+ fun frameBefore (timestamp : Long ): String? = samples.before(timestamp)?.path
104+
105+ /* * 页面切换后调用:丢弃旧页面样本,并要求下一帧强制写盘。 */
106+ fun invalidateSamples () {
107+ samples.invalidate()
108+ writer.requestFreshFrame()
102109 }
103110
104111 /* * 动作后补一帧,捕捉页面跳转后的画面;[actionSeq] 用于把帧回填到动作。 */
105112 fun scheduleAfter (timestamp : Long , actionSeq : Long? = null) {
106113 if (stopped || ! acceptingAfter) return
107- if (writer.frameCapReached()) {
108- writer.markFrameCapReached()
109- return
110- }
111114 val job = scope.launch {
112115 delay(AFTER_DELAY_MS )
113116 // 如果采样线程已经抓到了足够晚的帧,直接复用,避免重复截图。
114- val existing = synchronized(lock) {
115- samples.lastOrNull { it.timestamp >= timestamp + AFTER_DELAY_MS }
116- }
117- val sample = existing ? : captureOnce() ? : return @launch
117+ val existing = samples.atOrAfter(timestamp + AFTER_DELAY_MS )
118+ val sample = existing ? : captureOnce(fresh = true ) ? : return @launch
118119 if (actionSeq != null && actionSeq > 0L ) {
119120 writer.record(
120121 ContextEvent (
@@ -131,15 +132,22 @@ class RecordingCaptureLoop(
131132 }
132133 }
133134
134- private fun captureOnce (): FrameSample ? {
135+ private fun captureOnce (fresh : Boolean ): FrameSample ? {
135136 if (stopped) return null
137+ val startedWall = System .currentTimeMillis()
138+ // 到帧上限后继续写 stale screencap 事件,让时间线保持连续;但不再把旧帧
139+ // 计入样本索引,否则后续动作会被标成上限之前的页面。
136140 if (writer.frameCapReached()) {
137- writer.markFrameCapReached()
141+ // 上限之后没有可信的新样本:清掉旧索引,避免后续动作继续挂上限前的页面。
142+ samples.invalidate()
143+ writer.recordCappedFrame(
144+ atMs = writer.timestampOf(startedWall),
145+ source = RecordSource .USER ,
146+ )
138147 return null
139148 }
140- val startedWall = System .currentTimeMillis()
141149 val bitmap = runCatching {
142- OverlayCoordinator .withoutOverlay { captureProvider() }
150+ OverlayCoordinator .withoutOverlay { captureProvider(fresh ) }
143151 }.getOrElse {
144152 noteCaptureFailure(it.message)
145153 return null
@@ -159,12 +167,7 @@ class RecordingCaptureLoop(
159167 if (path != null ) {
160168 failureSinceUptime = 0L
161169 failureReported = false
162- val sample = FrameSample (timestamp, path)
163- synchronized(lock) {
164- samples.addLast(sample)
165- while (samples.size > MAX_SAMPLES ) samples.removeFirst()
166- }
167- return sample
170+ return samples.add(timestamp, path)
168171 }
169172 return null
170173 } finally {
@@ -187,8 +190,7 @@ class RecordingCaptureLoop(
187190 private companion object {
188191 const val MIN_INTERVAL_MS = 250L
189192 const val AFTER_DELAY_MS = 220L
190- const val MAX_SAMPLES = 12
191- const val STOP_GRACE_MS = 350L
193+ const val STOP_GRACE_MS = 1_000L
192194 const val FAILURE_REPORT_GRACE_MS = 5_000L
193195 }
194196}
0 commit comments