-
Notifications
You must be signed in to change notification settings - Fork 73
/
videostream.js
126 lines (110 loc) · 3.12 KB
/
videostream.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
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
const MediaElementWrapper = require('mediasource')
const pump = require('pump')
const MP4Remuxer = require('./mp4-remuxer')
function VideoStream (file, mediaElem, opts = {}) {
if (!(this instanceof VideoStream)) {
console.warn("Don't invoke VideoStream without the 'new' keyword.")
return new VideoStream(file, mediaElem, opts)
}
this.detailedError = null
this._elem = mediaElem
this._elemWrapper = new MediaElementWrapper(mediaElem)
this._waitingFired = false
this._trackMeta = null
this._file = file
this._tracks = null
if (this._elem.preload !== 'none') {
this._createMuxer()
}
this._onError = () => {
this.detailedError = this._elemWrapper.detailedError
this.destroy() // don't pass err though so the user doesn't need to listen for errors
}
this._onWaiting = () => {
this._waitingFired = true
if (!this._muxer) {
this._createMuxer()
} else if (this._tracks) {
this._pump()
}
}
if (mediaElem.autoplay) { mediaElem.preload = 'auto' }
mediaElem.addEventListener('waiting', this._onWaiting)
mediaElem.addEventListener('error', this._onError)
}
VideoStream.prototype = {
_createMuxer () {
this._muxer = new MP4Remuxer(this._file)
this._muxer.on('ready', data => {
this._tracks = data.map(trackData => {
const mediaSource = this._elemWrapper.createWriteStream(trackData.mime)
mediaSource.on('error', err => {
this._elemWrapper.error(err)
})
const track = {
muxed: null,
mediaSource,
initFlushed: false,
onInitFlushed: null
}
mediaSource.write(trackData.init, err => {
track.initFlushed = true
if (track.onInitFlushed) {
track.onInitFlushed(err)
}
})
return track
})
if (this._waitingFired || this._elem.preload === 'auto') {
this._pump()
}
})
this._muxer.on('error', err => {
this._elemWrapper.error(err)
})
},
_pump () {
const muxed = this._muxer.seek(this._elem.currentTime, !this._tracks)
this._tracks.forEach((track, i) => {
const pumpTrack = () => {
if (track.muxed) {
track.muxed.destroy()
track.mediaSource = this._elemWrapper.createWriteStream(track.mediaSource)
track.mediaSource.on('error', err => {
this._elemWrapper.error(err)
})
}
track.muxed = muxed[i]
pump(track.muxed, track.mediaSource)
}
if (!track.initFlushed) {
track.onInitFlushed = err => {
if (err) {
this._elemWrapper.error(err)
return
}
pumpTrack()
}
} else {
pumpTrack()
}
})
},
destroy () {
if (this.destroyed) {
return
}
this.destroyed = true
this._elem.removeEventListener('waiting', this._onWaiting)
this._elem.removeEventListener('error', this._onError)
if (this._tracks) {
this._tracks.forEach(track => {
if (track.muxed) {
track.muxed.destroy()
}
})
}
this._elem.src = ''
}
}
module.exports = VideoStream