@decartai/sdk 0.0.4 → 0.0.5
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.
|
@@ -3,12 +3,14 @@ const ICE_SERVERS = [{ urls: "stun:stun.l.google.com:19302" }];
|
|
|
3
3
|
var WebRTCConnection = class {
|
|
4
4
|
pc = null;
|
|
5
5
|
ws = null;
|
|
6
|
+
localStream = null;
|
|
6
7
|
state = "disconnected";
|
|
7
8
|
constructor(callbacks = {}) {
|
|
8
9
|
this.callbacks = callbacks;
|
|
9
10
|
}
|
|
10
|
-
async connect(url, localStream, timeout =
|
|
11
|
+
async connect(url, localStream, timeout = 25e3) {
|
|
11
12
|
const deadline = Date.now() + timeout;
|
|
13
|
+
this.localStream = localStream;
|
|
12
14
|
await new Promise((resolve, reject) => {
|
|
13
15
|
const timer = setTimeout(() => reject(/* @__PURE__ */ new Error("WebSocket timeout")), timeout);
|
|
14
16
|
this.ws = new WebSocket(url);
|
|
@@ -28,24 +30,7 @@ var WebRTCConnection = class {
|
|
|
28
30
|
};
|
|
29
31
|
this.ws.onclose = () => this.setState("disconnected");
|
|
30
32
|
});
|
|
31
|
-
this.
|
|
32
|
-
this.pc = new RTCPeerConnection({ iceServers: ICE_SERVERS });
|
|
33
|
-
localStream.getTracks().forEach((track) => this.pc.addTrack(track, localStream));
|
|
34
|
-
this.pc.ontrack = (e) => {
|
|
35
|
-
if (e.streams?.[0]) this.callbacks.onRemoteStream?.(e.streams[0]);
|
|
36
|
-
};
|
|
37
|
-
this.pc.onicecandidate = (e) => {
|
|
38
|
-
if (e.candidate) this.send({
|
|
39
|
-
type: "ice-candidate",
|
|
40
|
-
candidate: e.candidate
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
this.pc.onconnectionstatechange = () => {
|
|
44
|
-
if (!this.pc) return;
|
|
45
|
-
const s = this.pc.connectionState;
|
|
46
|
-
this.setState(s === "connected" ? "connected" : ["connecting", "new"].includes(s) ? "connecting" : "disconnected");
|
|
47
|
-
};
|
|
48
|
-
this.handleSignalingMessage({ type: "ready" });
|
|
33
|
+
await this.setupNewPeerConnection();
|
|
49
34
|
while (Date.now() < deadline) {
|
|
50
35
|
if (this.state === "connected") return;
|
|
51
36
|
await new Promise((r) => setTimeout(r, 100));
|
|
@@ -89,6 +74,10 @@ var WebRTCConnection = class {
|
|
|
89
74
|
case "ice-candidate":
|
|
90
75
|
if (msg.candidate) await this.pc.addIceCandidate(msg.candidate);
|
|
91
76
|
break;
|
|
77
|
+
case "ice-restart":
|
|
78
|
+
const turnConfig = msg.turn_config;
|
|
79
|
+
if (turnConfig) await this.setupNewPeerConnection(turnConfig);
|
|
80
|
+
break;
|
|
92
81
|
}
|
|
93
82
|
} catch (error) {
|
|
94
83
|
console.error("[WebRTC] Error:", error);
|
|
@@ -105,12 +94,46 @@ var WebRTCConnection = class {
|
|
|
105
94
|
this.callbacks.onStateChange?.(state);
|
|
106
95
|
}
|
|
107
96
|
}
|
|
97
|
+
async setupNewPeerConnection(turnConfig) {
|
|
98
|
+
if (!this.localStream) throw new Error("No local stream found");
|
|
99
|
+
if (this.pc) {
|
|
100
|
+
this.pc.getSenders().forEach((sender) => {
|
|
101
|
+
if (sender.track) this.pc.removeTrack(sender);
|
|
102
|
+
});
|
|
103
|
+
this.pc.close();
|
|
104
|
+
}
|
|
105
|
+
let iceServers = ICE_SERVERS;
|
|
106
|
+
if (turnConfig) iceServers.push({
|
|
107
|
+
urls: turnConfig.server_url,
|
|
108
|
+
credential: turnConfig.credential,
|
|
109
|
+
username: turnConfig.username
|
|
110
|
+
});
|
|
111
|
+
this.pc = new RTCPeerConnection({ iceServers });
|
|
112
|
+
this.localStream.getTracks().forEach((track) => this.pc.addTrack(track, this.localStream));
|
|
113
|
+
this.pc.ontrack = (e) => {
|
|
114
|
+
if (e.streams?.[0]) this.callbacks.onRemoteStream?.(e.streams[0]);
|
|
115
|
+
};
|
|
116
|
+
this.pc.onicecandidate = (e) => {
|
|
117
|
+
this.send({
|
|
118
|
+
type: "ice-candidate",
|
|
119
|
+
candidate: e.candidate
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
this.pc.onconnectionstatechange = () => {
|
|
123
|
+
if (!this.pc) return;
|
|
124
|
+
const s = this.pc.connectionState;
|
|
125
|
+
this.setState(s === "connected" ? "connected" : ["connecting", "new"].includes(s) ? "connecting" : "disconnected");
|
|
126
|
+
};
|
|
127
|
+
this.pc.oniceconnectionstatechange = () => {};
|
|
128
|
+
this.handleSignalingMessage({ type: "ready" });
|
|
129
|
+
}
|
|
108
130
|
cleanup() {
|
|
109
131
|
this.pc?.getSenders().forEach((s) => s.track?.stop());
|
|
110
132
|
this.pc?.close();
|
|
111
133
|
this.pc = null;
|
|
112
134
|
this.ws?.close();
|
|
113
135
|
this.ws = null;
|
|
136
|
+
this.localStream = null;
|
|
114
137
|
this.setState("disconnected");
|
|
115
138
|
}
|
|
116
139
|
async applyCodecPreference(preferredCodecName) {
|