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

ChatRoom.js 24KB

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