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

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