Source: lib/media/segment_index.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.media.SegmentIndex');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.log');
  20. goog.require('shaka.media.SegmentReference');
  21. goog.require('shaka.util.IDestroyable');
  22. goog.require('shaka.util.ManifestParserUtils');
  23. /**
  24. * Creates a SegmentIndex.
  25. *
  26. * @param {!Array.<!shaka.media.SegmentReference>} references The list of
  27. * SegmentReferences, which must be sorted first by their start times
  28. * (ascending) and second by their end times (ascending), and have
  29. * continuous, increasing positions.
  30. *
  31. * @constructor
  32. * @struct
  33. * @implements {shaka.util.IDestroyable}
  34. * @export
  35. */
  36. shaka.media.SegmentIndex = function(references) {
  37. if (!COMPILED) {
  38. shaka.media.SegmentIndex.assertCorrectReferences_(references);
  39. }
  40. /** @private {Array.<!shaka.media.SegmentReference>} */
  41. this.references_ = references;
  42. };
  43. /**
  44. * @override
  45. * @export
  46. */
  47. shaka.media.SegmentIndex.prototype.destroy = function() {
  48. this.references_ = null;
  49. return Promise.resolve();
  50. };
  51. /**
  52. * Finds the position of the segment for the given time, in seconds, relative
  53. * to the start of a particular Period. Returns the position of the segment
  54. * with the largest end time if more than one segment is known for the given
  55. * time.
  56. *
  57. * @param {number} time
  58. * @return {?number} The position of the segment, or null
  59. * if the position of the segment could not be determined.
  60. * @export
  61. */
  62. shaka.media.SegmentIndex.prototype.find = function(time) {
  63. // For live streams, searching from the end is faster. For VOD, it balances
  64. // out either way. In both cases, references_.length is small enough that the
  65. // difference isn't huge.
  66. for (var i = this.references_.length - 1; i >= 0; --i) {
  67. var r = this.references_[i];
  68. // Note that a segment ends immediately before the end time.
  69. if ((time >= r.startTime) && (time < r.endTime)) {
  70. return r.position;
  71. }
  72. }
  73. if (this.references_.length && time < this.references_[0].startTime)
  74. return this.references_[0].position;
  75. return null;
  76. };
  77. /**
  78. * Gets the SegmentReference for the segment at the given position.
  79. *
  80. * @param {number} position The position of the segment.
  81. * @return {shaka.media.SegmentReference} The SegmentReference, or null if
  82. * no such SegmentReference exists.
  83. * @export
  84. */
  85. shaka.media.SegmentIndex.prototype.get = function(position) {
  86. if (this.references_.length == 0)
  87. return null;
  88. var index = position - this.references_[0].position;
  89. if (index < 0 || index >= this.references_.length)
  90. return null;
  91. return this.references_[index];
  92. };
  93. /**
  94. * Merges the given SegmentReferences. Supports extending the original
  95. * references only. Will not replace old references or interleave new ones.
  96. *
  97. * @param {!Array.<!shaka.media.SegmentReference>} references The list of
  98. * SegmentReferences, which must be sorted first by their start times
  99. * (ascending) and second by their end times (ascending), and have
  100. * continuous, increasing positions.
  101. * @export
  102. */
  103. shaka.media.SegmentIndex.prototype.merge = function(references) {
  104. if (!COMPILED) {
  105. shaka.media.SegmentIndex.assertCorrectReferences_(references);
  106. }
  107. var newReferences = [];
  108. var i = 0;
  109. var j = 0;
  110. while ((i < this.references_.length) && (j < references.length)) {
  111. var r1 = this.references_[i];
  112. var r2 = references[j];
  113. if (r1.startTime < r2.startTime) {
  114. newReferences.push(r1);
  115. i++;
  116. } else if (r1.startTime > r2.startTime) {
  117. // Drop the new reference if it would have to be interleaved with the
  118. // old one. Issue a warning, since this is not a supported update.
  119. shaka.log.warning('Refusing to rewrite original references on update!');
  120. j++;
  121. } else {
  122. // When period is changed, fitSegmentReference will expand the last
  123. // segment to the start of the next period. So, it is valid to have end
  124. // time updated to the last segment reference in a period
  125. if (Math.abs(r1.endTime - r2.endTime) > 0.1) {
  126. goog.asserts.assert(r2.endTime > r1.endTime &&
  127. i == this.references_.length - 1 &&
  128. j == references.length - 1,
  129. 'This should be an update of the last segment in a period');
  130. var r = new shaka.media.SegmentReference(r1.position,
  131. r2.startTime, r2.endTime, r2.getUris, r2.startByte, r2.endByte);
  132. newReferences.push(r);
  133. } else {
  134. // Drop the new reference if there's an old reference with the
  135. // same time.
  136. newReferences.push(r1);
  137. }
  138. i++;
  139. j++;
  140. }
  141. }
  142. while (i < this.references_.length) {
  143. newReferences.push(this.references_[i++]);
  144. }
  145. if (newReferences.length) {
  146. // The rest of these refs may need to be renumbered.
  147. var nextPosition = newReferences[newReferences.length - 1].position + 1;
  148. while (j < references.length) {
  149. var r = references[j++];
  150. var r2 = new shaka.media.SegmentReference(nextPosition++,
  151. r.startTime, r.endTime, r.getUris, r.startByte, r.endByte);
  152. newReferences.push(r2);
  153. }
  154. } else {
  155. newReferences = references;
  156. }
  157. if (!COMPILED) {
  158. shaka.media.SegmentIndex.assertCorrectReferences_(newReferences);
  159. }
  160. this.references_ = newReferences;
  161. };
  162. /**
  163. * Removes all SegmentReferences that end before the given time.
  164. *
  165. * @param {number} time The time in seconds.
  166. * @export
  167. */
  168. shaka.media.SegmentIndex.prototype.evict = function(time) {
  169. for (var i = 0; i < this.references_.length; ++i) {
  170. if (this.references_[i].endTime > time)
  171. break;
  172. }
  173. this.references_.splice(0, i);
  174. };
  175. /**
  176. * Expands the first SegmentReference so it begins at the start of its Period
  177. * if it already begins close to the start of its Period, and expands or
  178. * contracts the last SegmentReference so it ends at the end of its Period for
  179. * VOD presentations.
  180. *
  181. * @param {?number} periodDuration
  182. */
  183. shaka.media.SegmentIndex.prototype.fit = function(periodDuration) {
  184. if (this.references_.length == 0)
  185. return;
  186. /** @const {number} */
  187. var tolerance = shaka.util.ManifestParserUtils.GAP_OVERLAP_TOLERANCE_SECONDS;
  188. goog.asserts.assert(periodDuration != null,
  189. 'Period duration must be known for static content!');
  190. goog.asserts.assert(periodDuration != Infinity,
  191. 'Period duration must be finite for static content!');
  192. var lastReference = this.references_[this.references_.length - 1];
  193. // Sanity check.
  194. goog.asserts.assert(
  195. lastReference.startTime < periodDuration,
  196. 'lastReference cannot begin after the end of the Period');
  197. if (lastReference.startTime > periodDuration) return;
  198. // Log warning if necessary.
  199. if (lastReference.endTime <= periodDuration - tolerance) {
  200. shaka.log.warning(
  201. 'The last segment should not end before the end of the Period.',
  202. lastReference);
  203. } else if (lastReference.endTime >= periodDuration + tolerance) {
  204. shaka.log.warning(
  205. 'The last segment should not end after the end of the Period.',
  206. lastReference);
  207. }
  208. // Adjust the last SegmentReference.
  209. this.references_[this.references_.length - 1] =
  210. new shaka.media.SegmentReference(
  211. lastReference.position,
  212. lastReference.startTime, periodDuration,
  213. lastReference.getUris,
  214. lastReference.startByte, lastReference.endByte);
  215. };
  216. if (!COMPILED) {
  217. /**
  218. * Asserts that the given SegmentReferences are sorted and have continuous,
  219. * increasing positions.
  220. *
  221. * @param {!Array.<shaka.media.SegmentReference>} references
  222. * @private
  223. */
  224. shaka.media.SegmentIndex.assertCorrectReferences_ = function(references) {
  225. goog.asserts.assert(references.every(function(r2, i) {
  226. if (i == 0) return true;
  227. var r1 = references[i - 1];
  228. if (r2.position != r1.position + 1) return false;
  229. if (r1.startTime < r2.startTime) {
  230. return true;
  231. } else if (r1.startTime > r2.startTime) {
  232. return false;
  233. } else {
  234. if (r1.endTime <= r2.endTime) {
  235. return true;
  236. } else {
  237. return false;
  238. }
  239. }
  240. }), 'SegmentReferences are incorrect');
  241. };
  242. }