add-text-track-data.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file add-text-track-data.js
  3. */
  4. 'use strict';
  5. Object.defineProperty(exports, '__esModule', {
  6. value: true
  7. });
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  9. var _globalWindow = require('global/window');
  10. var _globalWindow2 = _interopRequireDefault(_globalWindow);
  11. var _videoJs = require('video.js');
  12. var _videoJs2 = _interopRequireDefault(_videoJs);
  13. /**
  14. * Define properties on a cue for backwards compatability,
  15. * but warn the user that the way that they are using it
  16. * is depricated and will be removed at a later date.
  17. *
  18. * @param {Cue} cue the cue to add the properties on
  19. * @private
  20. */
  21. var deprecateOldCue = function deprecateOldCue(cue) {
  22. Object.defineProperties(cue.frame, {
  23. id: {
  24. get: function get() {
  25. _videoJs2['default'].log.warn('cue.frame.id is deprecated. Use cue.value.key instead.');
  26. return cue.value.key;
  27. }
  28. },
  29. value: {
  30. get: function get() {
  31. _videoJs2['default'].log.warn('cue.frame.value is deprecated. Use cue.value.data instead.');
  32. return cue.value.data;
  33. }
  34. },
  35. privateData: {
  36. get: function get() {
  37. _videoJs2['default'].log.warn('cue.frame.privateData is deprecated. Use cue.value.data instead.');
  38. return cue.value.data;
  39. }
  40. }
  41. });
  42. };
  43. var durationOfVideo = function durationOfVideo(duration) {
  44. var dur = undefined;
  45. if (isNaN(duration) || Math.abs(duration) === Infinity) {
  46. dur = Number.MAX_VALUE;
  47. } else {
  48. dur = duration;
  49. }
  50. return dur;
  51. };
  52. /**
  53. * Add text track data to a source handler given the captions and
  54. * metadata from the buffer.
  55. *
  56. * @param {Object} sourceHandler the flash or virtual source buffer
  57. * @param {Array} captionArray an array of caption data
  58. * @param {Array} metadataArray an array of meta data
  59. * @private
  60. */
  61. var addTextTrackData = function addTextTrackData(sourceHandler, captionArray, metadataArray) {
  62. var Cue = _globalWindow2['default'].WebKitDataCue || _globalWindow2['default'].VTTCue;
  63. if (captionArray) {
  64. captionArray.forEach(function (caption) {
  65. var track = caption.stream;
  66. this.inbandTextTracks_[track].addCue(new Cue(caption.startTime + this.timestampOffset, caption.endTime + this.timestampOffset, caption.text));
  67. }, sourceHandler);
  68. }
  69. if (metadataArray) {
  70. (function () {
  71. var videoDuration = durationOfVideo(sourceHandler.mediaSource_.duration);
  72. metadataArray.forEach(function (metadata) {
  73. var time = metadata.cueTime + this.timestampOffset;
  74. metadata.frames.forEach(function (frame) {
  75. var cue = new Cue(time, time, frame.value || frame.url || frame.data || '');
  76. cue.frame = frame;
  77. cue.value = frame;
  78. deprecateOldCue(cue);
  79. this.metadataTrack_.addCue(cue);
  80. }, this);
  81. }, sourceHandler);
  82. // Updating the metadeta cues so that
  83. // the endTime of each cue is the startTime of the next cue
  84. // the endTime of last cue is the duration of the video
  85. if (sourceHandler.metadataTrack_ && sourceHandler.metadataTrack_.cues && sourceHandler.metadataTrack_.cues.length) {
  86. (function () {
  87. var cues = sourceHandler.metadataTrack_.cues;
  88. var cuesArray = [];
  89. // Create a copy of the TextTrackCueList...
  90. // ...disregarding cues with a falsey value
  91. for (var i = 0; i < cues.length; i++) {
  92. if (cues[i]) {
  93. cuesArray.push(cues[i]);
  94. }
  95. }
  96. // Group cues by their startTime value
  97. var cuesGroupedByStartTime = cuesArray.reduce(function (obj, cue) {
  98. var timeSlot = obj[cue.startTime] || [];
  99. timeSlot.push(cue);
  100. obj[cue.startTime] = timeSlot;
  101. return obj;
  102. }, {});
  103. // Sort startTimes by ascending order
  104. var sortedStartTimes = Object.keys(cuesGroupedByStartTime).sort(function (a, b) {
  105. return Number(a) - Number(b);
  106. });
  107. // Map each cue group's endTime to the next group's startTime
  108. sortedStartTimes.forEach(function (startTime, idx) {
  109. var cueGroup = cuesGroupedByStartTime[startTime];
  110. var nextTime = Number(sortedStartTimes[idx + 1]) || videoDuration;
  111. // Map each cue's endTime the next group's startTime
  112. cueGroup.forEach(function (cue) {
  113. cue.endTime = nextTime;
  114. });
  115. });
  116. })();
  117. }
  118. })();
  119. }
  120. };
  121. exports['default'] = {
  122. addTextTrackData: addTextTrackData,
  123. durationOfVideo: durationOfVideo
  124. };
  125. module.exports = exports['default'];