diff --git a/lib/player.js b/lib/player.js index cbcaed70a0..bdb52cbfed 100644 --- a/lib/player.js +++ b/lib/player.js @@ -4040,20 +4040,24 @@ shaka.Player = class extends shaka.util.FakeEventTarget { const LanguageUtils = shaka.util.LanguageUtils; const inputlanguage = LanguageUtils.normalize(language); const chaptersTracks = this.getChaptersTracks_(); - const chaptersTrack = chaptersTracks - .find((t) => LanguageUtils.normalize(t.language) == inputlanguage); - if (!chaptersTrack || !chaptersTrack.cues) { + const chaptersTracksWithLanguage = chaptersTracks + .filter((t) => LanguageUtils.normalize(t.language) == inputlanguage); + if (!chaptersTracksWithLanguage || !chaptersTracksWithLanguage.length) { return []; } const chapters = []; - for (const cue of chaptersTrack.cues) { - /** @type {shaka.extern.Chapter} */ - const chapter = { - title: cue.text, - startTime: cue.startTime, - endTime: cue.endTime, - }; - chapters.push(chapter); + for (const chaptersTrack of chaptersTracksWithLanguage) { + if (chaptersTrack && chaptersTrack.cues) { + for (const cue of chaptersTrack.cues) { + /** @type {shaka.extern.Chapter} */ + const chapter = { + title: cue.text, + startTime: cue.startTime, + endTime: cue.endTime, + }; + chapters.push(chapter); + } + } } return chapters; } diff --git a/test/player_integration.js b/test/player_integration.js index 83cfad0959..a9a0161718 100644 --- a/test/player_integration.js +++ b/test/player_integration.js @@ -1063,9 +1063,9 @@ describe('Player', () => { it('add external chapters in vtt format', async () => { await player.load('test:sintel_no_text_compiled'); const locationUri = new goog.Uri(location.href); - const partialUri = new goog.Uri('/base/test/test/assets/chapters.vtt'); - const absoluteUri = locationUri.resolve(partialUri); - await player.addChaptersTrack(absoluteUri.toString(), 'en'); + const partialUri1 = new goog.Uri('/base/test/test/assets/chapters.vtt'); + const absoluteUri1 = locationUri.resolve(partialUri1); + await player.addChaptersTrack(absoluteUri1.toString(), 'en'); await shaka.test.Util.delay(1.5); @@ -1078,11 +1078,44 @@ describe('Player', () => { const chapter2 = chapters[1]; expect(chapter2.title).toBe('Chapter 2'); expect(chapter2.startTime).toBe(5); - expect(chapter2.endTime).toBe(30); + expect(chapter2.endTime).toBe(10); const chapter3 = chapters[2]; expect(chapter3.title).toBe('Chapter 3'); - expect(chapter3.startTime).toBe(30); - expect(chapter3.endTime).toBe(61.349); + expect(chapter3.startTime).toBe(10); + expect(chapter3.endTime).toBe(20); + + const partialUri2 = new goog.Uri('/base/test/test/assets/chapters2.vtt'); + const absoluteUri2 = locationUri.resolve(partialUri2); + await player.addChaptersTrack(absoluteUri2.toString(), 'en'); + + await shaka.test.Util.delay(1.5); + + const chaptersUpdated = player.getChapters('en'); + expect(chaptersUpdated.length).toBe(6); + const chapterUpdated1 = chaptersUpdated[0]; + expect(chapterUpdated1.title).toBe('Chapter 1'); + expect(chapterUpdated1.startTime).toBe(0); + expect(chapterUpdated1.endTime).toBe(5); + const chapterUpdated2 = chaptersUpdated[1]; + expect(chapterUpdated2.title).toBe('Chapter 2'); + expect(chapterUpdated2.startTime).toBe(5); + expect(chapterUpdated2.endTime).toBe(10); + const chapterUpdated3 = chaptersUpdated[2]; + expect(chapterUpdated3.title).toBe('Chapter 3'); + expect(chapterUpdated3.startTime).toBe(10); + expect(chapterUpdated3.endTime).toBe(20); + const chapterUpdated4 = chaptersUpdated[3]; + expect(chapterUpdated4.title).toBe('Chapter 4'); + expect(chapterUpdated4.startTime).toBe(20); + expect(chapterUpdated4.endTime).toBe(30); + const chapterUpdated5 = chaptersUpdated[4]; + expect(chapterUpdated5.title).toBe('Chapter 5'); + expect(chapterUpdated5.startTime).toBe(30); + expect(chapterUpdated5.endTime).toBe(40); + const chapterUpdated6 = chaptersUpdated[5]; + expect(chapterUpdated6.title).toBe('Chapter 6'); + expect(chapterUpdated6.startTime).toBe(40); + expect(chapterUpdated6.endTime).toBe(61.349); }); it('add external chapters in srt format', async () => { @@ -1090,11 +1123,11 @@ describe('Player', () => { const locationUri = new goog.Uri(location.href); const partialUri = new goog.Uri('/base/test/test/assets/chapters.srt'); const absoluteUri = locationUri.resolve(partialUri); - await player.addChaptersTrack(absoluteUri.toString(), 'en'); + await player.addChaptersTrack(absoluteUri.toString(), 'es'); await shaka.test.Util.delay(1.5); - const chapters = player.getChapters('en'); + const chapters = player.getChapters('es'); expect(chapters.length).toBe(3); const chapter1 = chapters[0]; expect(chapter1.title).toBe('Chapter 1'); diff --git a/test/player_src_equals_integration.js b/test/player_src_equals_integration.js index d1d7cd496a..9599d18643 100644 --- a/test/player_src_equals_integration.js +++ b/test/player_src_equals_integration.js @@ -330,9 +330,9 @@ describe('Player Src Equals', () => { await loadWithSrcEquals(SMALL_MP4_CONTENT_URI, /* startTime= */ null); const locationUri = new goog.Uri(location.href); - const partialUri = new goog.Uri('/base/test/test/assets/chapters.vtt'); - const absoluteUri = locationUri.resolve(partialUri); - await player.addChaptersTrack(absoluteUri.toString(), 'en'); + const partialUri1 = new goog.Uri('/base/test/test/assets/chapters.vtt'); + const absoluteUri1 = locationUri.resolve(partialUri1); + await player.addChaptersTrack(absoluteUri1.toString(), 'en'); await shaka.test.Util.delay(1.5); @@ -345,11 +345,44 @@ describe('Player Src Equals', () => { const chapter2 = chapters[1]; expect(chapter2.title).toBe('Chapter 2'); expect(chapter2.startTime).toBe(5); - expect(chapter2.endTime).toBe(30); + expect(chapter2.endTime).toBe(10); const chapter3 = chapters[2]; expect(chapter3.title).toBe('Chapter 3'); - expect(chapter3.startTime).toBe(30); - expect(chapter3.endTime).toBe(61.349); + expect(chapter3.startTime).toBe(10); + expect(chapter3.endTime).toBe(20); + + const partialUri2 = new goog.Uri('/base/test/test/assets/chapters2.vtt'); + const absoluteUri2 = locationUri.resolve(partialUri2); + await player.addChaptersTrack(absoluteUri2.toString(), 'en'); + + await shaka.test.Util.delay(1.5); + + const chaptersUpdated = player.getChapters('en'); + expect(chaptersUpdated.length).toBe(6); + const chapterUpdated1 = chaptersUpdated[0]; + expect(chapterUpdated1.title).toBe('Chapter 1'); + expect(chapterUpdated1.startTime).toBe(0); + expect(chapterUpdated1.endTime).toBe(5); + const chapterUpdated2 = chaptersUpdated[1]; + expect(chapterUpdated2.title).toBe('Chapter 2'); + expect(chapterUpdated2.startTime).toBe(5); + expect(chapterUpdated2.endTime).toBe(10); + const chapterUpdated3 = chaptersUpdated[2]; + expect(chapterUpdated3.title).toBe('Chapter 3'); + expect(chapterUpdated3.startTime).toBe(10); + expect(chapterUpdated3.endTime).toBe(20); + const chapterUpdated4 = chaptersUpdated[3]; + expect(chapterUpdated4.title).toBe('Chapter 4'); + expect(chapterUpdated4.startTime).toBe(20); + expect(chapterUpdated4.endTime).toBe(30); + const chapterUpdated5 = chaptersUpdated[4]; + expect(chapterUpdated5.title).toBe('Chapter 5'); + expect(chapterUpdated5.startTime).toBe(30); + expect(chapterUpdated5.endTime).toBe(40); + const chapterUpdated6 = chaptersUpdated[5]; + expect(chapterUpdated6.title).toBe('Chapter 6'); + expect(chapterUpdated6.startTime).toBe(40); + expect(chapterUpdated6.endTime).toBe(61.349); }); it('add external chapters in srt format', async () => { @@ -358,11 +391,11 @@ describe('Player Src Equals', () => { const locationUri = new goog.Uri(location.href); const partialUri = new goog.Uri('/base/test/test/assets/chapters.srt'); const absoluteUri = locationUri.resolve(partialUri); - await player.addChaptersTrack(absoluteUri.toString(), 'en'); + await player.addChaptersTrack(absoluteUri.toString(), 'es'); await shaka.test.Util.delay(1.5); - const chapters = player.getChapters('en'); + const chapters = player.getChapters('es'); expect(chapters.length).toBe(3); const chapter1 = chapters[0]; expect(chapter1.title).toBe('Chapter 1'); diff --git a/test/test/assets/chapters.vtt b/test/test/assets/chapters.vtt index 882e540053..39b6c78ffb 100644 --- a/test/test/assets/chapters.vtt +++ b/test/test/assets/chapters.vtt @@ -3,8 +3,8 @@ WEBVTT 00:00.000 --> 00:05.000 Chapter 1 -00:05.000 --> 00:30.000 +00:05.000 --> 00:10.000 Chapter 2 -00:30.000 --> 01:01.349 +00:10.000 --> 00:20.000 Chapter 3 diff --git a/test/test/assets/chapters2.vtt b/test/test/assets/chapters2.vtt new file mode 100644 index 0000000000..c86fd04422 --- /dev/null +++ b/test/test/assets/chapters2.vtt @@ -0,0 +1,10 @@ +WEBVTT + +00:20.000 --> 00:30.000 +Chapter 4 + +00:30.000 --> 00:40.000 +Chapter 5 + +00:40.000 --> 01:01.349 +Chapter 6