| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
const GodotRTCDataChannel = { |
| 32 |
|
| 33 |
$GodotRTCDataChannel__deps: ['$IDHandler', '$GodotRuntime'], |
| 34 |
$GodotRTCDataChannel: { |
| 35 |
connect: function (p_id, p_on_open, p_on_message, p_on_error, p_on_close) { |
| 36 |
const ref = IDHandler.get(p_id); |
| 37 |
if (!ref) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
ref.binaryType = 'arraybuffer'; |
| 42 |
ref.onopen = function (event) { |
| 43 |
p_on_open(); |
| 44 |
}; |
| 45 |
ref.onclose = function (event) { |
| 46 |
p_on_close(); |
| 47 |
}; |
| 48 |
ref.onerror = function (event) { |
| 49 |
p_on_error(); |
| 50 |
}; |
| 51 |
ref.onmessage = function (event) { |
| 52 |
let buffer; |
| 53 |
let is_string = 0; |
| 54 |
if (event.data instanceof ArrayBuffer) { |
| 55 |
buffer = new Uint8Array(event.data); |
| 56 |
} else if (event.data instanceof Blob) { |
| 57 |
GodotRuntime.error('Blob type not supported'); |
| 58 |
return; |
| 59 |
} else if (typeof event.data === 'string') { |
| 60 |
is_string = 1; |
| 61 |
const enc = new TextEncoder('utf-8'); |
| 62 |
buffer = new Uint8Array(enc.encode(event.data)); |
| 63 |
} else { |
| 64 |
GodotRuntime.error('Unknown message type'); |
| 65 |
return; |
| 66 |
} |
| 67 |
const len = buffer.length * buffer.BYTES_PER_ELEMENT; |
| 68 |
const out = GodotRuntime.malloc(len); |
| 69 |
HEAPU8.set(buffer, out); |
| 70 |
p_on_message(out, len, is_string); |
| 71 |
GodotRuntime.free(out); |
| 72 |
}; |
| 73 |
}, |
| 74 |
|
| 75 |
close: function (p_id) { |
| 76 |
const ref = IDHandler.get(p_id); |
| 77 |
if (!ref) { |
| 78 |
return; |
| 79 |
} |
| 80 |
ref.onopen = null; |
| 81 |
ref.onmessage = null; |
| 82 |
ref.onerror = null; |
| 83 |
ref.onclose = null; |
| 84 |
ref.close(); |
| 85 |
}, |
| 86 |
|
| 87 |
get_prop: function (p_id, p_prop, p_def) { |
| 88 |
const ref = IDHandler.get(p_id); |
| 89 |
return (ref && ref[p_prop] !== undefined) ? ref[p_prop] : p_def; |
| 90 |
}, |
| 91 |
}, |
| 92 |
|
| 93 |
godot_js_rtc_datachannel_ready_state_get__proxy: 'sync', |
| 94 |
godot_js_rtc_datachannel_ready_state_get__sig: 'ii', |
| 95 |
godot_js_rtc_datachannel_ready_state_get: function (p_id) { |
| 96 |
const ref = IDHandler.get(p_id); |
| 97 |
if (!ref) { |
| 98 |
return 3; |
| 99 |
} |
| 100 |
|
| 101 |
switch (ref.readyState) { |
| 102 |
case 'connecting': |
| 103 |
return 0; |
| 104 |
case 'open': |
| 105 |
return 1; |
| 106 |
case 'closing': |
| 107 |
return 2; |
| 108 |
case 'closed': |
| 109 |
default: |
| 110 |
return 3; |
| 111 |
} |
| 112 |
}, |
| 113 |
|
| 114 |
godot_js_rtc_datachannel_send__proxy: 'sync', |
| 115 |
godot_js_rtc_datachannel_send__sig: 'iiiii', |
| 116 |
godot_js_rtc_datachannel_send: function (p_id, p_buffer, p_length, p_raw) { |
| 117 |
const ref = IDHandler.get(p_id); |
| 118 |
if (!ref) { |
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
const bytes_array = new Uint8Array(p_length); |
| 123 |
for (let i = 0; i < p_length; i++) { |
| 124 |
bytes_array[i] = GodotRuntime.getHeapValue(p_buffer + i, 'i8'); |
| 125 |
} |
| 126 |
|
| 127 |
if (p_raw) { |
| 128 |
ref.send(bytes_array.buffer); |
| 129 |
} else { |
| 130 |
const string = new TextDecoder('utf-8').decode(bytes_array); |
| 131 |
ref.send(string); |
| 132 |
} |
| 133 |
return 0; |
| 134 |
}, |
| 135 |
|
| 136 |
godot_js_rtc_datachannel_is_ordered__proxy: 'sync', |
| 137 |
godot_js_rtc_datachannel_is_ordered__sig: 'ii', |
| 138 |
godot_js_rtc_datachannel_is_ordered: function (p_id) { |
| 139 |
return GodotRTCDataChannel.get_prop(p_id, 'ordered', true); |
| 140 |
}, |
| 141 |
|
| 142 |
godot_js_rtc_datachannel_id_get__proxy: 'sync', |
| 143 |
godot_js_rtc_datachannel_id_get__sig: 'ii', |
| 144 |
godot_js_rtc_datachannel_id_get: function (p_id) { |
| 145 |
return GodotRTCDataChannel.get_prop(p_id, 'id', 65535); |
| 146 |
}, |
| 147 |
|
| 148 |
godot_js_rtc_datachannel_max_packet_lifetime_get__proxy: 'sync', |
| 149 |
godot_js_rtc_datachannel_max_packet_lifetime_get__sig: 'ii', |
| 150 |
godot_js_rtc_datachannel_max_packet_lifetime_get: function (p_id) { |
| 151 |
const ref = IDHandler.get(p_id); |
| 152 |
if (!ref) { |
| 153 |
return 65535; |
| 154 |
} |
| 155 |
if (ref['maxPacketLifeTime'] !== undefined) { |
| 156 |
return ref['maxPacketLifeTime']; |
| 157 |
} else if (ref['maxRetransmitTime'] !== undefined) { |
| 158 |
|
| 159 |
return ref['maxRetransmitTime']; |
| 160 |
} |
| 161 |
return 65535; |
| 162 |
}, |
| 163 |
|
| 164 |
godot_js_rtc_datachannel_max_retransmits_get__proxy: 'sync', |
| 165 |
godot_js_rtc_datachannel_max_retransmits_get__sig: 'ii', |
| 166 |
godot_js_rtc_datachannel_max_retransmits_get: function (p_id) { |
| 167 |
return GodotRTCDataChannel.get_prop(p_id, 'maxRetransmits', 65535); |
| 168 |
}, |
| 169 |
|
| 170 |
godot_js_rtc_datachannel_is_negotiated__proxy: 'sync', |
| 171 |
godot_js_rtc_datachannel_is_negotiated__sig: 'ii', |
| 172 |
godot_js_rtc_datachannel_is_negotiated: function (p_id) { |
| 173 |
return GodotRTCDataChannel.get_prop(p_id, 'negotiated', 65535); |
| 174 |
}, |
| 175 |
|
| 176 |
godot_js_rtc_datachannel_get_buffered_amount__proxy: 'sync', |
| 177 |
godot_js_rtc_datachannel_get_buffered_amount__sig: 'ii', |
| 178 |
godot_js_rtc_datachannel_get_buffered_amount: function (p_id) { |
| 179 |
return GodotRTCDataChannel.get_prop(p_id, 'bufferedAmount', 0); |
| 180 |
}, |
| 181 |
|
| 182 |
godot_js_rtc_datachannel_label_get__proxy: 'sync', |
| 183 |
godot_js_rtc_datachannel_label_get__sig: 'ii', |
| 184 |
godot_js_rtc_datachannel_label_get: function (p_id) { |
| 185 |
const ref = IDHandler.get(p_id); |
| 186 |
if (!ref || !ref.label) { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
return GodotRuntime.allocString(ref.label); |
| 190 |
}, |
| 191 |
|
| 192 |
godot_js_rtc_datachannel_protocol_get__sig: 'ii', |
| 193 |
godot_js_rtc_datachannel_protocol_get: function (p_id) { |
| 194 |
const ref = IDHandler.get(p_id); |
| 195 |
if (!ref || !ref.protocol) { |
| 196 |
return 0; |
| 197 |
} |
| 198 |
return GodotRuntime.allocString(ref.protocol); |
| 199 |
}, |
| 200 |
|
| 201 |
godot_js_rtc_datachannel_destroy__proxy: 'sync', |
| 202 |
godot_js_rtc_datachannel_destroy__sig: 'vi', |
| 203 |
godot_js_rtc_datachannel_destroy: function (p_id) { |
| 204 |
GodotRTCDataChannel.close(p_id); |
| 205 |
IDHandler.remove(p_id); |
| 206 |
}, |
| 207 |
|
| 208 |
godot_js_rtc_datachannel_connect__proxy: 'sync', |
| 209 |
godot_js_rtc_datachannel_connect__sig: 'viiiiii', |
| 210 |
godot_js_rtc_datachannel_connect: function (p_id, p_ref, p_on_open, p_on_message, p_on_error, p_on_close) { |
| 211 |
const onopen = GodotRuntime.get_func(p_on_open).bind(null, p_ref); |
| 212 |
const onmessage = GodotRuntime.get_func(p_on_message).bind(null, p_ref); |
| 213 |
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_ref); |
| 214 |
const onclose = GodotRuntime.get_func(p_on_close).bind(null, p_ref); |
| 215 |
GodotRTCDataChannel.connect(p_id, onopen, onmessage, onerror, onclose); |
| 216 |
}, |
| 217 |
|
| 218 |
godot_js_rtc_datachannel_close__proxy: 'sync', |
| 219 |
godot_js_rtc_datachannel_close__sig: 'vi', |
| 220 |
godot_js_rtc_datachannel_close: function (p_id) { |
| 221 |
const ref = IDHandler.get(p_id); |
| 222 |
if (!ref) { |
| 223 |
return; |
| 224 |
} |
| 225 |
GodotRTCDataChannel.close(p_id); |
| 226 |
}, |
| 227 |
}; |
| 228 |
|
| 229 |
autoAddDeps(GodotRTCDataChannel, '$GodotRTCDataChannel'); |
| 230 |
mergeInto(LibraryManager.library, GodotRTCDataChannel); |
| 231 |
|
| 232 |
const GodotRTCPeerConnection = { |
| 233 |
$GodotRTCPeerConnection__deps: ['$IDHandler', '$GodotRuntime', '$GodotRTCDataChannel'], |
| 234 |
$GodotRTCPeerConnection: { |
| 235 |
|
| 236 |
ConnectionState: { |
| 237 |
'new': 0, |
| 238 |
'connecting': 1, |
| 239 |
'connected': 2, |
| 240 |
'disconnected': 3, |
| 241 |
'failed': 4, |
| 242 |
'closed': 5, |
| 243 |
}, |
| 244 |
|
| 245 |
ConnectionStateCompat: { |
| 246 |
|
| 247 |
'new': 0, |
| 248 |
'checking': 1, |
| 249 |
'connected': 2, |
| 250 |
'completed': 2, |
| 251 |
'disconnected': 3, |
| 252 |
'failed': 4, |
| 253 |
'closed': 5, |
| 254 |
}, |
| 255 |
|
| 256 |
IceGatheringState: { |
| 257 |
'new': 0, |
| 258 |
'gathering': 1, |
| 259 |
'complete': 2, |
| 260 |
}, |
| 261 |
|
| 262 |
SignalingState: { |
| 263 |
'stable': 0, |
| 264 |
'have-local-offer': 1, |
| 265 |
'have-remote-offer': 2, |
| 266 |
'have-local-pranswer': 3, |
| 267 |
'have-remote-pranswer': 4, |
| 268 |
'closed': 5, |
| 269 |
}, |
| 270 |
|
| 271 |
|
| 272 |
create: function (config, onConnectionChange, onSignalingChange, onIceGatheringChange, onIceCandidate, onDataChannel) { |
| 273 |
let conn = null; |
| 274 |
try { |
| 275 |
conn = new RTCPeerConnection(config); |
| 276 |
} catch (e) { |
| 277 |
GodotRuntime.error(e); |
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
const id = IDHandler.add(conn); |
| 282 |
|
| 283 |
if ('connectionState' in conn && conn['connectionState'] !== undefined) { |
| 284 |
|
| 285 |
conn.onconnectionstatechange = function (event) { |
| 286 |
if (!IDHandler.get(id)) { |
| 287 |
return; |
| 288 |
} |
| 289 |
onConnectionChange(GodotRTCPeerConnection.ConnectionState[conn.connectionState] || 0); |
| 290 |
}; |
| 291 |
} else { |
| 292 |
|
| 293 |
conn.oniceconnectionstatechange = function (event) { |
| 294 |
if (!IDHandler.get(id)) { |
| 295 |
return; |
| 296 |
} |
| 297 |
onConnectionChange(GodotRTCPeerConnection.ConnectionStateCompat[conn.iceConnectionState] || 0); |
| 298 |
}; |
| 299 |
} |
| 300 |
conn.onicegatheringstatechange = function (event) { |
| 301 |
if (!IDHandler.get(id)) { |
| 302 |
return; |
| 303 |
} |
| 304 |
onIceGatheringChange(GodotRTCPeerConnection.IceGatheringState[conn.iceGatheringState] || 0); |
| 305 |
}; |
| 306 |
conn.onsignalingstatechange = function (event) { |
| 307 |
if (!IDHandler.get(id)) { |
| 308 |
return; |
| 309 |
} |
| 310 |
onSignalingChange(GodotRTCPeerConnection.SignalingState[conn.signalingState] || 0); |
| 311 |
}; |
| 312 |
conn.onicecandidate = function (event) { |
| 313 |
if (!IDHandler.get(id)) { |
| 314 |
return; |
| 315 |
} |
| 316 |
const c = event.candidate; |
| 317 |
if (!c || !c.candidate) { |
| 318 |
return; |
| 319 |
} |
| 320 |
const candidate_str = GodotRuntime.allocString(c.candidate); |
| 321 |
const mid_str = GodotRuntime.allocString(c.sdpMid); |
| 322 |
onIceCandidate(mid_str, c.sdpMLineIndex, candidate_str); |
| 323 |
GodotRuntime.free(candidate_str); |
| 324 |
GodotRuntime.free(mid_str); |
| 325 |
}; |
| 326 |
conn.ondatachannel = function (event) { |
| 327 |
if (!IDHandler.get(id)) { |
| 328 |
return; |
| 329 |
} |
| 330 |
const cid = IDHandler.add(event.channel); |
| 331 |
onDataChannel(cid); |
| 332 |
}; |
| 333 |
return id; |
| 334 |
}, |
| 335 |
|
| 336 |
destroy: function (p_id) { |
| 337 |
const conn = IDHandler.get(p_id); |
| 338 |
if (!conn) { |
| 339 |
return; |
| 340 |
} |
| 341 |
conn.onconnectionstatechange = null; |
| 342 |
conn.oniceconnectionstatechange = null; |
| 343 |
conn.onicegatheringstatechange = null; |
| 344 |
conn.onsignalingstatechange = null; |
| 345 |
conn.onicecandidate = null; |
| 346 |
conn.ondatachannel = null; |
| 347 |
IDHandler.remove(p_id); |
| 348 |
}, |
| 349 |
|
| 350 |
onsession: function (p_id, callback, session) { |
| 351 |
if (!IDHandler.get(p_id)) { |
| 352 |
return; |
| 353 |
} |
| 354 |
const type_str = GodotRuntime.allocString(session.type); |
| 355 |
const sdp_str = GodotRuntime.allocString(session.sdp); |
| 356 |
callback(type_str, sdp_str); |
| 357 |
GodotRuntime.free(type_str); |
| 358 |
GodotRuntime.free(sdp_str); |
| 359 |
}, |
| 360 |
|
| 361 |
onerror: function (p_id, callback, error) { |
| 362 |
const ref = IDHandler.get(p_id); |
| 363 |
if (!ref) { |
| 364 |
return; |
| 365 |
} |
| 366 |
GodotRuntime.error(error); |
| 367 |
callback(); |
| 368 |
}, |
| 369 |
}, |
| 370 |
|
| 371 |
godot_js_rtc_pc_create__proxy: 'sync', |
| 372 |
godot_js_rtc_pc_create__sig: 'iiiiiiii', |
| 373 |
godot_js_rtc_pc_create: function (p_config, p_ref, p_on_connection_state_change, p_on_ice_gathering_state_change, p_on_signaling_state_change, p_on_ice_candidate, p_on_datachannel) { |
| 374 |
const wrap = function (p_func) { |
| 375 |
return GodotRuntime.get_func(p_func).bind(null, p_ref); |
| 376 |
}; |
| 377 |
return GodotRTCPeerConnection.create( |
| 378 |
JSON.parse(GodotRuntime.parseString(p_config)), |
| 379 |
wrap(p_on_connection_state_change), |
| 380 |
wrap(p_on_signaling_state_change), |
| 381 |
wrap(p_on_ice_gathering_state_change), |
| 382 |
wrap(p_on_ice_candidate), |
| 383 |
wrap(p_on_datachannel) |
| 384 |
); |
| 385 |
}, |
| 386 |
|
| 387 |
godot_js_rtc_pc_close__proxy: 'sync', |
| 388 |
godot_js_rtc_pc_close__sig: 'vi', |
| 389 |
godot_js_rtc_pc_close: function (p_id) { |
| 390 |
const ref = IDHandler.get(p_id); |
| 391 |
if (!ref) { |
| 392 |
return; |
| 393 |
} |
| 394 |
ref.close(); |
| 395 |
}, |
| 396 |
|
| 397 |
godot_js_rtc_pc_destroy__proxy: 'sync', |
| 398 |
godot_js_rtc_pc_destroy__sig: 'vi', |
| 399 |
godot_js_rtc_pc_destroy: function (p_id) { |
| 400 |
GodotRTCPeerConnection.destroy(p_id); |
| 401 |
}, |
| 402 |
|
| 403 |
godot_js_rtc_pc_offer_create__proxy: 'sync', |
| 404 |
godot_js_rtc_pc_offer_create__sig: 'viiii', |
| 405 |
godot_js_rtc_pc_offer_create: function (p_id, p_obj, p_on_session, p_on_error) { |
| 406 |
const ref = IDHandler.get(p_id); |
| 407 |
if (!ref) { |
| 408 |
return; |
| 409 |
} |
| 410 |
const onsession = GodotRuntime.get_func(p_on_session).bind(null, p_obj); |
| 411 |
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_obj); |
| 412 |
ref.createOffer().then(function (session) { |
| 413 |
GodotRTCPeerConnection.onsession(p_id, onsession, session); |
| 414 |
}).catch(function (error) { |
| 415 |
GodotRTCPeerConnection.onerror(p_id, onerror, error); |
| 416 |
}); |
| 417 |
}, |
| 418 |
|
| 419 |
godot_js_rtc_pc_local_description_set__proxy: 'sync', |
| 420 |
godot_js_rtc_pc_local_description_set__sig: 'viiiii', |
| 421 |
godot_js_rtc_pc_local_description_set: function (p_id, p_type, p_sdp, p_obj, p_on_error) { |
| 422 |
const ref = IDHandler.get(p_id); |
| 423 |
if (!ref) { |
| 424 |
return; |
| 425 |
} |
| 426 |
const type = GodotRuntime.parseString(p_type); |
| 427 |
const sdp = GodotRuntime.parseString(p_sdp); |
| 428 |
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_obj); |
| 429 |
ref.setLocalDescription({ |
| 430 |
'sdp': sdp, |
| 431 |
'type': type, |
| 432 |
}).catch(function (error) { |
| 433 |
GodotRTCPeerConnection.onerror(p_id, onerror, error); |
| 434 |
}); |
| 435 |
}, |
| 436 |
|
| 437 |
godot_js_rtc_pc_remote_description_set__proxy: 'sync', |
| 438 |
godot_js_rtc_pc_remote_description_set__sig: 'viiiiii', |
| 439 |
godot_js_rtc_pc_remote_description_set: function (p_id, p_type, p_sdp, p_obj, p_session_created, p_on_error) { |
| 440 |
const ref = IDHandler.get(p_id); |
| 441 |
if (!ref) { |
| 442 |
return; |
| 443 |
} |
| 444 |
const type = GodotRuntime.parseString(p_type); |
| 445 |
const sdp = GodotRuntime.parseString(p_sdp); |
| 446 |
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_obj); |
| 447 |
const onsession = GodotRuntime.get_func(p_session_created).bind(null, p_obj); |
| 448 |
ref.setRemoteDescription({ |
| 449 |
'sdp': sdp, |
| 450 |
'type': type, |
| 451 |
}).then(function () { |
| 452 |
if (type !== 'offer') { |
| 453 |
return Promise.resolve(); |
| 454 |
} |
| 455 |
return ref.createAnswer().then(function (session) { |
| 456 |
GodotRTCPeerConnection.onsession(p_id, onsession, session); |
| 457 |
}); |
| 458 |
}).catch(function (error) { |
| 459 |
GodotRTCPeerConnection.onerror(p_id, onerror, error); |
| 460 |
}); |
| 461 |
}, |
| 462 |
|
| 463 |
godot_js_rtc_pc_ice_candidate_add__proxy: 'sync', |
| 464 |
godot_js_rtc_pc_ice_candidate_add__sig: 'viiii', |
| 465 |
godot_js_rtc_pc_ice_candidate_add: function (p_id, p_mid_name, p_mline_idx, p_sdp) { |
| 466 |
const ref = IDHandler.get(p_id); |
| 467 |
if (!ref) { |
| 468 |
return; |
| 469 |
} |
| 470 |
const sdpMidName = GodotRuntime.parseString(p_mid_name); |
| 471 |
const sdpName = GodotRuntime.parseString(p_sdp); |
| 472 |
ref.addIceCandidate(new RTCIceCandidate({ |
| 473 |
'candidate': sdpName, |
| 474 |
'sdpMid': sdpMidName, |
| 475 |
'sdpMlineIndex': p_mline_idx, |
| 476 |
})); |
| 477 |
}, |
| 478 |
|
| 479 |
godot_js_rtc_pc_datachannel_create__deps: ['$GodotRTCDataChannel'], |
| 480 |
godot_js_rtc_pc_datachannel_create__proxy: 'sync', |
| 481 |
godot_js_rtc_pc_datachannel_create__sig: 'iiii', |
| 482 |
godot_js_rtc_pc_datachannel_create: function (p_id, p_label, p_config) { |
| 483 |
try { |
| 484 |
const ref = IDHandler.get(p_id); |
| 485 |
if (!ref) { |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
const label = GodotRuntime.parseString(p_label); |
| 490 |
const config = JSON.parse(GodotRuntime.parseString(p_config)); |
| 491 |
|
| 492 |
const channel = ref.createDataChannel(label, config); |
| 493 |
return IDHandler.add(channel); |
| 494 |
} catch (e) { |
| 495 |
GodotRuntime.error(e); |
| 496 |
return 0; |
| 497 |
} |
| 498 |
}, |
| 499 |
}; |
| 500 |
|
| 501 |
autoAddDeps(GodotRTCPeerConnection, '$GodotRTCPeerConnection'); |
| 502 |
mergeInto(LibraryManager.library, GodotRTCPeerConnection); |