Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement event for maxAutoLevel change #5763

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1208,6 +1208,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 @@ -1792,6 +1794,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 @@ -662,6 +664,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 @@ -52,7 +52,7 @@ export default class Hls implements HlsEventEmitter {
private coreComponents: ComponentAPI[];
private networkControllers: NetworkComponentAPI[];
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 @@ -121,8 +121,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 @@ -382,6 +380,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 @@ -637,6 +637,7 @@ export default class Hls implements HlsEventEmitter {
if (this._autoLevelCapping !== newLevel) {
logger.log(`set autoLevelCapping:${newLevel}`);
this._autoLevelCapping = newLevel;
this.levelController.checkMaxAutoUpdated();
}
}

Expand All @@ -645,8 +646,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 @@ -221,6 +222,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