mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-26 17:46:26 +03:00
cd2d25cbb2
This changes the text APIs to correctly handle buffered ranges of segmented text. b/25517444 Related to issue #150 Change-Id: I3a11b87e8d93376a5012566deb3bf0d015f52391
248 lines
7.2 KiB
JavaScript
248 lines
7.2 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2015 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.media.TextEngine');
|
|
|
|
goog.require('shaka.asserts');
|
|
|
|
|
|
|
|
/**
|
|
* Manages text parsers and cues.
|
|
*
|
|
* @struct
|
|
* @constructor
|
|
* @param {TextTrack} track
|
|
* @param {string} mimeType
|
|
*/
|
|
shaka.media.TextEngine = function(track, mimeType) {
|
|
/** @private {shaka.media.TextEngine.TextParser} */
|
|
this.parser_ = shaka.media.TextEngine.parserMap_[mimeType];
|
|
|
|
// A more accurate work-alike would throw NotSupportedError here, but this
|
|
// should not happen if type-negotiation is working as it should.
|
|
shaka.asserts.assert(this.parser_,
|
|
'Text type negotiation should have happened already');
|
|
|
|
/** @private {TextTrack} */
|
|
this.track_ = track;
|
|
|
|
/** @private {number} */
|
|
this.timestampOffset_ = 0;
|
|
|
|
/** @private {number} */
|
|
this.appendWindowEnd_ = Number.POSITIVE_INFINITY;
|
|
|
|
/** @private {?number} */
|
|
this.bufferStart_ = null;
|
|
|
|
/** @private {?number} */
|
|
this.bufferEnd_ = null;
|
|
};
|
|
|
|
|
|
/**
|
|
* Parses a text buffer into an array of cues.
|
|
*
|
|
* @typedef {function((ArrayBuffer|ArrayBufferView)):!Array.<!TextTrackCue>}
|
|
* @exportDoc
|
|
*/
|
|
shaka.media.TextEngine.TextParser;
|
|
|
|
|
|
/** @private {!Object.<string, !shaka.media.TextEngine.TextParser>} */
|
|
shaka.media.TextEngine.parserMap_ = {};
|
|
|
|
|
|
/**
|
|
* @param {string} mimeType
|
|
* @param {!shaka.media.TextEngine.TextParser} parser
|
|
* @export
|
|
*/
|
|
shaka.media.TextEngine.registerParser = function(mimeType, parser) {
|
|
shaka.media.TextEngine.parserMap_[mimeType] = parser;
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {string} mimeType
|
|
* @export
|
|
*/
|
|
shaka.media.TextEngine.unregisterParser = function(mimeType) {
|
|
delete shaka.media.TextEngine.parserMap_[mimeType];
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {string} mimeType
|
|
* @return {boolean}
|
|
*/
|
|
shaka.media.TextEngine.isTypeSupported = function(mimeType) {
|
|
return !!shaka.media.TextEngine.parserMap_[mimeType];
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {ArrayBuffer|ArrayBufferView} buffer
|
|
* @param {number} startTime
|
|
* @param {number} endTime
|
|
* @return {!Promise}
|
|
*/
|
|
shaka.media.TextEngine.prototype.appendBuffer =
|
|
function(buffer, startTime, endTime) {
|
|
var offset = this.timestampOffset_;
|
|
|
|
startTime += offset;
|
|
endTime += offset;
|
|
|
|
// Start the operation asynchronously to avoid blocking the caller.
|
|
return Promise.resolve().then(function() {
|
|
// Parse the buffer and add the new cues.
|
|
var cues = this.parser_(buffer);
|
|
|
|
for (var i = 0; i < cues.length; ++i) {
|
|
cues[i].startTime += offset;
|
|
cues[i].endTime += offset;
|
|
if (cues[i].startTime >= this.appendWindowEnd_) break;
|
|
this.track_.addCue(cues[i]);
|
|
}
|
|
|
|
// NOTE: We update the buffered range from the start and end times passed
|
|
// down from the segment reference, not with the start and end times of the
|
|
// parsed cues. This is important because some segments may contain no
|
|
// cues, but we must still consider those ranges buffered.
|
|
if (this.bufferStart_ == null) {
|
|
this.bufferStart_ = startTime;
|
|
} else {
|
|
// We already had something in buffer, and we assume we are extending the
|
|
// range from the end.
|
|
shaka.asserts.assert((startTime - this.bufferEnd_) <= 1,
|
|
'There should not be a gap in text references >1s');
|
|
}
|
|
this.bufferEnd_ = Math.min(endTime, this.appendWindowEnd_);
|
|
}.bind(this));
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {number} start
|
|
* @param {number} end
|
|
* @return {!Promise}
|
|
*/
|
|
shaka.media.TextEngine.prototype.remove = function(start, end) {
|
|
// Start the operation asynchronously to avoid blocking the caller.
|
|
return Promise.resolve().then(function() {
|
|
var cues = this.track_.cues;
|
|
var removeMe = [];
|
|
|
|
for (var i = 0; i < cues.length; ++i) {
|
|
if (cues[i].startTime >= end || cues[i].endTime <= start) {
|
|
// Outside the remove range. Hang on to it.
|
|
} else {
|
|
// Remove these in another loop to avoid mutating the TextTrackCueList
|
|
// while iterating over it. This allows us to avoid making assumptions
|
|
// about whether or not this.track_.remove() will alter that list.
|
|
removeMe.push(cues[i]);
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < removeMe.length; ++i) {
|
|
this.track_.removeCue(removeMe[i]);
|
|
}
|
|
|
|
if (this.bufferStart_ == null) {
|
|
shaka.asserts.assert(this.bufferEnd_ == null,
|
|
'end must be null if start is null');
|
|
shaka.asserts.assert(removeMe.length == 0,
|
|
'buffer empty, should have removed nothing');
|
|
} else {
|
|
shaka.asserts.assert(this.bufferEnd_ != null,
|
|
'end must be non-null if start is non-null');
|
|
|
|
// Update buffered range.
|
|
if (end <= this.bufferStart_ || start >= this.bufferEnd_) {
|
|
// No intersection. Nothing was removed.
|
|
shaka.asserts.assert(removeMe.length == 0,
|
|
'no intersection, should have removed nothing');
|
|
} else if (start <= this.bufferStart_ && end >= this.bufferEnd_) {
|
|
// We wiped out everything.
|
|
shaka.asserts.assert(this.track_.cues.length == 0,
|
|
'should be no cues left');
|
|
this.bufferStart_ = this.bufferEnd_ = null;
|
|
} else if (start <= this.bufferStart_ && end < this.bufferEnd_) {
|
|
// We removed from the beginning of the range.
|
|
this.bufferStart_ = end;
|
|
} else if (start > this.bufferStart_ && end >= this.bufferEnd_) {
|
|
// We removed from the end of the range.
|
|
this.bufferEnd_ = start;
|
|
} else {
|
|
// We removed from the middle? StreamingEngine isn't supposed to.
|
|
shaka.asserts.assert(
|
|
false, 'removal from the middle is not supported by TextEngine');
|
|
}
|
|
}
|
|
}.bind(this));
|
|
};
|
|
|
|
|
|
/** @param {number} timestampOffset */
|
|
shaka.media.TextEngine.prototype.setTimestampOffset =
|
|
function(timestampOffset) {
|
|
this.timestampOffset_ = timestampOffset;
|
|
};
|
|
|
|
|
|
/** @param {number} windowEnd */
|
|
shaka.media.TextEngine.prototype.setAppendWindowEnd =
|
|
function(windowEnd) {
|
|
this.appendWindowEnd_ = windowEnd;
|
|
};
|
|
|
|
|
|
/**
|
|
* @return {?number} Time in seconds of the beginning of the buffered range,
|
|
* or null if nothing is buffered.
|
|
*/
|
|
shaka.media.TextEngine.prototype.bufferStart = function() {
|
|
return this.bufferStart_;
|
|
};
|
|
|
|
|
|
/**
|
|
* @return {?number} Time in seconds of the end of the buffered range,
|
|
* or null if nothing is buffered.
|
|
*/
|
|
shaka.media.TextEngine.prototype.bufferEnd = function() {
|
|
return this.bufferEnd_;
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {number} t A timestamp
|
|
* @return {number} Number of seconds ahead of 't' we have buffered
|
|
*/
|
|
shaka.media.TextEngine.prototype.bufferedAheadOf = function(t) {
|
|
if (this.bufferEnd_ == null || this.bufferEnd_ < t) return 0;
|
|
|
|
shaka.asserts.assert(this.bufferStart_ != null,
|
|
'start should not be null if end is not null');
|
|
|
|
if (t < this.bufferStart_) return 0;
|
|
|
|
return this.bufferEnd_ - t;
|
|
};
|