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

strophe.jingle.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* jshint -W117 */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var JingleSession = require("./JingleSessionPC");
  4. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  5. var RTCBrowserType = require("../RTC/RTCBrowserType");
  6. module.exports = function(XMPP, eventEmitter) {
  7. Strophe.addConnectionPlugin('jingle', {
  8. connection: null,
  9. sessions: {},
  10. ice_config: {iceServers: []},
  11. media_constraints: {
  12. mandatory: {
  13. 'OfferToReceiveAudio': true,
  14. 'OfferToReceiveVideo': true
  15. }
  16. // MozDontOfferDataChannel: true when this is firefox
  17. },
  18. init: function (conn) {
  19. this.connection = conn;
  20. if (this.connection.disco) {
  21. // http://xmpp.org/extensions/xep-0167.html#support
  22. // http://xmpp.org/extensions/xep-0176.html#support
  23. this.connection.disco.addFeature('urn:xmpp:jingle:1');
  24. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:1');
  25. this.connection.disco.addFeature('urn:xmpp:jingle:transports:ice-udp:1');
  26. this.connection.disco.addFeature('urn:xmpp:jingle:apps:dtls:0');
  27. this.connection.disco.addFeature('urn:xmpp:jingle:transports:dtls-sctp:1');
  28. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:audio');
  29. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:video');
  30. if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()
  31. || RTCBrowserType.isTemasysPluginUsed()) {
  32. this.connection.disco.addFeature('urn:ietf:rfc:4588');
  33. }
  34. // this is dealt with by SDP O/A so we don't need to announce this
  35. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
  36. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
  37. this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
  38. this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
  39. //this.connection.disco.addFeature('urn:ietf:rfc:5576'); // a=ssrc
  40. }
  41. this.connection.addHandler(this.onJingle.bind(this), 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
  42. },
  43. onJingle: function (iq) {
  44. var sid = $(iq).find('jingle').attr('sid');
  45. var action = $(iq).find('jingle').attr('action');
  46. var fromJid = iq.getAttribute('from');
  47. // send ack first
  48. var ack = $iq({type: 'result',
  49. to: fromJid,
  50. id: iq.getAttribute('id')
  51. });
  52. logger.log('on jingle ' + action + ' from ' + fromJid, iq);
  53. var sess = this.sessions[sid];
  54. if ('session-initiate' != action) {
  55. if (!sess) {
  56. ack.attrs({ type: 'error' });
  57. ack.c('error', {type: 'cancel'})
  58. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  59. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  60. logger.warn('invalid session id', iq);
  61. this.connection.send(ack);
  62. return true;
  63. }
  64. // local jid is not checked
  65. if (fromJid != sess.peerjid) {
  66. logger.warn(
  67. 'jid mismatch for session id', sid, sess.peerjid, iq);
  68. ack.attrs({ type: 'error' });
  69. ack.c('error', {type: 'cancel'})
  70. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  71. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  72. this.connection.send(ack);
  73. return true;
  74. }
  75. } else if (sess !== undefined) {
  76. // existing session with same session id
  77. // this might be out-of-order if the sess.peerjid is the same as from
  78. ack.attrs({ type: 'error' });
  79. ack.c('error', {type: 'cancel'})
  80. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  81. logger.warn('duplicate session id', sid, iq);
  82. this.connection.send(ack);
  83. return true;
  84. }
  85. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  86. switch (action) {
  87. case 'session-initiate':
  88. var now = window.performance.now();
  89. logger.log("(TIME) received session-initiate:\t", now);
  90. var startMuted = $(iq).find('jingle>startmuted');
  91. if (startMuted && startMuted.length > 0) {
  92. var audioMuted = startMuted.attr("audio");
  93. var videoMuted = startMuted.attr("video");
  94. eventEmitter.emit(XMPPEvents.START_MUTED_FROM_FOCUS,
  95. audioMuted === "true", videoMuted === "true");
  96. }
  97. sess = new JingleSession(
  98. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  99. fromJid,
  100. this.connection,
  101. this.media_constraints,
  102. this.ice_config, XMPP);
  103. this.sessions[sess.sid] = sess;
  104. var jingleOffer = $(iq).find('>jingle');
  105. // FIXME there's no nice way with event to get the reason
  106. // why the call was rejected
  107. eventEmitter.emit(XMPPEvents.CALL_INCOMING, sess, jingleOffer, now);
  108. if (!sess.active())
  109. {
  110. // Call not accepted
  111. ack.attrs({ type: 'error' });
  112. ack.c('error', {type: 'cancel'})
  113. .c('bad-request',
  114. { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' })
  115. .up();
  116. this.terminate(sess.sid);
  117. }
  118. break;
  119. case 'session-terminate':
  120. logger.log('terminating...', sess.sid);
  121. var reasonCondition = null;
  122. var reasonText = null;
  123. if ($(iq).find('>jingle>reason').length) {
  124. reasonCondition
  125. = $(iq).find('>jingle>reason>:first')[0].tagName;
  126. reasonText = $(iq).find('>jingle>reason>text').text();
  127. }
  128. this.terminate(sess.sid, reasonCondition, reasonText);
  129. break;
  130. case 'transport-replace':
  131. logger.info("(TIME) Start transport replace",
  132. window.performance.now());
  133. sess.replaceTransport($(iq).find('>jingle'),
  134. function () {
  135. logger.info(
  136. "(TIME) Transport replace success!",
  137. window.performance.now());
  138. },
  139. function(error) {
  140. logger.error('Transport replace failed', error);
  141. sess.sendTransportReject();
  142. });
  143. break;
  144. case 'addsource': // FIXME: proprietary, un-jingleish
  145. case 'source-add': // FIXME: proprietary
  146. sess.addSource($(iq).find('>jingle>content'));
  147. break;
  148. case 'removesource': // FIXME: proprietary, un-jingleish
  149. case 'source-remove': // FIXME: proprietary
  150. sess.removeSource($(iq).find('>jingle>content'));
  151. break;
  152. default:
  153. logger.warn('jingle action not implemented', action);
  154. ack.attrs({ type: 'error' });
  155. ack.c('error', {type: 'cancel'})
  156. .c('bad-request',
  157. { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' })
  158. .up();
  159. break;
  160. }
  161. this.connection.send(ack);
  162. return true;
  163. },
  164. terminate: function (sid, reasonCondition, reasonText) {
  165. if (this.sessions.hasOwnProperty(sid)) {
  166. if (this.sessions[sid].state != 'ended') {
  167. this.sessions[sid].onTerminated(reasonCondition, reasonText);
  168. }
  169. delete this.sessions[sid];
  170. }
  171. },
  172. getStunAndTurnCredentials: function () {
  173. // get stun and turn configuration from server via xep-0215
  174. // uses time-limited credentials as described in
  175. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  176. //
  177. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  178. // for a prosody module which implements this
  179. //
  180. // currently, this doesn't work with updateIce and therefore credentials with a long
  181. // validity have to be fetched before creating the peerconnection
  182. // TODO: implement refresh via updateIce as described in
  183. // https://code.google.com/p/webrtc/issues/detail?id=1650
  184. var self = this;
  185. this.connection.sendIQ(
  186. $iq({type: 'get', to: this.connection.domain})
  187. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  188. function (res) {
  189. var iceservers = [];
  190. $(res).find('>services>service').each(function (idx, el) {
  191. el = $(el);
  192. var dict = {};
  193. var type = el.attr('type');
  194. switch (type) {
  195. case 'stun':
  196. dict.url = 'stun:' + el.attr('host');
  197. if (el.attr('port')) {
  198. dict.url += ':' + el.attr('port');
  199. }
  200. iceservers.push(dict);
  201. break;
  202. case 'turn':
  203. case 'turns':
  204. dict.url = type + ':';
  205. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  206. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  207. dict.url += el.attr('username') + '@';
  208. } else {
  209. dict.username = el.attr('username'); // only works in M28
  210. }
  211. }
  212. dict.url += el.attr('host');
  213. if (el.attr('port') && el.attr('port') != '3478') {
  214. dict.url += ':' + el.attr('port');
  215. }
  216. if (el.attr('transport') && el.attr('transport') != 'udp') {
  217. dict.url += '?transport=' + el.attr('transport');
  218. }
  219. if (el.attr('password')) {
  220. dict.credential = el.attr('password');
  221. }
  222. iceservers.push(dict);
  223. break;
  224. }
  225. });
  226. self.ice_config.iceServers = iceservers;
  227. },
  228. function (err) {
  229. logger.warn('getting turn credentials failed', err);
  230. logger.warn('is mod_turncredentials or similar installed?');
  231. }
  232. );
  233. // implement push?
  234. },
  235. /**
  236. * Returns the data saved in 'updateLog' in a format to be logged.
  237. */
  238. getLog: function () {
  239. var data = {};
  240. var self = this;
  241. Object.keys(this.sessions).forEach(function (sid) {
  242. var session = self.sessions[sid];
  243. if (session.peerconnection && session.peerconnection.updateLog) {
  244. // FIXME: should probably be a .dump call
  245. data["jingle_" + session.sid] = {
  246. updateLog: session.peerconnection.updateLog,
  247. stats: session.peerconnection.stats,
  248. url: window.location.href
  249. };
  250. }
  251. });
  252. return data;
  253. }
  254. });
  255. };