ts.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. JSMpeg.Demuxer.TS = (function(){ "use strict";
  2. var TS = function(options) {
  3. this.bits = null;
  4. this.leftoverBytes = null;
  5. this.guessVideoFrameEnd = true;
  6. this.pidsToStreamIds = {};
  7. this.pesPacketInfo = {};
  8. this.startTime = 0;
  9. this.currentTime = 0;
  10. };
  11. TS.prototype.connect = function(streamId, destination) {
  12. this.pesPacketInfo[streamId] = {
  13. destination: destination,
  14. currentLength: 0,
  15. totalLength: 0,
  16. pts: 0,
  17. buffers: []
  18. };
  19. };
  20. TS.prototype.write = function(buffer) {
  21. if (this.leftoverBytes) {
  22. var totalLength = buffer.byteLength + this.leftoverBytes.byteLength;
  23. this.bits = new JSMpeg.BitBuffer(totalLength);
  24. this.bits.write([this.leftoverBytes, buffer]);
  25. }
  26. else {
  27. this.bits = new JSMpeg.BitBuffer(buffer);
  28. }
  29. while (this.bits.has(188 << 3) && this.parsePacket()) {}
  30. var leftoverCount = this.bits.byteLength - (this.bits.index >> 3);
  31. this.leftoverBytes = leftoverCount > 0
  32. ? this.bits.bytes.subarray(this.bits.index >> 3)
  33. : null;
  34. };
  35. TS.prototype.parsePacket = function() {
  36. // Check if we're in sync with packet boundaries; attempt to resync if not.
  37. if (this.bits.read(8) !== 0x47) {
  38. if (!this.resync()) {
  39. // Couldn't resync; maybe next time...
  40. return false;
  41. }
  42. }
  43. var end = (this.bits.index >> 3) + 187;
  44. var transportError = this.bits.read(1),
  45. payloadStart = this.bits.read(1),
  46. transportPriority = this.bits.read(1),
  47. pid = this.bits.read(13),
  48. transportScrambling = this.bits.read(2),
  49. adaptationField = this.bits.read(2),
  50. continuityCounter = this.bits.read(4);
  51. // If this is the start of a new payload; signal the end of the previous
  52. // frame, if we didn't do so already.
  53. var streamId = this.pidsToStreamIds[pid];
  54. if (payloadStart && streamId) {
  55. var pi = this.pesPacketInfo[streamId];
  56. if (pi && pi.currentLength) {
  57. this.packetComplete(pi);
  58. }
  59. }
  60. // Extract current payload
  61. if (adaptationField & 0x1) {
  62. if ((adaptationField & 0x2)) {
  63. var adaptationFieldLength = this.bits.read(8);
  64. this.bits.skip(adaptationFieldLength << 3);
  65. }
  66. if (payloadStart && this.bits.nextBytesAreStartCode()) {
  67. this.bits.skip(24);
  68. streamId = this.bits.read(8);
  69. this.pidsToStreamIds[pid] = streamId;
  70. var packetLength = this.bits.read(16)
  71. this.bits.skip(8);
  72. var ptsDtsFlag = this.bits.read(2);
  73. this.bits.skip(6);
  74. var headerLength = this.bits.read(8);
  75. var payloadBeginIndex = this.bits.index + (headerLength << 3);
  76. var pi = this.pesPacketInfo[streamId];
  77. if (pi) {
  78. var pts = 0;
  79. if (ptsDtsFlag & 0x2) {
  80. // The Presentation Timestamp is encoded as 33(!) bit
  81. // integer, but has a "marker bit" inserted at weird places
  82. // in between, making the whole thing 5 bytes in size.
  83. // You can't make this shit up...
  84. this.bits.skip(4);
  85. var p32_30 = this.bits.read(3);
  86. this.bits.skip(1);
  87. var p29_15 = this.bits.read(15);
  88. this.bits.skip(1);
  89. var p14_0 = this.bits.read(15);
  90. this.bits.skip(1);
  91. // Can't use bit shifts here; we need 33 bits of precision,
  92. // so we're using JavaScript's double number type. Also
  93. // divide by the 90khz clock to get the pts in seconds.
  94. pts = (p32_30 * 1073741824 + p29_15 * 32768 + p14_0)/90000;
  95. this.currentTime = pts;
  96. if (this.startTime === -1) {
  97. this.startTime = pts;
  98. }
  99. }
  100. var payloadLength = packetLength
  101. ? packetLength - headerLength - 3
  102. : 0;
  103. this.packetStart(pi, pts, payloadLength);
  104. }
  105. // Skip the rest of the header without parsing it
  106. this.bits.index = payloadBeginIndex;
  107. }
  108. if (streamId) {
  109. // Attempt to detect if the PES packet is complete. For Audio (and
  110. // other) packets, we received a total packet length with the PES
  111. // header, so we can check the current length.
  112. // For Video packets, we have to guess the end by detecting if this
  113. // TS packet was padded - there's no good reason to pad a TS packet
  114. // in between, but it might just fit exactly. If this fails, we can
  115. // only wait for the next PES header for that stream.
  116. var pi = this.pesPacketInfo[streamId];
  117. if (pi) {
  118. var start = this.bits.index >> 3;
  119. var complete = this.packetAddData(pi, start, end);
  120. var hasPadding = !payloadStart && (adaptationField & 0x2);
  121. if (complete || (this.guessVideoFrameEnd && hasPadding)) {
  122. this.packetComplete(pi);
  123. }
  124. }
  125. }
  126. }
  127. this.bits.index = end << 3;
  128. return true;
  129. };
  130. TS.prototype.resync = function() {
  131. // Check if we have enough data to attempt a resync. We need 5 full packets.
  132. if (!this.bits.has((188 * 6) << 3)) {
  133. return false;
  134. }
  135. var byteIndex = this.bits.index >> 3;
  136. // Look for the first sync token in the first 187 bytes
  137. for (var i = 0; i < 187; i++) {
  138. if (this.bits.bytes[byteIndex + i] === 0x47) {
  139. // Look for 4 more sync tokens, each 188 bytes appart
  140. var foundSync = true;
  141. for (var j = 1; j < 5; j++) {
  142. if (this.bits.bytes[byteIndex + i + 188 * j] !== 0x47) {
  143. foundSync = false;
  144. break;
  145. }
  146. }
  147. if (foundSync) {
  148. this.bits.index = (byteIndex + i + 1) << 3;
  149. return true;
  150. }
  151. }
  152. }
  153. // In theory, we shouldn't arrive here. If we do, we had enough data but
  154. // still didn't find sync - this can only happen if we were fed garbage
  155. // data. Check your source!
  156. console.warn('JSMpeg: Possible garbage data. Skipping.');
  157. this.bits.skip(187 << 3);
  158. return false;
  159. };
  160. TS.prototype.packetStart = function(pi, pts, payloadLength) {
  161. pi.totalLength = payloadLength;
  162. pi.currentLength = 0;
  163. pi.pts = pts;
  164. };
  165. TS.prototype.packetAddData = function(pi, start, end) {
  166. pi.buffers.push(this.bits.bytes.subarray(start, end));
  167. pi.currentLength += end - start;
  168. var complete = (pi.totalLength !== 0 && pi.currentLength >= pi.totalLength);
  169. return complete;
  170. };
  171. TS.prototype.packetComplete = function(pi) {
  172. pi.destination.write(pi.pts, pi.buffers);
  173. pi.totalLength = 0;
  174. pi.currentLength = 0;
  175. pi.buffers = [];
  176. };
  177. TS.STREAM = {
  178. PACK_HEADER: 0xBA,
  179. SYSTEM_HEADER: 0xBB,
  180. PROGRAM_MAP: 0xBC,
  181. PRIVATE_1: 0xBD,
  182. PADDING: 0xBE,
  183. PRIVATE_2: 0xBF,
  184. AUDIO_1: 0xC0,
  185. VIDEO_1: 0xE0,
  186. DIRECTORY: 0xFF
  187. };
  188. return TS;
  189. })();