-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
67 lines (52 loc) · 1.4 KB
/
index.js
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
const { Readable } = require('streamx')
const FIFO = require('fast-fifo')
const b4a = require('b4a')
module.exports = class MediaRecorderStream extends Readable {
constructor (media, opts = {}) {
super()
this.media = media
this.queued = new FIFO()
this.recorder = null
this.stopped = false
this.interval = opts.interval || 1000
this.options = opts
}
_open (cb) {
if (this.stopped) {
cb(null)
return
}
this.recorder = new window.MediaRecorder(this.media, this.options)
this.recorder.addEventListener('dataavailable', (ev) => this._queue(ev.data))
this.recorder.start(this.interval)
cb(null)
}
_queue (blob) {
const next = { buffer: null }
this.queued.push(next)
const r = new window.FileReader()
r.addEventListener('loadend', () => {
next.value = b4a.from(r.result)
while (true) {
const btm = this.queued.peek()
if (!btm || !btm.value) return
this.queued.shift()
this.push(btm.value)
}
})
r.readAsArrayBuffer(blob)
}
stop () {
this.stopped = true
if (this.recoder === null) {
this.push(null)
return
}
this.recorder.stop()
const video = this.media.getVideoTracks()
const audio = this.media.getAudioTracks()
for (const m of video) m.stop()
for (const m of audio) m.stop()
this.once('data', () => this.push(null))
}
}