Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BridgeChannel.js 14KB

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