UNPKG

npaw-plugin-nwf

Version:
90 lines (89 loc) 4.22 kB
import { VideoSegment } from '../Storage/VideoSegment'; import Cdn from '../Loaders/Cdn'; import { SyntheticCdn } from '../Utils/Constants'; import P2PLoader from '../Loaders/P2PLoader'; import BalancerOptions from '../Utils/Options'; /** * @class * @description This class is responsible for informing the CDN Balancer which CDN is more suitable to perform a request * @exports CDNSelectorBusinessObject */ type SyntheticCdnKind = typeof SyntheticCdn[keyof typeof SyntheticCdn]; /** * Hook the selector uses to read / clear the long-lived "last failed trial" * marker. The state lives on `CDNLoader` because `CDNSelectorBusinessObject` * is reinstantiated per `updateUrl` call and would otherwise lose the marker * between segments. Android parity (`ActiveSwitcher.lastFailedTrialProvider` * from `plugin-android@56bf3e20`): set when a trial CDN failed to serve its * segment, cleared as soon as the next sort picks a different trial (or no * trial) so the same CDN isn't probed back-to-back after a failure. */ export interface LastFailedTrialAccessor { get(): Cdn | null; clear(): void; } export default class CDNSelectorBusinessObject { private _p2pLoader; private _cdnList; private _activeSwitching?; private _bandwidthThreshold; private _highetsSegmentBandwidth; private _options; private _onSyntheticCdn?; private _effectiveNoProbing; private _lastFailedTrial; constructor(cdnList: Map<string, Cdn>, bandwidthThreshold: number, highetsSegmentBandwidth: number, activeSwitching: string | undefined, loader: P2PLoader, options: BalancerOptions, onSyntheticCdn?: (kind: SyntheticCdnKind) => void, effectiveNoProbing?: boolean, lastFailedTrial?: LastFailedTrialAccessor); getCDN(segment: VideoSegment, probingInfo: { currentCDN: string; }): string; /** * Starts by verifying if we need to perform a probe to any CDN on the list. * If there is no CDN to probe, starts to perform requests to all CDNs to assess their bandwidth. When that process is done, choses the one with the highest bandwidth * @returns {string} CDN name. * @private */ private _qualityPriority; /** * Uses always the first CDN of the list until its bandwidth is lower than the defined value, then uses following one as a fallback. * If no CDNs are valid, chooses the one with the highest bandwidth * It also verifies if we need to perform a probe to a CDN. * @returns {string} CDN name. * @private */ private _cdnPriority; /** * Returns always the CDN with the best score value provided by the API. * @returns {string} CDN name. * @private */ private _bestScoredCdn; /** * Gate the trial-selection path on the two preconditions Android `a6dc9b66` * added: * 1. `onlyForVideoSegments` (TrialSettings, default true) — audio / * init / manifest fetches don't produce bandwidth signal worth * measuring, so they shouldn't burn the per-CDN cooldown. * 2. The current CDN must have a known bandwidth (EMA or peak). Without * it, the trial-deadline recompute has nothing to compare against * and would degenerate into "probe anything that's idle" — exactly * the regime that motivated the dynamic-trial-timeout work. */ private _eligibleForTrial; /** * Stamp the segment as a trial fetch and capture the data the dynamic * trial-deadline recompute needs (Android `TrialTag`): the current good * CDN's bandwidth (so the recompute knows the fallback time), its name (for * diagnostics), the safety factor, and the baseline call timeout to compare * the recomputed deadline against in the "stricter only" check. */ private _tagAsTrial; /** * Based on the RTT and estimated bandwidth filters and sorts the list of CDNs. If after filtering we don't have an empty list returns the name of the first CDN. * @param cdnList The list of CDNs available * @param asMode The active switching mode * @param cdnInUse The CDN currently being used * @returns The CDN name of the CDN to probe */ private _getCdnToProbe; } export {};