recorder.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. streamprocess = require('streamprocess'),
  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. // add custom extensions
  15. mime.extensions['audio/aac'] = ['aac'];
  16. mime.extensions['audio/ac3'] = ['ac3'];
  17. function HlsStreamRecorder(reader, dst, options) {
  18. options = options || {};
  19. this.reader = reader;
  20. this.dst = dst; // target directory
  21. this.nextSegmentSeq = -1;
  22. this.seq = 0;
  23. this.index = null;
  24. this.startOffset = parseFloat(options.startOffset);
  25. this.subreader = options.subreader;
  26. this.recorders = [];
  27. }
  28. HlsStreamRecorder.prototype.start = function() {
  29. // TODO: make async?
  30. if (!fs.existsSync(this.dst))
  31. mkdirp.sync(this.dst);
  32. streamprocess(this.reader, this.process.bind(this));
  33. this.updateIndex(this.reader.index);
  34. this.reader.on('index', this.updateIndex.bind(this));
  35. };
  36. HlsStreamRecorder.prototype.updateIndex = function(update) {
  37. var self = this;
  38. if (!update) return;
  39. if (!this.index) {
  40. this.index = new m3u8parse.M3U8Playlist(update);
  41. if (!this.index.variant) {
  42. if (this.index.version < 2) {
  43. // version 2 is required to support the remapped IV attribute
  44. this.index.version = 2;
  45. debug('changed index version to:', this.index.version);
  46. }
  47. this.index.segments = [];
  48. this.index.first_seq_no = self.seq;
  49. this.index.type = 'EVENT';
  50. this.index.ended = false;
  51. this.index.discontinuity_sequence = 0; // not allowed in event playlists
  52. if (!isNaN(this.startOffset)) {
  53. var offset = this.startOffset;
  54. if (offset < 0) offset = Math.min(offset, -3 * this.target_duration);
  55. this.index.start.decimalInteger('offset', offset);
  56. }
  57. } else {
  58. debug('programs', this.index.programs);
  59. if (this.subreader) {
  60. var programNo = Object.keys(this.index.programs)[0];
  61. var programs = this.index.programs[programNo];
  62. // remove backup sources
  63. var used = {};
  64. programs = programs.filter(function(program) {
  65. var bw = parseInt(program.info.bandwidth, 10);
  66. var res = !(bw in used);
  67. used[bw] = true;
  68. return res;
  69. });
  70. this.index.programs[programNo] = programs;
  71. programs.forEach(function(program, index) {
  72. var programUrl = url.resolve(self.reader.baseUrl, program.uri);
  73. debug('url', programUrl);
  74. // check for duplicate source urls
  75. var rec = this.recorderForUrl(programUrl);
  76. if (!rec || !rec.localUrl) {
  77. var dir = self.variantName(program.info, index);
  78. rec = new HlsStreamRecorder(self.subreader(programUrl), path.join(self.dst, dir), { startOffset: self.startOffset });
  79. rec.localUrl = url.format({pathname: path.join(dir, 'index.m3u8')});
  80. rec.remoteUrl = programUrl;
  81. this.recorders.push(rec);
  82. }
  83. program.uri = rec.localUrl;
  84. }, this);
  85. var allGroups = [];
  86. for (var group in this.index.groups)
  87. [].push.apply(allGroups, this.index.groups[group]);
  88. allGroups.forEach(function(groupItem, index) {
  89. var srcUri = groupItem.quotedString('uri');
  90. if (srcUri) {
  91. var itemUrl = url.resolve(self.reader.baseUrl, srcUri);
  92. debug('url', itemUrl);
  93. var rec = this.recorderForUrl(itemUrl);
  94. if (!rec || !rec.localUrl) {
  95. var dir = self.groupSrcName(groupItem, index);
  96. rec = new HlsStreamRecorder(self.subreader(itemUrl), path.join(self.dst, dir), { startOffset: self.startOffset });
  97. rec.localUrl = url.format({pathname: path.join(dir, 'index.m3u8')});
  98. rec.remoteUrl = itemUrl;
  99. this.recorders.push(rec);
  100. }
  101. groupItem.quotedString('uri', rec.localUrl);
  102. }
  103. }, this);
  104. // start all recordings
  105. this.recorders.forEach(function(recording) {
  106. recording.start();
  107. });
  108. this.index.iframes = {};
  109. } else {
  110. this.index.programs = {};
  111. this.index.groups = {};
  112. this.index.iframes = {};
  113. }
  114. }
  115. // hook end listener
  116. this.reader.on('end', function() {
  117. self.index.ended = true;
  118. self.flushIndex(function(/*err*/) {
  119. debug('done');
  120. });
  121. });
  122. }
  123. // validate update
  124. if (this.index.target_duration > update.target_duration)
  125. throw new Error('Invalid index');
  126. };
  127. HlsStreamRecorder.prototype.process = function(segmentInfo, next) {
  128. var self = this;
  129. var segment = new m3u8parse.M3U8Segment(segmentInfo.details, true);
  130. var meta = segmentInfo.file;
  131. // remove byterange info
  132. delete segment.byterange;
  133. // mark discontinuities
  134. if (this.nextSegmentSeq !== -1 &&
  135. this.nextSegmentSeq !== segmentInfo.seq)
  136. segment.discontinuity = true;
  137. this.nextSegmentSeq = segmentInfo.seq + 1;
  138. // create our own uri
  139. segment.uri = util.format('%s.%s', this.segmentName(this.seq), mime.extension(meta.mime));
  140. // save the stream segment
  141. var stream = oncemore(segmentInfo.stream);
  142. stream.pipe(fs.createWriteStream(path.join(this.dst, segment.uri)));
  143. stream.once('end', 'error', function(err) {
  144. // only to report errors
  145. if (err) debug('stream error', err.stack || err);
  146. // update index
  147. self.index.segments.push(segment);
  148. self.flushIndex(next);
  149. });
  150. this.seq++;
  151. };
  152. HlsStreamRecorder.prototype.variantName = function(info, index) {
  153. return util.format('v%d', index);
  154. };
  155. HlsStreamRecorder.prototype.groupSrcName = function(info, index) {
  156. var lang = (info.quotedString('language') || '').replace(/\W/g, '').toLowerCase();
  157. var id = (info.quotedString('group-id') || 'unk').replace(/\W/g, '').toLowerCase();
  158. return util.format('grp/%s/%s%d', id, lang ? lang + '-' : '', index);
  159. };
  160. HlsStreamRecorder.prototype.segmentName = function(seqNo) {
  161. function name(n) {
  162. var next = ~~(n / 26);
  163. var chr = String.fromCharCode(97 + n % 26); // 'a' + n
  164. if (next) return name(next - 1) + chr;
  165. return chr;
  166. }
  167. return name(seqNo);
  168. };
  169. HlsStreamRecorder.prototype.flushIndex = function(cb) {
  170. var appendString, indexString = this.index.toString().trim();
  171. if (this.lastIndexString && indexString.lastIndexOf(this.lastIndexString, 0) === 0) {
  172. var lastLength = this.lastIndexString.length;
  173. appendString = indexString.substr(lastLength);
  174. }
  175. this.lastIndexString = indexString;
  176. if (appendString) {
  177. fs.appendFile(path.join(this.dst, 'index.m3u8'), appendString, cb);
  178. } else {
  179. writeFileAtomic(path.join(this.dst, 'index.m3u8'), indexString, cb);
  180. }
  181. };
  182. HlsStreamRecorder.prototype.recorderForUrl = function(remoteUrl) {
  183. var idx, len = this.recorders.length;
  184. for (idx = 0; idx < len; idx++) {
  185. var rec = this.recorders[idx];
  186. if (rec.remoteUrl === remoteUrl)
  187. return rec;
  188. }
  189. return null;
  190. };
  191. var hlsrecorder = module.exports = function hlsrecorder(reader, dst, options) {
  192. return new HlsStreamRecorder(reader, dst, options);
  193. };
  194. hlsrecorder.HlsStreamRecorder = HlsStreamRecorder;