Skip to content

Commit

Permalink
Move utils to separate folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Sep 28, 2023
1 parent 978d95d commit 9671b0c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 64 deletions.
2 changes: 1 addition & 1 deletion packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HybridLoader } from "./hybrid-loader";
import { Stream, StreamWithSegments, Segment, Settings } from "./types";
import * as Utils from "./utils";
import * as Utils from "./utils/utils";
import { LinkedMap } from "./linked-map";
import { BandwidthApproximator } from "./bandwidth-approximator";
import { EngineCallbacks } from "./request";
Expand Down
8 changes: 4 additions & 4 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Settings } from "./types";
import { BandwidthApproximator } from "./bandwidth-approximator";
import { Playback, QueueItem } from "./internal-types";
import { RequestContainer, EngineCallbacks } from "./request";
import * as Utils from "./utils";
import * as QueueUtils from "./utils/queue-utils";
import { FetchError } from "./errors";

export class HybridLoader {
Expand Down Expand Up @@ -37,11 +37,11 @@ export class HybridLoader {
if (!this.activeStream.segments.has(segment.localId)) {
return false;
}
const bufferRanges = Utils.getLoadBufferRanges(
const bufferRanges = QueueUtils.getLoadBufferRanges(
this.playback,
this.settings
);
return Utils.isSegmentActual(segment, bufferRanges);
return QueueUtils.isSegmentActual(segment, bufferRanges);
});
this.p2pLoaders = new P2PLoadersContainer(
this.streamManifestUrl,
Expand Down Expand Up @@ -90,7 +90,7 @@ export class HybridLoader {
this.lastQueueProcessingTimeStamp = now;

const stream = this.activeStream;
const { queue, queueSegmentIds } = Utils.generateQueue({
const { queue, queueSegmentIds } = QueueUtils.generateQueue({
segment: this.lastRequestedSegment,
stream,
playback: this.playback,
Expand Down
4 changes: 2 additions & 2 deletions packages/p2p-media-loader-core/src/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export type LoadBufferRanges = {
p2p: NumberRange;
};

export type QueueStatuses = {
export type QueueItemStatuses = {
isHighDemand: boolean;
isHttpDownloadable: boolean;
isP2PDownloadable: boolean;
};

export type QueueItem = { segment: Segment; statuses: QueueStatuses };
export type QueueItem = { segment: Segment; statuses: QueueItemStatuses };

export type BasePeerCommand<T extends PeerCommandType = PeerCommandType> = {
c: T;
Expand Down
4 changes: 2 additions & 2 deletions packages/p2p-media-loader-core/src/p2p-loader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import TrackerClient, { PeerCandidate } from "bittorrent-tracker";
import * as RIPEMD160 from "ripemd160";
import { Peer } from "./peer";
import * as PeerUtil from "./peer-utils";
import * as PeerUtil from "./utils/peer-utils";
import { Segment, Settings, StreamWithSegments } from "./types";
import { JsonSegmentAnnouncement } from "./internal-types";
import { SegmentsMemoryStorage } from "./segments-storage";
import * as Utils from "./utils";
import * as Utils from "./utils/utils";
import { PeerSegmentStatus } from "./enums";
import { RequestContainer } from "./request";

Expand Down
4 changes: 2 additions & 2 deletions packages/p2p-media-loader-core/src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
PeerSendSegmentCommand,
} from "./internal-types";
import { PeerCommandType, PeerSegmentStatus } from "./enums";
import * as PeerUtil from "./peer-utils";
import * as PeerUtil from "./utils/peer-utils";
import { P2PRequest } from "./request";
import { Segment, Settings } from "./types";
import * as Utils from "./utils";
import * as Utils from "./utils/utils";
import { PeerRequestError } from "./errors";

type PeerEventHandlers = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonSegmentAnnouncement, PeerCommand } from "./internal-types";
import * as TypeGuard from "./type-guards";
import { PeerSegmentStatus } from "./enums";
import { JsonSegmentAnnouncement, PeerCommand } from "../internal-types";
import * as TypeGuard from "../type-guards";
import { PeerSegmentStatus } from "../enums";
import * as RIPEMD160 from "ripemd160";

export function generatePeerId(): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
import { Segment, Settings, Stream, StreamWithSegments } from "./index";
import { Segment, Settings, StreamWithSegments } from "../types";
import {
QueueStatuses,
Playback,
LoadBufferRanges,
QueueItem,
NumberRange,
} from "./internal-types";

export function getStreamExternalId(
manifestResponseUrl: string,
stream: Readonly<Stream>
): string {
const { type, index } = stream;
return `${manifestResponseUrl}-${type}-${index}`;
}

export function getSegmentFullExternalId(
externalStreamId: string,
externalSegmentId: string
) {
return `${externalStreamId}|${externalSegmentId}`;
}

export function getSegmentFromStreamsMap(
streams: Map<string, StreamWithSegments>,
segmentId: string
): { segment: Segment; stream: StreamWithSegments } | undefined {
for (const stream of streams.values()) {
const segment = stream.segments.get(segmentId);
if (segment) return { segment, stream };
}
}
Playback,
QueueItem,
QueueItemStatuses,
} from "../internal-types";

export function generateQueue({
segment,
Expand Down Expand Up @@ -63,11 +38,14 @@ export function generateQueue({
let i = 0;
for (const segment of stream.segments.values(requestedSegmentId)) {
const statuses = getSegmentLoadStatuses(segment, bufferRanges);
if (!statuses && !(i === 0 && isNextSegmentHighDemand)) break;
const isNotActual = isNotActualStatuses(statuses);
if (isNotActual && !(i === 0 && isNextSegmentHighDemand)) {
break;
}
if (isSegmentLoaded(segment)) continue;

queueSegmentIds.add(segment.localId);
statuses.isHighDemand = true;
if (isNotActual) statuses.isHighDemand = true;
queue.push({ segment, statuses });
i++;
}
Expand Down Expand Up @@ -105,7 +83,7 @@ export function getLoadBufferRanges(
export function getSegmentLoadStatuses(
segment: Readonly<Segment>,
loadBufferRanges: LoadBufferRanges
): QueueStatuses {
): QueueItemStatuses {
const { highDemand, http, p2p } = loadBufferRanges;
const { startTime, endTime } = segment;

Expand All @@ -123,6 +101,11 @@ export function getSegmentLoadStatuses(
};
}

function isNotActualStatuses(statuses: QueueItemStatuses) {
const { isHighDemand, isHttpDownloadable, isP2PDownloadable } = statuses;
return !isHighDemand && !isHttpDownloadable && !isP2PDownloadable;
}

export function isSegmentActual(
segment: Readonly<Segment>,
bufferRanges: LoadBufferRanges
Expand All @@ -139,20 +122,3 @@ export function isSegmentActual(

return isInRange(startTime) || isInRange(endTime);
}

export function getControlledPromise<T>() {
let resolve: (value: T) => void;
let reject: (reason?: unknown) => void;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});

return {
promise,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
resolve: resolve!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
reject: reject!,
};
}
43 changes: 43 additions & 0 deletions packages/p2p-media-loader-core/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Segment, Stream, StreamWithSegments } from "../index";

export function getStreamExternalId(
manifestResponseUrl: string,
stream: Readonly<Stream>
): string {
const { type, index } = stream;
return `${manifestResponseUrl}-${type}-${index}`;
}

export function getSegmentFullExternalId(
externalStreamId: string,
externalSegmentId: string
) {
return `${externalStreamId}|${externalSegmentId}`;
}

export function getSegmentFromStreamsMap(
streams: Map<string, StreamWithSegments>,
segmentId: string
): { segment: Segment; stream: StreamWithSegments } | undefined {
for (const stream of streams.values()) {
const segment = stream.segments.get(segmentId);
if (segment) return { segment, stream };
}
}

export function getControlledPromise<T>() {
let resolve: (value: T) => void;
let reject: (reason?: unknown) => void;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});

return {
promise,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
resolve: resolve!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
reject: reject!,
};
}

0 comments on commit 9671b0c

Please sign in to comment.