-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfaststart.js
More file actions
346 lines (282 loc) · 10.1 KB
/
faststart.js
File metadata and controls
346 lines (282 loc) · 10.1 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Logic based on http://github.com/danielgtaylor/qtfaststart/blob/master/qtfaststart/processor.py
// Implementation based on ReadStream http://github.com/joyent/node/blob/master/lib/fs.js
// Format info http://xhelmboyx.tripod.com/formats/mp4-layout.txt
var fs = require('fs'),
util = require('util'),
async = require('async'),
assert = require('assert');
var debug;
if (process.env.FASTSTART_DEBUG) {
debug = function(x) { console.error('FASTSTART: %s', x); };
} else {
debug = function() { };
}
var faststart = exports;
var Stream = require('stream').Stream;
var MAX_UINT32 = Math.pow(2, 32)-1;
// Due to platform limitations 64-bit values can't have more than 52 bits of data
var MAX_UINT64 = Math.pow(2, 52)-1;
function read_atom_header(fd, offset, callback) {
var buffer = new Buffer(16);
fs.read(fd, buffer, 0, 16, offset, function(err, bytesRead, buf) {
if (err) return callback(err);
if (bytesRead < 8) return callback();
var size = buf.readUInt32BE(0, true);
var type = buf.toString('ascii', 4, 8);
var skip = 8;
if (size === 1 && bytesRead == 16) { // 64-bit size
size = buf.readUInt32BE(8, true)*Math.pow(2, 32) + buf.readUInt32BE(12, true);
assert.ok(size <= MAX_UINT64, 'Size too large to calculate');
skip = 16;
}
callback(null, {type: type, offset: offset, size: size, skip: skip});
});
};
function get_index(fd, callback) {
var index = [];
var moov, mdat;
read_atom_header(fd, 0, function check(err, atom) {
if (err) return callback(err);
if (!atom) return callback(null, index, moov, mdat);
if (atom.offset === 0 && atom.type !== 'ftyp')
return callback(new ParseError('Unknown/Unsupported file type.'));
if (atom.type === 'moov') {
if (moov) return callback(new ParseError('Multiple "moov" fragments.'));
moov = index.length;
}
if (atom.type === 'mdat') {
if (mdat) return callback(new ParseError('Multiple "mdat" fragments.'));
mdat = index.length;
}
// mdat atoms are allowed to have 0 size if they are at the end
if (atom.size === 0 && atom.type === 'mdat') {
// calculate the actual size
fs.fstat(fd, function(err, stats) {
if (err) return callback(err);
atom.size = stats.size - atom.offset;
index.push(atom);
return callback(err, index, moov, mdat); // We're done
});
}
if (atom.size < 8) // ensure we don't end in an infinite loop
return callback(new ParseError('Invalid atom size (less than 8 bytes).'));
// TODO: limit index size?
index.push(atom);
read_atom_header(fd, atom.offset + atom.size, check);
});
}
function mirror_events(events, src, dst) {
events.forEach(function(event) {
src.listeners(event).forEach(function(listener) {
dst.addListener(event, listener);
});
});
src.on('newListener', function(event, listener) {
if (events.indexOf(event) !== -1)
dst.addListener(event, listener);
});
var srcRemoveListener = src.removeListener;
src.removeListener = function(type, listener) {
srcRemoveListener.apply(src, arguments);
if (events.indexOf(type) !== -1) {
dst.removeListener.apply(dst, arguments);
}
}
}
faststart.createReadStream = function(path, options) {
return new FSStream(path, options);
};
var FSStream = faststart.FSStream = function(path, options) {
if (!(this instanceof FSStream)) return new FSStream(path, options);
Stream.call(this);
var self = this;
this.path = path;
this.fd = null;
this.readable = true;
this.paused = false;
this.flags = 'r';
this.mode = 438; /*=0666*/
this.bufferSize = 64 * 1024;
// FSStream specific options
this.passthrough = false; // passthrough any file
options = options || {};
// Mixin options into this
var keys = Object.keys(options);
for (var index = 0, length = keys.length; index < length; index++) {
var key = keys[index];
this[key] = options[key];
}
// FIXME: how should encoding be handled?
if (this.encoding) this.setEncoding(this.encoding);
this.offset = 0;
// this.encoding = 'binary';
function clone_stream(start, end) {
self._stream = fs.createReadStream(self._path, {
fd: self.fd, mode: self.mode, bufferSize: self.bufferSize,
start: start, end: end
});
mirror_events(['data', 'close'], self, self._stream);
self._stream.on('end', function() {
self.emit('end');
self.destroy();
});
self._stream.on('error', self._emitError);
if (self.paused) self.pause();
if (self._pipedst) self.pipe(self._pipedst, self._pipeopts);
}
fs.open(this.path, 'r', function(err, fd) {
if (err) return self._emitError(err);
self.fd = fd;
self.emit('open', fd);
get_index(fd, function(err, index, moov, mdat) {
if (!self.readable) return;
if (err && !(self.passthrough && err instanceof ParseError))
return self._emitError(err);
if (index && (!moov || !mdat))
return self._emitError(new ParseError('Missing moov/mdat atom.'));
if (!index) {
debug('Passthrough');
clone_stream();
} else if (index[moov].offset < index[mdat].offset) {
debug('File is already fast start');
clone_stream();
} else {
// re-arrange index
var new_index = [index[0]];
new_index = new_index.concat(index.slice(moov)); // moov + any extra
var stream_index = index.slice(1, moov); // mdat + remaining
var first = new_index[1]
var last = new_index.slice(-1)[0];
self.offset = (last.offset + last.size) - first.offset;
process.nextTick(function() {
async.forEachSeries(new_index, function(atom, next) {
self._read(atom, next);
}, function(err) {
if (err) return self._emitError(err);
var first = stream_index[0]
var last = stream_index.slice(-1)[0];
clone_stream(first.offset, last.offset + last.size - 1);
});
});
}
});
});
}
util.inherits(FSStream, Stream);
// Helper functions
FSStream.prototype._emitError = function(err) {
this.emit('error', err);
this.readable = false;
}
FSStream.prototype._read = function(base_atom, callback) {
var self = this;
var end = base_atom.offset + base_atom.size;
var process_atoms = ['moov', 'trak', 'mdia', 'minf', 'stbl'];
if (process_atoms.indexOf(base_atom.type) === -1) {
var buffer = new Buffer(base_atom.size);
fs.read(self.fd, buffer, 0, base_atom.size, base_atom.offset, function(err, bytesRead, buf) {
if (err) return callback(err);
if (self.offset) {
if (base_atom.type === 'stco') {
var count = buf.readUInt32BE(12, true);
for (var i=0, pos=16; i<count; i++, pos+=4) {
var offset = buf.readUInt32BE(pos, true)+self.offset;
if (offset > MAX_UINT32)
return callback(new Error('Bad input file: "stco" atom found, but "co64" is required for fast start.'));
buf.writeUInt32BE(offset, pos, true);
}
} else if (base_atom.type === 'co64') {
var count = buf.readUInt32BE(12, true);
for (var i=0, pos=16; i<count; i++, pos+=8) {
// 64-bit 2's-complement math (with assumptions for speed)
var hi = buf.readUInt32BE(pos, true);
var lo = buf.readUInt32BE(pos+4, true) + self.offset;
while (lo > MAX_UINT32) {
lo -= MAX_UINT32+1;
hi += 1;
}
buf.writeUInt32BE(hi, pos, true);
buf.writeUInt32BE(low, pos+4, true);
}
}
}
self.emit('data', buf);
callback(null);
});
return;
}
// emit container atom header
var buf = new Buffer(base_atom.skip);
if (base_atom.skip == 16) {
buf.writeUInt32BE(1, 0, true);
buf.writeUInt32BE(base_atom.size>>32, 8, true);
buf.writeUInt32BE(base_atom.size, 12, true);
} else {
buf.writeUInt32BE(base_atom.size, 0, true);
}
buf.write(base_atom.type, 4, 4, 'ascii');
self.emit('data', buf);
// read and parse sub-atoms
read_atom_header(self.fd, base_atom.offset+base_atom.skip, function handle_atom(err, atom) {
if (err || !atom) return callback(err);
var atom_end = atom.offset + atom.size;
if (atom_end > end)
return callback(new ParseError('Invalid atom structure.'));
if (atom.size < 8) // ensure we don't end in an infinite loop
return callback(new ParseError('Invalid atom size (less than 8 bytes).'));
self._read(atom, function(err) {
if (err) return callback(err);
if (atom_end === end)
return callback();
read_atom_header(self.fd, atom_end, handle_atom);
});
});
}
// Stream API http://nodejs.org/api/stream.html
FSStream.prototype.pause = function() {
this.paused = true;
if (this._stream) this._stream.pause.apply(this._stream, arguments);
}
FSStream.prototype.resume = function() {
this.paused = false;
if (this._stream) this._stream.resume.apply(this._stream, arguments);
}
FSStream.prototype.destroy = function(cb) {
var self = this;
if (!this.readable) {
if (cb) process.nextTick(function() { cb(null); });
return;
}
this.readable = false;
if (this._stream) return this._stream.destroy.apply(this._stream, arguments);
function close() {
fs.close(self.fd, function(err) {
if (err) {
if (cb) cb(err);
self.emit('error', err);
return;
}
if (cb) cb(null);
self.emit('close');
});
}
if (this.fd === null) {
this.addListener('open', close);
} else {
close();
}
}
// FIXME: is there really any point in overriding the pipe implementation?
/*FSStream.prototype.pipe = function(dest, opts) {
if (this._stream) return Stream.prototype.pipe.call(this._stream, dest, opts)
// FIXME: should we close this if we never make a stream?
this._pipedst = dest;
this._pipeopts = opts;
return dest;
}*/
var ParseError = faststart.ParseError = function(msg, constr) {
Error.captureStackTrace(this, constr || this)
this.message = msg || 'Error'
}
util.inherits(ParseError, Error)
ParseError.prototype.name = 'Parser Error'