diff --git a/packages/hms-video-store/src/caption-manager/CaptionManager.ts b/packages/hms-video-store/src/caption-manager/CaptionManager.ts new file mode 100644 index 0000000000..cd00bf7045 --- /dev/null +++ b/packages/hms-video-store/src/caption-manager/CaptionManager.ts @@ -0,0 +1,45 @@ +import { CaptionQueue } from './CaptionQueue'; +import { CaptionData, Captions } from '../schema/caption-data'; + +export class CaptionManager { + private storage: { [key: string]: CaptionQueue } = {}; + private peerCapacity = 3; + + add(data: CaptionData) { + const captionData = { + final: data.final, + caption: data.caption, + }; + if (data.peer_id in this.storage) { + this.storage[data.peer_id].enqueue(captionData); + return; + } + if (this.size() === this.peerCapacity) { + this.delete(); + } + this.storage[data.peer_id] = new CaptionQueue(3); + this.storage[data.peer_id].enqueue(captionData); + } + // map ordered to delete first key.. + delete(): boolean { + const key: string = Object.keys(this.storage).shift() || ''; + if (!key) { + return false; + } + delete this.storage[key]; + return true; + } + + getCaptions(): Captions[] { + const keys = Object.keys(this.storage); + const data = keys.map((peerId: string) => { + const word = this.storage[peerId].getCaption(); + return { peerId, caption: word }; + }); + return data; + } + + size(): number { + return Object.getOwnPropertyNames(this.storage).length; + } +} diff --git a/packages/hms-video-store/src/caption-manager/CaptionQueue.ts b/packages/hms-video-store/src/caption-manager/CaptionQueue.ts new file mode 100644 index 0000000000..1abd276233 --- /dev/null +++ b/packages/hms-video-store/src/caption-manager/CaptionQueue.ts @@ -0,0 +1,50 @@ +export interface ICaptionQueue { + enqueue(item: QueueData): void; + dequeue(): QueueData | undefined; + getCaption(): string | undefined; + size(): number; +} +export interface QueueData { + final: boolean; + caption: string; +} +export class CaptionQueue implements ICaptionQueue { + private storage: QueueData[] = []; + + constructor(private capacity: number = 6) {} + + enqueue(item: QueueData): void { + if (this.size() === this.capacity && this.storage[this.size() - 1].final) { + this.dequeue(); + } + // if (this.size() === 0 || item.final) { + // this.storage.push(item); + // return; + // } + // if (item.final) { + // this.storage.push(item); + // return; + // } + this.storage[this.size() - 1] = item; + } + dequeue(): QueueData | undefined { + if (this.size() <= 0) { + return undefined; + } + return this.storage.shift(); + } + getCaption(): string { + let script = ''; + this.storage.forEach((value: QueueData) => (script += `${value.caption} `)); + return script; + } + size(): number { + return this.storage.length; + } +} +// "123", "234" [] [] [] [] +// 1 2 3 1 2 3 + +// "HI" +// "HI AMar" +// "HI AMAR or" final = true diff --git a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts index 5fc292b07b..967cf114a0 100644 --- a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts +++ b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts @@ -1135,6 +1135,10 @@ export class HMSSDKActions { + // store.captions; + // }); + } + /* * Note: speakers array contain the value only for peers who have audioLevel != 0 */ diff --git a/packages/hms-video-store/src/schema/caption-data.ts b/packages/hms-video-store/src/schema/caption-data.ts new file mode 100644 index 0000000000..6ec1056e89 --- /dev/null +++ b/packages/hms-video-store/src/schema/caption-data.ts @@ -0,0 +1,12 @@ +export interface CaptionData { + start: number; + end: number; + peer_id: string; + final: boolean; + caption: string; +} + +export interface Captions { + peerId: string; + caption: string; +} diff --git a/packages/hms-video-store/src/schema/schema.ts b/packages/hms-video-store/src/schema/schema.ts index 9e9aa9f664..2eeb7f8d23 100644 --- a/packages/hms-video-store/src/schema/schema.ts +++ b/packages/hms-video-store/src/schema/schema.ts @@ -1,3 +1,4 @@ +import { Captions } from './caption-data'; import { HMSException } from './error'; import { HMSMessage, HMSMessageID } from './message'; import { HMSPeer, HMSPeerID, HMSSpeaker, HMSTrack, HMSTrackID } from './peer'; @@ -48,6 +49,7 @@ export interface HMSStore; + captions: Captions[]; } export interface HMSStatsStore { @@ -132,6 +134,7 @@ export const createDefaultStoreState = (): HMSStore