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

Add isRepresentationPlayable util #1539

Open
wants to merge 2 commits 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
5 changes: 1 addition & 4 deletions src/core/cmcd/cmcd_data_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ export default class CmcdDataBuilder {
props.st = content.manifest.isDynamic ? "l" : "v";
props.tb = content.adaptation.representations.reduce(
(acc: number | undefined, representation: IRepresentation) => {
if (
representation.isSupported !== true ||
representation.decipherable === false
) {
if (representation.isPlayable() !== true) {
return acc;
}
if (acc === undefined) {
Expand Down
5 changes: 1 addition & 4 deletions src/core/stream/adaptation/adaptation_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ export default function AdaptationStream(

const initialRepIds = content.representations.getValue().representationIds;
const initialRepresentations = content.adaptation.representations.filter(
(r) =>
arrayIncludes(initialRepIds, r.id) &&
r.decipherable !== false &&
r.isSupported !== false,
(r) => arrayIncludes(initialRepIds, r.id) && r.isPlayable() !== false,
);

/** Emit the list of Representation for the adaptive logic. */
Expand Down
6 changes: 3 additions & 3 deletions src/core/stream/period/period_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ function createOrReuseSegmentSink(
* @returns {string}
*/
function getFirstDeclaredMimeType(adaptation: IAdaptation): string {
const representations = adaptation.representations.filter((r) => {
return r.isSupported === true && r.decipherable !== false;
});
const representations = adaptation.representations.filter(
(r) => r.isPlayable() !== false,
);
if (representations.length === 0) {
const noRepErr = new MediaError(
"NO_PLAYABLE_REPRESENTATION",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ export default function getAdaptationSwitchStrategy(
function hasCompatibleCodec(adaptation: IAdaptation, segmentSinkCodec: string): boolean {
return adaptation.representations.some(
(rep) =>
rep.isSupported === true &&
rep.decipherable !== false &&
rep.isPlayable() === true &&
areCodecsCompatible(rep.getMimeTypeString(), segmentSinkCodec),
);
}
9 changes: 3 additions & 6 deletions src/main_thread/tracks_store/track_dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ITrackSwitchingMode,
} from "../../core/types";
import type { IAdaptationMetadata, IRepresentationMetadata } from "../../manifest";
import { isRepresentationPlayable } from "../../manifest";
import type {
IAudioRepresentationsSwitchingMode,
IVideoRepresentationsSwitchingMode,
Expand Down Expand Up @@ -186,11 +187,7 @@ export default class TrackDispatcher extends EventEmitter<ITrackDispatcherEvent>
if (repSettings === null) {
// unlocking
playableRepresentations = trackInfo.adaptation.representations.filter(
(representation) => {
return (
representation.isSupported === true && representation.decipherable !== false
);
},
(representation) => isRepresentationPlayable(representation) === true,
);

// No need to remove the previous content when unlocking
Expand All @@ -202,7 +199,7 @@ export default class TrackDispatcher extends EventEmitter<ITrackDispatcherEvent>
arrayIncludes(representationIds, r.id),
);
playableRepresentations = representations.filter(
(r) => r.isSupported === true && r.decipherable !== false,
(representation) => isRepresentationPlayable(representation) === true,
);
if (playableRepresentations.length === 0) {
self.trigger("noPlayableLockedRepresentation", null);
Expand Down
3 changes: 2 additions & 1 deletion src/main_thread/tracks_store/tracks_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
} from "../../manifest";
import {
getSupportedAdaptations,
isRepresentationPlayable,
toAudioTrack,
toTextTrack,
toVideoTrack,
Expand Down Expand Up @@ -399,7 +400,7 @@ export default class TracksStore extends EventEmitter<ITracksStoreEvents> {
return false;
}
const playableRepresentations = adaptation.representations.filter(
(r) => r.isSupported === true && r.decipherable !== false,
(r) => isRepresentationPlayable(r) === true,
);
return playableRepresentations.length > 0;
},
Expand Down
19 changes: 18 additions & 1 deletion src/manifest/classes/representation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import log from "../../log";
import type { IRepresentationMetadata } from "../../manifest";
import { isRepresentationPlayable, type IRepresentationMetadata } from "../../manifest";
import type {
ICdnMetadata,
IContentProtections,
Expand Down Expand Up @@ -429,6 +429,23 @@ class Representation implements IRepresentationMetadata {
return true;
}

/**
* Returns `true` if the `Representation` has a high chance of being playable on
* the current device (its codec seems supported and we don't consider it to be
* un-decipherable).
*
* Returns `false` if the `Representation` has a high chance of being unplayable
* on the current device (its codec seems unsupported and/or we consider it to
* be un-decipherable).
*
* Returns `undefined` if we don't know as the codec has not been checked yet.
*
* @returns {boolean|undefined}
*/
public isPlayable(): boolean | undefined {
return isRepresentationPlayable(this);
}

/**
* Format the current `Representation`'s properties into a
* `IRepresentationMetadata` format which can better be communicated through
Expand Down
36 changes: 29 additions & 7 deletions src/manifest/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ export function toAudioTrack(
audioDescription: adaptation.isAudioDescription === true,
id: adaptation.id,
representations: (filterPlayable
? adaptation.representations.filter(
(r) => r.isSupported === true && r.decipherable !== false,
)
? adaptation.representations.filter((r) => isRepresentationPlayable(r))
: adaptation.representations
).map(toAudioRepresentation),
label: adaptation.label,
Expand Down Expand Up @@ -283,7 +281,7 @@ export function toVideoTrack(
const representations = (
filterPlayable
? trickModeAdaptation.representations.filter(
(r) => r.isSupported === true && r.decipherable !== false,
(r) => isRepresentationPlayable(r) === true,
)
: trickModeAdaptation.representations
).map(toVideoRepresentation);
Expand All @@ -302,9 +300,7 @@ export function toVideoTrack(
const videoTrack: IVideoTrack = {
id: adaptation.id,
representations: (filterPlayable
? adaptation.representations.filter(
(r) => r.isSupported === true && r.decipherable !== false,
)
? adaptation.representations.filter((r) => isRepresentationPlayable(r) === true)
: adaptation.representations
).map(toVideoRepresentation),
label: adaptation.label,
Expand Down Expand Up @@ -389,6 +385,32 @@ export function toTaggedTrack(adaptation: IAdaptation): ITaggedTrack {
}
}

/**
* Returns `true` if the `Representation` has a high chance of being playable on
* the current device (its codec seems supported and we don't consider it to be
* un-decipherable).
*
* Returns `false` if the `Representation` has a high chance of being unplayable
* on the current device (its codec seems unsupported and/or we consider it to
* be un-decipherable).
*
* Returns `undefined` if we don't know as the codec has not been checked yet.
*
* @param {Object} representation
* @returns {boolean|undefined}
*/
export function isRepresentationPlayable(
representation: IRepresentationMetadata,
): boolean | undefined {
if (representation.isSupported === undefined) {
if (representation.decipherable === false) {
return false;
}
return undefined;
}
return representation.isSupported && representation.decipherable !== false;
}

/**
* Information on a Representation affected by a `decipherabilityUpdates` event.
*/
Expand Down
Loading