You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTCPeerConnection.js 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. * @private
  92. * @returns {void}
  93. */
  94. function _LOGE(...args) {
  95. console && console.error && console.error(...args);
  96. }
  97. /**
  98. * Adapts react-native-webrtc's {@link RTCPeerConnection#setRemoteDescription}
  99. * implementation which uses the deprecated, callback-based version to the
  100. * <tt>Promise</tt>-based version.
  101. *
  102. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  103. * which specifies the configuration of the remote end of the connection.
  104. * @private
  105. * @private
  106. * @returns {Promise}
  107. */
  108. function _setRemoteDescription(sessionDescription) {
  109. return new Promise((resolve, reject) => {
  110. /* eslint-disable no-invalid-this */
  111. // Ensure I'm not remembering onaddstream invocations from previous
  112. // setRemoteDescription calls. I shouldn't be but... anyway.
  113. this._onaddstreamQueue = [];
  114. RTCPeerConnection.prototype.setRemoteDescription.call(
  115. this,
  116. sessionDescription,
  117. (...args) => {
  118. let q;
  119. try {
  120. resolve(...args);
  121. } finally {
  122. q = this._onaddstreamQueue;
  123. this._onaddstreamQueue = undefined;
  124. }
  125. this._invokeQueuedOnaddstream(q);
  126. },
  127. (...args) => {
  128. this._onaddstreamQueue = undefined;
  129. reject(...args);
  130. });
  131. /* eslint-enable no-invalid-this */
  132. });
  133. }
  134. /**
  135. * Synthesize IPv6 addresses on iOS in order to support IPv6 NAT64 networks.
  136. *
  137. * @param {RTCSessionDescription} sdp - The RTCSessionDescription which
  138. * specifies the configuration of the remote end of the connection.
  139. * @private
  140. * @returns {Promise}
  141. */
  142. function _synthesizeIPv6Addresses(sdp) {
  143. // The synthesis of IPv6 addresses is implemented on iOS only at the time of
  144. // this writing.
  145. if (Platform.OS !== 'ios') {
  146. return Promise.resolve(sdp);
  147. }
  148. return (
  149. new Promise(resolve => resolve(_synthesizeIPv6Addresses0(sdp)))
  150. .then(({ ips, lines }) =>
  151. Promise.all(Array.from(ips.values()))
  152. .then(() => _synthesizeIPv6Addresses1(sdp, ips, lines))
  153. ));
  154. }
  155. /* eslint-disable max-depth */
  156. /**
  157. * Implements the initial phase of the synthesis of IPv6 addresses.
  158. *
  159. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  160. * for which IPv6 addresses will be synthesized.
  161. * @private
  162. * @returns {{
  163. * ips: Map,
  164. * lines: Array
  165. * }}
  166. */
  167. function _synthesizeIPv6Addresses0(sessionDescription) {
  168. const sdp = sessionDescription.sdp;
  169. let start = 0;
  170. const lines = [];
  171. const ips = new Map();
  172. do {
  173. const end = sdp.indexOf('\r\n', start);
  174. let line;
  175. if (end === -1) {
  176. line = sdp.substring(start);
  177. // Break out of the loop at the end of the iteration.
  178. start = undefined;
  179. } else {
  180. line = sdp.substring(start, end);
  181. start = end + 2;
  182. }
  183. if (line.startsWith('a=candidate:')) {
  184. const candidate = line.split(' ');
  185. if (candidate.length >= 10 && candidate[6] === 'typ') {
  186. const ip4s = [ candidate[4] ];
  187. let abort = false;
  188. for (let i = 8; i < candidate.length; ++i) {
  189. if (candidate[i] === 'raddr') {
  190. ip4s.push(candidate[++i]);
  191. break;
  192. }
  193. }
  194. for (const ip of ip4s) {
  195. if (ip.indexOf(':') === -1) {
  196. ips.has(ip)
  197. || ips.set(ip, new Promise((resolve, reject) => {
  198. const v = ips.get(ip);
  199. if (v && typeof v === 'string') {
  200. resolve(v);
  201. } else {
  202. POSIX.getaddrinfo(ip).then(
  203. value => {
  204. if (value.indexOf(':') === -1
  205. || value === ips.get(ip)) {
  206. ips.delete(ip);
  207. } else {
  208. ips.set(ip, value);
  209. }
  210. resolve(value);
  211. },
  212. reject);
  213. }
  214. }));
  215. } else {
  216. abort = true;
  217. break;
  218. }
  219. }
  220. if (abort) {
  221. ips.clear();
  222. break;
  223. }
  224. line = candidate;
  225. }
  226. }
  227. lines.push(line);
  228. } while (start);
  229. return {
  230. ips,
  231. lines
  232. };
  233. }
  234. /* eslint-enable max-depth */
  235. /**
  236. * Implements the initial phase of the synthesis of IPv6 addresses.
  237. *
  238. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  239. * for which IPv6 addresses are being synthesized.
  240. * @param {Map} ips - A Map of IPv4 addresses found in the specified
  241. * sessionDescription to synthesized IPv6 addresses.
  242. * @param {Array} lines - The lines of the specified sessionDescription.
  243. * @private
  244. * @returns {RTCSessionDescription} A RTCSessionDescription that represents the
  245. * result of the synthesis of IPv6 addresses.
  246. */
  247. function _synthesizeIPv6Addresses1(sessionDescription, ips, lines) {
  248. if (ips.size === 0) {
  249. return sessionDescription;
  250. }
  251. for (let l = 0; l < lines.length; ++l) {
  252. const candidate = lines[l];
  253. if (typeof candidate !== 'string') {
  254. let ip4 = candidate[4];
  255. let ip6 = ips.get(ip4);
  256. ip6 && (candidate[4] = ip6);
  257. for (let i = 8; i < candidate.length; ++i) {
  258. if (candidate[i] === 'raddr') {
  259. ip4 = candidate[++i];
  260. (ip6 = ips.get(ip4)) && (candidate[i] = ip6);
  261. break;
  262. }
  263. }
  264. lines[l] = candidate.join(' ');
  265. }
  266. }
  267. return new RTCSessionDescription({
  268. sdp: lines.join('\r\n'),
  269. type: sessionDescription.type
  270. });
  271. }