add-text-track-data.js.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: add-text-track-data.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: add-text-track-data.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @file add-text-track-data.js
  21. */
  22. import window from 'global/window';
  23. import videojs from 'video.js';
  24. /**
  25. * Define properties on a cue for backwards compatability,
  26. * but warn the user that the way that they are using it
  27. * is depricated and will be removed at a later date.
  28. *
  29. * @param {Cue} cue the cue to add the properties on
  30. * @private
  31. */
  32. const deprecateOldCue = function(cue) {
  33. Object.defineProperties(cue.frame, {
  34. id: {
  35. get() {
  36. videojs.log.warn(
  37. 'cue.frame.id is deprecated. Use cue.value.key instead.'
  38. );
  39. return cue.value.key;
  40. }
  41. },
  42. value: {
  43. get() {
  44. videojs.log.warn(
  45. 'cue.frame.value is deprecated. Use cue.value.data instead.'
  46. );
  47. return cue.value.data;
  48. }
  49. },
  50. privateData: {
  51. get() {
  52. videojs.log.warn(
  53. 'cue.frame.privateData is deprecated. Use cue.value.data instead.'
  54. );
  55. return cue.value.data;
  56. }
  57. }
  58. });
  59. };
  60. const durationOfVideo = function(duration) {
  61. let dur;
  62. if (isNaN(duration) || Math.abs(duration) === Infinity) {
  63. dur = Number.MAX_VALUE;
  64. } else {
  65. dur = duration;
  66. }
  67. return dur;
  68. };
  69. /**
  70. * Add text track data to a source handler given the captions and
  71. * metadata from the buffer.
  72. *
  73. * @param {Object} sourceHandler the flash or virtual source buffer
  74. * @param {Array} captionArray an array of caption data
  75. * @param {Array} metadataArray an array of meta data
  76. * @private
  77. */
  78. const addTextTrackData = function(sourceHandler, captionArray, metadataArray) {
  79. let Cue = window.WebKitDataCue || window.VTTCue;
  80. if (captionArray) {
  81. captionArray.forEach(function(caption) {
  82. let track = caption.stream;
  83. this.inbandTextTracks_[track].addCue(
  84. new Cue(
  85. caption.startTime + this.timestampOffset,
  86. caption.endTime + this.timestampOffset,
  87. caption.text
  88. ));
  89. }, sourceHandler);
  90. }
  91. if (metadataArray) {
  92. let videoDuration = durationOfVideo(sourceHandler.mediaSource_.duration);
  93. metadataArray.forEach(function(metadata) {
  94. let time = metadata.cueTime + this.timestampOffset;
  95. metadata.frames.forEach(function(frame) {
  96. let cue = new Cue(
  97. time,
  98. time,
  99. frame.value || frame.url || frame.data || '');
  100. cue.frame = frame;
  101. cue.value = frame;
  102. deprecateOldCue(cue);
  103. this.metadataTrack_.addCue(cue);
  104. }, this);
  105. }, sourceHandler);
  106. // Updating the metadeta cues so that
  107. // the endTime of each cue is the startTime of the next cue
  108. // the endTime of last cue is the duration of the video
  109. if (sourceHandler.metadataTrack_ &amp;&amp;
  110. sourceHandler.metadataTrack_.cues &amp;&amp;
  111. sourceHandler.metadataTrack_.cues.length) {
  112. let cues = sourceHandler.metadataTrack_.cues;
  113. let cuesArray = [];
  114. // Create a copy of the TextTrackCueList...
  115. // ...disregarding cues with a falsey value
  116. for (let i = 0; i &lt; cues.length; i++) {
  117. if (cues[i]) {
  118. cuesArray.push(cues[i]);
  119. }
  120. }
  121. // Group cues by their startTime value
  122. let cuesGroupedByStartTime = cuesArray.reduce((obj, cue) => {
  123. let timeSlot = obj[cue.startTime] || [];
  124. timeSlot.push(cue);
  125. obj[cue.startTime] = timeSlot;
  126. return obj;
  127. }, {});
  128. // Sort startTimes by ascending order
  129. let sortedStartTimes = Object.keys(cuesGroupedByStartTime)
  130. .sort((a, b) => Number(a) - Number(b));
  131. // Map each cue group's endTime to the next group's startTime
  132. sortedStartTimes.forEach((startTime, idx) => {
  133. let cueGroup = cuesGroupedByStartTime[startTime];
  134. let nextTime = Number(sortedStartTimes[idx + 1]) || videoDuration;
  135. // Map each cue's endTime the next group's startTime
  136. cueGroup.forEach((cue) => {
  137. cue.endTime = nextTime;
  138. });
  139. });
  140. }
  141. }
  142. };
  143. export default {
  144. addTextTrackData,
  145. durationOfVideo
  146. };
  147. </code></pre>
  148. </article>
  149. </section>
  150. </div>
  151. <nav>
  152. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="FlashMediaSource.html">FlashMediaSource</a></li><li><a href="FlashSourceBuffer.html">FlashSourceBuffer</a></li><li><a href="HtmlMediaSource.html">HtmlMediaSource</a></li><li><a href="MessageHandlers.html">MessageHandlers</a></li><li><a href="VirtualSourceBuffer.html">VirtualSourceBuffer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#abort">abort</a></li><li><a href="global.html#addSourceBuffer">addSourceBuffer</a></li><li><a href="global.html#appendBuffer">appendBuffer</a></li><li><a href="global.html#appendGopInfo_">appendGopInfo_</a></li><li><a href="global.html#endOfStream">endOfStream</a></li><li><a href="global.html#FlashTransmuxerWorker">FlashTransmuxerWorker</a></li><li><a href="global.html#get">get</a></li><li><a href="global.html#gopsSafeToAlignWith">gopsSafeToAlignWith</a></li><li><a href="global.html#MediaSource">MediaSource</a></li><li><a href="global.html#open">open</a></li><li><a href="global.html#remove">remove</a></li><li><a href="global.html#removeGopBuffer">removeGopBuffer</a></li><li><a href="global.html#set">set</a></li><li><a href="global.html#supportsNativeMediaSources">supportsNativeMediaSources</a></li><li><a href="global.html#TransmuxerWorker">TransmuxerWorker</a></li><li><a href="global.html#updateGopBuffer">updateGopBuffer</a></li><li><a href="global.html#URL">URL</a></li></ul>
  153. </nav>
  154. <br class="clear">
  155. <footer>
  156. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.4</a> on Thu Nov 02 2017 12:03:25 GMT-0400 (EDT)
  157. </footer>
  158. <script> prettyPrint(); </script>
  159. <script src="scripts/linenumber.js"> </script>
  160. </body>
  161. </html>