Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

strophe.jingle.js 14KB

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