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
  23. * to the end of the segment, inclusive. A value of null indicates that the
  24. * segment extends to the end of the resource.
  25. * @param {null|shaka.extern.MediaQualityInfo=} mediaQuality Information about
  26. * the quality of the media associated with this init segment.
  27. */
  28. constructor(uris, startByte, endByte, mediaQuality = null) {
  29. /** @type {function():!Array.<string>} */
  30. this.getUris = uris;
  31. /** @const {number} */
  32. this.startByte = startByte;
  33. /** @const {?number} */
  34. this.endByte = endByte;
  35. /** @const {shaka.extern.MediaQualityInfo|null} */
  36. this.mediaQuality = mediaQuality;
  37. }
  38. /**
  39. * Returns the offset from the start of the resource to the
  40. * start of the segment.
  41. *
  42. * @return {number}
  43. * @export
  44. */
  45. getStartByte() {
  46. return this.startByte;
  47. }
  48. /**
  49. * Returns the offset from the start of the resource to the end of the
  50. * segment, inclusive. A value of null indicates that the segment extends
  51. * to the end of the resource.
  52. *
  53. * @return {?number}
  54. * @export
  55. */
  56. getEndByte() {
  57. return this.endByte;
  58. }
  59. /**
  60. * Returns the size of the init segment.
  61. * @return {?number}
  62. */
  63. getSize() {
  64. if (this.endByte) {
  65. return this.endByte - this.startByte;
  66. } else {
  67. return null;
  68. }
  69. }
  70. /**
  71. * Returns media quality information for the segments associated with
  72. * this init segment.
  73. *
  74. * @return {?shaka.extern.MediaQualityInfo}
  75. */
  76. getMediaQuality() {
  77. return this.mediaQuality;
  78. }
  79. /**
  80. * Check if two initSegmentReference have all the same values.
  81. * @param {?shaka.media.InitSegmentReference} reference1
  82. * @param {?shaka.media.InitSegmentReference} reference2
  83. * @return {boolean}
  84. */
  85. static equal(reference1, reference2) {
  86. const ArrayUtils = shaka.util.ArrayUtils;
  87. if (!reference1 || !reference2) {
  88. return reference1 == reference2;
  89. } else {
  90. return reference1.getStartByte() == reference2.getStartByte() &&
  91. reference1.getEndByte() == reference2.getEndByte() &&
  92. ArrayUtils.equal(reference1.getUris(), reference2.getUris());
  93. }
  94. }
  95. };
  96. /**
  97. * SegmentReference provides the start time, end time, and location to a media
  98. * segment.
  99. *
  100. * @export
  101. */
  102. shaka.media.SegmentReference = class {
  103. /**
  104. * @param {number} startTime The segment's start time in seconds.
  105. * @param {number} endTime The segment's end time in seconds. The segment
  106. * ends the instant before this time, so |endTime| must be strictly greater
  107. * than |startTime|.
  108. * @param {function():!Array.<string>} uris
  109. * A function that creates the URIs of the resource containing the segment.
  110. * @param {number} startByte The offset from the start of the resource to the
  111. * start of the segment.
  112. * @param {?number} endByte The offset from the start of the resource to the
  113. * end of the segment, inclusive. A value of null indicates that the
  114. * segment extends to the end of the resource.
  115. * @param {shaka.media.InitSegmentReference} initSegmentReference
  116. * The segment's initialization segment metadata, or null if the segments
  117. * are self-initializing.
  118. * @param {number} timestampOffset
  119. * The amount of time, in seconds, that must be added to the segment's
  120. * internal timestamps to align it to the presentation timeline.
  121. * <br>
  122. * For DASH, this value should equal the Period start time minus the first
  123. * presentation timestamp of the first frame/sample in the Period. For
  124. * example, for MP4 based streams, this value should equal Period start
  125. * minus the first segment's tfdt box's 'baseMediaDecodeTime' field (after
  126. * it has been converted to seconds).
  127. * <br>
  128. * For HLS, this value should be 0 to keep the presentation time at the most
  129. * recent discontinuity minus the corresponding media time.
  130. * @param {number} appendWindowStart
  131. * The start of the append window for this reference, relative to the
  132. * presentation. Any content from before this time will be removed by
  133. * MediaSource.
  134. * @param {number} appendWindowEnd
  135. * The end of the append window for this reference, relative to the
  136. * presentation. Any content from after this time will be removed by
  137. * MediaSource.
  138. * @param {!Array.<!shaka.media.SegmentReference>=} partialReferences
  139. * A list of SegmentReferences for the partial segments.
  140. * @param {?string=} tilesLayout
  141. * The value is a grid-item-dimension consisting of two positive decimal
  142. * integers in the format: column-x-row ('4x3'). It describes the
  143. * arrangement of Images in a Grid. The minimum valid LAYOUT is '1x1'.
  144. * @param {?number=} tileDuration
  145. * The explicit duration of an individual tile within the tiles grid.
  146. * If not provided, the duration should be automatically calculated based on
  147. * the duration of the reference.
  148. */
  149. constructor(
  150. startTime, endTime, uris, startByte, endByte, initSegmentReference,
  151. timestampOffset, appendWindowStart, appendWindowEnd,
  152. partialReferences = [], tilesLayout = '', tileDuration = null) {
  153. // A preload hinted Partial Segment has the same startTime and endTime.
  154. goog.asserts.assert(startTime <= endTime,
  155. 'startTime must be less than or equal to endTime');
  156. goog.asserts.assert((endByte == null) || (startByte < endByte),
  157. 'startByte must be < endByte');
  158. /** @type {number} */
  159. this.startTime = startTime;
  160. /** @type {number} */
  161. this.endTime = endTime;
  162. /**
  163. * The "true" end time of the segment, without considering the period end
  164. * time. This is necessary for thumbnail segments, where timing requires us
  165. * to know the original segment duration as described in the manifest.
  166. * @type {number}
  167. */
  168. this.trueEndTime = endTime;
  169. /** @type {function():!Array.<string>} */
  170. this.getUrisInner = uris;
  171. /** @const {number} */
  172. this.startByte = startByte;
  173. /** @const {?number} */
  174. this.endByte = endByte;
  175. /** @type {shaka.media.InitSegmentReference} */
  176. this.initSegmentReference = initSegmentReference;
  177. /** @type {number} */
  178. this.timestampOffset = timestampOffset;
  179. /** @type {number} */
  180. this.appendWindowStart = appendWindowStart;
  181. /** @type {number} */
  182. this.appendWindowEnd = appendWindowEnd;
  183. /** @type {!Array.<!shaka.media.SegmentReference>} */
  184. this.partialReferences = partialReferences;
  185. /** @type {?string} */
  186. this.tilesLayout = tilesLayout;
  187. /** @type {?number} */
  188. this.tileDuration = tileDuration;
  189. }
  190. /**
  191. * Creates and returns the URIs of the resource containing the segment.
  192. *
  193. * @return {!Array.<string>}
  194. * @export
  195. */
  196. getUris() {
  197. return this.getUrisInner();
  198. }
  199. /**
  200. * Returns the segment's start time in seconds.
  201. *
  202. * @return {number}
  203. * @export
  204. */
  205. getStartTime() {
  206. return this.startTime;
  207. }
  208. /**
  209. * Returns the segment's end time in seconds.
  210. *
  211. * @return {number}
  212. * @export
  213. */
  214. getEndTime() {
  215. return this.endTime;
  216. }
  217. /**
  218. * Returns the offset from the start of the resource to the
  219. * start of the segment.
  220. *
  221. * @return {number}
  222. * @export
  223. */
  224. getStartByte() {
  225. return this.startByte;
  226. }
  227. /**
  228. * Returns the offset from the start of the resource to the end of the
  229. * segment, inclusive. A value of null indicates that the segment extends to
  230. * the end of the resource.
  231. *
  232. * @return {?number}
  233. * @export
  234. */
  235. getEndByte() {
  236. return this.endByte;
  237. }
  238. /**
  239. * Returns the size of the segment.
  240. * @return {?number}
  241. */
  242. getSize() {
  243. if (this.endByte) {
  244. return this.endByte - this.startByte;
  245. } else {
  246. return null;
  247. }
  248. }
  249. /**
  250. * Returns true if it contains partial SegmentReferences.
  251. * @return {boolean}
  252. */
  253. hasPartialSegments() {
  254. return this.partialReferences.length > 0;
  255. }
  256. /**
  257. * Returns the segment's tiles layout. Only defined in image segments.
  258. *
  259. * @return {?string}
  260. * @export
  261. */
  262. getTilesLayout() {
  263. return this.tilesLayout;
  264. }
  265. /**
  266. * Returns the segment's explicit tile duration.
  267. * Only defined in image segments.
  268. *
  269. * @return {?number}
  270. * @export
  271. */
  272. getTileDuration() {
  273. return this.tileDuration;
  274. }
  275. };
  276. /**
  277. * A convenient typedef for when either type of reference is acceptable.
  278. *
  279. * @typedef {shaka.media.InitSegmentReference|shaka.media.SegmentReference}
  280. */
  281. shaka.media.AnySegmentReference;