-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSessionCaptureStrategy.kt
More file actions
183 lines (164 loc) · 6.02 KB
/
SessionCaptureStrategy.kt
File metadata and controls
183 lines (164 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package io.sentry.android.replay.capture
import android.graphics.Bitmap
import io.sentry.IScopes
import io.sentry.SentryLevel.DEBUG
import io.sentry.SentryLevel.INFO
import io.sentry.SentryOptions
import io.sentry.SentryReplayEvent.ReplayType
import io.sentry.android.replay.ReplayCache
import io.sentry.android.replay.ScreenshotRecorderConfig
import io.sentry.android.replay.capture.CaptureStrategy.ReplaySegment
import io.sentry.android.replay.util.submitSafely
import io.sentry.protocol.SentryId
import io.sentry.transport.ICurrentDateProvider
import io.sentry.util.FileUtils
import java.util.Date
import java.util.concurrent.ScheduledExecutorService
internal class SessionCaptureStrategy(
private val options: SentryOptions,
private val scopes: IScopes?,
private val dateProvider: ICurrentDateProvider,
executor: ScheduledExecutorService,
replayCacheProvider: ((replayId: SentryId) -> ReplayCache)? = null,
) : BaseCaptureStrategy(options, scopes, dateProvider, executor, replayCacheProvider) {
internal companion object {
private const val TAG = "SessionCaptureStrategy"
}
override fun start(segmentId: Int, replayId: SentryId, replayType: ReplayType?) {
super.start(segmentId, replayId, replayType)
// only set replayId on the scope if it's a full session, otherwise all events will be
// tagged with the replay that might never be sent when we're recording in buffer mode
scopes?.configureScope {
it.replayId = currentReplayId
it.replayType = this.replayType
screenAtStart = it.screen?.substringAfterLast('.')
}
}
override fun pause() {
createCurrentSegment("pause") { segment ->
if (segment is ReplaySegment.Created) {
segment.capture(scopes)
currentSegment++
}
}
super.pause()
}
override fun stop() {
val replayCacheDir = cache?.replayCacheDir
createCurrentSegment("stop") { segment ->
if (segment is ReplaySegment.Created) {
segment.capture(scopes)
}
currentSegment = -1
FileUtils.deleteRecursively(replayCacheDir)
}
scopes?.configureScope {
it.replayId = SentryId.EMPTY_ID
it.replayType = null
}
super.stop()
}
override fun captureReplay(isTerminating: Boolean, onSegmentSent: (Date) -> Unit) {
if (options.sessionReplay.isDebug) {
options.logger.log(
DEBUG,
"Replay is already running in 'session' mode, not capturing for event",
)
}
this.isTerminating.set(isTerminating)
}
override fun onScreenshotRecorded(
bitmap: Bitmap?,
store: ReplayCache.(frameTimestamp: Long) -> Unit,
) {
// have to do it before submitting, otherwise if the queue is busy, the timestamp won't be
// reflecting the exact time of when it was captured
val currentConfig = recorderConfig
val frameTimestamp = dateProvider.currentTimeMillis
replayExecutor.submitSafely(options, "$TAG.add_frame") {
cache?.store(frameTimestamp)
val currentSegmentTimestamp = segmentTimestamp
currentSegmentTimestamp
?: run {
options.logger.log(DEBUG, "Segment timestamp is not set, not recording frame")
return@submitSafely
}
if (isTerminating.get()) {
options.logger.log(
DEBUG,
"Not capturing segment, because the app is terminating, will be captured on next launch",
)
return@submitSafely
}
if (currentConfig == null) {
options.logger.log(DEBUG, "Recorder config is not set, not capturing a segment")
return@submitSafely
}
val now = dateProvider.currentTimeMillis
if ((now - currentSegmentTimestamp.time >= options.sessionReplay.sessionSegmentDuration)) {
val segment =
createSegmentInternal(
options.sessionReplay.sessionSegmentDuration,
currentSegmentTimestamp,
currentReplayId,
currentSegment,
currentConfig.recordingHeight,
currentConfig.recordingWidth,
currentConfig.frameRate,
currentConfig.bitRate,
)
if (segment is ReplaySegment.Created) {
segment.capture(scopes)
currentSegment++
// set next segment timestamp as close to the previous one as possible to avoid gaps
segmentTimestamp = segment.replay.timestamp
}
}
if ((now - replayStartTimestamp.get() >= options.sessionReplay.sessionDuration)) {
options.replayController.stop()
options.logger.log(INFO, "Session replay deadline exceeded (1h), stopping recording")
}
}
}
override fun onConfigurationChanged(recorderConfig: ScreenshotRecorderConfig) {
createCurrentSegment("onConfigurationChanged") { segment ->
if (segment is ReplaySegment.Created) {
segment.capture(scopes)
currentSegment++
// set next segment timestamp as close to the previous one as possible to avoid gaps
segmentTimestamp = segment.replay.timestamp
}
}
// refresh recorder config after submitting the last segment with current config
super.onConfigurationChanged(recorderConfig)
}
override fun convert(): CaptureStrategy = this
private fun createCurrentSegment(taskName: String, onSegmentCreated: (ReplaySegment) -> Unit) {
val currentConfig = recorderConfig
if (currentConfig == null) {
options.logger.log(
DEBUG,
"Recorder config is not set, not creating segment for task: $taskName",
)
return
}
val now = dateProvider.currentTimeMillis
val currentSegmentTimestamp = segmentTimestamp ?: return
val duration = now - currentSegmentTimestamp.time
val replayId = currentReplayId
replayExecutor.submitSafely(options, "$TAG.$taskName") {
val segment =
createSegmentInternal(
duration,
currentSegmentTimestamp,
replayId,
currentSegment,
currentConfig.recordingHeight,
currentConfig.recordingWidth,
currentConfig.frameRate,
currentConfig.bitRate,
)
onSegmentCreated(segment)
}
}
}