Source: lib/media/closed_caption_parser.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.media.ClosedCaptionParser');
  7. goog.provide('shaka.media.IClosedCaptionParser');
  8. goog.require('shaka.cea.CeaDecoder');
  9. goog.require('shaka.cea.DummyCeaParser');
  10. goog.require('shaka.cea.Mp4CeaParser');
  11. goog.require('shaka.util.BufferUtils');
  12. goog.requireType('shaka.cea.ICaptionDecoder');
  13. goog.requireType('shaka.cea.ICeaParser');
  14. /**
  15. * The IClosedCaptionParser defines the interface to provide all operations for
  16. * parsing the closed captions embedded in Dash videos streams.
  17. * TODO: Remove this interface and move method definitions
  18. * directly to ClosedCaptonParser.
  19. * @interface
  20. */
  21. shaka.media.IClosedCaptionParser = class {
  22. /**
  23. * Initialize the caption parser. This should be called only once.
  24. * @param {BufferSource} initSegment
  25. */
  26. init(initSegment) {}
  27. /**
  28. * Parses embedded CEA closed captions and interacts with the underlying
  29. * CaptionStream, and calls the callback function when there are closed
  30. * captions.
  31. *
  32. * @param {BufferSource} mediaFragment
  33. * @return {!Array<!shaka.cea.ICaptionDecoder.ClosedCaption>}
  34. * An array of parsed closed captions.
  35. */
  36. parseFrom(mediaFragment) {}
  37. /**
  38. * Resets the CaptionStream.
  39. */
  40. reset() {}
  41. };
  42. /**
  43. * Closed Caption Parser provides all operations for parsing the closed captions
  44. * embedded in Dash videos streams.
  45. *
  46. * @implements {shaka.media.IClosedCaptionParser}
  47. * @final
  48. */
  49. shaka.media.ClosedCaptionParser = class {
  50. /** */
  51. constructor(mimeType) {
  52. /** @private {!shaka.cea.ICeaParser} */
  53. this.ceaParser_ = new shaka.cea.DummyCeaParser();
  54. if (mimeType.includes('video/mp4')) {
  55. // MP4 Parser to extract closed caption packets from H.264 video.
  56. this.ceaParser_ = new shaka.cea.Mp4CeaParser();
  57. }
  58. /**
  59. * Decoder for decoding CEA-X08 data from closed caption packets.
  60. * @private {!shaka.cea.ICaptionDecoder}
  61. */
  62. this.ceaDecoder_ = new shaka.cea.CeaDecoder();
  63. }
  64. /**
  65. * @override
  66. */
  67. init(initSegment) {
  68. this.ceaParser_.init(initSegment);
  69. }
  70. /**
  71. * @override
  72. */
  73. parseFrom(mediaFragment) {
  74. // Parse the fragment.
  75. const captionPackets = this.ceaParser_.parse(mediaFragment);
  76. // Extract the caption packets for decoding.
  77. for (const captionPacket of captionPackets) {
  78. const uint8ArrayData =
  79. shaka.util.BufferUtils.toUint8(captionPacket.packet);
  80. if (uint8ArrayData.length > 0) {
  81. this.ceaDecoder_.extract(uint8ArrayData, captionPacket.pts);
  82. }
  83. }
  84. // Decode and return the parsed captions.
  85. return this.ceaDecoder_.decode();
  86. }
  87. /**
  88. * @override
  89. */
  90. reset() {
  91. this.ceaDecoder_.clear();
  92. }
  93. };