modified lib-jitsi-meet dev repo
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BridgeChannel.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. if (this.isOpen()) {
  87. return;
  88. }
  89. this._initWebSocket(this._wsUrl);
  90. timeoutS = Math.min(timeoutS * 2, 60);
  91. this._retryTimeout = setTimeout(reload, timeoutS * 1000);
  92. };
  93. this._retryTimeout = setTimeout(reload, timeoutS * 1000);
  94. }
  95. /**
  96. * Stops the websocket connection retries.
  97. *
  98. * @returns {void}
  99. */
  100. _stopConnectionRetries() {
  101. if (this._retryTimeout) {
  102. clearTimeout(this._retryTimeout);
  103. this._retryTimeout = undefined;
  104. }
  105. }
  106. /**
  107. * Retries to establish the websocket connection after the connection was closed by the server.
  108. *
  109. * @param {CloseEvent} closeEvent - The close event that triggered the retries.
  110. * @returns {void}
  111. */
  112. _retryWebSocketConnection(closeEvent) {
  113. if (!this._areRetriesEnabled) {
  114. return;
  115. }
  116. const { code, reason } = closeEvent;
  117. Statistics.sendAnalytics(createBridgeChannelClosedEvent(code, reason));
  118. this._areRetriesEnabled = false;
  119. this._eventEmitter.once(RTCEvents.DATA_CHANNEL_OPEN, () => {
  120. this._stopConnectionRetries();
  121. this._areRetriesEnabled = true;
  122. });
  123. this._startConnectionRetries();
  124. }
  125. /**
  126. * The channel mode.
  127. * @return {string} "datachannel" or "websocket" (or null if not yet set).
  128. */
  129. get mode() {
  130. return this._mode;
  131. }
  132. /**
  133. * Closes the currently opened channel.
  134. */
  135. close() {
  136. this._closedFromClient = true;
  137. this._stopConnectionRetries();
  138. this._areRetriesEnabled = false;
  139. if (this._channel) {
  140. try {
  141. this._channel.close();
  142. } catch (error) {} // eslint-disable-line no-empty
  143. this._channel = null;
  144. }
  145. }
  146. /**
  147. * Whether there is an underlying RTCDataChannel or WebSocket and it's
  148. * open.
  149. * @return {boolean}
  150. */
  151. isOpen() {
  152. return this._channel && (this._channel.readyState === 'open'
  153. || this._channel.readyState === WebSocket.OPEN);
  154. }
  155. /**
  156. * Sends local stats via the bridge channel.
  157. * @param {Object} payload The payload of the message.
  158. * @throws NetworkError/InvalidStateError/Error if the operation fails or if there is no data channel created.
  159. */
  160. sendEndpointStatsMessage(payload) {
  161. this._send({
  162. colibriClass: 'EndpointStats',
  163. ...payload
  164. });
  165. }
  166. /**
  167. * Sends message via the channel.
  168. * @param {string} to The id of the endpoint that should receive the
  169. * message. If "" the message will be sent to all participants.
  170. * @param {object} payload The payload of the message.
  171. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  172. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  173. * or from WebSocket#send or Error with "No opened channel" message.
  174. */
  175. sendMessage(to, payload) {
  176. this._send({
  177. colibriClass: 'EndpointMessage',
  178. msgPayload: payload,
  179. to
  180. });
  181. }
  182. /**
  183. * Sends a "lastN value changed" message via the channel.
  184. * @param {number} value The new value for lastN. -1 means unlimited.
  185. */
  186. sendSetLastNMessage(value) {
  187. logger.log(`Sending lastN=${value}.`);
  188. this._send({
  189. colibriClass: 'LastNChangedEvent',
  190. lastN: value
  191. });
  192. }
  193. /**
  194. * Sends a "selected endpoints changed" message via the channel.
  195. *
  196. * @param {Array<string>} endpointIds - The ids of the selected endpoints.
  197. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  198. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  199. * or from WebSocket#send or Error with "No opened channel" message.
  200. */
  201. sendSelectedEndpointsMessage(endpointIds) {
  202. logger.log(`Sending selected endpoints: ${endpointIds}.`);
  203. this._send({
  204. colibriClass: 'SelectedEndpointsChangedEvent',
  205. selectedEndpoints: endpointIds
  206. });
  207. }
  208. /**
  209. * Sends a "receiver video constraint" message via the channel.
  210. * @param {Number} maxFrameHeightPixels the maximum frame height,
  211. * in pixels, this receiver is willing to receive
  212. */
  213. sendReceiverVideoConstraintMessage(maxFrameHeightPixels) {
  214. logger.log(`Sending ReceiverVideoConstraint with maxFrameHeight=${maxFrameHeightPixels}px`);
  215. this._send({
  216. colibriClass: 'ReceiverVideoConstraint',
  217. maxFrameHeight: maxFrameHeightPixels
  218. });
  219. }
  220. /**
  221. * Sends a 'ReceiverVideoConstraints' message via the bridge channel.
  222. *
  223. * @param {ReceiverVideoConstraints} constraints video constraints.
  224. */
  225. sendNewReceiverVideoConstraintsMessage(constraints) {
  226. logger.log(`Sending ReceiverVideoConstraints with ${JSON.stringify(constraints)}`);
  227. this._send({
  228. colibriClass: 'ReceiverVideoConstraints',
  229. ...constraints
  230. });
  231. }
  232. /**
  233. * Sends a 'VideoTypeMessage' message via the bridge channel.
  234. *
  235. * @param {string} videoType 'camera', 'desktop' or 'none'.
  236. * @deprecated to be replaced with sendSourceVideoTypeMessage
  237. */
  238. sendVideoTypeMessage(videoType) {
  239. logger.debug(`Sending VideoTypeMessage with video type as ${videoType}`);
  240. this._send({
  241. colibriClass: 'VideoTypeMessage',
  242. videoType
  243. });
  244. }
  245. /**
  246. * Sends a 'VideoTypeMessage' message via the bridge channel.
  247. *
  248. * @param {BridgeVideoType} videoType - the video type.
  249. * @param {SourceName} sourceName - the source name of the video track.
  250. * @returns {void}
  251. */
  252. sendSourceVideoTypeMessage(sourceName, videoType) {
  253. logger.info(`Sending SourceVideoTypeMessage with video type ${sourceName}: ${videoType}`);
  254. this._send({
  255. colibriClass: 'SourceVideoTypeMessage',
  256. sourceName,
  257. videoType
  258. });
  259. }
  260. /**
  261. * Set events on the given RTCDataChannel or WebSocket instance.
  262. */
  263. _handleChannel(channel) {
  264. const emitter = this._eventEmitter;
  265. channel.onopen = () => {
  266. logger.info(`${this._mode} channel opened`);
  267. this._connected = true;
  268. emitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  269. };
  270. channel.onerror = event => {
  271. // WS error events contain no information about the failure (this is available in the onclose event) and
  272. // the event references the WS object itself, which causes hangs on mobile.
  273. if (this._mode !== 'websocket') {
  274. logger.error(`Channel error: ${event.message}`);
  275. }
  276. };
  277. channel.onmessage = ({ data }) => {
  278. // JSON object.
  279. let obj;
  280. try {
  281. obj = JSON.parse(data);
  282. } catch (error) {
  283. GlobalOnErrorHandler.callErrorHandler(error);
  284. logger.error('Failed to parse channel message as JSON: ', data, error);
  285. return;
  286. }
  287. const colibriClass = obj.colibriClass;
  288. switch (colibriClass) {
  289. case 'DominantSpeakerEndpointChangeEvent': {
  290. const { dominantSpeakerEndpoint, previousSpeakers = [], silence } = obj;
  291. logger.debug(`Dominant speaker: ${dominantSpeakerEndpoint}, previous speakers: ${previousSpeakers}`);
  292. emitter.emit(RTCEvents.DOMINANT_SPEAKER_CHANGED, dominantSpeakerEndpoint, previousSpeakers, silence);
  293. break;
  294. }
  295. case 'EndpointConnectivityStatusChangeEvent': {
  296. const endpoint = obj.endpoint;
  297. const isActive = obj.active === 'true';
  298. logger.info(`Endpoint connection status changed: ${endpoint} active=${isActive}`);
  299. emitter.emit(RTCEvents.ENDPOINT_CONN_STATUS_CHANGED, endpoint, isActive);
  300. break;
  301. }
  302. case 'EndpointMessage': {
  303. emitter.emit(RTCEvents.ENDPOINT_MESSAGE_RECEIVED, obj.from, obj.msgPayload);
  304. break;
  305. }
  306. case 'EndpointStats': {
  307. emitter.emit(RTCEvents.ENDPOINT_STATS_RECEIVED, obj.from, obj);
  308. break;
  309. }
  310. case 'ForwardedSources': {
  311. const forwardedSources = obj.forwardedSources;
  312. logger.info(`New forwarded sources: ${forwardedSources}`);
  313. emitter.emit(RTCEvents.FORWARDED_SOURCES_CHANGED, forwardedSources);
  314. break;
  315. }
  316. case 'SenderVideoConstraints': {
  317. const videoConstraints = obj.videoConstraints;
  318. if (videoConstraints) {
  319. logger.info(`SenderVideoConstraints: ${JSON.stringify(videoConstraints)}`);
  320. emitter.emit(RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED, videoConstraints);
  321. }
  322. break;
  323. }
  324. case 'SenderSourceConstraints': {
  325. if (typeof obj.sourceName === 'string' && typeof obj.maxHeight === 'number') {
  326. logger.info(`SenderSourceConstraints: ${obj.sourceName} - ${obj.maxHeight}`);
  327. emitter.emit(RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED, obj);
  328. } else {
  329. logger.error(`Invalid SenderSourceConstraints: ${obj.sourceName} - ${obj.maxHeight}`);
  330. }
  331. break;
  332. }
  333. case 'ServerHello': {
  334. logger.info(`Received ServerHello, version=${obj.version}.`);
  335. break;
  336. }
  337. case 'VideoSourcesMap': {
  338. logger.info(`Received VideoSourcesMap: ${JSON.stringify(obj.mappedSources)}`);
  339. emitter.emit(RTCEvents.VIDEO_SSRCS_REMAPPED, obj);
  340. break;
  341. }
  342. case 'AudioSourcesMap': {
  343. logger.info(`Received AudioSourcesMap: ${JSON.stringify(obj.mappedSources)}`);
  344. emitter.emit(RTCEvents.AUDIO_SSRCS_REMAPPED, obj);
  345. break;
  346. }
  347. default: {
  348. logger.debug('Channel JSON-formatted message: ', obj);
  349. // The received message appears to be appropriately formatted
  350. // (i.e. is a JSON object which assigns a value to the
  351. // mandatory property colibriClass) so don't just swallow it,
  352. // expose it to public consumption.
  353. emitter.emit(`rtc.datachannel.${colibriClass}`, obj);
  354. }
  355. }
  356. };
  357. channel.onclose = event => {
  358. logger.info(`Channel closed by ${this._closedFromClient ? 'client' : 'server'}`);
  359. if (this._mode === 'websocket') {
  360. if (!this._closedFromClient) {
  361. this._retryWebSocketConnection(event);
  362. }
  363. }
  364. if (!this._closedFromClient) {
  365. const { code, reason } = event;
  366. logger.error(`Channel closed: ${code} ${reason}`);
  367. // We only want to send this event the first time the failure happens.
  368. if (typeof this._connected === 'undefined' || this._connected) {
  369. this._connected = false;
  370. emitter.emit(RTCEvents.DATA_CHANNEL_CLOSED, {
  371. code,
  372. reason
  373. });
  374. }
  375. }
  376. // Remove the channel.
  377. this._channel = null;
  378. };
  379. // Store the channel.
  380. this._channel = channel;
  381. }
  382. /**
  383. * Sends passed object via the channel.
  384. * @param {object} jsonObject The object that will be sent.
  385. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  386. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  387. * or from WebSocket#send or Error with "No opened channel" message.
  388. */
  389. _send(jsonObject) {
  390. const channel = this._channel;
  391. if (!this.isOpen()) {
  392. logger.error('Bridge Channel send: no opened channel.');
  393. throw new Error('No opened channel');
  394. }
  395. channel.send(JSON.stringify(jsonObject));
  396. }
  397. }