@fabriktor/fx 0.0.166 → 0.0.167
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/calls/calls.d.ts +3 -0
- package/dist/src/calls/calls.js +8 -0
- package/dist/src/calls/session.d.ts +84 -0
- package/dist/src/calls/session.js +309 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CallsOperator, NotificationsOperator } from "@fabriktor/client";
|
|
2
|
+
import { CallSession, type CallSessionOptions } from "./session.js";
|
|
2
3
|
export type MintVideoTokenOptions = Parameters<CallsOperator["mintVideoToken"]>[0];
|
|
3
4
|
export type RelayNotificationOptions = Parameters<NotificationsOperator["relayNotification"]>[0];
|
|
4
5
|
export type StartCallOptions = {
|
|
@@ -17,6 +18,7 @@ export type CallsFXOptions = {
|
|
|
17
18
|
notifications: NotificationsOperator;
|
|
18
19
|
};
|
|
19
20
|
export interface CallsFXOperator {
|
|
21
|
+
createSession(opts: CallSessionOptions): CallSession;
|
|
20
22
|
startCall(opts: StartCallOptions): Promise<string>;
|
|
21
23
|
answerCall(opts: AnswerCallOptions): Promise<string>;
|
|
22
24
|
cancelCall(opts: CancelCallOptions): Promise<void>;
|
|
@@ -25,6 +27,7 @@ export declare class CallsFX implements CallsFXOperator {
|
|
|
25
27
|
private calls;
|
|
26
28
|
private notifications;
|
|
27
29
|
constructor(opts: CallsFXOptions);
|
|
30
|
+
createSession(opts: CallSessionOptions): CallSession;
|
|
28
31
|
startCall(opts: StartCallOptions): Promise<string>;
|
|
29
32
|
answerCall(opts: AnswerCallOptions): Promise<string>;
|
|
30
33
|
cancelCall(opts: CancelCallOptions): Promise<void>;
|
package/dist/src/calls/calls.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// not use this file except in compliance with the License. You may obtain
|
|
5
5
|
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
6
|
import { errResolveFailed } from "../core/err.js";
|
|
7
|
+
import { CallSession } from "./session.js";
|
|
7
8
|
const resourceVideoToken = "video_token";
|
|
8
9
|
const msgUnableToMintVideoToken = "Unable to mint video token.";
|
|
9
10
|
export class CallsFX {
|
|
@@ -13,6 +14,13 @@ export class CallsFX {
|
|
|
13
14
|
this.calls = opts.calls;
|
|
14
15
|
this.notifications = opts.notifications;
|
|
15
16
|
}
|
|
17
|
+
createSession(opts) {
|
|
18
|
+
return new CallSession({
|
|
19
|
+
...opts,
|
|
20
|
+
mint: (mint) => this.requireVideoToken(mint),
|
|
21
|
+
notifications: this.notifications,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
16
24
|
async startCall(opts) {
|
|
17
25
|
const token = await this.requireVideoToken(opts.mint);
|
|
18
26
|
await this.notifications.relayNotification(opts.notification);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { CallsOperator, NotificationsOperator } from "@fabriktor/client";
|
|
2
|
+
import { CallModeAudio, CallModeVideo } from "@fabriktor/schema";
|
|
3
|
+
export declare const callsNotificationKey = "calls.events.received";
|
|
4
|
+
export declare const callRingTimeoutMS = 60000;
|
|
5
|
+
export type CallMode = typeof CallModeAudio | typeof CallModeVideo;
|
|
6
|
+
export type CallPhase = "idle" | "incoming" | "connecting" | "ringing" | "connected" | "reconnecting";
|
|
7
|
+
export type CallParticipant = {
|
|
8
|
+
id: string;
|
|
9
|
+
user_id: string;
|
|
10
|
+
local: boolean;
|
|
11
|
+
audio: boolean;
|
|
12
|
+
video: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type CallMediaState = {
|
|
15
|
+
participants: readonly CallParticipant[];
|
|
16
|
+
reconnecting: boolean;
|
|
17
|
+
};
|
|
18
|
+
export interface CallMediaConnection {
|
|
19
|
+
disconnect(): void;
|
|
20
|
+
microphone(enabled: boolean): Promise<void>;
|
|
21
|
+
camera(enabled: boolean): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export interface CallMediaAdapter {
|
|
24
|
+
connect(opts: {
|
|
25
|
+
token: string;
|
|
26
|
+
room_id: string;
|
|
27
|
+
video: boolean;
|
|
28
|
+
signal: AbortSignal;
|
|
29
|
+
on_change: (state: CallMediaState) => void;
|
|
30
|
+
on_disconnect: (error?: unknown) => void;
|
|
31
|
+
}): Promise<CallMediaConnection>;
|
|
32
|
+
}
|
|
33
|
+
export type CallInvitation = {
|
|
34
|
+
id: string;
|
|
35
|
+
room_id: string;
|
|
36
|
+
caller_id: string;
|
|
37
|
+
mode: CallMode;
|
|
38
|
+
};
|
|
39
|
+
export type CallSessionState = {
|
|
40
|
+
phase: CallPhase;
|
|
41
|
+
call: CallInvitation | null;
|
|
42
|
+
participants: readonly CallParticipant[];
|
|
43
|
+
microphone: boolean;
|
|
44
|
+
camera: boolean;
|
|
45
|
+
};
|
|
46
|
+
export type CallSessionOptions = {
|
|
47
|
+
owner_id: string;
|
|
48
|
+
media: CallMediaAdapter;
|
|
49
|
+
on_change: (state: CallSessionState) => void;
|
|
50
|
+
on_error: (error: unknown) => void;
|
|
51
|
+
};
|
|
52
|
+
export type CallSessionDependencies = CallSessionOptions & {
|
|
53
|
+
mint: (opts: Parameters<CallsOperator["mintVideoToken"]>[0]) => Promise<string>;
|
|
54
|
+
notifications: NotificationsOperator;
|
|
55
|
+
};
|
|
56
|
+
export declare class CallSession {
|
|
57
|
+
private opts;
|
|
58
|
+
state: CallSessionState;
|
|
59
|
+
private connection?;
|
|
60
|
+
private controller?;
|
|
61
|
+
private timer?;
|
|
62
|
+
private recipients;
|
|
63
|
+
private closed;
|
|
64
|
+
private had_remote;
|
|
65
|
+
private disposed;
|
|
66
|
+
private inviting?;
|
|
67
|
+
constructor(opts: CallSessionDependencies);
|
|
68
|
+
start(room_id: string, members: readonly string[], mode: CallMode): Promise<void>;
|
|
69
|
+
receive(data: Readonly<Record<string, string>>): void;
|
|
70
|
+
answer(): Promise<void>;
|
|
71
|
+
end(): Promise<void>;
|
|
72
|
+
microphone(enabled: boolean): Promise<void>;
|
|
73
|
+
camera(enabled: boolean): Promise<void>;
|
|
74
|
+
dispose(): void;
|
|
75
|
+
private begin;
|
|
76
|
+
private connect;
|
|
77
|
+
private relay;
|
|
78
|
+
private current;
|
|
79
|
+
private publish;
|
|
80
|
+
private reset;
|
|
81
|
+
private remember;
|
|
82
|
+
private armTimeout;
|
|
83
|
+
private clearTimer;
|
|
84
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// Copyright (C) Fabriktor, Inc. 2025-present.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
4
|
+
// not use this file except in compliance with the License. You may obtain
|
|
5
|
+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
import { CallIDKey, CallRoomIDKey, CallSenderIDKey, CallModeKey, CallSignalKey, CallModeAudio, CallModeVideo, CallSignalInvite, CallSignalAnswered, CallSignalCancelled, ColCallsEvents, SvcCalls, } from "@fabriktor/schema";
|
|
7
|
+
import { errValidation } from "../core/err.js";
|
|
8
|
+
// Notification keys are preference-map keys, not generated Go constants.
|
|
9
|
+
export const callsNotificationKey = `${SvcCalls}.${ColCallsEvents}.received`;
|
|
10
|
+
export const callRingTimeoutMS = 60_000;
|
|
11
|
+
const closedCallLimit = 100;
|
|
12
|
+
const callNotificationTitle = "Fabriktor call";
|
|
13
|
+
const callNotificationBody = "Open Fabriktor to join the call.";
|
|
14
|
+
function idleCallState() {
|
|
15
|
+
return {
|
|
16
|
+
phase: "idle",
|
|
17
|
+
call: null,
|
|
18
|
+
participants: [],
|
|
19
|
+
microphone: true,
|
|
20
|
+
camera: false,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
// One foreground call per authenticated session. Media ownership is independent of views.
|
|
24
|
+
export class CallSession {
|
|
25
|
+
opts;
|
|
26
|
+
state = idleCallState();
|
|
27
|
+
connection;
|
|
28
|
+
controller;
|
|
29
|
+
timer;
|
|
30
|
+
recipients = [];
|
|
31
|
+
closed = new Set();
|
|
32
|
+
had_remote = false;
|
|
33
|
+
disposed = false;
|
|
34
|
+
inviting;
|
|
35
|
+
constructor(opts) {
|
|
36
|
+
this.opts = opts;
|
|
37
|
+
}
|
|
38
|
+
async start(room_id, members, mode) {
|
|
39
|
+
if (this.disposed || this.state.call)
|
|
40
|
+
return;
|
|
41
|
+
const recipients = [...new Set(members)].filter((id) => id && id !== this.opts.owner_id);
|
|
42
|
+
if (!room_id || !recipients.length)
|
|
43
|
+
throw errValidation({
|
|
44
|
+
msg: "A call requires a room and another member.",
|
|
45
|
+
});
|
|
46
|
+
const call = {
|
|
47
|
+
id: crypto.randomUUID(),
|
|
48
|
+
room_id,
|
|
49
|
+
caller_id: this.opts.owner_id,
|
|
50
|
+
mode,
|
|
51
|
+
};
|
|
52
|
+
this.recipients = recipients;
|
|
53
|
+
this.begin(call, "connecting");
|
|
54
|
+
try {
|
|
55
|
+
await this.connect(call);
|
|
56
|
+
if (!this.current(call))
|
|
57
|
+
return;
|
|
58
|
+
// Do not ring recipients until the caller has joined successfully.
|
|
59
|
+
const inviting = this.relay(call, CallSignalInvite, recipients);
|
|
60
|
+
this.inviting = { id: call.id, promise: inviting };
|
|
61
|
+
await inviting;
|
|
62
|
+
if (!this.current(call))
|
|
63
|
+
return;
|
|
64
|
+
if (!this.had_remote)
|
|
65
|
+
this.publish({ phase: "ringing" });
|
|
66
|
+
this.armTimeout(call);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (this.current(call)) {
|
|
70
|
+
this.reset(call);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
if (this.inviting?.id === call.id)
|
|
76
|
+
this.inviting = undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
receive(data) {
|
|
80
|
+
if (this.disposed)
|
|
81
|
+
return;
|
|
82
|
+
const id = data[CallIDKey], room_id = data[CallRoomIDKey], sender = data[CallSenderIDKey];
|
|
83
|
+
const mode = data[CallModeKey], signal = data[CallSignalKey];
|
|
84
|
+
if (!id ||
|
|
85
|
+
!room_id ||
|
|
86
|
+
!sender ||
|
|
87
|
+
(mode !== CallModeAudio && mode !== CallModeVideo))
|
|
88
|
+
return;
|
|
89
|
+
const call = this.state.call;
|
|
90
|
+
if (signal === CallSignalInvite) {
|
|
91
|
+
if (sender === this.opts.owner_id ||
|
|
92
|
+
this.closed.has(id) ||
|
|
93
|
+
call?.id === id)
|
|
94
|
+
return;
|
|
95
|
+
const incoming = { id, room_id, caller_id: sender, mode };
|
|
96
|
+
if (call) {
|
|
97
|
+
// Busy/decline affects this recipient only, never an entire group.
|
|
98
|
+
this.remember(id);
|
|
99
|
+
void this.relay(incoming, CallSignalCancelled, [
|
|
100
|
+
sender,
|
|
101
|
+
this.opts.owner_id,
|
|
102
|
+
]).catch(this.opts.on_error);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
this.begin(incoming, "incoming");
|
|
106
|
+
this.armTimeout(incoming);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (signal !== CallSignalCancelled && signal !== CallSignalAnswered)
|
|
110
|
+
return;
|
|
111
|
+
if (!call || call.id !== id || call.room_id !== room_id) {
|
|
112
|
+
this.remember(id);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (sender === this.opts.owner_id) {
|
|
116
|
+
// The accepting device is already connecting; other devices still ring.
|
|
117
|
+
if (this.state.phase === "incoming")
|
|
118
|
+
this.reset(call);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (signal !== CallSignalCancelled)
|
|
122
|
+
return;
|
|
123
|
+
if (sender === call.caller_id &&
|
|
124
|
+
(this.state.phase === "incoming" || this.state.phase === "connecting")) {
|
|
125
|
+
this.reset(call);
|
|
126
|
+
}
|
|
127
|
+
else if (call.caller_id === this.opts.owner_id && !this.had_remote) {
|
|
128
|
+
this.recipients = this.recipients.filter((id) => id !== sender);
|
|
129
|
+
if (!this.recipients.length)
|
|
130
|
+
this.reset(call);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async answer() {
|
|
134
|
+
const call = this.state.call;
|
|
135
|
+
if (!call || this.state.phase !== "incoming")
|
|
136
|
+
return;
|
|
137
|
+
this.clearTimer();
|
|
138
|
+
this.publish({ phase: "connecting" });
|
|
139
|
+
try {
|
|
140
|
+
await this.connect(call);
|
|
141
|
+
if (!this.current(call))
|
|
142
|
+
return;
|
|
143
|
+
await this.relay(call, CallSignalAnswered, [this.opts.owner_id]);
|
|
144
|
+
if (this.current(call)) {
|
|
145
|
+
this.publish({ phase: this.had_remote ? "connected" : "connecting" });
|
|
146
|
+
this.armTimeout(call);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (this.current(call)) {
|
|
151
|
+
this.reset(call);
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async end() {
|
|
157
|
+
const call = this.state.call;
|
|
158
|
+
if (!call)
|
|
159
|
+
return;
|
|
160
|
+
const recipients = call.caller_id === this.opts.owner_id
|
|
161
|
+
? [...this.recipients]
|
|
162
|
+
: this.state.phase === "incoming" || this.state.phase === "connecting"
|
|
163
|
+
? [call.caller_id, this.opts.owner_id]
|
|
164
|
+
: [this.opts.owner_id];
|
|
165
|
+
const inviting = this.inviting?.id === call.id ? this.inviting.promise : undefined;
|
|
166
|
+
// Release devices immediately, including a pending permission/connect request.
|
|
167
|
+
this.reset(call);
|
|
168
|
+
if (inviting)
|
|
169
|
+
await inviting.catch(() => { });
|
|
170
|
+
if (!this.disposed && recipients.length)
|
|
171
|
+
await this.relay(call, CallSignalCancelled, recipients);
|
|
172
|
+
}
|
|
173
|
+
async microphone(enabled) {
|
|
174
|
+
const connection = this.connection;
|
|
175
|
+
if (!connection)
|
|
176
|
+
return;
|
|
177
|
+
await connection.microphone(enabled);
|
|
178
|
+
if (connection === this.connection)
|
|
179
|
+
this.publish({ microphone: enabled });
|
|
180
|
+
}
|
|
181
|
+
async camera(enabled) {
|
|
182
|
+
const connection = this.connection;
|
|
183
|
+
if (!connection || this.state.call?.mode !== CallModeVideo)
|
|
184
|
+
return;
|
|
185
|
+
await connection.camera(enabled);
|
|
186
|
+
if (connection === this.connection)
|
|
187
|
+
this.publish({ camera: enabled });
|
|
188
|
+
}
|
|
189
|
+
dispose() {
|
|
190
|
+
this.disposed = true;
|
|
191
|
+
if (this.state.call)
|
|
192
|
+
this.reset(this.state.call);
|
|
193
|
+
this.closed.clear();
|
|
194
|
+
}
|
|
195
|
+
begin(call, phase) {
|
|
196
|
+
this.controller = new AbortController();
|
|
197
|
+
this.had_remote = false;
|
|
198
|
+
this.state = {
|
|
199
|
+
...idleCallState(),
|
|
200
|
+
call,
|
|
201
|
+
phase,
|
|
202
|
+
camera: call.mode === CallModeVideo,
|
|
203
|
+
};
|
|
204
|
+
this.opts.on_change(this.state);
|
|
205
|
+
}
|
|
206
|
+
async connect(call) {
|
|
207
|
+
const signal = this.controller.signal;
|
|
208
|
+
const token = await this.opts.mint({ room_id: call.room_id });
|
|
209
|
+
if (!this.current(call))
|
|
210
|
+
return;
|
|
211
|
+
const connection = await this.opts.media.connect({
|
|
212
|
+
token,
|
|
213
|
+
room_id: call.room_id,
|
|
214
|
+
video: call.mode === CallModeVideo,
|
|
215
|
+
signal,
|
|
216
|
+
on_change: (state) => {
|
|
217
|
+
if (!this.current(call))
|
|
218
|
+
return;
|
|
219
|
+
const remote = state.participants.some((participant) => !participant.local);
|
|
220
|
+
if (remote) {
|
|
221
|
+
this.had_remote = true;
|
|
222
|
+
this.clearTimer();
|
|
223
|
+
}
|
|
224
|
+
else if (this.had_remote && !state.reconnecting) {
|
|
225
|
+
void this.end().catch(this.opts.on_error);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const local = state.participants.find((participant) => participant.local);
|
|
229
|
+
this.publish({
|
|
230
|
+
participants: state.participants,
|
|
231
|
+
...(local ? { microphone: local.audio, camera: local.video } : {}),
|
|
232
|
+
phase: state.reconnecting
|
|
233
|
+
? "reconnecting"
|
|
234
|
+
: this.had_remote
|
|
235
|
+
? "connected"
|
|
236
|
+
: this.state.phase,
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
on_disconnect: (error) => {
|
|
240
|
+
if (!this.current(call))
|
|
241
|
+
return;
|
|
242
|
+
void this.end().catch(this.opts.on_error);
|
|
243
|
+
if (error)
|
|
244
|
+
this.opts.on_error(error);
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
if (!this.current(call))
|
|
248
|
+
connection.disconnect();
|
|
249
|
+
else
|
|
250
|
+
this.connection = connection;
|
|
251
|
+
}
|
|
252
|
+
relay(call, signal, user_ids) {
|
|
253
|
+
return this.opts.notifications.relayNotification({
|
|
254
|
+
user_ids,
|
|
255
|
+
notif_k: callsNotificationKey,
|
|
256
|
+
// The relay owns and translates notification wording.
|
|
257
|
+
english_title: callNotificationTitle,
|
|
258
|
+
english_body: callNotificationBody,
|
|
259
|
+
path_custom_image: "",
|
|
260
|
+
url_custom_sound: "",
|
|
261
|
+
custom_data: {
|
|
262
|
+
[CallIDKey]: call.id,
|
|
263
|
+
[CallRoomIDKey]: call.room_id,
|
|
264
|
+
[CallModeKey]: call.mode,
|
|
265
|
+
[CallSignalKey]: signal,
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
current(call) {
|
|
270
|
+
return (!this.disposed &&
|
|
271
|
+
this.state.call === call &&
|
|
272
|
+
!this.controller?.signal.aborted);
|
|
273
|
+
}
|
|
274
|
+
publish(patch) {
|
|
275
|
+
this.state = { ...this.state, ...patch };
|
|
276
|
+
this.opts.on_change(this.state);
|
|
277
|
+
}
|
|
278
|
+
reset(call) {
|
|
279
|
+
this.remember(call.id);
|
|
280
|
+
this.clearTimer();
|
|
281
|
+
this.state = idleCallState();
|
|
282
|
+
this.controller?.abort();
|
|
283
|
+
this.controller = undefined;
|
|
284
|
+
const connection = this.connection;
|
|
285
|
+
this.connection = undefined;
|
|
286
|
+
connection?.disconnect();
|
|
287
|
+
this.recipients = [];
|
|
288
|
+
this.opts.on_change(this.state);
|
|
289
|
+
}
|
|
290
|
+
remember(id) {
|
|
291
|
+
this.closed.add(id);
|
|
292
|
+
if (this.closed.size > closedCallLimit)
|
|
293
|
+
this.closed.delete(this.closed.values().next().value);
|
|
294
|
+
}
|
|
295
|
+
armTimeout(call) {
|
|
296
|
+
this.clearTimer();
|
|
297
|
+
if (this.had_remote)
|
|
298
|
+
return;
|
|
299
|
+
this.timer = setTimeout(() => {
|
|
300
|
+
if (this.current(call))
|
|
301
|
+
void this.end().catch(this.opts.on_error);
|
|
302
|
+
}, callRingTimeoutMS);
|
|
303
|
+
}
|
|
304
|
+
clearTimer() {
|
|
305
|
+
if (this.timer)
|
|
306
|
+
clearTimeout(this.timer);
|
|
307
|
+
this.timer = undefined;
|
|
308
|
+
}
|
|
309
|
+
}
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED