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.

strophe.jingle.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* jshint -W117 */
  2. function CallIncomingJingle(sid) {
  3. var sess = connection.jingle.sessions[sid];
  4. // TODO: do we check activecall == null?
  5. activecall = sess;
  6. statistics.onConferenceCreated(sess);
  7. RTC.onConferenceCreated(sess);
  8. // TODO: check affiliation and/or role
  9. console.log('emuc data for', sess.peerjid, connection.emuc.members[sess.peerjid]);
  10. sess.usedrip = true; // not-so-naive trickle ice
  11. sess.sendAnswer();
  12. sess.accept();
  13. };
  14. Strophe.addConnectionPlugin('jingle', {
  15. connection: null,
  16. sessions: {},
  17. jid2session: {},
  18. ice_config: {iceServers: []},
  19. pc_constraints: {},
  20. media_constraints: {
  21. mandatory: {
  22. 'OfferToReceiveAudio': true,
  23. 'OfferToReceiveVideo': true
  24. }
  25. // MozDontOfferDataChannel: true when this is firefox
  26. },
  27. localAudio: null,
  28. localVideo: null,
  29. init: function (conn) {
  30. this.connection = conn;
  31. if (this.connection.disco) {
  32. // http://xmpp.org/extensions/xep-0167.html#support
  33. // http://xmpp.org/extensions/xep-0176.html#support
  34. this.connection.disco.addFeature('urn:xmpp:jingle:1');
  35. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:1');
  36. this.connection.disco.addFeature('urn:xmpp:jingle:transports:ice-udp:1');
  37. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:audio');
  38. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:video');
  39. // this is dealt with by SDP O/A so we don't need to annouce this
  40. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
  41. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
  42. if (config.useRtcpMux) {
  43. this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
  44. }
  45. if (config.useBundle) {
  46. this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
  47. }
  48. //this.connection.disco.addFeature('urn:ietf:rfc:5576'); // a=ssrc
  49. }
  50. this.connection.addHandler(this.onJingle.bind(this), 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
  51. },
  52. onJingle: function (iq) {
  53. var sid = $(iq).find('jingle').attr('sid');
  54. var action = $(iq).find('jingle').attr('action');
  55. var fromJid = iq.getAttribute('from');
  56. // send ack first
  57. var ack = $iq({type: 'result',
  58. to: fromJid,
  59. id: iq.getAttribute('id')
  60. });
  61. console.log('on jingle ' + action + ' from ' + fromJid, iq);
  62. var sess = this.sessions[sid];
  63. if ('session-initiate' != action) {
  64. if (sess === null) {
  65. ack.type = 'error';
  66. ack.c('error', {type: 'cancel'})
  67. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  68. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  69. this.connection.send(ack);
  70. return true;
  71. }
  72. // compare from to sess.peerjid (bare jid comparison for later compat with message-mode)
  73. // local jid is not checked
  74. if (Strophe.getBareJidFromJid(fromJid) != Strophe.getBareJidFromJid(sess.peerjid)) {
  75. console.warn('jid mismatch for session id', sid, fromJid, sess.peerjid);
  76. ack.type = 'error';
  77. ack.c('error', {type: 'cancel'})
  78. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  79. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  80. this.connection.send(ack);
  81. return true;
  82. }
  83. } else if (sess !== undefined) {
  84. // existing session with same session id
  85. // this might be out-of-order if the sess.peerjid is the same as from
  86. ack.type = 'error';
  87. ack.c('error', {type: 'cancel'})
  88. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  89. console.warn('duplicate session id', sid);
  90. this.connection.send(ack);
  91. return true;
  92. }
  93. // FIXME: check for a defined action
  94. this.connection.send(ack);
  95. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  96. switch (action) {
  97. case 'session-initiate':
  98. sess = new JingleSession($(iq).attr('to'), $(iq).find('jingle').attr('sid'), this.connection);
  99. // configure session
  100. //in firefox we have only one stream object
  101. if (this.localAudio != this.localVideo) {
  102. sess.localStreams.push(this.localAudio);
  103. }
  104. if (this.localVideo) {
  105. sess.localStreams.push(this.localVideo);
  106. }
  107. sess.media_constraints = this.media_constraints;
  108. sess.pc_constraints = this.pc_constraints;
  109. sess.ice_config = this.ice_config;
  110. sess.initiate(fromJid, false);
  111. // FIXME: setRemoteDescription should only be done when this call is to be accepted
  112. sess.setRemoteDescription($(iq).find('>jingle'), 'offer');
  113. this.sessions[sess.sid] = sess;
  114. this.jid2session[sess.peerjid] = sess;
  115. // the callback should either
  116. // .sendAnswer and .accept
  117. // or .sendTerminate -- not necessarily synchronus
  118. CallIncomingJingle(sess.sid);
  119. break;
  120. case 'session-accept':
  121. sess.setRemoteDescription($(iq).find('>jingle'), 'answer');
  122. sess.accept();
  123. $(document).trigger('callaccepted.jingle', [sess.sid]);
  124. break;
  125. case 'session-terminate':
  126. // If this is not the focus sending the terminate, we have
  127. // nothing more to do here.
  128. if (Object.keys(this.sessions).length < 1
  129. || !(this.sessions[Object.keys(this.sessions)[0]]
  130. instanceof JingleSession))
  131. {
  132. break;
  133. }
  134. console.log('terminating...', sess.sid);
  135. sess.terminate();
  136. this.terminate(sess.sid);
  137. if ($(iq).find('>jingle>reason').length) {
  138. $(document).trigger('callterminated.jingle', [
  139. sess.sid,
  140. sess.peerjid,
  141. $(iq).find('>jingle>reason>:first')[0].tagName,
  142. $(iq).find('>jingle>reason>text').text()
  143. ]);
  144. } else {
  145. $(document).trigger('callterminated.jingle',
  146. [sess.sid, sess.peerjid]);
  147. }
  148. break;
  149. case 'transport-info':
  150. sess.addIceCandidate($(iq).find('>jingle>content'));
  151. break;
  152. case 'session-info':
  153. var affected;
  154. if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  155. $(document).trigger('ringing.jingle', [sess.sid]);
  156. } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  157. affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  158. $(document).trigger('mute.jingle', [sess.sid, affected]);
  159. } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  160. affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  161. $(document).trigger('unmute.jingle', [sess.sid, affected]);
  162. }
  163. break;
  164. case 'addsource': // FIXME: proprietary, un-jingleish
  165. case 'source-add': // FIXME: proprietary
  166. sess.addSource($(iq).find('>jingle>content'), fromJid);
  167. break;
  168. case 'removesource': // FIXME: proprietary, un-jingleish
  169. case 'source-remove': // FIXME: proprietary
  170. sess.removeSource($(iq).find('>jingle>content'), fromJid);
  171. break;
  172. default:
  173. console.warn('jingle action not implemented', action);
  174. break;
  175. }
  176. return true;
  177. },
  178. initiate: function (peerjid, myjid) { // initiate a new jinglesession to peerjid
  179. var sess = new JingleSession(myjid || this.connection.jid,
  180. Math.random().toString(36).substr(2, 12), // random string
  181. this.connection);
  182. // configure session
  183. //in firefox we have only one stream
  184. if (this.localAudio != this.localVideo) {
  185. sess.localStreams.push(this.localAudio);
  186. }
  187. if (this.localVideo) {
  188. sess.localStreams.push(this.localVideo);
  189. }
  190. sess.media_constraints = this.media_constraints;
  191. sess.pc_constraints = this.pc_constraints;
  192. sess.ice_config = this.ice_config;
  193. sess.initiate(peerjid, true);
  194. this.sessions[sess.sid] = sess;
  195. this.jid2session[sess.peerjid] = sess;
  196. sess.sendOffer();
  197. return sess;
  198. },
  199. terminate: function (sid, reason, text) { // terminate by sessionid (or all sessions)
  200. if (sid === null || sid === undefined) {
  201. for (sid in this.sessions) {
  202. if (this.sessions[sid].state != 'ended') {
  203. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  204. this.sessions[sid].terminate();
  205. }
  206. delete this.jid2session[this.sessions[sid].peerjid];
  207. delete this.sessions[sid];
  208. }
  209. } else if (this.sessions.hasOwnProperty(sid)) {
  210. if (this.sessions[sid].state != 'ended') {
  211. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  212. this.sessions[sid].terminate();
  213. }
  214. delete this.jid2session[this.sessions[sid].peerjid];
  215. delete this.sessions[sid];
  216. }
  217. },
  218. // Used to terminate a session when an unavailable presence is received.
  219. terminateByJid: function (jid) {
  220. if (this.jid2session.hasOwnProperty(jid)) {
  221. var sess = this.jid2session[jid];
  222. if (sess) {
  223. sess.terminate();
  224. console.log('peer went away silently', jid);
  225. delete this.sessions[sess.sid];
  226. delete this.jid2session[jid];
  227. $(document).trigger('callterminated.jingle',
  228. [sess.sid, jid], 'gone');
  229. }
  230. }
  231. },
  232. terminateRemoteByJid: function (jid, reason) {
  233. if (this.jid2session.hasOwnProperty(jid)) {
  234. var sess = this.jid2session[jid];
  235. if (sess) {
  236. sess.sendTerminate(reason || (!sess.active()) ? 'kick' : null);
  237. sess.terminate();
  238. console.log('terminate peer with jid', sess.sid, jid);
  239. delete this.sessions[sess.sid];
  240. delete this.jid2session[jid];
  241. $(document).trigger('callterminated.jingle',
  242. [sess.sid, jid, 'kicked']);
  243. }
  244. }
  245. },
  246. getStunAndTurnCredentials: function () {
  247. // get stun and turn configuration from server via xep-0215
  248. // uses time-limited credentials as described in
  249. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  250. //
  251. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  252. // for a prosody module which implements this
  253. //
  254. // currently, this doesn't work with updateIce and therefore credentials with a long
  255. // validity have to be fetched before creating the peerconnection
  256. // TODO: implement refresh via updateIce as described in
  257. // https://code.google.com/p/webrtc/issues/detail?id=1650
  258. var self = this;
  259. this.connection.sendIQ(
  260. $iq({type: 'get', to: this.connection.domain})
  261. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  262. function (res) {
  263. var iceservers = [];
  264. $(res).find('>services>service').each(function (idx, el) {
  265. el = $(el);
  266. var dict = {};
  267. var type = el.attr('type');
  268. switch (type) {
  269. case 'stun':
  270. dict.url = 'stun:' + el.attr('host');
  271. if (el.attr('port')) {
  272. dict.url += ':' + el.attr('port');
  273. }
  274. iceservers.push(dict);
  275. break;
  276. case 'turn':
  277. case 'turns':
  278. dict.url = type + ':';
  279. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  280. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  281. dict.url += el.attr('username') + '@';
  282. } else {
  283. dict.username = el.attr('username'); // only works in M28
  284. }
  285. }
  286. dict.url += el.attr('host');
  287. if (el.attr('port') && el.attr('port') != '3478') {
  288. dict.url += ':' + el.attr('port');
  289. }
  290. if (el.attr('transport') && el.attr('transport') != 'udp') {
  291. dict.url += '?transport=' + el.attr('transport');
  292. }
  293. if (el.attr('password')) {
  294. dict.credential = el.attr('password');
  295. }
  296. iceservers.push(dict);
  297. break;
  298. }
  299. });
  300. self.ice_config.iceServers = iceservers;
  301. },
  302. function (err) {
  303. console.warn('getting turn credentials failed', err);
  304. console.warn('is mod_turncredentials or similar installed?');
  305. }
  306. );
  307. // implement push?
  308. },
  309. /**
  310. * Populates the log data
  311. */
  312. populateData: function () {
  313. var data = {};
  314. Object.keys(this.sessions).forEach(function (sid) {
  315. var session = this.sessions[sid];
  316. if (session.peerconnection && session.peerconnection.updateLog) {
  317. // FIXME: should probably be a .dump call
  318. data["jingle_" + session.sid] = {
  319. updateLog: session.peerconnection.updateLog,
  320. stats: session.peerconnection.stats,
  321. url: window.location.href
  322. };
  323. }
  324. });
  325. return data;
  326. }
  327. });