Source: lib/media/segment_reference.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.media.InitSegmentReference');
  7. goog.provide('shaka.media.SegmentReference');
  8. goog.require('goog.asserts');
  9. goog.require('shaka.util.ArrayUtils');
  10. /**
  11. * Creates an InitSegmentReference, which provides the location to an
  12. * initialization segment.
  13. *
  14. * @export
  15. */
  16. shaka.media.InitSegmentReference = class {
  17. /**
  18. * @param {function():!Array.<string>} uris A function that creates the URIs
  19. * of the resource containing the segment.
  20. * @param {number} startByte The offset from the start of the resource to the
  21. * start of the segment.
  22. * @param {?number} endByte The offset from the start of the resource to the
  23. * end of the segment, inclusive. A value of null indicates that the
  24. * segment extends to the end of the resource.
  25. */
  26. constructor(uris, startByte, endByte) {
  27. /** @type {function():!Array.<string>} */
  28. this.getUris = uris;
  29. /** @const {number} */
  30. this.startByte = startByte;
  31. /** @const {?number} */
  32. this.endByte = endByte;
  33. }
  34. /**
  35. * Returns the offset from the start of the resource to the
  36. * start of the segment.
  37. *
  38. * @return {number}
  39. * @export
  40. */
  41. getStartByte() {
  42. return this.startByte;
  43. }
  44. /**
  45. * Returns the offset from the start of the resource to the end of the
  46. * segment, inclusive. A value of null indicates that the segment extends
  47. * to the end of the resource.
  48. *
  49. * @return {?number}
  50. * @export
  51. */
  52. getEndByte() {
  53. return this.endByte;
  54. }
  55. /**
  56. * Returns the size of the init segment.
  57. * @return {?number}
  58. */
  59. getSize() {
  60. if (this.endByte) {
  61. return this.endByte - this.startByte;
  62. } else {
  63. return null;
  64. }
  65. }
  66. /**
  67. * Check if two initSegmentReference have all the same values.
  68. * @param {?shaka.media.InitSegmentReference} reference1
  69. * @param {?shaka.media.InitSegmentReference} reference2
  70. * @return {boolean}
  71. */
  72. static equal(reference1, reference2) {
  73. const ArrayUtils = shaka.util.ArrayUtils;
  74. if (!reference1 || !reference2) {
  75. return reference1 == reference2;
  76. } else {
  77. return reference1.getStartByte() == reference2.getStartByte() &&
  78. reference1.getEndByte() == reference2.getEndByte() &&
  79. ArrayUtils.equal(reference1.getUris(), reference2.getUris());
  80. }
  81. }
  82. };
  83. /**
  84. * SegmentReference provides the start time, end time, and location to a media
  85. * segment.
  86. *
  87. * @export
  88. */
  89. shaka.media.SegmentReference = class {
  90. /**
  91. * @param {number} startTime The segment's start time in seconds.
  92. * @param {number} endTime The segment's end time in seconds. The segment
  93. * ends the instant before this time, so |endTime| must be strictly greater
  94. * than |startTime|.
  95. * @param {function():!Array.<string>} uris
  96. * A function that creates the URIs of the resource containing the segment.
  97. * @param {number} startByte The offset from the start of the resource to the
  98. * start of the segment.
  99. * @param {?number} endByte The offset from the start of the resource to the
  100. * end of the segment, inclusive. A value of null indicates that the
  101. * segment extends to the end of the resource.
  102. * @param {shaka.media.InitSegmentReference} initSegmentReference
  103. * The segment's initialization segment metadata, or null if the segments
  104. * are self-initializing.
  105. * @param {number} timestampOffset
  106. * The amount of time, in seconds, that must be added to the segment's
  107. * internal timestamps to align it to the presentation timeline.
  108. * <br>
  109. * For DASH, this value should equal the Period start time minus the first
  110. * presentation timestamp of the first frame/sample in the Period. For
  111. * example, for MP4 based streams, this value should equal Period start
  112. * minus the first segment's tfdt box's 'baseMediaDecodeTime' field (after
  113. * it has been converted to seconds).
  114. * <br>
  115. * For HLS, this value should be 0 to keep the presentation time at the most
  116. * recent discontinuity minus the corresponding media time.
  117. * @param {number} appendWindowStart
  118. * The start of the append window for this reference, relative to the
  119. * presentation. Any content from before this time will be removed by
  120. * MediaSource.
  121. * @param {number} appendWindowEnd
  122. * The end of the append window for this reference, relative to the
  123. * presentation. Any content from after this time will be removed by
  124. * MediaSource.
  125. * @param {!Array.<!shaka.media.SegmentReference>=} partialReferences
  126. A list of SegmentReferences for the partial segments.
  127. */
  128. constructor(
  129. startTime, endTime, uris, startByte, endByte, initSegmentReference,
  130. timestampOffset, appendWindowStart, appendWindowEnd,
  131. partialReferences = []) {
  132. // A preload hinted Partial Segment has the same startTime and endTime.
  133. goog.asserts.assert(startTime <= endTime,
  134. 'startTime must be less than or equal to endTime');
  135. goog.asserts.assert((endByte == null) || (startByte < endByte),
  136. 'startByte must be < endByte');
  137. /** @type {number} */
  138. this.startTime = startTime;
  139. /** @type {number} */
  140. this.endTime = endTime;
  141. /** @type {function():!Array.<string>} */
  142. this.getUrisInner = uris;
  143. /** @const {number} */
  144. this.startByte = startByte;
  145. /** @const {?number} */
  146. this.endByte = endByte;
  147. /** @type {shaka.media.InitSegmentReference} */
  148. this.initSegmentReference = initSegmentReference;
  149. /** @type {number} */
  150. this.timestampOffset = timestampOffset;
  151. /** @type {number} */
  152. this.appendWindowStart = appendWindowStart;
  153. /** @type {number} */
  154. this.appendWindowEnd = appendWindowEnd;
  155. /** @type {!Array.<!shaka.media.SegmentReference>} */
  156. this.partialReferences = partialReferences;
  157. }
  158. /**
  159. * Creates and returns the URIs of the resource containing the segment.
  160. *
  161. * @return {!Array.<string>}
  162. * @export
  163. */
  164. getUris() {
  165. return this.getUrisInner();
  166. }
  167. /**
  168. * Returns the segment's start time in seconds.
  169. *
  170. * @return {number}
  171. * @export
  172. */
  173. getStartTime() {
  174. return this.startTime;
  175. }
  176. /**
  177. * Returns the segment's end time in seconds.
  178. *
  179. * @return {number}
  180. * @export
  181. */
  182. getEndTime() {
  183. return this.endTime;
  184. }
  185. /**
  186. * Returns the offset from the start of the resource to the
  187. * start of the segment.
  188. *
  189. * @return {number}
  190. * @export
  191. */
  192. getStartByte() {
  193. return this.startByte;
  194. }
  195. /**
  196. * Returns the offset from the start of the resource to the end of the
  197. * segment, inclusive. A value of null indicates that the segment extends to
  198. * the end of the resource.
  199. *
  200. * @return {?number}
  201. * @export
  202. */
  203. getEndByte() {
  204. return this.endByte;
  205. }
  206. /**
  207. * Returns the size of the segment.
  208. * @return {?number}
  209. */
  210. getSize() {
  211. if (this.endByte) {
  212. return this.endByte - this.startByte;
  213. } else {
  214. return null;
  215. }
  216. }
  217. /**
  218. * Returns true if it contains partial SegmentReferences.
  219. * @return {boolean}
  220. */
  221. hasPartialSegments() {
  222. return this.partialReferences.length > 0;
  223. }
  224. };
  225. /**
  226. * A convenient typedef for when either type of reference is acceptable.
  227. *
  228. * @typedef {shaka.media.InitSegmentReference|shaka.media.SegmentReference}
  229. */
  230. shaka.media.AnySegmentReference;