modified lib-jitsi-meet dev repo
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.

E2EEContext.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. const logger = getLogger(__filename);
  4. // Flag to set on senders / receivers to avoid setting up the encryption transform
  5. // more than once.
  6. const kJitsiE2EE = Symbol('kJitsiE2EE');
  7. /**
  8. * Context encapsulating the cryptography bits required for E2EE.
  9. * This uses the WebRTC Insertable Streams API which is explained in
  10. * https://github.com/alvestrand/webrtc-media-streams/blob/master/explainer.md
  11. * that provides access to the encoded frames and allows them to be transformed.
  12. *
  13. * The encoded frame format is explained below in the _encodeFunction method.
  14. * High level design goals were:
  15. * - do not require changes to existing SFUs and retain (VP8) metadata.
  16. * - allow the SFU to rewrite SSRCs, timestamp, pictureId.
  17. * - allow for the key to be rotated frequently.
  18. */
  19. export default class E2EEcontext {
  20. /**
  21. * Build a new E2EE context instance, which will be used in a given conference.
  22. *
  23. * @param {string} options.salt - Salt to be used for key deviation.
  24. * FIXME: We currently use the MUC room name for this which has the same lifetime
  25. * as this context. While not (pseudo)random as recommended in
  26. * https://developer.mozilla.org/en-US/docs/Web/API/Pbkdf2Params
  27. * this is easily available and the same for all participants.
  28. * We currently do not enforce a minimum length of 16 bytes either.
  29. */
  30. constructor(options) {
  31. this._options = options;
  32. // Figure out the URL for the worker script. Relative URLs are relative to
  33. // the entry point, not the script that launches the worker.
  34. let baseUrl = '';
  35. const ljm = document.querySelector('script[src*="lib-jitsi-meet"]');
  36. if (ljm) {
  37. const idx = ljm.src.lastIndexOf('/');
  38. baseUrl = `${ljm.src.substring(0, idx)}/`;
  39. }
  40. // Initialize the E2EE worker.
  41. this._worker = new Worker(`${baseUrl}lib-jitsi-meet.e2ee-worker.js`, { name: 'E2EE Worker' });
  42. this._worker.onerror = e => logger.onerror(e);
  43. // Initialize the salt and convert it once.
  44. const encoder = new TextEncoder();
  45. // Send initial options to worker.
  46. this._worker.postMessage({
  47. operation: 'initialize',
  48. salt: encoder.encode(options.salt)
  49. });
  50. }
  51. /**
  52. * Handles the given {@code RTCRtpReceiver} by creating a {@code TransformStream} which will inject
  53. * a frame decoder.
  54. *
  55. * @param {RTCRtpReceiver} receiver - The receiver which will get the decoding function injected.
  56. * @param {string} kind - The kind of track this receiver belongs to.
  57. * @param {string} participantId - The participant id that this receiver belongs to.
  58. */
  59. handleReceiver(receiver, kind, participantId) {
  60. if (receiver[kJitsiE2EE]) {
  61. return;
  62. }
  63. receiver[kJitsiE2EE] = true;
  64. let receiverStreams;
  65. if (receiver.createEncodedStreams) {
  66. receiverStreams = receiver.createEncodedStreams();
  67. } else {
  68. receiverStreams = kind === 'video' ? receiver.createEncodedVideoStreams()
  69. : receiver.createEncodedAudioStreams();
  70. }
  71. this._worker.postMessage({
  72. operation: 'decode',
  73. readableStream: receiverStreams.readable || receiverStreams.readableStream,
  74. writableStream: receiverStreams.writable || receiverStreams.writableStream,
  75. participantId
  76. }, [ receiverStreams.readable || receiverStreams.readableStream,
  77. receiverStreams.writable || receiverStreams.writableStream ]);
  78. }
  79. /**
  80. * Handles the given {@code RTCRtpSender} by creating a {@code TransformStream} which will inject
  81. * a frame encoder.
  82. *
  83. * @param {RTCRtpSender} sender - The sender which will get the encoding function injected.
  84. * @param {string} kind - The kind of track this sender belongs to.
  85. * @param {string} participantId - The participant id that this sender belongs to.
  86. */
  87. handleSender(sender, kind, participantId) {
  88. if (sender[kJitsiE2EE]) {
  89. return;
  90. }
  91. sender[kJitsiE2EE] = true;
  92. let senderStreams;
  93. if (sender.createEncodedStreams) {
  94. senderStreams = sender.createEncodedStreams();
  95. } else {
  96. senderStreams = kind === 'video' ? sender.createEncodedVideoStreams()
  97. : sender.createEncodedAudioStreams();
  98. }
  99. this._worker.postMessage({
  100. operation: 'encode',
  101. readableStream: senderStreams.readable || senderStreams.readableStream,
  102. writableStream: senderStreams.writable || senderStreams.writableStream,
  103. participantId
  104. }, [ senderStreams.readable || senderStreams.readableStream,
  105. senderStreams.writable || senderStreams.writableStream ]);
  106. }
  107. /**
  108. * Sets the key to be used for E2EE.
  109. *
  110. * @param {string} value - Value to be used as the new key. May be falsy to disable end-to-end encryption.
  111. */
  112. setKey(value) {
  113. let key;
  114. if (value) {
  115. const encoder = new TextEncoder();
  116. key = encoder.encode(value);
  117. } else {
  118. key = false;
  119. }
  120. this._worker.postMessage({
  121. operation: 'setKey',
  122. key
  123. });
  124. }
  125. }