-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
290 lines (244 loc) · 7.99 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
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
/*! mediasource. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
module.exports = MediaElementWrapper
var inherits = require('inherits')
var stream = require('readable-stream')
var toArrayBuffer = require('to-arraybuffer')
var MediaSource = typeof window !== 'undefined' && window.MediaSource
var DEFAULT_BUFFER_DURATION = 60 // seconds
function MediaElementWrapper (elem, opts) {
var self = this
if (!(self instanceof MediaElementWrapper)) return new MediaElementWrapper(elem, opts)
if (!MediaSource) throw new Error('web browser lacks MediaSource support')
if (!opts) opts = {}
self._debug = opts.debug
self._bufferDuration = opts.bufferDuration || DEFAULT_BUFFER_DURATION
self._elem = elem
self._mediaSource = new MediaSource()
self._streams = []
self.detailedError = null
self._errorHandler = function () {
self._elem.removeEventListener('error', self._errorHandler)
var streams = self._streams.slice()
streams.forEach(function (stream) {
stream.destroy(self._elem.error)
})
}
self._elem.addEventListener('error', self._errorHandler)
self._elem.src = window.URL.createObjectURL(self._mediaSource)
}
/*
* `obj` can be a previous value returned by this function
* or a string
*/
MediaElementWrapper.prototype.createWriteStream = function (obj) {
var self = this
return new MediaSourceStream(self, obj)
}
/*
* Use to trigger an error on the underlying media element
*/
MediaElementWrapper.prototype.error = function (err) {
var self = this
// be careful not to overwrite any existing detailedError values
if (!self.detailedError) {
self.detailedError = err
}
self._dumpDebugData()
try {
self._mediaSource.endOfStream('decode')
} catch (err) {}
try {
// Attempt to clean up object URL
window.URL.revokeObjectURL(self._elem.src)
} catch (err) {}
}
/*
* When self._debug is set, dump all data to files
*/
MediaElementWrapper.prototype._dumpDebugData = function () {
var self = this
if (self._debug) {
self._debug = false // prevent multiple dumps on multiple errors
self._streams.forEach(function (stream, i) {
downloadBuffers(stream._debugBuffers, 'mediasource-stream-' + i)
})
}
}
inherits(MediaSourceStream, stream.Writable)
function MediaSourceStream (wrapper, obj) {
var self = this
stream.Writable.call(self)
self._wrapper = wrapper
self._elem = wrapper._elem
self._mediaSource = wrapper._mediaSource
self._allStreams = wrapper._streams
self._allStreams.push(self)
self._bufferDuration = wrapper._bufferDuration
self._sourceBuffer = null
self._debugBuffers = []
self._openHandler = function () {
self._onSourceOpen()
}
self._flowHandler = function () {
self._flow()
}
self._errorHandler = function (err) {
if (!self.destroyed) {
self.emit('error', err)
}
}
if (typeof obj === 'string') {
self._type = obj
// Need to create a new sourceBuffer
if (self._mediaSource.readyState === 'open') {
self._createSourceBuffer()
} else {
self._mediaSource.addEventListener('sourceopen', self._openHandler)
}
} else if (obj._sourceBuffer === null) {
obj.destroy()
self._type = obj._type // The old stream was created but hasn't finished initializing
self._mediaSource.addEventListener('sourceopen', self._openHandler)
} else if (obj._sourceBuffer) {
obj.destroy()
self._type = obj._type
self._sourceBuffer = obj._sourceBuffer // Copy over the old sourceBuffer
self._debugBuffers = obj._debugBuffers // Copy over previous debug data
self._sourceBuffer.addEventListener('updateend', self._flowHandler)
self._sourceBuffer.addEventListener('error', self._errorHandler)
} else {
throw new Error('The argument to MediaElementWrapper.createWriteStream must be a string or a previous stream returned from that function')
}
self._elem.addEventListener('timeupdate', self._flowHandler)
self.on('error', function (err) {
self._wrapper.error(err)
})
self.on('finish', function () {
if (self.destroyed) return
self._finished = true
if (self._allStreams.every(function (other) { return other._finished })) {
self._wrapper._dumpDebugData()
try {
self._mediaSource.endOfStream()
} catch (err) {}
}
})
}
MediaSourceStream.prototype._onSourceOpen = function () {
var self = this
if (self.destroyed) return
self._mediaSource.removeEventListener('sourceopen', self._openHandler)
self._createSourceBuffer()
}
MediaSourceStream.prototype.destroy = function (err) {
var self = this
if (self.destroyed) return
self.destroyed = true
// Remove from allStreams
self._allStreams.splice(self._allStreams.indexOf(self), 1)
self._mediaSource.removeEventListener('sourceopen', self._openHandler)
self._elem.removeEventListener('timeupdate', self._flowHandler)
if (self._sourceBuffer) {
self._sourceBuffer.removeEventListener('updateend', self._flowHandler)
self._sourceBuffer.removeEventListener('error', self._errorHandler)
if (self._mediaSource.readyState === 'open') {
self._sourceBuffer.abort()
}
}
if (err) self.emit('error', err)
self.emit('close')
}
MediaSourceStream.prototype._createSourceBuffer = function () {
var self = this
if (self.destroyed) return
if (MediaSource.isTypeSupported(self._type)) {
self._sourceBuffer = self._mediaSource.addSourceBuffer(self._type)
self._sourceBuffer.addEventListener('updateend', self._flowHandler)
self._sourceBuffer.addEventListener('error', self._errorHandler)
if (self._cb) {
var cb = self._cb
self._cb = null
cb()
}
} else {
self.destroy(new Error('The provided type is not supported'))
}
}
MediaSourceStream.prototype._write = function (chunk, encoding, cb) {
var self = this
if (self.destroyed) return
if (!self._sourceBuffer) {
self._cb = function (err) {
if (err) return cb(err)
self._write(chunk, encoding, cb)
}
return
}
if (self._sourceBuffer.updating) {
return cb(new Error('Cannot append buffer while source buffer updating'))
}
var arr = toArrayBuffer(chunk)
if (self._wrapper._debug) {
self._debugBuffers.push(arr)
}
try {
self._sourceBuffer.appendBuffer(arr)
} catch (err) {
// appendBuffer can throw for a number of reasons, most notably when the data
// being appended is invalid or if appendBuffer is called after another error
// already occurred on the media element. In Chrome, there may be useful debugging
// info in chrome://media-internals
self.destroy(err)
return
}
self._cb = cb
}
MediaSourceStream.prototype._flow = function () {
var self = this
if (self.destroyed || !self._sourceBuffer || self._sourceBuffer.updating) {
return
}
if (self._mediaSource.readyState === 'open') {
// check buffer size
if (self._getBufferDuration() > self._bufferDuration) {
return
}
}
if (self._cb) {
var cb = self._cb
self._cb = null
cb()
}
}
// TODO: if zero actually works in all browsers, remove the logic associated with this below
var EPSILON = 0
MediaSourceStream.prototype._getBufferDuration = function () {
var self = this
var buffered = self._sourceBuffer.buffered
var currentTime = self._elem.currentTime
var bufferEnd = -1 // end of the buffer
// This is a little over complex because some browsers seem to separate the
// buffered region into multiple sections with slight gaps.
for (var i = 0; i < buffered.length; i++) {
var start = buffered.start(i)
var end = buffered.end(i) + EPSILON
if (start > currentTime) {
// Reached past the joined buffer
break
} else if (bufferEnd >= 0 || currentTime <= end) {
// Found the start/continuation of the joined buffer
bufferEnd = end
}
}
var bufferedTime = bufferEnd - currentTime
if (bufferedTime < 0) {
bufferedTime = 0
}
return bufferedTime
}
function downloadBuffers (bufs, name) {
var a = document.createElement('a')
a.href = window.URL.createObjectURL(new window.Blob(bufs))
a.download = name
a.click()
}