recorder.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. var fs = require('fs'),
  2. path = require('path'),
  3. url = require('url'),
  4. util = require('util');
  5. var mime = require('mime'),
  6. streamprocess = require('streamprocess'),
  7. oncemore = require('oncemore'),
  8. m3u8parse = require('m3u8parse'),
  9. debug = require('debug')('hls:recorder');
  10. module.exports = hlsrecorder;
  11. hlsrecorder.HlsStreamRecorder = HlsStreamRecorder;
  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.subreader = options.subreader;
  20. }
  21. HlsStreamRecorder.prototype.start = function() {
  22. var self = this;
  23. // TODO: make async?
  24. if (!fs.existsSync(this.dst))
  25. fs.mkdirSync(this.dst);
  26. streamprocess(this.reader, this.process.bind(this));
  27. this.updateIndex(this.reader.index);
  28. this.reader.on('index', this.updateIndex.bind(this));
  29. };
  30. HlsStreamRecorder.prototype.updateIndex = function(update) {
  31. var self = this;
  32. if (!update) return;
  33. if (!this.index) {
  34. this.index = new m3u8parse.M3U8Playlist(update);
  35. this.index.segments = [];
  36. this.index.first_seq_no = self.seq;
  37. this.index.type = 'EVENT';
  38. this.index.ended = false;
  39. debug('programs', this.index.programs);
  40. if (this.subreader) {
  41. for (var programNo in this.index.programs) {
  42. var programs = this.index.programs[programNo];
  43. // remove backup sources
  44. var used = {};
  45. programs = programs.filter(function(program) {
  46. var bw = parseInt(program.info.bandwidth, 10);
  47. var res = !(bw in used);
  48. used[bw] = true;
  49. return res;
  50. });
  51. this.index.programs[programNo] = programs;
  52. programs.forEach(function(program, index) {
  53. var programUrl = url.resolve(self.reader.baseUrl, program.uri);
  54. debug('url', programUrl);
  55. var dir = self.variantName(program.info, index);
  56. program.uri = path.join(dir, 'index.m3u8');
  57. program.recorder = hlsrecorder(self.subreader(programUrl), path.join(self.dst, dir)).start();
  58. });
  59. }
  60. // TODO: handle groups!!
  61. this.index.groups = {};
  62. this.index.iframes = {};
  63. } else {
  64. this.index.programs = {};
  65. this.index.groups = {};
  66. this.index.iframes = {};
  67. }
  68. // hook end listener
  69. this.reader.on('end', function() {
  70. self.index.ended = true;
  71. self.flushIndex(function(err) {
  72. debug('done');
  73. });
  74. });
  75. }
  76. // validate update
  77. if (this.index.target_duration > update.target_duration)
  78. throw new Error('Invalid index');
  79. };
  80. HlsStreamRecorder.prototype.process = function(obj, next) {
  81. var self = this;
  82. var segment = new m3u8parse.M3U8Segment(obj.segment);
  83. var meta = obj.meta;
  84. // mark discontinuities
  85. if (this.nextSegmentSeq !== -1 &&
  86. this.nextSegmentSeq !== obj.seq)
  87. segment.discontinuity = true;
  88. this.nextSegmentSeq = obj.seq+1;
  89. // create our own uri
  90. segment.uri = util.format('%s.%s', this.segmentName(this.seq), mime.extension(meta.mime));
  91. // manually set iv if sequence based, since we generate our own sequence numbering
  92. if (segment.key && !segment.key.iv) {
  93. var seqStr = obj.seq.toString();
  94. segment.key.iv = '0x00000000000000000000000000000000'.slice(-seqStr.length) + seqStr;
  95. if (this.index.version > 2) {
  96. this.index.version = 2;
  97. debug('changed index version to:', this.index.version);
  98. }
  99. }
  100. // save the stream segment
  101. var stream = oncemore(obj.stream);
  102. stream.pipe(fs.createWriteStream(path.join(this.dst, segment.uri)));
  103. stream.once('end', 'error', function(err) {
  104. // only to report errors
  105. if (err) debug('stream error', err.stack || err);
  106. // update index
  107. self.index.segments.push(segment);
  108. self.flushIndex(next);
  109. });
  110. this.seq++;
  111. };
  112. HlsStreamRecorder.prototype.variantName = function(info, index) {
  113. return util.format('v%d', index);
  114. };
  115. HlsStreamRecorder.prototype.segmentName = function(seqNo) {
  116. function name(n) {
  117. var next = ~~(n/26);
  118. var chr = String.fromCharCode(97 + n%26); // 'a' + n
  119. if (next) return name(next-1) + chr;
  120. return chr;
  121. }
  122. return name(seqNo);
  123. };
  124. HlsStreamRecorder.prototype.flushIndex = function(cb) {
  125. // TODO: make atomic by writing to temp file & renaming
  126. fs.writeFile(path.join(this.dst, 'index.m3u8'), this.index, cb);
  127. };
  128. function hlsrecorder(reader, dst, options) {
  129. return new HlsStreamRecorder(reader, dst, options);
  130. }