/
player.ts
379 lines (316 loc) · 10.7 KB
/
player.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { PointerComponent } from './pointer'
import { updateDom } from './dom'
import {
reduxStore,
PlayerTypes,
ProgressState,
getTime,
isSnapshot,
delay,
toTimeStamp,
base64ToFloat32Array,
encodeWAV
} from '@timecat/utils'
import { ProgressComponent } from './progress'
import { ContainerComponent } from './container'
import { RecordData, AudioData, SnapshotRecord, ReplayPack, ReplayData } from '@timecat/share'
import { BroadcasterComponent } from './broadcaster'
import { AnimationFrame } from './animationFrame'
export class PlayerComponent {
c: ContainerComponent
pointer: PointerComponent
progress: ProgressComponent
broadcaster: BroadcasterComponent
audioNode: HTMLAudioElement
progressState: ProgressState
records: RecordData[]
speed = 0
recordIndex = 0
frameIndex = 0
lastPercentage = 0
isFirstTimePlay = true
frameInterval = 250
frames: number[]
startTime: number
elapsedTime = 0
audioOffset = 500
curViewEndTime: number
curViewDiffTime = 0
subtitlesIndex = 0
audioData: AudioData
audioBlobUrl: string
RAF: AnimationFrame
constructor(
c: ContainerComponent,
pointer: PointerComponent,
progress: ProgressComponent,
broadcaster: BroadcasterComponent
) {
this.c = c
this.pointer = pointer
this.progress = progress
this.broadcaster = broadcaster
this.audioNode = new Audio()
this.initViewState()
if (!this.records.length) {
// is live mode
window.addEventListener('record-data', this.streamHandle.bind(this))
} else {
reduxStore.subscribe('player', state => {
if (state) {
this.progressState = reduxStore.getState('progress')
const speed = state.speed
this.speed = speed
this.frames = this.getAccuratelyFrame()
if (speed > 0) {
this.play()
} else {
this.pause()
}
this.setProgress()
}
})
}
}
initAudio() {
if (!this.audioData) {
return
}
if (this.audioData.src) {
this.audioBlobUrl = location.href.split('/').slice(0, -1).join('/') + '/' + this.audioData.src
} else {
const bufferStrList = this.audioData.bufferStrList
if (!bufferStrList.length) {
return
}
const dataArray: Float32Array[] = []
for (let i = 0; i < bufferStrList.length; i++) {
const data = base64ToFloat32Array(bufferStrList[i])
dataArray.push(data)
}
const audioBlob = encodeWAV(dataArray, this.audioData.opts)
const audioBlobUrl = URL.createObjectURL(audioBlob)
this.audioBlobUrl = audioBlobUrl
}
}
streamHandle(this: PlayerComponent, e: CustomEvent) {
const frame = e.detail as RecordData
if (isSnapshot(frame)) {
window.__ReplayData__.snapshot = frame as SnapshotRecord
this.c.setViewState()
return
}
this.execFrame(frame as RecordData)
}
initViewState() {
const { __ReplayPacks__: packs } = window
const firstPack = packs[0] as ReplayPack
const firstData = firstPack.body[0]
this.records = firstData.records
this.audioData = firstData.audio
this.initAudio()
// live mode
if (!this.records.length) {
return
}
this.subtitlesIndex = 0
this.broadcaster.cleanText()
this.curViewEndTime = +this.records.slice(-1)[0].time
this.curViewDiffTime = 0
window.__ReplayData__ = firstData
}
async switchNextView(delayTime?: number) {
const { __ReplayData__: rData, __ReplayPacks__: packs } = window as {
__ReplayData__: ReplayData
__ReplayPacks__: ReplayPack[]
}
if (!this.records) {
return
}
const nextData = getNextData(rData)
if (!nextData) {
return
}
function getNextData(curData: ReplayData) {
for (let i = 0; i < packs.length; i++) {
const body = packs[i].body
const nextPackBody = packs[i + 1]?.body
for (let j = 0; j < body.length; j++) {
if (curData === body[j]) {
const next = body[j + 1]
if (next) {
return next
} else if (nextPackBody.length) {
return nextPackBody[0]
}
return null
}
}
}
return null
}
const curEndTime = +this.records.slice(-1)[0].time
const nextStartTime = +nextData.records[0].time
this.curViewDiffTime += nextStartTime - curEndTime
window.__ReplayData__ = nextData
this.records = nextData.records
this.audioData = nextData.audio
this.initAudio()
this.curViewEndTime = +this.records.slice(-1)[0].time
this.recordIndex = 0
if (delayTime) {
await delay(delayTime)
}
this.c.setViewState()
}
play() {
this.playAudio()
if (this.recordIndex === 0) {
this.progress.resetThumb()
if (!this.isFirstTimePlay) {
// Indicates the second times play
this.initViewState()
this.c.setViewState()
}
this.isFirstTimePlay = false
}
if (this.RAF && this.RAF.requestID) {
this.RAF.stop()
}
const maxFps = 30
this.RAF = new AnimationFrame(loop.bind(this), maxFps)
this.RAF.start()
const initTime = getTime()
this.startTime = 0
async function loop(this: PlayerComponent, t: number, loopIndex: number) {
const timeStamp = getTime() - initTime
if (this.frameIndex > 0 && !this.frames[this.frameIndex]) {
this.stop()
return
}
if (!this.startTime) {
this.startTime = Number(this.frames[this.frameIndex])
}
const currTime = this.startTime + timeStamp * this.speed
let nextTime = Number(this.frames[this.frameIndex])
if (nextTime > this.curViewEndTime - this.curViewDiffTime) {
// delay 200ms wait for all frame finished and switch next
await this.switchNextView(200)
}
while (nextTime && currTime >= nextTime) {
this.renderEachFrame()
this.frameIndex++
nextTime = Number(this.frames[this.frameIndex])
}
this.elapsedTime = (currTime - this.frames[0]) / 1000
// sync audio time
// every 2s check once
const frameCount = Math.floor(2 / (this.frameInterval / 1000))
const checkInterval = !(this.frameIndex % frameCount)
const shouldCheckAudioTime = this.audioNode.src && checkInterval && !((loopIndex % frameCount) * 2)
if (shouldCheckAudioTime) {
const allowDiff = 200
if (Math.abs((this.elapsedTime - this.audioNode.currentTime) * 1000) > this.audioOffset + allowDiff) {
this.syncAudioCurrentTime()
}
}
}
}
playAudio() {
if (!this.audioData) {
return
}
if (!this.audioBlobUrl) {
this.pauseAudio()
return
}
if (this.audioNode) {
if (!this.audioNode.src || this.audioNode.src !== this.audioBlobUrl) {
this.audioNode.src = this.audioBlobUrl
}
this.syncAudioCurrentTime()
if (this.speed > 1) {
this.audioNode.pause()
} else {
this.audioNode.play()
}
}
}
syncAudioCurrentTime(elapsedTime: number = this.elapsedTime, offset: number = this.audioOffset / 1000) {
this.audioNode.currentTime = elapsedTime + offset
}
pauseAudio() {
if (this.audioNode) {
this.audioNode.pause()
}
}
setProgress() {
this.progress.setProgressAnimation(this.frameIndex, this.frames.length, this.frameInterval, this.speed)
}
renderEachFrame() {
this.progress.updateTimer(((this.frameIndex + 1) * this.frameInterval) / 1000)
let data: RecordData
while (
this.recordIndex < this.records.length &&
+(data = this.records[this.recordIndex]).time - this.curViewDiffTime <= this.frames[this.frameIndex]
) {
this.execFrame.call(this, data)
this.recordIndex++
}
if (this.audioData && this.audioData.subtitles.length) {
const subtitles = this.audioData.subtitles
const { start, end, text } = subtitles[this.subtitlesIndex]
const audioStartTime = toTimeStamp(start)
const audioEndTime = toTimeStamp(end)
if (this.elapsedTime > audioEndTime / 1000) {
this.broadcaster.cleanText()
if (this.subtitlesIndex < subtitles.length - 1) {
this.subtitlesIndex++
}
} else if (this.elapsedTime > audioStartTime / 1000) {
this.broadcaster.updateText(text)
}
}
}
pause() {
if (this.RAF) {
this.RAF.stop()
}
reduxStore.dispatch({
type: PlayerTypes.SPEED,
data: {
speed: 0
}
})
this.pauseAudio()
}
stop() {
this.speed = 0
this.recordIndex = 0
this.frameIndex = 0
this.lastPercentage = 0
this.elapsedTime = 0 // unit: sec
this.pause()
this.audioNode.currentTime = 0
}
execFrame(this: PlayerComponent, record: RecordData) {
setTimeout(() => updateDom.call(this, record), 0)
}
getPercentInterval() {
const k = 0.08
const b = 0.2
return this.speed * k + b
}
getAccuratelyFrame(interval = this.frameInterval) {
this.progressState = reduxStore.getState()['progress']
const { startTime, endTime } = this.progressState
const s = +startTime
const e = +endTime
const result: number[] = []
for (let i = s; i < e; i += interval) {
result.push(i)
}
result.push(e)
return result
}
}