Skip to content

Commit

Permalink
[POC] Update Manifest structure to make HLS implementation possible
Browse files Browse the repository at this point in the history
NOTE: This is just a background, low-priority, Proof-Of-Concept.
It is not currently functional and is in the middle of its
implementation, there's also no HLS code for now, only HLS-related
ideas.

Overview
========

This PR is a Proof-Of-Concept where I explore whether HLS playback can
be natively implemented in the RxPlayer without sacrificing too much the
RxPlayer code's readability. This is not the first attempt, but the
approach here is different than previous ones.

One of the core idea is to keep the RxPlayer API as is, and thus to hide
all HLS differences in the RxPlayer code.

We already thought of potential HLS compatibility when designing our v4, so
there seem to be nothing in our API incompatible with HLS concepts.
Also our core code is for the most part protocol-agnostic, so we presumably
could keep, and profit from, most of our logic while playing HLS streams.

However, this attempt completely changes our internal `Manifest` concept,
especially the `Adaptation` subpart of it (which was equivalent to the
concept of a "track").

Manifest hierarchy change
=========================

The issue
---------

One of the main differences between HLS and other protocols handled by the
RxPlayer (such as DASH) is the concept of "variant streams" only HLS have.

Basically in DASH, you first select your wanted audio + video + text tracks
and then select a Representation (i.e. quality) according to your current
playback conditions such as your network bandwidth.

In HLS however, this is reversed. You first have to select your "variant
stream" based on your playback conditions (mainly: network bandwidth) and
then see which "media" - including our notion of a track - is available for
that variant stream.

Because of edge cases, we cannot translate the HLS model into our DASH-like
model wihout losing some HLS features.
Doing the reverse (adapting DASH model into HLS model, which seems to be
what e.g. the shaka-player more-or-less did) could work yet it doesn't seem
like an optimal solution (e.g. we would either lose some bandwidth
information or creating a huge number of variant streams for all potential
combinations).

How I tried to fix it
---------------------

So what I did here, was to change the Manifest object's hierarchy so it
can adapt to both how HLS is treating thing and how DASH is treating
things.

The Manifest looked like this before this commit:

```ts
type Manifest = {
  periods: Array<{
    // Define a particular track
    adaptations: Record<
      "audio" | "video" | "text",
      Array<{
        // qualities
        representations: Array<{
          // ... That Representation's metadata
        }>;
        // ... The rest of that track's metadata
      }>
    >;
    // ... The rest of that Period's metadata
  }>;
  // ... The rest of the Manifest's metadata
};
```

Now it looks like:

```ts
type Manifest = {
  periods: Array<{
    tracksMetadata: Record<
      "audio" | "video" | "text",
      Record<
        string, // The track's id
        {
          // All qualities linked to that track
          representations: Record<
            string, // That Representation's id
            {
              // ... That Representation's metadata
            }
          >;
          // ... The rest of that track's metadata
        }
      >
    >;

    // Groups of available tracks and qualities combinations
    variantStreams: Array<{
      id: string;
      // bandwidth for that variant, only defined for HLS, others have
      // only a single variant
      bandwidth: number | undefined;
      // Authorized tracks + qualities combinations in that variantStream
      media: Record<
        "audio" | "video" | "text",
        Array<{
          // `id` the corresponding track in `tracksMetadata`
          linkedTrackId: string;
          // `id` list of the Representations' in `tracksMetadata`
          representations: string[];
        }>
      >;
    }>;
    // ... The rest of that Period's metadata
  }>;
  // ... The rest of the Manifest's metadata
};
```

So basically in a Period, we'll now separate a tracks' metadata and the conditions in
which we may play them: the former rely on `tracksMetadata`, the latter on
`variantStreams`.

Note that `variantStreams` only use `id`s to refer to tracks and Representations: it does
not contain the metadata directly. This is to avoid having to repeat a track's and
representation's metadata already present in the `tracksMetadata`. We cannot just rely on
a same-object-reference trick because the Manifest object has to be able to be transmitted
through the worker and main tread. Worker compatibility is also the main reason why we're
not relyng on `Map` objects to link ids and metadata, though it may seem more logical.

This new structure allows to enforce complex HLS features, like restrictions on which
tracks and qualities can be played when another unrelated track is selected. Here
incompatible tracks would never be present the same variantStream.

Though it is very clear that it adds net complexity on top of our Manifest's structure, as
those "variant streams" concept do not exist in Smooth or DASH. For Smooth and DASH, only
a single `variantStream` will be present, with a `bandwidth` set to `undefined`
(Representation-level `bandwidth` is still exploited by our ABR logic) and containing all
tracks declared in `tracksMetadata`.
  • Loading branch information
peaBerberian committed Aug 29, 2024
1 parent e9f9758 commit 9c77ee0
Show file tree
Hide file tree
Showing 80 changed files with 1,885 additions and 1,789 deletions.
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

0 comments on commit 9c77ee0

Please sign in to comment.