recorder.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*jslint node: true */
  2. "use strict";
  3. var fs = require('fs'),
  4. path = require('path'),
  5. url = require('url'),
  6. util = require('util');
  7. var mime = require('mime-types'),
  8. StreamEach = require('stream-each'),
  9. oncemore = require('oncemore'),
  10. M3U8Parse = require('m3u8parse'),
  11. mkdirp = require('mkdirp'),
  12. writeFileAtomic = require('write-file-atomic'),
  13. debug = require('debug')('hls:recorder');
  14. var SegmentDecrypt = require('./segment-decrypt');
  15. // add custom extensions
  16. mime.extensions['audio/aac'] = ['aac'];
  17. mime.extensions['audio/ac3'] = ['ac3'];
  18. function HlsStreamRecorder(reader, dst, options) {
  19. options = options || {};
  20. this.reader = reader;
  21. this.dst = dst; // target directory
  22. this.nextSegmentSeq = -1;
  23. this.seq = 0;
  24. this.index = null;
  25. this.startOffset = parseFloat(options.startOffset);
  26. this.subreader = options.subreader;
  27. this.collect = !!options.collect; // collect into a single file (v4 feature)
  28. this.decrypt = options.decrypt;
  29. this.recorders = [];
  30. }
  31. HlsStreamRecorder.prototype.start = function() {
  32. // TODO: make async?
  33. if (!fs.existsSync(this.dst))
  34. mkdirp.sync(this.dst);
  35. StreamEach(this.reader, this.process.bind(this));
  36. this.updateIndex(this.reader.index);
  37. this.reader.on('index', this.updateIndex.bind(this));
  38. };
  39. HlsStreamRecorder.prototype.updateIndex = function(update) {
  40. var self = this;
  41. if (!update) return;
  42. if (!this.index) {
  43. this.index = new M3U8Parse.M3U8Playlist(update);
  44. if (!this.index.master) {
  45. if (this.collect)
  46. this.index.version = Math.max(4, this.index.version); // v4 is required for byterange support
  47. this.index.version = Math.max(2, this.index.version); // v2 is required to support the remapped IV attribute
  48. if (this.index.version !== update.version)
  49. debug('changed index version to:', this.index.version);
  50. this.index.segments = [];
  51. this.index.first_seq_no = this.seq;
  52. this.index.type = 'EVENT';
  53. this.index.ended = false;
  54. this.index.discontinuity_sequence = 0; // not allowed in event playlists
  55. if (!isNaN(this.startOffset)) {
  56. var offset = this.startOffset;
  57. if (!update.ended) {
  58. if (offset < 0) offset = Math.min(offset, -3 * this.index.target_duration);
  59. }
  60. this.index.start.decimalInteger('time-offset', offset);
  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. var variantUrl = url.resolve(self.reader.baseUrl, variant.uri);
  75. debug('url', variantUrl);
  76. // check for duplicate source urls
  77. var rec = this.recorderForUrl(variantUrl);
  78. if (!rec || !rec.localUrl) {
  79. var dir = self.variantName(variant.info, index);
  80. rec = new HlsStreamRecorder(self.subreader(variantUrl), path.join(self.dst, dir), { startOffset: self.startOffset, collect: self.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. var allGroups = [];
  88. for (var group in this.index.groups)
  89. [].push.apply(allGroups, this.index.groups[group]);
  90. allGroups.forEach(function(groupItem, index) {
  91. var srcUri = groupItem.quotedString('uri');
  92. if (srcUri) {
  93. var itemUrl = url.resolve(self.reader.baseUrl, srcUri);
  94. debug('url', itemUrl);
  95. var rec = this.recorderForUrl(itemUrl);
  96. if (!rec || !rec.localUrl) {
  97. var dir = self.groupSrcName(groupItem, index);
  98. rec = new HlsStreamRecorder(self.subreader(itemUrl), path.join(self.dst, dir), { startOffset: self.startOffset, collect: self.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. }, this);
  106. // start all recordings
  107. this.recorders.forEach(function(recording) {
  108. recording.start();
  109. });
  110. this.index.iframes = [];
  111. } else {
  112. this.index.variants = [];
  113. this.index.groups = {};
  114. this.index.iframes = [];
  115. }
  116. }
  117. // hook end listener
  118. this.reader.on('end', function() {
  119. self.index.ended = true;
  120. self.flushIndex(function(/*err*/) {
  121. debug('done');
  122. });
  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. HlsStreamRecorder.prototype.process = function(segmentInfo, done) {
  133. var self = this;
  134. var segment = new M3U8Parse.M3U8Segment(segmentInfo.details, true);
  135. var meta = segmentInfo.file;
  136. // mark discontinuities
  137. if (this.nextSegmentSeq !== -1 &&
  138. this.nextSegmentSeq !== segmentInfo.seq)
  139. segment.discontinuity = true;
  140. this.nextSegmentSeq = segmentInfo.seq + 1;
  141. // create our own uri
  142. segment.uri = util.format('%s.%s', this.segmentName(this.seq), mime.extension(meta.mime));
  143. // handle byterange
  144. var first = self.index.segments.length === 0;
  145. var newFile = first || self.index.segments[self.index.segments.length - 1].uri !== segment.uri;
  146. if (this.collect) {
  147. segment.byterange = {
  148. length: 0,
  149. offset: newFile ? 0 : null
  150. }
  151. } else {
  152. delete segment.byterange;
  153. }
  154. // save the stream segment
  155. SegmentDecrypt.decrypt(segmentInfo.stream, segmentInfo.details.keys, this.decrypt, function (err, stream, decrypted) {
  156. if (err) {
  157. console.error('decrypt failed', err.stack);
  158. stream = segmentInfo.stream;
  159. } else if (decrypted) {
  160. segment.keys = null;
  161. }
  162. stream = oncemore(stream);
  163. stream.pipe(fs.createWriteStream(path.join(self.dst, segment.uri), { flags: newFile ? 'w' : 'a' }));
  164. var bytesWritten = 0;
  165. if (self.collect) {
  166. stream.on('data', function(chunk) {
  167. bytesWritten += chunk.length;
  168. });
  169. }
  170. stream.once('end', 'error', function(err) {
  171. // only to report errors
  172. if (err) debug('stream error', err.stack || err);
  173. if (segment.byterange)
  174. segment.byterange.length = bytesWritten;
  175. // update index
  176. self.index.segments.push(segment);
  177. self.flushIndex(done);
  178. });
  179. self.seq++;
  180. });
  181. };
  182. HlsStreamRecorder.prototype.variantName = function(info, index) {
  183. return util.format('v%d', index);
  184. };
  185. HlsStreamRecorder.prototype.groupSrcName = function(info, index) {
  186. var lang = (info.quotedString('language') || '').replace(/\W/g, '').toLowerCase();
  187. var id = (info.quotedString('group-id') || 'unk').replace(/\W/g, '').toLowerCase();
  188. return util.format('grp/%s/%s%d', id, lang ? lang + '-' : '', index);
  189. };
  190. HlsStreamRecorder.prototype.segmentName = function(seqNo) {
  191. function name(n) {
  192. var next = ~~(n / 26);
  193. var chr = String.fromCharCode(97 + n % 26); // 'a' + n
  194. if (next) return name(next - 1) + chr;
  195. return chr;
  196. }
  197. return this.collect ? 'stream' : name(seqNo);
  198. };
  199. HlsStreamRecorder.prototype.flushIndex = function(cb) {
  200. var appendString, indexString = this.index.toString().trim();
  201. if (this.lastIndexString && indexString.lastIndexOf(this.lastIndexString, 0) === 0) {
  202. var lastLength = this.lastIndexString.length;
  203. appendString = indexString.substr(lastLength);
  204. }
  205. this.lastIndexString = indexString;
  206. if (appendString) {
  207. fs.appendFile(path.join(this.dst, 'index.m3u8'), appendString, cb);
  208. } else {
  209. writeFileAtomic(path.join(this.dst, 'index.m3u8'), indexString, cb);
  210. }
  211. };
  212. HlsStreamRecorder.prototype.recorderForUrl = function(remoteUrl) {
  213. var idx, len = this.recorders.length;
  214. for (idx = 0; idx < len; idx++) {
  215. var rec = this.recorders[idx];
  216. if (rec.remoteUrl === remoteUrl)
  217. return rec;
  218. }
  219. return null;
  220. };
  221. var hlsrecorder = module.exports = function hlsrecorder(reader, dst, options) {
  222. return new HlsStreamRecorder(reader, dst, options);
  223. };
  224. hlsrecorder.HlsStreamRecorder = HlsStreamRecorder;