recorder.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. 'use strict';
  2. const Fs = require('fs');
  3. const Path = require('path');
  4. const Url = require('url');
  5. const Mime = require('mime-types');
  6. const StreamEach = require('stream-each');
  7. const Oncemore = require('oncemore');
  8. const M3U8Parse = require('m3u8parse');
  9. const Mkdirp = require('mkdirp');
  10. const writeFileAtomic = require('write-file-atomic');
  11. const debug = require('debug')('hls:recorder');
  12. const SegmentDecrypt = require('./segment-decrypt');
  13. // add custom extensions
  14. Mime.extensions['audio/aac'] = ['aac'];
  15. Mime.extensions['audio/ac3'] = ['ac3'];
  16. function HlsStreamRecorder(reader, dst, options) {
  17. options = options || {};
  18. this.reader = reader;
  19. this.dst = dst; // target directory
  20. this.nextSegmentSeq = -1;
  21. this.seq = 0;
  22. this.index = null;
  23. this.startOffset = parseFloat(options.startOffset);
  24. this.subreader = options.subreader;
  25. this.collect = !!options.collect; // collect into a single file (v4 feature)
  26. this.decrypt = options.decrypt;
  27. this.recorders = [];
  28. this.mapSeq = 0;
  29. this.nextMap = null;
  30. this.writing = null; // tracks writing state
  31. }
  32. HlsStreamRecorder.prototype.start = function() {
  33. // TODO: make async?
  34. if (!Fs.existsSync(this.dst)) {
  35. Mkdirp.sync(this.dst);
  36. }
  37. StreamEach(this.reader, this.process.bind(this));
  38. this.updateIndex(this.reader.index);
  39. this.reader.on('index', this.updateIndex.bind(this));
  40. };
  41. HlsStreamRecorder.prototype.updateIndex = function(update) {
  42. if (!update) {
  43. return;
  44. }
  45. if (!this.index) {
  46. this.index = new M3U8Parse.M3U8Playlist(update);
  47. if (!this.index.master) {
  48. if (this.collect)
  49. this.index.version = Math.max(4, this.index.version); // v4 is required for byterange support
  50. this.index.version = Math.max(2, this.index.version); // v2 is required to support the remapped IV attribute
  51. if (this.index.version !== update.version)
  52. debug('changed index version to:', this.index.version);
  53. this.index.segments = [];
  54. this.index.first_seq_no = this.seq;
  55. this.index.type = 'EVENT';
  56. this.index.ended = false;
  57. this.index.discontinuity_sequence = 0; // not allowed in event playlists
  58. if (!isNaN(this.startOffset)) {
  59. let offset = this.startOffset;
  60. if (!update.ended) {
  61. if (offset < 0) offset = Math.min(offset, -3 * this.index.target_duration);
  62. }
  63. this.index.start.decimalInteger('time-offset', offset);
  64. }
  65. }
  66. else {
  67. debug('variants', this.index.variants);
  68. if (this.subreader) {
  69. // remove backup sources
  70. let used = {};
  71. this.index.variants = this.index.variants.filter((variant) => {
  72. let bw = parseInt(variant.info.bandwidth, 10);
  73. let res = !(bw in used);
  74. used[bw] = true;
  75. return res;
  76. });
  77. this.index.variants.forEach((variant, index) => {
  78. let variantUrl = Url.resolve(this.reader.baseUrl, variant.uri);
  79. debug('url', variantUrl);
  80. // check for duplicate source urls
  81. let rec = this.recorderForUrl(variantUrl);
  82. if (!rec || !rec.localUrl) {
  83. let dir = this.variantName(variant.info, index);
  84. rec = new HlsStreamRecorder(this.subreader(variantUrl), Path.join(this.dst, dir), { startOffset: this.startOffset, collect: this.collect, decrypt: this.decrypt });
  85. rec.localUrl = Url.format({pathname: Path.join(dir, 'index.m3u8')});
  86. rec.remoteUrl = variantUrl;
  87. this.recorders.push(rec);
  88. }
  89. variant.uri = rec.localUrl;
  90. });
  91. let allGroups = [];
  92. for (let group in this.index.groups)
  93. Array.prototype.push.apply(allGroups, this.index.groups[group]);
  94. allGroups.forEach((groupItem, index) => {
  95. let srcUri = groupItem.quotedString('uri');
  96. if (srcUri) {
  97. let itemUrl = Url.resolve(this.reader.baseUrl, srcUri);
  98. debug('url', itemUrl);
  99. let rec = this.recorderForUrl(itemUrl);
  100. if (!rec || !rec.localUrl) {
  101. let dir = this.groupSrcName(groupItem, index);
  102. rec = new HlsStreamRecorder(this.subreader(itemUrl), Path.join(this.dst, dir), { startOffset: this.startOffset, collect: this.collect, decrypt: this.decrypt });
  103. rec.localUrl = Url.format({pathname: Path.join(dir, 'index.m3u8')});
  104. rec.remoteUrl = itemUrl;
  105. this.recorders.push(rec);
  106. }
  107. groupItem.quotedString('uri', rec.localUrl);
  108. }
  109. });
  110. // start all recordings
  111. this.recorders.forEach((recording) => {
  112. recording.start();
  113. });
  114. this.index.iframes = [];
  115. }
  116. else {
  117. this.index.variants = [];
  118. this.index.groups = {};
  119. this.index.iframes = [];
  120. }
  121. }
  122. // hook end listener
  123. this.reader.on('end', () => {
  124. this.index.ended = true;
  125. this.flushIndex((/*err*/) => {
  126. debug('done');
  127. });
  128. });
  129. if (this.decrypt) {
  130. this.decrypt.base = this.reader.baseUrl;
  131. }
  132. }
  133. // validate update
  134. if (this.index.target_duration > update.target_duration) {
  135. throw new Error('Invalid index');
  136. }
  137. };
  138. HlsStreamRecorder.prototype.process = function (segmentInfo, next) {
  139. if (segmentInfo.type === 'segment') {
  140. return this.processSegment(segmentInfo, next);
  141. }
  142. if (segmentInfo.type === 'init') {
  143. return this.processInfo(segmentInfo, next);
  144. }
  145. debug('unknown segment type: ' + segmentInfo.type);
  146. return next();
  147. };
  148. HlsStreamRecorder.prototype.processInfo = function (segmentInfo, callback) {
  149. const meta = segmentInfo.file;
  150. const uri = `${this.segmentName(this.mapSeq, true)}.${Mime.extension(meta.mime)}`;
  151. this.writeStream(segmentInfo.stream, uri, (err, bytesWritten) => {
  152. // only to report errors
  153. if (err) debug('stream error', err.stack || err);
  154. const map = new M3U8Parse.AttrList();
  155. map.quotedString('uri', uri);
  156. // handle byterange
  157. if (this.collect) {
  158. map.quotedString('byterange', `${bytesWritten}@${this.writing.bytes - bytesWritten}`);
  159. }
  160. this.nextMap = map;
  161. return callback();
  162. });
  163. this.mapSeq++;
  164. };
  165. HlsStreamRecorder.prototype.processSegment = function (segmentInfo, callback) {
  166. let segment = new M3U8Parse.M3U8Segment(segmentInfo.segment.details, true);
  167. let meta = segmentInfo.file;
  168. // mark discontinuities
  169. if (this.nextSegmentSeq !== -1 &&
  170. this.nextSegmentSeq !== segmentInfo.segment.seq) {
  171. segment.discontinuity = true;
  172. }
  173. this.nextSegmentSeq = segmentInfo.segment.seq + 1;
  174. // create our own uri
  175. segment.uri = `${this.segmentName(this.seq)}.${Mime.extension(meta.mime)}`;
  176. // add map info
  177. if (this.nextMap) {
  178. segment.map = this.nextMap;
  179. this.nextMap = null;
  180. }
  181. delete segment.byterange;
  182. // save the stream segment
  183. SegmentDecrypt.decrypt(segmentInfo.stream, segmentInfo.segment.details.keys, this.decrypt, (err, stream, decrypted) => {
  184. if (err) {
  185. console.error('decrypt failed', err.stack);
  186. stream = segmentInfo.stream;
  187. }
  188. else if (decrypted) {
  189. segment.keys = null;
  190. }
  191. this.writeStream(stream, segment.uri, (err, bytesWritten) => {
  192. // only to report errors
  193. if (err) debug('stream error', err.stack || err);
  194. // handle byterange
  195. if (this.collect) {
  196. const isContigious = this.writing.segmentHead > 0 && ((this.writing.segmentHead + bytesWritten) === this.writing.bytes);
  197. segment.byterange = {
  198. length: bytesWritten,
  199. offset: isContigious ? null : this.writing.bytes - bytesWritten
  200. }
  201. this.writing.segmentHead = this.writing.bytes;
  202. }
  203. // update index
  204. this.index.segments.push(segment);
  205. this.flushIndex(callback);
  206. });
  207. this.seq++;
  208. });
  209. };
  210. HlsStreamRecorder.prototype.writeStream = function (stream, name, callback) {
  211. if (!this.writing || !this.collect) {
  212. this.writing = {
  213. bytes: 0,
  214. segmentHead: 0
  215. };
  216. }
  217. stream.pipe(Fs.createWriteStream(Path.join(this.dst, name), { flags: this.writing.bytes === 0 ? 'w' : 'a' }));
  218. let bytesWritten = 0;
  219. if (this.collect) {
  220. stream.on('data', (chunk) => {
  221. bytesWritten += +chunk.length;
  222. });
  223. }
  224. Oncemore(stream).once('end', 'error', (err) => {
  225. this.writing.bytes += bytesWritten;
  226. return callback(err, bytesWritten);
  227. });
  228. };
  229. HlsStreamRecorder.prototype.variantName = function(info, index) {
  230. return `v${index}`;
  231. };
  232. HlsStreamRecorder.prototype.groupSrcName = function(info, index) {
  233. let lang = (info.quotedString('language') || '').replace(/\W/g, '').toLowerCase();
  234. let id = (info.quotedString('group-id') || 'unk').replace(/\W/g, '').toLowerCase();
  235. return `grp/${id}/${lang ? lang + '-' : ''}${index}`;
  236. };
  237. HlsStreamRecorder.prototype.segmentName = function(seqNo, isInit) {
  238. const name = (n) => {
  239. let next = ~~(n / 26);
  240. let chr = String.fromCharCode(97 + n % 26); // 'a' + n
  241. if (next) return name(next - 1) + chr;
  242. return chr;
  243. };
  244. return this.collect ? 'stream' : (isInit ? 'init-' : '') + name(seqNo);
  245. };
  246. HlsStreamRecorder.prototype.flushIndex = function(cb) {
  247. let appendString, indexString = this.index.toString().trim();
  248. if (this.lastIndexString && indexString.lastIndexOf(this.lastIndexString, 0) === 0) {
  249. let lastLength = this.lastIndexString.length;
  250. appendString = indexString.substr(lastLength);
  251. }
  252. this.lastIndexString = indexString;
  253. if (appendString) {
  254. Fs.appendFile(Path.join(this.dst, 'index.m3u8'), appendString, cb);
  255. }
  256. else {
  257. writeFileAtomic(Path.join(this.dst, 'index.m3u8'), indexString, cb);
  258. }
  259. };
  260. HlsStreamRecorder.prototype.recorderForUrl = function(remoteUrl) {
  261. let idx, len = this.recorders.length;
  262. for (idx = 0; idx < len; idx++) {
  263. let rec = this.recorders[idx];
  264. if (rec.remoteUrl === remoteUrl) {
  265. return rec;
  266. }
  267. }
  268. return null;
  269. };
  270. const hlsrecorder = module.exports = function hlsrecorder(reader, dst, options) {
  271. return new HlsStreamRecorder(reader, dst, options);
  272. };
  273. hlsrecorder.HlsStreamRecorder = HlsStreamRecorder;