reader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // stream from hls source
  2. var util = require('util'),
  3. url = require('url'),
  4. zlib = require('zlib'),
  5. assert = require('assert');
  6. var request = require('request'),
  7. oncemore = require('./oncemore'),
  8. debug = require('debug')('hls:reader');
  9. try {
  10. var Readable = require('stream').Readable;
  11. assert(Readable);
  12. var Passthrough = null;
  13. } catch (e) {
  14. var Readable = require('readable-stream');
  15. var Passthrough = require('readable-stream/passthrough');
  16. }
  17. var m3u8 = require('./m3u8');
  18. function noop() {};
  19. var DEFAULT_AGENT = util.format('hls-tools/v%s (http://github.com/kanongil/node-hls-tools) node.js/%s', require('../package').version, process.version);
  20. module.exports = hlsreader;
  21. hlsreader.HlsStreamReader = HlsStreamReader;
  22. /*
  23. options:
  24. startSeq*
  25. noData // don't emit any data - useful for analyzing the stream structure
  26. maxRedirects*
  27. cacheDir*
  28. headers* // allows for custom user-agent, cookies, auth, etc
  29. emits:
  30. index (m3u8)
  31. segment (seqNo, duration, datetime, size?, )
  32. */
  33. function inheritErrors(stream) {
  34. stream.on('pipe', function(source) {
  35. source.on('error', stream.emit.bind(stream, 'error'));
  36. });
  37. stream.on('unpipe', function(source) {
  38. source.removeListener('error', stream.emit.bind(stream, 'error'));
  39. });
  40. return stream;
  41. }
  42. function getFileStream(srcUrl, options, cb) {
  43. assert(srcUrl.protocol);
  44. if (typeof options === 'function') {
  45. cb = options;
  46. options = {};
  47. }
  48. options = options || {};
  49. if (srcUrl.protocol === 'http:' || srcUrl.protocol === 'https:') {
  50. var headers = options.headers || {};
  51. if (!headers['user-agent']) headers['user-agent'] = DEFAULT_AGENT;
  52. if (!headers['accept-encoding']) headers['accept-encoding'] = ['gzip','deflate'];
  53. var start = options.start || 0;
  54. if (start > 0)
  55. headers['range'] = 'bytes=' + options.start + '-';
  56. var req = (options.probe ? request.head : request.get)({uri:url.format(srcUrl), pool:false, headers:headers, timeout:60*1000});
  57. req.on('error', onreqerror);
  58. req.on('response', onresponse);
  59. function onreqerror(err) {
  60. req.removeListener('error', onreqerror);
  61. req.removeListener('response', onresponse);
  62. cb(err);
  63. }
  64. function onresponse(res) {
  65. req.removeListener('error', onreqerror);
  66. req.removeListener('response', onresponse);
  67. if (res.statusCode !== 200 && res.statusCode !== 206) {
  68. req.abort();
  69. if (res.statusCode >= 500 && res.statusCode !== 501)
  70. return cb(new TempError('HTTP Server returned: '+res.statusCode));
  71. else
  72. return cb(new Error('Bad server response code: '+res.statusCode));
  73. }
  74. var size = res.headers['content-length'] ? parseInt(res.headers['content-length'], 10) : -1;
  75. // turn bad content-length into actual errors
  76. if (size >= 0 && !options.probe) {
  77. var accum = 0;
  78. res.on('data', function(chunk) {
  79. accum += chunk.length;
  80. if (accum > size)
  81. req.abort();
  82. });
  83. oncemore(res).once('end', 'error', function(err) {
  84. // TODO: make this a custom error?
  85. if (!err && accum !== size)
  86. stream.emit('error', new PartialError('Stream length did not match header', accum, size));
  87. });
  88. }
  89. // transparently handle gzip responses
  90. var stream = res;
  91. if (res.headers['content-encoding'] === 'gzip' || res.headers['content-encoding'] === 'deflate') {
  92. unzip = zlib.createUnzip();
  93. stream = stream.pipe(inheritErrors(unzip));
  94. size = -1;
  95. }
  96. // adapt old style streams for pre-streams2 node versions
  97. if (Passthrough && !(stream instanceof Readable))
  98. stream = stream.pipe(inheritErrors(new Passthrough()));
  99. // allow aborting the request
  100. stream.abort = req.abort.bind(req);
  101. // forward all future errors to response stream
  102. req.on('error', function(err) {
  103. if (stream.listeners('error').length !== 0)
  104. stream.emit('error', err);
  105. });
  106. // attach empty 'error' listener to keep it from ever throwing
  107. stream.on('error', noop);
  108. // extract meta information from header
  109. var typeparts = /^(.+?\/.+?)(?:;\w*.*)?$/.exec(res.headers['content-type']) || [null, 'application/octet-stream'],
  110. mimetype = typeparts[1].toLowerCase(),
  111. modified = res.headers['last-modified'] ? new Date(res.headers['last-modified']) : null;
  112. cb(null, stream, {url:url.format(req.uri), mime:mimetype, size:start+size, modified:modified});
  113. }
  114. } else {
  115. process.nextTick(function() {
  116. cb(new Error('Unsupported protocol: '+srcUrl.protocol));
  117. });
  118. }
  119. /* if (srcUrl.protocol === 'file:') {
  120. } else if (srcUrl.protocol === 'data:') {
  121. //var regex = /^data:(.+\/.+);base64,(.*)$/;
  122. // add content-type && content-length headers
  123. } else {
  124. }*/
  125. }
  126. function HlsStreamReader(src, options) {
  127. var self = this;
  128. options = options || {};
  129. if (typeof src === 'string')
  130. src = url.parse(src);
  131. this.url = src;
  132. this.baseUrl = src;
  133. this.fullStream = !!options.fullStream;
  134. this.keepConnection = !!options.keepConnection;
  135. this.noData = !!options.noData;
  136. this.indexStream = null;
  137. this.index = null;
  138. this.readState = {
  139. currentSeq:-1,
  140. currentSegment:null,
  141. stream:null
  142. }
  143. function getUpdateInterval(updated) {
  144. if (updated && self.index.segments.length)
  145. return Math.min(self.index.target_duration, self.index.segments[self.index.segments.length-1].duration);
  146. else
  147. return self.index.target_duration / 2;
  148. }
  149. function updatecheck(updated) {
  150. if (updated) {
  151. if (self.readState.currentSeq===-1)
  152. self.readState.currentSeq = self.index.startSeqNo(self.fullStream);
  153. else if (self.readState.currentSeq < self.index.startSeqNo(true))
  154. self.readState.currentSeq = self.index.startSeqNo(true);
  155. self.emit('index', self.index);
  156. if (self.index.variant)
  157. return self.end();
  158. }
  159. checkcurrent();
  160. if (!self.index.ended) {
  161. var updateInterval = getUpdateInterval(updated);
  162. debug('scheduling index refresh', updateInterval);
  163. setTimeout(updateindex, Math.max(1, updateInterval)*1000);
  164. }
  165. }
  166. function updateindex() {
  167. getFileStream(self.url, function(err, stream, meta) {
  168. if (err) {
  169. if (self.index && self.keepConnection) {
  170. console.error('Failed to update index at '+url.format(self.url)+':', err.stack || err);
  171. return updatecheck(false);
  172. }
  173. return self.emit('error', err);
  174. }
  175. if (meta.mime !== 'application/vnd.apple.mpegurl' &&
  176. meta.mime !== 'application/x-mpegurl' && meta.mime !== 'audio/mpegurl')
  177. return self.emit('error', new Error('Invalid MIME type: '+meta.mime));
  178. // FIXME: correctly handle .m3u us-ascii encoding
  179. self.baseUrl = meta.url;
  180. m3u8.parse(stream, function(err, index) {
  181. if (err) {
  182. if (self.index && self.keepConnection) {
  183. console.error('Failed to parse index at '+url.format(self.url)+':', err.stack || err);
  184. return updatecheck(false);
  185. }
  186. return self.emit('error', err);
  187. }
  188. var updated = true;
  189. if (self.index && self.index.lastSeqNo() === index.lastSeqNo()) {
  190. debug('index was not updated');
  191. updated = false;
  192. }
  193. self.index = index;
  194. updatecheck(updated);
  195. });
  196. });
  197. }
  198. updateindex();
  199. function checkcurrent() {
  200. if (self.readState.currentSegment) return; // already processing
  201. self.readState.currentSegment = self.index.getSegment(self.readState.currentSeq);
  202. if (self.readState.currentSegment) {
  203. var url = self.readState.currentSegment.uri;
  204. function tryfetch(start) {
  205. var seq = self.readState.currentSeq;
  206. fetchfrom(seq, self.readState.currentSegment, start, function(err) {
  207. if (err) {
  208. if (!self.keepConnection) return self.emit('error', err);
  209. console.error('While fetching '+url+':', err.stack || err);
  210. // retry with missing range if it is still relevant
  211. if (err instanceof PartialError && err.processed > 0 &&
  212. self.index.getSegment(seq))
  213. return tryfetch(start + err.processed);
  214. }
  215. self.readState.currentSegment = null;
  216. if (seq === self.readState.currentSeq)
  217. self.readState.currentSeq++;
  218. checkcurrent();
  219. });
  220. }
  221. tryfetch(0);
  222. } else if (self.index.ended)
  223. self.end();
  224. else if (!self.index.type && (self.index.lastSeqNo() < self.readState.currentSeq-1)) {
  225. // handle live stream restart
  226. self.readState.currentSeq = self.index.startSeqNo(true);
  227. checkcurrent();
  228. }
  229. }
  230. function fetchfrom(seqNo, segment, start, cb) {
  231. var segmentUrl = url.resolve(self.baseUrl, segment.uri)
  232. debug('fetching segment', segmentUrl);
  233. getFileStream(url.parse(segmentUrl), {probe:!!self.noData, start:start}, function(err, stream, meta) {
  234. if (err) return cb(err);
  235. debug('got segment info', meta);
  236. if (meta.mime !== 'video/mp2t'/* &&
  237. meta.mime !== 'audio/aac' && meta.mime !== 'audio/x-aac' &&
  238. meta.mime !== 'audio/ac3'*/) {
  239. if (stream && stream.abort)
  240. stream.abort();
  241. return cb(new Error('Unsupported segment MIME type: '+meta.mime));
  242. }
  243. if (!start)
  244. self.emit('segment', seqNo, segment.duration, meta);
  245. if (stream) {
  246. debug('preparing to push stream to reader', meta.url);
  247. stream.on('error', onerror);
  248. function onerror(err) {
  249. debug('stream error', err);
  250. stream.removeListener('error', onerror);
  251. cb(err)
  252. }
  253. self.readState.stream = stream;
  254. self.readState.stream_started = false;
  255. self.readState.doneCb = function() {
  256. debug('finished with input stream', meta.url);
  257. stream.removeListener('error', onerror);
  258. cb(null);
  259. };
  260. // force a new _read in the future()
  261. if (self.push(''))
  262. self.stream_start();
  263. } else {
  264. process.nextTick(cb);
  265. }
  266. });
  267. }
  268. // allow piping content to self
  269. this.write = function(chunk) {
  270. self.push(chunk);
  271. return true;
  272. };
  273. this.end = function() {};
  274. this.stream_start = function() {
  275. var stream = self.readState.stream;
  276. if (stream && !self.readState.stream_started) {
  277. debug('pushing input stream to reader');
  278. stream.pipe(self);
  279. oncemore(stream).once('end', 'error', function(err) {
  280. clearTimeout(self.readState.timer);
  281. stream.unpipe(self);
  282. self.readState.stream = null;
  283. if (!err)
  284. self.readState.doneCb();
  285. });
  286. clearTimeout(self.readState.timer);
  287. // abort() indicates a temporal stream. Ie. ensure it is completed in a timely fashion
  288. if (self.index.isLive() && typeof stream.abort == 'function') {
  289. var duration = self.readState.currentSegment.duration || self.index.target_duration || 10;
  290. duration = Math.min(duration, self.index.target_duration || 10);
  291. self.readState.timer = setTimeout(function() {
  292. if (self.readState.stream) {
  293. debug('timed out waiting for data');
  294. self.readState.stream.abort();
  295. }
  296. // TODO: ensure Done() is always called
  297. self.readState.timer = null;
  298. }, 1.5*duration*1000);
  299. }
  300. self.readState.stream_started = true;
  301. }
  302. }
  303. Readable.call(this, options);
  304. }
  305. util.inherits(HlsStreamReader, Readable);
  306. HlsStreamReader.prototype._read = function(n, cb) {
  307. this.stream_start();
  308. };
  309. function hlsreader(url, options) {
  310. return new HlsStreamReader(url, options);
  311. }
  312. function TempError(msg) {
  313. Error.captureStackTrace(this, this);
  314. this.message = msg || 'TempError';
  315. }
  316. util.inherits(TempError, Error);
  317. TempError.prototype.name = 'Temporary Error';
  318. function PartialError(msg, processed, expected) {
  319. Error.captureStackTrace(this, this);
  320. this.message = msg || 'TempError';
  321. this.processed = processed || -1;
  322. this.expected = expected;
  323. }
  324. util.inherits(PartialError, Error);
  325. PartialError.prototype.name = 'Partial Error';