@yrzhao/aves-core 1.1.0 → 1.2.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.
Files changed (36) hide show
  1. package/README.md +55 -40
  2. package/dist/core/AvesClient.d.ts +32 -8
  3. package/dist/core/AvesClient.js +254 -55
  4. package/dist/core/AvesError.d.ts +1 -1
  5. package/dist/core/EventEmitter.d.ts +8 -16
  6. package/dist/core/EventEmitter.js +41 -10
  7. package/dist/core/SignalingClient.d.ts +39 -44
  8. package/dist/core/SignalingClient.js +434 -78
  9. package/dist/core/WebRTCManager.d.ts +67 -102
  10. package/dist/core/WebRTCManager.js +607 -136
  11. package/dist/core/webrtc/channels.d.ts +6 -0
  12. package/dist/core/webrtc/channels.js +15 -0
  13. package/dist/core/webrtc/fileTransfer.d.ts +46 -3
  14. package/dist/core/webrtc/fileTransfer.js +725 -78
  15. package/dist/core/webrtc/mediaTracks.d.ts +38 -5
  16. package/dist/core/webrtc/mediaTracks.js +269 -58
  17. package/dist/esm/core/AvesClient.d.ts +32 -8
  18. package/dist/esm/core/AvesClient.js +254 -55
  19. package/dist/esm/core/AvesError.d.ts +1 -1
  20. package/dist/esm/core/EventEmitter.d.ts +8 -16
  21. package/dist/esm/core/EventEmitter.js +41 -10
  22. package/dist/esm/core/SignalingClient.d.ts +39 -44
  23. package/dist/esm/core/SignalingClient.js +434 -78
  24. package/dist/esm/core/WebRTCManager.d.ts +67 -102
  25. package/dist/esm/core/WebRTCManager.js +608 -137
  26. package/dist/esm/core/webrtc/channels.d.ts +6 -0
  27. package/dist/esm/core/webrtc/channels.js +14 -0
  28. package/dist/esm/core/webrtc/fileTransfer.d.ts +46 -3
  29. package/dist/esm/core/webrtc/fileTransfer.js +722 -77
  30. package/dist/esm/core/webrtc/mediaTracks.d.ts +38 -5
  31. package/dist/esm/core/webrtc/mediaTracks.js +269 -58
  32. package/dist/esm/index.d.ts +2 -1
  33. package/dist/esm/types/types.d.ts +106 -9
  34. package/dist/index.d.ts +2 -1
  35. package/dist/types/types.d.ts +106 -9
  36. package/package.json +8 -6
@@ -1,17 +1,35 @@
1
1
  import { AvesError } from "./AvesError";
2
- import { AvesMessage, AvesVideoConstraints, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, ScreenShareState } from "../types/types";
2
+ import { AvesAudioConstraints, AvesMessage, AvesPeerStats, AvesVideoConstraints, FileTransferConfig, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, ScreenShareOptions, ScreenShareState } from "../types/types";
3
+ export interface WebRTCManagerOptions {
4
+ iceServers: RTCIceServer[];
5
+ rtcConfiguration?: Omit<RTCConfiguration, "iceServers">;
6
+ fileChunkSize?: number;
7
+ fileTransfer?: FileTransferConfig;
8
+ videoConstraints?: AvesVideoConstraints;
9
+ audioConstraints?: AvesAudioConstraints;
10
+ iceDisconnectGraceMs?: number;
11
+ }
3
12
  /**
4
- * WebRTCManager - Manages WebRTC PeerConnections, DataChannels, and media tracks
13
+ * WebRTCManager - Manages WebRTC PeerConnections, Perfect Negotiation,
14
+ * ICE candidate buffering, DataChannels, file transfer, media tracks, and RTCStats.
5
15
  */
