您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RTCPeerConnection.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { RTCPeerConnection, RTCSessionDescription } from 'react-native-webrtc';
  2. import { Platform } from '../../react';
  3. import { POSIX } from '../../react-native';
  4. // XXX At the time of this writing extending RTCPeerConnection using ES6 'class'
  5. // and 'extends' causes a runtime error related to the attempt to define the
  6. // onaddstream property setter. The error mentions that babelHelpers.set is
  7. // undefined which appears to be a thing inside React Native's packager. As a
  8. // workaround, extend using the pre-ES6 way.
  9. /**
  10. * The RTCPeerConnection provided by react-native-webrtc fires onaddstream
  11. * before it remembers remotedescription (and thus makes it available to API
  12. * clients). Because that appears to be a problem for lib-jitsi-meet which has
  13. * been successfully running on Chrome, Firefox, Temasys, etc. for a very long
  14. * time, attempt to meets its expectations (by extending RTCPPeerConnection).
  15. *
  16. * @class
  17. */
  18. export default function _RTCPeerConnection(...args) {
  19. /* eslint-disable no-invalid-this */
  20. RTCPeerConnection.apply(this, args);
  21. this.onaddstream = (...args) => // eslint-disable-line no-shadow
  22. (this._onaddstreamQueue
  23. ? this._queueOnaddstream
  24. : this._invokeOnaddstream)
  25. .apply(this, args);
  26. // Shadow RTCPeerConnection's onaddstream but after _RTCPeerConnection has
  27. // assigned to the property in question. Defining the property on
  28. // _RTCPeerConnection's prototype may (or may not, I don't know) work but I
  29. // don't want to try because the following approach appears to work and I
  30. // understand it.
  31. Object.defineProperty(this, 'onaddstream', {
  32. configurable: true,
  33. enumerable: true,
  34. get() {
  35. return this._onaddstream;
  36. },
  37. set(value) {
  38. this._onaddstream = value;
  39. }
  40. });
  41. /* eslint-enable no-invalid-this */
  42. }
  43. _RTCPeerConnection.prototype = Object.create(RTCPeerConnection.prototype);
  44. _RTCPeerConnection.prototype.constructor = _RTCPeerConnection;
  45. _RTCPeerConnection.prototype._invokeOnaddstream = function(...args) {
  46. const onaddstream = this._onaddstream;
  47. return onaddstream && onaddstream.apply(this, args);
  48. };
  49. _RTCPeerConnection.prototype._invokeQueuedOnaddstream = function(q) {
  50. q && q.forEach(args => {
  51. try {
  52. this._invokeOnaddstream(...args);
  53. } catch (e) {
  54. // TODO Determine whether the combination of the standard
  55. // setRemoteDescription and onaddstream results in a similar
  56. // swallowing of errors.
  57. _LOGE(e);
  58. }
  59. });
  60. };
  61. _RTCPeerConnection.prototype._queueOnaddstream = function(...args) {
  62. this._onaddstreamQueue.push(Array.from(args));
  63. };
  64. _RTCPeerConnection.prototype.setRemoteDescription = function(
  65. sessionDescription,
  66. successCallback,
  67. errorCallback) {
  68. // If the deprecated callback-based version is used, translate it to the
  69. // Promise-based version.
  70. if (typeof successCallback !== 'undefined'
  71. || typeof errorCallback !== 'undefined') {
  72. // XXX Returning a Promise is not necessary. But I don't see why it'd
  73. // hurt (much).
  74. return (
  75. _RTCPeerConnection.prototype.setRemoteDescription.call(
  76. this,
  77. sessionDescription)
  78. .then(successCallback, errorCallback));
  79. }
  80. return (
  81. _synthesizeIPv6Addresses(sessionDescription)
  82. .catch(reason => {
  83. reason && _LOGE(reason);
  84. return sessionDescription;
  85. })
  86. .then(value => _setRemoteDescription.bind(this)(value)));
  87. };
  88. /**
  89. * Logs at error level.
  90. *
  91. * @returns {void}
  92. */
  93. function _LOGE(...args) {
  94. console && console.error && console.error(...args);
  95. }
  96. /**
  97. * Adapts react-native-webrtc's {@link RTCPeerConnection#setRemoteDescription}
  98. * implementation which uses the deprecated, callback-based version to the
  99. * <tt>Promise</tt>-based version.
  100. *
  101. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  102. * which specifies the configuration of the remote end of the connection.
  103. * @returns {Promise}
  104. */
  105. function _setRemoteDescription(sessionDescription) {
  106. return new Promise((resolve, reject) => {
  107. /* eslint-disable no-invalid-this */
  108. // Ensure I'm not remembering onaddstream invocations from previous
  109. // setRemoteDescription calls. I shouldn't be but... anyway.
  110. this._onaddstreamQueue = [];
  111. RTCPeerConnection.prototype.setRemoteDescription.call(
  112. this,
  113. sessionDescription,
  114. (...args) => {
  115. let q;
  116. try {
  117. resolve(...args);
  118. } finally {
  119. q = this._onaddstreamQueue;
  120. this._onaddstreamQueue = undefined;
  121. }
  122. this._invokeQueuedOnaddstream(q);
  123. },
  124. (...args) => {
  125. this._onaddstreamQueue = undefined;
  126. reject(...args);
  127. });
  128. /* eslint-enable no-invalid-this */
  129. });
  130. }
  131. /**
  132. * Synthesize IPv6 addresses on iOS in order to support IPv6 NAT64 networks.
  133. *
  134. * @param {RTCSessionDescription} sdp - The RTCSessionDescription which
  135. * specifies the configuration of the remote end of the connection.
  136. * @returns {Promise}
  137. */
  138. function _synthesizeIPv6Addresses(sdp) {
  139. // The synthesis of IPv6 addresses is implemented on iOS only at the time of
  140. // this writing.
  141. if (Platform.OS !== 'ios') {
  142. return Promise.resolve(sdp);
  143. }
  144. return (
  145. new Promise(resolve => resolve(_synthesizeIPv6Addresses0(sdp)))
  146. .then(({ ips, lines }) =>
  147. Promise.all(Array.from(ips.values()))
  148. .then(() => _synthesizeIPv6Addresses1(sdp, ips, lines))
  149. ));
  150. }
  151. /* eslint-disable max-depth */
  152. /**
  153. * Implements the initial phase of the synthesis of IPv6 addresses.
  154. *
  155. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  156. * for which IPv6 addresses will be synthesized.
  157. * @returns {{
  158. * ips: Map,
  159. * lines: Array
  160. * }}
  161. */
  162. function _synthesizeIPv6Addresses0(sessionDescription) {
  163. const sdp = sessionDescription.sdp;
  164. let start = 0;
  165. const lines = [];
  166. const ips = new Map();
  167. do {
  168. const end = sdp.indexOf('\r\n', start);
  169. let line;
  170. if (end === -1) {
  171. line = sdp.substring(start);
  172. // Break out of the loop at the end of the iteration.
  173. start = undefined;
  174. } else {
  175. line = sdp.substring(start, end);
  176. start = end + 2;
  177. }
  178. if (line.startsWith('a=candidate:')) {
  179. const candidate = line.split(' ');
  180. if (candidate.length >= 10 && candidate[6] === 'typ') {
  181. const ip4s = [ candidate[4] ];
  182. let abort = false;
  183. for (let i = 8; i < candidate.length; ++i) {
  184. if (candidate[i] === 'raddr') {
  185. ip4s.push(candidate[++i]);
  186. break;
  187. }
  188. }
  189. for (const ip of ip4s) {
  190. if (ip.indexOf(':') === -1) {
  191. ips.has(ip)
  192. || ips.set(ip, new Promise((resolve, reject) => {
  193. const v = ips.get(ip);
  194. if (v && typeof v === 'string') {
  195. resolve(v);
  196. } else {
  197. POSIX.getaddrinfo(ip).then(
  198. value => {
  199. if (value.indexOf(':') === -1
  200. || value === ips.get(ip)) {
  201. ips.delete(ip);
  202. } else {
  203. ips.set(ip, value);
  204. }
  205. resolve(value);
  206. },
  207. reject);
  208. }
  209. }));
  210. } else {
  211. abort = true;
  212. break;
  213. }
  214. }
  215. if (abort) {
  216. ips.clear();
  217. break;
  218. }
  219. line = candidate;
  220. }
  221. }
  222. lines.push(line);
  223. } while (start);
  224. return {
  225. ips,
  226. lines
  227. };
  228. }
  229. /* eslint-enable max-depth */
  230. /**
  231. * Implements the initial phase of the synthesis of IPv6 addresses.
  232. *
  233. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  234. * for which IPv6 addresses are being synthesized.
  235. * @param {Map} ips - A Map of IPv4 addresses found in the specified
  236. * sessionDescription to synthesized IPv6 addresses.
  237. * @param {Array} lines - The lines of the specified sessionDescription.
  238. * @returns {RTCSessionDescription} A RTCSessionDescription that represents the
  239. * result of the synthesis of IPv6 addresses.
  240. */
  241. function _synthesizeIPv6Addresses1(sessionDescription, ips, lines) {
  242. if (ips.size === 0) {
  243. return sessionDescription;
  244. }
  245. for (let l = 0; l < lines.length; ++l) {
  246. const candidate = lines[l];
  247. if (typeof candidate !== 'string') {
  248. let ip4 = candidate[4];
  249. let ip6 = ips.get(ip4);
  250. ip6 && (candidate[4] = ip6);
  251. for (let i = 8; i < candidate.length; ++i) {
  252. if (candidate[i] === 'raddr') {
  253. ip4 = candidate[++i];
  254. (ip6 = ips.get(ip4)) && (candidate[i] = ip6);
  255. break;
  256. }
  257. }
  258. lines[l] = candidate.join(' ');
  259. }
  260. }
  261. return new RTCSessionDescription({
  262. sdp: lines.join('\r\n'),
  263. type: sessionDescription.type
  264. });
  265. }