track-decode-info.js 3.2 KB

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