forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecordingBroadcast.ts
More file actions
89 lines (81 loc) · 2.32 KB
/
Copy pathrecordingBroadcast.ts
File metadata and controls
89 lines (81 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Cross-window communication for recording state using BroadcastChannel API.
*
* This allows pop-out windows to receive recording state updates and send
* control commands back to the main window.
*/
import type { RecordingState, AudioMode, TranscriptSegment } from '@/components/recording/RecordingContext';
// Channel name for recording state sync
const CHANNEL_NAME = 'omi-recording-channel';
// Message types
export type RecordingBroadcastMessage =
| { type: 'state-update'; state: RecordingState; audioMode: AudioMode; duration: number; micLevel: number; systemLevel: number }
| { type: 'segments-update'; segments: TranscriptSegment[] }
| { type: 'command'; command: 'pause' | 'resume' | 'stop' }
| { type: 'request-state' };
/**
* Creates a BroadcastChannel for recording state communication.
* Returns null if BroadcastChannel is not supported.
*/
export function createRecordingChannel(): BroadcastChannel | null {
if (typeof window === 'undefined' || !('BroadcastChannel' in window)) {
return null;
}
return new BroadcastChannel(CHANNEL_NAME);
}
/**
* Broadcasts a state update to all listening windows
*/
export function broadcastStateUpdate(
channel: BroadcastChannel,
state: RecordingState,
audioMode: AudioMode,
duration: number,
micLevel: number,
systemLevel: number
): void {
const message: RecordingBroadcastMessage = {
type: 'state-update',
state,
audioMode,
duration,
micLevel,
systemLevel,
};
channel.postMessage(message);
}
/**
* Broadcasts transcript segments to all listening windows
*/
export function broadcastSegmentsUpdate(
channel: BroadcastChannel,
segments: TranscriptSegment[]
): void {
const message: RecordingBroadcastMessage = {
type: 'segments-update',
segments,
};
channel.postMessage(message);
}
/**
* Sends a command to control recording (pause, resume, stop)
*/
export function sendRecordingCommand(
channel: BroadcastChannel,
command: 'pause' | 'resume' | 'stop'
): void {
const message: RecordingBroadcastMessage = {
type: 'command',
command,
};
channel.postMessage(message);
}
/**
* Requests current state from the main window
*/
export function requestCurrentState(channel: BroadcastChannel): void {
const message: RecordingBroadcastMessage = {
type: 'request-state',
};
channel.postMessage(message);
}