Source: lib/hls/manifest_text_parser.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.ManifestTextParser');
  18. goog.require('shaka.hls.Attribute');
  19. goog.require('shaka.hls.Playlist');
  20. goog.require('shaka.hls.PlaylistType');
  21. goog.require('shaka.hls.Segment');
  22. goog.require('shaka.hls.Tag');
  23. goog.require('shaka.hls.Utils');
  24. goog.require('shaka.util.Error');
  25. goog.require('shaka.util.StringUtils');
  26. goog.require('shaka.util.TextParser');
  27. /**
  28. * Creates a new ManifestTextParser.
  29. *
  30. * @constructor
  31. * @struct
  32. */
  33. shaka.hls.ManifestTextParser = function() {
  34. /** @private {number} */
  35. this.globalId_ = 0;
  36. };
  37. /**
  38. * @param {ArrayBuffer} data
  39. * @param {string} absolutePlaylistUri An absolute, final URI after redirects.
  40. * @return {!shaka.hls.Playlist}
  41. * @throws {shaka.util.Error}
  42. */
  43. shaka.hls.ManifestTextParser.prototype.parsePlaylist =
  44. function(data, absolutePlaylistUri) {
  45. const MEDIA_PLAYLIST_TAGS = shaka.hls.ManifestTextParser.MEDIA_PLAYLIST_TAGS;
  46. const SEGMENT_TAGS = shaka.hls.ManifestTextParser.SEGMENT_TAGS;
  47. // Get the input as a string. Normalize newlines to \n.
  48. let str = shaka.util.StringUtils.fromUTF8(data);
  49. str = str.replace(/\r\n|\r(?=[^\n]|$)/gm, '\n').trim();
  50. const lines = str.split(/\n+/m);
  51. if (!/^#EXTM3U($|[ \t\n])/m.test(lines[0])) {
  52. throw new shaka.util.Error(
  53. shaka.util.Error.Severity.CRITICAL,
  54. shaka.util.Error.Category.MANIFEST,
  55. shaka.util.Error.Code.HLS_PLAYLIST_HEADER_MISSING);
  56. }
  57. /** shaka.hls.PlaylistType */
  58. let playlistType = shaka.hls.PlaylistType.MASTER;
  59. // First, look for media playlist tags, so that we know what the playlist
  60. // type really is before we start parsing.
  61. for (let i = 1; i < lines.length; i++) {
  62. // Ignore comments.
  63. if (!shaka.hls.Utils.isComment(lines[i])) {
  64. const tag = this.parseTag_(lines[i]);
  65. // These tags won't actually be used, so don't increment the global id.
  66. this.globalId_ -= 1;
  67. if (MEDIA_PLAYLIST_TAGS.indexOf(tag.name) >= 0) {
  68. playlistType = shaka.hls.PlaylistType.MEDIA;
  69. break;
  70. } else if (tag.name == 'EXT-X-STREAM-INF') {
  71. i += 1;
  72. }
  73. }
  74. }
  75. /** {Array.<shaka.hls.Tag>} */
  76. const tags = [];
  77. for (let i = 1; i < lines.length;) {
  78. // Skip comments
  79. if (shaka.hls.Utils.isComment(lines[i])) {
  80. i += 1;
  81. continue;
  82. }
  83. const tag = this.parseTag_(lines[i]);
  84. if (SEGMENT_TAGS.includes(tag.name)) {
  85. if (playlistType != shaka.hls.PlaylistType.MEDIA) {
  86. // Only media playlists should contain segment tags
  87. throw new shaka.util.Error(
  88. shaka.util.Error.Severity.CRITICAL,
  89. shaka.util.Error.Category.MANIFEST,
  90. shaka.util.Error.Code.HLS_INVALID_PLAYLIST_HIERARCHY);
  91. }
  92. const segmentsData = lines.splice(i, lines.length - i);
  93. const segments = this.parseSegments_(
  94. absolutePlaylistUri, segmentsData, tags);
  95. return new shaka.hls.Playlist(
  96. absolutePlaylistUri, playlistType, tags, segments);
  97. }
  98. tags.push(tag);
  99. i += 1;
  100. // An EXT-X-STREAM-INF tag is followed by a URI of a media playlist.
  101. // Add the URI to the tag object.
  102. if (tag.name == 'EXT-X-STREAM-INF') {
  103. const tagUri = new shaka.hls.Attribute('URI', lines[i]);
  104. tag.addAttribute(tagUri);
  105. i += 1;
  106. }
  107. }
  108. return new shaka.hls.Playlist(absolutePlaylistUri, playlistType, tags);
  109. };
  110. /**
  111. * Parses an array of strings into an array of HLS Segment objects.
  112. *
  113. * @param {string} absoluteMediaPlaylistUri
  114. * @param {!Array.<string>} lines
  115. * @param {!Array.<!shaka.hls.Tag>} playlistTags
  116. * @return {!Array.<shaka.hls.Segment>}
  117. * @private
  118. * @throws {shaka.util.Error}
  119. */
  120. shaka.hls.ManifestTextParser.prototype.parseSegments_ =
  121. function(absoluteMediaPlaylistUri, lines, playlistTags) {
  122. /** @type {!Array.<shaka.hls.Segment>} */
  123. let segments = [];
  124. /** @type {!Array.<shaka.hls.Tag>} */
  125. let segmentTags = [];
  126. lines.forEach((line) => {
  127. if (/^(#EXT)/.test(line)) {
  128. let tag = this.parseTag_(line);
  129. if (shaka.hls.ManifestTextParser.MEDIA_PLAYLIST_TAGS
  130. .indexOf(tag.name) >= 0) {
  131. playlistTags.push(tag);
  132. } else {
  133. segmentTags.push(tag);
  134. }
  135. } else if (shaka.hls.Utils.isComment(line)) {
  136. // Skip comments.
  137. return [];
  138. } else {
  139. const verbatimSegmentUri = line.trim();
  140. const absoluteSegmentUri = shaka.hls.Utils.constructAbsoluteUri(
  141. absoluteMediaPlaylistUri, verbatimSegmentUri);
  142. // The URI appears after all of the tags describing the segment.
  143. const segment = new shaka.hls.Segment(absoluteSegmentUri, segmentTags);
  144. segments.push(segment);
  145. segmentTags = [];
  146. }
  147. });
  148. return segments;
  149. };
  150. /**
  151. * Parses a string into an HLS Tag object while tracking what id to use next.
  152. *
  153. * @param {string} word
  154. * @return {!shaka.hls.Tag}
  155. * @throws {shaka.util.Error}
  156. * @private
  157. */
  158. shaka.hls.ManifestTextParser.prototype.parseTag_ = function(word) {
  159. return shaka.hls.ManifestTextParser.parseTag(this.globalId_++, word);
  160. };
  161. /**
  162. * Parses a string into an HLS Tag object.
  163. *
  164. * @param {number} id
  165. * @param {string} word
  166. * @return {!shaka.hls.Tag}
  167. * @throws {shaka.util.Error}
  168. */
  169. shaka.hls.ManifestTextParser.parseTag = function(id, word) {
  170. /* HLS tags start with '#EXT'. A tag can have a set of attributes
  171. (#EXT-<tagname>:<attribute list>) or a value (#EXT-<tagname>:<value>).
  172. An attribute's format is 'AttributeName=AttributeValue'.
  173. The parsing logic goes like this:
  174. 1. Everything before ':' is a name (we ignore '#').
  175. 2. Everything after should be parsed as attributes if it contains '='.
  176. 3. Otherwise, this is a value.
  177. 4. If there is no ":", it's a simple tag with no attributes and no value */
  178. let blocks = word.match(/^#(EXT[^:]*)(?::(.*))?$/);
  179. if (!blocks) {
  180. throw new shaka.util.Error(
  181. shaka.util.Error.Severity.CRITICAL,
  182. shaka.util.Error.Category.MANIFEST,
  183. shaka.util.Error.Code.INVALID_HLS_TAG,
  184. word);
  185. }
  186. let name = blocks[1];
  187. let data = blocks[2];
  188. let attributes = [];
  189. if (data && data.indexOf('=') >= 0) {
  190. let parser = new shaka.util.TextParser(data);
  191. let blockAttrs;
  192. // Regex:
  193. // 1. Key name ([1])
  194. // 2. Equals sign
  195. // 3. Either:
  196. // a. A quoted string (everything up to the next quote, [2])
  197. // b. An unquoted string
  198. // (everything up to the next comma or end of line, [3])
  199. // 4. Either:
  200. // a. A comma
  201. // b. End of line
  202. const regex = /([^=]+)=(?:"([^"]*)"|([^",]*))(?:,|$)/g;
  203. while ((blockAttrs = parser.readRegex(regex))) {
  204. let attrName = blockAttrs[1];
  205. let attrValue = blockAttrs[2] || blockAttrs[3];
  206. let attribute = new shaka.hls.Attribute(attrName, attrValue);
  207. attributes.push(attribute);
  208. }
  209. } else if (data) {
  210. return new shaka.hls.Tag(id, name, attributes, data);
  211. }
  212. return new shaka.hls.Tag(id, name, attributes);
  213. };
  214. /**
  215. * HLS tags that only appear on Media Playlists.
  216. * Used to determine a playlist type.
  217. *
  218. * @const {!Array.<string>}
  219. */
  220. shaka.hls.ManifestTextParser.MEDIA_PLAYLIST_TAGS = [
  221. 'EXT-X-TARGETDURATION',
  222. 'EXT-X-MEDIA-SEQUENCE',
  223. 'EXT-X-DISCONTINUITY-SEQUENCE',
  224. 'EXT-X-PLAYLIST-TYPE',
  225. 'EXT-X-MAP',
  226. 'EXT-X-I-FRAMES-ONLY',
  227. 'EXT-X-ENDLIST'
  228. ];
  229. /**
  230. * HLS tags that only appear on Segments in a Media Playlists.
  231. * Used to determine the start of the segments info.
  232. *
  233. * @const {!Array.<string>}
  234. */
  235. shaka.hls.ManifestTextParser.SEGMENT_TAGS = [
  236. 'EXTINF',
  237. 'EXT-X-BYTERANGE',
  238. 'EXT-X-DISCONTINUITY',
  239. 'EXT-X-PROGRAM-DATE-TIME',
  240. 'EXT-X-KEY',
  241. 'EXT-X-DATERANGE'
  242. ];