From e54a52b0bf42b453c38ba9ec1582d6809f33579a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Tue, 18 Apr 2023 19:59:45 +0200 Subject: [PATCH] fix(HLS): Fix support of fragmented WebVTT (#5156) Fixes https://github.com/shaka-project/shaka-player/issues/5076 --- lib/hls/hls_parser.js | 13 ++++++++--- lib/media/streaming_engine.js | 9 ++++++- test/hls/hls_parser_unit.js | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/lib/hls/hls_parser.js b/lib/hls/hls_parser.js index 543fd4e7aa..d61694dc71 100644 --- a/lib/hls/hls_parser.js +++ b/lib/hls/hls_parser.js @@ -1458,9 +1458,9 @@ shaka.hls.HlsParser = class { let codecs = ''; /** @type {string} */ const type = this.getType_(tag); - // Text does not require a codec. - if (type != shaka.util.ManifestParserUtils.ContentType.TEXT && groupId && - this.groupIdToCodecsMap_.has(groupId)) { + if (type == shaka.util.ManifestParserUtils.ContentType.TEXT) { + codecs = tag.getAttributeValue('CODECS') || ''; + } else if (groupId && this.groupIdToCodecsMap_.has(groupId)) { codecs = this.groupIdToCodecsMap_.get(groupId); } @@ -1736,6 +1736,13 @@ shaka.hls.HlsParser = class { } } + if (type == ContentType.TEXT) { + const firstSegment = realStream.segmentIndex.get(0); + if (firstSegment && firstSegment.initSegmentReference) { + stream.mimeType = 'application/mp4'; + } + } + // Add finishing touches to the stream that can only be done once we have // more full context on the media as a whole. if (this.hasEnoughInfoToFinalizeStreams_()) { diff --git a/lib/media/streaming_engine.js b/lib/media/streaming_engine.js index 9d8c514b98..14226a4780 100644 --- a/lib/media/streaming_engine.js +++ b/lib/media/streaming_engine.js @@ -362,7 +362,7 @@ shaka.media.StreamingEngine = class { /** * @param {shaka.extern.Stream} textStream */ - switchTextStream(textStream) { + async switchTextStream(textStream) { this.currentTextStream_ = textStream; if (!this.startupComplete_) { @@ -374,6 +374,13 @@ shaka.media.StreamingEngine = class { goog.asserts.assert(textStream && textStream.type == ContentType.TEXT, 'Wrong stream type passed to switchTextStream!'); + // In HLS it is possible that the mimetype changes when the media + // playlist is downloaded, so it is necessary to have the updated data + // here. + if (!textStream.segmentIndex) { + await textStream.createSegmentIndex(); + } + this.switchInternal_( textStream, /* clearBuffer= */ true, /* safeMargin= */ 0, /* force= */ false); diff --git a/test/hls/hls_parser_unit.js b/test/hls/hls_parser_unit.js index 8abfd9bdd8..5f96d2dda2 100644 --- a/test/hls/hls_parser_unit.js +++ b/test/hls/hls_parser_unit.js @@ -2244,6 +2244,50 @@ describe('HlsParser', () => { expect(actual).toEqual(manifest); }); + it('parses manifest with MP4+WEBVTT streams', async () => { + const master = [ + '#EXTM3U\n', + '#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="sub1",LANGUAGE="eng",', + 'CODECS="wvtt",URI="text"\n', + '#EXT-X-STREAM-INF:BANDWIDTH=200,CODECS="avc1",', + 'RESOLUTION=960x540,FRAME-RATE=60,SUBTITLES="sub1"\n', + 'video\n', + ].join(''); + + const media = [ + '#EXTM3U\n', + '#EXT-X-PLAYLIST-TYPE:VOD\n', + '#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n', + '#EXTINF:5,\n', + '#EXT-X-BYTERANGE:121090@616\n', + 'main.mp4', + ].join(''); + + const manifest = shaka.test.ManifestGenerator.generate((manifest) => { + manifest.anyTimeline(); + manifest.addPartialVariant((variant) => { + variant.addPartialStream(ContentType.VIDEO); + }); + manifest.addPartialTextStream((stream) => { + stream.language = 'en'; + stream.mime('application/mp4', 'wvtt'); + }); + manifest.sequenceMode = true; + }); + + fakeNetEngine + .setResponseText('test:/master', master) + .setResponseText('test:/audio', media) + .setResponseText('test:/video', media) + .setResponseText('test:/text', media) + .setResponseValue('test:/init.mp4', initSegmentData) + .setResponseValue('test:/main.mp4', segmentData); + + const actual = await parser.start('test:/master', playerInterface); + await loadAllStreamsFor(actual); + expect(actual).toEqual(manifest); + }); + it('detects VTT streams by codec', async () => { const master = [ '#EXTM3U\n',