您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChatRoom.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* global Strophe, $, $pres, $iq, $msg */
  2. /* jshint -W101,-W069 */
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  5. var Moderator = require("./moderator");
  6. var EventEmitter = require("events");
  7. var Recorder = require("./recording");
  8. var JIBRI_XMLNS = 'http://jitsi.org/protocol/jibri';
  9. var parser = {
  10. packet2JSON: function (packet, nodes) {
  11. var self = this;
  12. $(packet).children().each(function (index) {
  13. var tagName = $(this).prop("tagName");
  14. var node = {
  15. tagName: tagName
  16. };
  17. node.attributes = {};
  18. $($(this)[0].attributes).each(function( index, attr ) {
  19. node.attributes[ attr.name ] = attr.value;
  20. });
  21. var text = Strophe.getText($(this)[0]);
  22. if (text) {
  23. node.value = text;
  24. }
  25. node.children = [];
  26. nodes.push(node);
  27. self.packet2JSON($(this), node.children);
  28. });
  29. },
  30. JSON2packet: function (nodes, packet) {
  31. for(var i = 0; i < nodes.length; i++) {
  32. var node = nodes[i];
  33. if(!node || node === null){
  34. continue;
  35. }
  36. packet.c(node.tagName, node.attributes);
  37. if(node.value)
  38. packet.t(node.value);
  39. if(node.children)
  40. this.JSON2packet(node.children, packet);
  41. packet.up();
  42. }
  43. // packet.up();
  44. }
  45. };
  46. /**
  47. * Returns array of JS objects from the presence JSON associated with the passed nodeName
  48. * @param pres the presence JSON
  49. * @param nodeName the name of the node (videomuted, audiomuted, etc)
  50. */
  51. function filterNodeFromPresenceJSON(pres, nodeName){
  52. var res = [];
  53. for(var i = 0; i < pres.length; i++)
  54. if(pres[i].tagName === nodeName)
  55. res.push(pres[i]);
  56. return res;
  57. }
  58. function ChatRoom(connection, jid, password, XMPP, options) {
  59. this.eventEmitter = new EventEmitter();
  60. this.xmpp = XMPP;
  61. this.connection = connection;
  62. this.roomjid = Strophe.getBareJidFromJid(jid);
  63. this.myroomjid = jid;
  64. this.password = password;
  65. logger.info("Joined MUC as " + this.myroomjid);
  66. this.members = {};
  67. this.presMap = {};
  68. this.presHandlers = {};
  69. this.joined = false;
  70. this.role = 'none';
  71. this.focusMucJid = null;
  72. this.bridgeIsDown = false;
  73. this.options = options || {};
  74. this.moderator = new Moderator(this.roomjid, this.xmpp, this.eventEmitter);
  75. this.initPresenceMap();
  76. this.session = null;
  77. var self = this;
  78. this.lastPresences = {};
  79. this.phoneNumber = null;
  80. this.phonePin = null;
  81. }
  82. ChatRoom.prototype.initPresenceMap = function () {
  83. this.presMap['to'] = this.myroomjid;
  84. this.presMap['xns'] = 'http://jabber.org/protocol/muc';
  85. this.presMap["nodes"] = [];
  86. this.presMap["nodes"].push( {
  87. "tagName": "user-agent",
  88. "value": navigator.userAgent,
  89. "attributes": {xmlns: 'http://jitsi.org/jitmeet/user-agent'}
  90. });
  91. };
  92. ChatRoom.prototype.updateDeviceAvailability = function (devices) {
  93. this.presMap["nodes"].push( {
  94. "tagName": "devices",
  95. "children": [
  96. {
  97. "tagName": "audio",
  98. "value": devices.audio,
  99. },
  100. {
  101. "tagName": "video",
  102. "value": devices.video,
  103. }
  104. ]
  105. });
  106. };
  107. ChatRoom.prototype.join = function (password) {
  108. if(password)
  109. this.password = password;
  110. var self = this;
  111. this.moderator.allocateConferenceFocus(function()
  112. {
  113. self.sendPresence(true);
  114. }.bind(this));
  115. };
  116. ChatRoom.prototype.sendPresence = function (fromJoin) {
  117. if (!this.presMap['to'] || (!this.joined && !fromJoin)) {
  118. // Too early to send presence - not initialized
  119. return;
  120. }
  121. var pres = $pres({to: this.presMap['to'] });
  122. pres.c('x', {xmlns: this.presMap['xns']});
  123. if (this.password) {
  124. pres.c('password').t(this.password).up();
  125. }
  126. pres.up();
  127. // Send XEP-0115 'c' stanza that contains our capabilities info
  128. if (this.connection.caps) {
  129. this.connection.caps.node = this.xmpp.options.clientNode;
  130. pres.c('c', this.connection.caps.generateCapsAttrs()).up();
  131. }
  132. parser.JSON2packet(this.presMap.nodes, pres);
  133. this.connection.send(pres);
  134. };
  135. ChatRoom.prototype.doLeave = function () {
  136. logger.log("do leave", this.myroomjid);
  137. var pres = $pres({to: this.myroomjid, type: 'unavailable' });
  138. this.presMap.length = 0;
  139. this.connection.send(pres);
  140. };
  141. ChatRoom.prototype.createNonAnonymousRoom = function () {
  142. // http://xmpp.org/extensions/xep-0045.html#createroom-reserved
  143. var getForm = $iq({type: 'get', to: this.roomjid})
  144. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  145. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  146. var self = this;
  147. this.connection.sendIQ(getForm, function (form) {
  148. if (!$(form).find(
  149. '>query>x[xmlns="jabber:x:data"]' +
  150. '>field[var="muc#roomconfig_whois"]').length) {
  151. logger.error('non-anonymous rooms not supported');
  152. return;
  153. }
  154. var formSubmit = $iq({to: this.roomjid, type: 'set'})
  155. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  156. formSubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  157. formSubmit.c('field', {'var': 'FORM_TYPE'})
  158. .c('value')
  159. .t('http://jabber.org/protocol/muc#roomconfig').up().up();
  160. formSubmit.c('field', {'var': 'muc#roomconfig_whois'})
  161. .c('value').t('anyone').up().up();
  162. self.connection.sendIQ(formSubmit);
  163. }, function (error) {
  164. logger.error("Error getting room configuration form");
  165. });
  166. };
  167. ChatRoom.prototype.onPresence = function (pres) {
  168. var from = pres.getAttribute('from');
  169. // Parse roles.
  170. var member = {};
  171. member.show = $(pres).find('>show').text();
  172. member.status = $(pres).find('>status').text();
  173. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  174. member.affiliation = tmp.attr('affiliation');
  175. member.role = tmp.attr('role');
  176. // Focus recognition
  177. member.jid = tmp.attr('jid');
  178. member.isFocus = false;
  179. if (member.jid
  180. && member.jid.indexOf(this.moderator.getFocusUserJid() + "/") === 0) {
  181. member.isFocus = true;
  182. }
  183. $(pres).find(">x").remove();
  184. var nodes = [];
  185. parser.packet2JSON(pres, nodes);
  186. this.lastPresences[from] = nodes;
  187. var jibri = null;
  188. // process nodes to extract data needed for MUC_JOINED and MUC_MEMBER_JOINED
  189. // events
  190. for(var i = 0; i < nodes.length; i++)
  191. {
  192. var node = nodes[i];
  193. switch(node.tagName)
  194. {
  195. case "nick":
  196. member.nick = node.value;
  197. break;
  198. case "userId":
  199. member.id = node.value;
  200. break;
  201. }
  202. }
  203. if (from == this.myroomjid) {
  204. if (member.affiliation == 'owner'
  205. && this.role !== member.role) {
  206. this.role = member.role;
  207. this.eventEmitter.emit(
  208. XMPPEvents.LOCAL_ROLE_CHANGED, this.role);
  209. }
  210. if (!this.joined) {
  211. this.joined = true;
  212. console.log("(TIME) MUC joined:\t", window.performance.now());
  213. this.eventEmitter.emit(XMPPEvents.MUC_JOINED);
  214. }
  215. } else if (this.members[from] === undefined) {
  216. // new participant
  217. this.members[from] = member;
  218. logger.log('entered', from, member);
  219. if (member.isFocus) {
  220. this.focusMucJid = from;
  221. if(!this.recording) {
  222. this.recording = new Recorder(this.options.recordingType,
  223. this.eventEmitter, this.connection, this.focusMucJid,
  224. this.options.jirecon, this.roomjid);
  225. if(this.lastJibri)
  226. this.recording.handleJibriPresence(this.lastJibri);
  227. }
  228. logger.info("Ignore focus: " + from + ", real JID: " + member.jid);
  229. }
  230. else {
  231. this.eventEmitter.emit(
  232. XMPPEvents.MUC_MEMBER_JOINED, from, member.nick, member.role);
  233. }
  234. } else {
  235. // Presence update for existing participant
  236. // Watch role change:
  237. if (this.members[from].role != member.role) {
  238. this.members[from].role = member.role;
  239. this.eventEmitter.emit(
  240. XMPPEvents.MUC_ROLE_CHANGED, from, member.role);
  241. }
  242. // store the new display name
  243. if(member.displayName)
  244. this.members[from].displayName = member.displayName;
  245. }
  246. // after we had fired member or room joined events, lets fire events
  247. // for the rest info we got in presence
  248. for(var i = 0; i < nodes.length; i++)
  249. {
  250. var node = nodes[i];
  251. switch(node.tagName)
  252. {
  253. case "nick":
  254. if(!member.isFocus) {
  255. var displayName = !this.xmpp.options.displayJids
  256. ? member.nick : Strophe.getResourceFromJid(from);
  257. if (displayName && displayName.length > 0) {
  258. this.eventEmitter.emit(
  259. XMPPEvents.DISPLAY_NAME_CHANGED, from, displayName);
  260. }
  261. }
  262. break;
  263. case "bridgeIsDown":
  264. if(!this.bridgeIsDown) {
  265. this.bridgeIsDown = true;
  266. this.eventEmitter.emit(XMPPEvents.BRIDGE_DOWN);
  267. }
  268. break;
  269. case "jibri-recording-status":
  270. var jibri = node;
  271. break;
  272. case "call-control":
  273. var att = node.attributes;
  274. if(!att)
  275. break;
  276. this.phoneNumber = att.phone || null;
  277. this.phonePin = att.pin || null;
  278. this.eventEmitter.emit(XMPPEvents.PHONE_NUMBER_CHANGED);
  279. break;
  280. default :
  281. this.processNode(node, from);
  282. }
  283. }
  284. if(!member.isFocus)
  285. this.eventEmitter.emit(XMPPEvents.USER_ID_CHANGED, from, member.id);
  286. // Trigger status message update
  287. if (member.status) {
  288. this.eventEmitter.emit(XMPPEvents.PRESENCE_STATUS, from, member);
  289. }
  290. if(jibri)
  291. {
  292. this.lastJibri = jibri;
  293. if(this.recording)
  294. this.recording.handleJibriPresence(jibri);
  295. }
  296. };
  297. ChatRoom.prototype.processNode = function (node, from) {
  298. if(this.presHandlers[node.tagName])
  299. this.presHandlers[node.tagName](node, Strophe.getResourceFromJid(from));
  300. };
  301. ChatRoom.prototype.sendMessage = function (body, nickname) {
  302. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  303. msg.c('body', body).up();
  304. if (nickname) {
  305. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  306. }
  307. this.connection.send(msg);
  308. this.eventEmitter.emit(XMPPEvents.SENDING_CHAT_MESSAGE, body);
  309. };
  310. ChatRoom.prototype.setSubject = function (subject) {
  311. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  312. msg.c('subject', subject);
  313. this.connection.send(msg);
  314. logger.log("topic changed to " + subject);
  315. };
  316. ChatRoom.prototype.onParticipantLeft = function (jid) {
  317. delete this.lastPresences[jid];
  318. this.eventEmitter.emit(XMPPEvents.MUC_MEMBER_LEFT, jid);
  319. this.moderator.onMucMemberLeft(jid);
  320. };
  321. ChatRoom.prototype.onPresenceUnavailable = function (pres, from) {
  322. // room destroyed ?
  323. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  324. '>destroy').length) {
  325. var reason;
  326. var reasonSelect = $(pres).find(
  327. '>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  328. '>destroy>reason');
  329. if (reasonSelect.length) {
  330. reason = reasonSelect.text();
  331. }
  332. this.xmpp.leaveRoom(this.roomjid);
  333. this.eventEmitter.emit(XMPPEvents.MUC_DESTROYED, reason);
  334. delete this.connection.emuc.rooms[Strophe.getBareJidFromJid(from)];
  335. return true;
  336. }
  337. // Status code 110 indicates that this notification is "self-presence".
  338. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  339. delete this.members[from];
  340. this.onParticipantLeft(from);
  341. }
  342. // If the status code is 110 this means we're leaving and we would like
  343. // to remove everyone else from our view, so we trigger the event.
  344. else if (Object.keys(this.members).length > 1) {
  345. for (var i in this.members) {
  346. var member = this.members[i];
  347. delete this.members[i];
  348. this.onParticipantLeft(member);
  349. }
  350. }
  351. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="307"]').length) {
  352. if (this.myroomjid === from) {
  353. this.xmpp.leaveRoom(this.roomjid);
  354. this.eventEmitter.emit(XMPPEvents.KICKED);
  355. }
  356. }
  357. };
  358. ChatRoom.prototype.onMessage = function (msg, from) {
  359. var nick =
  360. $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]')
  361. .text() ||
  362. Strophe.getResourceFromJid(from);
  363. var txt = $(msg).find('>body').text();
  364. var type = msg.getAttribute("type");
  365. if (type == "error") {
  366. this.eventEmitter.emit(XMPPEvents.CHAT_ERROR_RECEIVED,
  367. $(msg).find('>text').text(), txt);
  368. return true;
  369. }
  370. var subject = $(msg).find('>subject');
  371. if (subject.length) {
  372. var subjectText = subject.text();
  373. if (subjectText || subjectText === "") {
  374. this.eventEmitter.emit(XMPPEvents.SUBJECT_CHANGED, subjectText);
  375. logger.log("Subject is changed to " + subjectText);
  376. }
  377. }
  378. // xep-0203 delay
  379. var stamp = $(msg).find('>delay').attr('stamp');
  380. if (!stamp) {
  381. // or xep-0091 delay, UTC timestamp
  382. stamp = $(msg).find('>[xmlns="jabber:x:delay"]').attr('stamp');
  383. if (stamp) {
  384. // the format is CCYYMMDDThh:mm:ss
  385. var dateParts = stamp.match(/(\d{4})(\d{2})(\d{2}T\d{2}:\d{2}:\d{2})/);
  386. stamp = dateParts[1] + "-" + dateParts[2] + "-" + dateParts[3] + "Z";
  387. }
  388. }
  389. if (txt) {
  390. logger.log('chat', nick, txt);
  391. this.eventEmitter.emit(XMPPEvents.MESSAGE_RECEIVED,
  392. from, nick, txt, this.myroomjid, stamp);
  393. }
  394. };
  395. ChatRoom.prototype.onPresenceError = function (pres, from) {
  396. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  397. logger.log('on password required', from);
  398. this.eventEmitter.emit(XMPPEvents.PASSWORD_REQUIRED);
  399. } else if ($(pres).find(
  400. '>error[type="cancel"]>not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  401. var toDomain = Strophe.getDomainFromJid(pres.getAttribute('to'));
  402. if (toDomain === this.xmpp.options.hosts.anonymousdomain) {
  403. // enter the room by replying with 'not-authorized'. This would
  404. // result in reconnection from authorized domain.
  405. // We're either missing Jicofo/Prosody config for anonymous
  406. // domains or something is wrong.
  407. this.eventEmitter.emit(XMPPEvents.ROOM_JOIN_ERROR, pres);
  408. } else {
  409. logger.warn('onPresError ', pres);
  410. this.eventEmitter.emit(XMPPEvents.ROOM_CONNECT_ERROR, pres);
  411. }
  412. } else {
  413. logger.warn('onPresError ', pres);
  414. this.eventEmitter.emit(XMPPEvents.ROOM_CONNECT_ERROR, pres);
  415. }
  416. };
  417. ChatRoom.prototype.kick = function (jid) {
  418. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  419. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  420. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  421. .c('reason').t('You have been kicked.').up().up().up();
  422. this.connection.sendIQ(
  423. kickIQ,
  424. function (result) {
  425. logger.log('Kick participant with jid: ', jid, result);
  426. },
  427. function (error) {
  428. logger.log('Kick participant error: ', error);
  429. });
  430. };
  431. ChatRoom.prototype.lockRoom = function (key, onSuccess, onError, onNotSupported) {
  432. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  433. var ob = this;
  434. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  435. function (res) {
  436. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  437. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  438. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  439. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  440. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  441. // Fixes a bug in prosody 0.9.+ https://code.google.com/p/lxmppd/issues/detail?id=373
  442. formsubmit.c('field', {'var': 'muc#roomconfig_whois'}).c('value').t('anyone').up().up();
  443. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  444. ob.connection.sendIQ(formsubmit,
  445. onSuccess,
  446. onError);
  447. } else {
  448. onNotSupported();
  449. }
  450. }, onError);
  451. };
  452. ChatRoom.prototype.addToPresence = function (key, values) {
  453. values.tagName = key;
  454. this.presMap["nodes"].push(values);
  455. };
  456. ChatRoom.prototype.removeFromPresence = function (key) {
  457. for(var i = 0; i < this.presMap.nodes.length; i++)
  458. {
  459. if(key === this.presMap.nodes[i].tagName)
  460. this.presMap.nodes.splice(i, 1);
  461. }
  462. };
  463. ChatRoom.prototype.addPresenceListener = function (name, handler) {
  464. this.presHandlers[name] = handler;
  465. };
  466. ChatRoom.prototype.removePresenceListener = function (name) {
  467. delete this.presHandlers[name];
  468. };
  469. ChatRoom.prototype.isModerator = function () {
  470. return this.role === 'moderator';
  471. };
  472. ChatRoom.prototype.getMemberRole = function (peerJid) {
  473. if (this.members[peerJid]) {
  474. return this.members[peerJid].role;
  475. }
  476. return null;
  477. };
  478. ChatRoom.prototype.setJingleSession = function(session){
  479. this.session = session;
  480. this.session.room = this;
  481. };
  482. ChatRoom.prototype.removeStream = function (stream, callback) {
  483. if(!this.session)
  484. return;
  485. this.session.removeStream(stream, callback);
  486. };
  487. ChatRoom.prototype.switchStreams = function (stream, oldStream, callback, isAudio) {
  488. if(this.session) {
  489. // FIXME: will block switchInProgress on true value in case of exception
  490. this.session.switchStreams(stream, oldStream, callback, isAudio);
  491. } else {
  492. // We are done immediately
  493. logger.warn("No conference handler or conference not started yet");
  494. callback();
  495. }
  496. };
  497. ChatRoom.prototype.addStream = function (stream, callback) {
  498. if(this.session) {
  499. // FIXME: will block switchInProgress on true value in case of exception
  500. this.session.addStream(stream, callback);
  501. } else {
  502. // We are done immediately
  503. logger.warn("No conference handler or conference not started yet");
  504. callback();
  505. }
  506. };
  507. ChatRoom.prototype.setVideoMute = function (mute, callback, options) {
  508. var self = this;
  509. var localCallback = function (mute) {
  510. self.sendVideoInfoPresence(mute);
  511. if(callback)
  512. callback(mute);
  513. };
  514. if(this.session)
  515. {
  516. this.session.setVideoMute(
  517. mute, localCallback, options);
  518. }
  519. else {
  520. localCallback(mute);
  521. }
  522. };
  523. ChatRoom.prototype.setAudioMute = function (mute, callback) {
  524. //This will be for remote streams only
  525. // if (this.forceMuted && !mute) {
  526. // logger.info("Asking focus for unmute");
  527. // this.connection.moderate.setMute(this.connection.emuc.myroomjid, mute);
  528. // // FIXME: wait for result before resetting muted status
  529. // this.forceMuted = false;
  530. // }
  531. return this.sendAudioInfoPresence(mute, callback);
  532. };
  533. ChatRoom.prototype.addAudioInfoToPresence = function (mute) {
  534. this.removeFromPresence("audiomuted");
  535. this.addToPresence("audiomuted",
  536. {attributes:
  537. {"audions": "http://jitsi.org/jitmeet/audio"},
  538. value: mute.toString()});
  539. };
  540. ChatRoom.prototype.sendAudioInfoPresence = function(mute, callback) {
  541. this.addAudioInfoToPresence(mute);
  542. if(this.connection) {
  543. this.sendPresence();
  544. }
  545. if(callback)
  546. callback();
  547. this.removeFromPresence("audiomuted");
  548. };
  549. ChatRoom.prototype.addVideoInfoToPresence = function (mute) {
  550. this.removeFromPresence("videomuted");
  551. this.addToPresence("videomuted",
  552. {attributes:
  553. {"videons": "http://jitsi.org/jitmeet/video"},
  554. value: mute.toString()});
  555. };
  556. ChatRoom.prototype.sendVideoInfoPresence = function (mute) {
  557. this.addVideoInfoToPresence(mute);
  558. if(!this.connection)
  559. return;
  560. this.sendPresence();
  561. this.removeFromPresence("videomuted");
  562. };
  563. ChatRoom.prototype.addListener = function(type, listener) {
  564. this.eventEmitter.on(type, listener);
  565. };
  566. ChatRoom.prototype.removeListener = function (type, listener) {
  567. this.eventEmitter.removeListener(type, listener);
  568. };
  569. ChatRoom.prototype.remoteStreamAdded = function(data, sid, thessrc) {
  570. if(this.lastPresences[data.peerjid])
  571. {
  572. var pres = this.lastPresences[data.peerjid];
  573. var audiomuted = filterNodeFromPresenceJSON(pres, "audiomuted");
  574. var videomuted = filterNodeFromPresenceJSON(pres, "videomuted");
  575. data.videomuted = ((videomuted.length > 0
  576. && videomuted[0]
  577. && videomuted[0]["value"] === "true")? true : false);
  578. data.audiomuted = ((audiomuted.length > 0
  579. && audiomuted[0]
  580. && audiomuted[0]["value"] === "true")? true : false);
  581. }
  582. this.eventEmitter.emit(XMPPEvents.REMOTE_STREAM_RECEIVED, data, sid, thessrc);
  583. };
  584. ChatRoom.prototype.getJidBySSRC = function (ssrc) {
  585. if (!this.session)
  586. return null;
  587. return this.session.getSsrcOwner(ssrc);
  588. };
  589. /**
  590. * Returns true if the recording is supproted and false if not.
  591. */
  592. ChatRoom.prototype.isRecordingSupported = function () {
  593. if(this.recording)
  594. return this.recording.isSupported();
  595. return false;
  596. };
  597. /**
  598. * Returns null if the recording is not supported, "on" if the recording started
  599. * and "off" if the recording is not started.
  600. */
  601. ChatRoom.prototype.getRecordingState = function () {
  602. if(this.recording)
  603. return this.recording.getState();
  604. return "off";
  605. }
  606. /**
  607. * Returns the url of the recorded video.
  608. */
  609. ChatRoom.prototype.getRecordingURL = function () {
  610. if(this.recording)
  611. return this.recording.getURL();
  612. return null;
  613. }
  614. /**
  615. * Starts/stops the recording
  616. * @param token token for authentication
  617. * @param statusChangeHandler {function} receives the new status as argument.
  618. */
  619. ChatRoom.prototype.toggleRecording = function (options, statusChangeHandler) {
  620. if(this.recording)
  621. return this.recording.toggleRecording(options, statusChangeHandler);
  622. return statusChangeHandler("error",
  623. new Error("The conference is not created yet!"));
  624. }
  625. /**
  626. * Returns true if the SIP calls are supported and false otherwise
  627. */
  628. ChatRoom.prototype.isSIPCallingSupported = function () {
  629. if(this.moderator)
  630. return this.moderator.isSipGatewayEnabled();
  631. return false;
  632. }
  633. /**
  634. * Dials a number.
  635. * @param number the number
  636. */
  637. ChatRoom.prototype.dial = function (number) {
  638. return this.connection.rayo.dial(number, "fromnumber",
  639. Strophe.getNodeFromJid(this.myroomjid), this.password,
  640. this.focusMucJid);
  641. }
  642. /**
  643. * Hangup an existing call
  644. */
  645. ChatRoom.prototype.hangup = function () {
  646. return this.connection.rayo.hangup();
  647. }
  648. /**
  649. * Returns the phone number for joining the conference.
  650. */
  651. ChatRoom.prototype.getPhoneNumber = function () {
  652. return this.phoneNumber;
  653. }
  654. /**
  655. * Returns the pin for joining the conference with phone.
  656. */
  657. ChatRoom.prototype.getPhonePin = function () {
  658. return this.phonePin;
  659. }
  660. /**
  661. * Returns the connection state for the current session.
  662. */
  663. ChatRoom.prototype.getConnectionState = function () {
  664. if(!this.session)
  665. return null;
  666. return this.session.getIceConnectionState();
  667. }
  668. /**
  669. * Mutes remote participant.
  670. * @param jid of the participant
  671. * @param mute
  672. */
  673. ChatRoom.prototype.muteParticipant = function (jid, mute) {
  674. logger.info("set mute", mute);
  675. var iqToFocus = $iq(
  676. {to: this.focusMucJid, type: 'set'})
  677. .c('mute', {
  678. xmlns: 'http://jitsi.org/jitmeet/audio',
  679. jid: jid
  680. })
  681. .t(mute.toString())
  682. .up();
  683. this.connection.sendIQ(
  684. iqToFocus,
  685. function (result) {
  686. logger.log('set mute', result);
  687. },
  688. function (error) {
  689. logger.log('set mute error', error);
  690. });
  691. }
  692. ChatRoom.prototype.onMute = function (iq) {
  693. var from = iq.getAttribute('from');
  694. if (from !== this.focusMucJid) {
  695. logger.warn("Ignored mute from non focus peer");
  696. return false;
  697. }
  698. var mute = $(iq).find('mute');
  699. if (mute.length) {
  700. var doMuteAudio = mute.text() === "true";
  701. this.eventEmitter.emit(XMPPEvents.AUDIO_MUTED_BY_FOCUS, doMuteAudio);
  702. }
  703. return true;
  704. }
  705. module.exports = ChatRoom;