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

RTCPeerConnection.js 9.9KB

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