modified lib-jitsi-meet dev repo
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BridgeChannel.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 "selected endpoints changed" message via the channel.
  204. *
  205. * @param {Array<string>} endpointIds - The ids of the selected endpoints.
  206. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  207. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  208. * or from WebSocket#send or Error with "No opened channel" message.
  209. */
  210. sendSelectedEndpointsMessage(endpointIds) {
  211. logger.log(`Sending selected endpoints: ${endpointIds}.`);
  212. this._send({
  213. colibriClass: 'SelectedEndpointsChangedEvent',
  214. selectedEndpoints: endpointIds
  215. });
  216. }
  217. /**
  218. * Sends a "receiver video constraint" message via the channel.
  219. * @param {Number} maxFrameHeightPixels the maximum frame height,
  220. * in pixels, this receiver is willing to receive
  221. */
  222. sendReceiverVideoConstraintMessage(maxFrameHeightPixels) {
  223. logger.log(`Sending ReceiverVideoConstraint with maxFrameHeight=${maxFrameHeightPixels}px`);
  224. this._send({
  225. colibriClass: 'ReceiverVideoConstraint',
  226. maxFrameHeight: maxFrameHeightPixels
  227. });
  228. }
  229. /**
  230. * Sends a 'ReceiverVideoConstraints' message via the bridge channel.
  231. *
  232. * @param {ReceiverVideoConstraints} constraints video constraints.
  233. */
  234. sendNewReceiverVideoConstraintsMessage(constraints) {
  235. logger.log(`Sending ReceiverVideoConstraints with ${JSON.stringify(constraints)}`);
  236. this._send({
  237. colibriClass: 'ReceiverVideoConstraints',
  238. ...constraints
  239. });
  240. }
  241. /**
  242. * Sends a 'VideoTypeMessage' message via the bridge channel.
  243. *
  244. * @param {string} videoType 'camera', 'desktop' or 'none'.
  245. * @deprecated to be replaced with sendSourceVideoTypeMessage
  246. */
  247. sendVideoTypeMessage(videoType) {
  248. logger.debug(`Sending VideoTypeMessage with video type as ${videoType}`);
  249. this._send({
  250. colibriClass: 'VideoTypeMessage',
  251. videoType
  252. });
  253. }
  254. /**
  255. * Sends a 'VideoTypeMessage' message via the bridge channel.
  256. *
  257. * @param {BridgeVideoType} videoType - the video type.
  258. * @param {SourceName} sourceName - the source name of the video track.
  259. * @returns {void}
  260. */
  261. sendSourceVideoTypeMessage(sourceName, videoType) {
  262. logger.info(`Sending SourceVideoTypeMessage with video type ${sourceName}: ${videoType}`);
  263. this._send({
  264. colibriClass: 'SourceVideoTypeMessage',
  265. sourceName,
  266. videoType
  267. });
  268. }
  269. /**
  270. * Set events on the given RTCDataChannel or WebSocket instance.
  271. */
  272. _handleChannel(channel) {
  273. const emitter = this._eventEmitter;
  274. channel.onopen = () => {
  275. logger.info(`${this._mode} channel opened`);
  276. this._connected = true;
  277. emitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  278. };
  279. channel.onerror = event => {
  280. // WS error events contain no information about the failure (this is available in the onclose event) and
  281. // the event references the WS object itself, which causes hangs on mobile.
  282. if (this._mode !== 'websocket') {
  283. logger.error(`Channel error: ${event.message}`);
  284. }
  285. };
  286. channel.onmessage = ({ data }) => {
  287. // JSON object.
  288. let obj;
  289. try {
  290. obj = JSON.parse(data);
  291. } catch (error) {
  292. GlobalOnErrorHandler.callErrorHandler(error);
  293. logger.error('Failed to parse channel message as JSON: ', data, error);
  294. return;
  295. }
  296. const colibriClass = obj.colibriClass;
  297. switch (colibriClass) {
  298. case 'DominantSpeakerEndpointChangeEvent': {
  299. const { dominantSpeakerEndpoint, previousSpeakers = [], silence } = obj;
  300. logger.debug(`Dominant speaker: ${dominantSpeakerEndpoint}, previous speakers: ${previousSpeakers}`);
  301. emitter.emit(RTCEvents.DOMINANT_SPEAKER_CHANGED, dominantSpeakerEndpoint, previousSpeakers, silence);
  302. break;
  303. }
  304. case 'EndpointConnectivityStatusChangeEvent': {
  305. const endpoint = obj.endpoint;
  306. const isActive = obj.active === 'true';
  307. logger.info(`Endpoint connection status changed: ${endpoint} active=${isActive}`);
  308. emitter.emit(RTCEvents.ENDPOINT_CONN_STATUS_CHANGED, endpoint, isActive);
  309. break;
  310. }
  311. case 'EndpointMessage': {
  312. emitter.emit(RTCEvents.ENDPOINT_MESSAGE_RECEIVED, obj.from, obj.msgPayload);
  313. break;
  314. }
  315. case 'EndpointStats': {
  316. emitter.emit(RTCEvents.ENDPOINT_STATS_RECEIVED, obj.from, obj);
  317. break;
  318. }
  319. case 'ForwardedSources': {
  320. const forwardedSources = obj.forwardedSources;
  321. logger.info(`New forwarded sources: ${forwardedSources}`);
  322. emitter.emit(RTCEvents.FORWARDED_SOURCES_CHANGED, forwardedSources);
  323. break;
  324. }
  325. case 'SenderVideoConstraints': {
  326. const videoConstraints = obj.videoConstraints;
  327. if (videoConstraints) {
  328. logger.info(`SenderVideoConstraints: ${JSON.stringify(videoConstraints)}`);
  329. emitter.emit(RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED, videoConstraints);
  330. }
  331. break;
  332. }
  333. case 'SenderSourceConstraints': {
  334. if (typeof obj.sourceName === 'string' && typeof obj.maxHeight === 'number') {
  335. logger.info(`SenderSourceConstraints: ${obj.sourceName} - ${obj.maxHeight}`);
  336. emitter.emit(RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED, obj);
  337. } else {
  338. logger.error(`Invalid SenderSourceConstraints: ${obj.sourceName} - ${obj.maxHeight}`);
  339. }
  340. break;
  341. }
  342. case 'ServerHello': {
  343. logger.info(`Received ServerHello, version=${obj.version}.`);
  344. break;
  345. }
  346. case 'VideoSourcesMap': {
  347. logger.info(`Received VideoSourcesMap: ${JSON.stringify(obj.mappedSources)}`);
  348. emitter.emit(RTCEvents.VIDEO_SSRCS_REMAPPED, obj);
  349. break;
  350. }
  351. case 'AudioSourcesMap': {
  352. logger.info(`Received AudioSourcesMap: ${JSON.stringify(obj.mappedSources)}`);
  353. emitter.emit(RTCEvents.AUDIO_SSRCS_REMAPPED, obj);
  354. break;
  355. }
  356. default: {
  357. logger.debug('Channel JSON-formatted message: ', obj);
  358. // The received message appears to be appropriately formatted
  359. // (i.e. is a JSON object which assigns a value to the
  360. // mandatory property colibriClass) so don't just swallow it,
  361. // expose it to public consumption.
  362. emitter.emit(`rtc.datachannel.${colibriClass}`, obj);
  363. }
  364. }
  365. };
  366. channel.onclose = event => {
  367. logger.debug(`Channel closed by ${this._closedFromClient ? 'client' : 'server'}`);
  368. if (channel !== this._channel) {
  369. logger.debug('Skip close handler, channel instance is not equal to stored one');
  370. return;
  371. }
  372. // When the JVB closes the connection gracefully due to the participant being alone in
  373. // the meeting it uses code 1001, so treat that as a graceful close and don't say
  374. // anything.
  375. const isGracefulClose = this._closedFromClient || event.code === 1001;
  376. if (!isGracefulClose) {
  377. const { code, reason } = event;
  378. logger.error(`Channel closed: ${code} ${reason}`);
  379. if (this._mode === 'websocket') {
  380. this._retryWebSocketConnection(event);
  381. }
  382. // We only want to send this event the first time the failure happens.
  383. if (this._connected !== false) {
  384. emitter.emit(RTCEvents.DATA_CHANNEL_CLOSED, {
  385. code,
  386. reason
  387. });
  388. }
  389. }
  390. this._connected = false;
  391. // Remove the channel.
  392. this._channel = null;
  393. };
  394. // Store the channel.
  395. this._channel = channel;
  396. }
  397. /**
  398. * Sends passed object via the channel.
  399. * @param {object} jsonObject The object that will be sent.
  400. * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
  401. * {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
  402. * or from WebSocket#send or Error with "No opened channel" message.
  403. */
  404. _send(jsonObject) {
  405. const channel = this._channel;
  406. if (!this.isOpen()) {
  407. logger.error('Bridge Channel send: no opened channel.');
  408. throw new Error('No opened channel');
  409. }
  410. channel.send(JSON.stringify(jsonObject));
  411. }
  412. }