-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmp4_pull_demuxer.js
246 lines (195 loc) · 6.35 KB
/
mp4_pull_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
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
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.debug('fetching file');
fetch(uri).then(response => {
console.debug('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
}
getAudioSpecificConfig() {
// TODO: make sure this is coming from the right track.
// 0x04 is the DecoderConfigDescrTag. Assuming MP4Box always puts this at position 0.
console.assert(this.file.moov.traks[0].mdia.minf.stbl.stsd.entries[0].esds.esd.descs[0].tag == 0x04);
// 0x40 is the Audio OTI, per table 5 of ISO 14496-1
console.assert(this.file.moov.traks[0].mdia.minf.stbl.stsd.entries[0].esds.esd.descs[0].oti == 0x40);
// 0x05 is the DecSpecificInfoTag
console.assert(this.file.moov.traks[0].mdia.minf.stbl.stsd.entries[0].esds.esd.descs[0].descs[0].tag == 0x05);
return this.file.moov.traks[0].mdia.minf.stbl.stsd.entries[0].esds.esd.descs[0].descs[0].data;
}
selectTrack(track) {
console.debug('selecting track %d', track.id);
this.file.setExtractionOptions(track.id);
}
start(onSamples) {
// console.debug('starting source');
this._onSamples = onSamples;
this.file.start();
}
stop() {
this.file.stop();
}
onSamples(track_id, ref, samples) {
this._onSamples(samples);
}
}
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 MP4PullDemuxer {
constructor(uri) {
this.source = new MP4Source(uri);
this.readySamples = [];
this._pending_read_resolver = null;
}
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 getAudioTrackInfo() {
await this.ready();
let info = {
codec: this.audioTrack.codec,
numberOfChannels: this.audioTrack.audio.channel_count,
sampleRate: this.audioTrack.audio.sample_rate,
extradata: this.source.getAudioSpecificConfig()
};
return info;
}
async getVideoTrackInfo() {
await this.ready();
let info = {
codec: this.videoTrack.codec,
displayWidth: this.videoTrack.track_width,
displayHeight: this.videoTrack.track_height,
extradata: this.getAvcDescription(this.source.getAvccBox())
}
return Promise.resolve(info);
}
selectTrack(track) {
console.assert(!this.selectedTrack, "changing tracks is not implemented");
this.selectedTrack = track;
this.source.selectTrack(track);
}
selectVideo() {
this.selectTrack(this.videoTrack);
}
selectAudio() {
this.selectTrack(this.audioTrack);
}
async readSample() {
console.assert(this.selectedTrack);
console.assert(!this._pending_read_resolver);
if (this.readySamples.length) {
return Promise.resolve(this.readySamples.shift());
}
let promise = new Promise((resolver) => { this._pending_read_resolver = resolver; });
console.assert(this._pending_read_resolver);
this.source.start(this.onSamples.bind(this));
return promise;
}
onSamples(samples) {
const SAMPLE_BUFFER_TARGET_SIZE = 50;
this.readySamples.push(...samples);
if (this.readySamples.length >= SAMPLE_BUFFER_TARGET_SIZE)
this.source.stop();
let firstSampleTime = samples[0].cts * 1000000 / samples[0].timescale ;
console.debug('adding new %d samples (first = %d). total = %d', samples.length, firstSampleTime, this.readySamples.length);
if (this._pending_read_resolver) {
this._pending_read_resolver(this.readySamples.shift());
this._pending_read_resolver = null;
}
}
}