You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WatchRTC.ts 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import Logger from '@jitsi/logger';
  2. import watchRTC from '@testrtc/watchrtc-sdk';
  3. import browser from '../browser';
  4. import { isAnalyticsEnabled, isWatchRTCEnabled } from './functions';
  5. import { IWatchRTCConfiguration } from './interfaces';
  6. const logger = Logger.getLogger(__filename);
  7. /**
  8. * Class that controls the watchRTC flow, because it overwrites and proxies global function it should only be
  9. * initialized once.
  10. */
  11. class WatchRTCHandler {
  12. options?: IWatchRTCConfiguration;
  13. /**
  14. * Initialize watchRTC, it overwrites GUM and PeerConnection global functions and adds a proxy over them
  15. * used to capture stats.
  16. *
  17. * @param {Object} options - Init options.
  18. * @returns {void}
  19. */
  20. init(options: any): void {
  21. if (isWatchRTCEnabled(options)) {
  22. // @ts-ignore
  23. if (browser.isReactNative()) {
  24. logger.warn('Cannot initialize WatchRTC in a react native environment!');
  25. return;
  26. }
  27. if (!isAnalyticsEnabled(options)) {
  28. logger.error('Cannot initialize WatchRTC when analytics or third party requests are disabled.');
  29. return;
  30. }
  31. try {
  32. if (options?.watchRTCConfigParams?.rtcApiKey) {
  33. watchRTC.init({
  34. rtcApiKey: options.watchRTCConfigParams.rtcApiKey,
  35. });
  36. this.options = options.watchRTCConfigParams;
  37. logger.info('WatchRTC initialized.');
  38. } else {
  39. logger.error('WatchRTC is enabled but missing API key.');
  40. }
  41. } catch (error) {
  42. logger.error('Failed to initialize WatchRTC: ', error);
  43. }
  44. }
  45. }
  46. /**
  47. * Begin watchRTC session considering roomName and userName if already not available
  48. *
  49. * @param {string} roomName - The room name we are currently in.
  50. * @param {string} userName - The user name. This value is obtained from
  51. * JitsiConference.prototype.myUserId
  52. * @returns {void}
  53. */
  54. start(roomName: string, userName: string): void {
  55. try {
  56. if (this.options) {
  57. this.options.rtcRoomId = this.options.rtcRoomId ? this.options.rtcRoomId : roomName;
  58. this.options.rtcPeerId = this.options.rtcPeerId ? this.options.rtcPeerId : userName;
  59. watchRTC.setConfig(this.options);
  60. logger.info('WatchRTC setConfig.');
  61. }
  62. } catch (error) {
  63. logger.error('Failed to start WatchRTC session: ', error);
  64. }
  65. }
  66. }
  67. export default new WatchRTCHandler();