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

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