Source: lib/text/mp4_ttml_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.text.Mp4TtmlParser');
  18. goog.require('shaka.text.TextEngine');
  19. goog.require('shaka.text.TtmlTextParser');
  20. goog.require('shaka.util.Error');
  21. goog.require('shaka.util.Mp4Parser');
  22. /**
  23. * @struct
  24. * @constructor
  25. * @implements {shaka.extern.TextParser}
  26. */
  27. shaka.text.Mp4TtmlParser = function() {
  28. /**
  29. * @type {!shaka.extern.TextParser}
  30. * @private
  31. */
  32. this.parser_ = new shaka.text.TtmlTextParser();
  33. };
  34. /** @override **/
  35. shaka.text.Mp4TtmlParser.prototype.parseInit = function(data) {
  36. const Mp4Parser = shaka.util.Mp4Parser;
  37. let sawSTPP = false;
  38. new Mp4Parser()
  39. .box('moov', Mp4Parser.children)
  40. .box('trak', Mp4Parser.children)
  41. .box('mdia', Mp4Parser.children)
  42. .box('minf', Mp4Parser.children)
  43. .box('stbl', Mp4Parser.children)
  44. .fullBox('stsd', Mp4Parser.sampleDescription)
  45. .box('stpp', function(box) {
  46. sawSTPP = true;
  47. box.parser.stop();
  48. }).parse(data);
  49. if (!sawSTPP) {
  50. throw new shaka.util.Error(
  51. shaka.util.Error.Severity.CRITICAL,
  52. shaka.util.Error.Category.TEXT,
  53. shaka.util.Error.Code.INVALID_MP4_TTML);
  54. }
  55. };
  56. /** @override **/
  57. shaka.text.Mp4TtmlParser.prototype.parseMedia = function(data, time) {
  58. const Mp4Parser = shaka.util.Mp4Parser;
  59. let sawMDAT = false;
  60. let payload = [];
  61. new Mp4Parser()
  62. .box('mdat', Mp4Parser.allData(function(data) {
  63. sawMDAT = true;
  64. // Join this to any previous payload, in case the mp4 has multiple
  65. // mdats.
  66. payload = payload.concat(this.parser_.parseMedia(data, time));
  67. }.bind(this))).parse(data);
  68. if (!sawMDAT) {
  69. throw new shaka.util.Error(
  70. shaka.util.Error.Severity.CRITICAL,
  71. shaka.util.Error.Category.TEXT,
  72. shaka.util.Error.Code.INVALID_MP4_TTML);
  73. }
  74. return payload;
  75. };
  76. shaka.text.TextEngine.registerParser(
  77. 'application/mp4; codecs="stpp"',
  78. shaka.text.Mp4TtmlParser);
  79. shaka.text.TextEngine.registerParser(
  80. 'application/mp4; codecs="stpp.TTML.im1t"',
  81. shaka.text.Mp4TtmlParser);