您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

E2EEContext.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Determine 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. In order to avoid CORS issues, start the worker and have it
  41. // synchronously load the JS.
  42. const workerUrl = `${baseUrl}lib-jitsi-meet.e2ee-worker.js`;
  43. const workerBlob
  44. = new Blob([ `importScripts("${workerUrl}");` ], { type: 'application/javascript' });
  45. const blobUrl = window.URL.createObjectURL(workerBlob);
  46. this._worker = new Worker(blobUrl, { name: 'E2EE Worker' });
  47. this._worker.onerror = e => logger.onerror(e);
  48. }
  49. /**
  50. * Cleans up all state associated with the given participant. This is needed when a
  51. * participant leaves the current conference.
  52. *
  53. * @param {string} participantId - The participant that just left.
  54. */
  55. cleanup(participantId) {
  56. this._worker.postMessage({
  57. operation: 'cleanup',
  58. participantId
  59. });
  60. }
  61. /**
  62. * Handles the given {@code RTCRtpReceiver} by creating a {@code TransformStream} which will inject
  63. * a frame decoder.
  64. *
  65. * @param {RTCRtpReceiver} receiver - The receiver which will get the decoding function injected.
  66. * @param {string} kind - The kind of track this receiver belongs to.
  67. * @param {string} participantId - The participant id that this receiver belongs to.
  68. */
  69. handleReceiver(receiver, kind, participantId) {
  70. if (receiver[kJitsiE2EE]) {
  71. return;
  72. }
  73. receiver[kJitsiE2EE] = true;
  74. let receiverStreams;
  75. if (receiver.createEncodedStreams) {
  76. receiverStreams = receiver.createEncodedStreams();
  77. } else {
  78. receiverStreams = kind === 'video' ? receiver.createEncodedVideoStreams()
  79. : receiver.createEncodedAudioStreams();
  80. }
  81. this._worker.postMessage({
  82. operation: 'decode',
  83. readableStream: receiverStreams.readable || receiverStreams.readableStream,
  84. writableStream: receiverStreams.writable || receiverStreams.writableStream,
  85. participantId
  86. }, [ receiverStreams.readable || receiverStreams.readableStream,
  87. receiverStreams.writable || receiverStreams.writableStream ]);
  88. }
  89. /**
  90. * Handles the given {@code RTCRtpSender} by creating a {@code TransformStream} which will inject
  91. * a frame encoder.
  92. *
  93. * @param {RTCRtpSender} sender - The sender which will get the encoding function injected.
  94. * @param {string} kind - The kind of track this sender belongs to.
  95. * @param {string} participantId - The participant id that this sender belongs to.
  96. */
  97. handleSender(sender, kind, participantId) {
  98. if (sender[kJitsiE2EE]) {
  99. return;
  100. }
  101. sender[kJitsiE2EE] = true;
  102. let senderStreams;
  103. if (sender.createEncodedStreams) {
  104. senderStreams = sender.createEncodedStreams();
  105. } else {
  106. senderStreams = kind === 'video' ? sender.createEncodedVideoStreams()
  107. : sender.createEncodedAudioStreams();
  108. }
  109. this._worker.postMessage({
  110. operation: 'encode',
  111. readableStream: senderStreams.readable || senderStreams.readableStream,
  112. writableStream: senderStreams.writable || senderStreams.writableStream,
  113. participantId
  114. }, [ senderStreams.readable || senderStreams.readableStream,
  115. senderStreams.writable || senderStreams.writableStream ]);
  116. }
  117. /**
  118. * Set the E2EE key for the specified participant.
  119. *
  120. * @param {string} participantId - the ID of the participant who's key we are setting.
  121. * @param {Uint8Array | boolean} key - they key for the given participant.
  122. * @param {Number} keyIndex - the key index.
  123. */
  124. setKey(participantId, key, keyIndex) {
  125. this._worker.postMessage({
  126. operation: 'setKey',
  127. participantId,
  128. key,
  129. keyIndex
  130. });
  131. }
  132. }