weblib.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. let PARSEC;
  2. function float_to_int(fval) {
  3. let ival = fval < 0 ? fval * 32768 : fval * 32767;
  4. if (ival < -32768) {
  5. ival = -32768;
  6. } else if (ival > 32767) {
  7. ival = 32767;
  8. }
  9. return ival;
  10. }
  11. const PARSEC_ENV = {
  12. // user.bin
  13. bin_user_bin_get: function (asset_dir_c, session_id_c, size) {
  14. try {
  15. const cookies = document.cookie.split(";");
  16. for (let x = 0; x < cookies.length; x++) {
  17. const cookie = cookies[x].trim();
  18. const name = "parsec_login=";
  19. if (cookie.indexOf(name) == 0) {
  20. const auth = JSON.parse(
  21. cookie.substring(name.length, cookie.length)
  22. );
  23. mty_str_to_c(auth["token"], session_id_c, size);
  24. return 0;
  25. }
  26. }
  27. } catch (e) {
  28. console.error(e);
  29. }
  30. return -1;
  31. },
  32. bin_user_bin_set: function (asset_dir_c, session_id_c) {
  33. const session_id = mty_str_to_js(session_id_c);
  34. const value = JSON.stringify({
  35. token: session_id,
  36. userId: 0,
  37. });
  38. const hostname = window.location.hostname.replace(/.*?\./, "");
  39. const secure = window.location.protocol == "https:";
  40. document.cookie =
  41. "parsec_login=" +
  42. value +
  43. ";domain=" +
  44. hostname +
  45. ";path=/;" +
  46. (secure ? "secure" : "") +
  47. ";max-age=31536000;samesite=strict;";
  48. },
  49. bin_user_bin_delete: function (asset_dir_c) {
  50. const hostname = window.location.hostname.replace(/.*?\./, "");
  51. document.cookie =
  52. "parsec_login=;domain=" +
  53. hostname +
  54. ";path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
  55. },
  56. // Parsec protocol
  57. web_parsec_protocol: function (peer_id) {
  58. window.location.assign("parsec://peer_id=" + mty_str_to_js(peer_id));
  59. },
  60. // parsec SDK
  61. parsec_web_init: function () {
  62. if (PARSEC) return;
  63. const container = document.createElement("div");
  64. container.style.zIndex = -1;
  65. container.style.background = "black";
  66. container.style.position = "fixed";
  67. container.style.top = 0;
  68. container.style.right = 0;
  69. container.style.bottom = 0;
  70. container.style.left = 0;
  71. document.body.appendChild(container);
  72. const canvas = document.createElement("canvas");
  73. canvas.style.width = "100%";
  74. canvas.style.height = "100%";
  75. container.appendChild(canvas);
  76. const updateCanvas = (ev) => {
  77. const rect = canvas.getBoundingClientRect();
  78. canvas.width = rect.width;
  79. canvas.height = rect.height;
  80. window.resizeFlag = true;
  81. };
  82. updateCanvas(null);
  83. window.addEventListener("resize", updateCanvas);
  84. PARSEC = new Parsec(canvas);
  85. },
  86. parsec_web_destroy: function () {
  87. if (!PARSEC) return;
  88. PARSEC.destroy();
  89. PARSEC = undefined;
  90. },
  91. parsec_web_disconnect: function (e) {
  92. PARSEC.clientDisconnect(e);
  93. },
  94. parsec_web_get_status: function () {
  95. return PARSEC.clientGetStatus();
  96. },
  97. parsec_web_send_user_data: function (id, msg_c) {
  98. PARSEC.clientSendUserData(id, mty_str_to_js(msg_c));
  99. },
  100. parsec_web_get_guests: function (jstr_c, len) {
  101. mty_str_to_c(JSON.stringify(PARSEC.clientGetGuests()), jstr_c, len);
  102. },
  103. parsec_web_poll_events: function (event_str_c, len) {
  104. const evt = PARSEC.clientPollEvents();
  105. if (evt) {
  106. mty_str_to_c(JSON.stringify(evt), event_str_c, len);
  107. return true;
  108. }
  109. return false;
  110. },
  111. parsec_web_get_buffer_size: function (key) {
  112. return PARSEC.getBufferSize(key);
  113. },
  114. parsec_web_get_buffer: function (key, ptr) {
  115. const buffer = PARSEC.getBuffer(key);
  116. if (buffer) mty_memcpy(ptr, buffer);
  117. },
  118. parsec_web_send_message: function (msg_c) {
  119. PARSEC.clientSendMessage(JSON.parse(mty_str_to_js(msg_c)));
  120. },
  121. parsec_web_get_metrics: function (
  122. frame_w,
  123. frame_h,
  124. color444,
  125. full_range,
  126. decode,
  127. encode,
  128. network
  129. ) {
  130. const metrics = PARSEC.clientGetMetrics();
  131. mty_set_float(decode, metrics["decodeLatency"]);
  132. mty_set_float(encode, metrics["encodeLatency"]);
  133. mty_set_float(network, metrics["networkLatency"]);
  134. mty_set_uint32(frame_w, metrics["frameWidth"]);
  135. mty_set_uint32(frame_h, metrics["frameHeight"]);
  136. mty_set_int8(color444, metrics["444"]);
  137. mty_set_int8(full_range, metrics["fullRange"]);
  138. },
  139. parsec_web_get_network_failure: function () {
  140. return PARSEC.clientNetworkFailure();
  141. },
  142. parsec_web_get_self: function (owner_ptr, id_ptr) {
  143. const me = PARSEC.clientGetSelf();
  144. mty_set_int8(owner_ptr, me["owner"]);
  145. mty_set_uint32(id_ptr, me["id"]);
  146. },
  147. parsec_web_get_host_mode: function () {
  148. return PARSEC.clientGetHostMode();
  149. },
  150. parsec_web_new_attempt: async function (
  151. attempt_id,
  152. ufrag_c,
  153. pwd_c,
  154. fingerprint_c,
  155. buf_size,
  156. csync,
  157. err_c
  158. ) {
  159. const creds = await PARSEC.clientNewAttempt(mty_str_to_js(attempt_id));
  160. if (creds) {
  161. mty_set_uint32(err_c, 0);
  162. mty_str_to_c(creds["ice_ufrag"], ufrag_c, buf_size);
  163. mty_str_to_c(creds["ice_pwd"], pwd_c, buf_size);
  164. mty_str_to_c(creds["fingerprint"], fingerprint_c, buf_size);
  165. } else {
  166. mty_set_uint32(err_c, 1);
  167. }
  168. MTY_SignalPtr(csync);
  169. },
  170. parsec_web_begin_p2p: function (attempt_id, port, ufrag, pwd, fingerprint) {
  171. PARSEC.clientBeginP2P(
  172. mty_str_to_js(attempt_id),
  173. port,
  174. mty_str_to_js(ufrag),
  175. mty_str_to_js(pwd),
  176. mty_str_to_js(fingerprint)
  177. );
  178. },
  179. parsec_web_add_candidate: function (attempt_id, ip, port, sync, from_stun) {
  180. PARSEC.clientAddCandidate(
  181. mty_str_to_js(attempt_id),
  182. mty_str_to_js(ip),
  183. port,
  184. sync,
  185. from_stun
  186. );
  187. },
  188. parsec_web_poll_audio: function (cbuf, len) {
  189. const fbuf = PARSEC.clientPollAudio();
  190. if (fbuf) {
  191. const buf = new Int16Array(MTY_MEMORY.buffer, cbuf, len);
  192. for (let x = 0; x < fbuf.length; x++)
  193. buf[x] = float_to_int(fbuf[x]);
  194. return fbuf.length / 2;
  195. }
  196. return 0;
  197. },
  198. };