Blame view

node_modules/videojs-contrib-media-sources/docs/api/add-text-track-data.js.html 6.35 KB
2a09d1a4   liuqimichale   添加宜春 天水 宣化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="utf-8">
      <title>JSDoc: Source: add-text-track-data.js</title>
  
      <script src="scripts/prettify/prettify.js"> </script>
      <script src="scripts/prettify/lang-css.js"> </script>
      <!--[if lt IE 9]>
        <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
      <![endif]-->
      <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
      <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  </head>
  
  <body>
  
  <div id="main">
  
      <h1 class="page-title">Source: add-text-track-data.js</h1>
  
      
  
  
  
      
      <section>
          <article>
              <pre class="prettyprint source linenums"><code>/**
   * @file add-text-track-data.js
   */
  import window from 'global/window';
  import videojs from 'video.js';
  /**
   * Define properties on a cue for backwards compatability,
   * but warn the user that the way that they are using it
   * is depricated and will be removed at a later date.
   *
   * @param {Cue} cue the cue to add the properties on
   * @private
   */
  const deprecateOldCue = function(cue) {
    Object.defineProperties(cue.frame, {
      id: {
        get() {
          videojs.log.warn(
            'cue.frame.id is deprecated. Use cue.value.key instead.'
          );
          return cue.value.key;
        }
      },
      value: {
        get() {
          videojs.log.warn(
            'cue.frame.value is deprecated. Use cue.value.data instead.'
          );
          return cue.value.data;
        }
      },
      privateData: {
        get() {
          videojs.log.warn(
            'cue.frame.privateData is deprecated. Use cue.value.data instead.'
          );
          return cue.value.data;
        }
      }
    });
  };
  
  const durationOfVideo = function(duration) {
    let dur;
  
    if (isNaN(duration) || Math.abs(duration) === Infinity) {
      dur = Number.MAX_VALUE;
    } else {
      dur = duration;
    }
    return dur;
  };
  /**
   * Add text track data to a source handler given the captions and
   * metadata from the buffer.
   *
   * @param {Object} sourceHandler the flash or virtual source buffer
   * @param {Array} captionArray an array of caption data
   * @param {Array} metadataArray an array of meta data
   * @private
   */
  const addTextTrackData = function(sourceHandler, captionArray, metadataArray) {
    let Cue = window.WebKitDataCue || window.VTTCue;
  
    if (captionArray) {
      captionArray.forEach(function(caption) {
        let track = caption.stream;
  
        this.inbandTextTracks_[track].addCue(
          new Cue(
            caption.startTime + this.timestampOffset,
            caption.endTime + this.timestampOffset,
            caption.text
          ));
      }, sourceHandler);
    }
  
    if (metadataArray) {
      let videoDuration = durationOfVideo(sourceHandler.mediaSource_.duration);
  
      metadataArray.forEach(function(metadata) {
        let time = metadata.cueTime + this.timestampOffset;
  
        metadata.frames.forEach(function(frame) {
          let cue = new Cue(
            time,
            time,
            frame.value || frame.url || frame.data || '');
  
          cue.frame = frame;
          cue.value = frame;
          deprecateOldCue(cue);
  
          this.metadataTrack_.addCue(cue);
        }, this);
      }, sourceHandler);
  
      // Updating the metadeta cues so that
      // the endTime of each cue is the startTime of the next cue
      // the endTime of last cue is the duration of the video
      if (sourceHandler.metadataTrack_ &amp;&amp;
          sourceHandler.metadataTrack_.cues &amp;&amp;
          sourceHandler.metadataTrack_.cues.length) {
        let cues = sourceHandler.metadataTrack_.cues;
        let cuesArray = [];
  
        // Create a copy of the TextTrackCueList...
        // ...disregarding cues with a falsey value
        for (let i = 0; i &lt; cues.length; i++) {
          if (cues[i]) {
            cuesArray.push(cues[i]);
          }
        }
  
        // Group cues by their startTime value
        let cuesGroupedByStartTime = cuesArray.reduce((obj, cue) => {
          let timeSlot = obj[cue.startTime] || [];
  
          timeSlot.push(cue);
          obj[cue.startTime] = timeSlot;
  
          return obj;
        }, {});
  
        // Sort startTimes by ascending order
        let sortedStartTimes = Object.keys(cuesGroupedByStartTime)
                                     .sort((a, b) => Number(a) - Number(b));
  
        // Map each cue group's endTime to the next group's startTime
        sortedStartTimes.forEach((startTime, idx) => {
          let cueGroup = cuesGroupedByStartTime[startTime];
          let nextTime = Number(sortedStartTimes[idx + 1]) || videoDuration;
  
          // Map each cue's endTime the next group's startTime
          cueGroup.forEach((cue) => {
            cue.endTime = nextTime;
          });
        });
      }
    }
  };
  
  export default {
    addTextTrackData,
    durationOfVideo
  };
  </code></pre>
          </article>
      </section>
  
  
  
  
  </div>
  
  <nav>
      <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>
  </nav>
  
  <br class="clear">
  
  <footer>
      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)
  </footer>
  
  <script> prettyPrint(); </script>
  <script src="scripts/linenumber.js"> </script>
  </body>
  </html>