Skip to content

Commit e3588dd

Browse files
committed
refactor: Improve segment memory storage
1 parent 2a60c3d commit e3588dd

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

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

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import { CommonCoreConfig, StreamConfig, StreamType } from "../types.js";
22
import debug from "debug";
33
import { SegmentStorage } from "./index.js";
4+
import {
5+
isAndroid,
6+
isIPadOrIPhone,
7+
isAndroidWebview,
8+
getStorageItemId,
9+
} from "./utils.js";
410

511
type SegmentDataItem = {
612
segmentId: number;
713
streamId: string;
814
data: ArrayBuffer;
915
startTime: number;
1016
endTime: number;
11-
streamType: string;
17+
streamType: StreamType;
1218
};
1319

1420
type Playback = {
@@ -26,16 +32,6 @@ type LastRequestedSegmentInfo = {
2632
isLiveStream: boolean;
2733
};
2834

29-
const getStorageItemId = (streamId: string, segmentId: number) =>
30-
`${streamId}|${segmentId}`;
31-
32-
const isAndroid = (ua: string) => /Android/i.test(ua);
33-
34-
const isIPadOrIPhone = (ua: string) => /iPad|iPhone/i.test(ua);
35-
36-
const isAndroidWebview = (ua: string) =>
37-
/Android/i.test(ua) && !/Chrome|Firefox/i.test(ua);
38-
3935
const BYTES_PER_MB = 1048576;
4036

4137
export class SegmentMemoryStorage implements SegmentStorage {
@@ -117,7 +113,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
117113
endTime,
118114
streamType,
119115
});
120-
this.updateMemoryStorageSize(data.byteLength, true);
116+
this.increaseMemoryStorageSize(data.byteLength);
121117

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

@@ -145,19 +141,18 @@ export class SegmentMemoryStorage implements SegmentStorage {
145141
memoryUsed: this.currentMemoryStorageSize,
146142
};
147143
}
148-
const PlaybackPosition = this.currentPlayback.position;
144+
const playbackPosition = this.currentPlayback.position;
149145

150146
let potentialFreeSpace = 0;
151147
for (const segmentData of this.cache.values()) {
152148
const { endTime } = segmentData;
153149

154-
if (PlaybackPosition <= endTime) continue;
150+
if (playbackPosition <= endTime) continue;
155151

156152
potentialFreeSpace += segmentData.data.byteLength / BYTES_PER_MB;
157153
}
158154

159-
const usedMemoryInMB =
160-
this.currentMemoryStorageSize - potentialFreeSpace / BYTES_PER_MB;
155+
const usedMemoryInMB = this.currentMemoryStorageSize - potentialFreeSpace;
161156

162157
return {
163158
memoryLimit: this.segmentsMemoryStorageLimit,
@@ -215,8 +210,8 @@ export class SegmentMemoryStorage implements SegmentStorage {
215210

216211
if (!shouldRemove) continue;
217212

213+
this.decreaseMemoryStorageSize(segmentData.data.byteLength);
218214
this.cache.delete(storageId);
219-
this.updateMemoryStorageSize(segmentData.data.byteLength);
220215
affectedStreams.add(streamId);
221216
this.logger(`Removed segment ${segmentId} from stream ${streamId}`);
222217

@@ -249,17 +244,12 @@ export class SegmentMemoryStorage implements SegmentStorage {
249244
});
250245
}
251246

252-
private updateMemoryStorageSize(
253-
byteLength: number,
254-
isAddition: boolean = false,
255-
): void {
256-
const changeInMB = byteLength / BYTES_PER_MB;
247+
private increaseMemoryStorageSize(byteLength: number) {
248+
this.currentMemoryStorageSize += byteLength / BYTES_PER_MB;
249+
}
257250

258-
if (isAddition) {
259-
this.currentMemoryStorageSize += changeInMB;
260-
} else {
261-
this.currentMemoryStorageSize -= changeInMB;
262-
}
251+
private decreaseMemoryStorageSize(byteLength: number) {
252+
this.currentMemoryStorageSize -= byteLength / BYTES_PER_MB;
263253
}
264254

265255
private shouldRemoveSegment(
@@ -276,10 +266,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
276266
if (currentPlaybackPosition <= endTime) return false;
277267

278268
if (isLiveStream) {
279-
if (currentPlaybackPosition > highDemandTimeWindow + endTime) {
280-
return true;
281-
}
282-
return false;
269+
return currentPlaybackPosition > highDemandTimeWindow + endTime;
283270
}
284271

285272
return true;
@@ -303,15 +290,12 @@ export class SegmentMemoryStorage implements SegmentStorage {
303290
streamType: string,
304291
configKey: "highDemandTimeWindow" | "httpDownloadTimeWindow",
305292
): number {
306-
if (!this.mainStreamConfig || !this.secondaryStreamConfig) {
307-
return 0;
308-
}
309-
310293
const config =
311294
streamType === "main"
312295
? this.mainStreamConfig
313296
: this.secondaryStreamConfig;
314-
return config[configKey];
297+
298+
return config?.[configKey] ?? 0;
315299
}
316300

317301
public destroy() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const getStorageItemId = (streamId: string, segmentId: number) =>
2+
`${streamId}|${segmentId}`;
3+
4+
export const isAndroid = (userAgent: string) => /Android/i.test(userAgent);
5+
6+
export const isIPadOrIPhone = (userAgent: string) =>
7+
/iPad|iPhone/i.test(userAgent);
8+
9+
export const isAndroidWebview = (userAgent: string) =>
10+
/Android/i.test(userAgent) && !/Chrome|Firefox/i.test(userAgent);

0 commit comments

Comments
 (0)