2023-01-10 14:26:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* library_godot_webrtc.js */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2020-10-17 21:31:30 +00:00
|
|
|
|
2020-11-19 15:54:07 +00:00
|
|
|
const GodotRTCDataChannel = {
|
2020-10-17 21:31:30 +00:00
|
|
|
// Our socket implementation that forwards events to C++.
|
2020-11-19 15:54:07 +00:00
|
|
|
$GodotRTCDataChannel__deps: ['$IDHandler', '$GodotRuntime'],
|
2020-10-17 21:31:30 +00:00
|
|
|
$GodotRTCDataChannel: {
|
2020-11-23 12:27:13 +00:00
|
|
|
connect: function (p_id, p_on_open, p_on_message, p_on_error, p_on_close) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref.binaryType = 'arraybuffer';
|
|
|
|
ref.onopen = function (event) {
|
|
|
|
p_on_open();
|
|
|
|
};
|
|
|
|
ref.onclose = function (event) {
|
|
|
|
p_on_close();
|
|
|
|
};
|
|
|
|
ref.onerror = function (event) {
|
|
|
|
p_on_error();
|
|
|
|
};
|
2020-11-23 12:27:13 +00:00
|
|
|
ref.onmessage = function (event) {
|
|
|
|
let buffer;
|
|
|
|
let is_string = 0;
|
2020-10-17 21:31:30 +00:00
|
|
|
if (event.data instanceof ArrayBuffer) {
|
|
|
|
buffer = new Uint8Array(event.data);
|
|
|
|
} else if (event.data instanceof Blob) {
|
2020-11-23 12:27:13 +00:00
|
|
|
GodotRuntime.error('Blob type not supported');
|
2020-10-17 21:31:30 +00:00
|
|
|
return;
|
2020-11-23 12:27:13 +00:00
|
|
|
} else if (typeof event.data === 'string') {
|
2020-10-17 21:31:30 +00:00
|
|
|
is_string = 1;
|
2020-11-23 12:27:13 +00:00
|
|
|
const enc = new TextEncoder('utf-8');
|
2020-10-17 21:31:30 +00:00
|
|
|
buffer = new Uint8Array(enc.encode(event.data));
|
|
|
|
} else {
|
2020-11-23 12:27:13 +00:00
|
|
|
GodotRuntime.error('Unknown message type');
|
2020-10-17 21:31:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-11-23 12:27:13 +00:00
|
|
|
const len = buffer.length * buffer.BYTES_PER_ELEMENT;
|
|
|
|
const out = GodotRuntime.malloc(len);
|
2020-10-17 21:31:30 +00:00
|
|
|
HEAPU8.set(buffer, out);
|
|
|
|
p_on_message(out, len, is_string);
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.free(out);
|
2020-11-23 12:27:13 +00:00
|
|
|
};
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
close: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref.onopen = null;
|
|
|
|
ref.onmessage = null;
|
|
|
|
ref.onerror = null;
|
|
|
|
ref.onclose = null;
|
|
|
|
ref.close();
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
get_prop: function (p_id, p_prop, p_def) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
return (ref && ref[p_prop] !== undefined) ? ref[p_prop] : p_def;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_ready_state_get__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_ready_state_get: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return 3; // CLOSED
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
switch (ref.readyState) {
|
|
|
|
case 'connecting':
|
|
|
|
return 0;
|
|
|
|
case 'open':
|
|
|
|
return 1;
|
|
|
|
case 'closing':
|
|
|
|
return 2;
|
|
|
|
case 'closed':
|
|
|
|
default:
|
|
|
|
return 3;
|
2020-10-17 21:31:30 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_send__sig: 'iiiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_send: function (p_id, p_buffer, p_length, p_raw) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bytes_array = new Uint8Array(p_length);
|
2020-11-23 12:27:13 +00:00
|
|
|
for (let i = 0; i < p_length; i++) {
|
2020-11-19 15:54:07 +00:00
|
|
|
bytes_array[i] = GodotRuntime.getHeapValue(p_buffer + i, 'i8');
|
2020-10-17 21:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_raw) {
|
|
|
|
ref.send(bytes_array.buffer);
|
|
|
|
} else {
|
|
|
|
const string = new TextDecoder('utf-8').decode(bytes_array);
|
|
|
|
ref.send(string);
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
return 0;
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_is_ordered__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_is_ordered: function (p_id) {
|
2021-07-18 01:03:37 +00:00
|
|
|
return GodotRTCDataChannel.get_prop(p_id, 'ordered', true);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_id_get__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_id_get: function (p_id) {
|
2021-07-18 01:03:37 +00:00
|
|
|
return GodotRTCDataChannel.get_prop(p_id, 'id', 65535);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_max_packet_lifetime_get__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_max_packet_lifetime_get: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return 65535;
|
|
|
|
}
|
|
|
|
if (ref['maxPacketLifeTime'] !== undefined) {
|
|
|
|
return ref['maxPacketLifeTime'];
|
|
|
|
} else if (ref['maxRetransmitTime'] !== undefined) {
|
|
|
|
// Guess someone didn't appreciate the standardization process.
|
|
|
|
return ref['maxRetransmitTime'];
|
|
|
|
}
|
|
|
|
return 65535;
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_max_retransmits_get__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_max_retransmits_get: function (p_id) {
|
2021-07-18 01:03:37 +00:00
|
|
|
return GodotRTCDataChannel.get_prop(p_id, 'maxRetransmits', 65535);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_is_negotiated__sig: 'ii',
|
|
|
|
godot_js_rtc_datachannel_is_negotiated: function (p_id) {
|
2021-07-18 01:03:37 +00:00
|
|
|
return GodotRTCDataChannel.get_prop(p_id, 'negotiated', 65535);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2021-07-20 16:44:40 +00:00
|
|
|
godot_js_rtc_datachannel_get_buffered_amount__sig: 'ii',
|
|
|
|
godot_js_rtc_datachannel_get_buffered_amount: function (p_id) {
|
|
|
|
return GodotRTCDataChannel.get_prop(p_id, 'bufferedAmount', 0);
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_label_get__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_label_get: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref || !ref.label) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
return GodotRuntime.allocString(ref.label);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_protocol_get__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_protocol_get: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref || !ref.protocol) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
return GodotRuntime.allocString(ref.protocol);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_destroy__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_destroy: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotRTCDataChannel.close(p_id);
|
|
|
|
IDHandler.remove(p_id);
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_connect__sig: 'viiiiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_connect: function (p_id, p_ref, p_on_open, p_on_message, p_on_error, p_on_close) {
|
2020-11-19 15:54:07 +00:00
|
|
|
const onopen = GodotRuntime.get_func(p_on_open).bind(null, p_ref);
|
|
|
|
const onmessage = GodotRuntime.get_func(p_on_message).bind(null, p_ref);
|
|
|
|
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_ref);
|
|
|
|
const onclose = GodotRuntime.get_func(p_on_close).bind(null, p_ref);
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotRTCDataChannel.connect(p_id, onopen, onmessage, onerror, onclose);
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_datachannel_close__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_datachannel_close: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GodotRTCDataChannel.close(p_id);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
autoAddDeps(GodotRTCDataChannel, '$GodotRTCDataChannel');
|
|
|
|
mergeInto(LibraryManager.library, GodotRTCDataChannel);
|
|
|
|
|
2020-11-19 15:54:07 +00:00
|
|
|
const GodotRTCPeerConnection = {
|
|
|
|
$GodotRTCPeerConnection__deps: ['$IDHandler', '$GodotRuntime', '$GodotRTCDataChannel'],
|
2020-10-17 21:31:30 +00:00
|
|
|
$GodotRTCPeerConnection: {
|
2020-11-23 12:27:13 +00:00
|
|
|
onstatechange: function (p_id, p_conn, callback, event) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-25 10:09:53 +00:00
|
|
|
let state;
|
2020-11-23 12:27:13 +00:00
|
|
|
switch (p_conn.iceConnectionState) {
|
|
|
|
case 'new':
|
|
|
|
state = 0;
|
|
|
|
break;
|
|
|
|
case 'checking':
|
|
|
|
state = 1;
|
|
|
|
break;
|
|
|
|
case 'connected':
|
|
|
|
case 'completed':
|
|
|
|
state = 2;
|
|
|
|
break;
|
|
|
|
case 'disconnected':
|
|
|
|
state = 3;
|
|
|
|
break;
|
|
|
|
case 'failed':
|
|
|
|
state = 4;
|
|
|
|
break;
|
|
|
|
case 'closed':
|
|
|
|
default:
|
|
|
|
state = 5;
|
|
|
|
break;
|
2020-10-17 21:31:30 +00:00
|
|
|
}
|
|
|
|
callback(state);
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
onicecandidate: function (p_id, callback, event) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref || !event.candidate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
const c = event.candidate;
|
|
|
|
const candidate_str = GodotRuntime.allocString(c.candidate);
|
|
|
|
const mid_str = GodotRuntime.allocString(c.sdpMid);
|
2020-10-17 21:31:30 +00:00
|
|
|
callback(mid_str, c.sdpMLineIndex, candidate_str);
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.free(candidate_str);
|
|
|
|
GodotRuntime.free(mid_str);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
ondatachannel: function (p_id, callback, event) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cid = IDHandler.add(event.channel);
|
|
|
|
callback(cid);
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
onsession: function (p_id, callback, session) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-23 12:27:13 +00:00
|
|
|
const type_str = GodotRuntime.allocString(session.type);
|
|
|
|
const sdp_str = GodotRuntime.allocString(session.sdp);
|
2020-10-17 21:31:30 +00:00
|
|
|
callback(type_str, sdp_str);
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.free(type_str);
|
|
|
|
GodotRuntime.free(sdp_str);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
onerror: function (p_id, callback, error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.error(error);
|
2020-10-17 21:31:30 +00:00
|
|
|
callback();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_create__sig: 'iiiiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_create: function (p_config, p_ref, p_on_state_change, p_on_candidate, p_on_datachannel) {
|
2020-11-19 15:54:07 +00:00
|
|
|
const onstatechange = GodotRuntime.get_func(p_on_state_change).bind(null, p_ref);
|
|
|
|
const oncandidate = GodotRuntime.get_func(p_on_candidate).bind(null, p_ref);
|
|
|
|
const ondatachannel = GodotRuntime.get_func(p_on_datachannel).bind(null, p_ref);
|
2020-10-17 21:31:30 +00:00
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
const config = JSON.parse(GodotRuntime.parseString(p_config));
|
|
|
|
let conn = null;
|
2020-10-17 21:31:30 +00:00
|
|
|
try {
|
|
|
|
conn = new RTCPeerConnection(config);
|
|
|
|
} catch (e) {
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.error(e);
|
2020-10-17 21:31:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base = GodotRTCPeerConnection;
|
|
|
|
const id = IDHandler.add(conn);
|
|
|
|
conn.oniceconnectionstatechange = base.onstatechange.bind(null, id, conn, onstatechange);
|
|
|
|
conn.onicecandidate = base.onicecandidate.bind(null, id, oncandidate);
|
|
|
|
conn.ondatachannel = base.ondatachannel.bind(null, id, ondatachannel);
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_close__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_close: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref.close();
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_destroy__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_destroy: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref.oniceconnectionstatechange = null;
|
|
|
|
ref.onicecandidate = null;
|
|
|
|
ref.ondatachannel = null;
|
|
|
|
IDHandler.remove(p_id);
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_offer_create__sig: 'viiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_offer_create: function (p_id, p_obj, p_on_session, p_on_error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
const onsession = GodotRuntime.get_func(p_on_session).bind(null, p_obj);
|
|
|
|
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_obj);
|
2020-11-23 12:27:13 +00:00
|
|
|
ref.createOffer().then(function (session) {
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotRTCPeerConnection.onsession(p_id, onsession, session);
|
2020-11-23 12:27:13 +00:00
|
|
|
}).catch(function (error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotRTCPeerConnection.onerror(p_id, onerror, error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_local_description_set__sig: 'viiiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_local_description_set: function (p_id, p_type, p_sdp, p_obj, p_on_error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
const type = GodotRuntime.parseString(p_type);
|
|
|
|
const sdp = GodotRuntime.parseString(p_sdp);
|
|
|
|
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_obj);
|
2020-10-17 21:31:30 +00:00
|
|
|
ref.setLocalDescription({
|
|
|
|
'sdp': sdp,
|
2020-11-23 12:27:13 +00:00
|
|
|
'type': type,
|
|
|
|
}).catch(function (error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotRTCPeerConnection.onerror(p_id, onerror, error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_remote_description_set__sig: 'viiiiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_remote_description_set: function (p_id, p_type, p_sdp, p_obj, p_session_created, p_on_error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
const type = GodotRuntime.parseString(p_type);
|
|
|
|
const sdp = GodotRuntime.parseString(p_sdp);
|
|
|
|
const onerror = GodotRuntime.get_func(p_on_error).bind(null, p_obj);
|
|
|
|
const onsession = GodotRuntime.get_func(p_session_created).bind(null, p_obj);
|
2020-10-17 21:31:30 +00:00
|
|
|
ref.setRemoteDescription({
|
|
|
|
'sdp': sdp,
|
2020-11-23 12:27:13 +00:00
|
|
|
'type': type,
|
|
|
|
}).then(function () {
|
2020-11-19 15:54:07 +00:00
|
|
|
if (type !== 'offer') {
|
|
|
|
return Promise.resolve();
|
2020-10-17 21:31:30 +00:00
|
|
|
}
|
2020-11-23 12:27:13 +00:00
|
|
|
return ref.createAnswer().then(function (session) {
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotRTCPeerConnection.onsession(p_id, onsession, session);
|
|
|
|
});
|
2020-11-23 12:27:13 +00:00
|
|
|
}).catch(function (error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotRTCPeerConnection.onerror(p_id, onerror, error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_ice_candidate_add__sig: 'viiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_ice_candidate_add: function (p_id, p_mid_name, p_mline_idx, p_sdp) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-23 12:27:13 +00:00
|
|
|
const sdpMidName = GodotRuntime.parseString(p_mid_name);
|
|
|
|
const sdpName = GodotRuntime.parseString(p_sdp);
|
2020-10-17 21:31:30 +00:00
|
|
|
ref.addIceCandidate(new RTCIceCandidate({
|
2020-11-23 12:27:13 +00:00
|
|
|
'candidate': sdpName,
|
|
|
|
'sdpMid': sdpMidName,
|
|
|
|
'sdpMlineIndex': p_mline_idx,
|
2020-10-17 21:31:30 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
godot_js_rtc_pc_datachannel_create__deps: ['$GodotRTCDataChannel'],
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_rtc_pc_datachannel_create__sig: 'iiii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_rtc_pc_datachannel_create: function (p_id, p_label, p_config) {
|
2020-10-17 21:31:30 +00:00
|
|
|
try {
|
|
|
|
const ref = IDHandler.get(p_id);
|
|
|
|
if (!ref) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:54:07 +00:00
|
|
|
const label = GodotRuntime.parseString(p_label);
|
|
|
|
const config = JSON.parse(GodotRuntime.parseString(p_config));
|
2020-10-17 21:31:30 +00:00
|
|
|
|
|
|
|
const channel = ref.createDataChannel(label, config);
|
|
|
|
return IDHandler.add(channel);
|
|
|
|
} catch (e) {
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.error(e);
|
2020-10-17 21:31:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
autoAddDeps(GodotRTCPeerConnection, '$GodotRTCPeerConnection');
|
2020-10-17 21:31:30 +00:00
|
|
|
mergeInto(LibraryManager.library, GodotRTCPeerConnection);
|