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.

ChatRoom.js 24KB

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