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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* global $, $iq */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. const logger = getLogger(__filename);
  4. import Listenable from '../util/Listenable';
  5. import * as VideoSIPGWConstants from './VideoSIPGWConstants';
  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. */
  70. setState(newState) {
  71. if (newState === this.state) {
  72. return;
  73. }
  74. const oldState = this.state;
  75. this.state = newState;
  76. this.eventEmitter.emit(this.sipAddress,
  77. {
  78. name: STATE_CHANGED,
  79. oldState,
  80. newState: this.state
  81. }
  82. );
  83. }
  84. /**
  85. * Subscribes the passed listener to the event for state change of this
  86. * session.
  87. *
  88. * @param {Function} listener - The function that will receive the event.
  89. */
  90. addStateListener(listener) {
  91. this.addListener(STATE_CHANGED, listener);
  92. }
  93. /**
  94. * Unsubscribes the passed handler.
  95. *
  96. * @param {Function} listener - The function to be removed.
  97. */
  98. removeStateListener(listener) {
  99. this.removeListener(STATE_CHANGED, listener);
  100. }
  101. /**
  102. * Sends a jibri command using an iq.
  103. *
  104. * @private
  105. * @param {string} action - The action to send ('start' or 'stop').
  106. */
  107. _sendJibriIQ(action) {
  108. const attributes = {
  109. 'xmlns': 'http://jitsi.org/protocol/jibri',
  110. 'action': action,
  111. sipaddress: this.sipAddress
  112. };
  113. attributes.displayname = this.displayName;
  114. const iq = $iq({
  115. to: this.chatRoom.focusMucJid,
  116. type: 'set' })
  117. .c('jibri', attributes)
  118. .up();
  119. logger.log('Stop video SIP GW session', iq.nodeTree);
  120. this.chatRoom.connection.sendIQ(
  121. iq,
  122. result => {
  123. logger.log('Result', result);
  124. const initialState
  125. = $(result).find('jibri')
  126. .attr('state');
  127. this.setState(initialState);
  128. },
  129. error => {
  130. logger.log('Failed to start video SIP GW session, error: ',
  131. error);
  132. this.setState(VideoSIPGWConstants.STATE_FAILED);
  133. });
  134. }
  135. }