Skip to content

Commit

Permalink
Simplify metadata builder
Browse files Browse the repository at this point in the history
  • Loading branch information
karpov-kir committed May 16, 2024
1 parent c260da0 commit ecbbf27
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
75 changes: 48 additions & 27 deletions src/ts/ContentMetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,64 @@ export class ContentMetadataBuilder {
this.playbackStarted = value;
}

build(): Conviva.ConvivaMetadata {
const latestBuiltMetadata: Partial<Conviva.ContentMetadata> = this.latestBuiltMetadata || {};
private getStaticMetadata() {
const metadata: Partial<Conviva.ContentMetadata> = {};

// 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<Conviva.ContentMetadata> {
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<Conviva.ContentMetadata> = {
...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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ts/conviva/ConvivaExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string, string>;
public defaultResource: string;
public duration: number;
public encodedFrameRate: number;
Expand Down

0 comments on commit ecbbf27

Please sign in to comment.