@yrzhao/aves-core 0.1.0 → 0.3.0
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/README.md +157 -343
- package/dist/core/AvesClient.d.ts +45 -64
- package/dist/core/AvesClient.js +190 -142
- package/dist/core/AvesError.d.ts +39 -0
- package/dist/core/AvesError.js +42 -0
- package/dist/core/EventEmitter.d.ts +26 -24
- package/dist/core/EventEmitter.js +12 -23
- package/dist/core/SignalingClient.d.ts +30 -7
- package/dist/core/SignalingClient.js +177 -41
- package/dist/core/WebRTCManager.d.ts +152 -39
- package/dist/core/WebRTCManager.js +409 -196
- package/dist/core/webrtc/channels.d.ts +10 -0
- package/dist/core/webrtc/channels.js +25 -0
- package/dist/core/webrtc/fileTransfer.d.ts +84 -0
- package/dist/core/webrtc/fileTransfer.js +399 -0
- package/dist/core/webrtc/mediaTracks.d.ts +53 -0
- package/dist/core/webrtc/mediaTracks.js +385 -0
- package/dist/core/webrtc/messages.d.ts +4 -0
- package/dist/core/webrtc/messages.js +53 -0
- package/dist/esm/core/AvesClient.d.ts +45 -64
- package/dist/esm/core/AvesClient.js +193 -145
- package/dist/esm/core/AvesError.d.ts +39 -0
- package/dist/esm/core/AvesError.js +38 -0
- package/dist/esm/core/EventEmitter.d.ts +26 -24
- package/dist/esm/core/EventEmitter.js +12 -23
- package/dist/esm/core/SignalingClient.d.ts +30 -7
- package/dist/esm/core/SignalingClient.js +178 -42
- package/dist/esm/core/WebRTCManager.d.ts +152 -39
- package/dist/esm/core/WebRTCManager.js +409 -196
- package/dist/esm/core/webrtc/channels.d.ts +10 -0
- package/dist/esm/core/webrtc/channels.js +19 -0
- package/dist/esm/core/webrtc/fileTransfer.d.ts +84 -0
- package/dist/esm/core/webrtc/fileTransfer.js +393 -0
- package/dist/esm/core/webrtc/mediaTracks.d.ts +53 -0
- package/dist/esm/core/webrtc/mediaTracks.js +381 -0
- package/dist/esm/core/webrtc/messages.d.ts +4 -0
- package/dist/esm/core/webrtc/messages.js +48 -0
- package/dist/esm/index.d.ts +4 -1
- package/dist/esm/index.js +5 -4
- package/dist/esm/package.json +3 -0
- package/dist/esm/types/types.d.ts +84 -4
- package/dist/index.cjs +3 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +3 -1
- package/dist/types/types.d.ts +84 -4
- package/package.json +4 -3
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MediaTrackManager = void 0;
|
|
4
|
+
const AvesError_1 = require("../AvesError");
|
|
5
|
+
class MediaTrackManager {
|
|
6
|
+
constructor(peerConnections, getActivePeers, videoConstraints = {}) {
|
|
7
|
+
this.peerConnections = peerConnections;
|
|
8
|
+
this.getActivePeers = getActivePeers;
|
|
9
|
+
this.audioSenders = new Map();
|
|
10
|
+
this.remoteAudioStreams = new Map();
|
|
11
|
+
this.videoSenders = new Map();
|
|
12
|
+
this.remoteVideoStreams = new Map();
|
|
13
|
+
this.localAudioStream = null;
|
|
14
|
+
this.localAudioTrack = null;
|
|
15
|
+
this.isMuted = false;
|
|
16
|
+
this.localVideoStream = null;
|
|
17
|
+
this.localVideoTrack = null;
|
|
18
|
+
this.isVideoMuted = false;
|
|
19
|
+
this.screenShareStream = null;
|
|
20
|
+
this.screenShareTrack = null;
|
|
21
|
+
this.screenShareActive = false;
|
|
22
|
+
this.cameraTrackBeforeShare = null;
|
|
23
|
+
this.remoteAudioTrackCallbacks = new Set();
|
|
24
|
+
this.localAudioStateCallbacks = new Set();
|
|
25
|
+
this.remoteVideoTrackCallbacks = new Set();
|
|
26
|
+
this.localVideoStateCallbacks = new Set();
|
|
27
|
+
this.screenShareStateCallbacks = new Set();
|
|
28
|
+
this.videoConstraints = videoConstraints;
|
|
29
|
+
}
|
|
30
|
+
bindPeerConnection(peerId, pc) {
|
|
31
|
+
pc.ontrack = (event) => {
|
|
32
|
+
const stream = event.streams[0] ??
|
|
33
|
+
(typeof MediaStream !== "undefined"
|
|
34
|
+
? new MediaStream([event.track])
|
|
35
|
+
: null);
|
|
36
|
+
if (event.track.kind === "audio") {
|
|
37
|
+
this.remoteAudioStreams.set(peerId, stream);
|
|
38
|
+
this.remoteAudioTrackCallbacks.forEach((callback) => callback(peerId, stream, event.track));
|
|
39
|
+
}
|
|
40
|
+
else if (event.track.kind === "video") {
|
|
41
|
+
const existingStream = this.remoteVideoStreams.get(peerId);
|
|
42
|
+
if (existingStream) {
|
|
43
|
+
existingStream.getVideoTracks().forEach((track) => track.stop());
|
|
44
|
+
existingStream.addTrack(event.track);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.remoteVideoStreams.set(peerId, stream);
|
|
48
|
+
}
|
|
49
|
+
this.remoteVideoTrackCallbacks.forEach((callback) => callback(peerId, stream, event.track));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
this.audioSenders.set(peerId, this.createTransceiverSender(pc, "audio"));
|
|
53
|
+
this.videoSenders.set(peerId, this.createTransceiverSender(pc, "video"));
|
|
54
|
+
void this.syncLocalAudioTrack(peerId);
|
|
55
|
+
}
|
|
56
|
+
async startVoice() {
|
|
57
|
+
if (this.localAudioStream && this.localAudioTrack) {
|
|
58
|
+
this.emitLocalAudioState();
|
|
59
|
+
return this.localAudioStream;
|
|
60
|
+
}
|
|
61
|
+
if (typeof navigator === "undefined" ||
|
|
62
|
+
!navigator.mediaDevices ||
|
|
63
|
+
typeof navigator.mediaDevices.getUserMedia !== "function") {
|
|
64
|
+
throw new AvesError_1.AvesError({
|
|
65
|
+
message: "Audio capture is not available in this environment",
|
|
66
|
+
code: "MEDIA_NOT_AVAILABLE",
|
|
67
|
+
stage: "transport",
|
|
68
|
+
retryable: false,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
72
|
+
const [track] = stream.getAudioTracks();
|
|
73
|
+
if (!track) {
|
|
74
|
+
throw new AvesError_1.AvesError({
|
|
75
|
+
message: "No audio track available from getUserMedia",
|
|
76
|
+
code: "MEDIA_CAPTURE_FAILED",
|
|
77
|
+
stage: "transport",
|
|
78
|
+
retryable: false,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
track.enabled = !this.isMuted;
|
|
82
|
+
this.localAudioStream = stream;
|
|
83
|
+
this.localAudioTrack = track;
|
|
84
|
+
await Promise.all(this.getActivePeers().map((peerId) => this.syncLocalAudioTrack(peerId)));
|
|
85
|
+
this.emitLocalAudioState();
|
|
86
|
+
return stream;
|
|
87
|
+
}
|
|
88
|
+
stopVoice() {
|
|
89
|
+
if (this.localAudioTrack) {
|
|
90
|
+
this.audioSenders.forEach((sender) => {
|
|
91
|
+
if (sender && typeof sender.replaceTrack === "function") {
|
|
92
|
+
void sender.replaceTrack(null);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
this.localAudioTrack.stop();
|
|
96
|
+
}
|
|
97
|
+
if (this.localAudioStream) {
|
|
98
|
+
this.localAudioStream.getTracks().forEach((track) => {
|
|
99
|
+
if (track.readyState !== "ended") {
|
|
100
|
+
track.stop();
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
this.localAudioTrack = null;
|
|
105
|
+
this.localAudioStream = null;
|
|
106
|
+
this.emitLocalAudioState();
|
|
107
|
+
}
|
|
108
|
+
setMuted(muted) {
|
|
109
|
+
this.isMuted = muted;
|
|
110
|
+
if (this.localAudioTrack) {
|
|
111
|
+
this.localAudioTrack.enabled = !muted;
|
|
112
|
+
}
|
|
113
|
+
this.emitLocalAudioState();
|
|
114
|
+
}
|
|
115
|
+
getLocalAudioState() {
|
|
116
|
+
return {
|
|
117
|
+
active: !!this.localAudioTrack,
|
|
118
|
+
muted: this.isMuted,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
getRemoteAudioStream(peerId) {
|
|
122
|
+
return this.remoteAudioStreams.get(peerId) ?? null;
|
|
123
|
+
}
|
|
124
|
+
async startVideo(constraints) {
|
|
125
|
+
if (this.localVideoStream && this.localVideoTrack) {
|
|
126
|
+
this.emitLocalVideoState();
|
|
127
|
+
return this.localVideoStream;
|
|
128
|
+
}
|
|
129
|
+
if (typeof navigator === "undefined" ||
|
|
130
|
+
!navigator.mediaDevices ||
|
|
131
|
+
typeof navigator.mediaDevices.getUserMedia !== "function") {
|
|
132
|
+
throw new AvesError_1.AvesError({
|
|
133
|
+
message: "Video capture is not available in this environment",
|
|
134
|
+
code: "MEDIA_NOT_AVAILABLE",
|
|
135
|
+
stage: "transport",
|
|
136
|
+
retryable: false,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const mergedConstraints = { ...this.videoConstraints, ...constraints };
|
|
140
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
141
|
+
video: mergedConstraints,
|
|
142
|
+
});
|
|
143
|
+
const [track] = stream.getVideoTracks();
|
|
144
|
+
if (!track) {
|
|
145
|
+
throw new AvesError_1.AvesError({
|
|
146
|
+
message: "No video track available from getUserMedia",
|
|
147
|
+
code: "MEDIA_CAPTURE_FAILED",
|
|
148
|
+
stage: "transport",
|
|
149
|
+
retryable: false,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
track.enabled = !this.isVideoMuted;
|
|
153
|
+
this.localVideoStream = stream;
|
|
154
|
+
this.localVideoTrack = track;
|
|
155
|
+
if (!this.screenShareActive) {
|
|
156
|
+
await Promise.all(this.getActivePeers().map((peerId) => this.syncLocalVideoTrack(peerId)));
|
|
157
|
+
}
|
|
158
|
+
this.emitLocalVideoState();
|
|
159
|
+
return stream;
|
|
160
|
+
}
|
|
161
|
+
stopVideo() {
|
|
162
|
+
if (this.localVideoTrack) {
|
|
163
|
+
if (!this.screenShareActive) {
|
|
164
|
+
this.videoSenders.forEach((sender) => {
|
|
165
|
+
if (sender && typeof sender.replaceTrack === "function") {
|
|
166
|
+
void sender.replaceTrack(null);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
this.localVideoTrack.stop();
|
|
171
|
+
}
|
|
172
|
+
if (this.localVideoStream) {
|
|
173
|
+
this.localVideoStream.getTracks().forEach((track) => {
|
|
174
|
+
if (track.readyState !== "ended") {
|
|
175
|
+
track.stop();
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
this.localVideoTrack = null;
|
|
180
|
+
this.localVideoStream = null;
|
|
181
|
+
this.cameraTrackBeforeShare = null;
|
|
182
|
+
this.emitLocalVideoState();
|
|
183
|
+
}
|
|
184
|
+
setVideoMuted(muted) {
|
|
185
|
+
this.isVideoMuted = muted;
|
|
186
|
+
if (this.localVideoTrack) {
|
|
187
|
+
this.localVideoTrack.enabled = !muted;
|
|
188
|
+
}
|
|
189
|
+
this.emitLocalVideoState();
|
|
190
|
+
}
|
|
191
|
+
getLocalVideoState() {
|
|
192
|
+
return {
|
|
193
|
+
active: !!this.localVideoTrack,
|
|
194
|
+
muted: this.isVideoMuted,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
getRemoteVideoStream(peerId) {
|
|
198
|
+
return this.remoteVideoStreams.get(peerId) ?? null;
|
|
199
|
+
}
|
|
200
|
+
async startScreenShare() {
|
|
201
|
+
if (this.screenShareActive && this.screenShareStream) {
|
|
202
|
+
this.emitScreenShareState();
|
|
203
|
+
return this.screenShareStream;
|
|
204
|
+
}
|
|
205
|
+
if (typeof navigator === "undefined" ||
|
|
206
|
+
!navigator.mediaDevices ||
|
|
207
|
+
typeof navigator.mediaDevices.getDisplayMedia !== "function") {
|
|
208
|
+
throw new AvesError_1.AvesError({
|
|
209
|
+
message: "Screen sharing is not available in this environment",
|
|
210
|
+
code: "MEDIA_NOT_AVAILABLE",
|
|
211
|
+
stage: "transport",
|
|
212
|
+
retryable: false,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
const stream = await navigator.mediaDevices.getDisplayMedia({
|
|
216
|
+
video: true,
|
|
217
|
+
});
|
|
218
|
+
const [track] = stream.getVideoTracks();
|
|
219
|
+
if (!track) {
|
|
220
|
+
throw new AvesError_1.AvesError({
|
|
221
|
+
message: "No video track available from getDisplayMedia",
|
|
222
|
+
code: "MEDIA_CAPTURE_FAILED",
|
|
223
|
+
stage: "transport",
|
|
224
|
+
retryable: false,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
this.cameraTrackBeforeShare = this.localVideoTrack;
|
|
228
|
+
this.screenShareStream = stream;
|
|
229
|
+
this.screenShareTrack = track;
|
|
230
|
+
this.screenShareActive = true;
|
|
231
|
+
await Promise.all(this.getActivePeers().map(async (peerId) => {
|
|
232
|
+
const sender = this.videoSenders.get(peerId);
|
|
233
|
+
if (sender && typeof sender.replaceTrack === "function") {
|
|
234
|
+
await sender.replaceTrack(track);
|
|
235
|
+
}
|
|
236
|
+
}));
|
|
237
|
+
track.onended = () => {
|
|
238
|
+
this.stopScreenShare();
|
|
239
|
+
};
|
|
240
|
+
this.emitScreenShareState();
|
|
241
|
+
return stream;
|
|
242
|
+
}
|
|
243
|
+
stopScreenShare() {
|
|
244
|
+
if (!this.screenShareActive) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (this.screenShareTrack) {
|
|
248
|
+
this.screenShareTrack.stop();
|
|
249
|
+
}
|
|
250
|
+
if (this.screenShareStream) {
|
|
251
|
+
this.screenShareStream.getTracks().forEach((track) => {
|
|
252
|
+
if (track.readyState !== "ended") {
|
|
253
|
+
track.stop();
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
this.screenShareStream = null;
|
|
258
|
+
this.screenShareTrack = null;
|
|
259
|
+
this.screenShareActive = false;
|
|
260
|
+
const restoreTrack = this.cameraTrackBeforeShare;
|
|
261
|
+
this.cameraTrackBeforeShare = null;
|
|
262
|
+
if (restoreTrack && restoreTrack.readyState === "live") {
|
|
263
|
+
this.localVideoTrack = restoreTrack;
|
|
264
|
+
void Promise.all(this.getActivePeers().map(async (peerId) => {
|
|
265
|
+
const sender = this.videoSenders.get(peerId);
|
|
266
|
+
if (sender && typeof sender.replaceTrack === "function") {
|
|
267
|
+
await sender.replaceTrack(restoreTrack);
|
|
268
|
+
}
|
|
269
|
+
}));
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
void Promise.all(this.getActivePeers().map(async (peerId) => {
|
|
273
|
+
const sender = this.videoSenders.get(peerId);
|
|
274
|
+
if (sender && typeof sender.replaceTrack === "function") {
|
|
275
|
+
await sender.replaceTrack(null);
|
|
276
|
+
}
|
|
277
|
+
}));
|
|
278
|
+
}
|
|
279
|
+
this.emitScreenShareState();
|
|
280
|
+
this.emitLocalVideoState();
|
|
281
|
+
}
|
|
282
|
+
getScreenShareState() {
|
|
283
|
+
return {
|
|
284
|
+
active: this.screenShareActive,
|
|
285
|
+
source: this.screenShareActive ? "screen" : "camera",
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
onRemoteAudioTrack(callback) {
|
|
289
|
+
this.remoteAudioTrackCallbacks.add(callback);
|
|
290
|
+
}
|
|
291
|
+
onLocalAudioStateChange(callback) {
|
|
292
|
+
this.localAudioStateCallbacks.add(callback);
|
|
293
|
+
}
|
|
294
|
+
onRemoteVideoTrack(callback) {
|
|
295
|
+
this.remoteVideoTrackCallbacks.add(callback);
|
|
296
|
+
}
|
|
297
|
+
onLocalVideoStateChange(callback) {
|
|
298
|
+
this.localVideoStateCallbacks.add(callback);
|
|
299
|
+
}
|
|
300
|
+
onScreenShareStateChange(callback) {
|
|
301
|
+
this.screenShareStateCallbacks.add(callback);
|
|
302
|
+
}
|
|
303
|
+
closePeer(peerId) {
|
|
304
|
+
this.audioSenders.delete(peerId);
|
|
305
|
+
this.remoteAudioStreams.delete(peerId);
|
|
306
|
+
this.videoSenders.delete(peerId);
|
|
307
|
+
this.remoteVideoStreams.delete(peerId);
|
|
308
|
+
}
|
|
309
|
+
clearCallbacks() {
|
|
310
|
+
this.remoteAudioTrackCallbacks.clear();
|
|
311
|
+
this.localAudioStateCallbacks.clear();
|
|
312
|
+
this.remoteVideoTrackCallbacks.clear();
|
|
313
|
+
this.localVideoStateCallbacks.clear();
|
|
314
|
+
this.screenShareStateCallbacks.clear();
|
|
315
|
+
}
|
|
316
|
+
createTransceiverSender(pc, kind) {
|
|
317
|
+
const capable = pc;
|
|
318
|
+
if (typeof capable.addTransceiver === "function") {
|
|
319
|
+
const transceiver = capable.addTransceiver(kind, {
|
|
320
|
+
direction: "sendrecv",
|
|
321
|
+
});
|
|
322
|
+
return transceiver.sender;
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
async syncLocalAudioTrack(peerId) {
|
|
327
|
+
const pc = this.peerConnections.get(peerId);
|
|
328
|
+
if (!pc) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
let sender = this.audioSenders.get(peerId) ?? null;
|
|
332
|
+
if (sender && typeof sender.replaceTrack === "function") {
|
|
333
|
+
await sender.replaceTrack(this.localAudioTrack);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const addTrackCapable = pc;
|
|
337
|
+
if (this.localAudioTrack &&
|
|
338
|
+
typeof addTrackCapable.addTrack === "function") {
|
|
339
|
+
const stream = this.localAudioStream ??
|
|
340
|
+
(typeof MediaStream !== "undefined"
|
|
341
|
+
? new MediaStream([this.localAudioTrack])
|
|
342
|
+
: undefined);
|
|
343
|
+
sender = stream
|
|
344
|
+
? addTrackCapable.addTrack(this.localAudioTrack, stream)
|
|
345
|
+
: addTrackCapable.addTrack(this.localAudioTrack);
|
|
346
|
+
this.audioSenders.set(peerId, sender);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
async syncLocalVideoTrack(peerId) {
|
|
350
|
+
const pc = this.peerConnections.get(peerId);
|
|
351
|
+
if (!pc) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
let sender = this.videoSenders.get(peerId) ?? null;
|
|
355
|
+
if (sender && typeof sender.replaceTrack === "function") {
|
|
356
|
+
await sender.replaceTrack(this.localVideoTrack);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const addTrackCapable = pc;
|
|
360
|
+
if (this.localVideoTrack &&
|
|
361
|
+
typeof addTrackCapable.addTrack === "function") {
|
|
362
|
+
const stream = this.localVideoStream ??
|
|
363
|
+
(typeof MediaStream !== "undefined"
|
|
364
|
+
? new MediaStream([this.localVideoTrack])
|
|
365
|
+
: undefined);
|
|
366
|
+
sender = stream
|
|
367
|
+
? addTrackCapable.addTrack(this.localVideoTrack, stream)
|
|
368
|
+
: addTrackCapable.addTrack(this.localVideoTrack);
|
|
369
|
+
this.videoSenders.set(peerId, sender);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
emitLocalAudioState() {
|
|
373
|
+
const state = this.getLocalAudioState();
|
|
374
|
+
this.localAudioStateCallbacks.forEach((callback) => callback(state));
|
|
375
|
+
}
|
|
376
|
+
emitLocalVideoState() {
|
|
377
|
+
const state = this.getLocalVideoState();
|
|
378
|
+
this.localVideoStateCallbacks.forEach((callback) => callback(state));
|
|
379
|
+
}
|
|
380
|
+
emitScreenShareState() {
|
|
381
|
+
const state = this.getScreenShareState();
|
|
382
|
+
this.screenShareStateCallbacks.forEach((callback) => callback(state));
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
exports.MediaTrackManager = MediaTrackManager;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AvesMessage } from "../../types/types";
|
|
2
|
+
export declare function errorMessage(error: unknown): string;
|
|
3
|
+
export declare function isJsonSerializableValue(value: unknown, seen?: WeakSet<object>): value is AvesMessage;
|
|
4
|
+
export declare function serializeUserMessage(message: AvesMessage): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorMessage = errorMessage;
|
|
4
|
+
exports.isJsonSerializableValue = isJsonSerializableValue;
|
|
5
|
+
exports.serializeUserMessage = serializeUserMessage;
|
|
6
|
+
const AvesError_1 = require("../AvesError");
|
|
7
|
+
function errorMessage(error) {
|
|
8
|
+
return error instanceof Error ? error.message : String(error);
|
|
9
|
+
}
|
|
10
|
+
function isJsonSerializableValue(value, seen = new WeakSet()) {
|
|
11
|
+
if (value === null) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
switch (typeof value) {
|
|
15
|
+
case "string":
|
|
16
|
+
case "boolean":
|
|
17
|
+
return true;
|
|
18
|
+
case "number":
|
|
19
|
+
return Number.isFinite(value);
|
|
20
|
+
case "object": {
|
|
21
|
+
if (seen.has(value)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
seen.add(value);
|
|
25
|
+
if (Array.isArray(value)) {
|
|
26
|
+
const isSerializable = value.every((item) => isJsonSerializableValue(item, seen));
|
|
27
|
+
seen.delete(value);
|
|
28
|
+
return isSerializable;
|
|
29
|
+
}
|
|
30
|
+
const prototype = Object.getPrototypeOf(value);
|
|
31
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
32
|
+
seen.delete(value);
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const isSerializable = Object.values(value).every((item) => isJsonSerializableValue(item, seen));
|
|
36
|
+
seen.delete(value);
|
|
37
|
+
return isSerializable;
|
|
38
|
+
}
|
|
39
|
+
default:
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function serializeUserMessage(message) {
|
|
44
|
+
if (!isJsonSerializableValue(message)) {
|
|
45
|
+
throw new AvesError_1.AvesError({
|
|
46
|
+
message: "Message must be JSON-serializable: use finite numbers, strings, booleans, null, arrays, and plain objects only",
|
|
47
|
+
code: "MESSAGE_SERIALIZE_FAILED",
|
|
48
|
+
stage: "transport",
|
|
49
|
+
retryable: false,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return JSON.stringify(message);
|
|
53
|
+
}
|
|
@@ -1,84 +1,65 @@
|
|
|
1
1
|
import { EventEmitter } from "./EventEmitter";
|
|
2
|
-
import {
|
|
2
|
+
import { AvesError } from "./AvesError";
|
|
3
|
+
import { AvesClientConfig, AvesMessage, AvesVideoConstraints, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, Participant, ScreenShareState } from "../types/types";
|
|
4
|
+
export type AvesClientEvents = {
|
|
5
|
+
signalingStateChange: [state: string];
|
|
6
|
+
error: [error: AvesError];
|
|
7
|
+
userJoined: [user: Participant];
|
|
8
|
+
userLeft: [userId: string];
|
|
9
|
+
connectionStateChange: [peerId: string, state: RTCPeerConnectionState];
|
|
10
|
+
dataChannelStateChange: [peerId: string, state: RTCDataChannelState];
|
|
11
|
+
message: [peerId: string, message: AvesMessage];
|
|
12
|
+
remoteAudioTrack: [peerId: string, stream: MediaStream, track: MediaStreamTrack];
|
|
13
|
+
remoteVideoTrack: [peerId: string, stream: MediaStream, track: MediaStreamTrack];
|
|
14
|
+
localAudioStateChange: [state: LocalAudioState];
|
|
15
|
+
localVideoStateChange: [state: LocalVideoState];
|
|
16
|
+
screenShareStateChange: [state: ScreenShareState];
|
|
17
|
+
fileTransferStarted: [peerId: string, info: FileTransferInfo];
|
|
18
|
+
fileTransferProgress: [peerId: string, progress: FileTransferProgress];
|
|
19
|
+
fileTransferCompleted: [peerId: string, result: FileTransferResult];
|
|
20
|
+
fileTransferFailed: [peerId: string, info: FileTransferInfo | null, error: AvesError];
|
|
21
|
+
};
|
|
3
22
|
/**
|
|
4
|
-
* AvesClient - Main client class for WebRTC communication
|
|
5
|
-
*
|
|
6
|
-
* This class coordinates WebRTCManager and SignalingClient to provide
|
|
7
|
-
* a simple API for WebRTC-based real-time communication.
|
|
8
|
-
*
|
|
9
|
-
* Features:
|
|
10
|
-
* - Room management (create, join, leave)
|
|
11
|
-
* - Automatic WebRTC connection establishment
|
|
12
|
-
* - Message sending and receiving
|
|
13
|
-
* - Connection state monitoring
|
|
14
|
-
* - Automatic reconnection
|
|
23
|
+
* AvesClient - Main client class for WebRTC communication.
|
|
15
24
|
*/
|
|
16
|
-
export declare class AvesClient extends EventEmitter {
|
|
25
|
+
export declare class AvesClient extends EventEmitter<AvesClientEvents> {
|
|
17
26
|
private config;
|
|
18
27
|
private webrtcManager;
|
|
19
28
|
private signalingClient;
|
|
20
29
|
private currentRoomId;
|
|
21
30
|
private currentUserId;
|
|
31
|
+
private currentUserName;
|
|
22
32
|
private participants;
|
|
33
|
+
private preparedPeers;
|
|
34
|
+
private shouldRestoreSession;
|
|
23
35
|
constructor(config: AvesClientConfig);
|
|
24
|
-
/**
|
|
25
|
-
* Setup event handlers to coordinate between components
|
|
26
|
-
*/
|
|
27
36
|
private setupEventHandlers;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*/
|
|
37
|
+
private restoreRoomSession;
|
|
38
|
+
private preparePeerConnection;
|
|
31
39
|
private initiateConnection;
|
|
32
|
-
/**
|
|
33
|
-
* Create a new room
|
|
34
|
-
* @returns Promise that resolves with the room ID
|
|
35
|
-
*/
|
|
36
40
|
createRoom(): Promise<string>;
|
|
37
|
-
|
|
38
|
-
* Join an existing room
|
|
39
|
-
* @param roomId - Room ID to join
|
|
40
|
-
* @param userId - User ID
|
|
41
|
-
* @param userName - User name
|
|
42
|
-
* @returns Promise that resolves with the list of current participants
|
|
43
|
-
*/
|
|
41
|
+
joinRoom(roomId: string, userName: string): Promise<Participant[]>;
|
|
44
42
|
joinRoom(roomId: string, userId: string, userName: string): Promise<Participant[]>;
|
|
45
|
-
/**
|
|
46
|
-
* Leave the current room
|
|
47
|
-
* Disconnects from signaling server and closes all WebRTC connections
|
|
48
|
-
*/
|
|
49
43
|
leaveRoom(): Promise<void>;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
* @returns Connection state or 'closed' if peer not found
|
|
67
|
-
*/
|
|
44
|
+
sendMessage(message: AvesMessage): void;
|
|
45
|
+
sendMessageToPeer(peerId: string, message: AvesMessage): void;
|
|
46
|
+
sendFile(blob: Blob, options?: FileTransferOptions): Promise<FileTransferInfo[]>;
|
|
47
|
+
startVoice(): Promise<MediaStream>;
|
|
48
|
+
stopVoice(): void;
|
|
49
|
+
setMuted(muted: boolean): void;
|
|
50
|
+
getLocalAudioState(): LocalAudioState;
|
|
51
|
+
getRemoteAudioStream(peerId: string): MediaStream | null;
|
|
52
|
+
startVideo(constraints?: AvesVideoConstraints): Promise<MediaStream>;
|
|
53
|
+
stopVideo(): void;
|
|
54
|
+
setVideoMuted(muted: boolean): void;
|
|
55
|
+
getLocalVideoState(): LocalVideoState;
|
|
56
|
+
getRemoteVideoStream(peerId: string): MediaStream | null;
|
|
57
|
+
startScreenShare(): Promise<MediaStream>;
|
|
58
|
+
stopScreenShare(): void;
|
|
59
|
+
getScreenShareState(): ScreenShareState;
|
|
68
60
|
getConnectionState(peerId: string): RTCPeerConnectionState;
|
|
69
|
-
/**
|
|
70
|
-
* Get the list of current participants in the room
|
|
71
|
-
* @returns Array of participants
|
|
72
|
-
*/
|
|
73
61
|
getParticipants(): Participant[];
|
|
74
|
-
|
|
75
|
-
* Check if connected to the signaling server
|
|
76
|
-
* @returns true if connected, false otherwise
|
|
77
|
-
*/
|
|
62
|
+
getCurrentUserId(): string | null;
|
|
78
63
|
isConnected(): boolean;
|
|
79
|
-
/**
|
|
80
|
-
* Destroy the client and clean up all resources
|
|
81
|
-
* Closes all connections and removes all event listeners
|
|
82
|
-
*/
|
|
83
64
|
destroy(): void;
|
|
84
65
|
}
|