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.

VideoSIPGW.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { getLogger } from 'jitsi-meet-logger';
  2. const logger = getLogger(__filename);
  3. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  4. import JitsiVideoSIPGWSession from './JitsiVideoSIPGWSession';
  5. import * as Constants from './VideoSIPGWConstants';
  6. /**
  7. * Main video SIP GW handler. Stores references of all created sessions.
  8. */
  9. export default class VideoSIPGW {
  10. /**
  11. * Creates new handler.
  12. *
  13. * @param {ChatRoom} chatRoom - Tha chat room to handle.
  14. */
  15. constructor(chatRoom) {
  16. this.chatRoom = chatRoom;
  17. this.eventEmitter = chatRoom.eventEmitter;
  18. logger.debug('creating VideoSIPGW');
  19. this.sessions = {};
  20. this.sessionStateChangeListener = this.sessionStateChanged.bind(this);
  21. // VideoSIPGW, JitsiConference and ChatRoom are not reusable and no
  22. // more than one VideoSIPGW can be created per JitsiConference,
  23. // so we don't bother to cleanup
  24. chatRoom.addPresenceListener('jibri-sip-call-state',
  25. this.handleJibriSIPState.bind(this));
  26. }
  27. /**
  28. * Handles presence nodes with name: jibri-sip-call-state.
  29. *
  30. * @param {Object} node the presence node Object to handle.
  31. * Object representing part of the presence received over xmpp.
  32. */
  33. handleJibriSIPState(node) {
  34. const attributes = node.attributes;
  35. if (!attributes) {
  36. return;
  37. }
  38. logger.debug('Handle video sip gw state : ', attributes);
  39. const newState = attributes.state;
  40. if (newState === this.state) {
  41. return;
  42. }
  43. switch (newState) {
  44. case Constants.STATE_ON:
  45. case Constants.STATE_OFF:
  46. case Constants.STATE_PENDING:
  47. case Constants.STATE_RETRYING:
  48. case Constants.STATE_FAILED: {
  49. const address = attributes.sipaddress;
  50. if (!address) {
  51. return;
  52. }
  53. // find the corresponding session and set its state
  54. const session = this.sessions[address];
  55. if (session) {
  56. session.setState(newState, attributes.failure_reason);
  57. } else {
  58. logger.warn('Video SIP GW session not found:', address);
  59. }
  60. }
  61. }
  62. }
  63. /**
  64. * Creates new session and stores its reference if it does not exist or
  65. * returns an error otherwise.
  66. *
  67. * @param {string} sipAddress - The sip address to use.
  68. * @param {string} displayName - The display name to use.
  69. * @returns {JitsiVideoSIPGWSession|Error}
  70. */
  71. createVideoSIPGWSession(sipAddress, displayName) {
  72. if (this.sessions[sipAddress]) {
  73. logger.warn('There was already a Video SIP GW session for address',
  74. sipAddress);
  75. return new Error(Constants.ERROR_SESSION_EXISTS);
  76. }
  77. const session = new JitsiVideoSIPGWSession(
  78. sipAddress, displayName, this.chatRoom);
  79. session.addStateListener(this.sessionStateChangeListener);
  80. this.sessions[sipAddress] = session;
  81. return session;
  82. }
  83. /**
  84. * Listener for session state changed. When a session goes to off or failed
  85. * we delete its reference.
  86. *
  87. * @param {options} event - { address, oldState, newState, displayName }
  88. */
  89. sessionStateChanged(event) {
  90. const address = event.address;
  91. if (event.newState === Constants.STATE_OFF
  92. || event.newState === Constants.STATE_FAILED) {
  93. const session = this.sessions[address];
  94. if (!session) {
  95. logger.error('Missing Video SIP GW session with address:',
  96. address);
  97. return;
  98. }
  99. session.removeStateListener(this.sessionStateChangeListener);
  100. delete this.sessions[address];
  101. }
  102. this.eventEmitter.emit(
  103. XMPPEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED,
  104. event);
  105. }
  106. }