Skip to content

Commit

Permalink
fix(HLS): Fix external subtitles out of sync in HLS (#5491)
Browse files Browse the repository at this point in the history
Fixes #5458
Fixes #5443

Backported to v4.2.x
  • Loading branch information
avelad authored and joeyparrish committed Aug 21, 2023
1 parent cd3c2c8 commit ff2edc1
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 10 deletions.
6 changes: 5 additions & 1 deletion externs/shaka/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ shaka.extern.FetchCryptoKeysFunction;
* tilesLayout: (string|undefined),
* matchedStreams:
* (!Array.<shaka.extern.Stream>|!Array.<shaka.extern.StreamDB>|
* undefined)
* undefined),
* external: boolean
* }}
*
* @description
Expand Down Expand Up @@ -434,6 +435,9 @@ shaka.extern.FetchCryptoKeysFunction;
* @property {(!Array.<shaka.extern.Stream>|!Array.<shaka.extern.StreamDB>|
* undefined)} matchedStreams
* The streams in all periods which match the stream. Used for Dash.
* @property {boolean} external
* Indicate if the stream was added externally.
* Eg: external text tracks.
*
* @exportDoc
*/
Expand Down
6 changes: 5 additions & 1 deletion externs/shaka/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ shaka.extern.ManifestDB;
* audioSamplingRate: ?number,
* spatialAudio: boolean,
* closedCaptions: Map.<string, string>,
* tilesLayout: (string|undefined)
* tilesLayout: (string|undefined),
* external: boolean
* }}
*
* @property {number} id
Expand Down Expand Up @@ -193,6 +194,9 @@ shaka.extern.ManifestDB;
* The value is a grid-item-dimension consisting of two positive decimal
* integers in the format: column-x-row ('4x3'). It describes the arrangement
* of Images in a Grid. The minimum valid LAYOUT is '1x1'.
* @property {boolean} external
* Indicate if the stream was added externally.
* Eg: external text tracks.
*/
shaka.extern.StreamDB;

Expand Down
1 change: 1 addition & 0 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ shaka.dash.DashParser = class {
hdr,
tilesLayout,
matchedStreams: [],
external: false,
};
}

Expand Down
1 change: 1 addition & 0 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,7 @@ shaka.hls.HlsParser = class {
closedCaptions,
hdr: undefined,
tilesLayout: undefined,
external: false,
};

return {
Expand Down
11 changes: 6 additions & 5 deletions lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ shaka.media.MediaSourceEngine = class {
let mimeType = shaka.util.MimeUtils.getFullType(
stream.mimeType, stream.codecs);
if (contentType == ContentType.TEXT) {
this.reinitText(mimeType, sequenceMode);
this.reinitText(mimeType, sequenceMode, stream.external);
} else {
if ((forceTransmuxTS || !MediaSource.isTypeSupported(mimeType)) &&
shaka.media.Transmuxer.isSupported(mimeType, contentType)) {
Expand Down Expand Up @@ -383,13 +383,14 @@ shaka.media.MediaSourceEngine = class {
* Reinitialize the TextEngine for a new text type.
* @param {string} mimeType
* @param {boolean} sequenceMode
* @param {boolean} external
*/
reinitText(mimeType, sequenceMode) {
reinitText(mimeType, sequenceMode, external) {
if (!this.textEngine_) {
this.textEngine_ = new shaka.text.TextEngine(this.textDisplayer_);
}
this.textEngine_.initParser(mimeType, sequenceMode,
this.segmentRelativeVttTiming_);
external || this.segmentRelativeVttTiming_);
}

/**
Expand Down Expand Up @@ -564,7 +565,7 @@ shaka.media.MediaSourceEngine = class {
// the video stream, so textEngine may not have been initialized.
if (!this.textEngine_) {
this.reinitText(shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE,
this.sequenceMode_);
this.sequenceMode_, /* external= */ false);
}

if (transmuxedData.metadata) {
Expand Down Expand Up @@ -593,7 +594,7 @@ shaka.media.MediaSourceEngine = class {
} else if (hasClosedCaptions) {
if (!this.textEngine_) {
this.reinitText(shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE,
this.sequenceMode_);
this.sequenceMode_, /* external= */ false);
}
// If it is the init segment for closed captions, initialize the closed
// caption parser.
Expand Down
4 changes: 2 additions & 2 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ shaka.media.StreamingEngine = class {
const mimeType = shaka.util.MimeUtils.getFullType(
stream.mimeType, stream.codecs);
this.playerInterface_.mediaSourceEngine.reinitText(
mimeType, this.manifest_.sequenceMode);
mimeType, this.manifest_.sequenceMode, stream.external);

const textDisplayer =
this.playerInterface_.mediaSourceEngine.getTextDisplayer();
Expand Down Expand Up @@ -447,7 +447,7 @@ shaka.media.StreamingEngine = class {
const fullMimeType = shaka.util.MimeUtils.getFullType(
stream.mimeType, stream.codecs);
this.playerInterface_.mediaSourceEngine.reinitText(
fullMimeType, this.manifest_.sequenceMode);
fullMimeType, this.manifest_.sequenceMode, stream.external);
}

// Releases the segmentIndex of the old stream.
Expand Down
1 change: 1 addition & 0 deletions lib/offline/indexeddb/v1_storage_cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ shaka.offline.indexeddb.V1StorageCell = class
spatialAudio: false,
closedCaptions: null,
tilesLayout: undefined,
external: false,
};
}

Expand Down
1 change: 1 addition & 0 deletions lib/offline/indexeddb/v2_storage_cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ shaka.offline.indexeddb.V2StorageCell = class
spatialAudio: false,
closedCaptions: null,
tilesLayout: undefined,
external: false,
};
}

