Source: lib/hls/hls_classes.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.hls.Attribute');
  18. goog.provide('shaka.hls.Playlist');
  19. goog.provide('shaka.hls.PlaylistType');
  20. goog.provide('shaka.hls.Segment');
  21. goog.provide('shaka.hls.Tag');
  22. goog.require('goog.asserts');
  23. /**
  24. * Creates an HLS playlist object.
  25. *
  26. * @param {!string} uri
  27. * @param {!shaka.hls.PlaylistType} type
  28. * @param {!Array.<shaka.hls.Tag>} tags
  29. * @param {!Array.<shaka.hls.Segment>=} opt_segments
  30. *
  31. * @constructor
  32. * @struct
  33. */
  34. shaka.hls.Playlist = function(uri, type, tags, opt_segments) {
  35. /** @const {!string} */
  36. this.uri = uri;
  37. /** @const {shaka.hls.PlaylistType} */
  38. this.type = type;
  39. /** @const {!Array.<!shaka.hls.Tag>} */
  40. this.tags = tags;
  41. /** @const {Array.<!shaka.hls.Segment>} */
  42. this.segments = opt_segments || null;
  43. };
  44. /**
  45. * @enum {number}
  46. */
  47. shaka.hls.PlaylistType = {
  48. MASTER: 0,
  49. MEDIA: 1
  50. };
  51. /**
  52. * Creates an HLS tag object.
  53. *
  54. * @param {number} id
  55. * @param {!string} name
  56. * @param {!Array.<shaka.hls.Attribute>} attributes
  57. * @param {?string=} opt_value
  58. *
  59. * @constructor
  60. * @struct
  61. */
  62. shaka.hls.Tag = function(id, name, attributes, opt_value) {
  63. goog.asserts.assert(
  64. (attributes.length == 0 && opt_value) ||
  65. (attributes.length > 0 && !opt_value) ||
  66. (attributes.length == 0 && !opt_value),
  67. 'Tags can only take the form ' +
  68. '(1) <NAME>:<VALUE> ' +
  69. '(2) <NAME>:<ATTRIBUTE_LIST> ' +
  70. ' (3) <NAME>');
  71. /** @const {number} */
  72. this.id = id;
  73. /** @const {!string} */
  74. this.name = name;
  75. /** @const {Array.<shaka.hls.Attribute>} */
  76. this.attributes = attributes;
  77. /** @const {?string} */
  78. this.value = opt_value || null;
  79. };
  80. /**
  81. * Create the string representation of the tag.
  82. *
  83. * For the DRM system - the full tag needs to be passed down to the CDM. There
  84. * are two ways of doing this (1) save the original tag or (2) recreate the tag.
  85. * As with some cases (like in tests) the tag never existed in string form, it
  86. * is far easier to recreate the tag from the parsed form.
  87. *
  88. * @return {string}
  89. * @override
  90. */
  91. shaka.hls.Tag.prototype.toString = function() {
  92. /**
  93. * @param {shaka.hls.Attribute} attr
  94. * @return {!string}
  95. */
  96. var attr_to_str = function(attr) {
  97. return attr.name + '="' + attr.value + '"';
  98. };
  99. // A valid tag can only follow 1 of 3 patterns.
  100. // 1) <NAME>:<VALUE>
  101. // 2) <NAME>:<ATTRIBUTE LIST>
  102. // 3) <NAME>
  103. if (this.value) {
  104. return '#' + this.name + ':' + this.value;
  105. }
  106. if (this.attributes.length > 0) {
  107. return '#' + this.name + ':' + this.attributes.map(attr_to_str).join(',');
  108. }
  109. return '#' + this.name;
  110. };
  111. /**
  112. * Creates an HLS attribute object.
  113. *
  114. * @param {!string} name
  115. * @param {!string} value
  116. *
  117. * @constructor
  118. * @struct
  119. */
  120. shaka.hls.Attribute = function(name, value) {
  121. /** @const {!string} */
  122. this.name = name;
  123. /** @const {!string} */
  124. this.value = value;
  125. };
  126. /**
  127. * Adds an attribute to an HLS Tag.
  128. *
  129. * @param {!shaka.hls.Attribute} attribute
  130. */
  131. shaka.hls.Tag.prototype.addAttribute = function(attribute) {
  132. this.attributes.push(attribute);
  133. };
  134. /**
  135. * Gets the first attribute of the tag with a specified name.
  136. *
  137. * @param {!string} name
  138. * @return {?shaka.hls.Attribute} attribute
  139. */
  140. shaka.hls.Tag.prototype.getAttribute = function(name) {
  141. var attributes = this.attributes.filter(function(attr) {
  142. return attr.name == name;
  143. });
  144. goog.asserts.assert(attributes.length < 2,
  145. 'A tag should not have multiple attributes ' +
  146. 'with the same name!');
  147. if (attributes.length)
  148. return attributes[0];
  149. else
  150. return null;
  151. };
  152. /**
  153. * Gets the value of the first attribute of the tag with a specified name.
  154. * If not found, returns an optional default value.
  155. *
  156. * @param {!string} name
  157. * @param {string=} opt_defaultValue
  158. * @return {?string}
  159. */
  160. shaka.hls.Tag.prototype.getAttributeValue = function(name, opt_defaultValue) {
  161. var defaultValue = opt_defaultValue || null;
  162. var attribute = this.getAttribute(name);
  163. return attribute ? attribute.value : defaultValue;
  164. };
  165. /**
  166. * Creates an HLS segment object.
  167. *
  168. * @param {!string} uri
  169. * @param {!Array.<shaka.hls.Tag>} tags
  170. *
  171. * @constructor
  172. * @struct
  173. */
  174. shaka.hls.Segment = function(uri, tags) {
  175. /** @const {!Array.<shaka.hls.Tag>} */
  176. this.tags = tags;
  177. /** @const {!string} */
  178. this.uri = uri;
  179. };