Source: lib/hls/hls_classes.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.hls.Attribute');
  7. goog.provide('shaka.hls.Playlist');
  8. goog.provide('shaka.hls.PlaylistType');
  9. goog.provide('shaka.hls.Segment');
  10. goog.provide('shaka.hls.Tag');
  11. goog.require('goog.asserts');
  12. goog.require('shaka.util.Error');
  13. /**
  14. * HLS playlist class.
  15. */
  16. shaka.hls.Playlist = class {
  17. /**
  18. * @param {!shaka.hls.PlaylistType} type
  19. * @param {!Array.<shaka.hls.Tag>} tags
  20. * @param {!Array.<shaka.hls.Segment>=} segments
  21. */
  22. constructor(type, tags, segments) {
  23. /** @const {shaka.hls.PlaylistType} */
  24. this.type = type;
  25. /** @const {!Array.<!shaka.hls.Tag>} */
  26. this.tags = tags;
  27. /** @const {Array.<!shaka.hls.Segment>} */
  28. this.segments = segments || null;
  29. }
  30. };
  31. /**
  32. * @enum {number}
  33. */
  34. shaka.hls.PlaylistType = {
  35. MASTER: 0,
  36. MEDIA: 1,
  37. };
  38. /**
  39. * HLS tag class.
  40. */
  41. shaka.hls.Tag = class {
  42. /**
  43. * @param {number} id
  44. * @param {string} name
  45. * @param {!Array.<shaka.hls.Attribute>} attributes
  46. * @param {?string=} value
  47. */
  48. constructor(id, name, attributes, value = null) {
  49. /** @const {number} */
  50. this.id = id;
  51. /** @type {string} */
  52. this.name = name;
  53. /** @const {!Array.<shaka.hls.Attribute>} */
  54. this.attributes = attributes;
  55. /** @const {?string} */
  56. this.value = value;
  57. }
  58. /**
  59. * Create the string representation of the tag.
  60. *
  61. * For the DRM system - the full tag needs to be passed down to the CDM.
  62. * There are two ways of doing this (1) save the original tag or (2) recreate
  63. * the tag.
  64. * As in some cases (like in tests) the tag never existed in string form, it
  65. * is far easier to recreate the tag from the parsed form.
  66. *
  67. * @param {?Set.<string>=} attributesToSkip
  68. * @return {string}
  69. * @override
  70. */
  71. toString(attributesToSkip) {
  72. /**
  73. * @param {shaka.hls.Attribute} attr
  74. * @return {string}
  75. */
  76. const attrToStr = (attr) => {
  77. const isNumericAttr = !isNaN(Number(attr.value));
  78. const value = (isNumericAttr ? attr.value : '"' + attr.value + '"');
  79. return attr.name + '=' + value;
  80. };
  81. // A valid tag can only follow 1 of 4 patterns.
  82. // 1) <NAME>:<VALUE>
  83. // 2) <NAME>:<ATTRIBUTE LIST>
  84. // 3) <NAME>
  85. // 4) <NAME>:<VALUE>,<ATTRIBUTE_LIST>
  86. let tagStr = '#' + this.name;
  87. const appendages = this.attributes ? this.attributes.filter((attr) => {
  88. if (!attributesToSkip) {
  89. return true;
  90. }
  91. return !attributesToSkip.has(attr.name);
  92. }).map(attrToStr) : [];
  93. if (this.value) {
  94. appendages.unshift(this.value);
  95. }
  96. if (appendages.length > 0) {
  97. tagStr += ':' + appendages.join(',');
  98. }
  99. return tagStr;
  100. }
  101. /**
  102. * Create the string key of the tag.
  103. *
  104. * @return {string}
  105. */
  106. getTagKey() {
  107. const attributesToSkip = new Set()
  108. .add('AUDIO')
  109. .add('VIDEO')
  110. .add('SUBTITLES')
  111. .add('PATHWAY-ID')
  112. .add('GROUP-ID')
  113. .add('URI');
  114. return this.toString(attributesToSkip);
  115. }
  116. /**
  117. * Adds an attribute to an HLS Tag.
  118. *
  119. * @param {!shaka.hls.Attribute} attribute
  120. */
  121. addAttribute(attribute) {
  122. this.attributes.push(attribute);
  123. }
  124. /**
  125. * Gets the first attribute of the tag with a specified name.
  126. *
  127. * @param {string} name
  128. * @return {?shaka.hls.Attribute} attribute
  129. */
  130. getAttribute(name) {
  131. const attributes = this.attributes.filter((attr) => {
  132. return attr.name == name;
  133. });
  134. goog.asserts.assert(attributes.length < 2,
  135. 'A tag should not have multiple attributes ' +
  136. 'with the same name!');
  137. if (attributes.length) {
  138. return attributes[0];
  139. } else {
  140. return null;
  141. }
  142. }
  143. /**
  144. * Gets the value of the first attribute of the tag with a specified name.
  145. * If not found, returns an optional default value.
  146. *
  147. * @param {string} name
  148. * @param {string=} defaultValue
  149. * @return {?string}
  150. */
  151. getAttributeValue(name, defaultValue) {
  152. const attribute = this.getAttribute(name);
  153. return attribute ? attribute.value : (defaultValue || null);
  154. }
  155. /**
  156. * Finds the attribute and returns its value.
  157. * Throws an error if attribute was not found.
  158. *
  159. * @param {string} name
  160. * @return {string}
  161. */
  162. getRequiredAttrValue(name) {
  163. const attribute = this.getAttribute(name);
  164. if (!attribute) {
  165. throw new shaka.util.Error(
  166. shaka.util.Error.Severity.CRITICAL,
  167. shaka.util.Error.Category.MANIFEST,
  168. shaka.util.Error.Code.HLS_REQUIRED_ATTRIBUTE_MISSING,
  169. name);
  170. }
  171. return attribute.value;
  172. }
  173. /**
  174. * Set the name of the tag. Used only for Preload hinted MAP tag.
  175. * @param {string} name
  176. */
  177. setName(name) {
  178. this.name = name;
  179. }
  180. };
  181. /**
  182. * HLS segment class.
  183. */
  184. shaka.hls.Segment = class {
  185. /**
  186. * Creates an HLS segment object.
  187. *
  188. * @param {string} verbatimSegmentUri verbatim segment URI.
  189. * @param {!Array.<shaka.hls.Tag>} tags
  190. * @param {!Array.<shaka.hls.Tag>=} partialSegments
  191. */
  192. constructor(verbatimSegmentUri, tags, partialSegments=[]) {
  193. /** @const {!Array.<shaka.hls.Tag>} */
  194. this.tags = tags;
  195. /** @const {?string} */
  196. this.verbatimSegmentUri = verbatimSegmentUri;
  197. /** @type {!Array.<shaka.hls.Tag>} */
  198. this.partialSegments = partialSegments;
  199. }
  200. };
  201. /**
  202. * HLS Attribute class.
  203. */
  204. shaka.hls.Attribute = class {
  205. /**
  206. * Creates an HLS attribute object.
  207. *
  208. * @param {string} name
  209. * @param {string} value
  210. */
  211. constructor(name, value) {
  212. /** @const {string} */
  213. this.name = name;
  214. /** @const {string} */
  215. this.value = value;
  216. }
  217. };