Skip to content

Commit

Permalink
refactor: Naming
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Sep 24, 2024
1 parent e3588dd commit 074cfe4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { SegmentStorage } from "./segment-storage/index.js";
export class Core<TStream extends Stream = Stream> {
/** Default configuration for common core settings. */
static readonly DEFAULT_COMMON_CORE_CONFIG: CommonCoreConfig = {
segmentsMemoryStorageLimit: undefined,
segmentMemoryStorageLimit: undefined,
customSegmentStorageFactory: undefined,
};

Expand Down
18 changes: 10 additions & 8 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ export class HybridLoader {
}

private loadRandomThroughHttp() {
const availableMemoryPercent = this.getAvailableMemoryPercent();
if (availableMemoryPercent <= 10) return;
const availableStorageCapacityPercent =
this.getAvailableStorageCapacityPercent();
if (availableStorageCapacityPercent <= 10) return;

const { simultaneousHttpDownloads, httpErrorRetries } = this.config;
const p2pLoader = this.p2pLoaders.currentLoader;
Expand All @@ -399,7 +400,7 @@ export class HybridLoader {
this.playback,
this.config,
this.p2pLoaders.currentLoader,
availableMemoryPercent,
availableStorageCapacityPercent,
)) {
const swarmId = this.config.swarmId ?? this.streamManifestUrl;
const streamSwarmId = StreamUtils.getStreamSwarmId(
Expand Down Expand Up @@ -494,9 +495,9 @@ export class HybridLoader {
return false;
}

private getAvailableMemoryPercent(): number {
const { memoryLimit, memoryUsed } = this.segmentStorage.getUsedMemory();
return 100 - (memoryUsed / memoryLimit) * 100;
private getAvailableStorageCapacityPercent(): number {
const { totalCapacity, usedCapacity } = this.segmentStorage.getUsage();
return 100 - (usedCapacity / totalCapacity) * 100;
}

private generateQueue() {
Expand All @@ -505,13 +506,14 @@ export class HybridLoader {
let maxPossibleLength = 0;
let alreadyLoadedCount = 0;

const availableMemoryPercent = this.getAvailableMemoryPercent();
const availableStorageCapacityPercent =
this.getAvailableStorageCapacityPercent();
for (const item of QueueUtils.generateQueue(
this.lastRequestedSegment,
this.playback,
this.config,
this.p2pLoaders.currentLoader,
availableMemoryPercent,
availableStorageCapacityPercent,
)) {
maxPossibleLength++;
const { segment } = item;
Expand Down
6 changes: 3 additions & 3 deletions packages/p2p-media-loader-core/src/segment-storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ export interface SegmentStorage {
/**
* Returns used memory information in the storage
*/
getUsedMemory(): {
memoryLimit: number;
memoryUsed: number;
getUsage(): {
totalCapacity: number;
usedCapacity: number;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const BYTES_PER_MB = 1048576;

export class SegmentMemoryStorage implements SegmentStorage {
private readonly userAgent = navigator.userAgent;
private segmentsMemoryStorageLimit = 4000;
private segmentMemoryStorageLimit = 4000;
private currentMemoryStorageSize = 0;

private cache = new Map<string, SegmentDataItem>();
Expand Down Expand Up @@ -134,11 +134,11 @@ export class SegmentMemoryStorage implements SegmentStorage {
return dataItem.data;
}

getUsedMemory() {
getUsage() {
if (!this.lastRequestedSegment || !this.currentPlayback) {
return {
memoryLimit: this.segmentsMemoryStorageLimit,
memoryUsed: this.currentMemoryStorageSize,
totalCapacity: this.segmentMemoryStorageLimit,
usedCapacity: this.currentMemoryStorageSize,
};
}
const playbackPosition = this.currentPlayback.position;
Expand All @@ -155,8 +155,8 @@ export class SegmentMemoryStorage implements SegmentStorage {
const usedMemoryInMB = this.currentMemoryStorageSize - potentialFreeSpace;

return {
memoryLimit: this.segmentsMemoryStorageLimit,
memoryUsed: usedMemoryInMB,
totalCapacity: this.segmentMemoryStorageLimit,
usedCapacity: usedMemoryInMB,
};
}

Expand Down Expand Up @@ -224,7 +224,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
private isMemoryLimitReached(segmentByteLength: number) {
return (
this.currentMemoryStorageSize + segmentByteLength / BYTES_PER_MB >
this.segmentsMemoryStorageLimit
this.segmentMemoryStorageLimit
);
}

Expand Down Expand Up @@ -273,16 +273,16 @@ export class SegmentMemoryStorage implements SegmentStorage {
}

private setMemoryStorageLimit() {
if (this.coreConfig && this.coreConfig.segmentsMemoryStorageLimit) {
this.segmentsMemoryStorageLimit =
this.coreConfig.segmentsMemoryStorageLimit;
if (this.coreConfig && this.coreConfig.segmentMemoryStorageLimit) {
this.segmentMemoryStorageLimit =
this.coreConfig.segmentMemoryStorageLimit;
return;
}

if (isAndroidWebview(this.userAgent) || isIPadOrIPhone(this.userAgent)) {
this.segmentsMemoryStorageLimit = 1000;
this.segmentMemoryStorageLimit = 1000;
} else if (isAndroid(this.userAgent)) {
this.segmentsMemoryStorageLimit = 2000;
this.segmentMemoryStorageLimit = 2000;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/p2p-media-loader-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export type CommonCoreConfig = {
*
* @default
* ```typescript
* segmentsMemoryStorageLimit: undefined
* segmentMemoryStorageLimit: undefined
* ```
*
* - When `undefined`, the default limit is determined based on the device type and browser:
Expand All @@ -130,7 +130,7 @@ export type CommonCoreConfig = {
* - iOS WebView: 1000 MB
*
*/
segmentsMemoryStorageLimit: number | undefined;
segmentMemoryStorageLimit: number | undefined;

/**
* Optional custom storage factory for the segments storage.
Expand Down

0 comments on commit 074cfe4

Please sign in to comment.