diff --git a/sentry-android-replay/detekt-baseline.xml b/sentry-android-replay/detekt-baseline.xml
index 19845da6ac..92a7a6b787 100644
--- a/sentry-android-replay/detekt-baseline.xml
+++ b/sentry-android-replay/detekt-baseline.xml
@@ -58,9 +58,9 @@
LongMethod:ReplayIntegrationWithRecorderTest.kt$ReplayIntegrationWithRecorderTest$@Test fun `works with different recorder`()
LongMethod:SimpleVideoEncoder.kt$SimpleVideoEncoder$private fun drainCodec(endOfStream: Boolean)
LongMethod:ViewHierarchyNode.kt$ViewHierarchyNode.Companion$@JvmStatic fun fromView( view: View, parent: ViewHierarchyNode?, distance: Int, options: SentryMaskingOptions, ): ViewHierarchyNode
- LongParameterList:BaseCaptureStrategy.kt$BaseCaptureStrategy$( duration: Long, currentSegmentTimestamp: Date, replayId: SentryId, segmentId: Int, height: Int, width: Int, frameRate: Int, bitRate: Int, replayType: ReplayType = this.replayType, cache: ReplayCache? = this.cache, screenAtStart: String? = this.screenAtStart, breadcrumbs: List<Breadcrumb>? = null, events: Deque<RRWebEvent> = this.currentEvents, )
+ LongParameterList:BaseCaptureStrategy.kt$BaseCaptureStrategy$( duration: Long, currentSegmentTimestamp: Date, replayId: SentryId, segmentId: Int, height: Int, width: Int, frameRate: Int, bitRate: Int, replayType: ReplayType = this.replayType, cache: ReplayCache? = this.cache, screenAtStart: String? = this.screenAtStart, breadcrumbs: List<Breadcrumb>? = null, events: Deque<RRWebEvent> = this.currentEvents, videoFile: File? = null, )
LongParameterList:CaptureStrategy.kt$CaptureStrategy.Companion$( options: SentryOptions, video: File, currentReplayId: SentryId, segmentTimestamp: Date, segmentId: Int, height: Int, width: Int, frameCount: Int, frameRate: Int, videoDuration: Long, replayType: ReplayType, screenAtStart: String?, breadcrumbs: List<Breadcrumb>, events: Deque<RRWebEvent>, traceIds: List<String>, segmentNames: List<String>, )
- LongParameterList:CaptureStrategy.kt$CaptureStrategy.Companion$( scopes: IScopes?, options: SentryOptions, duration: Long, currentSegmentTimestamp: Date, replayId: SentryId, segmentId: Int, height: Int, width: Int, replayType: ReplayType, cache: ReplayCache?, frameRate: Int, bitRate: Int, screenAtStart: String?, breadcrumbs: List<Breadcrumb>?, events: Deque<RRWebEvent>, traceIds: List<String> = emptyList(), segmentNames: List<String> = emptyList(), )
+ LongParameterList:CaptureStrategy.kt$CaptureStrategy.Companion$( scopes: IScopes?, options: SentryOptions, duration: Long, currentSegmentTimestamp: Date, replayId: SentryId, segmentId: Int, height: Int, width: Int, replayType: ReplayType, cache: ReplayCache?, frameRate: Int, bitRate: Int, screenAtStart: String?, breadcrumbs: List<Breadcrumb>?, events: Deque<RRWebEvent>, traceIds: List<String> = emptyList(), segmentNames: List<String> = emptyList(), videoFile: File? = null, )
LongParameterList:ComposeViewHierarchyNode.kt$ComposeViewHierarchyNode$( node: LayoutNode, parent: ViewHierarchyNode?, distance: Int, isComposeRoot: Boolean, options: SentryMaskingOptions, logger: ILogger, )
LongParameterList:PixelCopyStrategy.kt$( destCanvas: Canvas, destPaint: Paint, tmpSrc: Rect, tmpDst: RectF, sourceBitmap: Bitmap, sourceX: Int, sourceY: Int, windowX: Int, windowY: Int, scaleFactorX: Float, scaleFactorY: Float, )
LongParameterList:PixelCopyStrategy.kt$PixelCopyStrategy$( root: View, captures: Array<SurfaceViewCapture?>, viewHierarchy: ViewHierarchyNode, windowX: Int, windowY: Int, resetUnstableCaptures: Boolean, )
diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BaseCaptureStrategy.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BaseCaptureStrategy.kt
index 4330f2e739..46b14d1268 100644
--- a/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BaseCaptureStrategy.kt
+++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BaseCaptureStrategy.kt
@@ -216,6 +216,7 @@ constructor(
screenAtStart: String? = this.screenAtStart,
breadcrumbs: List? = null,
events: Deque = this.currentEvents,
+ videoFile: File? = null,
): ReplaySegment {
val (traceIds, segmentNames) =
synchronized(replayContextLock) {
@@ -242,6 +243,7 @@ constructor(
events,
traceIds,
segmentNames,
+ videoFile,
)
}
diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BufferCaptureStrategy.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BufferCaptureStrategy.kt
index 487f0a5f6a..5713e375ae 100644
--- a/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BufferCaptureStrategy.kt
+++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BufferCaptureStrategy.kt
@@ -322,6 +322,9 @@ constructor(
currentConfig.recordingWidth,
currentConfig.frameRate,
currentConfig.bitRate,
+ // buffered segments get renumbered on rotation, so their filenames must not depend on
+ // segmentId
+ videoFile = File(replayCacheDir, "${SentryId()}.mp4"),
)
onSegmentCreated(segment)
},
diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/CaptureStrategy.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/CaptureStrategy.kt
index fc8b8d1306..05f23e9434 100644
--- a/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/CaptureStrategy.kt
+++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/capture/CaptureStrategy.kt
@@ -98,6 +98,7 @@ internal interface CaptureStrategy {
events: Deque,
traceIds: List = emptyList(),
segmentNames: List = emptyList(),
+ videoFile: File? = null,
): ReplaySegment {
val generatedVideo =
cache?.createVideoOf(
@@ -108,6 +109,7 @@ internal interface CaptureStrategy {
width,
frameRate,
bitRate,
+ videoFile ?: File(cache.replayCacheDir, "$segmentId.mp4"),
) ?: return ReplaySegment.Failed
val (video, frameCount, videoDuration) = generatedVideo
diff --git a/sentry-android-replay/src/test/java/io/sentry/android/replay/ReplaySmokeTest.kt b/sentry-android-replay/src/test/java/io/sentry/android/replay/ReplaySmokeTest.kt
index c7a368b06c..031d05f312 100644
--- a/sentry-android-replay/src/test/java/io/sentry/android/replay/ReplaySmokeTest.kt
+++ b/sentry-android-replay/src/test/java/io/sentry/android/replay/ReplaySmokeTest.kt
@@ -206,7 +206,7 @@ class ReplaySmokeTest {
check {
assertEquals(replay.replayId, it.replayId)
assertEquals(ReplayType.BUFFER, it.replayType)
- assertEquals("0.mp4", it.videoFile?.name)
+ assertEquals("mp4", it.videoFile?.extension)
assertEquals("replay_${replay.replayId}", it.videoFile?.parentFile?.name)
},
check {
diff --git a/sentry-android-replay/src/test/java/io/sentry/android/replay/capture/BufferCaptureStrategyTest.kt b/sentry-android-replay/src/test/java/io/sentry/android/replay/capture/BufferCaptureStrategyTest.kt
index a52b197798..2b4fd072dc 100644
--- a/sentry-android-replay/src/test/java/io/sentry/android/replay/capture/BufferCaptureStrategyTest.kt
+++ b/sentry-android-replay/src/test/java/io/sentry/android/replay/capture/BufferCaptureStrategyTest.kt
@@ -41,6 +41,7 @@ import java.util.concurrent.TimeUnit.MILLISECONDS
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
+import kotlin.test.assertNotEquals
import kotlin.test.assertTrue
import org.awaitility.kotlin.await
import org.junit.Rule
@@ -557,6 +558,71 @@ class BufferCaptureStrategyTest {
)
}
+ @Test
+ fun `buffered segment keeps its own video after earlier segment expires`() {
+ val replayId = SentryId()
+ val replayCacheDir = tmpDir.newFolder("replay_$replayId")
+ val strategy = fixture.getSut(replayCacheDir = replayCacheDir)
+ strategy.start(0, replayId)
+ strategy.onConfigurationChanged(fixture.recorderConfig)
+
+ var firstFrameTimestamp = fixture.now
+ whenever(fixture.replayCache.firstFrameTimestamp()).thenAnswer { firstFrameTimestamp }
+
+ var encodedCount = 0
+ val createdVideos = mutableListOf()
+ whenever(
+ fixture.replayCache.createVideoOf(
+ anyLong(),
+ anyLong(),
+ anyInt(),
+ anyInt(),
+ anyInt(),
+ anyInt(),
+ anyInt(),
+ any(),
+ ),
+ )
+ .thenAnswer { invocation ->
+ val videoFile = invocation.arguments[7] as File
+ videoFile.writeText("video-${encodedCount++}")
+ createdVideos += videoFile
+ GeneratedVideo(videoFile, 5, VIDEO_DURATION)
+ }
+
+ val capturedReplays = mutableListOf()
+ whenever(fixture.scopes.captureReplay(any(), any())).thenAnswer { invocation ->
+ capturedReplays += invocation.arguments[0] as SentryReplayEvent
+ SentryId()
+ }
+
+ // Buffer segment 0 on pause
+ strategy.pause()
+
+ // Buffer segment 1 on configuration change; currentSegment becomes 2
+ fixture.elapse(9_000L)
+ firstFrameTimestamp = fixture.now
+ strategy.onConfigurationChanged(fixture.recorderConfig)
+
+ // Expire segment 0 (bufferLimit = now - 30_000 > segment0 end);
+ // segment 1 is renumbered to 0 and currentSegment drops back to 1
+ fixture.elapse(30_000L)
+ firstFrameTimestamp = fixture.now
+ strategy.onScreenshotRecorded {}
+
+ // Error triggers captureReplay: current segment encodes at segmentId 1
+ fixture.elapse(1_000L)
+ strategy.captureReplay(false) {}
+
+ assertFalse(createdVideos[0].exists())
+ assertEquals(2, capturedReplays.size)
+ assertEquals(0, capturedReplays[0].segmentId)
+ assertEquals(1, capturedReplays[1].segmentId)
+ assertNotEquals(capturedReplays[0].videoFile, capturedReplays[1].videoFile)
+ assertEquals("video-1", capturedReplays[0].videoFile!!.readText())
+ assertEquals("video-2", capturedReplays[1].videoFile!!.readText())
+ }
+
/** The recording payload of every captured segment, in capture order, with its segment id. */
private fun capturedSegments(count: Int): List>> {
val events = argumentCaptor()