Skip to content

Commit 531e866

Browse files
committed
refactor: Add segment categories in clear logic
This commit optimizes the segment memory storage by introducing segment storage categories. The new SegmentCategories type is added to classify segments into different categories such as obsolete, beyondHalfHttpWindowBehind, behindPlayback, and aheadHttpWindow. The segment removal logic is updated to use these categories for better organization and efficiency.
1 parent 84efce1 commit 531e866

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

packages/p2p-media-loader-core/src/segment-storage/segment-memory-storage.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ type LastRequestedSegmentInfo = {
2323
endTime: number;
2424
};
2525

26+
type SegmentCategories = {
27+
obsolete: string[];
28+
beyondHalfHttpWindowBehind: string[];
29+
behindPlayback: string[];
30+
aheadHttpWindow: string[];
31+
};
2632
const getStorageItemId = (streamId: string, segmentId: number) =>
2733
`${streamId}|${segmentId}`;
2834

@@ -236,10 +242,12 @@ export class SegmentMemoryStorage implements SegmentStorage {
236242
return [];
237243
}
238244

239-
const segmentsToRemove: string[] = [];
240-
const halfSegmentsBehindPlayback: string[] = [];
241-
const allSegmentsBehindPlayback: string[] = [];
242-
const segmentsAheadOfPlayback: string[] = [];
245+
const segmentIds: SegmentCategories = {
246+
obsolete: [],
247+
beyondHalfHttpWindowBehind: [],
248+
behindPlayback: [],
249+
aheadHttpWindow: [],
250+
};
243251

244252
const currentPlayback = this.currentPlayback.position;
245253

@@ -255,31 +263,31 @@ export class SegmentMemoryStorage implements SegmentStorage {
255263
);
256264

257265
if (isLiveStream && currentPlayback > highDemandTimeWindow + endTime) {
258-
segmentsToRemove.push(storageId);
266+
segmentIds.obsolete.push(storageId);
259267
continue;
260268
}
261269

262270
if (currentPlayback > endTime + httpDownloadTimeWindow * 0.95) {
263-
segmentsToRemove.push(storageId);
271+
segmentIds.obsolete.push(storageId);
264272
}
265273
if (currentPlayback > endTime + httpDownloadTimeWindow * 0.5) {
266-
halfSegmentsBehindPlayback.push(storageId);
274+
segmentIds.beyondHalfHttpWindowBehind.push(storageId);
267275
}
268276
if (currentPlayback > endTime) {
269-
allSegmentsBehindPlayback.push(storageId);
277+
segmentIds.behindPlayback.push(storageId);
270278
}
271279
if (endTime > currentPlayback + httpDownloadTimeWindow) {
272-
segmentsAheadOfPlayback.push(storageId);
280+
segmentIds.aheadHttpWindow.push(storageId);
273281
}
274282
}
275283

276-
if (isLiveStream) return segmentsToRemove;
277-
if (segmentsToRemove.length > 0) return segmentsToRemove;
284+
if (isLiveStream) return segmentIds.obsolete;
285+
if (segmentIds.obsolete.length > 0) return segmentIds.obsolete;
278286

279287
return firstNonEmpty(
280-
halfSegmentsBehindPlayback,
281-
allSegmentsBehindPlayback,
282-
segmentsAheadOfPlayback,
288+
segmentIds.beyondHalfHttpWindowBehind,
289+
segmentIds.behindPlayback,
290+
segmentIds.aheadHttpWindow,
283291
);
284292
}
285293

0 commit comments

Comments
 (0)