You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-01 04:43:29 +03:00
Merge pull request #1878 from SimonBrandner/task/i-hate-my-code
This commit is contained in:
20
src/@types/global.d.ts
vendored
20
src/@types/global.d.ts
vendored
@@ -33,14 +33,9 @@ declare global {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Window {
|
interface Window {
|
||||||
electron?: Electron;
|
|
||||||
webkitAudioContext: typeof AudioContext;
|
webkitAudioContext: typeof AudioContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Electron {
|
|
||||||
getDesktopCapturerSources(options: GetSourcesOptions): Promise<Array<DesktopCapturerSource>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Crypto {
|
interface Crypto {
|
||||||
webkitSubtle?: Window["crypto"]["subtle"];
|
webkitSubtle?: Window["crypto"]["subtle"];
|
||||||
}
|
}
|
||||||
@@ -67,21 +62,6 @@ declare global {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DesktopCapturerSource {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
thumbnailURL: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface GetSourcesOptions {
|
|
||||||
types: Array<string>;
|
|
||||||
thumbnailSize?: {
|
|
||||||
height: number;
|
|
||||||
width: number;
|
|
||||||
};
|
|
||||||
fetchWindowIcons?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface HTMLAudioElement {
|
interface HTMLAudioElement {
|
||||||
// sinkId & setSinkId are experimental and typescript doesn't know about them
|
// sinkId & setSinkId are experimental and typescript doesn't know about them
|
||||||
sinkId: string;
|
sinkId: string;
|
||||||
|
|||||||
@@ -228,21 +228,6 @@ const FALLBACK_ICE_SERVER = 'stun:turn.matrix.org';
|
|||||||
/** The length of time a call can be ringing for. */
|
/** The length of time a call can be ringing for. */
|
||||||
const CALL_TIMEOUT_MS = 60000;
|
const CALL_TIMEOUT_MS = 60000;
|
||||||
|
|
||||||
/** Retrieves sources from desktopCapturer */
|
|
||||||
export function getDesktopCapturerSources(): Promise<Array<DesktopCapturerSource>> {
|
|
||||||
const options: GetSourcesOptions = {
|
|
||||||
thumbnailSize: {
|
|
||||||
height: 176,
|
|
||||||
width: 312,
|
|
||||||
},
|
|
||||||
types: [
|
|
||||||
"screen",
|
|
||||||
"window",
|
|
||||||
],
|
|
||||||
};
|
|
||||||
return window.electron.getDesktopCapturerSources(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
export class CallError extends Error {
|
export class CallError extends Error {
|
||||||
code: string;
|
code: string;
|
||||||
|
|
||||||
@@ -789,12 +774,11 @@ export class MatrixCall extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Starts/stops screensharing
|
* Starts/stops screensharing
|
||||||
* @param enabled the desired screensharing state
|
* @param enabled the desired screensharing state
|
||||||
* @param selectDesktopCapturerSource callBack to select a screensharing stream on desktop
|
* @param {string} desktopCapturerSourceId optional id of the desktop capturer source to use
|
||||||
* @returns {boolean} new screensharing state
|
* @returns {boolean} new screensharing state
|
||||||
*/
|
*/
|
||||||
public async setScreensharingEnabled(
|
public async setScreensharingEnabled(
|
||||||
enabled: boolean,
|
enabled: boolean, desktopCapturerSourceId?: string,
|
||||||
selectDesktopCapturerSource?: () => Promise<DesktopCapturerSource>,
|
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
// Skip if there is nothing to do
|
// Skip if there is nothing to do
|
||||||
if (enabled && this.isScreensharing()) {
|
if (enabled && this.isScreensharing()) {
|
||||||
@@ -807,13 +791,13 @@ export class MatrixCall extends EventEmitter {
|
|||||||
|
|
||||||
// Fallback to replaceTrack()
|
// Fallback to replaceTrack()
|
||||||
if (!this.opponentSupportsSDPStreamMetadata()) {
|
if (!this.opponentSupportsSDPStreamMetadata()) {
|
||||||
return await this.setScreensharingEnabledWithoutMetadataSupport(enabled, selectDesktopCapturerSource);
|
return await this.setScreensharingEnabledWithoutMetadataSupport(enabled, desktopCapturerSourceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug(`Set screensharing enabled? ${enabled}`);
|
logger.debug(`Set screensharing enabled? ${enabled}`);
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
try {
|
try {
|
||||||
const stream = await getScreensharingStream(selectDesktopCapturerSource);
|
const stream = await getScreensharingStream(desktopCapturerSourceId);
|
||||||
if (!stream) return false;
|
if (!stream) return false;
|
||||||
this.pushLocalFeed(stream, SDPStreamMetadataPurpose.Screenshare);
|
this.pushLocalFeed(stream, SDPStreamMetadataPurpose.Screenshare);
|
||||||
return true;
|
return true;
|
||||||
@@ -839,17 +823,16 @@ export class MatrixCall extends EventEmitter {
|
|||||||
* Starts/stops screensharing
|
* Starts/stops screensharing
|
||||||
* Should be used ONLY if the opponent doesn't support SDPStreamMetadata
|
* Should be used ONLY if the opponent doesn't support SDPStreamMetadata
|
||||||
* @param enabled the desired screensharing state
|
* @param enabled the desired screensharing state
|
||||||
* @param selectDesktopCapturerSource callBack to select a screensharing stream on desktop
|
* @param {string} desktopCapturerSourceId optional id of the desktop capturer source to use
|
||||||
* @returns {boolean} new screensharing state
|
* @returns {boolean} new screensharing state
|
||||||
*/
|
*/
|
||||||
private async setScreensharingEnabledWithoutMetadataSupport(
|
private async setScreensharingEnabledWithoutMetadataSupport(
|
||||||
enabled: boolean,
|
enabled: boolean, desktopCapturerSourceId?: string,
|
||||||
selectDesktopCapturerSource?: () => Promise<DesktopCapturerSource>,
|
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
logger.debug(`Set screensharing enabled? ${enabled} using replaceTrack()`);
|
logger.debug(`Set screensharing enabled? ${enabled} using replaceTrack()`);
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
try {
|
try {
|
||||||
const stream = await getScreensharingStream(selectDesktopCapturerSource);
|
const stream = await getScreensharingStream(desktopCapturerSourceId);
|
||||||
if (!stream) return false;
|
if (!stream) return false;
|
||||||
|
|
||||||
const track = stream.getTracks().find((track) => {
|
const track = stream.getTracks().find((track) => {
|
||||||
@@ -1939,13 +1922,11 @@ export class MatrixCall extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getScreensharingStream(
|
async function getScreensharingStream(desktopCapturerSourceId: string): Promise<MediaStream> {
|
||||||
selectDesktopCapturerSource?: () => Promise<DesktopCapturerSource>,
|
const screenshareConstraints = getScreenshareContraints(desktopCapturerSourceId);
|
||||||
): Promise<MediaStream> {
|
|
||||||
const screenshareConstraints = await getScreenshareContraints(selectDesktopCapturerSource);
|
|
||||||
if (!screenshareConstraints) return null;
|
if (!screenshareConstraints) return null;
|
||||||
|
|
||||||
if (window.electron?.getDesktopCapturerSources) {
|
if (desktopCapturerSourceId) {
|
||||||
// We are using Electron
|
// We are using Electron
|
||||||
logger.debug("Getting screen stream using getUserMedia()...");
|
logger.debug("Getting screen stream using getUserMedia()...");
|
||||||
return await navigator.mediaDevices.getUserMedia(screenshareConstraints);
|
return await navigator.mediaDevices.getUserMedia(screenshareConstraints);
|
||||||
@@ -1993,27 +1974,20 @@ function getUserMediaContraints(type: ConstraintsType): MediaStreamConstraints {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getScreenshareContraints(
|
function getScreenshareContraints(desktopCapturerSourceId?: string): DesktopCapturerConstraints {
|
||||||
selectDesktopCapturerSource?: () => Promise<DesktopCapturerSource>,
|
if (desktopCapturerSourceId) {
|
||||||
): Promise<DesktopCapturerConstraints> {
|
logger.debug("Using desktop capturer source", desktopCapturerSourceId);
|
||||||
if (window.electron?.getDesktopCapturerSources && selectDesktopCapturerSource) {
|
|
||||||
// We have access to getDesktopCapturerSources()
|
|
||||||
logger.debug("Electron getDesktopCapturerSources() is available...");
|
|
||||||
const selectedSource = await selectDesktopCapturerSource();
|
|
||||||
if (!selectedSource) return null;
|
|
||||||
return {
|
return {
|
||||||
audio: false,
|
audio: false,
|
||||||
video: {
|
video: {
|
||||||
mandatory: {
|
mandatory: {
|
||||||
chromeMediaSource: "desktop",
|
chromeMediaSource: "desktop",
|
||||||
chromeMediaSourceId: selectedSource.id,
|
chromeMediaSourceId: desktopCapturerSourceId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// We do not have access to the Electron desktop capturer,
|
logger.debug("Not using desktop capturer source");
|
||||||
// therefore we can assume we are on the web
|
|
||||||
logger.debug("Electron desktopCapturer is not available...");
|
|
||||||
return {
|
return {
|
||||||
audio: false,
|
audio: false,
|
||||||
video: true,
|
video: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user