Skip to content

Commit

Permalink
Add isRepresentationPlayable util
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Sep 10, 2024
1 parent a50ca52 commit 7bf784b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 52 deletions.
7 changes: 2 additions & 5 deletions src/core/cmcd/cmcd_data_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IRepresentation,
ISegment,
} from "../../manifest";
import { isRepresentationPlayable } from "../../manifest";
import type {
IReadOnlyPlaybackObserver,
IRebufferingStatus,
Expand Down Expand Up @@ -333,11 +334,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.isCodecSupported !== true ||
representation.isResolutionSupported === false ||
representation.decipherable === false
) {
if (isRepresentationPlayable(representation) !== true) {
return acc;
}
if (acc === undefined) {
Expand Down
7 changes: 2 additions & 5 deletions src/core/stream/adaptation/adaptation_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import config from "../../../config";
import { formatError } from "../../../errors";
import log from "../../../log";
import type { IRepresentation } from "../../../manifest";
import { isRepresentationPlayable } from "../../../manifest";
import arrayIncludes from "../../../utils/array_includes";
import { assertUnreachable } from "../../../utils/assert";
import cancellableSleep from "../../../utils/cancellable_sleep";
Expand Down Expand Up @@ -95,11 +96,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.isCodecSupported !== false &&
r.isResolutionSupported !== false,
(r) => arrayIncludes(initialRepIds, r.id) && isRepresentationPlayable(r) !== false,
);

/** Emit the list of Representation for the adaptive logic. */
Expand Down
12 changes: 4 additions & 8 deletions src/core/stream/period/period_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import config from "../../../config";
import { formatError, MediaError } from "../../../errors";
import log from "../../../log";
import type { IAdaptation, IPeriod } from "../../../manifest";
import { toTaggedTrack } from "../../../manifest";
import { isRepresentationPlayable, toTaggedTrack } from "../../../manifest";
import type { IReadOnlyPlaybackObserver } from "../../../playback_observer";
import type { ITrackType } from "../../../public_types";
import arrayFind from "../../../utils/array_find";
Expand Down Expand Up @@ -472,13 +472,9 @@ function createOrReuseSegmentSink(
* @returns {string}
*/
function getFirstDeclaredMimeType(adaptation: IAdaptation): string {
const representations = adaptation.representations.filter((r) => {
return (
r.isCodecSupported === true &&
r.decipherable !== false &&
r.isResolutionSupported !== false
);
});
const representations = adaptation.representations.filter(
(r) => isRepresentationPlayable(r) !== 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 @@ -16,6 +16,7 @@

import config from "../../../../config";
import type { IAdaptation, IPeriod } from "../../../../manifest";
import { isRepresentationPlayable } from "../../../../manifest";
import type { IReadOnlyPlaybackObserver } from "../../../../playback_observer";
import areCodecsCompatible from "../../../../utils/are_codecs_compatible";
import type { IRange } from "../../../../utils/ranges";
Expand Down Expand Up @@ -189,9 +190,7 @@ export default function getAdaptationSwitchStrategy(
function hasCompatibleCodec(adaptation: IAdaptation, segmentSinkCodec: string): boolean {
return adaptation.representations.some(
(rep) =>
rep.isCodecSupported === true &&
rep.decipherable !== false &&
rep.isResolutionSupported !== false &&
isRepresentationPlayable(rep) === true &&
areCodecsCompatible(rep.getMimeTypeString(), segmentSinkCodec),
);
}
14 changes: 3 additions & 11 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,13 +187,7 @@ export default class TrackDispatcher extends EventEmitter<ITrackDispatcherEvent>
if (repSettings === null) {
// unlocking
playableRepresentations = trackInfo.adaptation.representations.filter(
(representation) => {
return (
representation.isCodecSupported === true &&
representation.decipherable !== false &&
representation.isResolutionSupported !== false
);
},
(representation) => isRepresentationPlayable(representation) === true,
);

// No need to remove the previous content when unlocking
Expand All @@ -204,10 +199,7 @@ export default class TrackDispatcher extends EventEmitter<ITrackDispatcherEvent>
arrayIncludes(representationIds, r.id),
);
playableRepresentations = representations.filter(
(r) =>
r.isCodecSupported === true &&
r.decipherable !== false &&
r.isResolutionSupported !== false,
(representation) => isRepresentationPlayable(representation) === true,
);
if (playableRepresentations.length === 0) {
self.trigger("noPlayableLockedRepresentation", null);
Expand Down
6 changes: 2 additions & 4 deletions 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,10 +400,7 @@ export default class TracksStore extends EventEmitter<ITracksStoreEvents> {
return false;
}
const playableRepresentations = adaptation.representations.filter(
(r) =>
r.isCodecSupported === true &&
r.decipherable !== false &&
r.isResolutionSupported !== false,
(r) => isRepresentationPlayable(r) === true,
);
return playableRepresentations.length > 0;
},
Expand Down
32 changes: 16 additions & 16 deletions src/manifest/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,7 @@ export function toAudioTrack(
audioDescription: adaptation.isAudioDescription === true,
id: adaptation.id,
representations: (filterPlayable
? adaptation.representations.filter(
(r) =>
r.isCodecSupported === true &&
r.decipherable !== false &&
r.isResolutionSupported !== false,
)
? adaptation.representations.filter((r) => isRepresentationPlayable(r))
: adaptation.representations
).map(toAudioRepresentation),
label: adaptation.label,
Expand Down Expand Up @@ -286,10 +281,7 @@ export function toVideoTrack(
const representations = (
filterPlayable
? trickModeAdaptation.representations.filter(
(r) =>
r.isCodecSupported === true &&
r.decipherable !== false &&
r.isResolutionSupported !== false,
(r) => isRepresentationPlayable(r) === true,
)
: trickModeAdaptation.representations
).map(toVideoRepresentation);
Expand All @@ -308,12 +300,7 @@ export function toVideoTrack(
const videoTrack: IVideoTrack = {
id: adaptation.id,
representations: (filterPlayable
? adaptation.representations.filter(
(r) =>
r.isCodecSupported === true &&
r.decipherable !== false &&
r.isResolutionSupported !== false,
)
? adaptation.representations.filter((r) => isRepresentationPlayable(r) === true)
: adaptation.representations
).map(toVideoRepresentation),
label: adaptation.label,
Expand Down Expand Up @@ -400,6 +387,19 @@ export function toTaggedTrack(adaptation: IAdaptation): ITaggedTrack {
}
}

export function isRepresentationPlayable(
representation: IRepresentationMetadata,
): boolean | undefined {
if (representation.isCodecSupported === undefined) {
return undefined;
}
return (
representation.isCodecSupported &&
representation.isResolutionSupported !== false &&
representation.decipherable !== false
);
}

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

0 comments on commit 7bf784b

Please sign in to comment.