recorder.js 6.6 KB

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