-
Notifications
You must be signed in to change notification settings - Fork 0
/
after.js
246 lines (212 loc) · 6.41 KB
/
after.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
(function () {
// configurations
var AUDIO_URL = './sample.mp4';
var AUDIO_MIME_TYPE = 'audio/mp4; codecs="mp4a.40.2"';
var AUTO_RESET = true;
var AUTO_RESET_SEC = 3;
var REUSE_DATA = true;
var isLoading = false;
var data = [];
var audioElement = null;
var mediaSource = null;
var objectURL = null;
var sourceBuffer = null;
var loadIndex = 0;
var timeRange = { start: 0, end: 0 };
var count = document.title = 0;
var out = document.getElementById('out');
document.getElementById('start').addEventListener('click', start);
document.getElementById('resetData').addEventListener('click', resetData);
document.getElementById('resetMSE').addEventListener('click', resetMSE);
// entry point
function start() {
console.log('start');
if (isLoading) {
console.log('wait a minute!');
return;
}
isLoading = true;
resetMSE()
.then(() => {
if (!REUSE_DATA) {
resetData();
}
})
.then(() => loadData())
.then(() => {
startAudioPlayback();
document.title = ++count;
if (AUTO_RESET) {
setTimeout(() => {
isLoading = false;
start();
}, AUTO_RESET_SEC * 1000);
} else {
isLoading = false;
}
});
}
// load array buffers via XHR
function loadData() {
if (data.length) {
return Promise.resolve();
}
console.log('loadData');
return new Promise(resolve => {
get(0);
function get(start) {
var unit = 2 * 1024 * 1024; // 2MB
var xhr = new XMLHttpRequest();
var end = start + unit;
xhr.open('GET', AUDIO_URL, true);
xhr.responseType = 'arraybuffer';
xhr.setRequestHeader('Range', `bytes=${start}-${end}`);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 206) {
data.push(xhr.response);
var matches = xhr.getResponseHeader('content-range').match(/(\d+)-(\d+)\/(\d+)/);
var to = Number(matches[2]);
var total = Number(matches[3]);
if (to + 1 >= total) {
console.log('loadData complete');
resolve();
} else {
get(to + 1);
}
}
};
xhr.send(null);
}
});
}
// entry point of playback
function startAudioPlayback() {
console.log('play');
audioElement = new Audio();
audioElement.controls = true;
audioElement.style.cssText = 'display: block; width: 100%;';
mediaSource = new MediaSource();
objectURL = URL.createObjectURL(mediaSource);
audioElement.src = objectURL;
out.appendChild(audioElement);
audioElement.addEventListener('timeupdate', update);
mediaSource.addEventListener('sourceopen', onSourceopen);
}
// this event will fire from MediaSource
function onSourceopen() {
console.log('onSourceopen');
mediaSource.removeEventListener('sourceopen', onSourceopen);
sourceBuffer = mediaSource.addSourceBuffer(AUDIO_MIME_TYPE);
sourceBuffer.addEventListener('updateend', onUpdateEnd);
update();
}
// this event will fire from SourceBuffer
function onUpdateEnd() {
sourceBuffer.appendWindowStart = 0;
sourceBuffer.appendWindowEnd = audioElement.duration;
const buffered = sourceBuffer.buffered;
const start = buffered.start(0);
const end = buffered.end(buffered.length - 1);
const duration = end - start;
timeRange = {
start: start,
end: end,
};
console.log('onUpdateEnd:', {
appendWindowStart: sourceBuffer.appendWindowStart,
appendWindowEnd: sourceBuffer.appendWindowEnd,
start: start,
end: end,
});
if (duration > 10) {
audioElement.play();
}
update();
}
// this event will fire from Audio
function onTimeupdate() {
update();
}
// this function will be called from onSourceopen, onUpdateEnd and onTimeupdate
// add next buffer if it is required
function update() {
if (!data[loadIndex]) {
console.log('update skip: !data[loadIndex]');
return;
}
if (!sourceBuffer || !mediaSource || !audioElement) {
return;
}
if (sourceBuffer.updating) {
console.log('update skip: sourceBuffer.updating');
return;
}
if (mediaSource.readyState === 'closed') {
console.log('update skip: mediaSource.readyState === \'closed\'');
return;
}
if (timeRange.end - audioElement.currentTime > 2 * 60) {
console.log('update skip: timeRange.end - audioElement.currentTime > 30');
return;
}
console.log('will append buffer', loadIndex);
sourceBuffer.appendBuffer(data[loadIndex]);
loadIndex++;
}
// reset ArrayBuffers on memory
function resetData() {
data = [];
console.log('resetData');
}
// reset all of elements about Audio Playback
// NOTE: Rewrite to async to wait for the buffer to be removed.
function resetMSE() {
return new Promise(resolve => {
if (sourceBuffer && sourceBuffer.updating) {
sourceBuffer.abort();
console.log('sourceBuffer.abort()');
}
if (audioElement) {
audioElement.removeEventListener('timeupdate', onTimeupdate);
audioElement.pause();
console.log('reset audioElement');
}
if (sourceBuffer) {
sourceBuffer.removeEventListener('updateend', onUpdateEnd);
sourceBuffer.addEventListener('updateend', onBufferRemoved);
sourceBuffer.remove(timeRange.start, timeRange.end);
console.log('will remove sourceBuffer');
}
if (mediaSource) {
mediaSource.removeEventListener('sourceopen', onSourceopen);
}
if (!sourceBuffer) {
resolve();
}
function onBufferRemoved() {
console.log('removed sourceBuffer');
sourceBuffer.removeEventListener('updateend', onBufferRemoved);
resolve();
}
})
.then(() => {
if (mediaSource) {
mediaSource.removeSourceBuffer(sourceBuffer);
mediaSource.endOfStream();
console.log('remove mediaSource');
}
if (audioElement && objectURL) {
URL.revokeObjectURL(objectURL);
delete audioElement.src;
audioElement.remove();
console.log('remove audioElement');
}
audioElement = null;
mediaSource = null;
objectURL = null;
sourceBuffer = null;
loadIndex = 0;
timeRange = { start: 0, end: 0 };
});
}
})();