Skip to content

Commit

Permalink
Add progress functionality to Peer class.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Sep 20, 2023
1 parent 695deca commit ddf2265
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/p2p-media-loader-core/src/p2p-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class P2PLoader {
}

private async onSegmentStorageUpdate() {
const storedSegmentIds = await this.segmentStorage.getStoredSegmentIds();
const { storedSegmentIds } = this.segmentStorage;
const loaded: Segment[] = [];
const httpLoading: Segment[] = [];

Expand Down
21 changes: 13 additions & 8 deletions packages/p2p-media-loader-core/src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ type PeerRequest = {
p2pRequest: P2PRequest;
resolve: (data: ArrayBuffer) => void;
reject: (reason?: unknown) => void;
bytesDownloaded: number;
chunks: ArrayBuffer[];
segmentByteLength?: number;
responseTimeoutId: number;
};

Expand Down Expand Up @@ -95,7 +93,11 @@ export class Peer {

case PeerCommandType.SegmentData:
if (this.request?.segment.externalId.toString() === command.i) {
this.request.segmentByteLength = command.s;
this.request.p2pRequest.progress = {
percent: 0,
loadedBytes: 0,
totalBytes: command.s,
};
}
break;

Expand Down Expand Up @@ -139,10 +141,10 @@ export class Peer {
resolve,
reject,
responseTimeoutId: this.setRequestTimeout(),
bytesDownloaded: 0,
chunks: [],
p2pRequest: {
type: "p2p",
startTimestamp: performance.now(),
promise,
abort: () => this.cancelSegmentRequest(new RequestAbortError()),
},
Expand Down Expand Up @@ -213,16 +215,19 @@ export class Peer {
}

private receiveSegmentChuck(chuck: ArrayBuffer): void {
// TODO: check can be chunk received before peer command answer
const { request } = this;
if (!request) return;
const progress = request?.p2pRequest?.progress;
if (!request || !progress) return;

request.bytesDownloaded += chuck.byteLength;
progress.loadedBytes += chuck.byteLength;
progress.percent = (progress.loadedBytes / progress.loadedBytes) * 100;
request.chunks.push(chuck);

if (request.bytesDownloaded === request.segmentByteLength) {
if (progress.loadedBytes === progress.totalBytes) {
const segmentData = joinChunks(request.chunks);
this.approveRequest(segmentData);
} else if (request.bytesDownloaded > (request.segmentByteLength ?? 0)) {
} else if (progress.loadedBytes > progress.totalBytes) {
this.cancelSegmentRequest(new ResponseBytesMismatchError());
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/p2p-media-loader-core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export type EngineCallbacks = {
export type LoadProgress = {
percent: number;
loadedBytes: number;
totalLength: number;
totalBytes: number;
};

type RequestBase = {
promise: Promise<ArrayBuffer>;
abort: () => void;
progress?: Readonly<LoadProgress>;
progress?: LoadProgress;
startTimestamp: number;
};

Expand Down
4 changes: 4 additions & 0 deletions packages/p2p-media-loader-core/src/segments-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class SegmentsMemoryStorage {
return this.cachedSegmentIds.has(segmentExternalId);
}

get storedSegmentIds(): ReadonlySet<string> {
return this.cachedSegmentIds;
}

async clear(): Promise<boolean> {
const segmentsToDelete: string[] = [];
const remainingSegments: {
Expand Down

0 comments on commit ddf2265

Please sign in to comment.