Expand Down
1 change: 1 addition & 0 deletions lib/offline/manifest_converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ shaka.offline.ManifestConverter = class {
spatialAudio: streamDB.spatialAudio,
closedCaptions: streamDB.closedCaptions,
tilesLayout: streamDB.tilesLayout,
external: streamDB.external,
};

return stream;
Expand Down
1 change: 1 addition & 0 deletions lib/offline/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ shaka.offline.Storage = class {
spatialAudio: stream.spatialAudio,
closedCaptions: stream.closedCaptions,
tilesLayout: stream.tilesLayout,
external: stream.external,
};

const startTime =
Expand Down
3 changes: 3 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
audioSamplingRate: null,
spatialAudio: false,
closedCaptions: null,
external: false,
},
bandwidth: 100,
allowedByApplication: true,
Expand Down Expand Up @@ -4627,6 +4628,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
audioSamplingRate: null,
spatialAudio: false,
closedCaptions: null,
external: true,
};

const fullMimeType = shaka.util.MimeUtils.getFullType(
Expand Down Expand Up @@ -5057,6 +5059,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
audioSamplingRate: null,
spatialAudio: false,
closedCaptions: null,
external: false,
};
manifest.textStreams.push(textStream);
closedCaptionsSet.add(id);
Expand Down
2 changes: 2 additions & 0 deletions lib/util/periods.js
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,7 @@ shaka.util.PeriodCombiner = class {
audioSamplingRate: null,
spatialAudio: false,
closedCaptions: null,
external: false,
};
}

Expand Down Expand Up @@ -1527,6 +1528,7 @@ shaka.util.PeriodCombiner = class {
audioSamplingRate: null,
spatialAudio: false,
closedCaptions: null,
external: false,
};
}

Expand Down
1 change: 1 addition & 0 deletions test/media/adaptation_set_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ describe('AdaptationSet', () => {
forced: false,
trickModeVideo: null,
type: '',
external: false,
};
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/media/media_source_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ describe('MediaSourceEngine', () => {
});

it('destroys text engines', async () => {
mediaSourceEngine.reinitText('text/vtt', false);
mediaSourceEngine.reinitText('text/vtt', false, false);

await mediaSourceEngine.destroy();
expect(mockTextEngine).toBeTruthy();
Expand Down
5 changes: 5 additions & 0 deletions test/offline/manifest_convert_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ describe('ManifestConverter', () => {
audioSamplingRate: null,
spatialAudio: false,
closedCaptions: null,
external: false,
};

return streamDB;
Expand Down Expand Up @@ -389,6 +390,7 @@ describe('ManifestConverter', () => {
spatialAudio: false,
closedCaptions: null,
tilesLayout: undefined,
external: false,
};
}

Expand Down Expand Up @@ -438,6 +440,7 @@ describe('ManifestConverter', () => {
spatialAudio: false,
closedCaptions: null,
tilesLayout: undefined,
external: false,
};
}

Expand Down Expand Up @@ -486,6 +489,7 @@ describe('ManifestConverter', () => {
spatialAudio: false,
closedCaptions: null,
tilesLayout: undefined,
external: false,
};
}

Expand Down Expand Up @@ -531,6 +535,7 @@ describe('ManifestConverter', () => {
spatialAudio: streamDb.spatialAudio,
closedCaptions: streamDb.closedCaptions,
tilesLayout: streamDb.tilesLayout,
external: streamDb.external,
};

expect(stream).toEqual(expectedStream);
Expand Down
2 changes: 2 additions & 0 deletions test/test/util/manifest_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ shaka.test.ManifestGenerator.Stream = class {
this.hdr = undefined;
/** @type {(string|undefined)} */
this.tilesLayout = undefined;
/** @type {boolean} */
this.external = false;
}

/** @type {shaka.extern.Stream} */
Expand Down
1 change: 1 addition & 0 deletions test/test/util/offline_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ shaka.test.OfflineUtils = class {
audioSamplingRate: null,
spatialAudio: false,
closedCaptions: null,
external: false,
};
}

Expand Down
3 changes: 3 additions & 0 deletions test/test/util/streaming_engine_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ shaka.test.StreamingEngineUtil = class {
roles: [],
forced: false,
spatialAudio: false,
external: false,
};
}

Expand Down Expand Up @@ -447,6 +448,7 @@ shaka.test.StreamingEngineUtil = class {
roles: [],
forced: false,
spatialAudio: false,
external: false,
};
}

Expand Down Expand Up @@ -482,6 +484,7 @@ shaka.test.StreamingEngineUtil = class {
roles: [],
forced: false,
spatialAudio: false,
external: false,
};
}
};

0 comments on commit ff2edc1

Please sign in to comment.