Skip to content

Commit eb1ba54

Browse files
committed
Move Request file to separate file.
1 parent e019870 commit eb1ba54

File tree

8 files changed

+233
-259
lines changed

8 files changed

+233
-259
lines changed

packages/p2p-media-loader-core/src/http-loader.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,9 @@ export async function fulfillHttpSegmentRequest(
3535
});
3636

3737
if (fetchResponse.ok) {
38-
const totalBytesString = fetchResponse.headers.get("Content-Length");
39-
if (!fetchResponse.body) {
40-
fetchResponse.arrayBuffer().then((data) => {
41-
requestControls.addLoadedChunk(data);
42-
requestControls.completeOnSuccess();
43-
});
44-
return;
45-
}
38+
if (!fetchResponse.body) return;
4639

40+
const totalBytesString = fetchResponse.headers.get("Content-Length");
4741
if (totalBytesString) request.setTotalBytes(+totalBytesString);
4842

4943
const reader = fetchResponse.body.getReader();

packages/p2p-media-loader-core/src/hybrid-loader.ts

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import { SegmentsMemoryStorage } from "./segments-storage";
44
import { Settings, CoreEventHandlers } from "./types";
55
import { BandwidthApproximator } from "./bandwidth-approximator";
66
import { Playback, QueueItem } from "./internal-types";
7-
import {
8-
RequestsContainer,
9-
EngineCallbacks,
10-
HybridLoaderRequest,
11-
Request,
12-
} from "./request-container";
7+
import { RequestsContainer } from "./request-container";
8+
import { Request, EngineCallbacks, HybridLoaderRequest } from "./request";
139
import * as QueueUtils from "./utils/queue";
1410
import * as LoggerUtils from "./utils/logger";
11+
import * as StreamUtils from "./utils/stream";
12+
import * as Utils from "./utils/utils";
1513
import { P2PLoadersContainer } from "./p2p/loaders-container";
1614
import { PeerRequestError } from "./p2p/peer";
1715
import debug from "debug";
@@ -40,7 +38,7 @@ export class HybridLoader {
4038
this.lastRequestedSegment = requestedSegment;
4139
const activeStream = requestedSegment.stream;
4240
this.playback = { position: requestedSegment.startTime, rate: 1 };
43-
this.segmentAvgDuration = getSegmentAvgDuration(activeStream);
41+
this.segmentAvgDuration = StreamUtils.getSegmentAvgDuration(activeStream);
4442
this.requests = new RequestsContainer(
4543
requestedSegment.stream.type,
4644
this.bandwidthApproximator
@@ -159,23 +157,6 @@ export class HybridLoader {
159157

160158
if (statuses.isHighDemand) {
161159
if (request?.type === "http") continue;
162-
163-
if (request?.type === "p2p") {
164-
const timeToPlayback = getTimeToSegmentPlayback(
165-
segment,
166-
this.playback
167-
);
168-
const remainingDownloadTime =
169-
getPredictedRemainingDownloadTime(request);
170-
if (
171-
remainingDownloadTime === undefined ||
172-
remainingDownloadTime > timeToPlayback
173-
) {
174-
request.abort();
175-
} else {
176-
continue;
177-
}
178-
}
179160
if (this.requests.executingHttpCount < simultaneousHttpDownloads) {
180161
void this.loadThroughHttp(item);
181162
continue;
@@ -218,8 +199,11 @@ export class HybridLoader {
218199

219200
// api method for engines
220201
abortSegment(segment: Segment) {
202+
const request = this.requests.get(segment);
203+
if (!request) return;
204+
request.abort();
205+
this.createProcessQueueMicrotask();
221206
this.logger.engine("abort: ", LoggerUtils.getSegmentString(segment));
222-
this.requests.abortEngineRequest(segment);
223207
}
224208

225209
private async loadThroughHttp(item: QueueItem, isRandom = false) {
@@ -286,7 +270,7 @@ export class HybridLoader {
286270
const shouldLoad = Math.random() < probability;
287271

288272
if (!shouldLoad) return;
289-
const item = queue[Math.floor(Math.random() * queue.length)];
273+
const item = Utils.getRandomItem(queue);
290274
void this.loadThroughHttp(item, true);
291275

292276
this.logger.loader(
@@ -387,36 +371,3 @@ function* arrayBackwards<T>(arr: T[]) {
387371
yield arr[i];
388372
}
389373
}
390-
391-
function getTimeToSegmentPlayback(segment: Segment, playback: Playback) {
392-
return Math.max(segment.startTime - playback.position, 0) / playback.rate;
393-
}
394-
395-
function getPredictedRemainingDownloadTime(request: HybridLoaderRequest) {
396-
const { progress } = request;
397-
if (!progress || progress.lastLoadedChunkTimestamp === undefined) {
398-
return undefined;
399-
}
400-
401-
const now = performance.now();
402-
const bandwidth =
403-
progress.percent /
404-
(progress.lastLoadedChunkTimestamp - progress.startTimestamp);
405-
const remainingDownloadPercent = 100 - progress.percent;
406-
const predictedRemainingTimeFromLastDownload =
407-
remainingDownloadPercent / bandwidth;
408-
const timeFromLastDownload = now - progress.lastLoadedChunkTimestamp;
409-
return (predictedRemainingTimeFromLastDownload - timeFromLastDownload) / 1000;
410-
}
411-
412-
function getSegmentAvgDuration(stream: StreamWithSegments) {
413-
const { segments } = stream;
414-
let sumDuration = 0;
415-
const size = segments.size;
416-
for (const segment of segments.values()) {
417-
const duration = segment.endTime - segment.startTime;
418-
sumDuration += duration;
419-
}
420-
421-
return sumDuration / size;
422-
}

packages/p2p-media-loader-core/src/p2p/loader.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import * as PeerUtil from "../utils/peer";
44
import { Segment, Settings, StreamWithSegments } from "../types";
55
import { QueueItem } from "../internal-types";
66
import { SegmentsMemoryStorage } from "../segments-storage";
7-
import * as Utils from "../utils/utils";
87
import * as LoggerUtils from "../utils/logger";
8+
import * as StreamUtils from "../utils/stream";
9+
import * as Utils from "../utils/utils";
910
import { PeerSegmentStatus } from "../enums";
10-
import { Request, RequestsContainer } from "../request-container";
11+
import { RequestsContainer } from "../request-container";
12+
import { Request } from "../request";
1113
import debug from "debug";
1214

1315
export class P2PLoader {
@@ -26,7 +28,7 @@ export class P2PLoader {
2628
private readonly settings: Settings
2729
) {
2830
this.peerId = PeerUtil.generatePeerId();
29-
const streamExternalId = Utils.getStreamExternalId(
31+
const streamExternalId = StreamUtils.getStreamExternalId(
3032
this.streamManifestUrl,
3133
this.stream
3234
);
@@ -108,7 +110,7 @@ export class P2PLoader {
108110
}
109111

110112
const peer = untestedPeers.length
111-
? getRandomItem(untestedPeers)
113+
? Utils.getRandomItem(untestedPeers)
112114
: fastestPeer;
113115

114116
if (!peer) return;
@@ -182,7 +184,7 @@ export class P2PLoader {
182184
};
183185

184186
private async onSegmentRequested(peer: Peer, segmentExternalId: string) {
185-
const segment = Utils.getSegmentFromStreamByExternalId(
187+
const segment = StreamUtils.getSegmentFromStreamByExternalId(
186188
this.stream,
187189
segmentExternalId
188190
);
@@ -245,7 +247,3 @@ function utf8ToHex(utf8String: string) {
245247

246248
return result;
247249
}
248-
249-
function getRandomItem<T>(items: T[]): T {
250-
return items[Math.floor(Math.random() * items.length)];
251-
}

packages/p2p-media-loader-core/src/p2p/peer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "../internal-types";
99
import { PeerCommandType, PeerSegmentStatus } from "../enums";
1010
import * as PeerUtil from "../utils/peer";
11-
import { Request, RequestControls } from "../request-container";
11+
import { Request, RequestControls } from "../request";
1212
import { Segment, Settings } from "../types";
1313
import * as Utils from "../utils/utils";
1414
import debug from "debug";

0 commit comments

Comments
 (0)