Skip to content

Commit

Permalink
chore(ServerAbrStream): Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRT committed Nov 2, 2024
1 parent 175264b commit 008ded0
Showing 1 changed file with 30 additions and 42 deletions.
72 changes: 30 additions & 42 deletions src/core/ServerAbrStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class ServerAbrStream extends EventEmitterLike {
const clientAbrState: ClientAbrState = {
lastManualDirection: 0,
timeSinceLastManualFormatSelectionMs: 0,
quality: videoFormats.length === 1 ? firstVideoFormat?.width : DEFAULT_QUALITY,
selectedQualityHeight: videoFormats.length === 1 ? firstVideoFormat?.width : DEFAULT_QUALITY,
quality: videoFormats.length === 1 ? firstVideoFormat?.height : DEFAULT_QUALITY,
selectedQualityHeight: videoFormats.length === 1 ? firstVideoFormat?.height : DEFAULT_QUALITY,
startTimeMs: 0,
visibility: 0,
mediaType: ClientAbrState_MediaType.MEDIA_TYPE_DEFAULT,
Expand Down Expand Up @@ -118,6 +118,7 @@ export class ServerAbrStream extends EventEmitterLike {
}
} catch (error) {
this.emit('error', error);
clientAbrState.startTimeMs = Infinity;
}
}

Expand Down Expand Up @@ -219,29 +220,8 @@ export class ServerAbrStream extends EventEmitterLike {

const formatKey = getFormatKey(mediaHeader.formatId);

let currentFormat = this.formatsByKey.get(formatKey);
if (!currentFormat) {
this.initializedFormats.push({
formatId: mediaHeader.formatId,
formatKey,
durationMs: mediaHeader.durationMs,
mimeType: undefined,
sequenceCount: undefined,
sequenceList: [],
mediaChunks: [],
_state: {
formatId: mediaHeader.formatId,
startTimeMs: 0,
durationMs: 0,
startSegmentIndex: 1,
endSegmentIndex: 0
}
});

this.formatsByKey.set(formatKey, this.initializedFormats[this.initializedFormats.length - 1]);

currentFormat = this.formatsByKey.get(formatKey)!;
}
const currentFormat = this.formatsByKey.get(formatKey) || this.registerFormat(mediaHeader);
if (!currentFormat) return;

// FIXME: This is a hacky workaround to prevent duplicate sequences from being added. This should be fixed in the future (preferably by figuring out how to make the server not send duplicates).
if (mediaHeader.sequenceNumber !== undefined && this.previousSequences.get(formatKey)?.includes(mediaHeader.sequenceNumber))
Expand Down Expand Up @@ -299,36 +279,44 @@ export class ServerAbrStream extends EventEmitterLike {

private processFormatInitialization(data: Uint8Array) {
const formatInitializationMetadata = FormatInitializationMetadata.decode(data);
if (!formatInitializationMetadata.formatId) return;
this.registerFormat(formatInitializationMetadata);
}

const formatKey = getFormatKey(formatInitializationMetadata.formatId);
private processSabrRedirect(data: Uint8Array): SabrRedirect {
const sabrRedirect = SabrRedirect.decode(data);
if (!sabrRedirect.url) throw new Error('Invalid SABR redirect');
this.serverAbrStreamingUrl = sabrRedirect.url;
return sabrRedirect;
}

private registerFormat(data: MediaHeader | FormatInitializationMetadata): InitializedFormat | undefined {
if (!data.formatId)
return;

const formatKey = getFormatKey(data.formatId);

if (!this.formatsByKey.has(formatKey)) {
this.initializedFormats.push({
formatId: formatInitializationMetadata.formatId,
formatKey: getFormatKey(formatInitializationMetadata.formatId),
durationMs: formatInitializationMetadata.durationMs,
mimeType: formatInitializationMetadata.mimeType,
sequenceCount: formatInitializationMetadata.field4,
const format: InitializedFormat = {
formatId: data.formatId,
formatKey: formatKey,
durationMs: data.durationMs,
mimeType: 'mimeType' in data ? data.mimeType : undefined,
sequenceCount: 'field4' in data ? data.field4 : undefined,
sequenceList: [],
mediaChunks: [],
_state: {
formatId: formatInitializationMetadata.formatId,
formatId: data.formatId,
startTimeMs: 0,
durationMs: 0,
startSegmentIndex: 1,
endSegmentIndex: 0
}
});
};

this.initializedFormats.push(format);
this.formatsByKey.set(formatKey, this.initializedFormats[this.initializedFormats.length - 1]);
}
}

private processSabrRedirect(data: Uint8Array): SabrRedirect {
const sabrRedirect = SabrRedirect.decode(data);
if (!sabrRedirect.url) throw new Error('Invalid SABR redirect');
this.serverAbrStreamingUrl = sabrRedirect.url;
return sabrRedirect;
return format;
}
}
}

0 comments on commit 008ded0

Please sign in to comment.