-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/hms-video-store/src/caption-manager/CaptionManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
packages/hms-video-store/src/caption-manager/CaptionQueue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters