Skip to content

Commit

Permalink
Fix issue with not clearing aborted engine request.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Oct 2, 2023
1 parent 44ed56b commit 2b59ee2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions p2p-media-loader-demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function App() {
},
},
});
player.play();
setPlayerToWindow(player);
};

Expand Down
7 changes: 1 addition & 6 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,12 @@ export class Core<TStream extends Stream = Stream> {
await this.segmentStorage.initialize();
}
const segment = this.identifySegment(segmentLocalId);
console.log(
"segment: ",
`${segment.stream.index}-${segment.externalId}`,
segment.startTime,
segment.endTime
);
const loader = this.getStreamHybridLoader(segment);
void loader.loadSegment(segment, callbacks);
}

abortSegmentLoading(segmentId: string): void {
const segment = this.identifySegment(segmentId);
this.mainStreamLoader?.abortSegment(segmentId);
this.secondaryStreamLoader?.abortSegment(segmentId);
}
Expand Down
29 changes: 13 additions & 16 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Segment, StreamWithSegments } from "./index";
import { RequestAbortError, Segment, StreamWithSegments } from "./index";
import { getHttpSegmentRequest } from "./http-loader";
import { P2PLoader } from "./p2p-loader";
import { SegmentsMemoryStorage } from "./segments-storage";
Expand Down Expand Up @@ -95,6 +95,18 @@ export class HybridLoader {

const { simultaneousHttpDownloads, simultaneousP2PDownloads } =
this.settings;

for (const request of this.requests.engineRequests()) {
const { segment, loaderRequest } = request;
if (
!queueSegmentIds.has(segment.localId) &&
!loaderRequest &&
segment.startTime < this.lastRequestedSegment.startTime
) {
request.engineCallbacks.onError(new RequestAbortError());
}
}

for (const { segment, statuses } of queue) {
// const timeToPlayback = getTimeToSegmentPlayback(segment, this.playback);
if (statuses.isHighDemand) {
Expand Down Expand Up @@ -141,17 +153,6 @@ export class HybridLoader {
// }
break;
}

console.log(
[...this.requests.values()].map((req) => {
const { loaderRequest, engineCallbacks, segment } = req;
const { stream } = segment;

return `${stream.index}-${segment.externalId}-l${
loaderRequest ? 1 : 0
}-e${engineCallbacks ? 1 : 0}`;
})
);
}

// api method for engines
Expand Down Expand Up @@ -225,10 +226,6 @@ export class HybridLoader {

if (isPositionChanged) this.playback.position = position;
if (isRateChanged) this.playback.rate = rate;

if (isPositionSignificantlyChanged) {
console.log("\nposition: ", position);
}
void this.processQueue(isPositionSignificantlyChanged);
}

Expand Down
19 changes: 18 additions & 1 deletion packages/p2p-media-loader-core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { RequestAbortError } from "./errors";
import { Subscriptions } from "./segments-storage";
import Debug from "debug";

type SetRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;

export type EngineCallbacks = {
onSuccess: (response: SegmentResponse) => void;
onError: (reason?: unknown) => void;
Expand Down Expand Up @@ -38,6 +40,8 @@ type Request = {
engineCallbacks?: Readonly<EngineCallbacks>;
};

type RequestWithEngineCallbacks = SetRequired<Request, "engineCallbacks">;

function getRequestItemId(segment: Segment) {
return segment.localId;
}
Expand Down Expand Up @@ -91,12 +95,19 @@ export class RequestContainer {
const segmentId = getRequestItemId(segment);
const requestItem = this.requests.get(segmentId);

const { onSuccess } = engineCallbacks;
const { onSuccess, onError } = engineCallbacks;
engineCallbacks.onSuccess = (response) => {
this.clearRequestItem(segmentId, "engine");
return onSuccess(response);
};

engineCallbacks.onError = (error) => {
if (error instanceof RequestAbortError) {
this.clearRequestItem(segmentId, "engine");
}
return onError(error);
};

if (requestItem) {
requestItem.engineCallbacks = engineCallbacks;
} else {
Expand Down Expand Up @@ -124,6 +135,12 @@ export class RequestContainer {
}
}

*engineRequests(): Generator<RequestWithEngineCallbacks, void> {
for (const request of this.requests.values()) {
if (request.engineCallbacks) yield request as RequestWithEngineCallbacks;
}
}

resolveEngineRequest(segment: Segment, response: SegmentResponse) {
const id = getRequestItemId(segment);
this.requests.get(id)?.engineCallbacks?.onSuccess(response);
Expand Down
5 changes: 1 addition & 4 deletions packages/p2p-media-loader-hlsjs/src/fragment-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ export class FragmentLoaderBase implements Loader<FragmentLoaderContext> {
};

const onError = (error: unknown) => {
if (error instanceof RequestAbortError) {
if (this.stats.aborted) return;
this.callbacks?.onAbort?.(this.stats, this.context, {});
}
if (error instanceof RequestAbortError && this.stats.aborted) return;
this.handleError(error);
};

Expand Down

0 comments on commit 2b59ee2

Please sign in to comment.