Skip to content

Commit

Permalink
fix: Support multiple chapter tracks with same language (#3868)
Browse files Browse the repository at this point in the history
Issue #3597
  • Loading branch information
Álvaro Velad Galván committed Jan 18, 2022
1 parent 9f53d39 commit 8c626ae
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 29 deletions.
26 changes: 15 additions & 11 deletions lib/player.js
Expand Up @@ -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;
}
Expand Down
49 changes: 41 additions & 8 deletions test/player_integration.js
Expand Up @@ -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);

Expand All @@ -1078,23 +1078,56 @@ 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 () => {
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.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');
Expand Down
49 changes: 41 additions & 8 deletions test/player_src_equals_integration.js
Expand Up @@ -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);

Expand All @@ -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 () => {
Expand All @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions test/test/assets/chapters.vtt
Expand Up @@ -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
10 changes: 10 additions & 0 deletions 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

0 comments on commit 8c626ae

Please sign in to comment.