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 13KB

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