forked from hsiaosiyuan0/fmp4-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
177 lines (147 loc) · 4.25 KB
/
app.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
import { Downloader } from "./downloader.js";
const url = "http://localhost:8000/1.mp4";
function loadMediaData(ctx) {
const { dw, mp4file } = ctx.shared;
dw.chunkStart = mp4file.seek(0, true).offset;
mp4file.start();
dw.resume();
}
function handleUpdateEnd(ctx) {
console.log("upd", ctx.sb.updating);
if (ctx.sb.updating || ctx.shared.ms.readyState !== "open") return;
const seg = ctx.pending.shift();
if (seg && seg.isInit) {
ctx.shared.pendingInitCnt--;
}
if (ctx.shared.pendingInitCnt === 0 && !ctx.shared.loading) {
ctx.shared.loading = true;
loadMediaData(ctx);
return;
}
if (ctx.isEof) {
ctx.shared.notEndCnt--;
}
if (ctx.shared.notEndCnt === 0 && !ctx.shared.isMseEnd) {
if (ctx.sampleNum) {
ctx.shared.mp4file.releaseUsedSamples(ctx.id, ctx.sampleNum);
ctx.sampleNum = null;
}
ctx.shared.isMseEnd = true;
ctx.shared.ms.endOfStream();
}
if (seg && !seg.isInit) {
ctx.sampleNum = seg.sampleNum;
ctx.isEof = seg.isEnd;
console.log("appendBuffer", seg);
ctx.sb.appendBuffer(seg.buffer);
}
}
function linkMsAndMp4(vElem, ms, mp4file, mp4info) {
const trackLen = mp4info.tracks.length;
const shared = {
ms,
vElem,
loading: false,
notEndCnt: trackLen,
pendingInitCnt: trackLen,
dw: mp4file.dw,
mp4file,
loading: false,
isMseEnd: false
};
mp4info.tracks.forEach(track => {
setSegmentOptions(ms, mp4file, track, shared);
});
}
function setSegmentOptions(ms, mp4file, track, shared) {
const mime = `video/mp4; codecs="${track.codec}"`;
if (!MediaSource.isTypeSupported(mime)) {
throw new Error("MSE does not support: " + mime);
}
const sb = ms.addSourceBuffer(mime);
const ctx = {
sb,
id: track.id,
pending: [],
shared
};
sb.addEventListener("error", e => console.error(e));
sb.addEventListener("updateend", () => handleUpdateEnd(ctx));
mp4file.setSegmentOptions(track.id, ctx);
}
function initializeSegmentation(mp4file) {
mp4file.initializeSegmentation().forEach(seg => {
const ctx = seg.user;
console.log(seg);
ctx.sb.appendBuffer(seg.buffer);
ctx.pending.push({ isInit: true });
});
}
function handleSourceOpen(evt, vElem) {
URL.revokeObjectURL(evt.target.src);
console.log("handleSourceOpen");
const ms = evt.target;
const mp4file = MP4Box.createFile();
mp4file.onReady = info => {
console.log("mp4file is ready: ", info);
ms.duration = info.duration / info.timescale;
linkMsAndMp4(vElem, ms, mp4file, info);
initializeSegmentation(mp4file);
};
mp4file.onSegment = function(id, user, buffer, sampleNum, isEnd) {
console.log("onSegment", { id, user, buffer, sampleNum, isEnd });
const ctx = user;
ctx.pending.push({
id: id,
buffer: buffer,
sampleNum: sampleNum,
isEnd: isEnd
});
handleUpdateEnd(ctx);
};
const dw = (mp4file.dw = new Downloader({
url,
chunkSize: 300 * 1024,
onChunk({ bytes, isEof }) {
const next = mp4file.appendBuffer(bytes, isEof);
if (isEof) {
mp4file.flush();
} else {
dw.chunkStart = next;
}
}
}));
mp4file.dw.start();
vElem.addEventListener("seeking", () => handleSeeking(vElem, mp4file));
vElem.addEventListener("timeupdate", () => handleSeeking(vElem, mp4file));
}
function handleSeeking(video, mp4file) {
if (video.lastSeekTime === video.currentTime) return;
let start;
let end;
for (let i = 0, len = video.buffered.length; i < len; i++) {
start = video.buffered.start(i);
end = video.buffered.end(i);
if (video.currentTime >= start && video.currentTime <= end) {
return;
}
}
const dw = mp4file.dw;
const seek = mp4file.seek(video.currentTime, true);
dw.chunkStart = seek.offset;
video.currentTime = seek.time;
dw.stop();
dw.resume();
video.lastSeekTime = video.currentTime;
}
function attachMediaSource(vElem) {
const ms = new MediaSource();
ms.addEventListener("sourceopen", evt => handleSourceOpen(evt, vElem));
vElem.src = URL.createObjectURL(ms);
}
function bootstrap() {
if (!window.MediaSource) throw new Error("Browser does not support MSE");
const vElem = document.querySelector("video");
attachMediaSource(vElem);
}
document.addEventListener("DOMContentLoaded", bootstrap);