Source: lib/media/segment_reference.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.InitSegmentReference');
  18. goog.provide('shaka.media.SegmentReference');
  19. goog.require('goog.asserts');
  20. /**
  21. * Creates an InitSegmentReference, which provides the location to an
  22. * initialization segment.
  23. *
  24. * @param {function():!Array.<string>} uris A function that creates the URIs of
  25. * the resource containing the segment.
  26. * @param {number} startByte The offset from the start of the resource to the
  27. * start of the segment.
  28. * @param {?number} endByte The offset from the start of the resource to the
  29. * end of the segment, inclusive. A value of null indicates that the segment
  30. * extends to the end of the resource.
  31. *
  32. * @constructor
  33. * @struct
  34. * @export
  35. */
  36. shaka.media.InitSegmentReference = function(uris, startByte, endByte) {
  37. /** @type {function():!Array.<string>} */
  38. this.getUris = uris;
  39. /** @const {number} */
  40. this.startByte = startByte;
  41. /** @const {?number} */
  42. this.endByte = endByte;
  43. };
  44. /**
  45. * Creates the URIs of the resource containing the segment.
  46. *
  47. * @return {!Array.<string>}
  48. * @export
  49. */
  50. shaka.media.InitSegmentReference.prototype.createUris = function() {
  51. return this.getUris();
  52. };
  53. /**
  54. * Returns the offset from the start of the resource to the
  55. * start of the segment.
  56. *
  57. * @return {number}
  58. * @export
  59. */
  60. shaka.media.InitSegmentReference.prototype.getStartByte = function() {
  61. return this.startByte;
  62. };
  63. /**
  64. * Returns the offset from the start of the resource to the end of the segment,
  65. * inclusive. A value of null indicates that the segment extends to the end of
  66. * the resource.
  67. *
  68. * @return {?number}
  69. * @export
  70. */
  71. shaka.media.InitSegmentReference.prototype.getEndByte = function() {
  72. return this.endByte;
  73. };
  74. /**
  75. * Creates a SegmentReference, which provides the start time, end time, and
  76. * location to a media segment.
  77. *
  78. * @param {number} position The segment's position within a particular Period.
  79. * The following should hold true between any two SegmentReferences from the
  80. * same Period, r1 and r2:
  81. * IF r2.position > r1.position THEN
  82. * [ (r2.startTime > r1.startTime) OR
  83. * (r2.startTime == r1.startTime AND r2.endTime >= r1.endTime) ]
  84. * @param {number} startTime The segment's start time in seconds, relative to
  85. * the start of a particular Period.
  86. * @param {number} endTime The segment's end time in seconds, relative to
  87. * the start of a particular Period. The segment ends the instant before
  88. * this time, so |endTime| must be strictly greater than |startTime|.
  89. * @param {function():!Array.<string>} uris
  90. * A function that creates the URIs of the resource containing the segment.
  91. * @param {number} startByte The offset from the start of the resource to the
  92. * start of the segment.
  93. * @param {?number} endByte The offset from the start of the resource to the
  94. * end of the segment, inclusive. A value of null indicates that the segment
  95. * extends to the end of the resource.
  96. *
  97. * @constructor
  98. * @struct
  99. * @export
  100. */
  101. shaka.media.SegmentReference = function(
  102. position, startTime, endTime, uris, startByte, endByte) {
  103. goog.asserts.assert(startTime < endTime,
  104. 'startTime must be less than endTime');
  105. goog.asserts.assert((startByte < endByte) || (endByte == null),
  106. 'startByte must be < endByte');
  107. /** @const {number} */
  108. this.position = position;
  109. /** @type {number} */
  110. this.startTime = startTime;
  111. /** @type {number} */
  112. this.endTime = endTime;
  113. /** @type {function():!Array.<string>} */
  114. this.getUris = uris;
  115. /** @const {number} */
  116. this.startByte = startByte;
  117. /** @const {?number} */
  118. this.endByte = endByte;
  119. };
  120. /**
  121. * Returns the segment's position within a particular Period.
  122. *
  123. * @return {number} The segment's position.
  124. * @export
  125. */
  126. shaka.media.SegmentReference.prototype.getPosition = function() {
  127. return this.position;
  128. };
  129. /**
  130. * Returns the segment's start time in seconds, relative to
  131. * the start of a particular Period.
  132. *
  133. * @return {number}
  134. * @export
  135. */
  136. shaka.media.SegmentReference.prototype.getStartTime = function() {
  137. return this.startTime;
  138. };
  139. /**
  140. * Returns the segment's end time in seconds, relative to
  141. * the start of a particular Period.
  142. *
  143. * @return {number}
  144. * @export
  145. */
  146. shaka.media.SegmentReference.prototype.getEndTime = function() {
  147. return this.endTime;
  148. };
  149. /**
  150. * Creates the URIs of the resource containing the segment.
  151. *
  152. * @return {!Array.<string>}
  153. * @export
  154. */
  155. shaka.media.SegmentReference.prototype.createUris = function() {
  156. return this.getUris();
  157. };
  158. /**
  159. * Returns the offset from the start of the resource to the
  160. * start of the segment.
  161. *
  162. * @return {number}
  163. * @export
  164. */
  165. shaka.media.SegmentReference.prototype.getStartByte = function() {
  166. return this.startByte;
  167. };
  168. /**
  169. * Returns the offset from the start of the resource to the end of the segment,
  170. * inclusive. A value of null indicates that the segment extends to the end of
  171. * the resource.
  172. *
  173. * @return {?number}
  174. * @export
  175. */
  176. shaka.media.SegmentReference.prototype.getEndByte = function() {
  177. return this.endByte;
  178. };
  179. /**
  180. * A convenient typedef for when either type of reference is acceptable.
  181. *
  182. * @typedef {shaka.media.InitSegmentReference|shaka.media.SegmentReference}
  183. */
  184. shaka.media.AnySegmentReference;