Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PB-3099] feat: remove deferred class usage from OlmAdapter #8

Open
wants to merge 1 commit into
base: feature/PB-2667-add-pq-layer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ declare global {
RTCRtpScriptTransform: Window.RTCRtpScriptTransform;
onrtctransform: Window.onrtctransform;
}

declare class RTCRtpScriptTransform {
constructor(worker: Worker, options?: any);
}
}
3 changes: 0 additions & 3 deletions modules/browser/BrowserCapabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const FROZEN_MACOS_VERSION = '10.15.7';
* Implements browser capabilities for lib-jitsi-meet.
*/
export default class BrowserCapabilities extends BrowserDetection {
isWebKitBased() {
throw new Error('Method not implemented.');
}
/**
* Tells whether or not the <tt>MediaStream/tt> is removed from the <tt>PeerConnection</tt> and disposed on video
* mute (in order to turn off the camera device). This is needed on Firefox because of the following bug
Expand Down
117 changes: 79 additions & 38 deletions modules/e2ee/E2EEContext.js → modules/e2ee/E2EEContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
/* global RTCRtpScriptTransform */
import { getLogger } from '@jitsi/logger';
import { getLogger } from "@jitsi/logger";

// Extend the RTCRtpReceiver interface due to lack of support of streams
interface CustomRTCRtpReceiver extends RTCRtpReceiver {
createEncodedStreams?: () => {
readable: ReadableStream;
writable: WritableStream;
};
transform: RTCRtpScriptTransform;
}

interface CustomRTCRtpSender extends RTCRtpSender {
createEncodedStreams?: () => {
readable: ReadableStream;
writable: WritableStream;
};
transform: RTCRtpScriptTransform;
}

const logger = getLogger(__filename);

// Flag to set on senders / receivers to avoid setting up the encryption transform
// more than once.
const kJitsiE2EE = Symbol('kJitsiE2EE');
const kJitsiE2EE = Symbol("kJitsiE2EE");

/**
* Context encapsulating the cryptography bits required for E2EE.
Expand All @@ -20,18 +37,20 @@ const kJitsiE2EE = Symbol('kJitsiE2EE');
* - allow for the key to be rotated frequently.
*/
export default class E2EEcontext {
// private _worker: Worker;
private _worker: Worker;
/**
* Build a new E2EE context instance, which will be used in a given conference.
*/
constructor() {
// Determine the URL for the worker script. Relative URLs are relative to
// the entry point, not the script that launches the worker.
let baseUrl = '';
const ljm = document.querySelector('script[src*="lib-jitsi-meet"]');// as HTMLImageElement;
let baseUrl = "";
const ljm = document.querySelector<HTMLImageElement>(
'script[src*="lib-jitsi-meet"]'
); // as HTMLImageElement;

if (ljm) {
const idx = ljm.src.lastIndexOf('/');
const idx = ljm.src.lastIndexOf("/");

baseUrl = `${ljm.src.substring(0, idx)}/`;
}
Expand All @@ -41,19 +60,19 @@ export default class E2EEcontext {
// If there is no baseUrl then we create the worker in a normal way
// as you cant load scripts inside blobs from relative paths.
// See: https://www.html5rocks.com/en/tutorials/workers/basics/#toc-inlineworkers-loadingscripts
if (baseUrl && baseUrl !== '/') {
if (baseUrl && baseUrl !== "/") {
// Initialize the E2EE worker. In order to avoid CORS issues, start the worker and have it
// synchronously load the JS.
const workerBlob
= new Blob([ `importScripts("${workerUrl}");` ], { type: 'application/javascript' });
const workerBlob = new Blob([`importScripts("${workerUrl}");`], {
type: "application/javascript",
});

workerUrl = window.URL.createObjectURL(workerBlob);
}

this._worker = new Worker(workerUrl, { name: 'E2EE Worker' });

this._worker.onerror = e => logger.error(e);
this._worker = new Worker(workerUrl, { name: "E2EE Worker" });

this._worker.onerror = (e) => logger.error(e);
}

/**
Expand All @@ -62,10 +81,10 @@ export default class E2EEcontext {
*
* @param {string} participantId - The participant that just left.
*/
cleanup(participantId) {
cleanup(participantId: string) {
this._worker.postMessage({
operation: 'cleanup',
participantId
operation: "cleanup",
participantId,
});
}

Expand All @@ -75,7 +94,7 @@ export default class E2EEcontext {
*/
cleanupAll() {
this._worker.postMessage({
operation: 'cleanupAll'
operation: "cleanupAll",
});
}

Expand All @@ -87,28 +106,38 @@ export default class E2EEcontext {
* @param {string} kind - The kind of track this receiver belongs to.
* @param {string} participantId - The participant id that this receiver belongs to.
*/
handleReceiver(receiver, kind, participantId) {
handleReceiver(
receiver: CustomRTCRtpReceiver,
kind: string,
participantId: string
) {
if (receiver[kJitsiE2EE]) {
return;
}
receiver[kJitsiE2EE] = true;

if (window.RTCRtpScriptTransform) {
const options = {
operation: 'decode',
participantId
operation: "decode",
participantId,
};

receiver.transform = new RTCRtpScriptTransform(this._worker, options);
receiver.transform = new RTCRtpScriptTransform(
this._worker,
options
);
} else {
const receiverStreams = receiver.createEncodedStreams();

this._worker.postMessage({
operation: 'decode',
readableStream: receiverStreams.readable,
writableStream: receiverStreams.writable,
participantId
}, [ receiverStreams.readable, receiverStreams.writable ]);
this._worker.postMessage(
{
operation: "decode",
readableStream: receiverStreams.readable,
writableStream: receiverStreams.writable,
participantId,
},
[receiverStreams.readable, receiverStreams.writable]
);
}
}

Expand All @@ -120,28 +149,35 @@ export default class E2EEcontext {
* @param {string} kind - The kind of track this sender belongs to.
* @param {string} participantId - The participant id that this sender belongs to.
*/
handleSender(sender, kind, participantId) {
handleSender(
sender: CustomRTCRtpSender,
kind: string,
participantId: string
) {
if (sender[kJitsiE2EE]) {
return;
}
sender[kJitsiE2EE] = true;

if (window.RTCRtpScriptTransform) {
const options = {
operation: 'encode',
participantId
operation: "encode",
participantId,
};

sender.transform = new RTCRtpScriptTransform(this._worker, options);
} else {
const senderStreams = sender.createEncodedStreams();

this._worker.postMessage({
operation: 'encode',
readableStream: senderStreams.readable,
writableStream: senderStreams.writable,
participantId
}, [ senderStreams.readable, senderStreams.writable ]);
this._worker.postMessage(
{
operation: "encode",
readableStream: senderStreams.readable,
writableStream: senderStreams.writable,
participantId,
},
[senderStreams.readable, senderStreams.writable]
);
}
}

Expand All @@ -153,13 +189,18 @@ export default class E2EEcontext {
* @param {Uint8Array} pqKey - olm key for the given participant.
* @param {Number} keyIndex - the key index.
*/
setKey(participantId, olmKey, pqKey, index) {
setKey(
participantId: string,
olmKey: Uint8Array,
pqKey: Uint8Array,
index: Number
) {
this._worker.postMessage({
operation: 'setKey',
operation: "setKey",
olmKey,
pqKey,
index,
participantId
participantId,
});
}
}
4 changes: 1 addition & 3 deletions modules/e2ee/E2EEncryption.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import base64js from "base64-js";

import browser from "../browser";

import { ManagedKeyHandler } from "./ManagedKeyHandler";
Expand Down Expand Up @@ -53,7 +51,7 @@ export class E2EEncryption {
* @param {boolean} enabled - whether E2EE should be enabled or not.
* @returns {void}
*/
async setEnabled(enabled) {
async setEnabled(enabled: boolean): Promise<void> {
await this._keyHandler.setEnabled(enabled);
}

Expand Down
Loading