Skip to content

Commit

Permalink
Implement event for maxAutoLevel change (#5763)
Browse files Browse the repository at this point in the history
  • Loading branch information
robwalch authored Aug 31, 2023
1 parent 301be58 commit 6227816
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,8 @@ export enum Events {
// (undocumented)
MANIFEST_PARSED = "hlsManifestParsed",
// (undocumented)
MAX_AUTO_LEVEL_UPDATED = "hlsMaxAutoLevelUpdated",
// (undocumented)
MEDIA_ATTACHED = "hlsMediaAttached",
// (undocumented)
MEDIA_ATTACHING = "hlsMediaAttaching",
Expand Down Expand Up @@ -1802,6 +1804,10 @@ export interface HlsListeners {
[Events.MANIFEST_LOADING]: (event: Events.MANIFEST_LOADING, data: ManifestLoadingData) => void;
// (undocumented)
[Events.MANIFEST_PARSED]: (event: Events.MANIFEST_PARSED, data: ManifestParsedData) => void;
// Warning: (ae-forgotten-export) The symbol "MaxAutoLevelUpdatedData" needs to be exported by the entry point hls.d.ts
//
// (undocumented)
[Events.MAX_AUTO_LEVEL_UPDATED]: (event: Events.MAX_AUTO_LEVEL_UPDATED, data: MaxAutoLevelUpdatedData) => void;
// (undocumented)
[Events.MEDIA_ATTACHED]: (event: Events.MEDIA_ATTACHED, data: MediaAttachedData) => void;
// (undocumented)
Expand Down
16 changes: 16 additions & 0 deletions src/controller/level-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ let chromeOrFirefox: boolean;
export default class LevelController extends BasePlaylistController {
private _levels: Level[] = [];
private _firstLevel: number = -1;
private _maxAutoLevel: number = -1;
private _startLevel?: number;
private currentLevel: Level | null = null;
private currentLevelIndex: number = -1;
Expand Down Expand Up @@ -104,6 +105,7 @@ export default class LevelController extends BasePlaylistController {
this.currentLevelIndex = -1;
this.currentLevel = null;
this._levels = [];
this._maxAutoLevel = -1;
}

private onManifestLoading(
Expand Down Expand Up @@ -680,6 +682,20 @@ export default class LevelController extends BasePlaylistController {
) {
this._levels = levels;
}

public checkMaxAutoUpdated() {
const { autoLevelCapping, maxAutoLevel, maxHdcpLevel } = this.hls;
if (this._maxAutoLevel !== maxAutoLevel) {
this._maxAutoLevel = maxAutoLevel;
this.hls.trigger(Events.MAX_AUTO_LEVEL_UPDATED, {
autoLevelCapping,
levels: this.levels,
maxAutoLevel,
minAutoLevel: this.hls.minAutoLevel,
maxHdcpLevel,
});
}
}
}

function assignTrackIdsByGroup(tracks: MediaPlaylist[]): void {
Expand Down
7 changes: 7 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
BufferFlushingData,
BufferEOSData,
LevelSwitchingData,
MaxAutoLevelUpdatedData,
FPSDropLevelCappingData,
FPSDropData,
BufferCreatedData,
Expand Down Expand Up @@ -151,6 +152,8 @@ export enum Events {
FPS_DROP = 'hlsFpsDrop',
// triggered when FPS drop triggers auto level capping - data: { level, droppedLevel }
FPS_DROP_LEVEL_CAPPING = 'hlsFpsDropLevelCapping',
// triggered when maxAutoLevel changes - data { autoLevelCapping, levels, maxAutoLevel, minAutoLevel, maxHdcpLevel }
MAX_AUTO_LEVEL_UPDATED = 'hlsMaxAutoLevelUpdated',
// Identifier for an error event - data: { type : error type, details : error details, fatal : if true, hls.js cannot/will not try to recover, if false, hls.js will try to recover,other error specific data }
ERROR = 'hlsError',
// fired when hls.js instance starts destroying. Different from MEDIA_DETACHED as one could want to detach and reattach a media to the instance of hls.js to handle mid-rolls for example - data: { }
Expand Down Expand Up @@ -349,6 +352,10 @@ export interface HlsListeners {
event: Events.FPS_DROP_LEVEL_CAPPING,
data: FPSDropLevelCappingData,
) => void;
[Events.MAX_AUTO_LEVEL_UPDATED]: (
event: Events.MAX_AUTO_LEVEL_UPDATED,
data: MaxAutoLevelUpdatedData,
) => void;
[Events.ERROR]: (event: Events.ERROR, data: ErrorData) => void;
[Events.DESTROYING]: (event: Events.DESTROYING) => void;
[Events.KEY_LOADING]: (
Expand Down
10 changes: 6 additions & 4 deletions src/hls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Hls implements HlsEventEmitter {
private networkControllers: NetworkComponentAPI[];
private started: boolean = false;
private _emitter: HlsEventEmitter = new EventEmitter();
private _autoLevelCapping: number;
private _autoLevelCapping: number = -1;
private _maxHdcpLevel: HdcpLevel = null;
private abrController: AbrController;
private bufferController: BufferController;
Expand Down Expand Up @@ -122,8 +122,6 @@ export default class Hls implements HlsEventEmitter {
const config = (this.config = mergeConfig(Hls.DefaultConfig, userConfig));
this.userConfig = userConfig;

this._autoLevelCapping = -1;

if (config.progressive) {
enableStreamingMode(config);
}
Expand Down Expand Up @@ -383,6 +381,8 @@ export default class Hls implements HlsEventEmitter {
alwaysNormalize: true,
},
));
this._autoLevelCapping = -1;
this._maxHdcpLevel = null;
logger.log(`loadSource:${loadingSource}`);
if (
media &&
Expand Down Expand Up @@ -665,6 +665,7 @@ export default class Hls implements HlsEventEmitter {
if (this._autoLevelCapping !== newLevel) {
logger.log(`set autoLevelCapping:${newLevel}`);
this._autoLevelCapping = newLevel;
this.levelController.checkMaxAutoUpdated();
}
}

Expand All @@ -673,8 +674,9 @@ export default class Hls implements HlsEventEmitter {
}

set maxHdcpLevel(value: HdcpLevel) {
if (isHdcpLevel(value)) {
if (isHdcpLevel(value) && this._maxHdcpLevel !== value) {
this._maxHdcpLevel = value;
this.levelController.checkMaxAutoUpdated();
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Fragment } from '../loader/fragment';
import type { Part } from '../loader/fragment';
import type { LevelDetails } from '../loader/level-details';
import type {
HdcpLevel,
HlsUrlParameters,
Level,
LevelParsed,
Expand Down Expand Up @@ -222,6 +223,14 @@ export interface FPSDropLevelCappingData {
level: number;
}

export interface MaxAutoLevelUpdatedData {
autoLevelCapping: number;
levels: Level[] | null;
maxAutoLevel: number;
minAutoLevel: number;
maxHdcpLevel: HdcpLevel;
}

export interface ErrorData {
type: ErrorTypes;
details: ErrorDetails;
Expand Down

0 comments on commit 6227816

Please sign in to comment.