Skip to content

Commit

Permalink
Cancel last commit changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Dec 18, 2023
1 parent fb9ed3e commit 56eb438
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 46 deletions.
60 changes: 22 additions & 38 deletions packages/p2p-media-loader-shaka/src/segment-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
StreamWithReadonlySegments,
SegmentBase,
StreamType,
ReadonlyLinkedMap,
} from "p2p-media-loader-core";

export class SegmentManager {
Expand Down Expand Up @@ -73,7 +72,6 @@ export class SegmentManager {
staleSegmentsIds.delete(segmentLocalId);
}

if (!newSegments.length && !staleSegmentsIds.size) return;
this.core.updateStream(managerStream.localId, newSegments, [
...staleSegmentsIds,
]);
Expand All @@ -83,66 +81,52 @@ export class SegmentManager {
managerStream: StreamWithReadonlySegments<Stream>,
segmentReferences: shaka.media.SegmentReference[]
) {
const { segments } = managerStream;
const segments = [...managerStream.segments.values()];
const lastMediaSequence = Utils.getStreamLastMediaSequence(managerStream);

const newSegments: SegmentBase[] = [];
if (segments.size === 0) {
if (segments.length === 0) {
const firstReferenceMediaSequence =
lastMediaSequence === undefined
? 0
: lastMediaSequence - segmentReferences.length + 1;

for (const [index, reference] of segmentReferences.entries()) {
segmentReferences.forEach((reference, index) => {
const segment = Utils.createSegment({
segmentReference: reference,
externalId: firstReferenceMediaSequence + index,
});
newSegments.push(segment);
}
});
this.core.updateStream(managerStream.localId, newSegments);
return;
}

if (!lastMediaSequence) return;
let mediaSequence = lastMediaSequence;
let index = lastMediaSequence ?? 0;
const startSize = managerStream.segments.size;

for (const reference of itemsBackwards(segmentReferences)) {
for (let i = segmentReferences.length - 1; i >= 0; i--) {
const reference = segmentReferences[i];
const localId = Utils.getSegmentLocalIdFromReference(reference);
if (segments.has(localId)) break;
const segment = Utils.createSegment({
localId,
segmentReference: reference,
externalId: mediaSequence,
});
newSegments.push(segment);
mediaSequence--;
if (!managerStream.segments.has(localId)) {
const segment = Utils.createSegment({
localId,
segmentReference: reference,
externalId: index,
});
newSegments.push(segment);
index--;
} else {
break;
}
}
newSegments.reverse();

const deleteCount = managerStream.segments.size - startSize;
const staleSegmentIds: string[] = [];
const amountToDelete = newSegments.length;
for (const segment of nSegmentsBackwards(segments, amountToDelete)) {
for (let i = 0; i < deleteCount; i++) {
const segment = segments[i];
staleSegmentIds.push(segment.localId);
}

if (!newSegments.length && !staleSegmentIds.length) return;
this.core.updateStream(managerStream.localId, newSegments, staleSegmentIds);
}
}

function* itemsBackwards<T>(items: T[]) {
for (let i = items.length - 1; i >= 0; i--) yield items[i];
}

function* nSegmentsBackwards(
segments: ReadonlyLinkedMap<string, SegmentBase>,
amount: number
) {
let i = 0;
for (const segment of segments.valuesBackwards()) {
if (i >= amount) break;
yield segment;
i--;
}
}
17 changes: 9 additions & 8 deletions packages/p2p-media-loader-shaka/src/stream-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
} from "p2p-media-loader-core";

export function createSegment({
segmentReference,
externalId,
localId,
}: {
segmentReference,
externalId,
localId,
}: {
segmentReference: shaka.media.SegmentReference;
externalId: number;
localId?: string;
Expand Down Expand Up @@ -80,9 +80,10 @@ export function getStreamLastMediaSequence(
): number | undefined {
const { shakaStream } = stream;
const map = shakaStream.mediaSequenceTimeMap;
if (!map) return;

const firstMediaSequence = map.keys().next().value as number | undefined;
if (firstMediaSequence === undefined) return;
return firstMediaSequence + map.size - 1;
if (map) {
const firstMediaSequence = map.keys().next().value as number | undefined;
if (firstMediaSequence === undefined) return;
return firstMediaSequence + map.size - 1;
}
}

0 comments on commit 56eb438

Please sign in to comment.