Skip to content

Commit

Permalink
Force queue process if playback position was significantly changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Sep 28, 2023
1 parent 083d593 commit 420ffcd
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class HybridLoader {
private lastRequestedSegment: Readonly<Segment>;
private readonly playback: Playback;
private lastQueueProcessingTimeStamp?: number;
private readonly segmentAvgDuration: number;

constructor(
private streamManifestUrl: string,
Expand All @@ -29,6 +30,7 @@ export class HybridLoader {
this.lastRequestedSegment = requestedSegment;
this.activeStream = requestedStream;
this.playback = { position: requestedSegment.startTime, rate: 1 };
this.segmentAvgDuration = getSegmentAvgDuration(requestedStream);

if (!this.segmentStorage.isInitialized) {
throw new Error("Segment storage is not initialized.");
Expand Down Expand Up @@ -215,9 +217,13 @@ export class HybridLoader {

if (!isRateChanged && !isPositionChanged) return;

const isPositionSignificantlyChanged =
Math.abs(position - this.playback.position) / this.segmentAvgDuration >
0.5;

if (isPositionChanged) this.playback.position = position;
if (isRateChanged) this.playback.rate = rate;
void this.processQueue(false);
void this.processQueue(isPositionSignificantlyChanged);
}

destroy() {
Expand Down Expand Up @@ -320,3 +326,15 @@ class P2PLoadersContainer {
// const timeFromLastDownload = now - progress.lastLoadedChunkTimestamp;
// return predictedRemainingTimeFromLastDownload - timeFromLastDownload;
// }

function getSegmentAvgDuration(stream: StreamWithSegments) {
const { segments } = stream;
let sumDuration = 0;
const size = segments.size;
for (const segment of segments.values()) {
const duration = segment.endTime - segment.startTime;
sumDuration += duration;
}

return sumDuration / size;
}

0 comments on commit 420ffcd

Please sign in to comment.