timestamp-rollover-stream.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * mux.js
  3. *
  4. * Copyright (c) Brightcove
  5. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6. *
  7. * Accepts program elementary stream (PES) data events and corrects
  8. * decode and presentation time stamps to account for a rollover
  9. * of the 33 bit value.
  10. */
  11. 'use strict';
  12. var Stream = require('../utils/stream');
  13. var MAX_TS = 8589934592;
  14. var RO_THRESH = 4294967296;
  15. var TYPE_SHARED = 'shared';
  16. var handleRollover = function handleRollover(value, reference) {
  17. var direction = 1;
  18. if (value > reference) {
  19. // If the current timestamp value is greater than our reference timestamp and we detect a
  20. // timestamp rollover, this means the roll over is happening in the opposite direction.
  21. // Example scenario: Enter a long stream/video just after a rollover occurred. The reference
  22. // point will be set to a small number, e.g. 1. The user then seeks backwards over the
  23. // rollover point. In loading this segment, the timestamp values will be very large,
  24. // e.g. 2^33 - 1. Since this comes before the data we loaded previously, we want to adjust
  25. // the time stamp to be `value - 2^33`.
  26. direction = -1;
  27. } // Note: A seek forwards or back that is greater than the RO_THRESH (2^32, ~13 hours) will
  28. // cause an incorrect adjustment.
  29. while (Math.abs(reference - value) > RO_THRESH) {
  30. value += direction * MAX_TS;
  31. }
  32. return value;
  33. };
  34. var TimestampRolloverStream = function TimestampRolloverStream(type) {
  35. var lastDTS, referenceDTS;
  36. TimestampRolloverStream.prototype.init.call(this); // The "shared" type is used in cases where a stream will contain muxed
  37. // video and audio. We could use `undefined` here, but having a string
  38. // makes debugging a little clearer.
  39. this.type_ = type || TYPE_SHARED;
  40. this.push = function (data) {
  41. /**
  42. * Rollover stream expects data from elementary stream.
  43. * Elementary stream can push forward 2 types of data
  44. * - Parsed Video/Audio/Timed-metadata PES (packetized elementary stream) packets
  45. * - Tracks metadata from PMT (Program Map Table)
  46. * Rollover stream expects pts/dts info to be available, since it stores lastDTS
  47. * We should ignore non-PES packets since they may override lastDTS to undefined.
  48. * lastDTS is important to signal the next segments
  49. * about rollover from the previous segments.
  50. */
  51. if (data.type === 'metadata') {
  52. this.trigger('data', data);
  53. return;
  54. } // Any "shared" rollover streams will accept _all_ data. Otherwise,
  55. // streams will only accept data that matches their type.
  56. if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
  57. return;
  58. }
  59. if (referenceDTS === undefined) {
  60. referenceDTS = data.dts;
  61. }
  62. data.dts = handleRollover(data.dts, referenceDTS);
  63. data.pts = handleRollover(data.pts, referenceDTS);
  64. lastDTS = data.dts;
  65. this.trigger('data', data);
  66. };
  67. this.flush = function () {
  68. referenceDTS = lastDTS;
  69. this.trigger('done');
  70. };
  71. this.endTimeline = function () {
  72. this.flush();
  73. this.trigger('endedtimeline');
  74. };
  75. this.discontinuity = function () {
  76. referenceDTS = void 0;
  77. lastDTS = void 0;
  78. };
  79. this.reset = function () {
  80. this.discontinuity();
  81. this.trigger('reset');
  82. };
  83. };
  84. TimestampRolloverStream.prototype = new Stream();
  85. module.exports = {
  86. TimestampRolloverStream: TimestampRolloverStream,
  87. handleRollover: handleRollover
  88. };