@luxonis/visualizer-protobuf 2.0.0 → 2.0.2
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/apps/depthai-visualizer/src/connection/adapter.d.ts +3 -2
- package/dist/apps/depthai-visualizer/src/connection/adapter.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/adapter.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/communicator.d.ts +1 -1
- package/dist/apps/depthai-visualizer/src/connection/communicator.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/communicator.js +1 -1
- package/dist/apps/depthai-visualizer/src/connection/communicator.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/connection.d.ts +3 -1
- package/dist/apps/depthai-visualizer/src/connection/connection.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/connection.js +3 -2
- package/dist/apps/depthai-visualizer/src/connection/connection.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/webrtc/bridge.d.ts +8 -0
- package/dist/apps/depthai-visualizer/src/connection/webrtc/bridge.d.ts.map +1 -0
- package/dist/apps/depthai-visualizer/src/connection/webrtc/bridge.js +137 -0
- package/dist/apps/depthai-visualizer/src/connection/webrtc/bridge.js.map +1 -0
- package/dist/apps/depthai-visualizer/src/connection/webrtc/worker-utils.d.ts +43 -26
- package/dist/apps/depthai-visualizer/src/connection/webrtc/worker-utils.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/webrtc/worker-utils.js +59 -92
- package/dist/apps/depthai-visualizer/src/connection/webrtc/worker-utils.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/webrtc/worker.js +13 -2
- package/dist/apps/depthai-visualizer/src/connection/webrtc/worker.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/connection/ws/worker-utils.js +1 -1
- package/dist/apps/depthai-visualizer/src/connection/ws/worker-utils.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/index.d.ts +0 -1
- package/dist/apps/depthai-visualizer/src/index.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/index.js +0 -1
- package/dist/apps/depthai-visualizer/src/index.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/messaging/deserialization/detections/custom.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/messaging/deserialization/detections/custom.js +37 -17
- package/dist/apps/depthai-visualizer/src/messaging/deserialization/detections/custom.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/EncodedFrame.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/EncodedFrame.js +0 -3
- package/dist/apps/depthai-visualizer/src/protobuf.generated/EncodedFrame.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImageAnnotations.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImageAnnotations.js +0 -3
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImageAnnotations.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImgDetections.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImgDetections.js +0 -3
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImgDetections.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImgFrame.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImgFrame.js +0 -3
- package/dist/apps/depthai-visualizer/src/protobuf.generated/ImgFrame.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/PointCloudData.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/PointCloudData.js +0 -3
- package/dist/apps/depthai-visualizer/src/protobuf.generated/PointCloudData.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/SpatialImgDetections.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/SpatialImgDetections.js +0 -3
- package/dist/apps/depthai-visualizer/src/protobuf.generated/SpatialImgDetections.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/common.d.ts.map +1 -1
- package/dist/apps/depthai-visualizer/src/protobuf.generated/common.js +0 -3
- package/dist/apps/depthai-visualizer/src/protobuf.generated/common.js.map +1 -1
- package/dist/apps/depthai-visualizer/src/utils/artificial-worker.d.ts +6 -0
- package/dist/apps/depthai-visualizer/src/utils/artificial-worker.d.ts.map +1 -0
- package/dist/apps/depthai-visualizer/src/utils/artificial-worker.js +16 -0
- package/dist/apps/depthai-visualizer/src/utils/artificial-worker.js.map +1 -0
- package/dist/apps/depthai-visualizer/src/utils/encoding.d.ts.map +1 -1
- package/dist/{clike-BjlaHgsH.js → clike-CS-poxoD.js} +2 -0
- package/dist/{communicator-D_EeiIbd.js → communicator-ZAqNuwq6.js} +37 -23
- package/dist/{groovy-BD9p_7sW.js → groovy-ioGz2WSV.js} +2 -4
- package/dist/{index-DpXY8ing.js → index-9D6nsmX0.js} +4 -4
- package/dist/{index-DQCAxiet.js → index-B68UDyAv.js} +12509 -5359
- package/dist/{index-CnbrHWCd.js → index-B7v9Ptl2.js} +4 -4
- package/dist/{index-DudYiuIt.js → index-BHmiJ5Qk.js} +4 -4
- package/dist/{index-BeQsiPfT.js → index-By6lF-Xz.js} +4 -4
- package/dist/{index-QNisWGZ4.js → index-ByP7YJPY.js} +4 -4
- package/dist/{index-Al0pDnqr.js → index-C04gVVoP.js} +4 -4
- package/dist/{index-FOw7-sDA.js → index-CLESgVaD.js} +4 -4
- package/dist/{index-BtdqjdG0.js → index-CX_Do7kl.js} +4 -4
- package/dist/{index-doJtQKmP.js → index-CXwbrQKL.js} +4 -4
- package/dist/{index-D-Et9RVh.js → index-CsJJiZos.js} +4 -4
- package/dist/{index-M-zdB4A1.js → index-CsyZMo6w.js} +4 -4
- package/dist/{index-r1jTO4Ay.js → index-D502DL2z.js} +4 -4
- package/dist/{index-BfF6NM_p.js → index-Ddm491YR.js} +4 -4
- package/dist/{index-By94udNp.js → index-SWsY2eOm.js} +7 -7
- package/dist/{index-BOSrUvKZ.js → index-T_ckFaRN.js} +4 -4
- package/dist/{index-D0vwBTE3.js → index-WSNQlHZC.js} +4 -4
- package/dist/{index-IoSJKto7.js → index-qrTNSyOQ.js} +5 -5
- package/dist/{index-CgoOLCnB.js → index-uD6dIRaN.js} +15 -11
- package/dist/index.js +4 -4
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/RenderableLineAnnotation.js +3 -3
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/RenderableLineAnnotation.js.map +1 -1
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/normalizeAnnotations.d.ts +8 -0
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/normalizeAnnotations.d.ts.map +1 -1
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/normalizeAnnotations.js +2 -0
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/normalizeAnnotations.js.map +1 -1
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/types.d.ts +2 -0
- package/dist/packages/studio-base/src/panels/ThreeDeeRender/renderables/ImageMode/annotations/types.d.ts.map +1 -1
- package/dist/worker-ByVSTHSI.js +102 -0
- package/dist/{worker-1z9jlpUn.js → worker-DlZ2xp8p.js} +2 -2
- package/dist/{worker-utils-DfViKOHw.js → worker-utils-BNThsbdN.js} +2 -2
- package/package.json +3 -3
- package/dist/worker-DNYPTnyn.js +0 -9
- package/dist/worker-utils-BbyBpOVt.js +0 -453
|
@@ -1,453 +0,0 @@
|
|
|
1
|
-
import { F as FoxgloveCommunicator } from './communicator-D_EeiIbd.js';
|
|
2
|
-
|
|
3
|
-
class EventEmitter {
|
|
4
|
-
id = 0;
|
|
5
|
-
handlers = {};
|
|
6
|
-
emit(event, data) {
|
|
7
|
-
const handlers = this.handlers[event] ?? [];
|
|
8
|
-
for (const { handler } of handlers) {
|
|
9
|
-
handler(...data);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
on(event, handler) {
|
|
13
|
-
const key = event;
|
|
14
|
-
const id = this.id + 1;
|
|
15
|
-
if (!this.handlers[key]) {
|
|
16
|
-
this.handlers[key] = [];
|
|
17
|
-
}
|
|
18
|
-
this.handlers[key].push({ id, handler });
|
|
19
|
-
return this.off(event, id);
|
|
20
|
-
}
|
|
21
|
-
off(event, id) {
|
|
22
|
-
const key = event;
|
|
23
|
-
if (!this.handlers[key]) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
const index = this.handlers[key].findIndex(handler => handler.id === id);
|
|
27
|
-
if (index > 0) {
|
|
28
|
-
this.handlers[key].splice(index, 1);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
isEventHandled(event) {
|
|
32
|
-
const eventHandlers = this.handlers[event];
|
|
33
|
-
return !!eventHandlers && Object.values(eventHandlers).length > 0;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
class WebRtcDataChannel {
|
|
38
|
-
channel;
|
|
39
|
-
events = new EventEmitter();
|
|
40
|
-
oldMessages = [];
|
|
41
|
-
constructor(channel) {
|
|
42
|
-
this.channel = channel;
|
|
43
|
-
this.channel.onerror = event => this.events.emit('error', [event]);
|
|
44
|
-
this.channel.onmessage = event => this.events.emit('message', [event]);
|
|
45
|
-
this.channel.onclose = event => this.events.emit('close', [event]);
|
|
46
|
-
this.channel.onopen = event => {
|
|
47
|
-
this.events.emit('open', [event]);
|
|
48
|
-
for (const message of this.oldMessages) {
|
|
49
|
-
this.sendMessage(message);
|
|
50
|
-
}
|
|
51
|
-
this.oldMessages = [];
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
get raw() {
|
|
55
|
-
return this.channel;
|
|
56
|
-
}
|
|
57
|
-
get label() {
|
|
58
|
-
return this.channel.label;
|
|
59
|
-
}
|
|
60
|
-
sendMessage(message) {
|
|
61
|
-
if (this.channel.readyState === 'open') {
|
|
62
|
-
this.channel.send(message);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
this.oldMessages.push(message);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* This method does **not** filter messages. Use this method only intead of {@link on}
|
|
70
|
-
* if you are sure all messages sent over this channel will contain text.
|
|
71
|
-
*/
|
|
72
|
-
onTextMessage(callback) {
|
|
73
|
-
return this.events.on('message', async (event) => {
|
|
74
|
-
const buffer = await event.data.arrayBuffer();
|
|
75
|
-
const message = new TextDecoder('utf-8').decode(buffer);
|
|
76
|
-
callback(message);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
on = this.events.on.bind(this.events);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
class WebRtcConnection {
|
|
83
|
-
iceServers;
|
|
84
|
-
#peerConnection;
|
|
85
|
-
connected = false;
|
|
86
|
-
events = new EventEmitter();
|
|
87
|
-
onIceCandidate;
|
|
88
|
-
onConnectionEstablished;
|
|
89
|
-
constructor(props) {
|
|
90
|
-
this.iceServers = props.iceServers;
|
|
91
|
-
this.onIceCandidate = props.onCandidate;
|
|
92
|
-
this.onConnectionEstablished = props.onConnectionEstablished;
|
|
93
|
-
this.#peerConnection = this.createPeerConnection();
|
|
94
|
-
}
|
|
95
|
-
createPeerConnection() {
|
|
96
|
-
const config = {
|
|
97
|
-
iceServers: this.iceServers ?? [],
|
|
98
|
-
};
|
|
99
|
-
const connection = new RTCPeerConnection(config);
|
|
100
|
-
connection.addEventListener('connectionstatechange', () => {
|
|
101
|
-
if (connection.connectionState === 'disconnected' ||
|
|
102
|
-
connection.connectionState === 'failed' ||
|
|
103
|
-
connection.connectionState === 'closed') {
|
|
104
|
-
this.events.emit('connection_closed', []);
|
|
105
|
-
this.connected = false;
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
connection.addEventListener('iceconnectionstatechange', () => {
|
|
109
|
-
if (connection.connectionState === 'connected') {
|
|
110
|
-
this.onConnectionEstablished();
|
|
111
|
-
this.connected = true;
|
|
112
|
-
}
|
|
113
|
-
else if (this.connected) {
|
|
114
|
-
this.events.emit('connection_closed', []);
|
|
115
|
-
this.connected = false;
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
connection.onicecandidate = event => {
|
|
119
|
-
if (event.candidate) {
|
|
120
|
-
this.onIceCandidate(event.candidate);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
connection.ondatachannel = event => {
|
|
124
|
-
const channel = new WebRtcDataChannel(event.channel);
|
|
125
|
-
if (channel.label === 'ping-pong') {
|
|
126
|
-
channel.on('message', () => channel.sendMessage('pong'));
|
|
127
|
-
}
|
|
128
|
-
this.events.emit('data_channel', [channel]);
|
|
129
|
-
};
|
|
130
|
-
return connection;
|
|
131
|
-
}
|
|
132
|
-
get peerConnection() {
|
|
133
|
-
return this.#peerConnection;
|
|
134
|
-
}
|
|
135
|
-
createDataChannel(label, options) {
|
|
136
|
-
const channel = this.#peerConnection.createDataChannel(label, options);
|
|
137
|
-
return new WebRtcDataChannel(channel);
|
|
138
|
-
}
|
|
139
|
-
setRemoteDescription(type, sdp) {
|
|
140
|
-
return this.#peerConnection.setRemoteDescription({ type, sdp });
|
|
141
|
-
}
|
|
142
|
-
async createDescription(type) {
|
|
143
|
-
const description = await this.#peerConnection[type === 'offer' ? 'createOffer' : 'createAnswer']();
|
|
144
|
-
await this.#peerConnection.setLocalDescription(description);
|
|
145
|
-
return description;
|
|
146
|
-
}
|
|
147
|
-
addIceCandidate(candidate) {
|
|
148
|
-
return this.#peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
|
|
149
|
-
}
|
|
150
|
-
close() {
|
|
151
|
-
return this.#peerConnection.close();
|
|
152
|
-
}
|
|
153
|
-
on = this.events.on.bind(this.events);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const DEFAULT_SIGNALING_SERVER_URL = 'wss://signal.cloud.luxonis.com/session/';
|
|
157
|
-
class WebRtcClient {
|
|
158
|
-
config;
|
|
159
|
-
socket;
|
|
160
|
-
connection = null;
|
|
161
|
-
signalingConnectionRetries;
|
|
162
|
-
events = new EventEmitter();
|
|
163
|
-
iceCandidates = [];
|
|
164
|
-
constructor(config) {
|
|
165
|
-
this.config = config;
|
|
166
|
-
this.signalingConnectionRetries = (config.signalingConnectionRetries ?? 3) || 0;
|
|
167
|
-
this.connectSignalingServer();
|
|
168
|
-
}
|
|
169
|
-
get signalingServerConnected() {
|
|
170
|
-
return this.socket.readyState === WebSocket.OPEN;
|
|
171
|
-
}
|
|
172
|
-
disconnect(reason) {
|
|
173
|
-
this.connection?.close();
|
|
174
|
-
this.sendMessage({
|
|
175
|
-
Disconnect: {
|
|
176
|
-
type: 'normal',
|
|
177
|
-
reason,
|
|
178
|
-
},
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
connect() {
|
|
182
|
-
this.sendMessage({
|
|
183
|
-
ConnectSession: {
|
|
184
|
-
client_id: this.config.clientId,
|
|
185
|
-
application_identifier: this.config.applicationIdentifier,
|
|
186
|
-
auth_payload: this.config.authPayload,
|
|
187
|
-
},
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
connectSignalingServer() {
|
|
191
|
-
this.socket = new WebSocket(this.config.signalingServerUrl ?? DEFAULT_SIGNALING_SERVER_URL);
|
|
192
|
-
this.socket.onopen = () => {
|
|
193
|
-
this.connect();
|
|
194
|
-
};
|
|
195
|
-
// this.socket.onclose = () => console.debug('[WebRTC] Disconnected from signaling server');
|
|
196
|
-
this.socket.onerror = error => {
|
|
197
|
-
this.events.emit('error', ['signaling_connection_error', error]);
|
|
198
|
-
if (!this.events.isEventHandled('error')) {
|
|
199
|
-
console.error('[WebRTC] Signaling connection error:', error);
|
|
200
|
-
}
|
|
201
|
-
setTimeout(() => {
|
|
202
|
-
if (this.signalingConnectionRetries) {
|
|
203
|
-
this.signalingConnectionRetries -= 1;
|
|
204
|
-
if (this.socket.readyState === WebSocket.CLOSED || this.socket.readyState === WebSocket.CLOSING) {
|
|
205
|
-
// console.debug('[WebRTC] Reconnecting to signaling server');
|
|
206
|
-
this.connectSignalingServer();
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}, 3_000);
|
|
210
|
-
};
|
|
211
|
-
this.socket.onmessage = event => {
|
|
212
|
-
const message = JSON.parse(event.data);
|
|
213
|
-
this.handleSignalingMessage(message);
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
disconnectSignalingServer(reason) {
|
|
217
|
-
if (this.signalingServerConnected) {
|
|
218
|
-
this.sendMessage({
|
|
219
|
-
Disconnect: {
|
|
220
|
-
type: 'normal',
|
|
221
|
-
reason,
|
|
222
|
-
},
|
|
223
|
-
});
|
|
224
|
-
this.socket.close();
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
handleSignalingMessage(message) {
|
|
228
|
-
if ('AcknowledgeSession' in message) {
|
|
229
|
-
if (message.AcknowledgeSession.acknowledge) {
|
|
230
|
-
this.createConnection();
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
if (!this.events.isEventHandled('error')) {
|
|
234
|
-
console.error('[WebRTC] Connection refused:', message.AcknowledgeSession.reason);
|
|
235
|
-
}
|
|
236
|
-
this.events.emit('error', ['signaling_rejected', message.AcknowledgeSession.reason]);
|
|
237
|
-
this.dispose('Connection refused');
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
else if ('Candidate' in message) {
|
|
241
|
-
void this.handleCandidateMessage(message);
|
|
242
|
-
}
|
|
243
|
-
else if ('Disconnect' in message) {
|
|
244
|
-
this.dispose('Targed disconnected');
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
async handleCandidateMessage({ Candidate: { type: kind, candidate } }) {
|
|
248
|
-
if (!this.connection) {
|
|
249
|
-
// console.error('[WebRTC] Candidate message dropped: conenction not established');
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
// console.debug('[WebRTC] Processing candidate message', kind);
|
|
253
|
-
switch (kind) {
|
|
254
|
-
case 'answer':
|
|
255
|
-
case 'offer': {
|
|
256
|
-
const data = `${candidate}`;
|
|
257
|
-
if (data === 'null' || data === '') {
|
|
258
|
-
console.error('[WebRTC] Offer refused: remote description cannot be null');
|
|
259
|
-
this.reconnect('Error: received invalid candidate');
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
// console.debug('[WebRTC] Offer accepted. Setting remote description..');
|
|
263
|
-
await this.connection.setRemoteDescription(kind, candidate);
|
|
264
|
-
// console.debug('[WebRTC] Set remote description');
|
|
265
|
-
for (const candidate of this.iceCandidates) {
|
|
266
|
-
// console.debug('[WebRTC] Adding stored ICE candidate');
|
|
267
|
-
this.connection.addIceCandidate(JSON.parse(candidate));
|
|
268
|
-
}
|
|
269
|
-
this.iceCandidates = [];
|
|
270
|
-
if (kind === 'offer') {
|
|
271
|
-
const answer = await this.connection.createDescription('answer');
|
|
272
|
-
// console.debug('[WebRTC] Answer created (set local description)');
|
|
273
|
-
this.sendMessage({
|
|
274
|
-
Candidate: {
|
|
275
|
-
type: 'answer',
|
|
276
|
-
candidate: answer.sdp ?? '',
|
|
277
|
-
},
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
case 'candidate': {
|
|
283
|
-
if (this.connection.peerConnection.remoteDescription === null) {
|
|
284
|
-
// console.debug('[WebRTC] Storing ICE candidate (remote description is not set yet)');
|
|
285
|
-
this.iceCandidates.push(candidate);
|
|
286
|
-
}
|
|
287
|
-
else {
|
|
288
|
-
// console.debug('[WebRTC] Adding ICE candidate');
|
|
289
|
-
return this.connection.addIceCandidate(JSON.parse(candidate));
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
reconnect(reason) {
|
|
295
|
-
// console.debug(`[WebRTC] Reconnecting: ${reason}`);
|
|
296
|
-
this.disconnect(reason);
|
|
297
|
-
this.connect();
|
|
298
|
-
}
|
|
299
|
-
sendMessage(message) {
|
|
300
|
-
// console.debug('[WebRTC] Sending message', message);
|
|
301
|
-
if (this.socket.readyState === WebSocket.OPEN) {
|
|
302
|
-
this.socket.send(JSON.stringify(message));
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
createConnection() {
|
|
306
|
-
this.connection = new WebRtcConnection({
|
|
307
|
-
iceServers: this.config.iceServers,
|
|
308
|
-
onCandidate: candidate => {
|
|
309
|
-
this.sendMessage({
|
|
310
|
-
Candidate: {
|
|
311
|
-
type: 'candidate',
|
|
312
|
-
candidate: JSON.stringify(candidate),
|
|
313
|
-
},
|
|
314
|
-
});
|
|
315
|
-
},
|
|
316
|
-
onConnectionEstablished: () => this.disconnectSignalingServer('P2P connection established'),
|
|
317
|
-
});
|
|
318
|
-
this.events.emit('connection_established', [this.connection]);
|
|
319
|
-
}
|
|
320
|
-
dispose(reason) {
|
|
321
|
-
this.disconnect(reason);
|
|
322
|
-
this.disconnectSignalingServer(reason);
|
|
323
|
-
}
|
|
324
|
-
on = this.events.on.bind(this.events);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
328
|
-
// License, v2.0. If a copy of the MPL was not distributed with this
|
|
329
|
-
// file, You can obtain one at http://mozilla.org/MPL/2.0/
|
|
330
|
-
|
|
331
|
-
const MESSAGE_CHANNEL_NAME = "message";
|
|
332
|
-
const SYSTEM_CHANNEL_NAME = "system";
|
|
333
|
-
class WebRtcWorker extends FoxgloveCommunicator {
|
|
334
|
-
#webrtc;
|
|
335
|
-
#messageChannel = null;
|
|
336
|
-
#systemChannel = null;
|
|
337
|
-
#isReady = false;
|
|
338
|
-
#subscriptionId = 0;
|
|
339
|
-
#createWebRtc(config) {
|
|
340
|
-
this.#webrtc = new WebRtcClient(config);
|
|
341
|
-
}
|
|
342
|
-
#handleMessages() {
|
|
343
|
-
this.#webrtc.on("connection_established", connection => {
|
|
344
|
-
connection.on("data_channel", channel => {
|
|
345
|
-
// this.#onOpen(); // TODO:
|
|
346
|
-
|
|
347
|
-
if (channel.label === MESSAGE_CHANNEL_NAME) {
|
|
348
|
-
this.#messageChannel = channel;
|
|
349
|
-
channel.on("message", async ({
|
|
350
|
-
data: {
|
|
351
|
-
subscriptionId,
|
|
352
|
-
data
|
|
353
|
-
}
|
|
354
|
-
}) => {
|
|
355
|
-
this.handleGenericMessage(subscriptionId, data);
|
|
356
|
-
});
|
|
357
|
-
} else if (channel.label === SYSTEM_CHANNEL_NAME) {
|
|
358
|
-
this.#systemChannel = channel;
|
|
359
|
-
channel.on("message", async event => {
|
|
360
|
-
// TODO: Handle service call responses
|
|
361
|
-
const message = JSON.parse(event.data);
|
|
362
|
-
if (message.op === "advertise") {
|
|
363
|
-
this.handleChannelsAdvertisement(message.channels);
|
|
364
|
-
} else if (message.op === 'unadvertise') {
|
|
365
|
-
this.handleChannelsUnadvertisement(message.channelIds);
|
|
366
|
-
} else if (message.op === 'advertiseServices') {
|
|
367
|
-
this.handleServicesAdvertisement(message.services);
|
|
368
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
369
|
-
} else if (message.op === 'unadvertiseServices') {
|
|
370
|
-
this.handleServicesUnadvertisement(message.serviceIds);
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
connection.on("connection_closed", () => {
|
|
376
|
-
// this.#onClose(); // TODO:
|
|
377
|
-
});
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
isReady() {
|
|
381
|
-
return this.#isReady;
|
|
382
|
-
}
|
|
383
|
-
sendServiceCallRequest(request) {
|
|
384
|
-
// TODO: Should be binary
|
|
385
|
-
// https://github.com/foxglove/ws-protocol/blob/main/docs/spec.md#service-call-request
|
|
386
|
-
this.#messageChannel?.sendMessage(JSON.stringify(request));
|
|
387
|
-
}
|
|
388
|
-
subscribe(channel) {
|
|
389
|
-
this.#subscriptionId += 1;
|
|
390
|
-
this.#systemChannel?.sendMessage(JSON.stringify({
|
|
391
|
-
op: 'subscribe',
|
|
392
|
-
subscriptions: [{
|
|
393
|
-
id: this.#subscriptionId,
|
|
394
|
-
channelId: channel
|
|
395
|
-
}]
|
|
396
|
-
}));
|
|
397
|
-
return this.#subscriptionId;
|
|
398
|
-
}
|
|
399
|
-
constructor(config, send, sendWithTransfer) {
|
|
400
|
-
super();
|
|
401
|
-
this.send = send;
|
|
402
|
-
this.sendWithTransfer = sendWithTransfer;
|
|
403
|
-
this.#createWebRtc(config);
|
|
404
|
-
this.#handleMessages();
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// TODO: binary support
|
|
408
|
-
rawSend(channel, data) {
|
|
409
|
-
if (this.isReady()) {
|
|
410
|
-
const target = channel === 'message' ? this.#messageChannel : this.#systemChannel;
|
|
411
|
-
target?.sendMessage(data);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
close() {
|
|
415
|
-
this.#webrtc.dispose("Connection closed");
|
|
416
|
-
}
|
|
417
|
-
callService({
|
|
418
|
-
name,
|
|
419
|
-
serviceCallId,
|
|
420
|
-
body = {}
|
|
421
|
-
}) {
|
|
422
|
-
super.callService({
|
|
423
|
-
name,
|
|
424
|
-
serviceCallId,
|
|
425
|
-
body
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
function createWebRtcWorkerMessageListener(send, sendWithTransfer) {
|
|
430
|
-
let webrtcWorker = null;
|
|
431
|
-
return async event => {
|
|
432
|
-
const {
|
|
433
|
-
type,
|
|
434
|
-
data
|
|
435
|
-
} = event.data;
|
|
436
|
-
switch (type) {
|
|
437
|
-
case "open":
|
|
438
|
-
webrtcWorker = new WebRtcWorker(data.config, send, sendWithTransfer);
|
|
439
|
-
break;
|
|
440
|
-
case "close":
|
|
441
|
-
webrtcWorker?.close();
|
|
442
|
-
break;
|
|
443
|
-
case "send":
|
|
444
|
-
webrtcWorker?.rawSend(data.channel, data.message);
|
|
445
|
-
break;
|
|
446
|
-
case "call_service":
|
|
447
|
-
webrtcWorker?.callService(data);
|
|
448
|
-
break;
|
|
449
|
-
}
|
|
450
|
-
};
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
export { createWebRtcWorkerMessageListener as c };
|