-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmp4_demuxer.js
202 lines (161 loc) · 4.75 KB
/
mp4_demuxer.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
const DEMUX_STOP_SECS = 30; // 30 seconds
class MP4Source {
constructor(uri) {
this.file = MP4Box.createFile();
this.file.onError = console.error.bind(console);
this.file.onReady = this.onReady.bind(this);
this.file.onSamples = this.onSamples.bind(this);
console.log('fetching file');
fetch(uri).then(response => {
console.log('fetch done');
const reader = response.body.getReader();
let offset = 0;
let mp4File = this.file;
function appendBuffers({done, value}) {
if(done) {
mp4File.flush();
return;
}
let buf = value.buffer;
buf.fileStart = offset;
offset += buf.byteLength;
mp4File.appendBuffer(buf);
return reader.read().then(appendBuffers);
}
return reader.read().then(appendBuffers);
})
this.info = null;
this._info_resolver = null;
}
onReady(info) {
// TODO: Generate configuration changes.
this.info = info;
if (this._info_resolver) {
this._info_resolver(info);
this._info_resolver = null;
}
}
getInfo() {
if (this.info)
return Promise.resolve(this.info);
return new Promise((resolver) => { this._info_resolver = resolver; });
}
getAvccBox() {
// TODO: make sure this is coming from the right track.
return this.file.moov.traks[0].mdia.minf.stbl.stsd.entries[0].avcC
}
start(time, track, onChunk) {
if (!this.stopped)
this.stop();
this.stopped = false;
this._onChunk = onChunk;
this.file.setExtractionOptions(track.id);
// seek seems to always go to the key frame before time, so + 1 to ensure we get exactly the frame
this.file.seek(/* time in sec */ (time + 1) / 1000000, /* useRap */ true);
this.file.start();
}
stop() {
this.file.stop();
this.stopped = true;
}
onSamples(track_id, ref, samples) {
for (const sample of samples) {
const pts_secs = sample.cts / sample.timescale;
const type = sample.is_sync ? "key" : "delta";
const chunk = new EncodedVideoChunk({
type: type,
timestamp: pts_secs * 1000000,
duration: sample.duration,
data: sample.data
});
this._onChunk(chunk);
if (this.stopped)
return;
}
}
}
class Writer {
constructor(size) {
this.data = new Uint8Array(size);
this.idx = 0;
this.size = size;
}
getData() {
if(this.idx != this.size)
throw "Mismatch between size reserved and sized used"
return this.data.slice(0, this.idx);
}
writeUint8(value) {
this.data.set([value], this.idx);
this.idx++;
}
writeUint16(value) {
// TODO: find a more elegant solution to endianess.
var arr = new Uint16Array(1);
arr[0] = value;
var buffer = new Uint8Array(arr.buffer);
this.data.set([buffer[1], buffer[0]], this.idx);
this.idx +=2;
}
writeUint8Array(value) {
this.data.set(value, this.idx);
this.idx += value.length;
}
}
export class MP4Demuxer {
constructor(uri) {
this.source = new MP4Source(uri);
}
getAvcDescription(avccBox) {
var i;
var size = 7;
for (i = 0; i < avccBox.SPS.length; i++) {
// nalu length is encoded as a uint16.
size+= 2 + avccBox.SPS[i].length;
}
for (i = 0; i < avccBox.PPS.length; i++) {
// nalu length is encoded as a uint16.
size+= 2 + avccBox.PPS[i].length;
}
var writer = new Writer(size);
writer.writeUint8(avccBox.configurationVersion);
writer.writeUint8(avccBox.AVCProfileIndication);
writer.writeUint8(avccBox.profile_compatibility);
writer.writeUint8(avccBox.AVCLevelIndication);
writer.writeUint8(avccBox.lengthSizeMinusOne + (63<<2));
writer.writeUint8(avccBox.nb_SPS_nalus + (7<<5));
for (i = 0; i < avccBox.SPS.length; i++) {
writer.writeUint16(avccBox.SPS[i].length);
writer.writeUint8Array(avccBox.SPS[i].nalu);
window.temp = avccBox.SPS[i].nalu;
}
writer.writeUint8(avccBox.nb_PPS_nalus);
for (i = 0; i < avccBox.PPS.length; i++) {
writer.writeUint16(avccBox.PPS[i].length);
writer.writeUint8Array(avccBox.PPS[i].nalu);
}
return writer.getData();
}
async ready() {
let info = await this.source.getInfo();
this.videoTrack = info.videoTracks[0];
this.audioTrack = info.audioTracks[0];
}
async getVideoTrackInfo() {
await this.ready();
let config = {
codec: this.videoTrack.codec,
displayWidth: this.videoTrack.track_width,
displayHeight: this.videoTrack.track_height,
extradata: this.getAvcDescription(this.source.getAvccBox())
}
return Promise.resolve(config);
}
demuxVideo(time, onChunk) {
this.source.start(time, this.videoTrack, onChunk);
}
stop() {
console.log('stopping');
this.source.stop();
}
}