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

JitsiVideoSIPGWSession.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* global $ */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import { $iq } from 'strophe.js';
  4. import Listenable from '../util/Listenable';
  5. import * as VideoSIPGWConstants from './VideoSIPGWConstants';
  6. const logger = getLogger(__filename);
  7. /**
  8. * The event name for current sip video session state changed.
  9. * @type {string} event name for sip video session state changed.
  10. */
  11. const STATE_CHANGED = 'STATE_CHANGED';
  12. /**
  13. * Jitsi video SIP GW session. Holding its state and able to start/stop it.
  14. * When session is in OFF or FAILED stated it cannot be used anymore.
  15. */
  16. export default class JitsiVideoSIPGWSession extends Listenable {
  17. /**
  18. * Creates new session with the desired sip address and display name.
  19. *
  20. * @param {string} sipAddress - The sip address to use when
  21. * starting the session.
  22. * @param {string} displayName - The display name to use for
  23. * that participant.
  24. * @param {ChatRoom} chatRoom - The chat room this session is bound to.
  25. */
  26. constructor(sipAddress, displayName, chatRoom) {
  27. super();
  28. this.sipAddress = sipAddress;
  29. this.displayName = displayName;
  30. this.chatRoom = chatRoom;
  31. /*
  32. * The initial state is undefined. Initial state cannot be STATE_OFF,
  33. * the session enters this state when it was in STATE_ON and was stopped
  34. * and such session cannot be used anymore.
  35. *
  36. * @type {VideoSIPGWConstants|undefined}
  37. */
  38. this.state = undefined;
  39. }
  40. /**
  41. * Stops the current session.
  42. */
  43. stop() {
  44. if (this.state === VideoSIPGWConstants.STATE_OFF
  45. || this.state === VideoSIPGWConstants.STATE_FAILED) {
  46. logger.warn('Video SIP GW session already stopped or failed!');
  47. return;
  48. }
  49. this._sendJibriIQ('stop');
  50. }
  51. /**
  52. * Starts a new session. Sends an iq to the focus.
  53. */
  54. start() {
  55. // if state is off, this session was active for some reason
  56. // and we should create new one, rather than reusing it
  57. if (this.state === VideoSIPGWConstants.STATE_ON
  58. || this.state === VideoSIPGWConstants.STATE_OFF
  59. || this.state === VideoSIPGWConstants.STATE_PENDING
  60. || this.state === VideoSIPGWConstants.STATE_RETRYING) {
  61. logger.warn('Video SIP GW session already started!');
  62. return;
  63. }
  64. this._sendJibriIQ('start');
  65. }
  66. /**
  67. * Changes the state of this session.
  68. *
  69. * @param {string} newState - The new {VideoSIPGWConstants} state to set.
  70. */
  71. setState(newState) {
  72. if (newState === this.state) {
  73. return;
  74. }
  75. const oldState = this.state;
  76. this.state = newState;
  77. this.eventEmitter.emit(this.sipAddress,
  78. {
  79. name: STATE_CHANGED,
  80. oldState,
  81. newState: this.state
  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.log('Stop video SIP GW session', iq.nodeTree);
  121. this.chatRoom.connection.sendIQ(
  122. iq,
  123. result => {
  124. logger.log('Result', result);
  125. const initialState
  126. = $(result).find('jibri')
  127. .attr('state');
  128. this.setState(initialState);
  129. },
  130. error => {
  131. logger.log('Failed to start video SIP GW session, error: ',
  132. error);
  133. this.setState(VideoSIPGWConstants.STATE_FAILED);
  134. });
  135. }
  136. }