1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Add getScreenshareContraints()

This is nicer since we avoid some async functions

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner
2021-03-02 12:58:45 +01:00
parent 5849ea8e63
commit e7562898cd

View File

@@ -178,7 +178,6 @@ enum ConstraintsType {
Audio = "audio", Audio = "audio",
Video = "video", Video = "video",
AudioVideo = "audiovideo", AudioVideo = "audiovideo",
Screenshare = "screenshare",
} }
/** /**
@@ -335,10 +334,10 @@ export class MatrixCall extends EventEmitter {
* Place a voice call to this room. * Place a voice call to this room.
* @throws If you have not specified a listener for 'error' events. * @throws If you have not specified a listener for 'error' events.
*/ */
async placeVoiceCall() { placeVoiceCall() {
logger.debug("placeVoiceCall"); logger.debug("placeVoiceCall");
this.checkForErrorListener(); this.checkForErrorListener();
const constraints = await getUserMediaContraints(ConstraintsType.Audio); const constraints = getUserMediaContraints(ConstraintsType.Audio);
this.placeCallWithConstraints(constraints); this.placeCallWithConstraints(constraints);
this.type = CallType.Voice; this.type = CallType.Voice;
} }
@@ -351,12 +350,12 @@ export class MatrixCall extends EventEmitter {
* to render the local camera preview. * to render the local camera preview.
* @throws If you have not specified a listener for 'error' events. * @throws If you have not specified a listener for 'error' events.
*/ */
async placeVideoCall(remoteVideoElement: HTMLVideoElement, localVideoElement: HTMLVideoElement) { placeVideoCall(remoteVideoElement: HTMLVideoElement, localVideoElement: HTMLVideoElement) {
logger.debug("placeVideoCall"); logger.debug("placeVideoCall");
this.checkForErrorListener(); this.checkForErrorListener();
this.localVideoElement = localVideoElement; this.localVideoElement = localVideoElement;
this.remoteVideoElement = remoteVideoElement; this.remoteVideoElement = remoteVideoElement;
const constraints = await getUserMediaContraints(ConstraintsType.AudioVideo); const constraints = getUserMediaContraints(ConstraintsType.AudioVideo);
this.placeCallWithConstraints(constraints); this.placeCallWithConstraints(constraints);
this.type = CallType.Video; this.type = CallType.Video;
} }
@@ -382,10 +381,7 @@ export class MatrixCall extends EventEmitter {
this.remoteVideoElement = remoteVideoElement; this.remoteVideoElement = remoteVideoElement;
try { try {
const screenshareConstraints = await getUserMediaContraints( const screenshareConstraints = await getScreenshareContraints();
ConstraintsType.Screenshare,
selectDesktopCapturerSource,
);
if (window.electron?.getDesktopCapturerSources) { if (window.electron?.getDesktopCapturerSources) {
// We are using Electron // We are using Electron
logger.debug("Getting screen stream using getUserMedia()..."); logger.debug("Getting screen stream using getUserMedia()...");
@@ -397,7 +393,7 @@ export class MatrixCall extends EventEmitter {
} }
logger.debug("Got screen stream, requesting audio stream..."); logger.debug("Got screen stream, requesting audio stream...");
const audioConstraints = await getUserMediaContraints(ConstraintsType.Audio); const audioConstraints = getUserMediaContraints(ConstraintsType.Audio);
this.placeCallWithConstraints(audioConstraints); this.placeCallWithConstraints(audioConstraints);
} catch (err) { } catch (err) {
this.emit(CallEvent.Error, this.emit(CallEvent.Error,
@@ -598,7 +594,7 @@ export class MatrixCall extends EventEmitter {
logger.debug(`Answering call ${this.callId} of type ${this.type}`); logger.debug(`Answering call ${this.callId} of type ${this.type}`);
if (!this.localAVStream && !this.waitForLocalAVStream) { if (!this.localAVStream && !this.waitForLocalAVStream) {
const constraints = await getUserMediaContraints( const constraints = getUserMediaContraints(
this.type == CallType.Video ? this.type == CallType.Video ?
ConstraintsType.AudioVideo: ConstraintsType.AudioVideo:
ConstraintsType.Audio, ConstraintsType.Audio,
@@ -1704,10 +1700,7 @@ function setTracksEnabled(tracks: Array<MediaStreamTrack>, enabled: boolean) {
} }
} }
async function getUserMediaContraints( function getUserMediaContraints(type: ConstraintsType) {
type: ConstraintsType,
selectDesktopCapturerSource?: () => Promise<DesktopCapturerSource>,
) {
const isWebkit = !!navigator.webkitGetUserMedia; const isWebkit = !!navigator.webkitGetUserMedia;
switch (type) { switch (type) {
@@ -1750,31 +1743,32 @@ async function getUserMediaContraints(
}, },
}; };
} }
case ConstraintsType.Screenshare: { }
if (window.electron?.getDesktopCapturerSources && selectDesktopCapturerSource) { }
// We have access to getDesktopCapturerSources()
logger.debug("Electron getDesktopCapturerSources() is available..."); async function getScreenshareContraints(selectDesktopCapturerSource?: () => Promise<DesktopCapturerSource>) {
const selectedSource = await selectDesktopCapturerSource(); if (window.electron?.getDesktopCapturerSources && selectDesktopCapturerSource) {
if (!selectedSource) return null; // We have access to getDesktopCapturerSources()
return { logger.debug("Electron getDesktopCapturerSources() is available...");
audio: false, const selectedSource = await selectDesktopCapturerSource();
video: { if (!selectedSource) return null;
mandatory: { return {
chromeMediaSource: "desktop", audio: false,
chromeMediaSourceId: selectedSource.id, video: {
}, mandatory: {
}, chromeMediaSource: "desktop",
}; chromeMediaSourceId: selectedSource.id,
} else { },
// We do not have access to the Electron desktop capturer, },
// therefore we can assume we are on the web };
logger.debug("Electron desktopCapturer is not available..."); } else {
return { // We do not have access to the Electron desktop capturer,
audio: false, // therefore we can assume we are on the web
video: true, logger.debug("Electron desktopCapturer is not available...");
}; return {
} audio: false,
} video: true,
};
} }
} }