|
|
@@ -1,28 +1,27 @@
|
|
|
-/*jslint node: true */
|
|
|
+'use strict';
|
|
|
|
|
|
-"use strict";
|
|
|
+const Fs = require('fs');
|
|
|
+const Path = require('path');
|
|
|
+const Url = require('url');
|
|
|
|
|
|
-var fs = require('fs'),
|
|
|
- path = require('path'),
|
|
|
- url = require('url'),
|
|
|
- util = require('util');
|
|
|
+const Mime = require('mime-types');
|
|
|
+const StreamEach = require('stream-each');
|
|
|
+const Oncemore = require('oncemore');
|
|
|
+const M3U8Parse = require('m3u8parse');
|
|
|
+const Mkdirp = require('mkdirp');
|
|
|
+const writeFileAtomic = require('write-file-atomic');
|
|
|
+const debug = require('debug')('hls:recorder');
|
|
|
|
|
|
-var mime = require('mime-types'),
|
|
|
- StreamEach = require('stream-each'),
|
|
|
- oncemore = require('oncemore'),
|
|
|
- M3U8Parse = require('m3u8parse'),
|
|
|
- mkdirp = require('mkdirp'),
|
|
|
- writeFileAtomic = require('write-file-atomic'),
|
|
|
- debug = require('debug')('hls:recorder');
|
|
|
+const SegmentDecrypt = require('./segment-decrypt');
|
|
|
|
|
|
-var SegmentDecrypt = require('./segment-decrypt');
|
|
|
|
|
|
// add custom extensions
|
|
|
-mime.extensions['audio/aac'] = ['aac'];
|
|
|
-mime.extensions['audio/ac3'] = ['ac3'];
|
|
|
+Mime.extensions['audio/aac'] = ['aac'];
|
|
|
+Mime.extensions['audio/ac3'] = ['ac3'];
|
|
|
|
|
|
|
|
|
function HlsStreamRecorder(reader, dst, options) {
|
|
|
+
|
|
|
options = options || {};
|
|
|
|
|
|
this.reader = reader;
|
|
|
@@ -41,9 +40,11 @@ function HlsStreamRecorder(reader, dst, options) {
|
|
|
}
|
|
|
|
|
|
HlsStreamRecorder.prototype.start = function() {
|
|
|
+
|
|
|
// TODO: make async?
|
|
|
- if (!fs.existsSync(this.dst))
|
|
|
- mkdirp.sync(this.dst);
|
|
|
+ if (!Fs.existsSync(this.dst)) {
|
|
|
+ Mkdirp.sync(this.dst);
|
|
|
+ }
|
|
|
|
|
|
StreamEach(this.reader, this.process.bind(this));
|
|
|
|
|
|
@@ -52,9 +53,10 @@ HlsStreamRecorder.prototype.start = function() {
|
|
|
};
|
|
|
|
|
|
HlsStreamRecorder.prototype.updateIndex = function(update) {
|
|
|
- var self = this;
|
|
|
|
|
|
- if (!update) return;
|
|
|
+ if (!update) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
if (!this.index) {
|
|
|
this.index = new M3U8Parse.M3U8Playlist(update);
|
|
|
@@ -70,18 +72,20 @@ HlsStreamRecorder.prototype.updateIndex = function(update) {
|
|
|
this.index.ended = false;
|
|
|
this.index.discontinuity_sequence = 0; // not allowed in event playlists
|
|
|
if (!isNaN(this.startOffset)) {
|
|
|
- var offset = this.startOffset;
|
|
|
+ let offset = this.startOffset;
|
|
|
if (!update.ended) {
|
|
|
if (offset < 0) offset = Math.min(offset, -3 * this.index.target_duration);
|
|
|
}
|
|
|
this.index.start.decimalInteger('time-offset', offset);
|
|
|
}
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else {
|
|
|
debug('variants', this.index.variants);
|
|
|
if (this.subreader) {
|
|
|
// remove backup sources
|
|
|
let used = {};
|
|
|
this.index.variants = this.index.variants.filter((variant) => {
|
|
|
+
|
|
|
let bw = parseInt(variant.info.bandwidth, 10);
|
|
|
let res = !(bw in used);
|
|
|
used[bw] = true;
|
|
|
@@ -89,15 +93,16 @@ HlsStreamRecorder.prototype.updateIndex = function(update) {
|
|
|
});
|
|
|
|
|
|
this.index.variants.forEach((variant, index) => {
|
|
|
- var variantUrl = url.resolve(self.reader.baseUrl, variant.uri);
|
|
|
+
|
|
|
+ let variantUrl = Url.resolve(this.reader.baseUrl, variant.uri);
|
|
|
debug('url', variantUrl);
|
|
|
|
|
|
// check for duplicate source urls
|
|
|
- var rec = this.recorderForUrl(variantUrl);
|
|
|
+ let rec = this.recorderForUrl(variantUrl);
|
|
|
if (!rec || !rec.localUrl) {
|
|
|
- var dir = self.variantName(variant.info, index);
|
|
|
- rec = new HlsStreamRecorder(self.subreader(variantUrl), path.join(self.dst, dir), { startOffset: self.startOffset, collect: self.collect, decrypt: this.decrypt });
|
|
|
- rec.localUrl = url.format({pathname: path.join(dir, 'index.m3u8')});
|
|
|
+ let dir = this.variantName(variant.info, index);
|
|
|
+ rec = new HlsStreamRecorder(this.subreader(variantUrl), Path.join(this.dst, dir), { startOffset: this.startOffset, collect: this.collect, decrypt: this.decrypt });
|
|
|
+ rec.localUrl = Url.format({pathname: Path.join(dir, 'index.m3u8')});
|
|
|
rec.remoteUrl = variantUrl;
|
|
|
|
|
|
this.recorders.push(rec);
|
|
|
@@ -106,21 +111,22 @@ HlsStreamRecorder.prototype.updateIndex = function(update) {
|
|
|
variant.uri = rec.localUrl;
|
|
|
});
|
|
|
|
|
|
- var allGroups = [];
|
|
|
- for (var group in this.index.groups)
|
|
|
- [].push.apply(allGroups, this.index.groups[group]);
|
|
|
+ let allGroups = [];
|
|
|
+ for (let group in this.index.groups)
|
|
|
+ Array.prototype.push.apply(allGroups, this.index.groups[group]);
|
|
|
+
|
|
|
+ allGroups.forEach((groupItem, index) => {
|
|
|
|
|
|
- allGroups.forEach(function(groupItem, index) {
|
|
|
- var srcUri = groupItem.quotedString('uri');
|
|
|
+ let srcUri = groupItem.quotedString('uri');
|
|
|
if (srcUri) {
|
|
|
- var itemUrl = url.resolve(self.reader.baseUrl, srcUri);
|
|
|
+ let itemUrl = Url.resolve(this.reader.baseUrl, srcUri);
|
|
|
debug('url', itemUrl);
|
|
|
|
|
|
- var rec = this.recorderForUrl(itemUrl);
|
|
|
+ let rec = this.recorderForUrl(itemUrl);
|
|
|
if (!rec || !rec.localUrl) {
|
|
|
- var dir = self.groupSrcName(groupItem, index);
|
|
|
- rec = new HlsStreamRecorder(self.subreader(itemUrl), path.join(self.dst, dir), { startOffset: self.startOffset, collect: self.collect, decrypt: this.decrypt });
|
|
|
- rec.localUrl = url.format({pathname: path.join(dir, 'index.m3u8')});
|
|
|
+ let dir = this.groupSrcName(groupItem, index);
|
|
|
+ rec = new HlsStreamRecorder(this.subreader(itemUrl), Path.join(this.dst, dir), { startOffset: this.startOffset, collect: this.collect, decrypt: this.decrypt });
|
|
|
+ rec.localUrl = Url.format({pathname: Path.join(dir, 'index.m3u8')});
|
|
|
rec.remoteUrl = itemUrl;
|
|
|
|
|
|
this.recorders.push(rec);
|
|
|
@@ -128,15 +134,16 @@ HlsStreamRecorder.prototype.updateIndex = function(update) {
|
|
|
|
|
|
groupItem.quotedString('uri', rec.localUrl);
|
|
|
}
|
|
|
- }, this);
|
|
|
+ });
|
|
|
|
|
|
// start all recordings
|
|
|
- this.recorders.forEach(function(recording) {
|
|
|
+ this.recorders.forEach((recording) => {
|
|
|
recording.start();
|
|
|
});
|
|
|
|
|
|
this.index.iframes = [];
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else {
|
|
|
this.index.variants = [];
|
|
|
this.index.groups = {};
|
|
|
this.index.iframes = [];
|
|
|
@@ -144,9 +151,11 @@ HlsStreamRecorder.prototype.updateIndex = function(update) {
|
|
|
}
|
|
|
|
|
|
// hook end listener
|
|
|
- this.reader.on('end', function() {
|
|
|
- self.index.ended = true;
|
|
|
- self.flushIndex(function(/*err*/) {
|
|
|
+ this.reader.on('end', () => {
|
|
|
+
|
|
|
+ this.index.ended = true;
|
|
|
+ this.flushIndex((/*err*/) => {
|
|
|
+
|
|
|
debug('done');
|
|
|
});
|
|
|
});
|
|
|
@@ -157,119 +166,137 @@ HlsStreamRecorder.prototype.updateIndex = function(update) {
|
|
|
}
|
|
|
|
|
|
// validate update
|
|
|
- if (this.index.target_duration > update.target_duration)
|
|
|
+ if (this.index.target_duration > update.target_duration) {
|
|
|
throw new Error('Invalid index');
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
HlsStreamRecorder.prototype.process = function(segmentInfo, done) {
|
|
|
- var self = this;
|
|
|
|
|
|
- var segment = new M3U8Parse.M3U8Segment(segmentInfo.details, true);
|
|
|
- var meta = segmentInfo.file;
|
|
|
+ let segment = new M3U8Parse.M3U8Segment(segmentInfo.details, true);
|
|
|
+ let meta = segmentInfo.file;
|
|
|
|
|
|
// mark discontinuities
|
|
|
if (this.nextSegmentSeq !== -1 &&
|
|
|
- this.nextSegmentSeq !== segmentInfo.seq)
|
|
|
+ this.nextSegmentSeq !== segmentInfo.seq) {
|
|
|
segment.discontinuity = true;
|
|
|
+ }
|
|
|
this.nextSegmentSeq = segmentInfo.seq + 1;
|
|
|
|
|
|
// create our own uri
|
|
|
- segment.uri = util.format('%s.%s', this.segmentName(this.seq), mime.extension(meta.mime));
|
|
|
+ segment.uri = `${this.segmentName(this.seq)}.${Mime.extension(meta.mime)}`;
|
|
|
|
|
|
// handle byterange
|
|
|
- var first = self.index.segments.length === 0;
|
|
|
- var newFile = first || self.index.segments[self.index.segments.length - 1].uri !== segment.uri;
|
|
|
+ let first = this.index.segments.length === 0;
|
|
|
+ let newFile = first || this.index.segments[this.index.segments.length - 1].uri !== segment.uri;
|
|
|
if (this.collect) {
|
|
|
segment.byterange = {
|
|
|
length: 0,
|
|
|
offset: newFile ? 0 : null
|
|
|
}
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else {
|
|
|
delete segment.byterange;
|
|
|
}
|
|
|
|
|
|
// save the stream segment
|
|
|
- SegmentDecrypt.decrypt(segmentInfo.stream, segmentInfo.details.keys, this.decrypt, function (err, stream, decrypted) {
|
|
|
+ SegmentDecrypt.decrypt(segmentInfo.stream, segmentInfo.details.keys, this.decrypt, (err, stream, decrypted) => {
|
|
|
+
|
|
|
if (err) {
|
|
|
console.error('decrypt failed', err.stack);
|
|
|
stream = segmentInfo.stream;
|
|
|
- } else if (decrypted) {
|
|
|
+ }
|
|
|
+ else if (decrypted) {
|
|
|
segment.keys = null;
|
|
|
}
|
|
|
|
|
|
- stream = oncemore(stream);
|
|
|
- stream.pipe(fs.createWriteStream(path.join(self.dst, segment.uri), { flags: newFile ? 'w' : 'a' }));
|
|
|
+ stream = Oncemore(stream);
|
|
|
+ stream.pipe(Fs.createWriteStream(Path.join(this.dst, segment.uri), { flags: newFile ? 'w' : 'a' }));
|
|
|
+
|
|
|
+ let bytesWritten = 0;
|
|
|
+ if (this.collect) {
|
|
|
+ stream.on('data', (chunk) => {
|
|
|
|
|
|
- var bytesWritten = 0;
|
|
|
- if (self.collect) {
|
|
|
- stream.on('data', function(chunk) {
|
|
|
bytesWritten += chunk.length;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- stream.once('end', 'error', function(err) {
|
|
|
+ stream.once('end', 'error', (err) => {
|
|
|
+
|
|
|
// only to report errors
|
|
|
if (err) debug('stream error', err.stack || err);
|
|
|
|
|
|
- if (segment.byterange)
|
|
|
+ if (segment.byterange) {
|
|
|
segment.byterange.length = bytesWritten;
|
|
|
+ }
|
|
|
|
|
|
// update index
|
|
|
- self.index.segments.push(segment);
|
|
|
- self.flushIndex(done);
|
|
|
+ this.index.segments.push(segment);
|
|
|
+ this.flushIndex(done);
|
|
|
});
|
|
|
|
|
|
- self.seq++;
|
|
|
+ this.seq++;
|
|
|
});
|
|
|
};
|
|
|
|
|
|
HlsStreamRecorder.prototype.variantName = function(info, index) {
|
|
|
- return util.format('v%d', index);
|
|
|
+
|
|
|
+ return `v${index}`;
|
|
|
};
|
|
|
|
|
|
HlsStreamRecorder.prototype.groupSrcName = function(info, index) {
|
|
|
- var lang = (info.quotedString('language') || '').replace(/\W/g, '').toLowerCase();
|
|
|
- var id = (info.quotedString('group-id') || 'unk').replace(/\W/g, '').toLowerCase();
|
|
|
- return util.format('grp/%s/%s%d', id, lang ? lang + '-' : '', index);
|
|
|
+
|
|
|
+ let lang = (info.quotedString('language') || '').replace(/\W/g, '').toLowerCase();
|
|
|
+ let id = (info.quotedString('group-id') || 'unk').replace(/\W/g, '').toLowerCase();
|
|
|
+ return `grp/${id}/${lang ? lang + '-' : ''}${index}`;
|
|
|
};
|
|
|
|
|
|
HlsStreamRecorder.prototype.segmentName = function(seqNo) {
|
|
|
- function name(n) {
|
|
|
- var next = ~~(n / 26);
|
|
|
- var chr = String.fromCharCode(97 + n % 26); // 'a' + n
|
|
|
+
|
|
|
+ const name = (n) => {
|
|
|
+
|
|
|
+ let next = ~~(n / 26);
|
|
|
+ let chr = String.fromCharCode(97 + n % 26); // 'a' + n
|
|
|
if (next) return name(next - 1) + chr;
|
|
|
return chr;
|
|
|
- }
|
|
|
+ };
|
|
|
+
|
|
|
return this.collect ? 'stream' : name(seqNo);
|
|
|
};
|
|
|
|
|
|
HlsStreamRecorder.prototype.flushIndex = function(cb) {
|
|
|
- var appendString, indexString = this.index.toString().trim();
|
|
|
+
|
|
|
+ let appendString, indexString = this.index.toString().trim();
|
|
|
if (this.lastIndexString && indexString.lastIndexOf(this.lastIndexString, 0) === 0) {
|
|
|
- var lastLength = this.lastIndexString.length;
|
|
|
+ let lastLength = this.lastIndexString.length;
|
|
|
appendString = indexString.substr(lastLength);
|
|
|
}
|
|
|
this.lastIndexString = indexString;
|
|
|
|
|
|
if (appendString) {
|
|
|
- fs.appendFile(path.join(this.dst, 'index.m3u8'), appendString, cb);
|
|
|
- } else {
|
|
|
- writeFileAtomic(path.join(this.dst, 'index.m3u8'), indexString, cb);
|
|
|
+ Fs.appendFile(Path.join(this.dst, 'index.m3u8'), appendString, cb);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ writeFileAtomic(Path.join(this.dst, 'index.m3u8'), indexString, cb);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
HlsStreamRecorder.prototype.recorderForUrl = function(remoteUrl) {
|
|
|
- var idx, len = this.recorders.length;
|
|
|
+
|
|
|
+ let idx, len = this.recorders.length;
|
|
|
for (idx = 0; idx < len; idx++) {
|
|
|
- var rec = this.recorders[idx];
|
|
|
- if (rec.remoteUrl === remoteUrl)
|
|
|
+ let rec = this.recorders[idx];
|
|
|
+ if (rec.remoteUrl === remoteUrl) {
|
|
|
return rec;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
return null;
|
|
|
};
|
|
|
|
|
|
|
|
|
-var hlsrecorder = module.exports = function hlsrecorder(reader, dst, options) {
|
|
|
+const hlsrecorder = module.exports = function hlsrecorder(reader, dst, options) {
|
|
|
+
|
|
|
return new HlsStreamRecorder(reader, dst, options);
|
|
|
};
|
|
|
|