Skip to content

Commit c0455d7

Browse files
authored
feat: Simplicate Text and Chapters APIs (#8264)
1 parent 8d6ae34 commit c0455d7

File tree

7 files changed

+126
-129
lines changed

7 files changed

+126
-129
lines changed

externs/shaka/player.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,79 @@ shaka.extern.Track;
464464
shaka.extern.AudioTrack;
465465

466466

467+
/**
468+
* @typedef {{
469+
* id: number,
470+
* active: boolean,
471+
* bandwidth: number,
472+
* language: string,
473+
* label: ?string,
474+
* kind: ?string,
475+
* mimeType: ?string,
476+
* codecs: ?string,
477+
* primary: boolean,
478+
* roles: !Array<string>,
479+
* accessibilityPurpose: ?shaka.media.ManifestParser.AccessibilityPurpose,
480+
* forced: boolean,
481+
* originalTextId: ?string,
482+
* originalLanguage: ?string
483+
* }}
484+
*
485+
* @description
486+
* An object describing a media track. This object should be treated as
487+
* read-only as changing any values does not have any effect. This is the
488+
* public view of an audio/video paring (variant type) or text track (text
489+
* type) or image track (image type).
490+
*
491+
* @property {number} id
492+
* The unique ID of the track.
493+
* @property {boolean} active
494+
* If true, this is the track being streamed (another track may be
495+
* visible/audible in the buffer).
496+
* @property {number} bandwidth
497+
* The bandwidth required to play the track, in bits/sec.
498+
* @property {string} language
499+
* The language of the track, or <code>'und'</code> if not given. This value
500+
* is normalized as follows - language part is always lowercase and translated
501+
* to ISO-639-1 when possible, locale part is always uppercase,
502+
* i.e. <code>'en-US'</code>.
503+
* @property {?string} label
504+
* The track label, which is unique text that should describe the track.
505+
* @property {?string} kind
506+
* (only for text tracks) The kind of text track, either
507+
* <code>'caption'</code> or <code>'subtitle'</code>.
508+
* @property {?string} mimeType
509+
* The MIME type of the content provided in the manifest.
510+
* @property {?string} codecs
511+
* The audio/video codecs string provided in the manifest, if present.
512+
* @property {boolean} primary
513+
* True indicates that this in the primary language for the content.
514+
* This flag is based on signals from the manifest.
515+
* This can be a useful hint about which language should be the default, and
516+
* indicates which track Shaka will use when the user's language preference
517+
* cannot be satisfied.
518+
* @property {!Array<string>} roles
519+
* The roles of the track, e.g. <code>'main'</code>, <code>'caption'</code>,
520+
* or <code>'commentary'</code>.
521+
* @property {?shaka.media.ManifestParser.AccessibilityPurpose
522+
* } accessibilityPurpose
523+
* The DASH accessibility descriptor, if one was provided for this track.
524+
* For text tracks, this describes the text; otherwise, this is for the audio.
525+
* @property {boolean} forced
526+
* True indicates that this in the forced text language for the content.
527+
* This flag is based on signals from the manifest.
528+
* @property {?string} originalTextId
529+
* (text tracks only) The original ID of the text track, if any, as it
530+
* appeared in the original manifest.
531+
* @property {?string} originalLanguage
532+
* The original language of the track, if any, as it appeared in the original
533+
* manifest. This is the exact value provided in the manifest; for normalized
534+
* value use <code>language</code> property.
535+
* @exportDoc
536+
*/
537+
shaka.extern.TextTrack;
538+
539+
467540
/**
468541
* @typedef {!Array<!shaka.extern.Track>}
469542
*/

lib/media/preload_manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
364364
/**
365365
* Gets the preloaded text track if it exists.
366366
*
367-
* @return {?shaka.extern.Track}
367+
* @return {?shaka.extern.TextTrack}
368368
* @export
369369
*/
370370
getPrefetchedTextTrack() {

lib/player.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5099,7 +5099,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
50995099
* <p>
51005100
* If the player has not loaded content, this will return an empty list.
51015101
*
5102-
* @return {!Array<shaka.extern.Track>}
5102+
* @return {!Array<shaka.extern.TextTrack>}
51035103
* @export
51045104
*/
51055105
getTextTracks() {
@@ -5344,7 +5344,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
53445344
* Note that <code>AdaptationEvents</code> are not fired for manual track
53455345
* selections.
53465346
*
5347-
* @param {shaka.extern.Track} track
5347+
* @param {shaka.extern.TextTrack} track
53485348
* @export
53495349
*/
53505350
selectTextTrack(track) {
@@ -5673,7 +5673,13 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
56735673
* Return a list of text language-role combinations available. If the player
56745674
* has not loaded any content, this will be return an empty list.
56755675
*
5676+
* <br>
5677+
*
5678+
* This API is deprecated and will be removed in version 5.0, please migrate
5679+
* to using `getTextTracks` and `selectTextTrack`.
5680+
*
56765681
* @return {!Array<shaka.extern.LanguageRole>}
5682+
* @deprecated
56775683
* @export
56785684
*/
56795685
getTextLanguagesAndRoles() {
@@ -5701,7 +5707,13 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
57015707
* Return a list of text languages available. If the player has not loaded
57025708
* any content, this will return an empty list.
57035709
*
5710+
* <br>
5711+
*
5712+
* This API is deprecated and will be removed in version 5.0, please migrate
5713+
* to using `getTextTracks` and `selectTextTrack`.
5714+
*
57045715
* @return {!Array<string>}
5716+
* @deprecated
57055717
* @export
57065718
*/
57075719
getTextLanguages() {
@@ -5812,9 +5824,15 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
58125824
* language and role, and chooses a new variant if need be. If the player has
58135825
* not loaded any content, this will be a no-op.
58145826
*
5827+
* <br>
5828+
*
5829+
* This API is deprecated and will be removed in version 5.0, please migrate
5830+
* to using `getTextTracks` and `selectTextTrack`.
5831+
*
58155832
* @param {string} language
58165833
* @param {string=} role
58175834
* @param {boolean=} forced
5835+
* @deprecated
58185836
* @export
58195837
*/
58205838
selectTextLanguage(language, role, forced = false) {
@@ -5964,7 +5982,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
59645982
/**
59655983
* Return a list of chapters tracks.
59665984
*
5967-
* @return {!Array<shaka.extern.Track>}
5985+
* @return {!Array<shaka.extern.TextTrack>}
59685986
* @export
59695987
*/
59705988
getChaptersTracks() {
@@ -6376,7 +6394,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
63766394
* @param {string=} codec
63776395
* @param {string=} label
63786396
* @param {boolean=} forced
6379-
* @return {!Promise<shaka.extern.Track>}
6397+
* @return {!Promise<shaka.extern.TextTrack>}
63806398
* @export
63816399
*/
63826400
async addTextTrackAsync(uri, language, kind, mimeType, codec, label,
@@ -6689,7 +6707,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
66896707
* @param {string} uri
66906708
* @param {string} language
66916709
* @param {string=} mimeType
6692-
* @return {!Promise<shaka.extern.Track>}
6710+
* @return {!Promise<shaka.extern.TextTrack>}
66936711
* @export
66946712
*/
66956713
async addChaptersTrack(uri, language, mimeType) {
@@ -8621,7 +8639,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
86218639
/**
86228640
* Get the normalized languages for a group of tracks.
86238641
*
8624-
* @param {!Array<?shaka.extern.Track>} tracks
8642+
* @param {!Array<?(shaka.extern.Track|shaka.extern.TextTrack)>} tracks
86258643
* @return {!Set<string>}
86268644
* @private
86278645
*/
@@ -8643,7 +8661,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
86438661
* Get all permutations of normalized languages and role for a group of
86448662
* tracks.
86458663
*
8646-
* @param {!Array<?shaka.extern.Track>} tracks
8664+
* @param {!Array<?(shaka.extern.Track|shaka.extern.TextTrack)>} tracks
86478665
* @return {!Array<shaka.extern.LanguageRole>}
86488666
* @private
86498667
*/
@@ -8653,7 +8671,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
86538671
/** @type {!Map<string, !Map<string, string>>} */
86548672
const languageRoleToLabel = new Map();
86558673

8656-
for (const track of tracks) {
8674+
for (let i = 0; i < tracks.length; i++) {
8675+
const track = /** @type {shaka.extern.Track} */(tracks[i]);
86578676
let language = 'und';
86588677
let roles = [];
86598678

lib/util/stream_utils.js

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,51 +1458,24 @@ shaka.util.StreamUtils = class {
14581458

14591459
/**
14601460
* @param {shaka.extern.Stream} stream
1461-
* @return {shaka.extern.Track}
1461+
* @return {shaka.extern.TextTrack}
14621462
*/
14631463
static textStreamToTrack(stream) {
1464-
const ContentType = shaka.util.ManifestParserUtils.ContentType;
1465-
1466-
/** @type {shaka.extern.Track} */
1464+
/** @type {shaka.extern.TextTrack} */
14671465
const track = {
14681466
id: stream.id,
14691467
active: false,
1470-
type: ContentType.TEXT,
14711468
bandwidth: 0,
14721469
language: stream.language,
14731470
label: stream.label,
14741471
kind: stream.kind || null,
1475-
width: null,
1476-
height: null,
1477-
frameRate: null,
1478-
pixelAspectRatio: null,
1479-
hdr: null,
1480-
colorGamut: null,
1481-
videoLayout: null,
14821472
mimeType: stream.mimeType,
1483-
audioMimeType: null,
1484-
videoMimeType: null,
14851473
codecs: stream.codecs || null,
1486-
audioCodec: null,
1487-
videoCodec: null,
14881474
primary: stream.primary,
14891475
roles: stream.roles,
1490-
audioRoles: null,
1476+
accessibilityPurpose: stream.accessibilityPurpose,
14911477
forced: stream.forced,
1492-
videoId: null,
1493-
audioId: null,
1494-
audioGroupId: null,
1495-
channelsCount: null,
1496-
audioSamplingRate: null,
1497-
spatialAudio: false,
1498-
tilesLayout: null,
1499-
audioBandwidth: null,
1500-
videoBandwidth: null,
1501-
originalVideoId: null,
1502-
originalAudioId: null,
15031478
originalTextId: stream.originalId,
1504-
originalImageId: null,
1505-
accessibilityPurpose: stream.accessibilityPurpose,
15061479
originalLanguage: stream.originalLanguage,
15071480
};
15081481

@@ -1605,16 +1578,26 @@ shaka.util.StreamUtils = class {
16051578

16061579
/**
16071580
* @param {TextTrack} textTrack
1608-
* @return {shaka.extern.Track}
1581+
* @return {shaka.extern.TextTrack}
16091582
*/
16101583
static html5TextTrackToTrack(textTrack) {
1611-
const StreamUtils = shaka.util.StreamUtils;
1612-
1613-
/** @type {shaka.extern.Track} */
1614-
const track = StreamUtils.html5TrackToGenericShakaTrack_(textTrack);
1615-
track.active = textTrack.mode != 'disabled';
1616-
track.type = 'text';
1617-
track.originalTextId = textTrack.id;
1584+
/** @type {shaka.extern.TextTrack} */
1585+
const track = {
1586+
id: shaka.util.StreamUtils.html5TrackId(textTrack),
1587+
active: textTrack.mode != 'disabled',
1588+
bandwidth: 0,
1589+
language: shaka.util.LanguageUtils.normalize(textTrack.language || 'und'),
1590+
label: textTrack.label,
1591+
kind: textTrack.kind,
1592+
mimeType: null,
1593+
codecs: null,
1594+
primary: false,
1595+
roles: [],
1596+
accessibilityPurpose: null,
1597+
forced: textTrack.kind == 'forced',
1598+
originalTextId: textTrack.id,
1599+
originalLanguage: textTrack.language,
1600+
};
16181601
if (textTrack.kind == 'captions') {
16191602
// See: https://github.com/shaka-project/shaka-player/issues/6233
16201603
track.mimeType = 'unknown';
@@ -1625,9 +1608,6 @@ shaka.util.StreamUtils = class {
16251608
if (textTrack.kind) {
16261609
track.roles = [textTrack.kind];
16271610
}
1628-
if (textTrack.kind == 'forced') {
1629-
track.forced = true;
1630-
}
16311611

16321612
return track;
16331613
}

0 commit comments

Comments
 (0)