Skip to content

Commit

Permalink
API: Add pathwayPriority getter/setter (video-dev#6295)
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelFomin90 authored and grabofus committed Jun 24, 2024
1 parent f92c68e commit 4577416
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
7 changes: 7 additions & 0 deletions api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ export class ContentSteeringController implements NetworkComponentAPI {
// (undocumented)
filterParsedLevels(levels: Level[]): Level[];
// (undocumented)
get pathwayPriority(): string[] | null;
set pathwayPriority(pathwayPriority: string[]);
// (undocumented)
pathways(): string[];
// (undocumented)
removeLevel(levelToRemove: Level): void;
// (undocumented)
startLoad(): void;
Expand Down Expand Up @@ -1637,6 +1642,8 @@ class Hls implements HlsEventEmitter {
on<E extends keyof HlsListeners, Context = undefined>(event: E, listener: HlsListeners[E], context?: Context): void;
// (undocumented)
once<E extends keyof HlsListeners, Context = undefined>(event: E, listener: HlsListeners[E], context?: Context): void;
get pathwayPriority(): string[] | null;
set pathwayPriority(pathwayPriority: string[]);
pauseBuffering(): void;
get playingDate(): Date | null;
recoverMediaError(): void;
Expand Down
30 changes: 21 additions & 9 deletions src/controller/content-steering-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class ContentSteeringController implements NetworkComponentAPI {
private loader: Loader<LoaderContext> | null = null;
private uri: string | null = null;
private pathwayId: string = '.';
private pathwayPriority: string[] | null = null;
private _pathwayPriority: string[] | null = null;
private timeToLoad: number = 300;
private reloadTimer: number = -1;
private updated: number = 0;
Expand Down Expand Up @@ -90,6 +90,23 @@ export default class ContentSteeringController implements NetworkComponentAPI {
hls.off(Events.ERROR, this.onError, this);
}

pathways() {
return (this.levels || []).reduce((pathways, level) => {
if (pathways.indexOf(level.pathwayId) === -1) {
pathways.push(level.pathwayId);
}
return pathways;
}, [] as string[]);
}

get pathwayPriority(): string[] | null {
return this._pathwayPriority;
}

set pathwayPriority(pathwayPriority: string[]) {
this.updatePathwayPriority(pathwayPriority);
}

startLoad() {
this.started = true;
this.clearTimeout();
Expand Down Expand Up @@ -176,7 +193,7 @@ export default class ContentSteeringController implements NetworkComponentAPI {
errorAction.flags === ErrorActionFlags.MoveAllAlternatesMatchingHost
) {
const levels = this.levels;
let pathwayPriority = this.pathwayPriority;
let pathwayPriority = this._pathwayPriority;
let errorPathway = this.pathwayId;
if (data.context) {
const { groupId, pathwayId, type } = data.context;
Expand All @@ -191,12 +208,7 @@ export default class ContentSteeringController implements NetworkComponentAPI {
}
if (!pathwayPriority && levels) {
// If PATHWAY-PRIORITY was not provided, list pathways for error handling
pathwayPriority = levels.reduce((pathways, level) => {
if (pathways.indexOf(level.pathwayId) === -1) {
pathways.push(level.pathwayId);
}
return pathways;
}, [] as string[]);
pathwayPriority = this.pathways();
}
if (pathwayPriority && pathwayPriority.length > 1) {
this.updatePathwayPriority(pathwayPriority);
Expand Down Expand Up @@ -245,7 +257,7 @@ export default class ContentSteeringController implements NetworkComponentAPI {
}

private updatePathwayPriority(pathwayPriority: string[]) {
this.pathwayPriority = pathwayPriority;
this._pathwayPriority = pathwayPriority;
let levels: Level[] | undefined;

// Evaluate if we should remove the pathway from the penalized list
Expand Down
24 changes: 24 additions & 0 deletions src/controller/level-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,30 @@ export default class LevelController extends BasePlaylistController {
this._startLevel = newLevel;
}

get pathwayPriority(): string[] | null {
if (this.steering) {
return this.steering.pathwayPriority;
}

return null;
}

set pathwayPriority(pathwayPriority: string[]) {
if (this.steering) {
const pathwaysList = this.steering.pathways();
const filteredPathwayPriority = pathwayPriority.filter((pathwayId) => {
return pathwaysList.indexOf(pathwayId) !== -1;
});
if (pathwayPriority.length < 1) {
this.warn(
`pathwayPriority ${pathwayPriority} should contain at least one pathway from list: ${pathwaysList}`,
);
return;
}
this.steering.pathwayPriority = filteredPathwayPriority;
}
}

protected onError(event: Events.ERROR, data: ErrorData) {
if (data.fatal || !data.context) {
return;
Expand Down
11 changes: 11 additions & 0 deletions src/hls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,17 @@ export default class Hls implements HlsEventEmitter {
get forceStartLoad(): boolean {
return this.streamController.forceStartLoad;
}

/**
* ContentSteering pathwayPriority getter/setter
*/
get pathwayPriority(): string[] | null {
return this.levelController.pathwayPriority;
}

set pathwayPriority(pathwayPriority: string[]) {
this.levelController.pathwayPriority = pathwayPriority;
}
}

export type {
Expand Down

0 comments on commit 4577416

Please sign in to comment.