reader.js 11 KB

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