Skip to content

Commit

Permalink
Fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Dec 18, 2023
1 parent 2c1aac6 commit fb9ed3e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
9 changes: 9 additions & 0 deletions packages/p2p-media-loader-core/src/linked-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ export class LinkedMap<K, V extends object> {
}
}

*valuesBackwards(key?: K) {
let value = key ? this.map.get(key) : this._last;
if (value === undefined) return;
while (value?.item !== undefined) {
yield value.item[1];
value = value.prev;
}
}

*keys(): Generator<K> {
let value = this._first;
if (value === undefined) return;
Expand Down
2 changes: 1 addition & 1 deletion packages/p2p-media-loader-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Stream = {

export type ReadonlyLinkedMap<K, V extends object> = Pick<
LinkedMap<K, V>,
"has" | "keys" | "values" | "size"
"has" | "keys" | "values" | "valuesBackwards" | "size"
>;

export type StreamWithSegments<
Expand Down
60 changes: 38 additions & 22 deletions packages/p2p-media-loader-shaka/src/segment-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
StreamWithReadonlySegments,
SegmentBase,
StreamType,
ReadonlyLinkedMap,
} from "p2p-media-loader-core";

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

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

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

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

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

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

const deleteCount = managerStream.segments.size - startSize;
const staleSegmentIds: string[] = [];
for (let i = 0; i < deleteCount; i++) {
const segment = segments[i];
const amountToDelete = newSegments.length;
for (const segment of nSegmentsBackwards(segments, amountToDelete)) {
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--;
}
}
9 changes: 4 additions & 5 deletions packages/p2p-media-loader-shaka/src/stream-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ export function getStreamLastMediaSequence(
): number | undefined {
const { shakaStream } = stream;
const map = shakaStream.mediaSequenceTimeMap;
if (!map) return;

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

0 comments on commit fb9ed3e

Please sign in to comment.