-
-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathBaseCaptureStrategy.kt
More file actions
217 lines (195 loc) · 7.9 KB
/
BaseCaptureStrategy.kt
File metadata and controls
217 lines (195 loc) · 7.9 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package io.sentry.android.replay.capture
import android.annotation.TargetApi
import android.view.MotionEvent
import io.sentry.Breadcrumb
import io.sentry.DateUtils
import io.sentry.IScopes
import io.sentry.SentryLevel.ERROR
import io.sentry.SentryOptions
import io.sentry.SentryReplayEvent.ReplayType
import io.sentry.SentryReplayEvent.ReplayType.BUFFER
import io.sentry.SentryReplayEvent.ReplayType.SESSION
import io.sentry.android.replay.ReplayCache
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_BIT_RATE
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_FRAME_RATE
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_HEIGHT
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_ID
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_REPLAY_ID
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_REPLAY_SCREEN_AT_START
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_REPLAY_TYPE
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_TIMESTAMP
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_WIDTH
import io.sentry.android.replay.ScreenshotRecorderConfig
import io.sentry.android.replay.capture.CaptureStrategy.Companion.createSegment
import io.sentry.android.replay.capture.CaptureStrategy.ReplaySegment
import io.sentry.android.replay.gestures.ReplayGestureConverter
import io.sentry.android.replay.util.submitSafely
import io.sentry.protocol.SentryId
import io.sentry.rrweb.RRWebEvent
import io.sentry.transport.ICurrentDateProvider
import java.io.File
import java.util.Date
import java.util.Deque
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ThreadFactory
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.AtomicReference
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
@TargetApi(26)
internal abstract class BaseCaptureStrategy(
private val options: SentryOptions,
private val scopes: IScopes?,
private val dateProvider: ICurrentDateProvider,
protected val replayExecutor: ScheduledExecutorService,
private val replayCacheProvider: ((replayId: SentryId) -> ReplayCache)? = null,
) : CaptureStrategy {
internal companion object {
private const val TAG = "CaptureStrategy"
}
private val persistingExecutor: ScheduledExecutorService by lazy {
Executors.newSingleThreadScheduledExecutor(ReplayPersistingExecutorServiceThreadFactory())
}
private val gestureConverter = ReplayGestureConverter(dateProvider)
protected val isTerminating = AtomicBoolean(false)
protected var cache: ReplayCache? = null
internal var recorderConfig: ScreenshotRecorderConfig? by
persistableAtomicNullable(propertyName = "") { _, _, newValue ->
if (newValue == null) {
// recorderConfig is only nullable on init, but never after
return@persistableAtomicNullable
}
cache?.persistSegmentValues(SEGMENT_KEY_HEIGHT, newValue.recordingHeight.toString())
cache?.persistSegmentValues(SEGMENT_KEY_WIDTH, newValue.recordingWidth.toString())
cache?.persistSegmentValues(SEGMENT_KEY_FRAME_RATE, newValue.frameRate.toString())
cache?.persistSegmentValues(SEGMENT_KEY_BIT_RATE, newValue.bitRate.toString())
}
override var segmentTimestamp by
persistableAtomicNullable<Date>(propertyName = SEGMENT_KEY_TIMESTAMP) { _, _, newValue ->
cache?.persistSegmentValues(
SEGMENT_KEY_TIMESTAMP,
if (newValue == null) null else DateUtils.getTimestamp(newValue),
)
}
protected val replayStartTimestamp = AtomicLong()
protected var screenAtStart by
persistableAtomicNullable<String>(propertyName = SEGMENT_KEY_REPLAY_SCREEN_AT_START)
override var currentReplayId: SentryId by
persistableAtomic(initialValue = SentryId.EMPTY_ID, propertyName = SEGMENT_KEY_REPLAY_ID)
override var currentSegment: Int by
persistableAtomic(initialValue = -1, propertyName = SEGMENT_KEY_ID)
override val replayCacheDir: File?
get() = cache?.replayCacheDir
override var replayType by persistableAtomic<ReplayType>(propertyName = SEGMENT_KEY_REPLAY_TYPE)
protected val currentEvents: Deque<RRWebEvent> = ConcurrentLinkedDeque()
override fun start(segmentId: Int, replayId: SentryId, replayType: ReplayType?) {
cache = replayCacheProvider?.invoke(replayId) ?: ReplayCache(options, replayId)
this.currentReplayId = replayId
this.currentSegment = segmentId
this.replayType = replayType ?: (if (this is SessionCaptureStrategy) SESSION else BUFFER)
segmentTimestamp = DateUtils.getCurrentDateTime()
replayStartTimestamp.set(dateProvider.currentTimeMillis)
}
override fun resume() {
segmentTimestamp = DateUtils.getCurrentDateTime()
}
override fun pause() = Unit
override fun stop() {
cache?.close()
replayStartTimestamp.set(0)
segmentTimestamp = null
currentReplayId = SentryId.EMPTY_ID
}
protected fun createSegmentInternal(
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,
): ReplaySegment =
createSegment(
scopes,
options,
duration,
currentSegmentTimestamp,
replayId,
segmentId,
height,
width,
replayType,
cache,
frameRate,
bitRate,
screenAtStart,
breadcrumbs,
events,
)
override fun onConfigurationChanged(recorderConfig: ScreenshotRecorderConfig) {
this.recorderConfig = recorderConfig
}
override fun onTouchEvent(event: MotionEvent) {
recorderConfig?.let { config ->
val rrwebEvents = gestureConverter.convert(event, config)
if (rrwebEvents != null) {
currentEvents += rrwebEvents
}
}
}
private class ReplayPersistingExecutorServiceThreadFactory : ThreadFactory {
private var cnt = 0
override fun newThread(r: Runnable): Thread {
val ret = Thread(r, "SentryReplayPersister-" + cnt++)
ret.setDaemon(true)
return ret
}
}
private inline fun <T> persistableAtomicNullable(
initialValue: T? = null,
propertyName: String,
crossinline onChange: (propertyName: String?, oldValue: T?, newValue: T?) -> Unit =
{ _, _, newValue ->
cache?.persistSegmentValues(propertyName, newValue.toString())
},
): ReadWriteProperty<Any?, T?> =
object : ReadWriteProperty<Any?, T?> {
private val value = AtomicReference(initialValue)
private fun runInBackground(task: () -> Unit) {
if (options.threadChecker.isMainThread) {
persistingExecutor.submitSafely(options, "$TAG.runInBackground") { task() }
} else {
try {
task()
} catch (e: Throwable) {
options.logger.log(ERROR, "Failed to execute task $TAG.runInBackground", e)
}
}
}
override fun getValue(thisRef: Any?, property: KProperty<*>): T? = value.get()
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
val oldValue = this.value.getAndSet(value)
if (oldValue != value) {
runInBackground { onChange(propertyName, oldValue, value) }
}
}
}
private inline fun <T> persistableAtomic(
initialValue: T? = null,
propertyName: String,
crossinline onChange: (propertyName: String?, oldValue: T?, newValue: T?) -> Unit =
{ _, _, newValue ->
cache?.persistSegmentValues(propertyName, newValue.toString())
},
): ReadWriteProperty<Any?, T> =
persistableAtomicNullable<T>(initialValue, propertyName, onChange) as ReadWriteProperty<Any?, T>
}