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

[POC] Update Manifest structure to make HLS implementation possible #1493

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
56 changes: 33 additions & 23 deletions src/core/adaptive/adaptive_representation_selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import config from "../../config";
import log from "../../log";
import type {
IAdaptation,
IManifest,
IPeriod,
IRepresentation,
ISegment,
ITrack,
} from "../../manifest";
import type {
ObservationPosition,
Expand Down Expand Up @@ -87,24 +87,24 @@ export default function createAdaptiveRepresentationSelector(
* @see IRepresentationEstimator
* @param {Object} context
* @param {Object} currentRepresentation
* @param {Object} representations
* @param {Object} representationList
* @param {Object} playbackObserver
* @param {Object} stopAllEstimates
* @returns {Array.<Object>}
*/
return function getEstimates(
context: { manifest: IManifest; period: IPeriod; adaptation: IAdaptation },
context: { manifest: IManifest; period: IPeriod; track: ITrack },
currentRepresentation: IReadOnlySharedReference<IRepresentation | null>,
representations: IReadOnlySharedReference<IRepresentation[]>,
representationList: IReadOnlySharedReference<IRepresentationListItem[]>,
playbackObserver: IReadOnlyPlaybackObserver<IRepresentationEstimatorPlaybackObservation>,
stopAllEstimates: CancellationSignal,
): IRepresentationEstimatorResponse {
const { type } = context.adaptation;
const bandwidthEstimator = _getBandwidthEstimator(type);
const initialBitrate = initialBitrates[type] ?? 0;
const { trackType } = context.track;
const bandwidthEstimator = _getBandwidthEstimator(trackType);
const initialBitrate = initialBitrates[trackType] ?? 0;
const filters = {
limitResolution: throttlers.limitResolution[type] ?? limitResolutionDefaultRef,
throttleBitrate: throttlers.throttleBitrate[type] ?? throttleBitrateDefaultRef,
limitResolution: throttlers.limitResolution[trackType] ?? limitResolutionDefaultRef,
throttleBitrate: throttlers.throttleBitrate[trackType] ?? throttleBitrateDefaultRef,
};
return getEstimateReference(
{
Expand All @@ -114,7 +114,7 @@ export default function createAdaptiveRepresentationSelector(
filters,
initialBitrate,
playbackObserver,
representations,
representationList,
lowLatencyMode,
},
stopAllEstimates,
Expand Down Expand Up @@ -166,7 +166,7 @@ function getEstimateReference(
initialBitrate,
lowLatencyMode,
playbackObserver,
representations: representationsRef,
representationList: representationsRef,
}: IRepresentationEstimatorArguments,
stopAllEstimates: CancellationSignal,
): IRepresentationEstimatorResponse {
Expand Down Expand Up @@ -222,14 +222,14 @@ function getEstimateReference(
* produced.
*/
function createEstimateReference(
unsortedRepresentations: IRepresentation[],
unsortedRepresentations: IRepresentationListItem[],
innerCancellationSignal: CancellationSignal,
): SharedReference<IABREstimate> {
if (unsortedRepresentations.length <= 1) {
// There's only a single Representation. Just choose it.
return new SharedReference<IABREstimate>({
bitrate: undefined,
representation: unsortedRepresentations[0],
representation: unsortedRepresentations[0]?.representation,
urgent: true,
knownStableBitrate: undefined,
});
Expand All @@ -240,7 +240,7 @@ function getEstimateReference(

/** Ensure `Representation` objects are sorted by bitrates and only rely on this. */
const sortedRepresentations = unsortedRepresentations.sort(
(ra, rb) => ra.bitrate - rb.bitrate,
(ra, rb) => ra.bandwidth - rb.bandwidth,
);

/**
Expand All @@ -249,7 +249,7 @@ function getEstimateReference(
* buffer size etc.).
*/
const bufferBasedChooser = new BufferBasedChooser(
sortedRepresentations.map((r) => r.bitrate),
sortedRepresentations.map((r) => r.bandwidth),
);

/** Store the previous estimate made here. */
Expand Down Expand Up @@ -560,10 +560,10 @@ function getEstimateReference(
* @returns {Array.<Representation>}
*/
function getFilteredRepresentations(
representations: IRepresentation[],
representations: IRepresentationListItem[],
resolutionLimit: IResolutionInfo | undefined,
bitrateThrottle: number | undefined,
): IRepresentation[] {
): IRepresentationListItem[] {
let filteredReps = representations;

if (bitrateThrottle !== undefined && bitrateThrottle < Infinity) {
Expand Down Expand Up @@ -670,7 +670,7 @@ export interface IMetricsCallbackPayload {
/** Context about the segment downloaded. */
content: {
representation: IRepresentation;
adaptation: IAdaptation;
track: ITrack;
segment: ISegment;
};
}
Expand Down Expand Up @@ -773,15 +773,15 @@ export interface IRepresentationEstimatorArguments {
*/
lowLatencyMode: boolean;
/** The list of Representations `getEstimateReference` can choose from. */
representations: IReadOnlySharedReference<IRepresentation[]>;
representationList: IReadOnlySharedReference<IRepresentationListItem[]>;
/** Context for the list of Representations to choose. */
context: {
/** In which Manifest the Representations are. */
manifest: IManifest;
/** In which Period the Representations are. */
period: IPeriod;
/** In which Adaptation the Representations are. */
adaptation: IAdaptation;
/** In which track the Representations are. */
track: ITrack;
};
}

Expand All @@ -791,11 +791,11 @@ export interface IRepresentationEstimatorArguments {
*/
export type IRepresentationEstimator = (
/** Information on the content for which a Representation will be chosen */
context: { manifest: IManifest; period: IPeriod; adaptation: IAdaptation },
context: { manifest: IManifest; period: IPeriod; track: ITrack },
/** Reference emitting the Representation currently loaded. */
currentRepresentation: IReadOnlySharedReference<IRepresentation | null>,
/** Reference emitting the list of available Representations to choose from. */
representations: IReadOnlySharedReference<IRepresentation[]>,
representationList: IReadOnlySharedReference<IRepresentationListItem[]>,
/** Regularly emits playback conditions */
playbackObserver: IReadOnlyPlaybackObserver<IRepresentationEstimatorPlaybackObservation>,
/**
Expand Down Expand Up @@ -847,4 +847,14 @@ export interface IRepresentationEstimatorThrottlers {
throttleBitrate: Partial<Record<IBufferType, IReadOnlySharedReference<number>>>;
}

export interface IRepresentationListItem {
/**
* The advised minimum bandwidth estimate at which the Representation
* should be selected.
*/
bandwidth: number;
/** The Representation itself. */
representation: IRepresentation;
}

export type { IResolutionInfo };
44 changes: 25 additions & 19 deletions src/core/adaptive/guess_based_chooser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import log from "../../log";
import type { IRepresentation } from "../../manifest";
import arrayFindIndex from "../../utils/array_find_index";
import getMonotonicTimeStamp from "../../utils/monotonic_timestamp";
import type { IRepresentationListItem } from "./adaptive_representation_selector";
import { estimateRequestBandwidth } from "./network_analyzer";
import type LastEstimateStorage from "./utils/last_estimate_storage";
import { ABRAlgorithmType } from "./utils/last_estimate_storage";
Expand Down Expand Up @@ -66,7 +67,7 @@ export default class GuessBasedChooser {
* Perform a "guess", which basically indicates which Representation should be
* chosen according to the `GuessBasedChooser`.
*
* @param {Array.<Object>} representations - Array of all Representation the
* @param {Array.<Object>} representationList - Array of all Representation the
* GuessBasedChooser can choose from, sorted by bitrate ascending.
* /!\ It is very important that Representation in that Array are sorted by
* bitrate ascending for this method to work as intented.
Expand All @@ -81,7 +82,7 @@ export default class GuessBasedChooser {
* algorithm).
*/
public getGuess(
representations: IRepresentation[],
representationList: IRepresentationListItem[],
observation: {
/**
* For the concerned media buffer, difference in seconds between the next
Expand Down Expand Up @@ -123,11 +124,11 @@ export default class GuessBasedChooser {
}
if (this._canGuessHigher(bufferGap, speed, scoreData)) {
const nextRepresentation = getNextRepresentation(
representations,
representationList,
currentRepresentation,
);
if (nextRepresentation !== null) {
return nextRepresentation;
return nextRepresentation.representation;
}
}
return null;
Expand Down Expand Up @@ -156,18 +157,21 @@ export default class GuessBasedChooser {
this._consecutiveWrongGuesses++;
this._blockGuessesUntil =
getMonotonicTimeStamp() + Math.min(this._consecutiveWrongGuesses * 15000, 120000);
return getPreviousRepresentation(representations, currentRepresentation);
return (
getPreviousRepresentation(representationList, currentRepresentation)
?.representation ?? null
);
} else if (scoreData === undefined) {
return currentRepresentation;
}

if (this._canGuessHigher(bufferGap, speed, scoreData)) {
const nextRepresentation = getNextRepresentation(
representations,
representationList,
currentRepresentation,
);
if (nextRepresentation !== null) {
return nextRepresentation;
return nextRepresentation.representation;
}
}
return currentRepresentation;
Expand Down Expand Up @@ -266,29 +270,30 @@ export default class GuessBasedChooser {
*
* /!\ The representations have to be already sorted by bitrate, in ascending
* order.
* @param {Array.<Object>} representations - Available representations to choose
* @param {Array.<Object>} representationList - Available representations to choose
* from, sorted by bitrate in ascending order.
* @param {Object} currentRepresentation - The Representation currently
* considered.
* @returns {Object|null}
*/
function getNextRepresentation(
representations: IRepresentation[],
representationList: IRepresentationListItem[],
currentRepresentation: IRepresentation,
): IRepresentation | null {
const len = representations.length;
): IRepresentationListItem | null {
const len = representationList.length;
let index = arrayFindIndex(
representations,
({ id }) => id === currentRepresentation.id,
representationList,
({ representation }) => representation.id === currentRepresentation.id,
);
if (index < 0) {
log.error("ABR: Current Representation not found.");
return null;
}

while (++index < len) {
if (representations[index].bitrate > currentRepresentation.bitrate) {
return representations[index];
// XXX TODO bitrate
if (representationList[index].bandwidth > currentRepresentation.bitrate) {
return representationList[index];
}
}
return null;
Expand All @@ -303,20 +308,21 @@ function getNextRepresentation(
* @returns {Object|null}
*/
function getPreviousRepresentation(
representations: IRepresentation[],
representations: IRepresentationListItem[],
currentRepresentation: IRepresentation,
): IRepresentation | null {
): IRepresentationListItem | null {
let index = arrayFindIndex(
representations,
({ id }) => id === currentRepresentation.id,
({ representation }) => representation.id === currentRepresentation.id,
);
if (index < 0) {
log.error("ABR: Current Representation not found.");
return null;
}

while (--index >= 0) {
if (representations[index].bitrate < currentRepresentation.bitrate) {
// XXX TODO bitrate
if (representations[index].bandwidth < currentRepresentation.bitrate) {
return representations[index];
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/adaptive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
IRepresentationEstimatorCallbacks,
IRepresentationEstimatorPlaybackObservation,
IRepresentationEstimatorThrottlers as IABRThrottlers,
IRepresentationListItem,
IRequestBeginCallbackPayload,
IRequestEndCallbackPayload,
IRequestProgressCallbackPayload,
Expand All @@ -39,6 +40,7 @@ export type {
IABREstimate,
IMetricsCallbackPayload,
IRepresentationEstimatorCallbacks,
IRepresentationListItem,
IRepresentationEstimatorPlaybackObservation,
IRequestBeginCallbackPayload,
IRequestProgressCallbackPayload,
Expand Down
12 changes: 6 additions & 6 deletions src/core/adaptive/utils/filter_by_bitrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import type { IRepresentation } from "../../../manifest";
import arrayFindIndex from "../../../utils/array_find_index";
import type { IRepresentationListItem } from "../adaptive_representation_selector";

/**
* Get only representations lower or equal to a given bitrate.
Expand All @@ -26,18 +26,18 @@ import arrayFindIndex from "../../../utils/array_find_index";
* @returns {Array.<Object>}
*/
export default function filterByBitrate(
representations: IRepresentation[],
representations: IRepresentationListItem[],
bitrate: number,
): IRepresentation[] {
): IRepresentationListItem[] {
if (representations.length === 0) {
return [];
}
representations.sort((ra, rb) => ra.bitrate - rb.bitrate);
const minimumBitrate = representations[0].bitrate;
representations.sort((ra, rb) => ra.bandwidth - rb.bandwidth);
const minimumBitrate = representations[0].bandwidth;
const bitrateCeil = Math.max(bitrate, minimumBitrate);
const firstSuperiorBitrateIndex = arrayFindIndex(
representations,
(representation) => representation.bitrate > bitrateCeil,
(representation) => representation.bandwidth > bitrateCeil,
);
if (firstSuperiorBitrateIndex === -1) {
return representations; // All representations have lower bitrates.
Expand Down
Loading
Loading