Files
shaka-player/lib/text/mp4_ttml_parser.js
T
Theodore Abshire 3af3a2caec Modified mp4 ttml parser to allow multiple MDATs.
Beforehand, if we were parsing a ttml subtitles stored in an mp4, we did
not account for the possibility that there would be multiple mdat boxes,
each with its own ttml data. In such a case, we would only use the
payload from the final mdat.
I don't know if having multiple mdat like that is really in the spec, but
at least one user has had this problem, and it's a straightforward change
to make that doesn't really slow down the text parser at all (since we
parse every mdat anyway).

Closes #1028

Change-Id: Id782695548cb92f7aa7278843695b670d8c15ff0
2017-09-20 16:18:24 +00:00

99 lines
2.6 KiB
JavaScript

/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.text.Mp4TtmlParser');
goog.require('shaka.text.TextEngine');
goog.require('shaka.text.TtmlTextParser');
goog.require('shaka.util.Error');
goog.require('shaka.util.Mp4Parser');
/**
* @struct
* @constructor
* @implements {shakaExtern.TextParser}
*/
shaka.text.Mp4TtmlParser = function() {
/**
* @type {!shakaExtern.TextParser}
* @private
*/
this.parser_ = new shaka.text.TtmlTextParser();
};
/** @override **/
shaka.text.Mp4TtmlParser.prototype.parseInit = function(data) {
var Mp4Parser = shaka.util.Mp4Parser;
var sawSTPP = false;
new Mp4Parser()
.box('moov', Mp4Parser.children)
.box('trak', Mp4Parser.children)
.box('mdia', Mp4Parser.children)
.box('minf', Mp4Parser.children)
.box('stbl', Mp4Parser.children)
.fullBox('stsd', Mp4Parser.sampleDescription)
.box('stpp', function(box) {
sawSTPP = true;
}).parse(data);
if (!sawSTPP) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.TEXT,
shaka.util.Error.Code.INVALID_MP4_TTML);
}
};
/** @override **/
shaka.text.Mp4TtmlParser.prototype.parseMedia = function(data, time) {
var Mp4Parser = shaka.util.Mp4Parser;
var sawMDAT = false;
var payload = [];
new Mp4Parser()
.box('mdat', Mp4Parser.allData(function(data) {
sawMDAT = true;
// Join this to any previous payload, in case the mp4 has multiple
// mdats.
payload = payload.concat(this.parser_.parseMedia(data.buffer, time));
}.bind(this))).parse(data);
if (!sawMDAT) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.TEXT,
shaka.util.Error.Code.INVALID_MP4_TTML);
}
return payload;
};
shaka.text.TextEngine.registerParser(
'application/mp4; codecs="stpp"',
shaka.text.Mp4TtmlParser);
shaka.text.TextEngine.registerParser(
'application/mp4; codecs="stpp.TTML.im1t"',
shaka.text.Mp4TtmlParser);