diff --git a/src/ts/ContentMetadataBuilder.ts b/src/ts/ContentMetadataBuilder.ts index 72c782e..cf44ee6 100644 --- a/src/ts/ContentMetadataBuilder.ts +++ b/src/ts/ContentMetadataBuilder.ts @@ -41,43 +41,64 @@ export class ContentMetadataBuilder { this.playbackStarted = value; } - build(): Conviva.ConvivaMetadata { - const latestBuiltMetadata: Partial = this.latestBuiltMetadata || {}; + private getStaticMetadata() { + const metadata: Partial = {}; + // This metadata can only be changed before the playback is started if (!this.playbackStarted) { // Asset name is only allowed to be set once - if (!latestBuiltMetadata.assetName) { - latestBuiltMetadata.assetName = this.assetName; - } + metadata.assetName = this.latestBuiltMetadata.assetName || this.assetName; - latestBuiltMetadata.viewerId = this.viewerId; - latestBuiltMetadata.streamType = this.metadataOverrides.streamType || this.metadata.streamType; - latestBuiltMetadata.applicationName = this.metadataOverrides.applicationName || this.metadata.applicationName; - latestBuiltMetadata.duration = this.metadataOverrides.duration || this.metadata.duration; + metadata.viewerId = this.viewerId; + metadata.streamType = this.metadataOverrides.streamType || this.metadata.streamType; + metadata.applicationName = this.metadataOverrides.applicationName || this.metadata.applicationName; + metadata.duration = this.metadataOverrides.duration || this.metadata.duration; - latestBuiltMetadata.custom = this.custom; + metadata.custom = this.custom; + } + // If the playback has been started, the values cannot be changed and the latest values before the playback started has to be used + else { + metadata.assetName = this.latestBuiltMetadata.assetName; + metadata.viewerId = this.latestBuiltMetadata.viewerId; + metadata.streamType = this.latestBuiltMetadata.streamType; + metadata.applicationName = this.latestBuiltMetadata.applicationName; + metadata.duration = this.latestBuiltMetadata.duration; + metadata.custom = this.latestBuiltMetadata.custom; } - latestBuiltMetadata.encodedFrameRate = this.metadataOverrides.encodedFrameRate || this.metadata.encodedFrameRate; - latestBuiltMetadata.defaultResource = this.metadataOverrides.defaultResource || this.metadata.defaultResource; - latestBuiltMetadata.streamUrl = this.metadataOverrides.streamUrl || this.metadata.streamUrl; - - this.latestBuiltMetadata = latestBuiltMetadata; - - const convivaContentInfo: ReservedContentMetadata = { - [Conviva.Constants.ASSET_NAME]: latestBuiltMetadata.assetName, - [Conviva.Constants.ENCODED_FRAMERATE]: latestBuiltMetadata.encodedFrameRate, - [Conviva.Constants.DURATION]: latestBuiltMetadata.duration, - [Conviva.Constants.DEFAULT_RESOURCE]: latestBuiltMetadata.defaultResource, - [Conviva.Constants.STREAM_URL]: latestBuiltMetadata.streamUrl, - [Conviva.Constants.IS_LIVE]: latestBuiltMetadata.streamType, - [Conviva.Constants.VIEWER_ID]: latestBuiltMetadata.viewerId || 'GET_VIEWER_ID_FROM_PLAYER', - [Conviva.Constants.PLAYER_NAME]: latestBuiltMetadata.applicationName || 'GET_PLAYER_NAME_OR_TYPE', + return metadata; + } + + private getDynamicMetadata(): Partial { + return { + encodedFrameRate: this.metadataOverrides.encodedFrameRate || this.metadata.encodedFrameRate, + defaultResource: this.metadataOverrides.defaultResource || this.metadata.defaultResource, + streamUrl: this.metadataOverrides.streamUrl || this.metadata.streamUrl, + } + } + + build(): Conviva.ConvivaMetadata { + const newMetadata: Partial = { + ...this.getStaticMetadata(), + ...this.getDynamicMetadata(), + }; + + this.latestBuiltMetadata = newMetadata; + + const reservedMetadata: ReservedContentMetadata = { + [Conviva.Constants.ASSET_NAME]: newMetadata.assetName, + [Conviva.Constants.ENCODED_FRAMERATE]: newMetadata.encodedFrameRate, + [Conviva.Constants.DURATION]: newMetadata.duration, + [Conviva.Constants.DEFAULT_RESOURCE]: newMetadata.defaultResource, + [Conviva.Constants.STREAM_URL]: newMetadata.streamUrl, + [Conviva.Constants.IS_LIVE]: newMetadata.streamType, + [Conviva.Constants.VIEWER_ID]: newMetadata.viewerId || 'GET_VIEWER_ID_FROM_PLAYER', + [Conviva.Constants.PLAYER_NAME]: newMetadata.applicationName || 'GET_PLAYER_NAME_OR_TYPE', }; return { - ...convivaContentInfo, - ...latestBuiltMetadata.custom, + ...reservedMetadata, + ...newMetadata.custom, } as Conviva.ConvivaMetadata; } diff --git a/src/ts/conviva/ConvivaExtension.ts b/src/ts/conviva/ConvivaExtension.ts index 558c0f8..e528389 100644 --- a/src/ts/conviva/ConvivaExtension.ts +++ b/src/ts/conviva/ConvivaExtension.ts @@ -2,7 +2,7 @@ * The following properties exist in the `window.Conviva` object but they are missing * in types of `conviva-core-sdk` package for some reason, so let's add them manually. * - * Keep this in the source code, so the types are available in the compiled code therefore in the consuming projects as. + * Keep this in the source code, so the types are available in the compiled code therefore in the consuming projects as well. */ import * as Conviva from '@convivainc/conviva-js-coresdk'; @@ -12,7 +12,7 @@ declare module '@convivainc/conviva-js-coresdk' { public static readonly StreamType: Conviva.ConvivaConstants['StreamType']; public applicationName: string; public assetName: string; - public custom: {}; + public custom: Record; public defaultResource: string; public duration: number; public encodedFrameRate: number;