-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
sessionRecordingPlayerLogic.ts
335 lines (318 loc) · 13.1 KB
/
sessionRecordingPlayerLogic.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { kea } from 'kea'
import { sessionRecordingPlayerLogicType } from './sessionRecordingPlayerLogicType'
import { Replayer } from 'rrweb'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { SessionPlayerState, SessionPlayerTime } from '~/types'
import { eventWithTime, playerMetaData } from 'rrweb/typings/types'
import { getBreakpoint } from 'lib/utils/responsiveUtils'
import { sessionRecordingLogic } from 'scenes/session-recordings/sessionRecordingLogic'
export const PLAYBACK_SPEEDS = [0.5, 1, 2, 4, 8, 16]
const BUFFER_TIME_BUFFER_MS = 5 * 1000 // The length of time player has to have loaded to get out of buffering state
export function getZeroOffsetTime(time: number, meta: playerMetaData): number {
return Math.max(Math.min(time - meta.startTime, meta.totalTime), 0)
}
export function getOffsetTime(zeroOffsetTime: number, meta: playerMetaData): number {
return Math.max(Math.min(zeroOffsetTime + meta.startTime, meta.endTime), meta.startTime)
}
export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>({
connect: {
logic: [eventUsageLogic],
values: [
sessionRecordingLogic,
['sessionRecordingId', 'sessionPlayerData', 'sessionPlayerDataLoading', 'isPlayable'],
],
actions: [sessionRecordingLogic, ['loadRecordingSnapshotsSuccess', 'loadRecordingMetaSuccess']],
},
actions: {
initReplayer: (frame: HTMLDivElement) => ({ frame }),
setReplayer: (replayer: Replayer) => ({ replayer }),
setPlay: true,
setPause: true,
setBuffer: true,
setSkip: true,
setScrub: true,
setMeta: (meta: playerMetaData) => ({ meta }),
setMetaDuration: (duration: number) => ({ duration }),
setCurrentTime: (time: number) => ({ time }),
setRealTime: (time: number) => ({ time }),
setLastBufferedTime: (time: number) => ({ time }),
setSpeed: (speed: number) => ({ speed }),
togglePlayPause: true,
seek: (time: number, forcePlay: boolean = false) => ({ time, forcePlay }),
seekForward: true,
seekBackward: true,
clearLoadingState: true,
resolvePlayerState: true,
updateAnimation: true,
stopAnimation: true,
},
reducers: () => ({
replayer: [
null as Replayer | null,
{
setReplayer: (_, { replayer }) => replayer,
},
],
time: [
{
current: 0,
lastBuffered: 0,
} as SessionPlayerTime,
{
setCurrentTime: (state, { time }) => ({ ...state, current: time }),
setLastBufferedTime: (state, { time }) => ({ ...state, lastBuffered: time }),
},
],
realTime: [
{
current: 0,
lastBuffered: 0,
} as SessionPlayerTime,
{
seek: (state, { time }) => ({ ...state, current: time }),
setCurrentTime: (state, { time }) => ({ ...state, current: time }),
setRealTime: (state, { time }) => ({ ...state, current: time }),
},
],
speed: [
1,
{
setSpeed: (_, { speed }) => speed,
},
],
meta: [
{
startTime: 0,
endTime: 0,
totalTime: 0,
} as playerMetaData,
{
setMeta: (_, { meta }) => meta,
setMetaDuration: (state, { duration }) => ({ ...state, endTime: duration, totalTime: duration }),
},
],
playingState: [
SessionPlayerState.PLAY as SessionPlayerState.PLAY | SessionPlayerState.PAUSE,
{
setPlay: () => SessionPlayerState.PLAY,
setPause: () => SessionPlayerState.PAUSE,
},
],
loadingState: [
SessionPlayerState.BUFFER as
| SessionPlayerState.BUFFER
| SessionPlayerState.SKIP
| SessionPlayerState.SCRUB
| null,
{
setBuffer: () => SessionPlayerState.BUFFER,
setSkip: () => SessionPlayerState.SKIP,
setScrub: () => SessionPlayerState.SCRUB,
clearLoadingState: () => null,
},
],
}),
selectors: {
currentPlayerState: [
(selectors) => [selectors.playingState, selectors.loadingState],
(playingState, loadingState) => {
if (loadingState === SessionPlayerState.SCRUB) {
// If scrubbing, playingState takes precedence
return playingState
}
return loadingState ?? playingState
},
],
jumpTimeMs: [(selectors) => [selectors.speed], (speed) => 10 * 1000 * speed],
snapshots: [
(selectors) => [selectors.sessionPlayerData],
(sessionPlayerData) => sessionPlayerData?.snapshots ?? [],
],
zeroOffsetTime: [
(selectors) => [selectors.time, selectors.realTime, selectors.meta],
(time, realTime, meta) => ({
current: getZeroOffsetTime(realTime.current, meta),
lastBuffered: getZeroOffsetTime(time.lastBuffered, meta),
}),
],
},
listeners: ({ values, actions, cache }) => ({
initReplayer: ({ frame }) => {
if (values.snapshots.length < 2) {
return
}
const replayer = new Replayer(values.snapshots, {
root: frame.current,
skipInactive: true,
triggerFocus: false,
speed: values.speed,
insertStyleRules: [
`.ph-no-capture { background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHZpZXdCb3g9IjAgMCAxNiAxNiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiBmaWxsPSJibGFjayIvPgo8cGF0aCBkPSJNOCAwSDE2TDAgMTZWOEw4IDBaIiBmaWxsPSIjMkQyRDJEIi8+CjxwYXRoIGQ9Ik0xNiA4VjE2SDhMMTYgOFoiIGZpbGw9IiMyRDJEMkQiLz4KPC9zdmc+Cg=="); }`,
],
})
replayer.on('finish', () => {
// Use 500ms buffer because current time is not always exactly identical to end time.
if (values.time.current + 500 >= values.meta.endTime) {
actions.setPause()
}
})
replayer.on('skip-start', () => {
if (values.loadingState !== SessionPlayerState.BUFFER) {
actions.setSkip()
}
})
replayer.on('skip-end', () => {
if (values.loadingState === SessionPlayerState.SKIP) {
actions.clearLoadingState()
}
})
actions.setReplayer(replayer)
},
setReplayer: () => {
actions.setPlay()
},
loadRecordingMetaSuccess: async ({ sessionPlayerData }, breakpoint) => {
// Set meta timestamps when first chunk loads. The first time is a guesstimate that's later corrected by
// the time the whole chunk loads.
const startOffset = sessionPlayerData?.session_recording?.start_time ?? 0
const duration = sessionPlayerData?.session_recording?.recording_duration ?? 0
actions.setMeta({
startTime: startOffset,
endTime: startOffset + duration,
totalTime: duration,
})
breakpoint()
},
loadRecordingSnapshotsSuccess: async (_, breakpoint) => {
// On loading more of the recording, trigger some state changes
const currentEvents = values.replayer?.service.state.context.events ?? []
const eventsToAdd = values.snapshots.slice(currentEvents.length) ?? []
if (eventsToAdd.length < 1) {
return
}
// If replayer isn't initialized, it will be initialized with the already loaded snapshots
if (!!values.replayer) {
eventsToAdd.forEach((event: eventWithTime) => {
values.replayer?.addEvent(event)
})
}
// Update last buffered point
const lastEvent = eventsToAdd[eventsToAdd.length - 1]
actions.setLastBufferedTime(lastEvent.timestamp)
// If buffering has completed, resume last playing state
if (
values.currentPlayerState === SessionPlayerState.BUFFER &&
values.time.current + BUFFER_TIME_BUFFER_MS < lastEvent.timestamp
) {
actions.clearLoadingState()
actions.setPlay()
}
// Set meta once whole session recording loads. This overrides the meta set when metadata
// was fetched separately.
if (!values.sessionPlayerDataLoading && !!values.replayer) {
const meta = values.replayer.getMetaData()
// Sometimes replayer doesn't update with events we recently added.
const endTime = Math.max(
meta.endTime,
eventsToAdd.length ? eventsToAdd[eventsToAdd.length - 1]?.timestamp : 0
)
const finalMeta = {
...meta,
endTime,
totalTime: endTime - meta.startTime,
}
actions.setMeta(finalMeta)
}
breakpoint()
},
setPlay: () => {
actions.stopAnimation()
values.replayer?.setConfig({ speed: values.speed }) // hotfix: speed changes on player state change
actions.seek(values.time.current, true)
},
setPause: () => {
actions.stopAnimation()
values.replayer?.setConfig({ speed: values.speed }) // hotfix: speed changes on player state change
values.replayer?.pause()
},
setBuffer: () => {
actions.stopAnimation()
},
setScrub: () => {
actions.stopAnimation()
},
setSpeed: ({ speed }) => {
values.replayer?.setConfig({ speed })
},
seek: async ({ time, forcePlay }, breakpoint) => {
// Real seeking is debounced so as not to overload rrweb.
await breakpoint(100)
// Time passed into seek function must be timestamp offset time.
const nextTime = getZeroOffsetTime(time ?? 0, values.meta)
// Set current time to keep player updated. Replayer will catch up once time is buffered
actions.setCurrentTime(time ?? 0)
// If next time is greater than last buffered time, set to buffering
if (nextTime > values.zeroOffsetTime.lastBuffered) {
values.replayer?.pause()
actions.setBuffer()
}
// If not forced to play and if last playing state was pause, pause
else if (!forcePlay && values.currentPlayerState === SessionPlayerState.PAUSE) {
values.replayer?.pause(nextTime)
actions.clearLoadingState()
}
// Otherwise play
else {
values.replayer?.play(nextTime)
actions.updateAnimation()
}
breakpoint()
},
seekForward: () => {
actions.seek(values.time.current + values.jumpTimeMs)
},
seekBackward: () => {
actions.seek(values.time.current - values.jumpTimeMs)
},
togglePlayPause: () => {
// If buffering, toggle is a noop
if (values.currentPlayerState === SessionPlayerState.BUFFER) {
return
}
// If paused, start playing
if (values.currentPlayerState === SessionPlayerState.PAUSE) {
actions.setPlay()
}
// If skipping, pause and turn skipping off
else if (values.currentPlayerState === SessionPlayerState.SKIP) {
actions.clearLoadingState()
actions.setPause()
}
// If playing, pause
else {
actions.setPause()
}
},
updateAnimation: () => {
const nextTime = getOffsetTime(values.replayer?.getCurrentTime() || 0, values.meta)
if (nextTime > values.time.lastBuffered) {
values.replayer?.pause()
actions.setBuffer()
} else {
actions.setCurrentTime(nextTime)
}
cache.timer = requestAnimationFrame(actions.updateAnimation)
},
stopAnimation: () => {
if (cache.timer) {
cancelAnimationFrame(cache.timer)
}
},
clearLoadingState: () => {
values.replayer?.setConfig({ speed: values.speed }) // hotfix: speed changes on player state change
},
}),
windowValues: {
isSmallScreen: (window) => window.innerWidth < getBreakpoint('md'),
},
})