Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BridgeChannel.js 15KB

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