diff --git a/packages/p2p-media-loader-core/src/p2p-loader.ts b/packages/p2p-media-loader-core/src/p2p-loader.ts index 3dec9832..28ddbc1a 100644 --- a/packages/p2p-media-loader-core/src/p2p-loader.ts +++ b/packages/p2p-media-loader-core/src/p2p-loader.ts @@ -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[] = []; diff --git a/packages/p2p-media-loader-core/src/peer.ts b/packages/p2p-media-loader-core/src/peer.ts index 5164c662..9aa0ea31 100644 --- a/packages/p2p-media-loader-core/src/peer.ts +++ b/packages/p2p-media-loader-core/src/peer.ts @@ -32,9 +32,7 @@ type PeerRequest = { p2pRequest: P2PRequest; resolve: (data: ArrayBuffer) => void; reject: (reason?: unknown) => void; - bytesDownloaded: number; chunks: ArrayBuffer[]; - segmentByteLength?: number; responseTimeoutId: number; }; @@ -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; @@ -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()), }, @@ -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()); } } diff --git a/packages/p2p-media-loader-core/src/request.ts b/packages/p2p-media-loader-core/src/request.ts index 49929c27..9ed306e3 100644 --- a/packages/p2p-media-loader-core/src/request.ts +++ b/packages/p2p-media-loader-core/src/request.ts @@ -9,13 +9,13 @@ export type EngineCallbacks = { export type LoadProgress = { percent: number; loadedBytes: number; - totalLength: number; + totalBytes: number; }; type RequestBase = { promise: Promise; abort: () => void; - progress?: Readonly; + progress?: LoadProgress; startTimestamp: number; }; diff --git a/packages/p2p-media-loader-core/src/segments-storage.ts b/packages/p2p-media-loader-core/src/segments-storage.ts index 8dec3af3..10d3b590 100644 --- a/packages/p2p-media-loader-core/src/segments-storage.ts +++ b/packages/p2p-media-loader-core/src/segments-storage.ts @@ -53,6 +53,10 @@ export class SegmentsMemoryStorage { return this.cachedSegmentIds.has(segmentExternalId); } + get storedSegmentIds(): ReadonlySet { + return this.cachedSegmentIds; + } + async clear(): Promise { const segmentsToDelete: string[] = []; const remainingSegments: {