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

ChatRoom.js 25KB

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