From 008ded015f380afe413ed4812e7744744193fc3d Mon Sep 17 00:00:00 2001 From: Luan Date: Sat, 2 Nov 2024 10:02:53 -0300 Subject: [PATCH] chore(ServerAbrStream): Clean up --- src/core/ServerAbrStream.ts | 72 ++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/src/core/ServerAbrStream.ts b/src/core/ServerAbrStream.ts index 3aae649..a7de081 100644 --- a/src/core/ServerAbrStream.ts +++ b/src/core/ServerAbrStream.ts @@ -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, @@ -118,6 +118,7 @@ export class ServerAbrStream extends EventEmitterLike { } } catch (error) { this.emit('error', error); + clientAbrState.startTimeMs = Infinity; } } @@ -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)) @@ -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; + } } } \ No newline at end of file