Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ChatRoom.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. for(var i = 0; i < nodes.length; i++)
  189. {
  190. var node = nodes[i];
  191. switch(node.tagName)
  192. {
  193. case "nick":
  194. member.nick = node.value;
  195. if(!member.isFocus) {
  196. var displayName = !this.xmpp.options.displayJids
  197. ? member.nick : Strophe.getResourceFromJid(from);
  198. if (displayName && displayName.length > 0) {
  199. this.eventEmitter.emit(XMPPEvents.DISPLAY_NAME_CHANGED, from, displayName);
  200. }
  201. }
  202. break;
  203. case "userId":
  204. member.id = node.value;
  205. break;
  206. case "bridgeIsDown":
  207. if(!this.bridgeIsDown) {
  208. this.bridgeIsDown = true;
  209. this.eventEmitter.emit(XMPPEvents.BRIDGE_DOWN);
  210. }
  211. break;
  212. case "jibri-recording-status":
  213. var jibri = node;
  214. break;
  215. case "call-control":
  216. var att = node.attributes;
  217. if(!att)
  218. break;
  219. this.phoneNumber = att.phone || null;
  220. this.phonePin = att.pin || null;
  221. this.eventEmitter.emit(XMPPEvents.PHONE_NUMBER_CHANGED);
  222. break;
  223. default :
  224. this.processNode(node, from);
  225. }
  226. }
  227. if (from == this.myroomjid) {
  228. if (member.affiliation == 'owner')
  229. if (this.role !== member.role) {
  230. this.role = member.role;
  231. this.eventEmitter.emit(XMPPEvents.LOCAL_ROLE_CHANGED, this.role);
  232. }
  233. if (!this.joined) {
  234. this.joined = true;
  235. console.log("(TIME) MUC joined:\t", window.performance.now());
  236. this.eventEmitter.emit(XMPPEvents.MUC_JOINED, from, member);
  237. }
  238. } else if (this.members[from] === undefined) {
  239. // new participant
  240. this.members[from] = member;
  241. logger.log('entered', from, member);
  242. if (member.isFocus) {
  243. this.focusMucJid = from;
  244. if(!this.recording) {
  245. this.recording = new Recorder(this.options.recordingType,
  246. this.eventEmitter, this.connection, this.focusMucJid,
  247. this.options.jirecon, this.roomjid);
  248. if(this.lastJibri)
  249. this.recording.handleJibriPresence(this.lastJibri);
  250. }
  251. logger.info("Ignore focus: " + from + ", real JID: " + member.jid);
  252. }
  253. else {
  254. this.eventEmitter.emit(XMPPEvents.MUC_MEMBER_JOINED, from, member.id, member.nick);
  255. }
  256. } else {
  257. // Presence update for existing participant
  258. // Watch role change:
  259. if (this.members[from].role != member.role) {
  260. this.members[from].role = member.role;
  261. this.eventEmitter.emit(XMPPEvents.MUC_ROLE_CHANGED, from, member.role);
  262. }
  263. // store the new display name
  264. if(member.displayName)
  265. this.members[from].displayName = member.displayName;
  266. }
  267. if(!member.isFocus)
  268. this.eventEmitter.emit(XMPPEvents.USER_ID_CHANGED, from, member.id);
  269. // Trigger status message update
  270. if (member.status) {
  271. this.eventEmitter.emit(XMPPEvents.PRESENCE_STATUS, from, member);
  272. }
  273. if(jibri)
  274. {
  275. this.lastJibri = jibri;
  276. if(this.recording)
  277. this.recording.handleJibriPresence(jibri);
  278. }
  279. };
  280. ChatRoom.prototype.processNode = function (node, from) {
  281. if(this.presHandlers[node.tagName])
  282. this.presHandlers[node.tagName](node, Strophe.getResourceFromJid(from));
  283. };
  284. ChatRoom.prototype.sendMessage = function (body, nickname) {
  285. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  286. msg.c('body', body).up();
  287. if (nickname) {
  288. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  289. }
  290. this.connection.send(msg);
  291. this.eventEmitter.emit(XMPPEvents.SENDING_CHAT_MESSAGE, body);
  292. };
  293. ChatRoom.prototype.setSubject = function (subject) {
  294. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  295. msg.c('subject', subject);
  296. this.connection.send(msg);
  297. logger.log("topic changed to " + subject);
  298. };
  299. ChatRoom.prototype.onParticipantLeft = function (jid) {
  300. delete this.lastPresences[jid];
  301. this.eventEmitter.emit(XMPPEvents.MUC_MEMBER_LEFT, jid);
  302. this.moderator.onMucMemberLeft(jid);
  303. };
  304. ChatRoom.prototype.onPresenceUnavailable = function (pres, from) {
  305. // room destroyed ?
  306. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  307. '>destroy').length) {
  308. var reason;
  309. var reasonSelect = $(pres).find(
  310. '>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  311. '>destroy>reason');
  312. if (reasonSelect.length) {
  313. reason = reasonSelect.text();
  314. }
  315. this.xmpp.leaveRoom(this.roomjid);
  316. this.eventEmitter.emit(XMPPEvents.MUC_DESTROYED, reason);
  317. delete this.connection.emuc.rooms[Strophe.getBareJidFromJid(from)];
  318. return true;
  319. }
  320. // Status code 110 indicates that this notification is "self-presence".
  321. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  322. delete this.members[from];
  323. this.onParticipantLeft(from);
  324. }
  325. // If the status code is 110 this means we're leaving and we would like
  326. // to remove everyone else from our view, so we trigger the event.
  327. else if (Object.keys(this.members).length > 1) {
  328. for (var i in this.members) {
  329. var member = this.members[i];
  330. delete this.members[i];
  331. this.onParticipantLeft(member);
  332. }
  333. }
  334. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="307"]').length) {
  335. if (this.myroomjid === from) {
  336. this.xmpp.leaveRoom(this.roomjid);
  337. this.eventEmitter.emit(XMPPEvents.KICKED);
  338. }
  339. }
  340. };
  341. ChatRoom.prototype.onMessage = function (msg, from) {
  342. var nick =
  343. $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]')
  344. .text() ||
  345. Strophe.getResourceFromJid(from);
  346. var txt = $(msg).find('>body').text();
  347. var type = msg.getAttribute("type");
  348. if (type == "error") {
  349. this.eventEmitter.emit(XMPPEvents.CHAT_ERROR_RECEIVED,
  350. $(msg).find('>text').text(), txt);
  351. return true;
  352. }
  353. var subject = $(msg).find('>subject');
  354. if (subject.length) {
  355. var subjectText = subject.text();
  356. if (subjectText || subjectText === "") {
  357. this.eventEmitter.emit(XMPPEvents.SUBJECT_CHANGED, subjectText);
  358. logger.log("Subject is changed to " + subjectText);
  359. }
  360. }
  361. // xep-0203 delay
  362. var stamp = $(msg).find('>delay').attr('stamp');
  363. if (!stamp) {
  364. // or xep-0091 delay, UTC timestamp
  365. stamp = $(msg).find('>[xmlns="jabber:x:delay"]').attr('stamp');
  366. if (stamp) {
  367. // the format is CCYYMMDDThh:mm:ss
  368. var dateParts = stamp.match(/(\d{4})(\d{2})(\d{2}T\d{2}:\d{2}:\d{2})/);
  369. stamp = dateParts[1] + "-" + dateParts[2] + "-" + dateParts[3] + "Z";
  370. }
  371. }
  372. if (txt) {
  373. logger.log('chat', nick, txt);
  374. this.eventEmitter.emit(XMPPEvents.MESSAGE_RECEIVED,
  375. from, nick, txt, this.myroomjid, stamp);
  376. }
  377. };
  378. ChatRoom.prototype.onPresenceError = function (pres, from) {
  379. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  380. logger.log('on password required', from);
  381. this.eventEmitter.emit(XMPPEvents.PASSWORD_REQUIRED);
  382. } else if ($(pres).find(
  383. '>error[type="cancel"]>not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  384. var toDomain = Strophe.getDomainFromJid(pres.getAttribute('to'));
  385. if (toDomain === this.xmpp.options.hosts.anonymousdomain) {
  386. // enter the room by replying with 'not-authorized'. This would
  387. // result in reconnection from authorized domain.
  388. // We're either missing Jicofo/Prosody config for anonymous
  389. // domains or something is wrong.
  390. this.eventEmitter.emit(XMPPEvents.ROOM_JOIN_ERROR, pres);
  391. } else {
  392. logger.warn('onPresError ', pres);
  393. this.eventEmitter.emit(XMPPEvents.ROOM_CONNECT_ERROR, pres);
  394. }
  395. } else {
  396. logger.warn('onPresError ', pres);
  397. this.eventEmitter.emit(XMPPEvents.ROOM_CONNECT_ERROR, pres);
  398. }
  399. };
  400. ChatRoom.prototype.kick = function (jid) {
  401. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  402. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  403. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  404. .c('reason').t('You have been kicked.').up().up().up();
  405. this.connection.sendIQ(
  406. kickIQ,
  407. function (result) {
  408. logger.log('Kick participant with jid: ', jid, result);
  409. },
  410. function (error) {
  411. logger.log('Kick participant error: ', error);
  412. });
  413. };
  414. ChatRoom.prototype.lockRoom = function (key, onSuccess, onError, onNotSupported) {
  415. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  416. var ob = this;
  417. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  418. function (res) {
  419. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  420. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  421. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  422. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  423. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  424. // Fixes a bug in prosody 0.9.+ https://code.google.com/p/lxmppd/issues/detail?id=373
  425. formsubmit.c('field', {'var': 'muc#roomconfig_whois'}).c('value').t('anyone').up().up();
  426. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  427. ob.connection.sendIQ(formsubmit,
  428. onSuccess,
  429. onError);
  430. } else {
  431. onNotSupported();
  432. }
  433. }, onError);
  434. };
  435. ChatRoom.prototype.addToPresence = function (key, values) {
  436. values.tagName = key;
  437. this.presMap["nodes"].push(values);
  438. };
  439. ChatRoom.prototype.removeFromPresence = function (key) {
  440. for(var i = 0; i < this.presMap.nodes.length; i++)
  441. {
  442. if(key === this.presMap.nodes[i].tagName)
  443. this.presMap.nodes.splice(i, 1);
  444. }
  445. };
  446. ChatRoom.prototype.addPresenceListener = function (name, handler) {
  447. this.presHandlers[name] = handler;
  448. };
  449. ChatRoom.prototype.removePresenceListener = function (name) {
  450. delete this.presHandlers[name];
  451. };
  452. ChatRoom.prototype.isModerator = function () {
  453. return this.role === 'moderator';
  454. };
  455. ChatRoom.prototype.getMemberRole = function (peerJid) {
  456. if (this.members[peerJid]) {
  457. return this.members[peerJid].role;
  458. }
  459. return null;
  460. };
  461. ChatRoom.prototype.setJingleSession = function(session){
  462. this.session = session;
  463. this.session.room = this;
  464. };
  465. ChatRoom.prototype.removeStream = function (stream, callback) {
  466. if(!this.session)
  467. return;
  468. this.session.removeStream(stream, callback);
  469. };
  470. ChatRoom.prototype.switchStreams = function (stream, oldStream, callback, isAudio) {
  471. if(this.session) {
  472. // FIXME: will block switchInProgress on true value in case of exception
  473. this.session.switchStreams(stream, oldStream, callback, isAudio);
  474. } else {
  475. // We are done immediately
  476. logger.warn("No conference handler or conference not started yet");
  477. callback();
  478. }
  479. };
  480. ChatRoom.prototype.addStream = function (stream, callback) {
  481. if(this.session) {
  482. // FIXME: will block switchInProgress on true value in case of exception
  483. this.session.addStream(stream, callback);
  484. } else {
  485. // We are done immediately
  486. logger.warn("No conference handler or conference not started yet");
  487. callback();
  488. }
  489. };
  490. ChatRoom.prototype.setVideoMute = function (mute, callback, options) {
  491. var self = this;
  492. var localCallback = function (mute) {
  493. self.sendVideoInfoPresence(mute);
  494. if(callback)
  495. callback(mute);
  496. };
  497. if(this.session)
  498. {
  499. this.session.setVideoMute(
  500. mute, localCallback, options);
  501. }
  502. else {
  503. localCallback(mute);
  504. }
  505. };
  506. ChatRoom.prototype.setAudioMute = function (mute, callback) {
  507. //This will be for remote streams only
  508. // if (this.forceMuted && !mute) {
  509. // logger.info("Asking focus for unmute");
  510. // this.connection.moderate.setMute(this.connection.emuc.myroomjid, mute);
  511. // // FIXME: wait for result before resetting muted status
  512. // this.forceMuted = false;
  513. // }
  514. return this.sendAudioInfoPresence(mute, callback);
  515. };
  516. ChatRoom.prototype.addAudioInfoToPresence = function (mute) {
  517. this.removeFromPresence("audiomuted");
  518. this.addToPresence("audiomuted",
  519. {attributes:
  520. {"audions": "http://jitsi.org/jitmeet/audio"},
  521. value: mute.toString()});
  522. };
  523. ChatRoom.prototype.sendAudioInfoPresence = function(mute, callback) {
  524. this.addAudioInfoToPresence(mute);
  525. if(this.connection) {
  526. this.sendPresence();
  527. }
  528. if(callback)
  529. callback();
  530. };
  531. ChatRoom.prototype.addVideoInfoToPresence = function (mute) {
  532. this.removeFromPresence("videomuted");
  533. this.addToPresence("videomuted",
  534. {attributes:
  535. {"videons": "http://jitsi.org/jitmeet/video"},
  536. value: mute.toString()});
  537. };
  538. ChatRoom.prototype.sendVideoInfoPresence = function (mute) {
  539. this.addVideoInfoToPresence(mute);
  540. if(!this.connection)
  541. return;
  542. this.sendPresence();
  543. };
  544. ChatRoom.prototype.addListener = function(type, listener) {
  545. this.eventEmitter.on(type, listener);
  546. };
  547. ChatRoom.prototype.removeListener = function (type, listener) {
  548. this.eventEmitter.removeListener(type, listener);
  549. };
  550. ChatRoom.prototype.remoteStreamAdded = function(data, sid, thessrc) {
  551. if(this.lastPresences[data.peerjid])
  552. {
  553. var pres = this.lastPresences[data.peerjid];
  554. var audiomuted = filterNodeFromPresenceJSON(pres, "audiomuted");
  555. var videomuted = filterNodeFromPresenceJSON(pres, "videomuted");
  556. data.videomuted = ((videomuted.length > 0
  557. && videomuted[0]
  558. && videomuted[0]["value"] === "true")? true : false);
  559. data.audiomuted = ((audiomuted.length > 0
  560. && audiomuted[0]
  561. && audiomuted[0]["value"] === "true")? true : false);
  562. }
  563. this.eventEmitter.emit(XMPPEvents.REMOTE_STREAM_RECEIVED, data, sid, thessrc);
  564. };
  565. ChatRoom.prototype.getJidBySSRC = function (ssrc) {
  566. if (!this.session)
  567. return null;
  568. return this.session.getSsrcOwner(ssrc);
  569. };
  570. /**
  571. * Returns true if the recording is supproted and false if not.
  572. */
  573. ChatRoom.prototype.isRecordingSupported = function () {
  574. if(this.recording)
  575. return this.recording.isSupported();
  576. return false;
  577. };
  578. /**
  579. * Returns null if the recording is not supported, "on" if the recording started
  580. * and "off" if the recording is not started.
  581. */
  582. ChatRoom.prototype.getRecordingState = function () {
  583. if(this.recording)
  584. return this.recording.getState();
  585. return "off";
  586. }
  587. /**
  588. * Returns the url of the recorded video.
  589. */
  590. ChatRoom.prototype.getRecordingURL = function () {
  591. if(this.recording)
  592. return this.recording.getURL();
  593. return null;
  594. }
  595. /**
  596. * Starts/stops the recording
  597. * @param token token for authentication
  598. * @param statusChangeHandler {function} receives the new status as argument.
  599. */
  600. ChatRoom.prototype.toggleRecording = function (options, statusChangeHandler) {
  601. if(this.recording)
  602. return this.recording.toggleRecording(options, statusChangeHandler);
  603. return statusChangeHandler("error",
  604. new Error("The conference is not created yet!"));
  605. }
  606. /**
  607. * Returns true if the SIP calls are supported and false otherwise
  608. */
  609. ChatRoom.prototype.isSIPCallingSupported = function () {
  610. if(this.moderator)
  611. return this.moderator.isSipGatewayEnabled();
  612. return false;
  613. }
  614. /**
  615. * Dials a number.
  616. * @param number the number
  617. */
  618. ChatRoom.prototype.dial = function (number) {
  619. return this.connection.rayo.dial(number, "fromnumber",
  620. Strophe.getNodeFromJid(this.myroomjid), this.password,
  621. this.focusMucJid);
  622. }
  623. /**
  624. * Hangup an existing call
  625. */
  626. ChatRoom.prototype.hangup = function () {
  627. return this.connection.rayo.hangup();
  628. }
  629. /**
  630. * Returns the phone number for joining the conference.
  631. */
  632. ChatRoom.prototype.getPhoneNumber = function () {
  633. return this.phoneNumber;
  634. }
  635. /**
  636. * Returns the pin for joining the conference with phone.
  637. */
  638. ChatRoom.prototype.getPhonePin = function () {
  639. return this.phonePin;
  640. }
  641. /**
  642. * Returns the connection state for the current session.
  643. */
  644. ChatRoom.prototype.getConnectionState = function () {
  645. if(!this.session)
  646. return null;
  647. return this.session.getIceConnectionState();
  648. }
  649. /**
  650. * Mutes remote participant.
  651. * @param jid of the participant
  652. * @param mute
  653. */
  654. ChatRoom.prototype.muteParticipant = function (jid, mute) {
  655. logger.info("set mute", mute);
  656. var iqToFocus = $iq(
  657. {to: this.focusMucJid, type: 'set'})
  658. .c('mute', {
  659. xmlns: 'http://jitsi.org/jitmeet/audio',
  660. jid: jid
  661. })
  662. .t(mute.toString())
  663. .up();
  664. this.connection.sendIQ(
  665. iqToFocus,
  666. function (result) {
  667. logger.log('set mute', result);
  668. },
  669. function (error) {
  670. logger.log('set mute error', error);
  671. });
  672. }
  673. ChatRoom.prototype.onMute = function (iq) {
  674. var from = iq.getAttribute('from');
  675. if (from !== this.focusMucJid) {
  676. logger.warn("Ignored mute from non focus peer");
  677. return false;
  678. }
  679. var mute = $(iq).find('mute');
  680. if (mute.length) {
  681. var doMuteAudio = mute.text() === "true";
  682. this.eventEmitter.emit(XMPPEvents.AUDIO_MUTED_BY_FOCUS, doMuteAudio);
  683. }
  684. return true;
  685. }
  686. module.exports = ChatRoom;