Skip to content

Commit a57ac95

Browse files
committed
feat(srgssr-middleware): add SRG SSR cues to text track as soon as possible
Allows SRG SSR `cues` to be made available as soon as the text track is added to the `player`. This lets retrieve the `cues` as soon as the `addtrack` event is emitted. - add `getChapters`, `getBlockedSegments`, `getIntervals`, `addTextTrackCues` - update `addChapters`, `addBlockedSegments`, `addIntervals` to use the newly created methods - update `createTextTrack` by adding a parameter that allows adding the `cues` to the track before it is passed to the `player` - add `addTextTrackCues` test cases - add `createTextTrack` test cases - add `getBlockedSegments` test cases - add `getChapters` test cases - add `getIntervals` test cases
1 parent 490a8a3 commit a57ac95

File tree

2 files changed

+293
-25
lines changed

2 files changed

+293
-25
lines changed

src/middleware/srgssr.js

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,9 @@ class SrgSsr {
3030
*/
3131
static async addBlockedSegments(player, segments = []) {
3232
const trackId = 'srgssr-blocked-segments';
33-
const segmentTrack = await SrgSsr.createTextTrack(player, trackId);
33+
const blockedSegmentsToAdd = SrgSsr.getBlockedSegments(segments);
3434

35-
if (!Array.isArray(segments) || !segments.length) return;
36-
37-
const blockedSegments = segments.filter(segment => segment.blockReason);
38-
39-
if (!blockedSegments.length) return;
40-
41-
blockedSegments.forEach(segment => {
42-
SrgSsr.addTextTrackCue(segmentTrack, segment);
43-
});
35+
await SrgSsr.createTextTrack(player, trackId, blockedSegmentsToAdd);
4436
}
4537

4638
/**
@@ -90,6 +82,24 @@ class SrgSsr {
9082
));
9183
}
9284

85+
/**
86+
* Add a new cues to a text track with the given array.
87+
*
88+
* @param {TextTrack} textTrack
89+
* @param {Array.<
90+
* Segment |
91+
* Chapter |
92+
* TimeInterval>
93+
* } cues SRG SSR's cues-like representation
94+
*/
95+
static addTextTrackCues(textTrack, cues = []) {
96+
if (!Array.isArray(cues)) return;
97+
98+
cues.forEach(cue => {
99+
SrgSsr.addTextTrackCue(textTrack, cue);
100+
});
101+
}
102+
93103
/**
94104
* Add multiple text tracks to the player.
95105
*
@@ -112,15 +122,9 @@ class SrgSsr {
112122
*/
113123
static async addChapters(player, chapterUrn, chapters = []) {
114124
const trackId = 'srgssr-chapters';
115-
const chapterTrack = await SrgSsr.createTextTrack(player, trackId);
116-
117-
if (!Array.isArray(chapters) || !chapters.length) return;
125+
const chaptersToAdd = SrgSsr.getChapters(chapterUrn, chapters);
118126

119-
chapters.forEach(chapter => {
120-
if (chapterUrn !== chapter.fullLengthUrn) return;
121-
122-
SrgSsr.addTextTrackCue(chapterTrack, chapter);
123-
});
127+
await SrgSsr.createTextTrack(player, trackId, chaptersToAdd);
124128
}
125129

126130
/**
@@ -131,13 +135,9 @@ class SrgSsr {
131135
*/
132136
static async addIntervals(player, intervals = []) {
133137
const trackId = 'srgssr-intervals';
134-
const intervalTrack = await SrgSsr.createTextTrack(player, trackId);
135-
136-
if (!Array.isArray(intervals) || !intervals.length) return;
138+
const instervalsToAdd = SrgSsr.getIntervals(intervals);
137139

138-
intervals.forEach(interval => {
139-
SrgSsr.addTextTrackCue(intervalTrack, interval);
140-
});
140+
await SrgSsr.createTextTrack(player, trackId, instervalsToAdd);
141141
}
142142

143143
/**
@@ -257,10 +257,15 @@ class SrgSsr {
257257
*
258258
* @param {Player} player
259259
* @param {String} trackId Text track unique ID
260+
* @param {Array.<
261+
* Segment |
262+
* Chapter |
263+
* TimeInterval>
264+
* } cues SRG SSR's cues-like representation
260265
*
261266
* @returns {Promise<TextTrack>}
262267
*/
263-
static async createTextTrack(player, trackId) {
268+
static async createTextTrack(player, trackId, cues = []) {
264269
const removeTrack = player.textTracks().getTrackById(trackId);
265270

266271
if (removeTrack) {
@@ -279,6 +284,8 @@ class SrgSsr {
279284
}, 100);
280285
});
281286

287+
SrgSsr.addTextTrackCues(textTrack, cues);
288+
282289
player.textTracks().addTrack(textTrack);
283290

284291
return textTrack;
@@ -424,6 +431,47 @@ class SrgSsr {
424431
return isBlocked ? blockedSegment : undefined;
425432
}
426433

434+
/**
435+
* Get blocked segments.
436+
*
437+
* @param {Player} player
438+
* @param {Array<Segment>} [segments=[]]
439+
*
440+
* @returns {Array<Segment>}
441+
*/
442+
static getBlockedSegments(segments = []) {
443+
if (!Array.isArray(segments) || !segments.length) return [];
444+
445+
return segments.filter(segment => segment.blockReason);
446+
}
447+
448+
/**
449+
* Get chapters related to the main chapter.
450+
*
451+
* @param {string} chapterUrn The URN of the main chapter.
452+
* @param {Array.<Chapter>} [chapters=[]]
453+
*
454+
* @returns {Array.<Chapter>}
455+
*/
456+
static getChapters(chapterUrn, chapters = []) {
457+
if (!Array.isArray(chapters) || !chapters.length) return [];
458+
459+
return chapters.filter(chapter => chapterUrn === chapter.fullLengthUrn);
460+
}
461+
462+
/**
463+
* Get intervals.
464+
*
465+
* @param {Array<TimeInterval>} [segments=[]]
466+
*
467+
* @returns {Array<TimeInterval>}
468+
*/
469+
static getIntervals(intervals = []) {
470+
if (!Array.isArray(intervals) || !intervals.length) return [];
471+
472+
return intervals;
473+
}
474+
427475
/**
428476
* Get mediaComposition from an URN.
429477
*

0 commit comments

Comments
 (0)