Skip to content

Commit

Permalink
refactor: Update storage usage calculation in SegmentMemoryStorage (#421
Browse files Browse the repository at this point in the history
)
  • Loading branch information
DimaDemchenko authored Sep 27, 2024
1 parent db830f1 commit 81ca8d5
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const BYTES_PER_MiB = 1048576;
export class SegmentMemoryStorage implements SegmentStorage {
private readonly userAgent = navigator.userAgent;
private segmentMemoryStorageLimit = 4 * 1024;
private currentMemoryStorageSize = 0;
private currentStorageUsage = 0;

private cache = new Map<string, SegmentDataItem>();
private readonly logger: debug.Debugger;
Expand Down Expand Up @@ -113,6 +113,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
endTime,
streamType,
});
this.increaseStorageUsage(data.byteLength);

this.logger(`add segment: ${segmentId} to ${streamId}`);

Expand All @@ -137,7 +138,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
if (!this.lastRequestedSegment || !this.currentPlayback) {
return {
totalCapacity: this.segmentMemoryStorageLimit,
usedCapacity: this.currentMemoryStorageSize,
usedCapacity: this.currentStorageUsage,
};
}
const playbackPosition = this.currentPlayback.position;
Expand Down Expand Up @@ -193,7 +194,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
);

for (const segmentData of sortedCache) {
const { streamId, segmentId } = segmentData;
const { streamId, segmentId, data } = segmentData;
const storageId = getStorageItemId(streamId, segmentId);

const shouldRemove = this.shouldRemoveSegment(
Expand All @@ -206,6 +207,8 @@ export class SegmentMemoryStorage implements SegmentStorage {

this.cache.delete(storageId);
affectedStreams.add(streamId);
this.decreaseStorageUsage(data.byteLength);

this.logger(`Removed segment ${segmentId} from stream ${streamId}`);

if (!this.isMemoryLimitReached(newSegmentSize) && !isLiveStream) break;
Expand All @@ -216,7 +219,7 @@ export class SegmentMemoryStorage implements SegmentStorage {

private isMemoryLimitReached(segmentByteLength: number) {
return (
this.currentMemoryStorageSize + segmentByteLength / BYTES_PER_MiB >
this.currentStorageUsage + segmentByteLength / BYTES_PER_MiB >
this.segmentMemoryStorageLimit
);
}
Expand Down Expand Up @@ -257,6 +260,14 @@ export class SegmentMemoryStorage implements SegmentStorage {
return true;
}

private increaseStorageUsage(segmentByteLength: number) {
this.currentStorageUsage += segmentByteLength / BYTES_PER_MiB;
}

private decreaseStorageUsage(segmentByteLength: number) {
this.currentStorageUsage -= segmentByteLength / BYTES_PER_MiB;
}

private setMemoryStorageLimit() {
if (this.coreConfig && this.coreConfig.segmentMemoryStorageLimit) {
this.segmentMemoryStorageLimit =
Expand Down

0 comments on commit 81ca8d5

Please sign in to comment.