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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* jshint -W117 */
  2. Strophe.addConnectionPlugin('jingle', {
  3. connection: null,
  4. sessions: {},
  5. jid2session: {},
  6. ice_config: {iceServers: [], iceTransports: 'relay'},
  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. // If this is not the focus sending the terminate, we have
  110. // nothing more to do here.
  111. if (Object.keys(this.sessions).length < 1
  112. || !(this.sessions[Object.keys(this.sessions)[0]]
  113. instanceof JingleSession))
  114. {
  115. break;
  116. }
  117. console.log('terminating...', sess.sid);
  118. sess.terminate();
  119. this.terminate(sess.sid);
  120. if ($(iq).find('>jingle>reason').length) {
  121. $(document).trigger('callterminated.jingle', [
  122. sess.sid,
  123. sess.peerjid,
  124. $(iq).find('>jingle>reason>:first')[0].tagName,
  125. $(iq).find('>jingle>reason>text').text()
  126. ]);
  127. } else {
  128. $(document).trigger('callterminated.jingle',
  129. [sess.sid, sess.peerjid]);
  130. }
  131. break;
  132. case 'transport-info':
  133. sess.addIceCandidate($(iq).find('>jingle>content'));
  134. break;
  135. case 'session-info':
  136. var affected;
  137. if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  138. $(document).trigger('ringing.jingle', [sess.sid]);
  139. } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  140. affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  141. $(document).trigger('mute.jingle', [sess.sid, affected]);
  142. } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  143. affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  144. $(document).trigger('unmute.jingle', [sess.sid, affected]);
  145. }
  146. break;
  147. case 'addsource': // FIXME: proprietary, un-jingleish
  148. case 'source-add': // FIXME: proprietary
  149. sess.addSource($(iq).find('>jingle>content'), fromJid);
  150. break;
  151. case 'removesource': // FIXME: proprietary, un-jingleish
  152. case 'source-remove': // FIXME: proprietary
  153. sess.removeSource($(iq).find('>jingle>content'), fromJid);
  154. break;
  155. default:
  156. console.warn('jingle action not implemented', action);
  157. break;
  158. }
  159. return true;
  160. },
  161. initiate: function (peerjid, myjid) { // initiate a new jinglesession to peerjid
  162. var sess = new JingleSession(myjid || this.connection.jid,
  163. Math.random().toString(36).substr(2, 12), // random string
  164. this.connection);
  165. // configure session
  166. if (this.localAudio) {
  167. sess.localStreams.push(this.localAudio);
  168. }
  169. if (this.localVideo) {
  170. sess.localStreams.push(this.localVideo);
  171. }
  172. sess.media_constraints = this.media_constraints;
  173. sess.pc_constraints = this.pc_constraints;
  174. sess.ice_config = this.ice_config;
  175. sess.initiate(peerjid, true);
  176. this.sessions[sess.sid] = sess;
  177. this.jid2session[sess.peerjid] = sess;
  178. sess.sendOffer();
  179. return sess;
  180. },
  181. terminate: function (sid, reason, text) { // terminate by sessionid (or all sessions)
  182. if (sid === null || sid === undefined) {
  183. for (sid in this.sessions) {
  184. if (this.sessions[sid].state != 'ended') {
  185. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  186. this.sessions[sid].terminate();
  187. }
  188. delete this.jid2session[this.sessions[sid].peerjid];
  189. delete this.sessions[sid];
  190. }
  191. } else if (this.sessions.hasOwnProperty(sid)) {
  192. if (this.sessions[sid].state != 'ended') {
  193. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  194. this.sessions[sid].terminate();
  195. }
  196. delete this.jid2session[this.sessions[sid].peerjid];
  197. delete this.sessions[sid];
  198. }
  199. },
  200. // Used to terminate a session when an unavailable presence is received.
  201. terminateByJid: function (jid) {
  202. if (this.jid2session.hasOwnProperty(jid)) {
  203. var sess = this.jid2session[jid];
  204. if (sess) {
  205. sess.terminate();
  206. console.log('peer went away silently', jid);
  207. delete this.sessions[sess.sid];
  208. delete this.jid2session[jid];
  209. $(document).trigger('callterminated.jingle',
  210. [sess.sid, jid], 'gone');
  211. }
  212. }
  213. },
  214. terminateRemoteByJid: function (jid, reason) {
  215. if (this.jid2session.hasOwnProperty(jid)) {
  216. var sess = this.jid2session[jid];
  217. if (sess) {
  218. sess.sendTerminate(reason || (!sess.active()) ? 'kick' : null);
  219. sess.terminate();
  220. console.log('terminate peer with jid', sess.sid, jid);
  221. delete this.sessions[sess.sid];
  222. delete this.jid2session[jid];
  223. $(document).trigger('callterminated.jingle',
  224. [sess.sid, jid, 'kicked']);
  225. }
  226. }
  227. },
  228. getStunAndTurnCredentials: function () {
  229. // get stun and turn configuration from server via xep-0215
  230. // uses time-limited credentials as described in
  231. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  232. //
  233. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  234. // for a prosody module which implements this
  235. //
  236. // currently, this doesn't work with updateIce and therefore credentials with a long
  237. // validity have to be fetched before creating the peerconnection
  238. // TODO: implement refresh via updateIce as described in
  239. // https://code.google.com/p/webrtc/issues/detail?id=1650
  240. var self = this;
  241. this.connection.sendIQ(
  242. $iq({type: 'get', to: this.connection.domain})
  243. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  244. function (res) {
  245. var iceservers = [];
  246. $(res).find('>services>service').each(function (idx, el) {
  247. el = $(el);
  248. var dict = {};
  249. var type = el.attr('type');
  250. switch (type) {
  251. case 'stun':
  252. dict.url = 'stun:' + el.attr('host');
  253. if (el.attr('port')) {
  254. dict.url += ':' + el.attr('port');
  255. }
  256. iceservers.push(dict);
  257. break;
  258. case 'turn':
  259. case 'turns':
  260. dict.url = type + ':';
  261. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  262. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  263. dict.url += el.attr('username') + '@';
  264. } else {
  265. dict.username = el.attr('username'); // only works in M28
  266. }
  267. }
  268. dict.url += el.attr('host');
  269. if (el.attr('port') && el.attr('port') != '3478') {
  270. dict.url += ':' + el.attr('port');
  271. }
  272. if (el.attr('transport') && el.attr('transport') != 'udp') {
  273. dict.url += '?transport=' + el.attr('transport');
  274. }
  275. if (el.attr('password')) {
  276. dict.credential = el.attr('password');
  277. }
  278. iceservers.push(dict);
  279. break;
  280. }
  281. });
  282. self.ice_config.iceServers = iceservers;
  283. },
  284. function (err) {
  285. console.warn('getting turn credentials failed', err);
  286. console.warn('is mod_turncredentials or similar installed?');
  287. }
  288. );
  289. // implement push?
  290. }
  291. });