Skip to content

Commit

Permalink
Merge pull request #1302 from canalplus/misc/log-abr
Browse files Browse the repository at this point in the history
Fix ABR on old Edge browser
  • Loading branch information
peaBerberian committed Oct 19, 2023
2 parents 6c00dff + 2d554bd commit ca307ac
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
14 changes: 7 additions & 7 deletions src/compat/event_listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,26 @@ function getDocumentVisibilityRef(
let prefix : string|undefined;

const doc = document as ICompatDocument;
if (doc.hidden != null) {
if (!isNullOrUndefined(doc.hidden)) {
prefix = "";
} else if (doc.mozHidden != null) {
} else if (!isNullOrUndefined(doc.mozHidden)) {
prefix = "moz";
} else if (doc.msHidden != null) {
} else if (!isNullOrUndefined(doc.msHidden)) {
prefix = "ms";
} else if (doc.webkitHidden != null) {
} else if (!isNullOrUndefined(doc.webkitHidden)) {
prefix = "webkit";
}

const hidden = isNonEmptyString(prefix) ? prefix + "Hidden" :
const hidden = isNonEmptyString(prefix) ? (prefix + "Hidden" as "hidden") :
"hidden";
const visibilityChangeEvent = isNonEmptyString(prefix) ? prefix + "visibilitychange" :
"visibilitychange";

const isHidden = document[hidden as "hidden"];
const isHidden = document[hidden];
const ref = new SharedReference(!isHidden, stopListening);

addEventListener(document, visibilityChangeEvent, () => {
const isVisible = !(document[hidden as "hidden"]);
const isVisible = !(document[hidden]);
ref.setValueIfChanged(isVisible);
}, stopListening);

Expand Down
6 changes: 3 additions & 3 deletions src/core/adaptive/__tests__/buffer_based_chooser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe("BufferBasedChooser", () => {
/* eslint-disable max-len */
it("should log an error and return the first bitrate if the given bitrate does not exist", () => {
/* eslint-enable max-len */
const logger = { debug: jest.fn(), error: jest.fn() };
const logger = { debug: jest.fn(), info: jest.fn() };
jest.mock("../../../log", () => ({ __esModule: true as const,
default: logger }));
const BufferBasedChooser = jest.requireActual("../buffer_based_chooser").default;
Expand All @@ -105,8 +105,8 @@ describe("BufferBasedChooser", () => {
currentScore: undefined,
});
expect(bbc.getLastEstimate()).toEqual(10);
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error)
expect(logger.info).toHaveBeenCalledTimes(1);
expect(logger.info)
.toHaveBeenCalledWith("ABR: Current Bitrate not found in the calculated levels");
});

Expand Down
36 changes: 27 additions & 9 deletions src/core/adaptive/adaptive_representation_selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,28 +200,46 @@ function getEstimateReference(

return { estimates: estimateRef, callbacks };

/**
* Emit the ABR estimates on various events through the returned
* `SharedReference`.
* @param {Array.<Object>} unsortedRepresentations - All `Representation` that
* the ABR logic can choose from.
* @param {Object} innerCancellationSignal - When this `CancellationSignal`
* emits, all events registered to to emit new estimates will be unregistered.
* Note that the returned reference may still be used to produce new estimates
* in the future if you want to even after this signal emits.
* @returns {Object} - `SharedReference` through which ABR estimates will be
* produced.
*/
function createEstimateReference(
representations : Representation[],
unsortedRepresentations : Representation[],
innerCancellationSignal : CancellationSignal
) : SharedReference<IABREstimate> {
if (representations.length <= 1) {
if (unsortedRepresentations.length <= 1) {
// There's only a single Representation. Just choose it.
return new SharedReference<IABREstimate>({ bitrate: undefined,
representation: representations[0],
urgent: true,
knownStableBitrate: undefined });
return new SharedReference<IABREstimate>({
bitrate: undefined,
representation: unsortedRepresentations[0],
urgent: true,
knownStableBitrate: undefined,
});
}

/** If true, Representation estimates based on the buffer health might be used. */
let allowBufferBasedEstimates = false;

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

/**
* Module calculating the optimal Representation based on the current
* buffer's health (i.e. whether enough data is buffered, history of
* buffer size etc.).
*/
const bufferBasedChooser = new BufferBasedChooser(
representations.map(r => r.bitrate)
sortedRepresentations.map(r => r.bitrate)
);

/** Store the previous estimate made here. */
Expand Down Expand Up @@ -282,7 +300,7 @@ function getEstimateReference(
const bitrateThrottle = filters.throttleBitrate.getValue();
const currentRepresentationVal = currentRepresentation.getValue();

const filteredReps = getFilteredRepresentations(representations,
const filteredReps = getFilteredRepresentations(sortedRepresentations,
resolutionLimit,
bitrateThrottle);
const requests = requestsStore.getRequests();
Expand Down Expand Up @@ -372,7 +390,7 @@ function getEstimateReference(
maximumPosition - position.last < 40)
{
chosenRepFromGuessMode = guessBasedChooser
.getGuess(representations,
.getGuess(sortedRepresentations,
lastPlaybackObservation,
currentRepresentationVal,
currentBestBitrate,
Expand Down
2 changes: 1 addition & 1 deletion src/core/adaptive/buffer_based_chooser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class BufferBasedChooser {
}

if (currentBitrateIndex < 0 || bitrates.length !== bufferLevels.length) {
log.error("ABR: Current Bitrate not found in the calculated levels");
log.info("ABR: Current Bitrate not found in the calculated levels");
this._currentEstimate = bitrates[0];
return ;
}
Expand Down
9 changes: 5 additions & 4 deletions src/core/adaptive/network_analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import config from "../../config";
import log from "../../log";
import { Representation } from "../../manifest";
import arrayFind from "../../utils/array_find";
import isNullOrUndefined from "../../utils/is_null_or_undefined";
import getMonotonicTimeStamp from "../../utils/monotonic_timestamp";
import {
IRepresentationEstimatorPlaybackObservation,
Expand Down Expand Up @@ -359,10 +360,10 @@ export default class NetworkAnalyzer {
this._lowLatencyMode,
lastEstimatedBitrate);

if (bandwidthEstimate != null) {
if (bandwidthEstimate !== undefined) {
log.info("ABR: starvation mode emergency estimate:", bandwidthEstimate);
bandwidthEstimator.reset();
newBitrateCeil = currentRepresentation == null ?
newBitrateCeil = isNullOrUndefined(currentRepresentation) ?
bandwidthEstimate :
Math.min(bandwidthEstimate, currentRepresentation.bitrate);
}
Expand All @@ -372,11 +373,11 @@ export default class NetworkAnalyzer {
if (newBitrateCeil == null) {
bandwidthEstimate = bandwidthEstimator.getEstimate();

if (bandwidthEstimate != null) {
if (bandwidthEstimate !== undefined) {
newBitrateCeil = bandwidthEstimate *
(this._inStarvationMode ? localConf.starvationBitrateFactor :
localConf.regularBitrateFactor);
} else if (lastEstimatedBitrate != null) {
} else if (lastEstimatedBitrate !== undefined) {
newBitrateCeil = lastEstimatedBitrate *
(this._inStarvationMode ? localConf.starvationBitrateFactor :
localConf.regularBitrateFactor);
Expand Down
3 changes: 2 additions & 1 deletion src/core/api/track_management/track_dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ export default class TrackDispatcher extends EventEmitter<ITrackDispatcherEvent>

// Check if Locked Representations have changed
const oldRef = reference.getValue();
const sortedReps = playableRepresentations.slice().sort();
const sortedReps = playableRepresentations.slice()
.sort((ra, rb) => ra.bitrate - rb.bitrate);
if (sortedReps.length !== oldRef.representations.length) {
reference.setValue({ representations: sortedReps, switchingMode });
return;
Expand Down

0 comments on commit ca307ac

Please sign in to comment.