Source: lib/hls/manifest_text_parser.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.hls.ManifestTextParser');
  7. goog.require('shaka.hls.Attribute');
  8. goog.require('shaka.hls.Playlist');
  9. goog.require('shaka.hls.PlaylistType');
  10. goog.require('shaka.hls.Segment');
  11. goog.require('shaka.hls.Tag');
  12. goog.require('shaka.hls.Utils');
  13. goog.require('shaka.util.Error');
  14. goog.require('shaka.util.StringUtils');
  15. goog.require('shaka.util.TextParser');
  16. /**
  17. * HlS manifest text parser.
  18. */
  19. shaka.hls.ManifestTextParser = class {
  20. constructor() {
  21. /** @private {number} */
  22. this.globalId_ = 0;
  23. }
  24. /**
  25. * @param {BufferSource} data
  26. * @return {!shaka.hls.Playlist}
  27. */
  28. parsePlaylist(data) {
  29. const MEDIA_PLAYLIST_TAGS =
  30. shaka.hls.ManifestTextParser.MEDIA_PLAYLIST_TAGS;
  31. const SEGMENT_TAGS = shaka.hls.ManifestTextParser.SEGMENT_TAGS;
  32. // Get the input as a string. Normalize newlines to \n.
  33. let str = shaka.util.StringUtils.fromUTF8(data);
  34. str = str.replace(/\r\n|\r(?=[^\n]|$)/gm, '\n').trim();
  35. const lines = str.split(/\n+/m);
  36. if (!/^#EXTM3U($|[ \t\n])/m.test(lines[0])) {
  37. throw new shaka.util.Error(
  38. shaka.util.Error.Severity.CRITICAL,
  39. shaka.util.Error.Category.MANIFEST,
  40. shaka.util.Error.Code.HLS_PLAYLIST_HEADER_MISSING);
  41. }
  42. /** shaka.hls.PlaylistType */
  43. let playlistType = shaka.hls.PlaylistType.MASTER;
  44. // First, look for media playlist tags, so that we know what the playlist
  45. // type really is before we start parsing.
  46. // TODO: refactor the for loop for better readability.
  47. // Whether to skip the next element; initialize to true to skip first elem.
  48. let skip = true;
  49. for (const line of lines) {
  50. // Ignore comments.
  51. if (shaka.hls.Utils.isComment(line) || skip) {
  52. skip = false;
  53. continue;
  54. }
  55. const tag = this.parseTag_(line);
  56. // These tags won't actually be used, so don't increment the global
  57. // id.
  58. this.globalId_ -= 1;
  59. if (MEDIA_PLAYLIST_TAGS.includes(tag.name)) {
  60. playlistType = shaka.hls.PlaylistType.MEDIA;
  61. break;
  62. } else if (tag.name == 'EXT-X-STREAM-INF') {
  63. skip = true;
  64. }
  65. }
  66. /** {Array<shaka.hls.Tag>} */
  67. const tags = [];
  68. // Initialize to "true" to skip the first element.
  69. skip = true;
  70. for (let i = 0; i < lines.length; i++) {
  71. const line = lines[i];
  72. const next = lines[i + 1];
  73. // Skip comments
  74. if (shaka.hls.Utils.isComment(line) || skip) {
  75. skip = false;
  76. continue;
  77. }
  78. const tag = this.parseTag_(line);
  79. if (SEGMENT_TAGS.includes(tag.name)) {
  80. if (playlistType != shaka.hls.PlaylistType.MEDIA) {
  81. // Only media playlists should contain segment tags
  82. throw new shaka.util.Error(
  83. shaka.util.Error.Severity.CRITICAL,
  84. shaka.util.Error.Category.MANIFEST,
  85. shaka.util.Error.Code.HLS_INVALID_PLAYLIST_HIERARCHY);
  86. }
  87. const segmentsData = lines.splice(i, lines.length - i);
  88. const segments = this.parseSegments_(segmentsData, tags);
  89. return new shaka.hls.Playlist(playlistType, tags, segments);
  90. }
  91. tags.push(tag);
  92. // An EXT-X-STREAM-INF tag is followed by a URI of a media playlist.
  93. // Add the URI to the tag object.
  94. if (tag.name == 'EXT-X-STREAM-INF') {
  95. const tagUri = new shaka.hls.Attribute('URI', next);
  96. tag.addAttribute(tagUri);
  97. skip = true;
  98. }
  99. }
  100. return new shaka.hls.Playlist(playlistType, tags);
  101. }
  102. /**
  103. * Parses an array of strings into an array of HLS Segment objects.
  104. *
  105. * @param {!Array<string>} lines
  106. * @param {!Array<!shaka.hls.Tag>} playlistTags
  107. * @return {!Array<shaka.hls.Segment>}
  108. * @private
  109. */
  110. parseSegments_(lines, playlistTags) {
  111. /** @type {!Array<shaka.hls.Segment>} */
  112. const segments = [];
  113. /** @type {!Array<shaka.hls.Tag>} */
  114. let segmentTags = [];
  115. /** @type {!Array<shaka.hls.Tag>} */
  116. let partialSegmentTags = [];
  117. // The last parsed EXT-X-MAP tag.
  118. /** @type {?shaka.hls.Tag} */
  119. let currentMapTag = null;
  120. for (const line of lines) {
  121. if (/^(#EXT)/.test(line)) {
  122. const tag = this.parseTag_(line);
  123. if (shaka.hls.ManifestTextParser.MEDIA_PLAYLIST_TAGS.includes(
  124. tag.name)) {
  125. playlistTags.push(tag);
  126. } else {
  127. // Mark the the EXT-X-MAP tag, and add it to the segment tags
  128. // following it later.
  129. if (tag.name == 'EXT-X-MAP') {
  130. currentMapTag = tag;
  131. } else if (tag.name == 'EXT-X-PART') {
  132. partialSegmentTags.push(tag);
  133. } else if (tag.name == 'EXT-X-PRELOAD-HINT') {
  134. if (tag.getAttributeValue('TYPE') == 'PART') {
  135. partialSegmentTags.push(tag);
  136. } else if (tag.getAttributeValue('TYPE') == 'MAP') {
  137. // Rename the Preload Hint tag to be a Map tag.
  138. tag.setName('EXT-X-MAP');
  139. currentMapTag = tag;
  140. }
  141. } else {
  142. segmentTags.push(tag);
  143. }
  144. }
  145. } else if (shaka.hls.Utils.isComment(line)) {
  146. // Skip comments.
  147. } else {
  148. const verbatimSegmentUri = line.trim();
  149. // Attach the last parsed EXT-X-MAP tag to the segment.
  150. if (currentMapTag) {
  151. segmentTags.push(currentMapTag);
  152. }
  153. // The URI appears after all of the tags describing the segment.
  154. const segment = new shaka.hls.Segment(
  155. verbatimSegmentUri, segmentTags, partialSegmentTags);
  156. segments.push(segment);
  157. segmentTags = [];
  158. partialSegmentTags = [];
  159. }
  160. }
  161. // After all the partial segments of a regular segment is published,
  162. // a EXTINF tag and Uri for a regular segment containing the same media
  163. // content will get published at last.
  164. // If no EXTINF tag follows the list of partial segment tags at the end,
  165. // create a segment to wrap the partial segment tags.
  166. if (partialSegmentTags.length) {
  167. if (currentMapTag) {
  168. segmentTags.push(currentMapTag);
  169. }
  170. const segment = new shaka.hls.Segment('', segmentTags,
  171. partialSegmentTags);
  172. segments.push(segment);
  173. }
  174. return segments;
  175. }
  176. /**
  177. * Parses a string into an HLS Tag object while tracking what id to use next.
  178. *
  179. * @param {string} word
  180. * @return {!shaka.hls.Tag}
  181. * @private
  182. */
  183. parseTag_(word) {
  184. return shaka.hls.ManifestTextParser.parseTag(this.globalId_++, word);
  185. }
  186. /**
  187. * Parses a string into an HLS Tag object.
  188. *
  189. * @param {number} id
  190. * @param {string} word
  191. * @return {!shaka.hls.Tag}
  192. */
  193. static parseTag(id, word) {
  194. /* HLS tags start with '#EXT'. A tag can have a set of attributes
  195. (#EXT-<tagname>:<attribute list>) and/or a value (#EXT-<tagname>:<value>).
  196. An attribute's format is 'AttributeName=AttributeValue'.
  197. The parsing logic goes like this:
  198. 1. Everything before ':' is a name (we ignore '#').
  199. 2. Everything after ':' is a list of comma-separated items,
  200. 2a. The first item might be a value, if it does not contain '='.
  201. 2b. Otherwise, items are attributes.
  202. 3. If there is no ":", it's a simple tag with no attributes and no value.
  203. */
  204. const blocks = word.match(/^#(EXT[^:]*)(?::(.*))?$/);
  205. if (!blocks) {
  206. throw new shaka.util.Error(
  207. shaka.util.Error.Severity.CRITICAL,
  208. shaka.util.Error.Category.MANIFEST,
  209. shaka.util.Error.Code.INVALID_HLS_TAG,
  210. word);
  211. }
  212. const name = blocks[1];
  213. const data = blocks[2];
  214. const attributes = [];
  215. let value;
  216. if (data) {
  217. const parser = new shaka.util.TextParser(data);
  218. let blockAttrs;
  219. // Regex: any number of non-equals-sign characters at the beginning
  220. // terminated by comma or end of line
  221. const valueRegex = /^([^,=]+)(?:,|$)/g;
  222. const blockValue = parser.readRegex(valueRegex);
  223. if (blockValue) {
  224. value = blockValue[1];
  225. }
  226. // Regex:
  227. // 1. Key name ([1])
  228. // 2. Equals sign
  229. // 3. Either:
  230. // a. A quoted string (everything up to the next quote, [2])
  231. // b. An unquoted string
  232. // (everything up to the next comma or end of line, [3])
  233. // 4. Either:
  234. // a. A comma
  235. // b. End of line
  236. const attributeRegex = /([^=]+)=(?:"([^"]*)"|([^",]*))(?:,|$)/g;
  237. while ((blockAttrs = parser.readRegex(attributeRegex))) {
  238. const attrName = blockAttrs[1];
  239. const attrValue = blockAttrs[2] || blockAttrs[3];
  240. const attribute = new shaka.hls.Attribute(attrName, attrValue);
  241. attributes.push(attribute);
  242. parser.skipWhitespace();
  243. }
  244. }
  245. return new shaka.hls.Tag(id, name, attributes, value);
  246. }
  247. };
  248. /**
  249. * HLS tags that only appear on Media Playlists.
  250. * Used to determine a playlist type.
  251. *
  252. * @const {!Array<string>}
  253. */
  254. shaka.hls.ManifestTextParser.MEDIA_PLAYLIST_TAGS = [
  255. 'EXT-X-TARGETDURATION',
  256. 'EXT-X-MEDIA-SEQUENCE',
  257. 'EXT-X-DISCONTINUITY-SEQUENCE',
  258. 'EXT-X-PLAYLIST-TYPE',
  259. 'EXT-X-I-FRAMES-ONLY',
  260. 'EXT-X-ENDLIST',
  261. 'EXT-X-SERVER-CONTROL',
  262. 'EXT-X-SKIP',
  263. 'EXT-X-PART-INF',
  264. 'EXT-X-DATERANGE',
  265. ];
  266. /**
  267. * HLS tags that only appear on Segments in a Media Playlists.
  268. * Used to determine the start of the segments info.
  269. *
  270. * @const {!Array<string>}
  271. */
  272. shaka.hls.ManifestTextParser.SEGMENT_TAGS = [
  273. 'EXTINF',
  274. 'EXT-X-BYTERANGE',
  275. 'EXT-X-DISCONTINUITY',
  276. 'EXT-X-PROGRAM-DATE-TIME',
  277. 'EXT-X-KEY',
  278. 'EXT-X-DATERANGE',
  279. 'EXT-X-MAP',
  280. 'EXT-X-GAP',
  281. 'EXT-X-TILES',
  282. ];