recorder.js 9.2 KB

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