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

BridgeChannel.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import RTCEvents from '../../service/RTC/RTCEvents';
  3. import { createBridgeChannelClosedEvent } from '../../service/statistics/AnalyticsEvents';
  4. import Statistics from '../statistics/statistics';
  5. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  6. const logger = getLogger(__filename);
  7. /**
  8. * Handles a WebRTC RTCPeerConnection or a WebSocket instance to communicate
  9. * with the videobridge.
  10. */
  11. export default class BridgeChannel {
  12. /**
  13. * Binds "ondatachannel" event listener on the given RTCPeerConnection
  14. * instance, or creates a WebSocket connection with the videobridge.
  15. * At least one of both, peerconnection or wsUrl parameters, must be
  16. * given.
  17. * @param {RTCPeerConnection} [peerconnection] WebRTC peer connection
  18. * instance.
  19. * @param {string} [wsUrl] WebSocket URL.
  20. * @param {EventEmitter} emitter the EventEmitter instance to use for event emission.
  21. */
  22. constructor(peerconnection, wsUrl, emitter) {
  23. if (!peerconnection && !wsUrl) {
  24. throw new TypeError('At least peerconnection or wsUrl must be given');
  25. } else if (peerconnection && wsUrl) {
  26. throw new TypeError('Just one of peerconnection or wsUrl must be given');
  27. }
  28. if (peerconnection) {
  29. logger.debug('constructor() with peerconnection');
  30. } else {
  31. logger.debug(`constructor() with wsUrl:"${wsUrl}"`);
  32. }
  33. // The underlying WebRTC RTCDataChannel or WebSocket instance.
  34. // @type {RTCDataChannel|WebSocket}
  35. this._channel = null;
  36. // @type {EventEmitter}
  37. this._eventEmitter = emitter;
  38. // Whether a RTCDataChannel or WebSocket is internally used.
  39. // @type {string} "datachannel" / "websocket"
  40. this._mode = null;
  41. // Indicates whether the connection retries are enabled or not.
  42. this._areRetriesEnabled = false;
  43. // Indicates whether the connection was closed from the client or not.
  44. this._closedFromClient = false;
  45. // If a RTCPeerConnection is given, listen for new RTCDataChannel
  46. // event.
  47. if (peerconnection) {
  48. const datachannel
  49. = peerconnection.createDataChannel(
  50. 'JVB data channel', {
  51. protocol: 'http://jitsi.org/protocols/colibri'
  52. });
  53. // Handle the RTCDataChannel.
  54. this._handleChannel(datachannel);
  55. this._mode = 'datachannel';
  56. // Otherwise create a WebSocket connection.
  57. } else if (wsUrl) {
  58. this._areRetriesEnabled = true;
  59. this._wsUrl = wsUrl;
  60. this._initWebSocket();
  61. }
  62. }
  63. /**
  64. * Initializes the web socket channel.
  65. *
  66. * @returns {void}
  67. */
  68. _initWebSocket() {
  69. // Create a WebSocket instance.
  70. const ws = new WebSocket(this._wsUrl);
  71. // Handle the WebSocket.
  72. this._handleChannel(ws);
  73. this._mode = 'websocket';
  74. }
  75. /**
  76. * Starts the websocket connection retries.
  77. *
  78. * @returns {void}
  79. */
  80. _startConnectionRetries() {
  81. let timeoutS = 1;
  82. const reload = () => {
  83. if (this.isOpen()) {
  84. return;
  85. }
  86. this._initWebSocket(this._wsUrl);
  87. timeoutS = Math.min(timeoutS * 2, 60);
  88. this._retryTimeout = setTimeout(reload, timeoutS * 1000);
  89. };
  90. this._retryTimeout = setTimeout(reload, timeoutS * 1000);
  91. }
  92. /**
  93. * Stops the websocket connection retries.
  94. *
  95. * @returns {void}
  96. */
  97. _stopConnectionRetries() {
  98. if (this._retryTimeout) {
  99. clearTimeout(this._retryTimeout);
  100. this._retryTimeout = undefined;
  101. }
  102. }
  103. /**
  104. * Retries to establish the websocket connection after the connection was closed by the server.
  105. *
  106. * @param {CloseEvent} closeEvent - The close event that triggered the retries.
  107. * @returns {void}
  108. */
  109. _retryWebSocketConnection(closeEvent) {
  110. if (!this._areRetriesEnabled) {
  111. return;
  112. }
  113. const { code, reason } = closeEvent;
  114. Statistics.sendAnalytics(createBridgeChannelClosedEvent(code, reason));
  115. this._areRetriesEnabled = false;
  116. this._eventEmitter.once(RTCEvents.DATA_CHANNEL_OPEN, () => {
  117. this._stopConnectionRetries();
  118. this._areRetriesEnabled = true;
  119. });
  120. this._startConnectionRetries();
  121. }
  122. /**
  123. * The channel mode.
  124. * @return {string} "datachannel" or "websocket" (or null if not yet set).
  125. */
  126. get mode() {
  127. return this._mode;
  128. }
  129. /**
  130. * Closes the currently opened channel.
  131. */
  132. close() {
  133. this._closedFromClient = true;
  134. this._stopConnectionRetries();
  135. this._areRetriesEnabled = false;
  136. if (this._channel) {
  137. try {
  138. this._channel.close();
  139. } catch (error) {} // eslint-disable-line no-empty
  140. this._channel = null;
  141. }
  142. }
  143. /**
  144. * Whether there is an underlying RTCDataChannel or WebSocket and it's
  145. * open.
  146. * @return {boolean}
  147. */
  148. isOpen() {
  149. return this._channel && (this._channel.readyState === 'open'
  150. || this._channel.readyState === WebSocket.OPEN);
  151. }
  152. /**
  153. * Sends local stats via the bridge channel.
  154. * @param {Object} payload The payload of the message.
  155. * @throws NetworkError/InvalidStateError/Error if the operation fails or if there is no data channel created.
  156. */
  157. sendEndpointStatsMessage(payload) {
  158. logger.debug(`Sending endpoint stats ${JSON.stringify(payload)}`);
  159. this._send({
  160. colibriClass: 'EndpointStats',
  161. ...payload
  162. });
  163. }
  164. /**
  165. * Sends message via the channel.
  166. * @param {string} to The id of the endpoint that should receive the
  167. * message. If "" the message will be sent to all participants.
  168. * @param {object} payload The payload of the message.
  169. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  170. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  171. * or from WebSocket#send or Error with "No opened channel" message.
  172. */
  173. sendMessage(to, payload) {
  174. this._send({
  175. colibriClass: 'EndpointMessage',
  176. msgPayload: payload,
  177. to
  178. });
  179. }
  180. /**
  181. * Sends a "lastN value changed" message via the channel.
  182. * @param {number} value The new value for lastN. -1 means unlimited.
  183. */
  184. sendSetLastNMessage(value) {
  185. logger.log(`Sending lastN=${value}.`);
  186. this._send({
  187. colibriClass: 'LastNChangedEvent',
  188. lastN: value
  189. });
  190. }
  191. /**
  192. * Sends a "selected endpoints changed" message via the channel.
  193. *
  194. * @param {Array<string>} endpointIds - The ids of the selected endpoints.
  195. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  196. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  197. * or from WebSocket#send or Error with "No opened channel" message.
  198. */
  199. sendSelectedEndpointsMessage(endpointIds) {
  200. logger.log(`Sending selected endpoints: ${endpointIds}.`);
  201. this._send({
  202. colibriClass: 'SelectedEndpointsChangedEvent',
  203. selectedEndpoints: endpointIds
  204. });
  205. }
  206. /**
  207. * Sends a "receiver video constraint" message via the channel.
  208. * @param {Number} maxFrameHeightPixels the maximum frame height,
  209. * in pixels, this receiver is willing to receive
  210. */
  211. sendReceiverVideoConstraintMessage(maxFrameHeightPixels) {
  212. logger.log(`Sending ReceiverVideoConstraint with maxFrameHeight=${maxFrameHeightPixels}px`);
  213. this._send({
  214. colibriClass: 'ReceiverVideoConstraint',
  215. maxFrameHeight: maxFrameHeightPixels
  216. });
  217. }
  218. /**
  219. * Sends a 'ReceiverVideoConstraints' message via the bridge channel.
  220. *
  221. * @param {ReceiverVideoConstraints} constraints video constraints.
  222. */
  223. sendNewReceiverVideoConstraintsMessage(constraints) {
  224. logger.log(`Sending ReceiverVideoConstraints with ${JSON.stringify(constraints)}`);
  225. this._send({
  226. colibriClass: 'ReceiverVideoConstraints',
  227. ...constraints
  228. });
  229. }
  230. /**
  231. * Set events on the given RTCDataChannel or WebSocket instance.
  232. */
  233. _handleChannel(channel) {
  234. const emitter = this._eventEmitter;
  235. channel.onopen = () => {
  236. logger.info(`${this._mode} channel opened`);
  237. // Code sample for sending string and/or binary data.
  238. // Sends string message to the bridge:
  239. // channel.send("Hello bridge!");
  240. // Sends 12 bytes binary message to the bridge:
  241. // channel.send(new ArrayBuffer(12));
  242. emitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  243. };
  244. channel.onerror = event => {
  245. // WS error events contain no information about the failure (this is available in the onclose event) and
  246. // the event references the WS object itself, which causes hangs on mobile.
  247. if (this._mode !== 'websocket') {
  248. logger.error(`Channel error: ${event.message}`);
  249. }
  250. };
  251. channel.onmessage = ({ data }) => {
  252. // JSON object.
  253. let obj;
  254. try {
  255. obj = JSON.parse(data);
  256. } catch (error) {
  257. GlobalOnErrorHandler.callErrorHandler(error);
  258. logger.error('Failed to parse channel message as JSON: ', data, error);
  259. return;
  260. }
  261. const colibriClass = obj.colibriClass;
  262. switch (colibriClass) {
  263. case 'DominantSpeakerEndpointChangeEvent': {
  264. const { dominantSpeakerEndpoint, previousSpeakers = [] } = obj;
  265. logger.debug(`Dominant speaker: ${dominantSpeakerEndpoint}, previous speakers: ${previousSpeakers}`);
  266. emitter.emit(RTCEvents.DOMINANT_SPEAKER_CHANGED, dominantSpeakerEndpoint, previousSpeakers);
  267. break;
  268. }
  269. case 'EndpointConnectivityStatusChangeEvent': {
  270. const endpoint = obj.endpoint;
  271. const isActive = obj.active === 'true';
  272. logger.info(`Endpoint connection status changed: ${endpoint} active=${isActive}`);
  273. emitter.emit(RTCEvents.ENDPOINT_CONN_STATUS_CHANGED, endpoint, isActive);
  274. break;
  275. }
  276. case 'EndpointMessage': {
  277. emitter.emit(RTCEvents.ENDPOINT_MESSAGE_RECEIVED, obj.from, obj.msgPayload);
  278. break;
  279. }
  280. case 'EndpointStats': {
  281. emitter.emit(RTCEvents.ENDPOINT_STATS_RECEIVED, obj.from, obj);
  282. break;
  283. }
  284. case 'LastNEndpointsChangeEvent': {
  285. // The new/latest list of last-n endpoint IDs (i.e. endpoints for which the bridge is sending video).
  286. const lastNEndpoints = obj.lastNEndpoints;
  287. logger.info(`New forwarded endpoints: ${lastNEndpoints}`);
  288. emitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED, lastNEndpoints);
  289. break;
  290. }
  291. case 'SenderVideoConstraints': {
  292. const videoConstraints = obj.videoConstraints;
  293. if (videoConstraints) {
  294. logger.info(`SenderVideoConstraints: ${JSON.stringify(videoConstraints)}`);
  295. emitter.emit(RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED, videoConstraints);
  296. }
  297. break;
  298. }
  299. case 'ServerHello': {
  300. logger.info(`Received ServerHello, version=${obj.version}.`);
  301. break;
  302. }
  303. default: {
  304. logger.debug('Channel JSON-formatted message: ', obj);
  305. // The received message appears to be appropriately formatted
  306. // (i.e. is a JSON object which assigns a value to the
  307. // mandatory property colibriClass) so don't just swallow it,
  308. // expose it to public consumption.
  309. emitter.emit(`rtc.datachannel.${colibriClass}`, obj);
  310. }
  311. }
  312. };
  313. channel.onclose = event => {
  314. logger.info(`Channel closed by ${this._closedFromClient ? 'client' : 'server'}`);
  315. if (this._mode === 'websocket') {
  316. if (!this._closedFromClient) {
  317. logger.error(`Channel closed: ${event.code} ${event.reason}`);
  318. this._retryWebSocketConnection(event);
  319. }
  320. }
  321. // Remove the channel.
  322. this._channel = null;
  323. };
  324. // Store the channel.
  325. this._channel = channel;
  326. }
  327. /**
  328. * Sends passed object via the channel.
  329. * @param {object} jsonObject The object that will be sent.
  330. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  331. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  332. * or from WebSocket#send or Error with "No opened channel" message.
  333. */
  334. _send(jsonObject) {
  335. const channel = this._channel;
  336. if (!this.isOpen()) {
  337. logger.error('Bridge Channel send: no opened channel.');
  338. throw new Error('No opened channel');
  339. }
  340. channel.send(JSON.stringify(jsonObject));
  341. }
  342. }