Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requests container with p2p integration. #306

Merged
merged 15 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { HybridLoader } from "./hybrid-loader";
import {
Stream,
StreamWithSegments,
Segment,
SegmentResponse,
Settings,
} from "./types";
import { Stream, StreamWithSegments, Segment, Settings } from "./types";
import * as Utils from "./utils";
import { LinkedMap } from "./linked-map";
import { BandwidthApproximator } from "./bandwidth-approximator";
import { EngineCallbacks } from "./request";

export class Core<TStream extends Stream = Stream> {
private manifestResponseUrl?: string;
private readonly streams = new Map<string, StreamWithSegments<TStream>>();
private readonly settings: Settings = {
simultaneousHttpDownloads: 3,
highDemandBufferLength: 25,
httpBufferLength: 60,
p2pBufferLength: 60,
highDemandTimeWindow: 25,
httpDownloadTimeWindow: 60,
p2pDownloadTimeWindow: 60,
cachedSegmentExpiration: 120,
cachedSegmentsCount: 50,
};
Expand Down Expand Up @@ -64,7 +59,7 @@ export class Core<TStream extends Stream = Stream> {
removeSegmentIds?.forEach((id) => stream.segments.delete(id));
}

loadSegment(segmentLocalId: string): Promise<SegmentResponse> {
loadSegment(segmentLocalId: string, callbacks: EngineCallbacks) {
const { segment, stream } = this.identifySegment(segmentLocalId);

let loader: HybridLoader;
Expand All @@ -76,7 +71,7 @@ export class Core<TStream extends Stream = Stream> {
new HybridLoader(this.settings, this.bandwidthApproximator);
loader = this.secondaryStreamLoader;
}
return loader.loadSegment(segment, stream);
void loader.loadSegment(segment, stream, callbacks);
}

abortSegmentLoading(segmentId: string): void {
Expand Down
1 change: 1 addition & 0 deletions packages/p2p-media-loader-core/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum PeerCommandType {
SegmentRequest,
SegmentData,
SegmentAbsent,
CancelSegmentRequest,
}

export enum PeerSegmentStatus {
Expand Down
24 changes: 24 additions & 0 deletions packages/p2p-media-loader-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,27 @@ export class FetchError extends Error {
this.details = details;
}
}

export class RequestAbortError extends Error {
constructor(message = "AbortError") {
super(message);
}
}

export class RequestTimeoutError extends Error {
constructor(message = "TimeoutError") {
super(message);
}
}

export class ResponseBytesMismatchError extends Error {
constructor(message = "ResponseBytesMismatch") {
super(message);
}
}

export class PeerSegmentAbsentError extends Error {
constructor(message = "PeerSegmentAbsent") {
super(message);
}
}
97 changes: 34 additions & 63 deletions packages/p2p-media-loader-core/src/http-loader.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,47 @@
import { FetchError } from "./errors";
import { RequestAbortError, FetchError } from "./errors";
import { Segment } from "./types";
import { HttpRequest } from "./request";

export function getHttpSegmentRequest(segment: Segment): Readonly<HttpRequest> {
const { promise, abortController } = fetchSegmentData(segment);
return {
type: "http",
promise,
abort: () => abortController.abort(),
};
}

type Request = {
promise: Promise<ArrayBuffer>;
abortController: AbortController;
};

export class HttpLoader {
private readonly requests = new Map<string, Request>();
function fetchSegmentData(segment: Segment) {
const headers = new Headers();
const { url, byteRange, localId: segmentId } = segment;

async load(segment: Segment) {
const abortController = new AbortController();
const promise = this.fetch(segment, abortController);
const requestContext: Request = {
abortController,
promise,
};
this.requests.set(segment.localId, requestContext);
await promise;
this.requests.delete(segment.localId);
return promise;
if (byteRange) {
const { start, end } = byteRange;
const byteRangeString = `bytes=${start}-${end}`;
headers.set("Range", byteRangeString);
}
const abortController = new AbortController();

private async fetch(segment: Segment, abortController: AbortController) {
const headers = new Headers();
const { url, byteRange } = segment;
const loadSegmentData = async () => {
try {
const response = await window.fetch(url, {
headers,
signal: abortController.signal,
});

if (byteRange) {
const { start, end } = byteRange;
const byteRangeString = `bytes=${start}-${end}`;
headers.set("Range", byteRangeString);
}
const response = await fetch(url, {
headers,
signal: abortController.signal,
});
if (!response.ok) {
if (response.ok) return response.arrayBuffer();
throw new FetchError(
response.statusText ?? "Fetch, bad network response",
response.statusText ?? `Network response was not for ${segmentId}`,
response.status,
response
);
} catch (error) {
if (error instanceof Error && error.name === "AbortError") {
throw new RequestAbortError(`Segment fetch was aborted ${segmentId}`);
}
throw error;
}
};

return response.arrayBuffer();
}

isLoading(segmentId: string) {
return this.requests.has(segmentId);
}

abort(segmentId: string) {
this.requests.get(segmentId)?.abortController.abort();
this.requests.delete(segmentId);
}

getLoadingsAmount() {
return this.requests.size;
}

getLoadingSegmentIds() {
return this.requests.keys();
}

getRequest(segmentId: string) {
return this.requests.get(segmentId)?.promise;
}

abortAll() {
for (const request of this.requests.values()) {
request.abortController.abort();
}
this.requests.clear();
}
return { promise: loadSegmentData(), abortController };
}
Loading