Skip to content

Commit ba56bae

Browse files
authored
feat: Add getChaptersAsync method (#8541)
This allows the segmentIndex to be created if it is not already created and also allows operation with CastProxy.
1 parent 1a82e27 commit ba56bae

File tree

7 files changed

+70
-16
lines changed

7 files changed

+70
-16
lines changed

docs/tutorials/upgrade.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ application:
131131
- The `TimelineRegionInfo.eventElement` has been replaced with `TimelineRegionInfo.eventNode` property, the new property type is `shaka.externs.xml.Node` instead of `Element`
132132
- New API for audio: `getAudioTracks` and `selectAudioTrack`, we also deprecated in v4.14 `getAudioLanguages`, `getAudioLanguagesAndRoles` and `selectAudioLanguage`.
133133
- `shaka.util.FairPlayUtils` is moved to `shaka.drm.FairPlay` (Deprecated in v4.14)
134+
- `getChapters` is replaced by `getChaptersAsync` (Deprecated in v4.15)

lib/cast/cast_utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ shaka.cast.CastUtils.LargePlayerGetterMethods = new Map()
346346
.set('getImageTracks', 2)
347347
.set('getVideoTracks', 2)
348348
.set('getTextTracks', 2)
349-
.set('getThumbnails', 2)
350349
.set('getVariantTracks', 2);
351350

352351

@@ -393,9 +392,6 @@ shaka.cast.CastUtils.PlayerInitAfterLoadState = [
393392
* @const {!Array<string>}
394393
*/
395394
shaka.cast.CastUtils.PlayerVoidMethods = [
396-
'addChaptersTrack',
397-
'addTextTrackAsync',
398-
'addThumbnailsTrack',
399395
'cancelTrickPlay',
400396
'configure',
401397
'configurationForLowLatency',
@@ -422,6 +418,12 @@ shaka.cast.CastUtils.PlayerVoidMethods = [
422418
* @const {!Array<string>}
423419
*/
424420
shaka.cast.CastUtils.PlayerPromiseMethods = [
421+
'addChaptersTrack',
422+
'addTextTrackAsync',
423+
'addThumbnailsTrack',
424+
'getAllThumbnails',
425+
'getChaptersAsync',
426+
'getThumbnails',
425427
'attach',
426428
'attachCanvas',
427429
'detach',

lib/player.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6238,6 +6238,9 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
62386238
* @export
62396239
*/
62406240
getChapters(language) {
6241+
shaka.Deprecate.deprecateFeature(5,
6242+
'getChapters',
6243+
'Please use an getChaptersAsync.');
62416244
if (!this.externalChaptersStreams_.length) {
62426245
return [];
62436246
}
@@ -6272,6 +6275,50 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
62726275
return chapters;
62736276
}
62746277

6278+
/**
6279+
* This returns the list of chapters.
6280+
*
6281+
* @param {string} language
6282+
* @return {!Promise<!Array<shaka.extern.Chapter>>}
6283+
* @export
6284+
*/
6285+
async getChaptersAsync(language) {
6286+
if (!this.externalChaptersStreams_.length) {
6287+
return [];
6288+
}
6289+
const LanguageUtils = shaka.util.LanguageUtils;
6290+
const inputLanguage = LanguageUtils.normalize(language);
6291+
const chapterStreams = this.externalChaptersStreams_
6292+
.filter((c) => LanguageUtils.normalize(c.language) == inputLanguage);
6293+
if (!chapterStreams.length) {
6294+
return [];
6295+
}
6296+
const chapters = [];
6297+
const uniqueChapters = new Set();
6298+
for (const chapterStream of chapterStreams) {
6299+
if (!chapterStream.segmentIndex) {
6300+
// eslint-disable-next-line no-await-in-loop
6301+
await chapterStream.createSegmentIndex();
6302+
}
6303+
chapterStream.segmentIndex.forEachTopLevelReference((ref) => {
6304+
const title = ref.getUris()[0];
6305+
const id = ref.startTime + '-' + ref.endTime + '-' + title;
6306+
/** @type {shaka.extern.Chapter} */
6307+
const chapter = {
6308+
id,
6309+
title,
6310+
startTime: ref.startTime,
6311+
endTime: ref.endTime,
6312+
};
6313+
if (!uniqueChapters.has(id)) {
6314+
chapters.push(chapter);
6315+
uniqueChapters.add(id);
6316+
}
6317+
});
6318+
}
6319+
return chapters;
6320+
}
6321+
62756322
/**
62766323
* Ignore the TextTracks with the 'metadata' or 'chapters' kind, or the one
62776324
* generated by the SimpleTextDisplayer.

test/cast/cast_utils_unit.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ describe('CastUtils', () => {
2121
'getMediaElement', // Handled specially
2222
'setMaxHardwareResolution',
2323
'destroy', // Should use CastProxy.destroy instead
24-
'getAllThumbnails', // Too large to proxy.
2524
'drmInfo', // Too large to proxy
2625
'getManifest', // Too large to proxy
2726
'getManifestParserFactory', // Would not serialize.

test/player_integration.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ describe('Player', () => {
12901290

12911291
// Data should be available as soon as addChaptersTrack resolves.
12921292
// See https://github.com/shaka-project/shaka-player/issues/4186
1293-
const chapters = player.getChapters('en');
1293+
const chapters = await player.getChaptersAsync('en');
12941294
expect(chapters.length).toBe(3);
12951295
const chapter1 = chapters[0];
12961296
expect(chapter1.title).toBe('Chapter 1');
@@ -1309,7 +1309,7 @@ describe('Player', () => {
13091309
const absoluteUri2 = locationUri.resolve(partialUri2);
13101310
await player.addChaptersTrack(absoluteUri2.toString(), 'en');
13111311

1312-
const chaptersUpdated = player.getChapters('en');
1312+
const chaptersUpdated = await player.getChaptersAsync('en');
13131313
expect(chaptersUpdated.length).toBe(6);
13141314
const chapterUpdated1 = chaptersUpdated[0];
13151315
expect(chapterUpdated1.title).toBe('Chapter 1');
@@ -1344,7 +1344,7 @@ describe('Player', () => {
13441344
const absoluteUri = locationUri.resolve(partialUri);
13451345
await player.addChaptersTrack(absoluteUri.toString(), 'es');
13461346

1347-
const chapters = player.getChapters('es');
1347+
const chapters = await player.getChaptersAsync('es');
13481348
expect(chapters.length).toBe(3);
13491349
const chapter1 = chapters[0];
13501350
expect(chapter1.title).toBe('Chapter 1');

test/player_src_equals_integration.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe('Player Src Equals', () => {
354354

355355
// Data should be available as soon as addChaptersTrack resolves.
356356
// See https://github.com/shaka-project/shaka-player/issues/4186
357-
const chapters = player.getChapters('en');
357+
const chapters = await player.getChaptersAsync('en');
358358
expect(chapters.length).toBe(3);
359359
const chapter1 = chapters[0];
360360
expect(chapter1.title).toBe('Chapter 1');
@@ -373,7 +373,7 @@ describe('Player Src Equals', () => {
373373
const absoluteUri2 = locationUri.resolve(partialUri2);
374374
await player.addChaptersTrack(absoluteUri2.toString(), 'en');
375375

376-
const chaptersUpdated = player.getChapters('en');
376+
const chaptersUpdated = await player.getChaptersAsync('en');
377377
expect(chaptersUpdated.length).toBe(6);
378378
const chapterUpdated1 = chaptersUpdated[0];
379379
expect(chapterUpdated1.title).toBe('Chapter 1');
@@ -409,7 +409,7 @@ describe('Player Src Equals', () => {
409409
const absoluteUri = locationUri.resolve(partialUri);
410410
await player.addChaptersTrack(absoluteUri.toString(), 'es');
411411

412-
const chapters = player.getChapters('es');
412+
const chapters = await player.getChaptersAsync('es');
413413
expect(chapters.length).toBe(3);
414414
const chapter1 = chapters[0];
415415
expect(chapter1.title).toBe('Chapter 1');

ui/chapter_selection.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ shaka.ui.ChapterSelection = class extends shaka.ui.SettingsMenu {
100100
/**
101101
* @private
102102
*/
103-
updateChapters_() {
103+
async updateChapters_() {
104104
/**
105105
* Does a value compare on chapters.
106106
* @param {shaka.extern.Chapter} a
@@ -121,16 +121,21 @@ shaka.ui.ChapterSelection = class extends shaka.ui.SettingsMenu {
121121
nextLanguage = locale;
122122
// If player is a proxy, and the cast receiver doesn't support this
123123
// method, you get back undefined.
124-
nextChapters = this.player.getChapters(nextLanguage) || [];
124+
if (this.player) {
125+
// eslint-disable-next-line no-await-in-loop
126+
nextChapters = (await this.player.getChaptersAsync(nextLanguage)) || [];
127+
}
125128
if (nextChapters.length) {
126129
break;
127130
}
128131
}
129132
if (!nextChapters.length) {
130133
nextLanguage = 'und';
131-
// If player is a proxy, and the cast receiver doesn't support this
132-
// method, you get back undefined.
133-
nextChapters = this.player.getChapters(nextLanguage) || [];
134+
if (this.player) {
135+
// If player is a proxy, and the cast receiver doesn't support this
136+
// method, you get back undefined.
137+
nextChapters = (await this.player.getChaptersAsync(nextLanguage)) || [];
138+
}
134139
}
135140

136141
const languageChanged = nextLanguage !== this.chaptersLanguage_;

0 commit comments

Comments
 (0)