diff --git a/packages/p2p-media-loader-core/src/core.ts b/packages/p2p-media-loader-core/src/core.ts index 615c9190..54aad8a8 100644 --- a/packages/p2p-media-loader-core/src/core.ts +++ b/packages/p2p-media-loader-core/src/core.ts @@ -30,7 +30,7 @@ import { SegmentStorage } from "./segment-storage/index.js"; export class Core { /** Default configuration for common core settings. */ static readonly DEFAULT_COMMON_CORE_CONFIG: CommonCoreConfig = { - segmentsMemoryStorageLimit: undefined, + segmentMemoryStorageLimit: undefined, customSegmentStorageFactory: undefined, }; diff --git a/packages/p2p-media-loader-core/src/hybrid-loader.ts b/packages/p2p-media-loader-core/src/hybrid-loader.ts index 2fea64f6..7a7c1e29 100644 --- a/packages/p2p-media-loader-core/src/hybrid-loader.ts +++ b/packages/p2p-media-loader-core/src/hybrid-loader.ts @@ -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; @@ -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( @@ -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() { @@ -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; diff --git a/packages/p2p-media-loader-core/src/segment-storage/index.ts b/packages/p2p-media-loader-core/src/segment-storage/index.ts index 68f6dbce..00cf19ce 100644 --- a/packages/p2p-media-loader-core/src/segment-storage/index.ts +++ b/packages/p2p-media-loader-core/src/segment-storage/index.ts @@ -77,9 +77,9 @@ export interface SegmentStorage { /** * Returns used memory information in the storage */ - getUsedMemory(): { - memoryLimit: number; - memoryUsed: number; + getUsage(): { + totalCapacity: number; + usedCapacity: number; }; /** diff --git a/packages/p2p-media-loader-core/src/segment-storage/segment-memory-storage.ts b/packages/p2p-media-loader-core/src/segment-storage/segment-memory-storage.ts index cd4fbf05..8fc2810d 100644 --- a/packages/p2p-media-loader-core/src/segment-storage/segment-memory-storage.ts +++ b/packages/p2p-media-loader-core/src/segment-storage/segment-memory-storage.ts @@ -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(); @@ -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; @@ -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, }; } @@ -224,7 +224,7 @@ export class SegmentMemoryStorage implements SegmentStorage { private isMemoryLimitReached(segmentByteLength: number) { return ( this.currentMemoryStorageSize + segmentByteLength / BYTES_PER_MB > - this.segmentsMemoryStorageLimit + this.segmentMemoryStorageLimit ); } @@ -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; } } diff --git a/packages/p2p-media-loader-core/src/types.ts b/packages/p2p-media-loader-core/src/types.ts index 6e43d89c..774e8218 100644 --- a/packages/p2p-media-loader-core/src/types.ts +++ b/packages/p2p-media-loader-core/src/types.ts @@ -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: @@ -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.