Source: lib/text/sbv_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.text.SbvTextParser');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.text.Cue');
  9. goog.require('shaka.text.TextEngine');
  10. goog.require('shaka.util.Error');
  11. goog.require('shaka.util.StringUtils');
  12. goog.require('shaka.util.TextParser');
  13. /**
  14. * @implements {shaka.extern.TextParser}
  15. * @export
  16. */
  17. shaka.text.SbvTextParser = class {
  18. /**
  19. * @override
  20. * @export
  21. */
  22. parseInit(data) {
  23. goog.asserts.assert(false, 'SubViewer does not have init segments');
  24. }
  25. /**
  26. * @override
  27. * @export
  28. */
  29. setSequenceMode(sequenceMode) {
  30. // Unused.
  31. }
  32. /**
  33. * @override
  34. * @export
  35. */
  36. setManifestType(manifestType) {
  37. // Unused.
  38. }
  39. /**
  40. * @override
  41. * @export
  42. */
  43. parseMedia(data, time) {
  44. const StringUtils = shaka.util.StringUtils;
  45. // Get the input as a string.
  46. const strFromData = StringUtils.fromUTF8(data);
  47. // remove dos newlines
  48. let str = strFromData.replace(/\r+/g, '');
  49. // trim white space start and end
  50. str = str.trim();
  51. /** @type {!Array<!shaka.text.Cue>} */
  52. const cues = [];
  53. // Supports no cues
  54. if (str == '') {
  55. return cues;
  56. }
  57. // get cues
  58. const blocklist = str.split('\n\n');
  59. for (const block of blocklist) {
  60. const lines = block.split('\n');
  61. // Parse the times.
  62. const parser = new shaka.util.TextParser(lines[0]);
  63. const start = parser.parseTime();
  64. const expect = parser.readRegex(/,/g);
  65. const end = parser.parseTime();
  66. if (start == null || expect == null || end == null) {
  67. throw new shaka.util.Error(
  68. shaka.util.Error.Severity.CRITICAL,
  69. shaka.util.Error.Category.TEXT,
  70. shaka.util.Error.Code.INVALID_TEXT_CUE,
  71. 'Could not parse cue time range in SubViewer');
  72. }
  73. // Get the payload.
  74. const payload = lines.slice(1).join('\n').trim();
  75. const cue = new shaka.text.Cue(start, end, payload);
  76. cues.push(cue);
  77. }
  78. return cues;
  79. }
  80. };
  81. shaka.text.TextEngine.registerParser(
  82. 'text/x-subviewer', () => new shaka.text.SbvTextParser());