@yrzhao/aves-core 0.3.0 → 1.0.1
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 +58 -175
- package/dist/core/AvesClient.js +1 -1
- package/dist/core/WebRTCManager.d.ts +6 -0
- package/dist/core/WebRTCManager.js +19 -2
- package/dist/core/webrtc/mediaTracks.d.ts +2 -0
- package/dist/core/webrtc/mediaTracks.js +24 -6
- package/dist/esm/core/AvesClient.js +1 -1
- package/dist/esm/core/WebRTCManager.d.ts +6 -0
- package/dist/esm/core/WebRTCManager.js +19 -2
- package/dist/esm/core/webrtc/mediaTracks.d.ts +2 -0
- package/dist/esm/core/webrtc/mediaTracks.js +24 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
1
|
# Aves Core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
轻量级 WebRTC 客户端库,零运行时依赖。支持消息、文件传输、语音、视频、屏幕共享,基于全网状 P2P 拓扑直连。
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## 特性
|
|
8
|
-
|
|
9
|
-
- 基于房间的通信模型,全网状 P2P 拓扑
|
|
10
|
-
- 文本消息广播与单播(DataChannel 直传,不经服务器)
|
|
11
|
-
- 文件传输:分块二进制传输,握手协议,进度追踪
|
|
12
|
-
- 实时语音 / 视频:getUserMedia + RTP transceiver,静音和视频开关控制
|
|
13
|
-
- 屏幕共享:getDisplayMedia,支持结束后恢复摄像头视频轨道
|
|
14
|
-
- 自动断线重连与会话恢复
|
|
15
|
-
- 内置 WebSocket 信令客户端,request/response 关联
|
|
16
|
-
- 结构化 `AvesError`,错误码、阶段、重试语义和上下文可编程处理
|
|
17
|
-
- 事件驱动架构,完整 TypeScript 类型定义
|
|
18
|
-
- 零运行时依赖
|
|
19
|
-
- CJS / ESM / TypeScript types 发布入口
|
|
5
|
+
版本:`0.3.0`
|
|
20
6
|
|
|
21
7
|
## 安装
|
|
22
8
|
|
|
@@ -29,214 +15,111 @@ npm install @yrzhao/aves-core
|
|
|
29
15
|
```typescript
|
|
30
16
|
import { AvesClient } from "@yrzhao/aves-core";
|
|
31
17
|
|
|
32
|
-
const client = new AvesClient({
|
|
33
|
-
signalingUrl: "ws://localhost:8080",
|
|
34
|
-
});
|
|
18
|
+
const client = new AvesClient({ signalingUrl: "ws://localhost:8080" });
|
|
35
19
|
|
|
36
|
-
|
|
37
|
-
client.on("
|
|
38
|
-
console.log(`来自 ${peerId}:`, message);
|
|
39
|
-
});
|
|
20
|
+
client.on("message", (peerId, message) => console.log(`${peerId}:`, message));
|
|
21
|
+
client.on("userJoined", (p) => console.log(`${p.name} 加入了`));
|
|
40
22
|
|
|
41
|
-
client.on("userJoined", (participant) => {
|
|
42
|
-
console.log(`${participant.name} 加入了房间`);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// 创建并加入房间
|
|
46
23
|
const roomId = await client.createRoom();
|
|
47
|
-
|
|
24
|
+
await client.joinRoom(roomId, "Alice");
|
|
48
25
|
|
|
49
|
-
// 发送消息
|
|
50
26
|
client.sendMessage({ text: "大家好!" });
|
|
51
|
-
|
|
52
|
-
// 离开房间
|
|
53
27
|
await client.leaveRoom();
|
|
54
28
|
```
|
|
55
29
|
|
|
56
|
-
##
|
|
30
|
+
## 核心 API
|
|
31
|
+
|
|
32
|
+
### 房间管理
|
|
33
|
+
|
|
34
|
+
| 方法 | 返回 | 说明 |
|
|
35
|
+
|------|------|------|
|
|
36
|
+
| `createRoom()` | `Promise<string>` | 创建房间 |
|
|
37
|
+
| `joinRoom(roomId, name)` | `Promise<Participant[]>` | 加入房间 |
|
|
38
|
+
| `leaveRoom()` | `Promise<void>` | 离开 |
|
|
39
|
+
| `destroy()` | `void` | 销毁客户端 |
|
|
40
|
+
|
|
41
|
+
### 消息
|
|
57
42
|
|
|
58
43
|
```typescript
|
|
59
|
-
|
|
44
|
+
client.sendMessage({ text: "hi" }); // 广播
|
|
45
|
+
client.sendMessageToPeer(peerId, { text: "hi" }); // 单播
|
|
46
|
+
```
|
|
60
47
|
|
|
61
|
-
|
|
48
|
+
### 文件传输
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
// 发送(广播)
|
|
62
52
|
await client.sendFile(fileBlob, { fileName: "photo.jpg" });
|
|
63
53
|
|
|
64
|
-
//
|
|
65
|
-
await client.sendFile(fileBlob, { fileName: "doc.pdf", peerId:
|
|
54
|
+
// 发送(单播)
|
|
55
|
+
await client.sendFile(fileBlob, { fileName: "doc.pdf", peerId: targetId });
|
|
66
56
|
|
|
67
|
-
//
|
|
68
|
-
client.on("fileTransferProgress", (
|
|
69
|
-
console.log(`${
|
|
57
|
+
// 监听
|
|
58
|
+
client.on("fileTransferProgress", (p, pgr) => {
|
|
59
|
+
console.log(`${p}: ${Math.round(pgr.progress * 100)}%`);
|
|
70
60
|
});
|
|
71
|
-
|
|
72
|
-
// 接收文件
|
|
73
|
-
client.on("fileTransferCompleted", (peerId: string, result: FileTransferResult) => {
|
|
74
|
-
// result.blob: Blob, result.name: string
|
|
75
|
-
if (!result.blob) return;
|
|
61
|
+
client.on("fileTransferCompleted", (_, result) => {
|
|
76
62
|
const url = URL.createObjectURL(result.blob);
|
|
77
|
-
// 用于下载或显示
|
|
78
63
|
});
|
|
79
64
|
```
|
|
80
65
|
|
|
81
|
-
|
|
66
|
+
### 语音
|
|
82
67
|
|
|
83
68
|
```typescript
|
|
84
|
-
// 开启语音(获取麦克风,发送给所有对等端)
|
|
85
69
|
await client.startVoice();
|
|
86
|
-
|
|
87
|
-
// 静音/取消静音
|
|
88
70
|
client.setMuted(true);
|
|
89
|
-
|
|
90
|
-
// 获取本地音频状态
|
|
91
|
-
const state = client.getLocalAudioState();
|
|
92
|
-
// { active: boolean, muted: boolean }
|
|
93
|
-
|
|
94
|
-
// 获取远端音频流
|
|
95
|
-
const remoteStream = client.getRemoteAudioStream("peer-123");
|
|
96
|
-
if (remoteStream) {
|
|
97
|
-
const audio = new Audio();
|
|
98
|
-
audio.srcObject = remoteStream;
|
|
99
|
-
audio.play();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 关闭语音
|
|
103
71
|
client.stopVoice();
|
|
104
72
|
```
|
|
105
73
|
|
|
106
|
-
|
|
74
|
+
### 视频
|
|
107
75
|
|
|
108
76
|
```typescript
|
|
109
|
-
// 开启摄像头视频
|
|
110
77
|
await client.startVideo({ width: 1280, height: 720, frameRate: 30 });
|
|
111
|
-
|
|
112
|
-
// 暂停/恢复本地视频轨道
|
|
113
78
|
client.setVideoMuted(true);
|
|
114
|
-
|
|
115
|
-
// 获取本地视频状态
|
|
116
|
-
const videoState = client.getLocalVideoState();
|
|
117
|
-
// { active: boolean, muted: boolean }
|
|
118
|
-
|
|
119
|
-
// 获取远端视频流
|
|
120
|
-
const remoteVideo = client.getRemoteVideoStream("peer-123");
|
|
121
|
-
|
|
122
|
-
// 开始屏幕共享;如果摄像头已开启,停止共享后会恢复摄像头轨道
|
|
123
|
-
await client.startScreenShare();
|
|
124
|
-
client.stopScreenShare();
|
|
79
|
+
client.stopVideo();
|
|
125
80
|
```
|
|
126
81
|
|
|
127
|
-
|
|
82
|
+
### 屏幕共享
|
|
128
83
|
|
|
129
84
|
```typescript
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
iceServers?: RTCIceServer[]; // STUN/TURN 服务器
|
|
133
|
-
fileChunkSize?: number; // 默认文件分块大小 bytes(默认:65536)
|
|
134
|
-
video?: AvesVideoConstraints; // 默认视频约束
|
|
135
|
-
reconnect?: {
|
|
136
|
-
maxAttempts?: number; // 最大重连次数(默认:5)
|
|
137
|
-
delay?: number; // 重连延迟 ms(默认:3000)
|
|
138
|
-
requestTimeoutMs?: number; // 房间请求超时 ms(默认:30000)
|
|
139
|
-
};
|
|
140
|
-
debug?: boolean; // 调试日志(默认:false)
|
|
141
|
-
}
|
|
85
|
+
await client.startScreenShare(); // 自动恢复摄像头
|
|
86
|
+
client.stopScreenShare();
|
|
142
87
|
```
|
|
143
88
|
|
|
144
|
-
|
|
89
|
+
## 配置
|
|
145
90
|
|
|
146
91
|
```typescript
|
|
147
|
-
{
|
|
148
|
-
|
|
92
|
+
new AvesClient({
|
|
93
|
+
signalingUrl: "ws://localhost:8080", // 必需
|
|
94
|
+
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
|
149
95
|
fileChunkSize: 65536,
|
|
150
96
|
reconnect: { maxAttempts: 5, delay: 3000, requestTimeoutMs: 30000 },
|
|
151
|
-
debug: false
|
|
152
|
-
}
|
|
97
|
+
debug: false,
|
|
98
|
+
});
|
|
153
99
|
```
|
|
154
100
|
|
|
155
|
-
## API 参考
|
|
156
|
-
|
|
157
|
-
### 房间管理
|
|
158
|
-
|
|
159
|
-
| 方法 | 返回值 | 说明 |
|
|
160
|
-
|------|--------|------|
|
|
161
|
-
| `createRoom()` | `Promise<string>` | 创建房间,返回 roomId |
|
|
162
|
-
| `joinRoom(roomId, userName)` | `Promise<Participant[]>` | 加入房间 |
|
|
163
|
-
| `joinRoom(roomId, userId, userName)` | `Promise<Participant[]>` | 以指定 userId 加入 |
|
|
164
|
-
| `leaveRoom()` | `Promise<void>` | 离开房间,关闭所有连接 |
|
|
165
|
-
| `destroy()` | `void` | 销毁客户端,清理全部资源 |
|
|
166
|
-
|
|
167
|
-
### 消息
|
|
168
|
-
|
|
169
|
-
| 方法 | 说明 |
|
|
170
|
-
|------|------|
|
|
171
|
-
| `sendMessage(message)` | 广播消息给所有对等端 |
|
|
172
|
-
| `sendMessageToPeer(peerId, message)` | 发送消息给特定对等端 |
|
|
173
|
-
|
|
174
|
-
### 文件与语音
|
|
175
|
-
|
|
176
|
-
| 方法 | 说明 |
|
|
177
|
-
|------|------|
|
|
178
|
-
| `sendFile(blob, options?)` | 发送文件(`options.peerId` 存在时单播,否则广播) |
|
|
179
|
-
| `startVoice()` | 开启语音 |
|
|
180
|
-
| `stopVoice()` | 关闭语音 |
|
|
181
|
-
| `setMuted(muted)` | 静音控制 |
|
|
182
|
-
| `getLocalAudioState()` | 获取本地音频状态 |
|
|
183
|
-
| `getRemoteAudioStream(peerId)` | 获取远端音频 MediaStream |
|
|
184
|
-
| `startVideo(constraints?)` | 开启摄像头视频 |
|
|
185
|
-
| `stopVideo()` | 关闭摄像头视频 |
|
|
186
|
-
| `setVideoMuted(muted)` | 本地视频轨道开关 |
|
|
187
|
-
| `getLocalVideoState()` | 获取本地视频状态 |
|
|
188
|
-
| `getRemoteVideoStream(peerId)` | 获取远端视频 MediaStream |
|
|
189
|
-
| `startScreenShare()` | 开始屏幕共享 |
|
|
190
|
-
| `stopScreenShare()` | 停止屏幕共享 |
|
|
191
|
-
| `getScreenShareState()` | 获取屏幕共享状态 |
|
|
192
|
-
|
|
193
|
-
### 状态查询
|
|
194
|
-
|
|
195
|
-
| 方法 | 说明 |
|
|
196
|
-
|------|------|
|
|
197
|
-
| `getConnectionState(peerId)` | 对等连接状态 |
|
|
198
|
-
| `getParticipants()` | 当前房间参与者列表 |
|
|
199
|
-
| `isConnected()` | 信令连接是否活跃 |
|
|
200
|
-
|
|
201
101
|
## 事件
|
|
202
102
|
|
|
203
|
-
| 事件 |
|
|
204
|
-
|
|
205
|
-
| `message` | `(peerId, message)` |
|
|
206
|
-
| `userJoined` | `(
|
|
207
|
-
| `
|
|
208
|
-
| `
|
|
209
|
-
| `
|
|
210
|
-
| `
|
|
211
|
-
| `
|
|
212
|
-
| `remoteVideoTrack` | `(peerId, stream, track)` |
|
|
213
|
-
| `localAudioStateChange` | `(state)` | 本地音频状态变化 |
|
|
214
|
-
| `localVideoStateChange` | `(state)` | 本地视频状态变化 |
|
|
215
|
-
| `screenShareStateChange` | `(state)` | 屏幕共享状态变化 |
|
|
216
|
-
| `fileTransferStarted` | `(peerId, info)` | 文件传输开始 |
|
|
217
|
-
| `fileTransferProgress` | `(peerId, progress)` | 文件传输进度 |
|
|
218
|
-
| `fileTransferCompleted` | `(peerId, result)` | 文件传输完成 |
|
|
219
|
-
| `fileTransferFailed` | `(peerId, info, error)` | 文件传输失败 |
|
|
103
|
+
| 事件 | 参数 | 说明 |
|
|
104
|
+
|------|------|------|
|
|
105
|
+
| `message` | `(peerId, message)` | 收到消息 |
|
|
106
|
+
| `userJoined` / `userLeft` | `(user)` / `(userId)` | 用户进出 |
|
|
107
|
+
| `connectionStateChange` | `(peerId, state)` | 连接状态 |
|
|
108
|
+
| `localAudioStateChange` | `(state)` | 本地音频状态 |
|
|
109
|
+
| `localVideoStateChange` | `(state)` | 本地视频状态 |
|
|
110
|
+
| `screenShareStateChange` | `(state)` | 屏幕共享状态 |
|
|
111
|
+
| `fileTransferStarted/Progress/Completed/Failed` | — | 文件传输各阶段 |
|
|
112
|
+
| `remoteAudioTrack` / `remoteVideoTrack` | `(peerId, stream, track)` | 收到远端媒体 |
|
|
220
113
|
| `error` | `(error)` | 错误 |
|
|
221
114
|
|
|
222
115
|
## 浏览器兼容性
|
|
223
116
|
|
|
224
|
-
|
|
225
|
-
- Firefox 44+
|
|
226
|
-
- Safari 11+
|
|
227
|
-
- Opera 43+
|
|
228
|
-
|
|
229
|
-
## 发布与性能说明
|
|
117
|
+
Chrome/Edge 56+、Firefox 44+、Safari 11+、Opera 43+
|
|
230
118
|
|
|
231
|
-
|
|
232
|
-
- 当前 dry-run tarball 约 30 KiB,解包后约 203 KiB
|
|
233
|
-
- 主要运行时开销来自 WebRTC 浏览器实现、网络状况和房间内 peer 数量
|
|
234
|
-
- 客户端全网状拓扑连接数为 O(n),每个 peer 会建立独立 RTCPeerConnection
|
|
119
|
+
## 发布信息
|
|
235
120
|
|
|
236
|
-
|
|
121
|
+
- 包大小:~30 KiB gzip
|
|
122
|
+
- 全网状拓扑:O(n) 连接数
|
|
123
|
+
- 零运行时依赖
|
|
237
124
|
|
|
238
125
|
MIT
|
|
239
|
-
|
|
240
|
-
## 相关包
|
|
241
|
-
|
|
242
|
-
- [@yrzhao/aves-node](https://www.npmjs.com/package/@yrzhao/aves-node) - 信令服务器
|
package/dist/core/AvesClient.js
CHANGED
|
@@ -141,7 +141,7 @@ class AvesClient extends EventEmitter_1.EventEmitter {
|
|
|
141
141
|
return;
|
|
142
142
|
}
|
|
143
143
|
try {
|
|
144
|
-
this.webrtcManager.
|
|
144
|
+
this.webrtcManager.closeAllPeerConnections();
|
|
145
145
|
this.participants.clear();
|
|
146
146
|
this.preparedPeers.clear();
|
|
147
147
|
const joinResult = await this.signalingClient.joinRoom(this.currentRoomId, this.currentUserId, this.currentUserName);
|
|
@@ -165,6 +165,11 @@ export declare class WebRTCManager {
|
|
|
165
165
|
* Close the connection with a specific peer.
|
|
166
166
|
*/
|
|
167
167
|
closePeerConnection(peerId: string): void;
|
|
168
|
+
/**
|
|
169
|
+
* Close all peer connections while preserving local media capture.
|
|
170
|
+
* Used during signaling reconnect restore so camera/mic intent survives.
|
|
171
|
+
*/
|
|
172
|
+
closeAllPeerConnections(): void;
|
|
168
173
|
/**
|
|
169
174
|
* Close all connections and clean up resources.
|
|
170
175
|
*/
|
|
@@ -193,6 +198,7 @@ export declare class WebRTCManager {
|
|
|
193
198
|
private setupPeerConnection;
|
|
194
199
|
private ensureDataChannel;
|
|
195
200
|
private setupDataChannel;
|
|
201
|
+
private isCurrentDataChannel;
|
|
196
202
|
private handleMessageChannelMessage;
|
|
197
203
|
private handleFileControlMessage;
|
|
198
204
|
private handleFileChunk;
|
|
@@ -336,12 +336,19 @@ class WebRTCManager {
|
|
|
336
336
|
this.iceCandidateCallbacks.delete(peerId);
|
|
337
337
|
}
|
|
338
338
|
/**
|
|
339
|
-
* Close all connections
|
|
339
|
+
* Close all peer connections while preserving local media capture.
|
|
340
|
+
* Used during signaling reconnect restore so camera/mic intent survives.
|
|
340
341
|
*/
|
|
341
|
-
|
|
342
|
+
closeAllPeerConnections() {
|
|
342
343
|
this.getActivePeers().forEach((peerId) => {
|
|
343
344
|
this.closePeerConnection(peerId);
|
|
344
345
|
});
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Close all connections and clean up resources.
|
|
349
|
+
*/
|
|
350
|
+
closeAll() {
|
|
351
|
+
this.closeAllPeerConnections();
|
|
345
352
|
this.stopVoice();
|
|
346
353
|
this.stopVideo();
|
|
347
354
|
this.stopScreenShare();
|
|
@@ -449,6 +456,9 @@ class WebRTCManager {
|
|
|
449
456
|
}
|
|
450
457
|
};
|
|
451
458
|
dataChannel.onerror = (error) => {
|
|
459
|
+
if (!this.isCurrentDataChannel(peerId, kind, dataChannel)) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
452
462
|
this.emitError(new AvesError_1.AvesError({ message: `DataChannel error with ${peerId}: ${(0, messages_1.errorMessage)(error)}`, code: "WEBRTC_DATACHANNEL_FAILED", stage: "transport", retryable: true, peerId }));
|
|
453
463
|
const transfer = kind === "file"
|
|
454
464
|
? this.outgoingTransfers.get(peerId) ?? this.incomingTransfers.get(peerId) ?? null
|
|
@@ -465,6 +475,13 @@ class WebRTCManager {
|
|
|
465
475
|
this.handleMessageChannelMessage(peerId, event.data);
|
|
466
476
|
};
|
|
467
477
|
}
|
|
478
|
+
isCurrentDataChannel(peerId, kind, dataChannel) {
|
|
479
|
+
if (!this.peerConnections.has(peerId)) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
const channels = (0, channels_1.normalizePeerChannels)(this.dataChannels.get(peerId));
|
|
483
|
+
return channels[kind] === dataChannel;
|
|
484
|
+
}
|
|
468
485
|
handleMessageChannelMessage(peerId, rawData) {
|
|
469
486
|
try {
|
|
470
487
|
const message = JSON.parse(rawData);
|
|
@@ -47,6 +47,8 @@ export declare class MediaTrackManager {
|
|
|
47
47
|
private createTransceiverSender;
|
|
48
48
|
private syncLocalAudioTrack;
|
|
49
49
|
private syncLocalVideoTrack;
|
|
50
|
+
private getOutgoingVideoTrack;
|
|
51
|
+
private getOutgoingVideoStream;
|
|
50
52
|
private emitLocalAudioState;
|
|
51
53
|
private emitLocalVideoState;
|
|
52
54
|
private emitScreenShareState;
|
|
@@ -52,6 +52,7 @@ class MediaTrackManager {
|
|
|
52
52
|
this.audioSenders.set(peerId, this.createTransceiverSender(pc, "audio"));
|
|
53
53
|
this.videoSenders.set(peerId, this.createTransceiverSender(pc, "video"));
|
|
54
54
|
void this.syncLocalAudioTrack(peerId);
|
|
55
|
+
void this.syncLocalVideoTrack(peerId);
|
|
55
56
|
}
|
|
56
57
|
async startVoice() {
|
|
57
58
|
if (this.localAudioStream && this.localAudioTrack) {
|
|
@@ -351,24 +352,41 @@ class MediaTrackManager {
|
|
|
351
352
|
if (!pc) {
|
|
352
353
|
return;
|
|
353
354
|
}
|
|
355
|
+
const outgoingTrack = this.getOutgoingVideoTrack();
|
|
354
356
|
let sender = this.videoSenders.get(peerId) ?? null;
|
|
355
357
|
if (sender && typeof sender.replaceTrack === "function") {
|
|
356
|
-
await sender.replaceTrack(
|
|
358
|
+
await sender.replaceTrack(outgoingTrack);
|
|
357
359
|
return;
|
|
358
360
|
}
|
|
359
361
|
const addTrackCapable = pc;
|
|
360
|
-
if (
|
|
362
|
+
if (outgoingTrack &&
|
|
361
363
|
typeof addTrackCapable.addTrack === "function") {
|
|
362
|
-
const stream = this.
|
|
364
|
+
const stream = this.getOutgoingVideoStream() ??
|
|
363
365
|
(typeof MediaStream !== "undefined"
|
|
364
|
-
? new MediaStream([
|
|
366
|
+
? new MediaStream([outgoingTrack])
|
|
365
367
|
: undefined);
|
|
366
368
|
sender = stream
|
|
367
|
-
? addTrackCapable.addTrack(
|
|
368
|
-
: addTrackCapable.addTrack(
|
|
369
|
+
? addTrackCapable.addTrack(outgoingTrack, stream)
|
|
370
|
+
: addTrackCapable.addTrack(outgoingTrack);
|
|
369
371
|
this.videoSenders.set(peerId, sender);
|
|
370
372
|
}
|
|
371
373
|
}
|
|
374
|
+
getOutgoingVideoTrack() {
|
|
375
|
+
if (this.screenShareActive &&
|
|
376
|
+
this.screenShareTrack &&
|
|
377
|
+
this.screenShareTrack.readyState === "live") {
|
|
378
|
+
return this.screenShareTrack;
|
|
379
|
+
}
|
|
380
|
+
return this.localVideoTrack;
|
|
381
|
+
}
|
|
382
|
+
getOutgoingVideoStream() {
|
|
383
|
+
if (this.screenShareActive &&
|
|
384
|
+
this.screenShareStream &&
|
|
385
|
+
this.screenShareTrack?.readyState === "live") {
|
|
386
|
+
return this.screenShareStream;
|
|
387
|
+
}
|
|
388
|
+
return this.localVideoStream;
|
|
389
|
+
}
|
|
372
390
|
emitLocalAudioState() {
|
|
373
391
|
const state = this.getLocalAudioState();
|
|
374
392
|
this.localAudioStateCallbacks.forEach((callback) => callback(state));
|
|
@@ -138,7 +138,7 @@ export class AvesClient extends EventEmitter {
|
|
|
138
138
|
return;
|
|
139
139
|
}
|
|
140
140
|
try {
|
|
141
|
-
this.webrtcManager.
|
|
141
|
+
this.webrtcManager.closeAllPeerConnections();
|
|
142
142
|
this.participants.clear();
|
|
143
143
|
this.preparedPeers.clear();
|
|
144
144
|
const joinResult = await this.signalingClient.joinRoom(this.currentRoomId, this.currentUserId, this.currentUserName);
|
|
@@ -165,6 +165,11 @@ export declare class WebRTCManager {
|
|
|
165
165
|
* Close the connection with a specific peer.
|
|
166
166
|
*/
|
|
167
167
|
closePeerConnection(peerId: string): void;
|
|
168
|
+
/**
|
|
169
|
+
* Close all peer connections while preserving local media capture.
|
|
170
|
+
* Used during signaling reconnect restore so camera/mic intent survives.
|
|
171
|
+
*/
|
|
172
|
+
closeAllPeerConnections(): void;
|
|
168
173
|
/**
|
|
169
174
|
* Close all connections and clean up resources.
|
|
170
175
|
*/
|
|
@@ -193,6 +198,7 @@ export declare class WebRTCManager {
|
|
|
193
198
|
private setupPeerConnection;
|
|
194
199
|
private ensureDataChannel;
|
|
195
200
|
private setupDataChannel;
|
|
201
|
+
private isCurrentDataChannel;
|
|
196
202
|
private handleMessageChannelMessage;
|
|
197
203
|
private handleFileControlMessage;
|
|
198
204
|
private handleFileChunk;
|
|
@@ -333,12 +333,19 @@ export class WebRTCManager {
|
|
|
333
333
|
this.iceCandidateCallbacks.delete(peerId);
|
|
334
334
|
}
|
|
335
335
|
/**
|
|
336
|
-
* Close all connections
|
|
336
|
+
* Close all peer connections while preserving local media capture.
|
|
337
|
+
* Used during signaling reconnect restore so camera/mic intent survives.
|
|
337
338
|
*/
|
|
338
|
-
|
|
339
|
+
closeAllPeerConnections() {
|
|
339
340
|
this.getActivePeers().forEach((peerId) => {
|
|
340
341
|
this.closePeerConnection(peerId);
|
|
341
342
|
});
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Close all connections and clean up resources.
|
|
346
|
+
*/
|
|
347
|
+
closeAll() {
|
|
348
|
+
this.closeAllPeerConnections();
|
|
342
349
|
this.stopVoice();
|
|
343
350
|
this.stopVideo();
|
|
344
351
|
this.stopScreenShare();
|
|
@@ -446,6 +453,9 @@ export class WebRTCManager {
|
|
|
446
453
|
}
|
|
447
454
|
};
|
|
448
455
|
dataChannel.onerror = (error) => {
|
|
456
|
+
if (!this.isCurrentDataChannel(peerId, kind, dataChannel)) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
449
459
|
this.emitError(new AvesError({ message: `DataChannel error with ${peerId}: ${errorMessage(error)}`, code: "WEBRTC_DATACHANNEL_FAILED", stage: "transport", retryable: true, peerId }));
|
|
450
460
|
const transfer = kind === "file"
|
|
451
461
|
? this.outgoingTransfers.get(peerId) ?? this.incomingTransfers.get(peerId) ?? null
|
|
@@ -462,6 +472,13 @@ export class WebRTCManager {
|
|
|
462
472
|
this.handleMessageChannelMessage(peerId, event.data);
|
|
463
473
|
};
|
|
464
474
|
}
|
|
475
|
+
isCurrentDataChannel(peerId, kind, dataChannel) {
|
|
476
|
+
if (!this.peerConnections.has(peerId)) {
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
479
|
+
const channels = normalizePeerChannels(this.dataChannels.get(peerId));
|
|
480
|
+
return channels[kind] === dataChannel;
|
|
481
|
+
}
|
|
465
482
|
handleMessageChannelMessage(peerId, rawData) {
|
|
466
483
|
try {
|
|
467
484
|
const message = JSON.parse(rawData);
|
|
@@ -47,6 +47,8 @@ export declare class MediaTrackManager {
|
|
|
47
47
|
private createTransceiverSender;
|
|
48
48
|
private syncLocalAudioTrack;
|
|
49
49
|
private syncLocalVideoTrack;
|
|
50
|
+
private getOutgoingVideoTrack;
|
|
51
|
+
private getOutgoingVideoStream;
|
|
50
52
|
private emitLocalAudioState;
|
|
51
53
|
private emitLocalVideoState;
|
|
52
54
|
private emitScreenShareState;
|
|
@@ -49,6 +49,7 @@ export class MediaTrackManager {
|
|
|
49
49
|
this.audioSenders.set(peerId, this.createTransceiverSender(pc, "audio"));
|
|
50
50
|
this.videoSenders.set(peerId, this.createTransceiverSender(pc, "video"));
|
|
51
51
|
void this.syncLocalAudioTrack(peerId);
|
|
52
|
+
void this.syncLocalVideoTrack(peerId);
|
|
52
53
|
}
|
|
53
54
|
async startVoice() {
|
|
54
55
|
if (this.localAudioStream && this.localAudioTrack) {
|
|
@@ -348,24 +349,41 @@ export class MediaTrackManager {
|
|
|
348
349
|
if (!pc) {
|
|
349
350
|
return;
|
|
350
351
|
}
|
|
352
|
+
const outgoingTrack = this.getOutgoingVideoTrack();
|
|
351
353
|
let sender = this.videoSenders.get(peerId) ?? null;
|
|
352
354
|
if (sender && typeof sender.replaceTrack === "function") {
|
|
353
|
-
await sender.replaceTrack(
|
|
355
|
+
await sender.replaceTrack(outgoingTrack);
|
|
354
356
|
return;
|
|
355
357
|
}
|
|
356
358
|
const addTrackCapable = pc;
|
|
357
|
-
if (
|
|
359
|
+
if (outgoingTrack &&
|
|
358
360
|
typeof addTrackCapable.addTrack === "function") {
|
|
359
|
-
const stream = this.
|
|
361
|
+
const stream = this.getOutgoingVideoStream() ??
|
|
360
362
|
(typeof MediaStream !== "undefined"
|
|
361
|
-
? new MediaStream([
|
|
363
|
+
? new MediaStream([outgoingTrack])
|
|
362
364
|
: undefined);
|
|
363
365
|
sender = stream
|
|
364
|
-
? addTrackCapable.addTrack(
|
|
365
|
-
: addTrackCapable.addTrack(
|
|
366
|
+
? addTrackCapable.addTrack(outgoingTrack, stream)
|
|
367
|
+
: addTrackCapable.addTrack(outgoingTrack);
|
|
366
368
|
this.videoSenders.set(peerId, sender);
|
|
367
369
|
}
|
|
368
370
|
}
|
|
371
|
+
getOutgoingVideoTrack() {
|
|
372
|
+
if (this.screenShareActive &&
|
|
373
|
+
this.screenShareTrack &&
|
|
374
|
+
this.screenShareTrack.readyState === "live") {
|
|
375
|
+
return this.screenShareTrack;
|
|
376
|
+
}
|
|
377
|
+
return this.localVideoTrack;
|
|
378
|
+
}
|
|
379
|
+
getOutgoingVideoStream() {
|
|
380
|
+
if (this.screenShareActive &&
|
|
381
|
+
this.screenShareStream &&
|
|
382
|
+
this.screenShareTrack?.readyState === "live") {
|
|
383
|
+
return this.screenShareStream;
|
|
384
|
+
}
|
|
385
|
+
return this.localVideoStream;
|
|
386
|
+
}
|
|
369
387
|
emitLocalAudioState() {
|
|
370
388
|
const state = this.getLocalAudioState();
|
|
371
389
|
this.localAudioStateCallbacks.forEach((callback) => callback(state));
|