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

strophe.jingle.js 14KB

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