Skip to content

Commit

Permalink
refactor: Clear logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Sep 20, 2024
1 parent c504d89 commit 8a631e7
Showing 1 changed file with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type LastRequestedSegmentInfo = {
isLiveStream: boolean;
};

type ObsoleteSegments = {
live: string[];
vod: string[];
vodDifferentQuality: string[];
};

const getStorageItemId = (streamId: string, segmentId: number) =>
`${streamId}|${segmentId}`;

Expand Down Expand Up @@ -267,49 +273,43 @@ export class SegmentMemoryStorage implements SegmentStorage {
return [];
}

const obsoleteSegments: string[] = [];
const obsoleteSegments: ObsoleteSegments = {
live: [],
vod: [],
vodDifferentQuality: [],
};
const playbackPosition = this.currentPlayback.position;

for (const segmentData of this.cache.values()) {
const { streamId, segmentId } = segmentData;
const { streamId, segmentId, endTime, streamType } = segmentData;
const storageId = getStorageItemId(streamId, segmentId);

const shouldRemove = this.shouldRemoveSegment(
segmentData,
isLiveStream,
currentStreamId,
playbackPosition,
const highDemandTimeWindow = this.getStreamTimeWindow(
streamType,
"highDemandTimeWindow",
);

if (shouldRemove) obsoleteSegments.push(storageId);
}
if (playbackPosition <= endTime) continue;

return obsoleteSegments;
}
if (isLiveStream) {
if (playbackPosition > endTime + highDemandTimeWindow) {
obsoleteSegments.live.push(storageId);
}
continue;
}

private shouldRemoveSegment(
segmentData: SegmentDataItem,
isLiveStream: boolean,
currentStreamId: string,
currentPlaybackPosition: number,
): boolean {
const { streamId, endTime, streamType } = segmentData;
const highDemandTimeWindow = this.getStreamTimeWindow(
streamType,
"highDemandTimeWindow",
);
if (streamId !== currentStreamId) {
obsoleteSegments.vodDifferentQuality.push(storageId);
continue;
}

if (currentPlaybackPosition <= endTime) return false;
if (streamId !== currentStreamId) return true;
if (
isLiveStream &&
currentPlaybackPosition > highDemandTimeWindow + endTime
) {
return true;
obsoleteSegments.vod.push(storageId);
}
if (!isLiveStream) return true;

return false;
if (obsoleteSegments.live.length > 0) return obsoleteSegments.live;
if (obsoleteSegments.vodDifferentQuality.length > 0) {
return obsoleteSegments.vodDifferentQuality;
}
return obsoleteSegments.vod;
}

private setMemoryStorageLimit() {
Expand Down

0 comments on commit 8a631e7

Please sign in to comment.