timestamp-rollover-stream.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Any "shared" rollover streams will accept _all_ data. Otherwise,
  42. // streams will only accept data that matches their type.
  43. if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
  44. return;
  45. }
  46. if (referenceDTS === undefined) {
  47. referenceDTS = data.dts;
  48. }
  49. data.dts = handleRollover(data.dts, referenceDTS);
  50. data.pts = handleRollover(data.pts, referenceDTS);
  51. lastDTS = data.dts;
  52. this.trigger('data', data);
  53. };
  54. this.flush = function () {
  55. referenceDTS = lastDTS;
  56. this.trigger('done');
  57. };
  58. this.endTimeline = function () {
  59. this.flush();
  60. this.trigger('endedtimeline');
  61. };
  62. this.discontinuity = function () {
  63. referenceDTS = void 0;
  64. lastDTS = void 0;
  65. };
  66. this.reset = function () {
  67. this.discontinuity();
  68. this.trigger('reset');
  69. };
  70. };
  71. TimestampRolloverStream.prototype = new Stream();
  72. module.exports = {
  73. TimestampRolloverStream: TimestampRolloverStream,
  74. handleRollover: handleRollover
  75. };