Skip to content

Commit

Permalink
Rename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlika committed Jan 10, 2024
1 parent d1ff740 commit f2bf765
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions packages/p2p-media-loader-core/src/bandwidth-calculator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class BandwidthCalculator {
private simultaneousLoadingsCount = 0;
private readonly bytes: number[] = [];
private readonly shiftedTimestamps: number[] = [];
private readonly loadingOnlyTimestamps: number[] = [];
private readonly timestamps: number[] = [];
private noLoadingsTotalTime = 0;
private allLoadingsStoppedTimestamp = 0;
Expand All @@ -10,7 +10,7 @@ export class BandwidthCalculator {

addBytes(bytesLength: number, now = performance.now()) {
this.bytes.push(bytesLength);
this.shiftedTimestamps.push(now - this.noLoadingsTotalTime);
this.loadingOnlyTimestamps.push(now - this.noLoadingsTotalTime);
this.timestamps.push(now);
}

Expand All @@ -30,20 +30,20 @@ export class BandwidthCalculator {
this.allLoadingsStoppedTimestamp = now;
}

getBandwidthForLastNSplicedSeconds(
getBandwidthLoadingOnly(
seconds: number,
ignoreThresholdTimestamp = Number.NEGATIVE_INFINITY
) {
if (!this.shiftedTimestamps.length) return 0;
if (!this.loadingOnlyTimestamps.length) return 0;
const milliseconds = seconds * 1000;
const lastItemTimestamp =
this.shiftedTimestamps[this.shiftedTimestamps.length - 1];
this.loadingOnlyTimestamps[this.loadingOnlyTimestamps.length - 1];
let lastCountedTimestamp = lastItemTimestamp;
const threshold = lastItemTimestamp - milliseconds;
let totalBytes = 0;

for (let i = this.bytes.length - 1; i >= 0; i--) {
const timestamp = this.shiftedTimestamps[i];
const timestamp = this.loadingOnlyTimestamps[i];
if (
timestamp < threshold ||
this.timestamps[i] < ignoreThresholdTimestamp
Expand All @@ -57,7 +57,7 @@ export class BandwidthCalculator {
return (totalBytes * 8000) / (lastItemTimestamp - lastCountedTimestamp);
}

getBandwidthForLastNSeconds(
getBandwidth(
seconds: number,
ignoreThresholdTimestamp = Number.NEGATIVE_INFINITY,
now = performance.now()
Expand All @@ -79,19 +79,19 @@ export class BandwidthCalculator {
}

clearStale() {
if (!this.shiftedTimestamps.length) return;
if (!this.loadingOnlyTimestamps.length) return;
const threshold =
this.shiftedTimestamps[this.shiftedTimestamps.length - 1] -
this.loadingOnlyTimestamps[this.loadingOnlyTimestamps.length - 1] -
this.clearThresholdMs;

let samplesToRemove = 0;
for (const timestamp of this.shiftedTimestamps) {
for (const timestamp of this.loadingOnlyTimestamps) {
if (timestamp > threshold) break;
samplesToRemove++;
}

this.bytes.splice(0, samplesToRemove);
this.shiftedTimestamps.splice(0, samplesToRemove);
this.loadingOnlyTimestamps.splice(0, samplesToRemove);
this.timestamps.splice(0, samplesToRemove);
}
}
22 changes: 11 additions & 11 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class HybridLoader {
if (data) {
engineRequest.resolve(
data,
this.bandwidthCalculators.all.getBandwidthForLastNSplicedSeconds(3)
this.bandwidthCalculators.all.getBandwidthLoadingOnly(3)
);
}
} else {
Expand Down Expand Up @@ -439,26 +439,26 @@ export class HybridLoader {
const { http, all } = this.bandwidthCalculators;
const { activeLevelBitrate } = this.streamDetails;
if (this.streamDetails.activeLevelBitrate === 0) {
return all.getBandwidthForLastNSplicedSeconds(3);
return all.getBandwidthLoadingOnly(3);
}
const { levelChangedTimestamp } = this;

const bandwidth = Math.max(
all.getBandwidthForLastNSeconds(30, levelChangedTimestamp),
all.getBandwidthForLastNSeconds(60, levelChangedTimestamp),
all.getBandwidthForLastNSeconds(90, levelChangedTimestamp)
all.getBandwidth(30, levelChangedTimestamp),
all.getBandwidth(60, levelChangedTimestamp),
all.getBandwidth(90, levelChangedTimestamp)
);
if (loadedPercentOfQueue >= 80 || bandwidth >= activeLevelBitrate * 0.9) {
return Math.max(
all.getBandwidthForLastNSplicedSeconds(1),
all.getBandwidthForLastNSplicedSeconds(3),
all.getBandwidthForLastNSplicedSeconds(5)
all.getBandwidthLoadingOnly(1),
all.getBandwidthLoadingOnly(3),
all.getBandwidthLoadingOnly(5)
);
}
const httpRealBandwidth = Math.max(
http.getBandwidthForLastNSplicedSeconds(1),
http.getBandwidthForLastNSplicedSeconds(3),
http.getBandwidthForLastNSplicedSeconds(5)
http.getBandwidthLoadingOnly(1),
http.getBandwidthLoadingOnly(3),
http.getBandwidthLoadingOnly(5)
);
return Math.max(bandwidth, httpRealBandwidth);
}
Expand Down

0 comments on commit f2bf765

Please sign in to comment.