Skip to content

Commit

Permalink
refactor: segments-storage-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Aug 23, 2024
1 parent 66452fd commit d8b9a2e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { ISegmentsStorage } from "./segments-storage/segments-storage.interface.
export class Core<TStream extends Stream = Stream> {
/** Default configuration for common core settings. */
static readonly DEFAULT_COMMON_CORE_CONFIG: CommonCoreConfig = {
cachedSegmentExpiration: undefined,
cachedSegmentsCount: 0,
customSegmentStorage: undefined,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class HybridLoader {
if (!this.segmentStorage.isInitialized()) {
throw new Error("Segment storage is not initialized.");
}
this.segmentStorage.setEngineRequestSegmentDurationCallback(() => {
this.segmentStorage.setLastRequestedSegmentDurationCallback(() => {
return {
startTime: this.lastRequestedSegment.startTime,
endTime: this.lastRequestedSegment.endTime,
Expand Down
13 changes: 8 additions & 5 deletions packages/p2p-media-loader-core/src/segments-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export class SegmentsMemoryStorage implements ISegmentsStorage {
private mainStreamConfig?: StreamConfig;
private secondaryStreamConfig?: StreamConfig;
private getCurrentPlaybackTime?: () => number;
private getSegmentDuration?: () => { startTime: number; endTime: number };
private getLastRequestedSegmentDuration?: () => {
startTime: number;
endTime: number;
};

constructor() {
this.logger = debug("p2pml-core:segment-memory-storage");
Expand Down Expand Up @@ -62,13 +65,13 @@ export class SegmentsMemoryStorage implements ISegmentsStorage {
this.getCurrentPlaybackTime = getCurrentPlaybackTime;
}

setEngineRequestSegmentDurationCallback(
setLastRequestedSegmentDurationCallback(
getSegmentDurationFromEngineRequest: () => {
startTime: number;
endTime: number;
},
) {
this.getSegmentDuration = getSegmentDurationFromEngineRequest;
this.getLastRequestedSegmentDuration = getSegmentDurationFromEngineRequest;
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down Expand Up @@ -200,7 +203,7 @@ export class SegmentsMemoryStorage implements ISegmentsStorage {
private getStorageMaxCacheCount() {
if (
!this.storageConfig ||
!this.getSegmentDuration ||
!this.getLastRequestedSegmentDuration ||
!this.mainStreamConfig ||
!this.secondaryStreamConfig
) {
Expand All @@ -216,7 +219,7 @@ export class SegmentsMemoryStorage implements ISegmentsStorage {
? this.mainStreamConfig.httpDownloadTimeWindow
: this.secondaryStreamConfig.httpDownloadTimeWindow;

const { startTime, endTime } = this.getSegmentDuration();
const { startTime, endTime } = this.getLastRequestedSegmentDuration();
const segmentDuration = endTime - startTime;
const segmentsInTimeWindow = Math.ceil(maxHttpTimeWindow / segmentDuration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export interface ISegmentsStorage {

setSegmentPlaybackCallback(getCurrentPlaybackTime: () => number): void;

setEngineRequestSegmentDurationCallback(
getSegmentDurationFromEngineRequest: () => {
setLastRequestedSegmentDurationCallback(
getLastRequestedSegmentDuration: () => {
startTime: number;
endTime: number;
},
Expand Down
10 changes: 0 additions & 10 deletions packages/p2p-media-loader-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,6 @@ export type DynamicCoreConfig = Partial<

/** Represents the configuration for the Core functionality that is common to all streams. */
export type CommonCoreConfig = {
/**
* Time after which a cached segment expires, in seconds.
* If set to undefined, the cacheSegmentExpiration is disabled for VOD streams, and a default value (20 minutes) is used for live streams.
*
* @default
* ```typescript
* cachedSegmentExpiration: undefined
* ```
*/
cachedSegmentExpiration?: number;
/**
* Maximum number of segments to store in the cache.
* Has to be less then httpDownloadTimeWindow and p2pDownloadTimeWindow.
Expand Down

0 comments on commit d8b9a2e

Please sign in to comment.