6
16
  export declare class WebRTCManager {
7
17
  private peerConnections;
8
18
  private dataChannels;
9
19
  private iceServers;
20
+ private rtcConfiguration;
21
+ private iceDisconnectGraceMs;
22
+ private pendingIceCandidates;
23
+ private disconnectGraceTimers;
24
+ private politeIceRestartTimers;
25
+ private negotiationStates;
10
26
  private fileTransfers;
11
27
  private messageCallbacks;
12
28
  private connectionStateCallbacks;
13
29
  private dataChannelStateCallbacks;
30
+ private fileChannelStateCallbacks;
14
31
  private iceCandidateCallbacks;
32
+ private negotiationOfferCallbacks;
15
33
  private outgoingTransfers;
16
34
  private incomingTransfers;
17
35
  private readyResolvers;
@@ -28,34 +46,54 @@ export declare class WebRTCManager {
28
46
  private localVideoStateCallbacks;
29
47
  private screenShareStateCallbacks;
30
48
  private errorCallbacks;
31
- constructor(iceServers: RTCIceServer[], fileChunkSize?: number, videoConstraints?: AvesVideoConstraints);
49
+ constructor(iceServersOrOptions: RTCIceServer[] | WebRTCManagerOptions, fileChunkSize?: number, videoConstraints?: AvesVideoConstraints);
50
+ /**
51
+ * Update ICE servers dynamically (e.g. when rotating ephemeral TURN credentials).
52
+ * Applies immediately to existing peer connections when supported by the browser.
53
+ */
54
+ updateIceServers(iceServers: RTCIceServer[]): void;
55
+ /**
56
+ * Mark a peer as polite (accepts rollback on offer glare) or impolite.
57
+ */
58
+ setPeerPolite(peerId: string, polite: boolean): void;
32
59
  /**
33
60
  * Create a new PeerConnection for the specified peer.
34
61
  * If a connection already exists, returns the existing connection.
35
62
  */
36
- createPeerConnection(peerId: string): RTCPeerConnection;
63
+ createPeerConnection(peerId: string, options?: {
64
+ polite?: boolean;
65
+ }): RTCPeerConnection;
37
66
  /**
38
67
  * Create an offer for the specified peer.
39
- * The offer side creates both the message and file channels.
68
+ * The offer side creates both the message and file channels and pre-creates
69
+ * sendrecv audio/video transceivers.
40
70
  */
41
- createOffer(peerId: string): Promise<RTCSessionDescriptionInit>;
71
+ createOffer(peerId: string, options?: RTCOfferOptions): Promise<RTCSessionDescriptionInit>;
72
+ /**
73
+ * Create an answer for the specified peer using Perfect Negotiation glare handling.
74
+ * Returns null when an impolite peer intentionally ignores a colliding offer.
75
+ */
76
+ handleRemoteOffer(peerId: string, offer: RTCSessionDescriptionInit): Promise<RTCSessionDescriptionInit | null>;
42
77
  /**
43
78
  * Create an answer for the specified peer.
44
- * The answer side receives data channels from the remote offer.
79
+ * Preserved for backward compatibility with direct callers and unit tests.
45
80
  */
46
81
  createAnswer(peerId: string, offer: RTCSessionDescriptionInit): Promise<RTCSessionDescriptionInit>;
47
82
  /**
48
- * Set the remote answer for the specified peer.
83
+ * Set the remote answer for the specified peer and flush buffered ICE candidates.
49
84
  */
50
85
  setRemoteAnswer(peerId: string, answer: RTCSessionDescriptionInit): Promise<void>;
51
86
  /**
52
87
  * Add an ICE candidate for the specified peer.
88
+ * Buffers candidates arriving before PeerConnection or remoteDescription is ready.
53
89
  */
54
90
  addIceCandidate(peerId: string, candidate: RTCIceCandidateInit): Promise<void>;
91
+ /**
92
+ * Trigger ICE restart for a peer and emit the fresh offer via onNegotiationOffer.
93
+ */
94
+ restartIce(peerId: string): Promise<RTCSessionDescriptionInit | null>;
55
95
  /**
56
96
  * Send a JSON message to all connected peers.
57
- * Peers with unavailable channels are skipped gracefully;
58
- * the message is delivered to every ready peer.
59
97
  */
60
98
  sendMessage(message: AvesMessage): void;
61
99
  /**
@@ -67,93 +105,38 @@ export declare class WebRTCManager {
67
105
  */
68
106
  sendFile(blob: Blob, options?: FileTransferOptions): Promise<FileTransferInfo[]>;
69
107
  /**
70
- * Start capturing local audio and bind it to current peer connections.
71
- */
72
- startVoice(): Promise<MediaStream>;
73
- /**
74
- * Stop local audio capture and detach it from active peers.
108
+ * Cancel an active file transfer with a peer.
75
109
  */
110
+ cancelFileTransfer(peerId: string, transferId?: string): boolean;
111
+ startVoice(constraints?: AvesAudioConstraints): Promise<MediaStream>;
112
+ switchAudioDevice(deviceId: string): Promise<MediaStream>;
76
113
  stopVoice(): void;
77
- /**
78
- * Toggle local mute state.
79
- */
80
114
  setMuted(muted: boolean): void;
81
- /**
82
- * Read current local audio state.
83
- */
84
115
  getLocalAudioState(): LocalAudioState;
85
- /**
86
- * Read current remote audio stream for a peer.
87
- */
88
116
  getRemoteAudioStream(peerId: string): MediaStream | null;
89
- /**
90
- * Start capturing local camera video and bind it to current peer connections.
91
- * Idempotent: returns the existing stream if already active.
92
- */
93
117
  startVideo(constraints?: AvesVideoConstraints): Promise<MediaStream>;
94
- /**
95
- * Stop local camera capture and detach it from active peers.
96
- */
118
+ switchVideoDevice(deviceId: string): Promise<MediaStream>;
97
119
  stopVideo(): void;
98
- /**
99
- * Toggle local video mute state (camera on/off).
100
- */
101
120
  setVideoMuted(muted: boolean): void;
102
- /**
103
- * Read current local video state.
104
- */
105
121
  getLocalVideoState(): LocalVideoState;
106
- /**
107
- * Read current remote video stream for a peer.
108
- */
109
122
  getRemoteVideoStream(peerId: string): MediaStream | null;
110
- /**
111
- * Start screen sharing. Replaces the camera video track with the display track
112
- * on all active peer connections. The camera track is saved and restored when
113
- * screen sharing stops.
114
- */
115
- startScreenShare(): Promise<MediaStream>;
116
- /**
117
- * Stop screen sharing and restore the camera video track (if it was active).
118
- */
123
+ startScreenShare(options?: ScreenShareOptions): Promise<MediaStream>;
119
124
  stopScreenShare(): void;
120
- /**
121
- * Read current screen share state.
122
- */
123
125
  getScreenShareState(): ScreenShareState;
124
- /**
125
- * Register a callback for remote video tracks.
126
- */
126
+ getPeerStats(peerId: string): Promise<AvesPeerStats | null>;
127
+ getAllPeerStats(): Promise<AvesPeerStats[]>;
127
128
  onRemoteVideoTrack(callback: (peerId: string, stream: MediaStream, track: MediaStreamTrack) => void): void;
128
- /**
129
- * Register a callback for local video state changes.
130
- */
131
129
  onLocalVideoStateChange(callback: (state: LocalVideoState) => void): void;
132
- /**
133
- * Register a callback for screen share state changes.
134
- */
135
130
  onScreenShareStateChange(callback: (state: ScreenShareState) => void): void;
136
- /**
137
- * Register a callback for WebRTCManager-level errors
138
- * (e.g. DataChannel failures, message parse errors).
139
- */
131
+ onRemoteTrackEnded(callback: (peerId: string, kind: "audio" | "video", track: MediaStreamTrack) => void): void;
132
+ onRemoteTrackMuted(callback: (peerId: string, kind: "audio" | "video", muted: boolean, track: MediaStreamTrack) => void): void;
133
+ onNegotiationOffer(callback: (peerId: string, offer: RTCSessionDescriptionInit) => void): void;
140
134
  onError(callback: (error: AvesError) => void): void;
141
135
  private emitError;
142
- /**
143
- * Register a callback for ICE candidates from the specified peer.
144
- */
145
136
  onIceCandidate(peerId: string, callback: (candidate: RTCIceCandidateInit) => void): void;
146
- /**
147
- * Register a callback for connection state changes.
148
- */
149
137
  onConnectionStateChange(peerId: string, callback: (state: RTCPeerConnectionState) => void): void;
150
- /**
151
- * Register a callback for main DataChannel state changes.
152
- */
153
138
  onDataChannelStateChange(peerId: string, callback: (state: RTCDataChannelState) => void): void;
154
- /**
155
- * Register a callback for incoming user messages.
156
- */
139
+ onFileChannelStateChange(peerId: string, callback: (state: RTCDataChannelState) => void): void;
157
140
  onMessage(callback: (peerId: string, message: AvesMessage) => void): void;
158
141
  onFileTransferStarted(callback: (peerId: string, info: FileTransferInfo) => void): void;
159
142
  onFileTransferProgress(callback: (peerId: string, progress: FileTransferProgress) => void): void;
@@ -165,40 +148,22 @@ export declare class WebRTCManager {
165
148
  * Close the connection with a specific peer.
166
149
  */
167
150
  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
151
  closeAllPeerConnections(): void;
173
- /**
174
- * Close all connections and clean up resources.
175
- */
176
152
  closeAll(): void;
177
153
  destroy(): void;
178
- /**
179
- * Get all active peer IDs.
180
- */
181
154
  getActivePeers(): string[];
182
- /**
183
- * Check if connected to a specific peer.
184
- */
185
155
  isConnected(peerId: string): boolean;
186
- /**
187
- * Get the actual RTCPeerConnectionState for a peer.
188
- */
189
156
  getConnectionState(peerId: string): RTCPeerConnectionState;
190
- /**
191
- * Get the primary message DataChannel state for a peer.
192
- */
157
+ getIceConnectionState(peerId: string): RTCIceConnectionState;
158
+ getSignalingState(peerId: string): RTCSignalingState;
193
159
  getDataChannelState(peerId: string): RTCDataChannelState | "closed";
194
- /**
195
- * Check if the main DataChannel is ready for a specific peer.
196
- */
160
+ getFileChannelState(peerId: string): RTCDataChannelState | "closed";
197
161
  isDataChannelReady(peerId: string): boolean;
198
- /**
199
- * Check if the file channel is ready for a specific peer.
200
- */
201
162
  isFileChannelReady(peerId: string): boolean;
163
+ private getOrCreateNegotiationState;
164
+ private enqueueIceCandidate;
165
+ private flushPendingIceCandidates;
166
+ private clearDisconnectGraceTimer;
202
167
  private setupPeerConnection;
203
168
  private ensureDataChannel;
204
169
  private setupDataChannel;