Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JitsiVideoSIPGWSession.js 4.4KB

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