track-decode-info.js 3.2 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. var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;
  8. /**
  9. * Store information about the start and end of the track and the
  10. * duration for each frame/sample we process in order to calculate
  11. * the baseMediaDecodeTime
  12. */
  13. var collectDtsInfo = function(track, data) {
  14. if (typeof data.pts === 'number') {
  15. if (track.timelineStartInfo.pts === undefined) {
  16. track.timelineStartInfo.pts = data.pts;
  17. }
  18. if (track.minSegmentPts === undefined) {
  19. track.minSegmentPts = data.pts;
  20. } else {
  21. track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
  22. }
  23. if (track.maxSegmentPts === undefined) {
  24. track.maxSegmentPts = data.pts;
  25. } else {
  26. track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
  27. }
  28. }
  29. if (typeof data.dts === 'number') {
  30. if (track.timelineStartInfo.dts === undefined) {
  31. track.timelineStartInfo.dts = data.dts;
  32. }
  33. if (track.minSegmentDts === undefined) {
  34. track.minSegmentDts = data.dts;
  35. } else {
  36. track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
  37. }
  38. if (track.maxSegmentDts === undefined) {
  39. track.maxSegmentDts = data.dts;
  40. } else {
  41. track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
  42. }
  43. }
  44. };
  45. /**
  46. * Clear values used to calculate the baseMediaDecodeTime between
  47. * tracks
  48. */
  49. var clearDtsInfo = function(track) {
  50. delete track.minSegmentDts;
  51. delete track.maxSegmentDts;
  52. delete track.minSegmentPts;
  53. delete track.maxSegmentPts;
  54. };
  55. /**
  56. * Calculate the track's baseMediaDecodeTime based on the earliest
  57. * DTS the transmuxer has ever seen and the minimum DTS for the
  58. * current track
  59. * @param track {object} track metadata configuration
  60. * @param keepOriginalTimestamps {boolean} If true, keep the timestamps
  61. * in the source; false to adjust the first segment to start at 0.
  62. */
  63. var calculateTrackBaseMediaDecodeTime = function(track, keepOriginalTimestamps) {
  64. var
  65. baseMediaDecodeTime,
  66. scale,
  67. minSegmentDts = track.minSegmentDts;
  68. // Optionally adjust the time so the first segment starts at zero.
  69. if (!keepOriginalTimestamps) {
  70. minSegmentDts -= track.timelineStartInfo.dts;
  71. }
  72. // track.timelineStartInfo.baseMediaDecodeTime is the location, in time, where
  73. // we want the start of the first segment to be placed
  74. baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime;
  75. // Add to that the distance this segment is from the very first
  76. baseMediaDecodeTime += minSegmentDts;
  77. // baseMediaDecodeTime must not become negative
  78. baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
  79. if (track.type === 'audio') {
  80. // Audio has a different clock equal to the sampling_rate so we need to
  81. // scale the PTS values into the clock rate of the track
  82. scale = track.samplerate / ONE_SECOND_IN_TS;
  83. baseMediaDecodeTime *= scale;
  84. baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
  85. }
  86. return baseMediaDecodeTime;
  87. };
  88. module.exports = {
  89. clearDtsInfo: clearDtsInfo,
  90. calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
  91. collectDtsInfo: collectDtsInfo
  92. };