Skip to content

Commit

Permalink
feat: add segmentRelativeVttTiming option
Browse files Browse the repository at this point in the history
  • Loading branch information
elsassph committed Apr 1, 2022
1 parent 4005754 commit f26d563
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 60 deletions.
5 changes: 5 additions & 0 deletions externs/shaka/player.js
Expand Up @@ -782,6 +782,7 @@ shaka.extern.HlsManifestConfiguration;
* disableText: boolean,
* disableThumbnails: boolean,
* defaultPresentationDelay: number,
* segmentRelativeVttTiming: boolean,
* dash: shaka.extern.DashManifestConfiguration,
* hls: shaka.extern.HlsManifestConfiguration
* }}
Expand Down Expand Up @@ -813,6 +814,10 @@ shaka.extern.HlsManifestConfiguration;
* configured or set as 0.
* For HLS, the default value is 3 segments duration if not configured or
* set as 0.
* @property {boolean} segmentRelativeVttTiming
* Option to calculate VTT text timings relatively to the segment start
* instead of relative to the period start (which is the default).
* Defaults to <code>false</code>.
* @property {shaka.extern.DashManifestConfiguration} dash
* Advanced parameters used by the DASH manifest parser.
* @property {shaka.extern.HlsManifestConfiguration} hls
Expand Down
6 changes: 5 additions & 1 deletion externs/shaka/text.js
Expand Up @@ -442,7 +442,8 @@ shaka.extern.TextParser = class {
* @typedef {{
* periodStart: number,
* segmentStart: number,
* segmentEnd: number
* segmentEnd: number,
* vttOffset: number
* }}
*
* @property {number} periodStart
Expand All @@ -451,6 +452,9 @@ shaka.extern.TextParser = class {
* The absolute start time of the segment in seconds.
* @property {number} segmentEnd
* The absolute end time of the segment in seconds.
* @property {number} vttOffset
* The start time relative to either segment or period start depending
* on <code>segmentRelativeVttTiming</code> configuration.
*
* @exportDoc
*/
Expand Down
13 changes: 12 additions & 1 deletion lib/media/media_source_engine.js
Expand Up @@ -66,6 +66,9 @@ shaka.media.MediaSourceEngine = class {
/** @private {shaka.text.TextEngine} */
this.textEngine_ = null;

/** @private {boolean} */
this.segmentRelativeVttTiming_ = false;

const onMetadataNoOp = (metadata, timestampOffset, segmentEnd) => {};

/** @private {!function(!Array.<shaka.extern.ID3Metadata>,
Expand Down Expand Up @@ -367,7 +370,8 @@ shaka.media.MediaSourceEngine = class {
if (!this.textEngine_) {
this.textEngine_ = new shaka.text.TextEngine(this.textDisplayer_);
}
this.textEngine_.initParser(mimeType, sequenceMode);
this.textEngine_.initParser(mimeType, sequenceMode,
this.segmentRelativeVttTiming_);
}

/**
Expand Down Expand Up @@ -1113,6 +1117,13 @@ shaka.media.MediaSourceEngine = class {
}
}

/**
* @param {boolean} segmentRelativeVttTiming
*/
setSegmentRelativeVttTiming(segmentRelativeVttTiming) {
this.segmentRelativeVttTiming_ = segmentRelativeVttTiming;
}

/**
* Apply platform-specific transformations to this segment to work around
* issues in the platform.
Expand Down
7 changes: 7 additions & 0 deletions lib/player.js
Expand Up @@ -1571,6 +1571,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
(metadata, offset, endTime) => {
this.processTimedMetadataMediaSrc_(metadata, offset, endTime);
});
const {segmentRelativeVttTiming} = this.config_.manifest;
mediaSourceEngine.setSegmentRelativeVttTiming(segmentRelativeVttTiming);

// Wait for media source engine to finish opening. This promise should
// NEVER be rejected as per the media source engine implementation.
Expand Down Expand Up @@ -3005,6 +3007,10 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}

if (this.mediaSourceEngine_) {
const {segmentRelativeVttTiming} = this.config_.manifest;
this.mediaSourceEngine_.setSegmentRelativeVttTiming(
segmentRelativeVttTiming);

const textDisplayerFactory = this.config_.textDisplayFactory;
if (this.lastTextFactory_ != textDisplayerFactory) {
const displayer =
Expand Down Expand Up @@ -4849,6 +4855,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
periodStart: 0,
segmentStart: 0,
segmentEnd: this.video_.duration,
vttOffset: 0,
};
const data = shaka.util.BufferUtils.toUint8(buffer);
const cues = obj.parseMedia(data, time);
Expand Down
11 changes: 10 additions & 1 deletion lib/text/text_engine.js
Expand Up @@ -31,6 +31,9 @@ shaka.text.TextEngine = class {
/** @private {shaka.extern.TextDisplayer} */
this.displayer_ = displayer;

/** @private {boolean} */
this.segmentRelativeVttTiming_ = false;

/** @private {number} */
this.timestampOffset_ = 0;

Expand Down Expand Up @@ -130,8 +133,9 @@ shaka.text.TextEngine = class {
*
* @param {string} mimeType
* @param {boolean} sequenceMode
* @param {boolean} segmentRelativeVttTiming
*/
initParser(mimeType, sequenceMode) {
initParser(mimeType, sequenceMode, segmentRelativeVttTiming) {
// No parser for CEA, which is extracted from video and side-loaded
// into TextEngine and TextDisplayer.
if (mimeType == shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE ||
Expand All @@ -149,6 +153,7 @@ shaka.text.TextEngine = class {
shaka.log.alwaysWarn(
'Text parsers should have a "setSequenceMode" method!');
}
this.segmentRelativeVttTiming_ = segmentRelativeVttTiming;
}

/**
Expand All @@ -174,11 +179,15 @@ shaka.text.TextEngine = class {
return;
}

const vttOffset = this.segmentRelativeVttTiming_ ?
startTime : this.timestampOffset_;

/** @type {shaka.extern.TextParser.TimeContext} **/
const time = {
periodStart: this.timestampOffset_,
segmentStart: startTime,
segmentEnd: endTime,
vttOffset: vttOffset,
};

// Parse the buffer and add the new cues.
Expand Down
6 changes: 5 additions & 1 deletion lib/text/vtt_text_parser.js
Expand Up @@ -62,9 +62,13 @@ shaka.text.VttTextParser = class {
shaka.util.Error.Code.INVALID_TEXT_HEADER);
}

// Depending on "segmentRelativeVttTiming" configuration,
// "vttOffset" will correspond to either "periodStart" (default)
// or "segmentStart", for segmented VTT where timings are relative
// to the beginning of each segment.
// NOTE: "periodStart" is the timestamp offset applied via TextEngine.
// It is no longer closely tied to periods, but the name stuck around.
let offset = time.periodStart;
let offset = time.vttOffset;

// Do not honor the 'X-TIMESTAMP-MAP' value when in sequence mode.
// That is because it is used mainly (solely?) to account for the timestamp
Expand Down
1 change: 1 addition & 0 deletions lib/util/player_configuration.js
Expand Up @@ -86,6 +86,7 @@ shaka.util.PlayerConfiguration = class {
disableText: false,
disableThumbnails: false,
defaultPresentationDelay: 0,
segmentRelativeVttTiming: false,
dash: {
clockSyncUri: '',
ignoreDrmInfo: false,
Expand Down

0 comments on commit f26d563

Please sign in to comment.