Skip to content

Commit

Permalink
feat: webrtc caption
Browse files Browse the repository at this point in the history
  • Loading branch information
amar-1995 committed Mar 26, 2024
1 parent 86f1b82 commit 1c0db8a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/hms-video-store/src/caption-manager/CaptionManager.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
50 changes: 50 additions & 0 deletions packages/hms-video-store/src/caption-manager/CaptionQueue.ts
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions packages/hms-video-store/src/reactive-store/HMSSDKActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,10 @@ export class HMSSDKActions<T extends HMSGenericTypes = { sessionStore: Record<st
}

protected onMessageReceived(message: MessageNotification) {
// check if type `captions`
if (message.info.type === 'captions') {
return;
}
const hmsMessage = SDKToHMS.convertMessage(message, this.store.getState(selectLocalPeerID)) as HMSMessage;
hmsMessage.read = false;
hmsMessage.ignored = this.ignoredMessageTypes.includes(hmsMessage.type);
Expand All @@ -1156,6 +1160,12 @@ export class HMSSDKActions<T extends HMSGenericTypes = { sessionStore: Record<st
);
}

protected putCaptionInStore() {
// this.setState(store => {
// store.captions;
// });
}

/*
* Note: speakers array contain the value only for peers who have audioLevel != 0
*/
Expand Down
12 changes: 12 additions & 0 deletions packages/hms-video-store/src/schema/caption-data.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions packages/hms-video-store/src/schema/schema.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -48,6 +49,7 @@ export interface HMSStore<T extends HMSGenericTypes = { sessionStore: Record<str
hideLocalPeer: boolean;
/** @alpha */
whiteboards: Record<string, HMSWhiteboard>;
captions: Captions[];
}

export interface HMSStatsStore {
Expand Down Expand Up @@ -132,6 +134,7 @@ export const createDefaultStoreState = <T extends HMSGenericTypes>(): HMSStore<T
polls: {},
whiteboards: {},
hideLocalPeer: false,
captions: [],
};
};

Expand Down

0 comments on commit 1c0db8a

Please sign in to comment.