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 {shakaExtern.TextParser}
  26. */
  27. shaka.text.Mp4TtmlParser = function() {
  28. /**
  29. * @type {!shakaExtern.TextParser}
  30. * @private
  31. */
  32. this.parser_ = new shaka.text.TtmlTextParser();
  33. };
  34. /** @override **/
  35. shaka.text.Mp4TtmlParser.prototype.parseInit = function(data) {
  36. var Mp4Parser = shaka.util.Mp4Parser;
  37. var 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. }).parse(data);
  48. if (!sawSTPP) {
  49. throw new shaka.util.Error(
  50. shaka.util.Error.Severity.CRITICAL,
  51. shaka.util.Error.Category.TEXT,
  52. shaka.util.Error.Code.INVALID_MP4_TTML);
  53. }
  54. };
  55. /** @override **/
  56. shaka.text.Mp4TtmlParser.prototype.parseMedia = function(data, time) {
  57. var Mp4Parser = shaka.util.Mp4Parser;
  58. var sawMDAT = false;
  59. var payload = [];
  60. new Mp4Parser()
  61. .box('mdat', Mp4Parser.allData(function(data) {
  62. sawMDAT = true;
  63. payload = this.parser_.parseMedia(data.buffer, time);
  64. }.bind(this))).parse(data);
  65. if (!sawMDAT) {
  66. throw new shaka.util.Error(
  67. shaka.util.Error.Severity.CRITICAL,
  68. shaka.util.Error.Category.TEXT,
  69. shaka.util.Error.Code.INVALID_MP4_TTML);
  70. }
  71. return payload;
  72. };
  73. shaka.text.TextEngine.registerParser(
  74. 'application/mp4; codecs="stpp"',
  75. shaka.text.Mp4TtmlParser);