Skip to content

Commit

Permalink
fix: browser freezes when using shaka on long live streams
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Jan 17, 2025
1 parent 113119f commit 02af012
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
12 changes: 8 additions & 4 deletions packages/p2p-media-loader-shaka/src/manifest-parser-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ export class ManifestParserDecorator implements shaka.extern.ManifestParser {

segmentIndex.get = originalGet;
try {
const references = [...segmentIndex];
const firstItemReference = references[0];
const lastItemReference = references[references.length - 1];
const referencesCount = segmentIndex.getNumReferences();
const firstItemReference = segmentIndex.get(0);
const lastItemReference = segmentIndex.get(referencesCount - 1);

if (!firstItemReference || !lastItemReference) {
return reference;
}

if (
firstItemReference === prevFirstItemReference &&
Expand All @@ -150,7 +154,7 @@ export class ManifestParserDecorator implements shaka.extern.ManifestParser {
prevLastItemReference = lastItemReference;

// Segment index have been updated
segmentManager.updateStreamSegments(stream, references);
segmentManager.updateStreamSegments(stream);
stream.isSegmentIndexAlreadyRead = true;
this.debug(`Stream ${stream.id} is updated`);
} catch {
Expand Down
51 changes: 24 additions & 27 deletions packages/p2p-media-loader-shaka/src/segment-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,26 @@ export class SegmentManager {
if (shakaStream.segmentIndex) this.updateStreamSegments(shakaStream);
}

updateStreamSegments(
shakaStream: HookedStream,
segmentReferences?: shaka.media.SegmentReference[],
) {
updateStreamSegments(shakaStream: HookedStream) {
const stream = this.core.getStream(shakaStream.id.toString());
if (!stream) return;

const { segmentIndex } = stream.shakaStream;
if (!segmentReferences && segmentIndex) {
try {
segmentReferences = [...segmentIndex];
} catch {
return;
}
}
if (!segmentReferences) return;

if (this.streamInfo.protocol === "hls") {
this.processHlsSegmentReferences(stream, segmentReferences);
this.processHlsSegmentReferences(stream);
} else {
this.processDashSegmentReferences(stream, segmentReferences);
this.processDashSegmentReferences(stream);
}
}

private processDashSegmentReferences(
managerStream: StreamWithReadonlySegments,
segmentReferences: shaka.media.SegmentReference[],
) {
const { segmentIndex } = managerStream.shakaStream;
if (!segmentIndex) return;

const staleSegmentsIds = new Set(managerStream.segments.keys());
const newSegments: Segment[] = [];
for (const reference of segmentReferences) {
for (const reference of segmentIndex) {
const externalId = Math.trunc(
reference.getStartTime() / SEGMENT_ID_RESOLUTION_IN_SECONDS,
);
Expand All @@ -89,22 +78,30 @@ export class SegmentManager {

private processHlsSegmentReferences(
managerStream: StreamWithReadonlySegments,
segmentReferences: shaka.media.SegmentReference[],
) {
const { segmentIndex } = managerStream.shakaStream;
const { segments } = managerStream;

if (!segmentIndex) return;

const lastMediaSequence = Utils.getStreamLastMediaSequence(managerStream);
const segmentRefsCount = segmentIndex.getNumReferences();

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

for (let i = 0; i < segmentRefsCount; i++) {
const reference = segmentIndex.get(i);

if (!reference) continue;

for (const [index, reference] of segmentReferences.entries()) {
const segment = Utils.createSegment({
segmentReference: reference,
externalId: firstReferenceMediaSequence + index,
externalId: firstReferenceMediaSequence + i,
});
newSegments.push(segment);
}
Expand All @@ -115,9 +112,13 @@ export class SegmentManager {
if (!lastMediaSequence) return;
let mediaSequence = lastMediaSequence;

for (const reference of itemsBackwards(segmentReferences)) {
for (let i = segmentRefsCount - 1; i >= 0; i--) {
const reference = segmentIndex.get(i);
if (!reference) continue;

const runtimeId = Utils.getSegmentRuntimeIdFromReference(reference);
if (segments.has(runtimeId)) break;

const segment = Utils.createSegment({
runtimeId,
segmentReference: reference,
Expand All @@ -143,10 +144,6 @@ export class SegmentManager {
}
}

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

function* nSegmentsBackwards(
segments: ReadonlyMap<string, Segment>,
count: number,
Expand Down
10 changes: 10 additions & 0 deletions packages/p2p-media-loader-shaka/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ export type HookedNetworkingEngine = shaka.net.NetworkingEngine & {
export type StreamWithReadonlySegments = Stream & {
segments: ReadonlyMap<string, SegmentWithStream>;
};

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace shaka.media {
interface SegmentIndex {
get(position: number): shaka.media.SegmentReference | null;
getNumReferences(): number;
}
}
}

0 comments on commit 02af012

Please sign in to comment.