Skip to content

Commit

Permalink
refactor: Implemented segments-storage interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Aug 19, 2024
1 parent 36097e3 commit 750c76c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 53 deletions.
21 changes: 15 additions & 6 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { HttpRequestExecutor } from "./http-loader.js";
import {
createSegmentInfoItem,
getStorageItemId,
SegmentsMemoryStorage,
} from "./segments-storage.js";
import { SegmentsMemoryStorage } from "./segments-storage.js";
import {
CoreEventMap,
EngineCallbacks,
Expand All @@ -26,6 +22,11 @@ import * as Utils from "./utils/utils.js";
import debug from "debug";
import { QueueItem } from "./utils/queue.js";
import { EventTarget } from "./utils/event-target.js";
import {
createSegmentDataItem,
createSegmentInfoItem,
getStorageItemId,
} from "./segments-storage/utils.js";

const FAILED_ATTEMPTS_CLEAR_INTERVAL = 60000;
const PEER_UPDATE_LATENCY = 1000;
Expand Down Expand Up @@ -223,10 +224,18 @@ export class HybridLoader {
streamId,
segment.externalId,
);
const segmentDataItem = createSegmentDataItem(
getStorageItemId(streamSwarmId, segment.externalId),
request.data,
now,
streamId,
segment.externalId,
streamSwarmId,
);

void this.segmentStorage.storeSegment(
segmentInfoItem,
request.data,
segmentDataItem,
this.streamDetails.isLive,
);
break;
Expand Down
6 changes: 2 additions & 4 deletions packages/p2p-media-loader-core/src/p2p/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import {
StreamConfig,
StreamWithSegments,
} from "../types.js";
import {
getStorageItemId,
SegmentsMemoryStorage,
} from "../segments-storage.js";
import { SegmentsMemoryStorage } from "../segments-storage.js";
import { RequestsContainer } from "../requests/request-container.js";
import { P2PTrackerClient } from "./tracker-client.js";
import * as StreamUtils from "../utils/stream.js";
import * as Utils from "../utils/utils.js";
import { EventTarget } from "../utils/event-target.js";
import { getStorageItemId } from "../segments-storage/utils.js";

export class P2PLoader {
private readonly trackerClient: P2PTrackerClient;
Expand Down
56 changes: 13 additions & 43 deletions packages/p2p-media-loader-core/src/segments-storage.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
import { CommonCoreConfig } from "./types.js";
import debug from "debug";
import { EventTarget } from "./utils/event-target.js";
import { ISegmentsStorage } from "./segments-storage/segments-storage.interface.js";
import {
SegmentDataItem,
SegmentInfoItem,
} from "./segments-storage/segments-types.js";

type StorageConfig = CommonCoreConfig;

export function getStorageItemId(streamSwarmId: string, externalId: number) {
return `${streamSwarmId}|${externalId}`;
}

export function createSegmentInfoItem(
streamSwarmId: string,
streamId: string,
externalId: number,
): SegmentInfoItem {
return { streamSwarmId, streamId, externalId };
}

type SegmentInfoItem = {
streamSwarmId: string;
streamId: string;
externalId: number;
};

type SegmentDataItem = {
storageId: string;
data: ArrayBuffer;
lastAccessed: number;
streamId: string;
externalId: number;
streamSwarmId: string;
};

type StorageEventHandlers = {
[key in `onStorageUpdated-${string}`]: () => void;
};

const DEFAULT_LIVE_CACHED_SEGMENT_EXPIRATION = 1200;

export class SegmentsMemoryStorage {
export class SegmentsMemoryStorage implements ISegmentsStorage {
private cache = new Map<string, SegmentDataItem>();
private cacheMap = new Map<string, Map<number, SegmentInfoItem>>();
private _isInitialized = false;
Expand Down Expand Up @@ -74,11 +52,11 @@ export class SegmentsMemoryStorage {

// eslint-disable-next-line @typescript-eslint/require-await
async storeSegment(
segment: SegmentInfoItem,
data: ArrayBuffer,
segmentInfoItem: SegmentInfoItem,
segmentDataItem: SegmentDataItem,
isLiveStream: boolean,
) {
const { streamId, externalId, streamSwarmId } = segment;
const { streamId, externalId, streamSwarmId } = segmentInfoItem;

if (!this.cacheMap.has(streamSwarmId)) {
this.cacheMap.set(streamSwarmId, new Map<number, SegmentInfoItem>());
Expand All @@ -88,18 +66,10 @@ export class SegmentsMemoryStorage {

if (streamCache === undefined) return;

streamCache.set(externalId, segment);

const storageId = getStorageItemId(streamSwarmId, externalId);
this.cache.set(storageId, {
storageId,
data,
lastAccessed: performance.now(),
streamId,
externalId,
streamSwarmId,
});
this.logger(`add segment: ${storageId}`);
streamCache.set(externalId, segmentInfoItem);

this.cache.set(segmentDataItem.storageId, segmentDataItem);
this.logger(`add segment: ${segmentDataItem.storageId}`);
this.dispatchStorageUpdatedEvent(streamId);
void this.clear(isLiveStream);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { SegmentDataItem, SegmentInfoItem } from "./segments-types.js";

export interface ISegmentsStorage {
readonly isInitialized: boolean;

initialize(): Promise<void>;

addIsSegmentLockedPredicate(
predicate: (segment: SegmentInfoItem) => boolean,
): void;

storeSegment(
segmentInfoItem: SegmentInfoItem,
segmentDataItem: SegmentDataItem,
isLiveStream: boolean,
): Promise<void>;

getSegmentData(segmentStorageId: string): Promise<ArrayBuffer | undefined>;

hasSegment(segmentStorageId: string): boolean;

getStoredSegmentExternalIdsOfStream(streamSwarmId: string): number[];

subscribeOnUpdate(streamId: string, listener: () => void): void;

unsubscribeFromUpdate(streamId: string, listener: () => void): void;

destroy(): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type SegmentInfoItem = {
streamSwarmId: string;
streamId: string;
externalId: number;
};

export type SegmentDataItem = {
storageId: string;
data: ArrayBuffer;
lastAccessed: number;
streamId: string;
externalId: number;
streamSwarmId: string;
};
24 changes: 24 additions & 0 deletions packages/p2p-media-loader-core/src/segments-storage/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SegmentDataItem, SegmentInfoItem } from "./segments-types.js";

export function getStorageItemId(streamSwarmId: string, externalId: number) {
return `${streamSwarmId}|${externalId}`;
}

export function createSegmentInfoItem(
streamSwarmId: string,
streamId: string,
externalId: number,
): SegmentInfoItem {
return { streamSwarmId, streamId, externalId };
}

export function createSegmentDataItem(
storageId: string,
data: ArrayBuffer,
lastAccessed: number,
streamId: string,
externalId: number,
streamSwarmId: string,
): SegmentDataItem {
return { storageId, data, lastAccessed, streamId, externalId, streamSwarmId };
}

0 comments on commit 750c76c

Please sign in to comment.