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.

JitsiVideoSIPGWSession.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import { $iq } from 'strophe.js';
  3. import Listenable from '../util/Listenable';
  4. import * as VideoSIPGWConstants from './VideoSIPGWConstants';
  5. const logger = getLogger(__filename);
  6. /**
  7. * The event name for current sip video session state changed.
  8. * @type {string} event name for sip video session state changed.
  9. */
  10. const STATE_CHANGED = 'STATE_CHANGED';
  11. /**
  12. * Jitsi video SIP GW session. Holding its state and able to start/stop it.
  13. * When session is in OFF or FAILED stated it cannot be used anymore.
  14. */
  15. export default class JitsiVideoSIPGWSession extends Listenable {
  16. /**
  17. * Creates new session with the desired sip address and display name.
  18. *
  19. * @param {string} sipAddress - The sip address to use when
  20. * starting the session.
  21. * @param {string} displayName - The display name to use for
  22. * that participant.
  23. * @param {ChatRoom} chatRoom - The chat room this session is bound to.
  24. */
  25. constructor(sipAddress, displayName, chatRoom) {
  26. super();
  27. this.sipAddress = sipAddress;
  28. this.displayName = displayName;
  29. this.chatRoom = chatRoom;
  30. /*
  31. * The initial state is undefined. Initial state cannot be STATE_OFF,
  32. * the session enters this state when it was in STATE_ON and was stopped
  33. * and such session cannot be used anymore.
  34. *
  35. * @type {VideoSIPGWConstants|undefined}
  36. */
  37. this.state = undefined;
  38. }
  39. /**
  40. * Stops the current session.
  41. */
  42. stop() {
  43. if (this.state === VideoSIPGWConstants.STATE_OFF
  44. || this.state === VideoSIPGWConstants.STATE_FAILED) {
  45. logger.warn('Video SIP GW session already stopped or failed!');
  46. return;
  47. }
  48. this._sendJibriIQ('stop');
  49. }
  50. /**
  51. * Starts a new session. Sends an iq to the focus.
  52. */
  53. start() {
  54. // if state is off, this session was active for some reason
  55. // and we should create new one, rather than reusing it
  56. if (this.state === VideoSIPGWConstants.STATE_ON
  57. || this.state === VideoSIPGWConstants.STATE_OFF
  58. || this.state === VideoSIPGWConstants.STATE_PENDING
  59. || this.state === VideoSIPGWConstants.STATE_RETRYING) {
  60. logger.warn('Video SIP GW session already started!');
  61. return;
  62. }
  63. this._sendJibriIQ('start');
  64. }
  65. /**
  66. * Changes the state of this session.
  67. *
  68. * @param {string} newState - The new {VideoSIPGWConstants} state to set.
  69. * @param {string} [optional] failureReason - The reason why a failure state
  70. * was entered.
  71. * @returns {void}
  72. */
  73. setState(newState, failureReason) {
  74. if (newState === this.state) {
  75. return;
  76. }
  77. const oldState = this.state;
  78. this.state = newState;
  79. this.eventEmitter.emit(STATE_CHANGED,
  80. {
  81. address: this.sipAddress,
  82. failureReason,
  83. oldState,
  84. newState: this.state,
  85. displayName: this.displayName
  86. }
  87. );
  88. }
  89. /**
  90. * Subscribes the passed listener to the event for state change of this
  91. * session.
  92. *
  93. * @param {Function} listener - The function that will receive the event.
  94. */
  95. addStateListener(listener) {
  96. this.addListener(STATE_CHANGED, listener);
  97. }
  98. /**
  99. * Unsubscribes the passed handler.
  100. *
  101. * @param {Function} listener - The function to be removed.
  102. */
  103. removeStateListener(listener) {
  104. this.removeListener(STATE_CHANGED, listener);
  105. }
  106. /**
  107. * Sends a jibri command using an iq.
  108. *
  109. * @private
  110. * @param {string} action - The action to send ('start' or 'stop').
  111. */
  112. _sendJibriIQ(action) {
  113. const attributes = {
  114. 'xmlns': 'http://jitsi.org/protocol/jibri',
  115. 'action': action,
  116. sipaddress: this.sipAddress
  117. };
  118. attributes.displayname = this.displayName;
  119. const iq = $iq({
  120. to: this.chatRoom.focusMucJid,
  121. type: 'set' })
  122. .c('jibri', attributes)
  123. .up();
  124. logger.debug(`${action} video SIP GW session`, iq.nodeTree);
  125. this.chatRoom.connection.sendIQ(
  126. iq,
  127. () => {}, // eslint-disable-line no-empty-function
  128. error => {
  129. logger.error(
  130. `Failed to ${action} video SIP GW session, error: `, error);
  131. this.setState(VideoSIPGWConstants.STATE_FAILED);
  132. });
  133. }
  134. }